Skip to content

Conversation

@Enselic
Copy link
Member

@Enselic Enselic commented Dec 4, 2025

Closes #60044 which has one 👍 and one ❤️ vote and just E-needs-test.

@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 Dec 4, 2025
@rustbot
Copy link
Collaborator

rustbot commented Dec 4, 2025

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
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

@clubby789
Copy link
Contributor

I think this should be a codegen-llvm test as it's not backend-specific

@Enselic
Copy link
Member Author

Enselic commented Dec 9, 2025

Good call. That deserves consideration.

Here is what the correct LLVM IR currently looks like:

; Function Attrs: mustprogress nofree norecurse nounwind nonlazybind willreturn memory(readwrite, argmem: none, inaccessiblemem: write)
define noundef range(i64 1, 0) i64 @some_non_zero_from_atomic_get() unnamed_addr #0 {
start:
  %0 = load atomic i64, ptr @_ZN38some_non_zero_from_atomic_optimization1X17h41fcdb7c72ef9763E monotonic, align 8
  %1 = icmp ne i64 %0, 0
  tail call void @llvm.assume(i1 %1)
  ret i64 %0
}
Full LLVM IR
; ModuleID = 'some_non_zero_from_atomic_optimization.ca4055969c8517ab-cgu.0'
source_filename = "some_non_zero_from_atomic_optimization.ca4055969c8517ab-cgu.0"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@_ZN38some_non_zero_from_atomic_optimization1X17h41fcdb7c72ef9763E = local_unnamed_addr global [8 x i8] c"\07\00\00\00\00\00\00\00", align 8

@some_non_zero_from_atomic_get2 = unnamed_addr alias i64 (), ptr @some_non_zero_from_atomic_get

; Function Attrs: mustprogress nofree norecurse nounwind nonlazybind willreturn memory(readwrite, argmem: none, inaccessiblemem: write)
define noundef range(i64 1, 0) i64 @some_non_zero_from_atomic_get() unnamed_addr #0 {
start:
  %0 = load atomic i64, ptr @_ZN38some_non_zero_from_atomic_optimization1X17h41fcdb7c72ef9763E monotonic, align 8
  %1 = icmp ne i64 %0, 0
  tail call void @llvm.assume(i1 %1)
  ret i64 %0
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write)
declare void @llvm.assume(i1 noundef) #1

attributes #0 = { mustprogress nofree norecurse nounwind nonlazybind willreturn memory(readwrite, argmem: none, inaccessiblemem: write) "probe-stack"="inline-asm" "target-cpu"="x86-64" }
attributes #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write) }

!llvm.module.flags = !{!0, !1}
!llvm.ident = !{!2}

!0 = !{i32 8, !"PIC Level", i32 2}
!1 = !{i32 2, !"RtLibUseGOT", i32 1}
!2 = !{!"rustc version 1.91.1 (ed61e7d7e 2025-11-07)"}

As you can see, the LLVM IR is considerably more elaborate and is likely to change over time as we adjust codegen and adapt to new LLVM versions. So an LLVM IR version of this test risks becoming a maintenance burden that will steal time from doing other improvements.

Besides, it does not seem impossible that this optimization could regress on the LLVM side, which an LLVM IR based test would not catch.

So my recommendation is to go ahead with this test, because it tests what actually matters (the end result) and that is unlikely to ever change.

That said, I can certainly turn this into an LLVM test if that is considered mandatory for merge. We can wait and see see what others think perhaps.

@Mark-Simulacrum
Copy link
Member

@bors r+

I don't think it has to check the LLVM IR necessarily.

@bors
Copy link
Collaborator

bors commented Dec 20, 2025

📌 Commit faf06cc has been approved by Mark-Simulacrum

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 Dec 20, 2025
Zalathar added a commit to Zalathar/rust that referenced this pull request Dec 21, 2025
…acrum

tests/assembly-llvm/some-non-zero-from-atomic-optimization.rs: New test

Closes rust-lang#60044 which has one 👍 and one ❤️  vote and just **E-needs-test**.
bors added a commit that referenced this pull request Dec 21, 2025
Rollup of 13 pull requests

Successful merges:

 - #146377 (Don't strip shebang in expr-ctxt `include!(…)`)
 - #149437 (Fix trailing newline in JUnit formatter)
 - #149658 (tests/assembly-llvm/some-non-zero-from-atomic-optimization.rs: New test)
 - #149812 (Add const default for OnceCell and OnceLock)
 - #149882 (miri: add -Zbinary-dep-depinfo to dependency builds)
 - #150009 (Enable llvm-libunwind by default for Hexagon targets)
 - #150035 (fix docustring on fetch_or)
 - #150082 (tests/ui/traits/fmt-pointer-trait.rs: Add HRTB fn pointer case)
 - #150160 (Fix ICE (#149980) for invalid EII in statement position)
 - #150184 (mir_build: Use the same length type for `TestableCase::Slice` and `TestKind::Len`)
 - #150191 (change non-canonical clone impl to {*self}, fix some doc comments)
 - #150203 (Drop the From derive macro from the v1 prelude)
 - #150208 (Update books)

