Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add naked_asm! macro for use in #[naked] functions #128651

Merged
merged 8 commits into from
Oct 7, 2024

Conversation

folkertdev
Copy link
Contributor

tracking issue: #90957

Adds the core::arch::naked_asm macro, to be used in #[naked] functions, but providing better error messages and a place to explain the restrictions on assembly in naked functions.

This PR does not yet require that the naked_asm! macro is used inside of #[naked] functions:

  • the asm! macro can still be used in #[naked] functions currently, with the same restrictions and error messages as before.
  • the naked_asm! macro can be used outside of #[naked] functions. It has not yet been decided whether that should be allowed long-term.

In this PR, the parsing code of naked_asm! now enforces the restrictions on assembly in naked functions, with the exception of checking that the noreturn option is specified. It also has not currently been decided if noreturn should be implicit or not.

This PR looks large because it touches a bunch of tests. The code changes are mostly straightforward I think: we now have 3 flavors of assembly macro, and that information must be propagated through the parsing code and error messages.

cc @Lokathor

r? @Amanieu

@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. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 4, 2024
@Lokathor
Copy link
Contributor

Lokathor commented Aug 4, 2024

My own vote is that the noreturn option should not be placed by users into the naked_asm macro. The same option should not mean one thing for asm and then nearly the opposite in naked_asm. That's needlessly confusing.

There are two reasonable designs that spring to mind:

  • naked_asm uses an alternative token such as nakedreturn, which still lets the reader know that there's some funny control flow going on, but which can be documented separately and compiler checked separately.
  • naked_asm simply doesn't have users write any return related option word at all.

@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Aug 4, 2024

Some changes occurred in src/tools/rustfmt

cc @rust-lang/rustfmt

@folkertdev
Copy link
Contributor Author

I agree, and think we can get away with making the noreturn implicit. Naked functions are an advanced feature anyway, so if you're writing naked_asm! it's up to you to make sure the safety conditions are satisfied.

But also, I don't want to touch the validation logic in this PR, it can be handled when we disallow the use of asm! in naked functions.

Copy link
Member

@Amanieu Amanieu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but I would make one change and remove the noreturn attribute from naked_asm, changing it to always return !.

The reason for this is that we want existing users of naked function to transition to using naked_asm instead and we want to eliminate noreturn as part of this transition.

r=me

@folkertdev
Copy link
Contributor Author

cool, we discussed noreturn a bit in the comments here earlier, and it looks like everyone is in agreement that the noreturn should be dropped.

I did not implement that here yet because then naked_asm! will fail the validation pass that is currently applied to asm! which mandates that the noreturn is present. So to make naked_asm! work without noreturn, that validation pass needs to be removed and in practice that means that asm! should also be disallowed in naked functions. That was always the plan of course, I just tried to break it up into smaller parts.

But, I'll line up those changes and then we can see how to merge them: either in 2 parts or as one combined unit.

@Amanieu
Copy link
Member

Amanieu commented Sep 5, 2024

It's probably fine to go ahead and disallow asm in naked functions as part of this PR. It will be easier this way.

compiler/rustc_ast/src/ast.rs Show resolved Hide resolved
compiler/rustc_ast/src/ast.rs Outdated Show resolved Hide resolved
compiler/rustc_hir_typeck/src/expr.rs Outdated Show resolved Hide resolved
compiler/rustc_passes/src/errors.rs Outdated Show resolved Hide resolved
compiler/rustc_passes/src/naked_functions.rs Outdated Show resolved Hide resolved
compiler/rustc_passes/src/naked_functions.rs Outdated Show resolved Hide resolved
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rustbot rustbot added the A-run-make Area: port run-make Makefiles to rmake.rs label Sep 5, 2024
@rustbot
Copy link
Collaborator

rustbot commented Sep 5, 2024

This PR modifies tests/run-make/. If this PR is trying to port a Makefile
run-make test to use rmake.rs, please update the
run-make port tracking issue
so we can track our progress. You can either modify the tracking issue
directly, or you can comment on the tracking issue and link this PR.

cc @jieyouxu

@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Sep 5, 2024

This PR changes MIR

cc @oli-obk, @RalfJung, @JakobDegen, @davidtwco, @celinval, @vakaras

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

@folkertdev
Copy link
Contributor Author

well that pinged a bunch of people.

