Skip to content

-C lto -O does not enable LLVM optimizations for the LTO passes #22525

Closed

Description

@rprichard

-O seems to function like an alias for -C opt-level=2, but -O does not enable certain LTO LLVM optimizations (inlining?), whereas -C opt-level=2 does. This seems like a mistake.

The problem is this code in librustc_trans/back/lto.rs:

    let opt = sess.opts.cg.opt_level.unwrap_or(0) as libc::c_uint;

    let builder = llvm::LLVMPassManagerBuilderCreate();
    llvm::LLVMPassManagerBuilderSetOptLevel(builder, opt);
    llvm::LLVMPassManagerBuilderPopulateLTOPassManager(builder, pm,
        /* Internalize = */ False,
        /* RunInliner = */ True);
    llvm::LLVMPassManagerBuilderDispose(builder);

sess.opts.cg.opt_level is the -C opt-level setting, which is None with -O.

test.rs:

pub fn test() {}

output:

$ rustc test.rs --crate-type staticlib --emit llvm-ir -C lto -O
$ ls -l test.ll
-rw-rw-r-- 1 rprichard rprichard 36920440 Feb 19 00:25 test.ll
$ rustc test.rs --crate-type staticlib --emit llvm-ir -C lto -C opt-level=2
$ ls -l test.ll 
-rw-rw-r-- 1 rprichard rprichard 1312070 Feb 19 00:25 test.ll

I think we should base the LLVM optimization level on sess.opts.optimize. (Maybe it ought to use write::get_llvm_opt_level?)

Activity

  1. dotdash commented on Feb 19, 2015

    @dotdash
    Contributor

    Nice find! Probably not noticed by many because those using -C lto also go straight to -C opt-level=3 as well. Do you want to make a PR to fix this?

  2. added
    I-slowIssue: Problems and improvements with respect to performance of generated code.
    on Feb 19, 2015
  3. rprichard commented on Feb 19, 2015

    @rprichard
    ContributorAuthor

    I'd be happy to make a PR, though I probably won't get to it until tomorrow.

  4. added a commit that references this issue on Feb 19, 2015
    dc3bc90
  5. added a commit that references this issue on Jun 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-slowIssue: Problems and improvements with respect to performance of generated code.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions