-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix linker-plugin-lto only doing thin lto
When rust provides LLVM bitcode files to lld and the bitcode contains function summaries as used for thin lto, lld defaults to using thin lto. This prevents some optimizations that are only applied for fat lto. Set the `ThinLTO=0` module flag to signal lld to do fat lto. The analogous code in clang that sets this flag is here: https://github.com/llvm/llvm-project/blob/560149b5e3c891c64899e9912e29467a69dc3a4c/clang/lib/CodeGen/BackendUtil.cpp#L1150 The code in LLVM that queries the flag and defaults to thin lto if not set is here: https://github.com/llvm/llvm-project/blob/e258bca9505f35e0a22cb213a305eea9b76d11ea/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp#L4441-L4446
- Loading branch information
Showing
9 changed files
with
132 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#![feature(no_core, lang_items)] | ||
#![no_core] | ||
#![crate_type = "rlib"] | ||
|
||
#[lang = "sized"] | ||
trait Sized {} | ||
|
||
pub fn foo() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#![allow(internal_features)] | ||
#![feature(no_core, lang_items)] | ||
#![no_core] | ||
#![crate_type = "cdylib"] | ||
|
||
extern crate lib; | ||
|
||
#[unsafe(no_mangle)] | ||
pub fn bar() { | ||
lib::foo(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Compile a library with lto=fat, then compile a binary with lto=thin | ||
// and check that lto is applied with the library. | ||
// The goal is to mimic the standard library being build with lto=fat | ||
// and allowing users to build with lto=thin. | ||
|
||
//@ only-x86_64-unknown-linux-gnu | ||
|
||
use run_make_support::{dynamic_lib_name, llvm_objdump, rustc}; | ||
|
||
fn main() { | ||
rustc().input("lib.rs").opt_level("3").arg("-Clto=fat").run(); | ||
rustc().input("main.rs").panic("abort").opt_level("3").arg("-Clto=thin").run(); | ||
|
||
llvm_objdump() | ||
.input(dynamic_lib_name("main")) | ||
.arg("--disassemble-symbols=bar") | ||
.run() | ||
// The called function should be inlined. | ||
// Check that we have a ret (to detect tail | ||
// calls with a jmp) and no call. | ||
.assert_stdout_contains("bar") | ||
.assert_stdout_contains("ret") | ||
.assert_stdout_not_contains("foo") | ||
.assert_stdout_not_contains("call"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" | ||
target triple = "x86_64-unknown-linux-gnu" | ||
|
||
define void @ir_callee() { | ||
ret void | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#![feature(no_core, lang_items)] | ||
#![no_core] | ||
#![crate_type = "cdylib"] | ||
|
||
#[lang = "sized"] | ||
trait Sized {} | ||
|
||
extern "C" { | ||
fn ir_callee(); | ||
} | ||
|
||
#[no_mangle] | ||
extern "C" fn rs_foo() { | ||
unsafe { | ||
ir_callee(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Check that -C lto=fat with -C linker-plugin-lto actually works and can inline functions. | ||
// A library is created from LLVM IR, defining a single function. Then a dylib is compiled, | ||
// linking to the library and calling the function from the library. | ||
// The function from the library should end up inlined and disappear from the output. | ||
|
||
//@ only-x86_64-unknown-linux-gnu | ||
|
||
use run_make_support::{dynamic_lib_name, llvm_as, llvm_objdump, rustc}; | ||
|
||
fn main() { | ||
llvm_as().input("ir.ll").run(); | ||
rustc() | ||
.input("main.rs") | ||
.opt_level("3") | ||
.args(&["-Clto=fat", "-Clinker-plugin-lto", "-Clink-arg=ir.bc"]) | ||
.run(); | ||
|
||
llvm_objdump() | ||
.input(dynamic_lib_name("main")) | ||
.arg("--disassemble-symbols=rs_foo") | ||
.run() | ||
// The called function should be inlined. | ||
// Check that we have a ret (to detect tail | ||
// calls with a jmp) and no call. | ||
.assert_stdout_contains("foo") | ||
.assert_stdout_contains("ret") | ||
.assert_stdout_not_contains("call"); | ||
} |