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

[EXPERIMENT] Don't monomorphize things that are unused due to if <T as Trait>::CONST #91222

Closed
wants to merge 4 commits into from

Conversation

scottmcm
Copy link
Member

@scottmcm scottmcm commented Nov 25, 2021

Fixes #85836

MIR optimizations have long meant that we do this for concrete constants, but with this change it also applies for things like <T as TrustedRandomAccess>::MAY_HAVE_SIDE_EFFECT.

There's likely not enough of this stuff for the extra work to be worth it, but I'm curious to see anyway.

r? @ghost

MIR optimizations have long meant that we do this for concrete constants, but with this it also applies for things like `<T as TrustedRandomAccess>::MAY_HAVE_SIDE_EFFECT`.
@scottmcm
Copy link
Member Author

Queue's empty, so...

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 25, 2021
@bors
Copy link
Contributor

bors commented Nov 25, 2021

⌛ Trying commit 568fe20 with merge 258c6a0d164d6864d65295e968956d4cc6e246f2...

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Nov 25, 2021

💔 Test failed - checks-actions

@bors bors added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Nov 25, 2021
@the8472
Copy link
Member

the8472 commented Nov 25, 2021

MAY_HAVE_SIDE_EFFECT doesn't seem like a particularly juicy target since it's mostly used in Zip.

The following could be a juicier one if it were extracted to an associated const if that works together with specialization

if mem::size_of::<T>() == 0
|| mem::size_of::<T>()
!= mem::size_of::<<<I as SourceIter>::Source as AsIntoIter>::Item>()
|| mem::align_of::<T>()
!= mem::align_of::<<<I as SourceIter>::Source as AsIntoIter>::Item>()

I can prepare a PR for that if it helps.

@scottmcm
Copy link
Member Author

Hmm, interesting that the try failed. I built a full stage2 locally without issue. Oh well, TBH I was surprised that just referencing these things "worked", so I'll take a look at removing the references to them too. Emitting less LLVM is always good.

--

@the8472 If you're excited to I'd happily take it in the branch, but this is still experimental enough that I can't honestly say that you should. It's still perfectly likely that this will go nowhere, so I wouldn't want you to waste time.

Honestly this PR might not be worth it until inline consts work so that could just be

        if const {
            mem::size_of::<T>() == 0
                || mem::size_of::<T>()
                    != mem::size_of::<<<I as SourceIter>::Source as AsIntoIter>::Item>()
                || mem::align_of::<T>()
                    != mem::align_of::<<<I as SourceIter>::Source as AsIntoIter>::Item>()
        } {
            // fallback to more generic implementations
            return SpecFromIterNested::from_iter(iterator);
        }

instead, and not need a whole bunch of these little one-use traits.

But right now they're using the basic "sugar for const item" implementation strategy which doesn't allow generic parameters to be referenced, making them unhelpful here.