The thing I had to solve was that the InlineAsmOptions::NORETURN value is (ab)used to answer the question "will this asm block diverge?", which is used in various places in type checking and codegen. The naked_asm! macro does not set this option, but does diverge. In hindsight that option maybe should have been named diverges? anyway, the current solution is a bit noisy, so if anyone has better ideas let me know.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

tgross35 added a commit to tgross35/rust that referenced this pull request Sep 27, 2024
…tgross35

update `compiler-builtins` to 0.1.126

this requires the addition of a bootstrap variant of the new `naked_asm!` macro

r? ``@tgross35``

extracted from rust-lang#128651
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Sep 27, 2024
…tgross35

update `compiler-builtins` to 0.1.126

this requires the addition of a bootstrap variant of the new `naked_asm!` macro

r? `@tgross35`

extracted from rust-lang#128651
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Sep 27, 2024
Rollup merge of rust-lang#130875 - folkertdev:naked-asm-bootstrap, r=tgross35

update `compiler-builtins` to 0.1.126

this requires the addition of a bootstrap variant of the new `naked_asm!` macro

r? `@tgross35`

extracted from rust-lang#128651
@rust-log-analyzer

This comment has been minimized.

Copy link
Member

@Amanieu Amanieu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with one minor nit.

r=me

self.items.push((ItemKind::NakedAsm, span));
}
rustc_ast::AsmMacro::GlobalAsm => {
// not allowed in this position
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this a span_bug!.

@Amanieu
Copy link
Member

Amanieu commented Oct 5, 2024

@bors delegate+

@bors
Copy link
Contributor

bors commented Oct 5, 2024

✌️ @folkertdev, you can now approve this pull request!

If @Amanieu told you to "r=me" after making some further change, please make that change, then do @bors r=@Amanieu

also disallow the `noreturn` option, and infer `naked_asm!` as `!`
- fix for divergence
- fix error message
- fix another cranelift test
- fix some cranelift things
- don't set the NORETURN option for naked asm
- fix use of naked_asm! in doc comment
- fix use of naked_asm! in run-make test
- use `span_bug` in unreachable branch
@folkertdev
Copy link
Contributor Author

@bors r=@Amanieu

@bors
Copy link
Contributor

bors commented Oct 6, 2024

📌 Commit 5fc60d1 has been approved by Amanieu

It is now in the queue for this repository.

@bors bors 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 Oct 6, 2024
@bors
Copy link
Contributor

bors commented Oct 6, 2024

⌛ Testing commit 5fc60d1 with merge 1b3b8e7...

@bors
Copy link
Contributor

bors commented Oct 7, 2024

☀️ Test successful - checks-actions
Approved by: Amanieu
Pushing 1b3b8e7 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Oct 7, 2024
@bors bors merged commit 1b3b8e7 into rust-lang:master Oct 7, 2024
7 checks passed
@rustbot rustbot added this to the 1.83.0 milestone Oct 7, 2024
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (1b3b8e7): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results (secondary 0.6%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.6% [0.6%, 0.6%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Cycles

Results (secondary 7.9%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
7.9% [2.5%, 10.0%] 7
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 774.699s -> 774.632s (-0.01%)
Artifact size: 329.53 MiB -> 329.53 MiB (-0.00%)

luojia65 added a commit to rustsbi/allwinner-hal that referenced this pull request Oct 8, 2024
…10-07

This feature has been added recently in this pull request: rust-lang/rust#128651

After this pull, `core::arch::asm!` can no longer be used in #[naked] functions. We should use `core::arch::naked_asm!` instead.

Tested rustc version:

```
rustc -vV
rustc 1.83.0-nightly (3ae715c8c 2024-10-07)
binary: rustc
commit-hash: 3ae715c8c63f9aeac47cbf7d8d9dadb3fa32c638
commit-date: 2024-10-07
host: x86_64-pc-windows-msvc
release: 1.83.0-nightly
LLVM version: 19.1.1
```

Signed-off-by: Zhouqi Jiang <luojia@hust.edu.cn>
luojia65 added a commit to rustsbi/bouffalo-hal that referenced this pull request Oct 10, 2024
…r rustc nightly 2024-10-07

Ref: rust-lang/rust#128651
Signed-off-by: Zhouqi Jiang <luojia@hust.edu.cn>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-run-make Area: port run-make Makefiles to rmake.rs merged-by-bors This PR was explicitly merged by bors. 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. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

9 participants