Skip to content

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

Closed
@rprichard

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?)

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