Skip to content

Commit 20f597f

Browse files
committed
Add LLVM module flags required for the VFE opt
To apply the optimization the `Virtual Function Elim` module flag has to be set. To apply this optimization post-link the `LTOPostLink` module flag has to be set.
1 parent def3fd8 commit 20f597f

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

compiler/rustc_codegen_llvm/src/back/lto.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -586,9 +586,21 @@ pub(crate) fn run_pass_manager(
586586
// LTO-specific optimization passes that LLVM provides.
587587
//
588588
// This code is based off the code found in llvm's LTO code generator:
589-
// tools/lto/LTOCodeGenerator.cpp
589+
// llvm/lib/LTO/LTOCodeGenerator.cpp
590590
debug!("running the pass manager");
591591
unsafe {
592+
if !llvm::LLVMRustHasModuleFlag(
593+
module.module_llvm.llmod(),
594+
"LTOPostLink".as_ptr().cast(),
595+
11,
596+
) {
597+
llvm::LLVMRustAddModuleFlag(
598+
module.module_llvm.llmod(),
599+
llvm::LLVMModFlagBehavior::Error,
600+
"LTOPostLink\0".as_ptr().cast(),
601+
1,
602+
);
603+
}
592604
if llvm_util::should_use_new_llvm_pass_manager(
593605
&config.new_llvm_pass_manager,
594606
&cgcx.target_arch,

compiler/rustc_codegen_llvm/src/context.rs

+9
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,15 @@ pub unsafe fn create_module<'ll>(
326326
)
327327
}
328328

329+
if sess.opts.debugging_opts.virtual_function_elimination {
330+
llvm::LLVMRustAddModuleFlag(
331+
llmod,
332+
llvm::LLVMModFlagBehavior::Error,
333+
"Virtual Function Elim\0".as_ptr().cast(),
334+
1,
335+
);
336+
}
337+
329338
llmod
330339
}
331340

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1936,6 +1936,7 @@ extern "C" {
19361936
name: *const c_char,
19371937
value: u32,
19381938
);
1939+
pub fn LLVMRustHasModuleFlag(M: &Module, name: *const c_char, len: size_t) -> bool;
19391940

19401941
pub fn LLVMRustMetadataAsValue<'a>(C: &'a Context, MD: &'a Metadata) -> &'a Value;
19411942

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,11 @@ extern "C" void LLVMRustAddModuleFlag(
672672
unwrap(M)->addModuleFlag(MergeBehavior, Name, Value);
673673
}
674674

675+
extern "C" bool LLVMRustHasModuleFlag(LLVMModuleRef M, const char *Name,
676+
size_t Len) {
677+
return unwrap(M)->getModuleFlag(StringRef(Name, Len)) != nullptr;
678+
}
679+
675680
extern "C" LLVMValueRef LLVMRustMetadataAsValue(LLVMContextRef C, LLVMMetadataRef MD) {
676681
return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
677682
}

0 commit comments

Comments
 (0)