(The context for this PR was seeing if I could get #85836 working.)

@the8472
Copy link
Member

the8472 commented Nov 25, 2021

associated const PR over at scottmcm#3

extract const guard to trait associated const for optimization experiment
@rust-log-analyzer

This comment has been minimized.

@scottmcm
Copy link
Member Author

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

@bors
Copy link
Contributor

bors commented Nov 26, 2021

⌛ Trying commit b728f16603191d29dda030521911a25c2e328c8d with merge 813a1d70587279815e909f5fc757d2e02322392e...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Nov 26, 2021

☀️ Try build successful - checks-actions
Build commit: 813a1d70587279815e909f5fc757d2e02322392e (813a1d70587279815e909f5fc757d2e02322392e)

@rust-timer
Copy link
Collaborator

Queued 813a1d70587279815e909f5fc757d2e02322392e with parent 9adfd9d, future comparison URL.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (813a1d70587279815e909f5fc757d2e02322392e): comparison url.

Summary: This change led to large relevant mixed results 🤷 in compiler performance.

  • Large improvement in instruction counts (up to -1.9% on full builds of inflate)
  • Large regression in instruction counts (up to 3.8% on incr-unchanged builds of deep-vector)

If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf.

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR led to changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

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

@rustbot rustbot added perf-regression Performance regression. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Nov 26, 2021
And hey, maybe even be faster because it means less IR emitted.
@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-llvm-12 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
.................F...............i.................................
failures:
Some tests failed in compiletest suite=mir-opt mode=mir-opt host=x86_64-unknown-linux-gnu target=i686-unknown-linux-gnu

---- [mir-opt] mir-opt/matches_reduce_branches.rs stdout ----
1 - // MIR for `match_nested_if` before MatchBranchSimplification
2 + // MIR for `match_nested_if` after MatchBranchSimplification
+   
4   fn match_nested_if() -> bool {
4   fn match_nested_if() -> bool {
5       let mut _0: bool;                    // return place in scope 0 at $DIR/matches_reduce_branches.rs:39:25: 39:29
6       let _1: bool;                        // in scope 0 at $DIR/matches_reduce_branches.rs:40:9: 40:12
13       scope 1 {
13       scope 1 {
14           debug val => _1;                 // in scope 1 at $DIR/matches_reduce_branches.rs:40:9: 40:12
- 
+   
17       bb0: {
17       bb0: {
18           StorageLive(_1);                 // scope 0 at $DIR/matches_reduce_branches.rs:40:9: 40:12
19           StorageLive(_2);                 // scope 0 at $DIR/matches_reduce_branches.rs:41:18: 41:81

24                                            // + span: $DIR/matches_reduce_branches.rs:41:24: 41:31
25                                            // + literal: Const { ty: fn() -> bool {unknown}, val: Value(Scalar(<ZST>)) }
- 
+   
28       bb1: {
28       bb1: {
29 -         switchInt(move _4) -> [false: bb3, otherwise: bb2]; // scope 0 at $DIR/matches_reduce_branches.rs:41:24: 41:33
30 -     }
- -
+ - 
32 -     bb2: {
32 -     bb2: {
33 -         _3 = const true;                 // scope 0 at $DIR/matches_reduce_branches.rs:41:36: 41:40
34 -         goto -> bb4;                     // scope 0 at $DIR/matches_reduce_branches.rs:41:21: 41:57
35 -     }
- -
+ - 
37 -     bb3: {
37 -     bb3: {
38 -         _3 = const false;                // scope 0 at $DIR/matches_reduce_branches.rs:41:50: 41:55
39 -         goto -> bb4;                     // scope 0 at $DIR/matches_reduce_branches.rs:41:21: 41:57
40 -     }
- -
+ - 
42 -     bb4: {
42 -     bb4: {
43 +         StorageLive(_5);                 // scope 0 at $DIR/matches_reduce_branches.rs:41:24: 41:33
44 +         _5 = move _4;                    // scope 0 at $DIR/matches_reduce_branches.rs:41:24: 41:33

47           StorageDead(_4);                 // scope 0 at $DIR/matches_reduce_branches.rs:41:56: 41:57
48 -         switchInt(move _3) -> [false: bb6, otherwise: bb5]; // scope 0 at $DIR/matches_reduce_branches.rs:41:21: 41:57
49 -     }
+ - 
51 -     bb5: {
51 -     bb5: {
52 -         _2 = const true;                 // scope 0 at $DIR/matches_reduce_branches.rs:41:60: 41:64
53 -         goto -> bb7;                     // scope 0 at $DIR/matches_reduce_branches.rs:41:18: 41:81
54 -     }
- -
+ - 
56 -     bb6: {
56 -     bb6: {
57 -         _2 = const false;                // scope 0 at $DIR/matches_reduce_branches.rs:41:74: 41:79
58 -         goto -> bb7;                     // scope 0 at $DIR/matches_reduce_branches.rs:41:18: 41:81
59 -     }
- -
+ - 
61 -     bb7: {
61 -     bb7: {
62 +         StorageLive(_6);                 // scope 0 at $DIR/matches_reduce_branches.rs:41:21: 41:57
63 +         _6 = move _3;                    // scope 0 at $DIR/matches_reduce_branches.rs:41:21: 41:57

66           StorageDead(_3);                 // scope 0 at $DIR/matches_reduce_branches.rs:41:80: 41:81
67 -         switchInt(move _2) -> [false: bb9, otherwise: bb8]; // scope 0 at $DIR/matches_reduce_branches.rs:41:18: 41:81
68 -     }
+ - 
70 -     bb8: {
70 -     bb8: {
71 +         StorageLive(_7);                 // scope 0 at $DIR/matches_reduce_branches.rs:41:18: 41:81
72 +         _7 = move _2;                    // scope 0 at $DIR/matches_reduce_branches.rs:41:18: 41:81

74 -         _1 = const true;                 // scope 0 at $DIR/matches_reduce_branches.rs:47:13: 47:17
75 -         goto -> bb10;                    // scope 0 at $DIR/matches_reduce_branches.rs:47:13: 47:17
76 -     }
+ - 
78 -     bb9: {
78 -     bb9: {
79 -         StorageDead(_2);                 // scope 0 at $DIR/matches_reduce_branches.rs:45:9: 45:10
80 -         _1 = const false;                // scope 0 at $DIR/matches_reduce_branches.rs:49:14: 49:19

81 -         goto -> bb10;                    // scope 0 at $DIR/matches_reduce_branches.rs:49:14: 49:19
82 -     }
+ - 
84 -     bb10: {
84 -     bb10: {
85 +         _1 = Ne(_7, const false);        // scope 0 at $DIR/matches_reduce_branches.rs:49:14: 49:19
86 +         StorageDead(_7);                 // scope 0 at $DIR/matches_reduce_branches.rs:41:18: 41:81

thread '[mir-opt] mir-opt/matches_reduce_branches.rs' panicked at 'Actual MIR output differs from expected MIR output /checkout/src/test/mir-opt/matches_reduce_branches.match_nested_if.MatchBranchSimplification.32bit.diff', src/tools/compiletest/src/runtest.rs:3357:25


failures:
    [mir-opt] mir-opt/matches_reduce_branches.rs
    [mir-opt] mir-opt/matches_reduce_branches.rs

test result: FAILED. 162 passed; 1 failed; 4 ignored; 0 measured; 0 filtered out; finished in 2.26s



command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-tools-bin/compiletest" "--compile-lib-path" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/lib" "--run-lib-path" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/i686-unknown-linux-gnu/lib" "--rustc-path" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "--src-base" "/checkout/src/test/mir-opt" "--build-base" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/mir-opt" "--stage-id" "stage2-i686-unknown-linux-gnu" "--suite" "mir-opt" "--mode" "mir-opt" "--target" "i686-unknown-linux-gnu" "--host" "x86_64-unknown-linux-gnu" "--llvm-filecheck" "/usr/lib/llvm-12/bin/FileCheck" "--nodejs" "/usr/bin/node" "--linker" "cc" "--host-rustcflags" "-Crpath -O -Cdebuginfo=0  -Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "--target-rustcflags" "-Crpath -O -Cdebuginfo=0  -Lnative=/checkout/obj/build/i686-unknown-linux-gnu/native/rust-test-helpers" "--docck-python" "/usr/bin/python3" "--lldb-python" "/usr/bin/python3" "--gdb" "/usr/bin/gdb" "--quiet" "--llvm-version" "12.0.0" "--llvm-components" "aarch64 aarch64asmparser aarch64codegen aarch64desc aarch64disassembler aarch64info aarch64utils aggressiveinstcombine all all-targets amdgpu amdgpuasmparser amdgpucodegen amdgpudesc amdgpudisassembler amdgpuinfo amdgpuutils analysis arm armasmparser armcodegen armdesc armdisassembler arminfo armutils asmparser asmprinter avr avrasmparser avrcodegen avrdesc avrdisassembler avrinfo binaryformat bitreader bitstreamreader bitwriter bpf bpfasmparser bpfcodegen bpfdesc bpfdisassembler bpfinfo cfguard codegen core coroutines coverage debuginfocodeview debuginfodwarf debuginfogsym debuginfomsf debuginfopdb demangle dlltooldriver dwarflinker engine executionengine extensions filecheck frontendopenacc frontendopenmp fuzzmutate globalisel hellonew hexagon hexagonasmparser hexagoncodegen hexagondesc hexagondisassembler hexagoninfo instcombine instrumentation interfacestub interpreter ipo irreader jitlink lanai lanaiasmparser lanaicodegen lanaidesc lanaidisassembler lanaiinfo libdriver lineeditor linker lto mc mca mcdisassembler mcjit mcparser mips mipsasmparser mipscodegen mipsdesc mipsdisassembler mipsinfo mirparser msp430 msp430asmparser msp430codegen msp430desc msp430disassembler msp430info native nativecodegen nvptx nvptxcodegen nvptxdesc nvptxinfo objcarcopts object objectyaml option orcjit orcshared orctargetprocess passes perfjitevents powerpc powerpcasmparser powerpccodegen powerpcdesc powerpcdisassembler powerpcinfo profiledata remarks riscv riscvasmparser riscvcodegen riscvdesc riscvdisassembler riscvinfo runtimedyld scalaropts selectiondag sparc sparcasmparser sparccodegen sparcdesc sparcdisassembler sparcinfo support symbolize systemz systemzasmparser systemzcodegen systemzdesc systemzdisassembler systemzinfo tablegen target textapi transformutils vectorize webassembly webassemblyasmparser webassemblycodegen webassemblydesc webassemblydisassembler webassemblyinfo windowsmanifest x86 x86asmparser x86codegen x86desc x86disassembler x86info xcore xcorecodegen xcoredesc xcoredisassembler xcoreinfo xray" "--cc" "" "--cxx" "" "--cflags" "" "--adb-path" "adb" "--adb-test-dir" "/data/tmp/work" "--android-cross-path" "" "--channel" "nightly" "--color" "always"


Build completed unsuccessfully in 0:01:45

@scottmcm scottmcm closed this Nov 27, 2021
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 20, 2024
Tweak inlining attributes for slice indexing

Doing some experiments in response to this unexpected regression: rust-lang#120863 (comment)

I expect the opt changes to be addressed by something like reviving rust-lang#91222. The debug changes are what I'm interested in.

Codegen tests will probably fail from time to time in this PR, I will fix them up later but also I don't trust the opt-level-z one: rust-lang#119878 (comment)

r? `@ghost`
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 22, 2024
Avoid lowering code under dead SwitchInt targets

r? `@ghost`

Reviving rust-lang#91222 per rust-lang#120848
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 22, 2024
Avoid lowering code under dead SwitchInt targets

r? `@ghost`

Reviving rust-lang#91222 per rust-lang#120848
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 22, 2024
Avoid lowering code under dead SwitchInt targets

r? `@ghost`

Reviving rust-lang#91222 per rust-lang#120848
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 24, 2024
Avoid lowering code under dead SwitchInt targets

r? `@ghost`

Reviving rust-lang#91222 per rust-lang#120848
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 25, 2024
Avoid lowering code under dead SwitchInt targets

The objective of this PR is to detect and eliminate code which is guarded by an `if false`, even if that `false` is a constant which is not known until monomorphization, or is `intrinsics::debug_assertions()`.

The effect of this is that we generate no LLVM IR the standard library's unsafe preconditions, when they are compiled in a build where they should be immediately optimized out. This mono-time optimization ensures that builds which disable debug assertions do not grow a linkage requirement against `core`, which compiler-builtins currently needs: rust-lang#121552

This revives the codegen side of rust-lang#91222 as planned in rust-lang#120848.
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 13, 2024
Avoid lowering code under dead SwitchInt targets

The objective of this PR is to detect and eliminate code which is guarded by an `if false`, even if that `false` is a constant which is not known until monomorphization, or is `intrinsics::debug_assertions()`.

The effect of this is that we generate no LLVM IR the standard library's unsafe preconditions, when they are compiled in a build where they should be immediately optimized out. This mono-time optimization ensures that builds which disable debug assertions do not grow a linkage requirement against `core`, which compiler-builtins currently needs: rust-lang#121552

This revives the codegen side of rust-lang#91222 as planned in rust-lang#120848.
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. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

dead-code optimize if const { expr } even in opt-level=0
6 participants