r? `@ghost`
`@rustbot` modify labels: rollup
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Dec 21, 2025
…acrum

tests/assembly-llvm/some-non-zero-from-atomic-optimization.rs: New test

Closes rust-lang#60044 which has one 👍 and one ❤️  vote and just **E-needs-test**.
bors added a commit that referenced this pull request Dec 21, 2025
Rollup of 11 pull requests

Successful merges:

 - #146377 (Don't strip shebang in expr-ctxt `include!(…)`)
 - #149658 (tests/assembly-llvm/some-non-zero-from-atomic-optimization.rs: New test)
 - #149812 (Add const default for OnceCell and OnceLock)
 - #149882 (miri: add -Zbinary-dep-depinfo to dependency builds)
 - #150009 (Enable llvm-libunwind by default for Hexagon targets)
 - #150035 (fix docustring on fetch_or)
 - #150082 (tests/ui/traits/fmt-pointer-trait.rs: Add HRTB fn pointer case)
 - #150160 (Fix ICE (#149980) for invalid EII in statement position)
 - #150184 (mir_build: Use the same length type for `TestableCase::Slice` and `TestKind::Len`)
 - #150191 (change non-canonical clone impl to {*self}, fix some doc comments)
 - #150203 (Drop the From derive macro from the v1 prelude)

r? `@ghost`
`@rustbot` modify labels: rollup
@JonathanBrouwer
Copy link
Contributor

@bors r- #150220 (comment)

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Dec 21, 2025
@clubby789
Copy link
Contributor

.set some_non_zero_from_atomic_get2, some_non_zero_from_atomic_get 

vs

// CHECK-DAG: some_non_zero_from_atomic_get2 = some_non_zero_from_atomic_get

@JonathanBrouwer
Copy link
Contributor

@bors try jobs=x86_64-gnu-llvm-20-3

@rust-bors

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Dec 22, 2025
tests/assembly-llvm/some-non-zero-from-atomic-optimization.rs: New test

try-job: x86_64-gnu-llvm-20-3
@rust-bors
Copy link

rust-bors bot commented Dec 22, 2025

☀️ Try build successful (CI)
Build commit: 7367c05 (7367c0569c59e5379c39bfd46bd6431b7b0eb4c6, parent: acfd264f4df23b15cf27a15a85f76ed61977b48a)

@nikic
Copy link
Contributor

nikic commented Dec 22, 2025

As you can see, the LLVM IR is considerably more elaborate and is likely to change over time as we adjust codegen and adapt to new LLVM versions. So an LLVM IR version of this test risks becoming a maintenance burden that will steal time from doing other improvements.

As you are currently finding out, the reason we test codegen instead of assembly is that assembly tests are even more fragile :)

For cases like this, you don't really need to check the exact emitted IR, something like CHECK-NOT: panic (often combined with CHECK-NOT: unreachable) tends to be good enough.

You can use -Zmerge-functions=disabled to avoid function merging, which tends to make writing tests more inconvenient.

@Enselic
Copy link
Member Author

Enselic commented Dec 22, 2025

Ok I have now been convinced that we should use an LLVM IR test instead :)

I am not relaxed enough with FileCheck to be comfortable with some CHECK-NOT: unreachable checks and the like, so I went for detailed checks instead. That way I feel confident we will not regress. But I'm of course more than willing to keep adjusting until everyone is happy with how it looks.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@Enselic Enselic changed the title tests/assembly-llvm/some-non-zero-from-atomic-optimization.rs: New test tests/codegen-llvm/some-non-zero-from-atomic-optimization.rs: New test Dec 23, 2025
@Enselic
Copy link
Member Author

Enselic commented Dec 23, 2025

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 23, 2025
Copy link
Member

@Mark-Simulacrum Mark-Simulacrum left a comment

Choose a reason for hiding this comment

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

I think we can simplify the tests and make them less brittle while doing so.

View changes since this review

@rustbot rustbot 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 Dec 28, 2025
@Enselic
Copy link
Member Author

Enselic commented Dec 31, 2025

Thanks for the feedback. I have now turned this into a "not panic" and "not unreachable" test and made sure that it will catch a regression.

The test fails with nightly-2024-02-08:

$ rustc +nightly-2024-02-08 --emit=llvm-ir -O -Zmerge-functions=disabled \
    tests/codegen-llvm/some-non-zero-from-atomic-optimization.rs &&  \
