Skip to content

Support NonNull pointers as asm! input operands - #165008

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
hsanzg:rich-asm-ptr-inputs
Nov 22, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
hsanzg:rich-asm-ptr-inputs

Conversation

@hsanzg

@hsanzg hsanzg commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

Tracking issue: #166132

Currently the asm! macro supports raw (thin) pointers as operands, and references as input operands. As discussed in Zulip, it seems reasonable to extend support to fundamental types with the same ABI as raw pointers. This commit adds support for (thin) NonNull pointer inputs. The reason for not allowing NonNull outputs is that the asm! code has no way to explicitly assert that the safety invariant of NonNull is preserved.

Support for Box<T> and Optional<NonNull<T>> seems a bit more debatable, so that's left as future work (which might require more than a FCP).

I'll make the existing error messages dependent on whether the operand is allowed specifically as an input in a subsequent PR (because it also affects references). Right now the message is a bit too general.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 26, 2026
@rustbot

rustbot commented Oct 26, 2026

Copy link
Copy Markdown
Collaborator

r? @davidtwco

rustbot has assigned @davidtwco.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 77 candidates
  • Random selection from 22 candidates

@hsanzg
hsanzg force-pushed the rich-asm-ptr-inputs branch from efa94c9 to 61b2e57 Compare October 26, 2026 22:23
@workingjubilee

Copy link
Copy Markdown
Member

re: that Zulip discussion, Amanieu was critical of this idea in its unqualified form and as our de facto expert in inline asm! for "wrote the code" reasons, I think the specifically input-operand-only version seems... plausible? but should first get a look from

r? @Amanieu

@rustbot rustbot assigned Amanieu and unassigned davidtwco Oct 27, 2026
@Amanieu

Amanieu commented Oct 29, 2026

Copy link
Copy Markdown
Member

LGTM, but this needs a positive codegen test. The current tests all fail and never reach codegen.

@hsanzg
hsanzg force-pushed the rich-asm-ptr-inputs branch from 61b2e57 to 8250453 Compare November 1, 2026 18:51
@rustbot rustbot added the A-test-infra-minicore Area: `minicore` test auxiliary and `//@ add-minicore` label Nov 1, 2026
@hsanzg

hsanzg commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Added codegen tests to all the [arch]-types.rs files that had existing raw pointer tests. I had to modify the check! macros a bit to support the $in_ty:$out_ty pattern; hopefully that's ok.

@rust-log-analyzer

This comment has been minimized.

@hsanzg
hsanzg force-pushed the rich-asm-ptr-inputs branch from 8250453 to fd4fdd7 Compare November 1, 2026 21:14
@workingjubilee

Copy link
Copy Markdown
Member

This will need a PR to update the reference re: input types, especially if we have no feature gate for this and it is instantly stable.

@workingjubilee workingjubilee added the needs-reference-pr This language change needs an approved Reference PR to proceed. label Nov 1, 2026
@workingjubilee

workingjubilee commented Nov 1, 2026 •

Copy link
Copy Markdown
Member

that or it needs to be an unstable feature. I don't particularly know what is preferred here on that front. Seems trivial to me.

hsanzg added a commit to hsanzg/rust-reference that referenced this pull request Nov 2, 2026
Shared and mutable references as `asm!` input operands were introduced
at least before 8ab0f2d3c5a85563b98c4896116e3d53154fff9c; `MaybeUninit`
was introduced in rust-lang/rust#114790;
`NonNull` is awaiting stabilization in
rust-lang/rust#165008.
hsanzg added a commit to hsanzg/rust-reference that referenced this pull request Nov 2, 2026
Shared and mutable references as `asm!` input operands were introduced
at least before 8ab0f2d3c5a85563b98c4896116e3d53154fff9c; `MaybeUninit`
was introduced in rust-lang/rust#114790;
`NonNull` is awaiting stabilization in
rust-lang/rust#165008.
hsanzg added a commit to hsanzg/rust-reference that referenced this pull request Nov 2, 2026
Shared and mutable references as `asm!` input operands were introduced
at least before 8ab0f2d3c5a85563b98c4896116e3d53154fff9c; `MaybeUninit`
was introduced in rust-lang/rust#114790;
`NonNull` is awaiting stabilization in
rust-lang/rust#165008.
hsanzg added a commit to hsanzg/rust-reference that referenced this pull request Nov 2, 2026
Shared and mutable references as `asm!` input operands were introduced
at least before 8ab0f2d3c5a85563b98c4896116e3d53154fff9c; `MaybeUninit`
was introduced in rust-lang/rust#114790;
`NonNull` is awaiting stabilization in
rust-lang/rust#165008.
@Amanieu

