Skip to content

GVN: transmute adts to their fields if a field projection is immediately transmuted anyway#153085

Open
oli-obk wants to merge 4 commits into
rust-lang:mainfrom
oli-obk:transmute_project
Open

GVN: transmute adts to their fields if a field projection is immediately transmuted anyway#153085
oli-obk wants to merge 4 commits into
rust-lang:mainfrom
oli-obk:transmute_project

Conversation

@oli-obk

@oli-obk oli-obk commented Feb 25, 2026

Copy link
Copy Markdown
Contributor

View all comments

follow-up to #152702

commits best reviewed individually.

Basically does

-         _11 = copy ((_10.0: std::ptr::Unique<()>).0: std::ptr::NonNull<()>) as *const () (Transmute);
+         _11 = copy _10 as *const () (Transmute);

in various forms, including going from an Option directly to a value in its Some variant if the value got niche optimized into the Option

r? @scottmcm
cc @cjgillot

@rustbot

rustbot commented Feb 25, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@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 Feb 25, 2026
@oli-obk

oli-obk commented Feb 25, 2026

Copy link
Copy Markdown
Contributor Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 25, 2026
rust-bors Bot pushed a commit that referenced this pull request Feb 25, 2026
GVN: transmute adts to their fields if a field projection is immediately transmuted anyway
@rust-bors

rust-bors Bot commented Feb 25, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: de634ba (de634ba015f8ccb17717cd0fbd048e381b7e297c, parent: 58745ca3b05afa82bd5720dd05f3854e674700e0)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (de634ba): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

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.6% [0.4%, 0.8%] 2
Regressions ❌
(secondary)
0.3% [0.1%, 0.4%] 3
Improvements ✅
(primary)
-0.3% [-0.8%, -0.2%] 8
Improvements ✅
(secondary)
-0.3% [-0.3%, -0.2%] 6
All ❌✅ (primary) -0.1% [-0.8%, 0.8%] 10

Max RSS (memory usage)

