2

If I compile with cargo rustc -- --emit=llvm-ir the compiler will emit LLVM IR.

Here are the LLVM passes that Rust uses. What LLVM passes, if any, have been performed on the emitted IR?

Is there any way to specify what passes you would like performed before emitting IR?

Lukas Kalbertodt
  • 79,749
  • 26
  • 255
  • 305
HiDefender
  • 2,088
  • 2
  • 14
  • 31
  • 1
    Not relevant to your question, but why do you want to know? – Shepmaster Apr 27 '18 at 19:07
  • 4
    Note that you've linked to a non-official fork of Rust's LLVM fork, and it was last updated **5 years ago**. I don't know why you picked that source, but it's highly unlikely to still be accurate. – Shepmaster Apr 27 '18 at 19:12
  • @Shepmaster I'm a grad student working on properties of programming languages. We work with LLVM IR, because many languages compile down to it. Didn't realize that was a fork, I'll try to find a list of official passes. Thanks. – HiDefender Apr 28 '18 at 20:11

1 Answers1

3

What LLVM passes, if any, have been performed on the emitted IR?

If you are using the nightly compiler, you could use the -Z print-llvm-passes to let LLVM print what passes are run. I'd recommend passing in -Z no-parallel-llvm and -C codegen-units=1 too to make the output cleaner and less repetitive.

$ rustc -C codegen-units=1 -Z no-parallel-llvm -Z print-llvm-passes 1.rs

Pass Arguments:  -tti -targetlibinfo -verify -ee-instrument
Target Transform Information
Target Library Information
  FunctionPass Manager
    Module Verifier
    Instrument function entry/exit with calls to e.g. mcount() (pre inlining)
Pass Arguments:  -tti -assumption-cache-tracker -profile-summary-info -targetlibinfo -forceattrs -basiccg -always-inline
Target Transform Information
Assumption Cache Tracker
Profile summary info
Target Library Information
  ModulePass Manager
    Force set function attributes
    CallGraph Construction
    Call Graph SCC Pass Manager
      Inliner for always_inline functions
...

(The -Z print-llvm-passes flag is equivalent to -C llvm-args=-debug-pass=Structure which is usable on stable rustc. However, without -Z no-parallel-llvm the output is quite unreadable.)


Is there any way to specify what passes you would like performed before emitting IR?

You could append additional passes using the -C passes argument. You may also clear the default optimization passes with -C no-prepopulate-passes. Example:

$ rustc -C passes=print-alias-sets 1.rs

Alias sets for function 'Alias sets for function '_ZN3std3sys4unix7process14process_common8ExitCode6as_i3217h65e06df78d6f4a47E':
_ZN3std2rt10lang_start17hd8fe8cd552faf2aaE':
...
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • By default, if using the 3 optimization level, are all optimization passes availaible enabled? – user2284570 Dec 03 '20 at 23:47
  • Afaik not all passes are enabled by default, not even on O3. And it's not just about a pass being used but also the right order. Some code patterns may require passes to be applied repeatedly to get some results. The default set of passes also applies some passes repeatedly. – the8472 Sep 28 '21 at 14:35