Amanieu commented Nov 16, 2026

Copy link
Copy Markdown
Member

Nominating for lang since this is a new capability for inline asm.

@Amanieu Amanieu added the I-lang-nominated Nominated for discussion during a lang team meeting. label Nov 16, 2026
@Amanieu

Amanieu commented Nov 16, 2026

Copy link
Copy Markdown
Member

@hsanzg Could you add an unstable feature gate for this so this isn't instantly stable? In general we are adverse to instantly stabilizing new features unless we really have to.

@Amanieu Amanieu added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 16, 2026
@hsanzg hsanzg mentioned this pull request Nov 17, 2026
2 of 4 tasks
hsanzg added a commit to hsanzg/rust-reference that referenced this pull request Nov 17, 2026
Shared and mutable references as `asm!` input operands were introduced
at least before 8ab0f2d3c5a85563b98c4896116e3d53154fff9c; `MaybeUninit`
was introduced in rust-lang/rust#114790;
`NonNull` is awaiting stabilization in
rust-lang/rust#165008.
@hsanzg
hsanzg force-pushed the rich-asm-ptr-inputs branch from fd4fdd7 to 7ade73b Compare November 17, 2026 22:14
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 18, 2026
@Amanieu

Amanieu commented Nov 21, 2026

Copy link
Copy Markdown
Member

@bors r+

@rust-bors

rust-bors Bot commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 6c11872 has been approved by Amanieu

It is now in the queue for this repository.

🌲 The tree is currently closed for pull requests below priority 100. This pull request will be tested once the tree is reopened.

Reason for tree closure: repeated failures in dist-x86_64-msvc

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 21, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 21, 2026
Support `NonNull` pointers as `asm!` input operands

Tracking issue: rust-lang#166132