Results (primary 5.3%, secondary 6.8%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
5.3% [2.1%, 13.0%] 4
Regressions ❌
(secondary)
6.8% [5.4%, 8.2%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 5.3% [2.1%, 13.0%] 4

Cycles

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

Binary size

Results (primary -0.1%, secondary 0.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
0.3% [0.0%, 0.5%] 5
Regressions ❌
(secondary)
0.3% [0.0%, 0.5%] 8
Improvements ✅
(primary)
-0.5% [-1.2%, -0.0%] 4
Improvements ✅
(secondary)
-0.3% [-0.7%, -0.0%] 5
All ❌✅ (primary) -0.1% [-1.2%, 0.5%] 9

Bootstrap: 478.569s -> 481.658s (0.65%)
Artifact size: 395.75 MiB -> 397.78 MiB (0.51%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Feb 25, 2026
Comment thread compiler/rustc_mir_transform/src/gvn.rs Outdated
Comment thread compiler/rustc_mir_transform/src/gvn.rs Outdated
@scottmcm

Copy link
Copy Markdown
Member

Very cool! I like that we can do this in mir so it can handle things like Box<T> -> *mut T directly that we couldn't do in the library because of needing to work with non-ZST Allocators. I skimmed through some of the test updates and those all seem fine. Plus lovely to see check & debug improvements from a pass that doesn't even run in debug :)

I really don't feel confident that I know the nuances of GVN enough to comfortable reviewing this, though, so
r? cjgillot

@rustbot rustbot assigned cjgillot and unassigned scottmcm Feb 26, 2026
StorageDead(_7);
StorageLive(_9);
_9 = copy _8 as *mut u8 (Transmute);
_10 = alloc::alloc::__rust_dealloc(move _9, move _5, move _6) -> [return: bb3, unwind unreachable];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

regression?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hmm, it means we didn't know the NonNull-ness before, and we don't (currently) elide transmutes like this when the middle type is more specific than the source or destination in order to preserve that extra niche information for the backend.

See discussion in #152702 (comment) ; I don't think doing something about it needs to be this PR.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Opened #153251 to get rid of the extra step here.

@oli-obk oli-obk force-pushed the transmute_project branch from c50b9af to 9bf1aaa Compare March 5, 2026 11:09
@rustbot

This comment has been minimized.

@oli-obk

oli-obk commented Mar 5, 2026

Copy link
Copy Markdown
Contributor Author

Plus lovely to see check & debug improvements from a pass that doesn't even run in debug :)

it is run on libstd/core, where we have this pattern a lot :D

@oli-obk

oli-obk commented Mar 5, 2026

Copy link
Copy Markdown
Contributor Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Mar 5, 2026
GVN: transmute adts to their fields if a field projection is immediately transmuted anyway
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 5, 2026
@rust-bors

rust-bors Bot commented Mar 5, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 26d5903 (26d59037c6489e40dfa37bc704efc86c723a310b, parent: 70d86e3abeecf3a655264d9a716c5d08160176b7)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (26d5903): comparison URL.

Overall result: ❌ regressions - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

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.6% [0.1%, 1.1%] 2
Regressions ❌
(secondary)
0.2% [0.1%, 0.3%] 9
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.6% [0.1%, 1.1%] 2

Max RSS (memory usage)

Results (primary -4.0%, secondary 3.8%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
3.8% [3.8%, 3.8%] 1
Improvements ✅
(primary)
-4.0% [-5.3%, -3.1%] 3
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -4.0% [-5.3%, -3.1%] 3

Cycles

Results (primary -1.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

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

Binary size

Results (primary 0.5%, secondary 0.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
1.2% [0.0%, 2.3%] 3
Regressions ❌
(secondary)
0.3% [0.0%, 0.4%] 12
Improvements ✅
(primary)
-0.0% [-0.0%, -0.0%] 4
Improvements ✅
(secondary)
-0.0% [-0.0%, -0.0%] 1
All ❌✅ (primary) 0.5% [-0.0%, 2.3%] 7

Bootstrap: 480.787s -> 479.516s (-0.26%)
Artifact size: 395.02 MiB -> 395.00 MiB (-0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 5, 2026
@oli-obk

oli-obk commented Mar 5, 2026

Copy link
Copy Markdown
Contributor Author

Fun. The entire previous improvement was from the PR this was based on

@oli-obk oli-obk force-pushed the transmute_project branch from 9bf1aaa to c44c650 Compare March 9, 2026 12:16
@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

@oli-obk oli-obk force-pushed the transmute_project branch from c44c650 to 54ef95d Compare April 21, 2026 07:28
@rustbot

This comment has been minimized.

@oli-obk

oli-obk commented Apr 21, 2026

Copy link
Copy Markdown
Contributor Author

The large regression (cargo) is mostly in LLVM (along with lots of noisy changes). Maybe there's just more optimizations enabled by this change? idk

@rustbot ready

could also just close this PR if you think it's not worth it

@rust-log-analyzer

This comment has been minimized.

@oli-obk oli-obk force-pushed the transmute_project branch from 54ef95d to cf83de7 Compare April 24, 2026 09:23
@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

@cjgillot

Copy link
Copy Markdown
Contributor

r=me after rebase

@cjgillot cjgillot 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 May 24, 2026
Comment thread compiler/rustc_mir_transform/src/gvn.rs Outdated
@oli-obk oli-obk force-pushed the transmute_project branch from cf83de7 to d05dccd Compare June 25, 2026 13:29
@rustbot

rustbot commented Jun 25, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job x86_64-gnu-gcc-core-tests failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
   Compiling gimli v0.32.3
[RUSTC-TIMING] memchr test:false 11.115
[RUSTC-TIMING] gimli test:false 7.969
   Compiling addr2line v0.25.1
libgccjit.so: error: : gcc_jit_context_new_call: mismatching types for argument 3 of function "memcmp": assignment to param arg2 (type: size_t) from bitcast(loadedValue6, __int64_t  __attribute__((aligned(8)))) (type: __int64_t  __attribute__((aligned(8))))
libgccjit.so: error: : gcc_jit_context_new_cast: NULL rvalue
gcc_jit_rvalue_get_type: NULL rvalue
gcc_jit_type_get_pointer: NULL type
libgccjit.so: error: : gcc_jit_context_new_bitcast: NULL type
gcc_jit_rvalue_dereference: NULL rvalue
gcc_jit_lvalue_as_rvalue: NULL lvalue
gcc_jit_rvalue_get_type: NULL rvalue
gcc_jit_type_unqualified: NULL type
gcc_jit_type_get_aligned: NULL type
gcc_jit_type_get_pointer: NULL type
libgccjit.so: error: : gcc_jit_context_new_cast: NULL rvalue
gcc_jit_rvalue_dereference: NULL rvalue
libgccjit.so: error: : gcc_jit_block_add_assignment: NULL lvalue
libgccjit.so: error: : gcc_jit_context_new_call: mismatching types for argument 3 of function "memcmp": assignment to param arg2 (type: size_t) from bitcast(loadedValue6, __int64_t  __attribute__((aligned(8)))) (type: __int64_t  __attribute__((aligned(8))))
libgccjit.so: error: : gcc_jit_context_new_cast: NULL rvalue
gcc_jit_rvalue_get_type: NULL rvalue
gcc_jit_type_get_pointer: NULL type
libgccjit.so: error: : gcc_jit_context_new_bitcast: NULL type
gcc_jit_rvalue_dereference: NULL rvalue
gcc_jit_lvalue_as_rvalue: NULL lvalue
gcc_jit_rvalue_get_type: NULL rvalue
gcc_jit_type_unqualified: NULL type
gcc_jit_type_get_aligned: NULL type
gcc_jit_type_get_pointer: NULL type
libgccjit.so: error: : gcc_jit_context_new_cast: NULL rvalue
gcc_jit_rvalue_dereference: NULL rvalue
libgccjit.so: error: : gcc_jit_block_add_assignment: NULL lvalue
error: failed to build archive at `/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-linux-gnu/dist/build/addr2line/3b48cc3f07a65e65/out/libaddr2line-3b48cc3f07a65e65.rlib`: failed to open object file: No such file or directory (os error 2)

[RUSTC-TIMING] addr2line test:false 1.150
rustc exited with signal: 6 (SIGABRT) (core dumped)
error: could not compile `addr2line` (lib) due to 1 previous error

Caused by:
  process didn't exit successfully: `/checkout/obj/build/bootstrap/debug/rustc /checkout/obj/build/bootstrap/debug/rustc --crate-name addr2line --edition=2018 /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/addr2line-0.25.1/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --crate-type lib --emit=dep-info,metadata,link -Z embed-metadata=no -C opt-level=s -C embed-bitcode=no -C codegen-units=1 -Cembed-bitcode=yes -Zunstable-options -Cforce-frame-pointers=non-leaf -C overflow-checks=on --cfg 'feature="alloc"' --cfg 'feature="core"' --cfg 'feature="rustc-dep-of-std"' --check-cfg 'cfg(docsrs,test)' --check-cfg 'cfg(feature, values("all", "alloc", "bin", "cargo-all", "core", "cpp_demangle", "default", "fallible-iterator", "loader", "rustc-demangle", "rustc-dep-of-std", "smallvec", "std", "wasm"))' -C metadata=121783180a5b731d -C extra-filename=-3b48cc3f07a65e65 --out-dir /checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-linux-gnu/dist/build/addr2line/3b48cc3f07a65e65/out --target x86_64-unknown-linux-gnu -C strip=debuginfo -L dependency=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/dist/build/cc/54a6600bab8cf9fc/out -L dependency=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/dist/build/compiler_builtins/b3edefae36c8c7c7/out -L dependency=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/dist/build/shlex/e43f9179e4b33b2b/out -L dependency=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-linux-gnu/dist/build/addr2line/3b48cc3f07a65e65/out -L dependency=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-linux-gnu/dist/build/alloc/49220ed811a06747/out -L dependency=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-linux-gnu/dist/build/compiler_builtins/828632ac172df5b0/out -L dependency=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-linux-gnu/dist/build/compiler_builtins/f815ffc9c670c233/out -L dependency=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-linux-gnu/dist/build/core/2b9ece7490e9479d/out -L dependency=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-linux-gnu/dist/build/gimli/929ad9b97a681af9/out -L dependency=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-linux-gnu/dist/build/rustc-std-workspace-alloc/f57839810cfe131e/out -L dependency=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-linux-gnu/dist/build/rustc-std-workspace-core/786a9c6b9c52478d/out -L dependency=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/dist/build/addr2line/3b48cc3f07a65e65/out --extern gimli=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-linux-gnu/dist/build/gimli/929ad9b97a681af9/out/libgimli-929ad9b97a681af9.rmeta --extern alloc=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-linux-gnu/dist/build/rustc-std-workspace-alloc/f57839810cfe131e/out/librustc_std_workspace_alloc-f57839810cfe131e.rmeta --extern core=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-linux-gnu/dist/build/rustc-std-workspace-core/786a9c6b9c52478d/out/librustc_std_workspace_core-786a9c6b9c52478d.rmeta --cap-lints allow -Zannotate-moves '--check-cfg=cfg(feature,values(any()))' -Zunstable-options -Zmacro-backtrace -Csplit-debuginfo=off -Cprefer-dynamic -Zinline-mir -Zinline-mir-preserve-debug -Zmir_strip_debuginfo=locals-in-tiny-functions -Clink-args=-Wl,-z,origin '-Clink-args=-Wl,-rpath,$ORIGIN/../lib' -Alinker-messages '-Zcrate-attr=doc(html_root_url="https://doc.rust-lang.org/nightly/")' -Cpanic=abort -Zpanic-abort-tests -Z binary-dep-depinfo -L native=/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-linux-gnu/dist/build/compiler_builtins/f815ffc9c670c233/out` (exit status: 254)
warning: build failed, waiting for other jobs to finish...
[RUSTC-TIMING] compiler_builtins test:false 22.744
[RUSTC-TIMING] object test:false 8.671
[RUSTC-TIMING] core test:false 45.643
Bootstrap failed while executing `--stage 1 test library/coretests`

Important

For more information how to resolve CI failures of this job, visit this link.

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

Labels

perf-regression Performance regression. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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

None yet

Development

Successfully merging this pull request may close these issues.

8 participants