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 inline-llvm option for disabling/enabling LLVM inlining #100293

Merged
merged 1 commit into from
Sep 12, 2022

Conversation

yanchen4791
Copy link
Contributor

In this PR, a new -Z option inline-llvm is added in order to be able to turn on/off LLVM inlining.

The capability of turning on/off inlining in LLVM backend is needed for testing performance implications of using recently enabled inlining in rustc's frontend (with -Z inline-mir=yes option, #91743). It would be interesting to see the performance effect using rustc's frontend inlining only without LLVM inlining enabled. Currently LLVM is still doing inlining no mater what value inline-mir is set to. With the option inline-llvm being added in this PR, user can turn off LLVM inlining by using -Z inline-llvm=no option (the default of inline-llvm is 'yes', LLVM inlining enabled).

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Aug 9, 2022
@rust-highfive
Copy link
Collaborator

r? @jackh726

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 9, 2022
@yanchen4791
Copy link
Contributor Author

r? @nnethercote

Some comparisons of rustc-perf results from local x86_64 linux machine with and without LLVM inlining controlled with the newly added inline-llvm option when inline-mir=yes is set:


Instructions:u of all benchmarks with Profile : opt, Scenario : full, Category : Primary

inline-mir=no, inline-llvm=yes inline-mir=yes, inline-llvm=yes inline-mir=yes, inline-llvm=no
0% (baseline) +0.50% -14.41%


Instructions:u of all benchmarks with Profile : opt, Scenario : full, Category : Primary & Secondary
inline-mir=no, inline-llvm=yes inline-mir=yes, inline-llvm=yes inline-mir=yes, inline-llvm=no
0% (baseline) +0.75% -7.96%

@rust-highfive rust-highfive assigned nnethercote and unassigned jackh726 Aug 9, 2022
@Mark-Simulacrum
Copy link
Member

Those results don't seem unexpected to me, but the real question I guess is the performance impact of disabling LLVM inlining -- one way to try to gauge that is to adjust our CI to run without it, and then see how much slower the produced rustc binaries are. My guess is "much slower, >50%", but I don't really know.

@nnethercote
Copy link
Contributor

For some context: I suggested this as a good first task for @yanchen4791 to look into, based on an idea from @joshtriplett. In the next day or two I will look at both the compile time and runtime effects.

Thanks for filing the PR, @yanchen4791!

@yanchen4791
Copy link
Contributor Author

Thanks for advice, @nnethercote !

Tested the rustc binary produced with LLVM inlining disabled (and MIR inlining enabled) using the following build command:

RUSTFLAGS_NOT_BOOTSTRAP="-Z inline-mir=yes -Z inline-llvm=no" ./x.py build

The produced rustc seems running faster than the rustc produced by default (-Z inline-mir=no -Z inline-llvm=yes").

Here are comparisons of 2 versions of rustc's, using raw data of instructions:u of Primary and Secondary benchmarks. With Primary benchmarks, the total combined instructions:u reduced 5.19%, while with Secondary benchmarks, total combined instructions:u increased 0.05%. (Note: I am not using the average percentage number here, it is kind of misleading. Instead, I am using the change in percentage of total number of instructions combined all benchmarks, this I think should be a more accurate measure for rustc performance)

instructions:u, Primary
Benchmark Profile Scenario % Change Significance Factor rustc built with LLVM inlining enabled rustc built with LLVM inlining disabled
helloworld opt full 42.99% 214.96x 407,423,342 582,580,064
cargo-0.60.0 opt full -6.77% 33.84x 758,280,046,569 706,959,562,484
clap-3.1.6 opt full -6.75% 33.76x 66,788,608,111 62,279,446,981
syn-1.0.89 opt full -6.07% 30.33x 65,627,563,878 61,646,963,419
html5ever-0.26.0 opt full -5.98% 29.90x 7,221,901,745 6,790,091,362
ripgrep-13.0.0 opt full -4.74% 23.68x 115,639,580,282 110,162,390,303
cranelift-codegen-0.82.1 opt full -4.48% 22.39x 220,659,697,912 210,777,466,299
webrender-2022 opt full -4.28% 21.38x 431,241,948,876 412,803,475,146
image-0.24.1 opt full -3.99% 19.94x 222,154,608,607 213,296,064,818
regex-1.5.5 opt full -3.93% 19.66x 74,743,565,807 71,804,575,529
serde_derive-1.0.136 opt full -2.93% 14.63x 82,563,686,535 80,147,312,244
hyper-0.14.18 opt full -2.86% 14.30x 30,174,957,120 29,311,996,958
bitmaps-3.1.0 opt full -2.49% 12.44x 4,588,689,647 4,474,565,110
unicode-normalization-0.1.19 opt full -1.56% 7.79x 6,546,553,109 6,444,495,516
serde-1.0.136 opt full -0.73% 3.64x 18,877,117,537 18,739,606,179
Total     -5.19%   2,105,515,949,077 1,996,220,592,412

instructions:u, Secondary
Benchmark Profile Scenario % Change Significance Factor rustc built with LLVM inlining enabled rustc built with LLVM inlining disabled
token-stream-stress opt full 40.63% 203.14x 487,643,896 685,763,993
unify-linearly opt full 34.26% 171.31x 508,496,651 682,722,547
issue-58319 opt full 22.78% 113.89x 764,603,485 938,771,102
issue-46449 opt full -17.98% 89.90x 1,349,705,817 1,107,015,583
issue-88862 opt full 13.82% 69.09x 782,531,601 890,656,865
regression-31157 opt full -8.08% 40.41x 15,765,757,699 14,491,444,758
unused-warnings opt full 7.16% 35.81x 2,433,770,167 2,608,055,064
coercions opt full 4.96% 24.78x 3,496,983,476 3,670,301,243
projection-caching opt full 4.81% 24.03x 3,620,614,561 3,794,589,406
tt-muncher opt full 2.55% 12.76x 7,396,393,270 7,585,135,253
deeply-nested-multi opt full -1.81% 9.03x 5,383,591,122 5,286,324,502
tuple-stress opt full 0.92% 4.62x 18,837,793,506 19,011,865,396
deep-vector opt full 0.67% 3.35x 17,333,531,114 17,449,808,611
Total     0.05%    78,161,416,365 78,202,454,323

@nnethercote
Copy link
Contributor

@yanchen4791: Can you update this PR so that inline_llvm is false by default? Then we can do a performance run on CI.

@yanchen4791
Copy link
Contributor Author

@nnethercote Sure. Make inline-llvm false by default. How about default of inline-mir ? Currently default value of inline-mir is false. Should it be changed to true for CI?

@nnethercote
Copy link
Contributor

inline-mir defaults to false? I'm surprised by that. @cjgillot, can you explain? I thought #91743 enabled it...

@yanchen4791
Copy link
Contributor Author

Some inline/codegen tests failed after setting inline_llvm default to false. Here is the list:

failures:

    [ui] src/test/ui/lto/thin-lto-inlines.rs
    [ui] src/test/ui/lto/thin-lto-inlines2.rs

    [codegen] src/test/codegen/alloc-optimisation.rs
    [codegen] src/test/codegen/array-equality.rs
    [codegen] src/test/codegen/atomic-operations.rs
    [codegen] src/test/codegen/autovectorize-f32x4.rs
    [codegen] src/test/codegen/binary-search-index-no-bound-check.rs
    [codegen] src/test/codegen/catch-unwind.rs
    [codegen] src/test/codegen/dealloc-no-unwind.rs
    [codegen] src/test/codegen/inline-always-works-always.rs#NO-OPT
    [codegen] src/test/codegen/inline-hint.rs
    [codegen] src/test/codegen/issue-13018.rs
    [codegen] src/test/codegen/issue-34634.rs
    [codegen] src/test/codegen/issue-34947-pow-i32.rs
    [codegen] src/test/codegen/issue-37945.rs
    [codegen] src/test/codegen/issue-45222.rs
    [codegen] src/test/codegen/issue-45466.rs
    [codegen] src/test/codegen/issue-45964-bounds-check-slice-pos.rs
    [codegen] src/test/codegen/issue-69101-bounds-check.rs
    [codegen] src/test/codegen/issue-73396-bounds-check-after-position.rs
    [codegen] src/test/codegen/issue-75659.rs
    [codegen] src/test/codegen/issue-96497-slice-size-nowrap.rs
    [codegen] src/test/codegen/non-terminate/infinite-loop-1.rs
    [codegen] src/test/codegen/non-terminate/infinite-loop-2.rs
    [codegen] src/test/codegen/non-terminate/nonempty-infinite-loop.rs
    [codegen] src/test/codegen/repeat-trusted-len.rs
    [codegen] src/test/codegen/simd-wide-sum.rs
    [codegen] src/test/codegen/slice-as_chunks.rs
    [codegen] src/test/codegen/slice-position-bounds-check.rs
    [codegen] src/test/codegen/slice-ref-equality.rs
    [codegen] src/test/codegen/slice-reverse.rs
    [codegen] src/test/codegen/slice-windows-no-bounds-check.rs
    [codegen] src/test/codegen/some-global-nonnull.rs
    [codegen] src/test/codegen/swap-large-types.rs
    [codegen] src/test/codegen/swap-simd-types.rs
    [codegen] src/test/codegen/swap-small-types.rs
    [codegen] src/test/codegen/target-feature-overrides.rs#COMPAT
    [codegen] src/test/codegen/thread-local.rs
    [codegen] src/test/codegen/to_vec.rs
    [codegen] src/test/codegen/vec-calloc-llvm14.rs
    [codegen] src/test/codegen/vec-in-place.rs
    [codegen] src/test/codegen/vec-iter-collect-len.rs
    [codegen] src/test/codegen/vec-optimizes-away.rs
    [codegen] src/test/codegen/vec-shrink-panik.rs

@cjgillot
Copy link
Contributor

@nnethercote the documentation is wrong. inline-mir defaults to None, which means "act depending on opt-level". inline-mir=yes/no will force/deny MIR inlining independently of the value of opt-level.
The flag documentation should really say "forcibly enable or disable MIR inlining."

@yanchen4791
Copy link
Contributor Author

With inline_llvm default being set to false, in addition to failures of some test cases of codegen and inline, x.py test hangs at the following stage:

     Running tests/lib.rs (build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-linux-gnu/release/deps/coretests-d5e01cd647bf6a6e)

running 1458 tests
........................................................................................ 88/1458
........................................................................................ 176/1458
........................................................................................ 264/1458
........................................................................................ 352/1458
........................................................................................ 440/1458
........................................................................................ 528/1458
.........................................................ii............................. 616/1458
........................................................................................ 704/1458
........................................................................................ 792/1458
........................................................................................ 880/1458
........................................................................................ 968/1458
........................................................................................ 1056/1458
........................................................................................ 1144/1458
........................................................................................ 1232/1458
........................................................................................ 1320/1458
........................................................................................ 1408/1458
............................................test slice::take_in_bounds_max_range_from has been running for over 60 seconds
test slice::take_in_bounds_max_range_to has been running for over 60 seconds
test slice::take_mut_in_bounds_max_range_from has been running for over 60 seconds
test slice::take_mut_in_bounds_max_range_to has been running for over 60 seconds
test slice::take_mut_oob_max_range_to_inclusive has been running for over 60 seconds
test slice::take_oob_max_range_to_inclusive has been running for over 60 seconds

@Kobzol
Copy link
Contributor

Kobzol commented Aug 11, 2022

Since we're quite interested in perf, I don't think that we have to resolve these failures outright. As long as the compiler builds, we can run perf, it doesn't require passing tests. So if you push a version that defaults to inline-llvm=false, I can start a perf. run. Of course if the perf. run fails then it will have to be fixed.

@yanchen4791
Copy link
Contributor Author

@Kobzol Sure, compiler builds without any problem. Pushed with inline_llvm defaults to false.

@Kobzol
Copy link
Contributor

Kobzol commented Aug 11, 2022

@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 Aug 11, 2022
@bors
Copy link
Contributor

bors commented Aug 11, 2022

⌛ Trying commit 7b27cbb1c4403562563661625d7ca2c38f9a4d6a with merge 9e67400a96e1cc2dc2652554e389f59791dc41b3...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Aug 11, 2022

☀️ Try build successful - checks-actions
Build commit: 9e67400a96e1cc2dc2652554e389f59791dc41b3 (9e67400a96e1cc2dc2652554e389f59791dc41b3)

@rust-timer
Copy link
Collaborator

Queued 9e67400a96e1cc2dc2652554e389f59791dc41b3 with parent aeb5067, future comparison URL.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (9e67400a96e1cc2dc2652554e389f59791dc41b3): comparison url.

Instruction count

  • Primary benchmarks: mixed results
  • Secondary benchmarks: mixed results
mean1 max count2
Regressions ❌
(primary)
224.7% 331.7% 231
Regressions ❌
(secondary)
216.0% 386.4% 266
Improvements ✅
(primary)
-25.4% -57.9% 20
Improvements ✅
(secondary)
-24.3% -57.4% 3
All ❌✅ (primary) 204.8% 331.7% 251

Max RSS (memory usage)

Results
  • Primary benchmarks: mixed results
  • Secondary benchmarks: mixed results
mean1 max count2
Regressions ❌
(primary)
3.7% 8.3% 8
Regressions ❌
(secondary)
6.3% 10.6% 3
Improvements ✅
(primary)
-6.6% -30.5% 204
Improvements ✅
(secondary)
-6.5% -17.6% 229
All ❌✅ (primary) -6.2% -30.5% 212

Cycles

Results
  • Primary benchmarks: mixed results
  • Secondary benchmarks: mixed results
mean1 max count2
Regressions ❌
(primary)
281.7% 561.0% 228
Regressions ❌
(secondary)
305.5% 643.3% 267
Improvements ✅
(primary)
-24.7% -60.4% 23
Improvements ✅
(secondary)
-29.3% -52.1% 2
All ❌✅ (primary) 253.6% 561.0% 251

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 may lead 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

Footnotes

  1. the arithmetic mean of the percent change 2 3

  2. number of relevant changes 2 3

@rustbot rustbot added the perf-regression Performance regression. label Aug 12, 2022
@Kobzol
Copy link
Contributor

Kobzol commented Aug 26, 2022

Bootstrap timings reduced by ~30 % when they're not LLVM inlined (but the compiling rustc is normally optimized), quite cool.

@tmandry
Copy link
Member

tmandry commented Aug 27, 2022

I'm having trouble keeping track of which results are considered valid and what is being measured. I would be grateful if someone made a list summarizing the experiments with links to the result summaries and what they mean.

Which stages of the build do bootstrap timings include? I looked for docs on this but couldn't find any.

@Kobzol
Copy link
Contributor

Kobzol commented Aug 27, 2022

Which stages of the build do bootstrap timings include? I looked for docs on this but couldn't find any.

If I understand it correctly, the bootstrap timings are gathered from compiling rustc/compiler with rustc from the PR. In other words, the rustc from the PR is used as the bootstrap compiler and builds stage 1. @Mark-Simulacrum Is this correct?

I tried to gather a summary of the results:

LLVM inlining disabled for rustc and for benchmarks and bootstrap

https://perf.rust-lang.org/compare.html?start=aeb5067967ef58e4a324b19dd0dba2f385d5959f&end=9e67400a96e1cc2dc2652554e389f59791dc41b3&stat=instructions:u

LLVM inlining disabled for rustc and for benchmarks and bootstrap, MIR inlining enabled for everything

https://perf.rust-lang.org/compare.html?start=9b4ea391a132ec5f5de40079597ab7ff2fd691ad&end=b102bd401c3f4366afba3adb1b706cec1018f7c3&stat=instructions:u

LLVM inlining enabled for rustc, disabled for benchmarks and bootstrap, MIR inlining enabled for everything

https://perf.rust-lang.org/compare.html?start=4d45b0745ab227feb9000bc15713ade4b99241ea&end=fc06468a9d6916240c025225bacc5f19817e4781&stat=instructions:u

LLVM inlining enabled for rustc, disabled for benchmarks and bootstrap, MIR inlining enabled for opt builds

https://perf.rust-lang.org/compare.html?start=7480389611f9d04bd34adf41a2b3029be4eb815e&end=b0cf6e1fb74239b3778b894185e4d7af516653ad&stat=instructions:u

LLVM inlining enabled for rustc and std, disabled for benchmarks and boostrap, MIR inlining enabled for everything

https://perf.rust-lang.org/compare.html?start=76f3b891a0c48e128c5a43ef57e70d86735e1cf2&end=7714bc32bda08036b7d1f2d88bedaa53bb269901&stat=instructions:u

I couldn't easily copy the summary tables, so I only posted links here. I'm not sure whether the inlining for std affects anything.

@yanchen4791
Copy link
Contributor Author

@nnethercote I've cleaned up testing code and rebased the branch. Also changed the documentation of inline-mir default from "no" to "off" since the default value of inline_mir is None, not Some(false).

inline_mir: Option<bool> = (None, parse_opt_bool, [TRACKED],
"enable MIR inlining (default: no)"),
"enable MIR inlining (default: off)"),
Copy link
Contributor

Choose a reason for hiding this comment

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

No other options use default: off, so I think this should stay as default: no.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know why inline_mir is an Option<bool> rather than just a bool. The comment on parse_opt_bool says:

    /// Use this for any boolean option that lacks a static default. (The
    /// actions taken when such an option is not specified will depend on
    /// other factors, such as other options, or target options.)

Seems like inline_mir has an obvious static value. Anyway, not really important for this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, changed back. Thanks.

Copy link
Member

Choose a reason for hiding this comment

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

My understanding is that MIR inlining is not on or off by default but depends on other options: whether it's a debug or release build, whether incremental is enabled.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, then the use of Option<bool> is good for inline_mir. Thanks for the clarification.

@@ -54,7 +54,8 @@
-Z incremental-info=val -- print high-level information about incremental reuse (or the lack thereof) (default: no)
-Z incremental-relative-spans=val -- hash spans relative to their parent item for incr. comp. (default: no)
-Z incremental-verify-ich=val -- verify incr. comp. hashes of green query instances (default: no)
-Z inline-mir=val -- enable MIR inlining (default: no)
-Z inline-llvm=val -- enable LLVM inlining (default: yes)
-Z inline-mir=val -- enable MIR inlining (default: off)
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here.

@nnethercote
Copy link
Contributor