Currently the `asm!` macro supports raw (thin) pointers as operands, and references as input operands. As discussed in [Zulip](https://rust-lang.zulipchat.com/#narrow/channel/213817-t-lang/topic/.60NonNull.60s.20as.20register.20operand.20values.20of.20asm.21/with/629100221), it seems reasonable to extend support to fundamental types with the same ABI as raw pointers. This commit adds support for (thin) `NonNull` pointer inputs. The reason for not allowing `NonNull` _outputs_ is that the `asm!` code has no way to explicitly assert that the safety invariant of `NonNull` is preserved.

Support for `Box<T>` and `Optional<NonNull<T>>` seems a bit more debatable, so that's left as future work (which might require more than a FCP).
rust-bors Bot pushed a commit that referenced this pull request Nov 22, 2026
…uwer

Rollup of 14 pull requests

Successful merges:

 - #166266 (miri subtree update)
 - #164878 (Don't require `extern "llvm-intrinsic"` return structs to be `#[repr(packed)]`)
 - #165008 (Support `NonNull` pointers as `asm!` input operands)
 - #165892 (`rustc_builtin_macros` cleanup, part 10)
 - #165930 (Add red-green mark when dumping dep graph. Show DefIds if available)
 - #166280 (std: also mark doctests with skip_unsupported_miri_tests)
 - #166295 (use `cfg!(overflow_checks)` in `overflow_checks` intrinsic instead of `cfg!(debug_assertions)`)
 - #165618 (Implement RangeIntersect on core::range types)
 - #166012 (Added more queries to AdtDef in rustc_public to check for rust language types)
 - #166142 (Lower to `hir::Param`s for extern functions)
 - #166259 (fmt: Improve `Debug` for `FormattingOptions`)
 - #166269 (libcore: Revert some comment changes)
 - #166287 (Use `core::ptr` in `implicit_provenance_casts` help for `no_std` crates)
 - #166306 (bootstrap: Stop passing `-Ztrim-paths` to cargo now that it's stable in beta)
@Zalathar

Copy link
Copy Markdown
Member

@bors try jobs=test-x86_64-rust-for-linux

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Nov 22, 2026
Support `NonNull` pointers as `asm!` input operands


try-job: test-x86_64-rust-for-linux
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 22, 2026
Support `NonNull` pointers as `asm!` input operands

Tracking issue: rust-lang#166132

Currently the `asm!` macro supports raw (thin) pointers as operands, and references as input operands. As discussed in [Zulip](https://rust-lang.zulipchat.com/#narrow/channel/213817-t-lang/topic/.60NonNull.60s.20as.20register.20operand.20values.20of.20asm.21/with/629100221), it seems reasonable to extend support to fundamental types with the same ABI as raw pointers. This commit adds support for (thin) `NonNull` pointer inputs. The reason for not allowing `NonNull` _outputs_ is that the `asm!` code has no way to explicitly assert that the safety invariant of `NonNull` is preserved.

Support for `Box<T>` and `Optional<NonNull<T>>` seems a bit more debatable, so that's left as future work (which might require more than a FCP).
@rust-bors

rust-bors Bot commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: ea3b344 (ea3b344632522ff9b6d4e2f8eca548738a1d7838)
Base parent: 342d744 (342d74463af7dd3018ef6aa04dd5500c2deb5bd1)

rust-bors Bot pushed a commit that referenced this pull request Nov 22, 2026
Rollup of 15 pull requests

Successful merges:

 - #166266 (miri subtree update)
 - #165720 (windows: remove redundant prefix parsing in `PathBuf::_push`)
 - #164878 (Don't require `extern "llvm-intrinsic"` return structs to be `#[repr(packed)]`)
 - #165008 (Support `NonNull` pointers as `asm!` input operands)
 - #165892 (`rustc_builtin_macros` cleanup, part 10)
 - #164485 (Stabilize unchecked `nonzero_ops`)
 - #165618 (Implement RangeIntersect on core::range types)
 - #165833 (Deallocate with the right provenance on Windows)
 - #165834 (Implement tidy alphabetical --bless on Windows)
 - #165889 (Fix docs of alloc fn: Allocator API stabilized)
 - #165923 (Don't load/store a LLVM aggregate type when returning an array from a generic SIMD intrinsic)
 - #166012 (Added more queries to AdtDef in rustc_public to check for rust language types)
 - #166216 (Stop using #![no_core] in profiler_builtins)
 - #166259 (fmt: Improve `Debug` for `FormattingOptions`)
 - #166263 (Show slice split results for empty and singleton slices)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 22, 2026
Support `NonNull` pointers as `asm!` input operands

Tracking issue: rust-lang#166132

Currently the `asm!` macro supports raw (thin) pointers as operands, and references as input operands. As discussed in [Zulip](https://rust-lang.zulipchat.com/#narrow/channel/213817-t-lang/topic/.60NonNull.60s.20as.20register.20operand.20values.20of.20asm.21/with/629100221), it seems reasonable to extend support to fundamental types with the same ABI as raw pointers. This commit adds support for (thin) `NonNull` pointer inputs. The reason for not allowing `NonNull` _outputs_ is that the `asm!` code has no way to explicitly assert that the safety invariant of `NonNull` is preserved.

Support for `Box<T>` and `Optional<NonNull<T>>` seems a bit more debatable, so that's left as future work (which might require more than a FCP).
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 22, 2026
Support `NonNull` pointers as `asm!` input operands

Tracking issue: rust-lang#166132

Currently the `asm!` macro supports raw (thin) pointers as operands, and references as input operands. As discussed in [Zulip](https://rust-lang.zulipchat.com/#narrow/channel/213817-t-lang/topic/.60NonNull.60s.20as.20register.20operand.20values.20of.20asm.21/with/629100221), it seems reasonable to extend support to fundamental types with the same ABI as raw pointers. This commit adds support for (thin) `NonNull` pointer inputs. The reason for not allowing `NonNull` _outputs_ is that the `asm!` code has no way to explicitly assert that the safety invariant of `NonNull` is preserved.

Support for `Box<T>` and `Optional<NonNull<T>>` seems a bit more debatable, so that's left as future work (which might require more than a FCP).
rust-bors Bot pushed a commit that referenced this pull request Nov 22, 2026
…uwer

Rollup of 19 pull requests

Successful merges:

 - #166266 (miri subtree update)
 - #164878 (Don't require `extern "llvm-intrinsic"` return structs to be `#[repr(packed)]`)
 - #165008 (Support `NonNull` pointers as `asm!` input operands)
 - #165892 (`rustc_builtin_macros` cleanup, part 10)
 - #165930 (Add red-green mark when dumping dep graph. Show DefIds if available)
 - #166280 (std: also mark doctests with skip_unsupported_miri_tests)
 - #166295 (use `cfg!(overflow_checks)` in `overflow_checks` intrinsic instead of `cfg!(debug_assertions)`)
 - #164485 (Stabilize unchecked `nonzero_ops`)
 - #165618 (Implement RangeIntersect on core::range types)
 - #165782 (Make unsafe unknown diagnostic attributes an error again)
 - #165833 (Deallocate with the right provenance on Windows)
 - #165834 (Implement tidy alphabetical --bless on Windows)
 - #165889 (Fix docs of alloc fn: Allocator API stabilized)
 - #165923 (Don't load/store a LLVM aggregate type when returning an array from a generic SIMD intrinsic)
 - #166012 (Added more queries to AdtDef in rustc_public to check for rust language types)
 - #166186 (OpenOptions::append: clarify docs)
 - #166216 (Stop using #![no_core] in profiler_builtins)
 - #166259 (fmt: Improve `Debug` for `FormattingOptions`)
 - #166263 (Show slice split results for empty and singleton slices)
rust-bors Bot pushed a commit that referenced this pull request Nov 22, 2026
…uwer

Rollup of 18 pull requests

Successful merges:

 - #164878 (Don't require `extern "llvm-intrinsic"` return structs to be `#[repr(packed)]`)
 - #165008 (Support `NonNull` pointers as `asm!` input operands)
 - #165892 (`rustc_builtin_macros` cleanup, part 10)
 - #165930 (Add red-green mark when dumping dep graph. Show DefIds if available)
 - #166280 (std: also mark doctests with skip_unsupported_miri_tests)
 - #166295 (use `cfg!(overflow_checks)` in `overflow_checks` intrinsic instead of `cfg!(debug_assertions)`)
 - #164485 (Stabilize unchecked `nonzero_ops`)
 - #165618 (Implement RangeIntersect on core::range types)
 - #165782 (Make unsafe unknown diagnostic attributes an error again)
 - #165833 (Deallocate with the right provenance on Windows)
 - #165834 (Implement tidy alphabetical --bless on Windows)
 - #165889 (Fix docs of alloc fn: Allocator API stabilized)
 - #165923 (Don't load/store a LLVM aggregate type when returning an array from a generic SIMD intrinsic)
 - #166012 (Added more queries to AdtDef in rustc_public to check for rust language types)
 - #166186 (OpenOptions::append: clarify docs)
 - #166216 (Stop using #![no_core] in profiler_builtins)
 - #166259 (fmt: Improve `Debug` for `FormattingOptions`)
 - #166263 (Show slice split results for empty and singleton slices)
rust-bors Bot pushed a commit that referenced this pull request Nov 22, 2026
…uwer

Rollup of 18 pull requests

Successful merges:

 - #164878 (Don't require `extern "llvm-intrinsic"` return structs to be `#[repr(packed)]`)
 - #165008 (Support `NonNull` pointers as `asm!` input operands)
 - #165892 (`rustc_builtin_macros` cleanup, part 10)
 - #165930 (Add red-green mark when dumping dep graph. Show DefIds if available)
 - #166280 (std: also mark doctests with skip_unsupported_miri_tests)
 - #166295 (use `cfg!(overflow_checks)` in `overflow_checks` intrinsic instead of `cfg!(debug_assertions)`)
 - #164485 (Stabilize unchecked `nonzero_ops`)
 - #165618 (Implement RangeIntersect on core::range types)
 - #165782 (Make unsafe unknown diagnostic attributes an error again)
 - #165833 (Deallocate with the right provenance on Windows)
 - #165834 (Implement tidy alphabetical --bless on Windows)
 - #165889 (Fix docs of alloc fn: Allocator API stabilized)
 - #165923 (Don't load/store a LLVM aggregate type when returning an array from a generic SIMD intrinsic)
 - #166012 (Added more queries to AdtDef in rustc_public to check for rust language types)
 - #166186 (OpenOptions::append: clarify docs)
 - #166216 (Stop using #![no_core] in profiler_builtins)
 - #166259 (fmt: Improve `Debug` for `FormattingOptions`)
 - #166263 (Show slice split results for empty and singleton slices)
@rust-bors
rust-bors Bot merged commit f5f9151 into rust-lang:main Nov 22, 2026
15 checks passed
@rustbot rustbot added this to the 1.102.0 milestone Nov 22, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 22, 2026
Rollup merge of #165008 - hsanzg:rich-asm-ptr-inputs, r=Amanieu

Support `NonNull` pointers as `asm!` input operands

Tracking issue: #166132

Currently the `asm!` macro supports raw (thin) pointers as operands, and references as input operands. As discussed in [Zulip](https://rust-lang.zulipchat.com/#narrow/channel/213817-t-lang/topic/.60NonNull.60s.20as.20register.20operand.20values.20of.20asm.21/with/629100221), it seems reasonable to extend support to fundamental types with the same ABI as raw pointers. This commit adds support for (thin) `NonNull` pointer inputs. The reason for not allowing `NonNull` _outputs_ is that the `asm!` code has no way to explicitly assert that the safety invariant of `NonNull` is preserved.

Support for `Box<T>` and `Optional<NonNull<T>>` seems a bit more debatable, so that's left as future work (which might require more than a FCP).
@rust-timer

Copy link
Copy Markdown
Collaborator

Note

This PR was benchmarked as part of triage of its containing rollup: triage URL.

Finished benchmarking commit (4963997): comparison URL.

Overall result: ❌✅ regressions and improvements - please read:

Our benchmarks found a performance regression caused by this PR.
This might be an actual regression, but it can also be just noise.

Next Steps:

  • If the regression was expected or you think it can be justified,
    please write a comment with sufficient written justification, and add
    @rustbot label: +perf-regression-triaged to it, to mark the regression as triaged.
  • If you think that you know of a way to resolve the regression, try to create
    a new PR with a fix for the regression.
  • If you do not understand the regression or you think that it is just noise,
    you can ask the @rust-lang/wg-compiler-performance working group for help (members of this group
    were already notified of this PR).

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.2% [0.2%, 0.2%] 3
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.2% [-0.2%, -0.2%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.1% [-0.2%, 0.2%] 4

Max RSS (memory usage)

This perf run didn't have relevant results for this metric.

Cycles

This perf run didn't have relevant results for this metric.

Binary size

This perf run didn't have relevant results for this metric.

Artifact size: 405.67 MiB -> 405.76 MiB (0.02%)

@rustbot rustbot added the perf-regression Performance regression. label Nov 22, 2026
@panstromek

Copy link
Copy Markdown
Contributor

perf triage:

The regressions are in tiny incr-patched|unchanged runs, incr-full improves. Cachegrind shows most of the change in malloc_default. Changes like this are not unusual, I don't think we need to dig deeper.

@rustbot label: +perf-regression-triaged

@rustbot rustbot added the perf-regression-triaged The performance regression has been triaged. label Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-test-infra-minicore Area: `minicore` test auxiliary and `//@ add-minicore` perf-regression Performance regression. perf-regression-triaged The performance regression has been triaged. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants