rustllvmrust-cargollvm-codegen

What LLVM passes are performed on emitted LLVM IR?


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?


Solution

  • 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':
    ...