/usr/lib/llvm-20/bin/FileCheck \
    tests/codegen-llvm/some-non-zero-from-atomic-optimization.rs \
    <some-non-zero-from-atomic-optimization.ll
tests/codegen-llvm/some-non-zero-from-atomic-optimization.rs:76:15: error: CHECK-NOT: excluded string found in input
// CHECK-NOT: panic
              ^
<stdin>:26:14: note: found here
; call core::panicking::panic
             ^~~~~
tests/codegen-llvm/some-non-zero-from-atomic-optimization.rs:77:15: error: CHECK-NOT: excluded string found in input
// CHECK-NOT: unreachable
              ^
<stdin>:28:2: note: found here
 unreachable
 ^~~~~~~~~~~

but passes with nightly-2024-02-09:

$ rustc +nightly-2024-02-09 --emit=llvm-ir -O -Zmerge-functions=disabled \
    tests/codegen-llvm/some-non-zero-from-atomic-optimization.rs &&  \
/usr/lib/llvm-20/bin/FileCheck \
    tests/codegen-llvm/some-non-zero-from-atomic-optimization.rs \
    <some-non-zero-from-atomic-optimization.ll && \
echo pass
pass

As you can see above I did a bisect and it was fixed in nightly-2024-02-09. Here is the set of candidate commits that fixed it. See #60044 (comment) for a guess on which one it was.

git log ^8ace7ea1f7cbba7b4f031e66c54ca237a0d65de6 98aa362 --no-merges --oneline

ad511ef Avoid ICE in drop recursion check in case of invalid drop impls
5cb9e31 Add release note for new ambiguous_wide_pointer_comparisons lint
7dc182d Fix mir pass ICE in the presence of other errors
6af2d3c Fix span_bug! backtraces
f867742 reviews + rebase
b181a12 rename instantiate_binder_with_placeholders
ac559af introduce enter_forall
da4ec6f Deduplicate tcx.instance_mir(instance) calls in try_instance_mir
4733b1b Test min_exhaustive_patterns in more cases
30793ca Match min_exhaustive_patterns implementation with exhaustive_patterns
f676c3d Remove myself from review rotation.
0ce71f6 make future diffs minimal
eab2adb Continue to borrowck even if there were previous errors
e5461de Taint borrowck results without running any borrowck if the MIR body was already tainted
1fcd04e inline a function that is only used in clippy
8a5847f Already poison the type_of result of the anon const used in the typeof expression
6b175a8 Add SubdiagnosticMessageOp as a trait alias.
9dca6be Prefer "0..MAX not covered" to "_ not covered"
970f46c Add tests
c636c7a address review comments and add more tests
cd21b1d No need to take ImplTraitContext by ref
18e5bbf improve pretty printing for trait objects
4229875 Update test output.
5d65418 Replace transmute_copy with ptr::read.
af48cf6 Don't use assert_unsafe_precondition twice.
fd92021 add test for pretty printing trait objects
a67b72c Make NonZero constructors generic.
58d70d6 Simplify impl_zeroable_primitive macro.
0a50dba docs: also check the inline stmt during redundant link check
3e8c8d8 hir: Add some FIXMEs for future work
a61019b hir: Remove fn opt_hir_id and fn opt_span
363b098 hir: Make sure all HirIds have corresponding HIR Nodes
a2ab48c resolve: Unload speculatively resolved crates before freezing cstore
6fbd761 Also turn moves into copies even if through projections.
c151ed4 Add test.
1a3214b Make sure refinement still works
e65abc0 Make the error message better
16cbdd0 Allow desugaring async fn in trait to compatible, concrete future types
a59a1e7 Remove some invalid cfg(doc) code
83738a9 Stop bailing out from compilation just because there were incoherent traits
7f1d523 Avoid emitting trait bound errors of incoherent traits
0211221 Prevent running some code if it is already in the map
74dbf3a fix ui tests
96108c5 make effect infer variables suggestable in diagnostics
c9192be Reconstify Add
6b2a824 Remove dead args from functions
0825ad3 Clarify the new binding dance
09d4613 Put new bindings first in refutable cases too
e902878 Clarify the binding dance
96ff1a4 Move Or test out of the loop
3ea464f Add tests

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 31, 2025
@Mark-Simulacrum
Copy link
Member

@bors r+

@bors
Copy link
Collaborator

bors commented Jan 2, 2026

📌 Commit 55833a9 has been approved by Mark-Simulacrum

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 Jan 2, 2026
bors added a commit that referenced this pull request Jan 2, 2026
tests/codegen-llvm/some-non-zero-from-atomic-optimization.rs: New test

Closes #60044 which has one 👍 and one ❤️  vote and just **E-needs-test**.
@bors
Copy link
Collaborator

bors commented Jan 2, 2026

⌛ Testing commit 55833a9 with merge 5497a36...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Missed NonZero optimization opportunity

9 participants