It just needs the off occurrences to be changed back to no and then it should be good. Thanks.

@nnethercote
Copy link
Contributor

@bors r+ rollup=always

@bors
Copy link
Contributor

bors commented Sep 12, 2022

📌 Commit 052887e has been approved by nnethercote

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 Sep 12, 2022
@bors
Copy link
Contributor

bors commented Sep 12, 2022

⌛ Testing commit 052887e with merge c0ec2e0a4576203fb7a9cd9377cb293617939fb7...

@bors
Copy link
Contributor

bors commented Sep 12, 2022

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Sep 12, 2022
@Dylan-DPC
Copy link
Member

@bors retry

@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 Sep 12, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 12, 2022
Rollup of 9 pull requests

Successful merges:

 - rust-lang#100293 (Add inline-llvm option for disabling/enabling LLVM inlining)
 - rust-lang#100767 (Remove manual <[u8]>::escape_ascii)
 - rust-lang#101668 (Suggest pub instead of public for const type item)
 - rust-lang#101671 (Fix naming format of IEEE 754 standard)
 - rust-lang#101676 (Check that the types in return position `impl Trait` in traits are well-formed)
 - rust-lang#101681 (Deny return-position `impl Trait` in traits for object safety)
 - rust-lang#101693 (Update browser UI test 0 10)
 - rust-lang#101701 (Rustdoc-Json: Add tests for trait impls.)
 - rust-lang#101706 (rustdoc: remove no-op `#search`)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@rust-log-analyzer
Copy link
Collaborator

A job failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)

@bors bors merged commit 4137032 into rust-lang:master Sep 12, 2022
@rustbot rustbot added this to the 1.65.0 milestone Sep 12, 2022
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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) 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.