Skip to content

Commit 1b3a5f2

Browse files
committed
Auto merge of rust-lang#91125 - eskarn:llvm-passes-plugin-support, r=nagisa
Allow loading LLVM plugins with both legacy and new pass manager Opening a draft PR to get feedback and start discussion on this feature. There is already a codegen option `passes` which allow giving a list of LLVM pass names, however we currently can't use a LLVM pass plugin (as described here : https://llvm.org/docs/WritingAnLLVMPass.html), the only available passes are the LLVM built-in ones. The proposed modification would be to add another codegen option `pass-plugins`, which can be set with a list of paths to shared library files. These libraries are loaded using the LLVM function `PassPlugin::Load`, which calls the expected symbol `lvmGetPassPluginInfo`, and register the pipeline parsing and optimization callbacks. An example usage with a single plugin and 3 passes would look like this in the `.cargo/config`: ```toml rustflags = [ "-C", "pass-plugins=/tmp/libLLVMPassPlugin", "-C", "passes=pass1 pass2 pass3", ] ``` This would give the same functionality as the opt LLVM tool directly integrated in rust build system. Additionally, we can also not specify the `passes` option, and use a plugin which inserts passes in the optimization pipeline, as one could do using clang.
2 parents d331cb7 + f431df0 commit 1b3a5f2

File tree

6 files changed

+58
-27
lines changed

6 files changed

+58
-27
lines changed

Diff for: compiler/rustc_codegen_llvm/src/back/lto.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::back::write::{
33
};
44
use crate::llvm::archive_ro::ArchiveRO;
55
use crate::llvm::{self, build_string, False, True};
6-
use crate::{LlvmCodegenBackend, ModuleLlvm};
6+
use crate::{llvm_util, LlvmCodegenBackend, ModuleLlvm};
77
use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule, ThinShared};
88
use rustc_codegen_ssa::back::symbol_export;
99
use rustc_codegen_ssa::back::write::{
@@ -596,7 +596,10 @@ pub(crate) fn run_pass_manager(
596596
// tools/lto/LTOCodeGenerator.cpp
597597
debug!("running the pass manager");
598598
unsafe {
599-
if write::should_use_new_llvm_pass_manager(cgcx, config) {
599+
if llvm_util::should_use_new_llvm_pass_manager(
600+
&config.new_llvm_pass_manager,
601+
&cgcx.target_arch,
602+
) {
600603
let opt_stage = if thin { llvm::OptStage::ThinLTO } else { llvm::OptStage::FatLTO };
601604
let opt_level = config.opt_level.unwrap_or(config::OptLevel::No);
602605
write::optimize_with_new_llvm_pass_manager(

Diff for: compiler/rustc_codegen_llvm/src/back/write.rs

+8-16
Original file line numberDiff line numberDiff line change
@@ -416,21 +416,6 @@ fn get_pgo_sample_use_path(config: &ModuleConfig) -> Option<CString> {
416416
.map(|path_buf| CString::new(path_buf.to_string_lossy().as_bytes()).unwrap())
417417
}
418418

419-
pub(crate) fn should_use_new_llvm_pass_manager(
420-
cgcx: &CodegenContext<LlvmCodegenBackend>,
421-
config: &ModuleConfig,
422-
) -> bool {
423-
// The new pass manager is enabled by default for LLVM >= 13.
424-
// This matches Clang, which also enables it since Clang 13.
425-
426-
// FIXME: There are some perf issues with the new pass manager
427-
// when targeting s390x, so it is temporarily disabled for that
428-
// arch, see https://github.com/rust-lang/rust/issues/89609
429-
config
430-
.new_llvm_pass_manager
431-
.unwrap_or_else(|| cgcx.target_arch != "s390x" && llvm_util::get_version() >= (13, 0, 0))
432-
}
433-
434419
pub(crate) unsafe fn optimize_with_new_llvm_pass_manager(
435420
cgcx: &CodegenContext<LlvmCodegenBackend>,
436421
diag_handler: &Handler,
@@ -473,6 +458,8 @@ pub(crate) unsafe fn optimize_with_new_llvm_pass_manager(
473458

474459
let extra_passes = config.passes.join(",");
475460

461+
let llvm_plugins = config.llvm_plugins.join(",");
462+
476463
// FIXME: NewPM doesn't provide a facility to pass custom InlineParams.
477464
// We would have to add upstream support for this first, before we can support
478465
// config.inline_threshold and our more aggressive default thresholds.
@@ -502,6 +489,8 @@ pub(crate) unsafe fn optimize_with_new_llvm_pass_manager(
502489
selfprofile_after_pass_callback,
503490
extra_passes.as_ptr().cast(),
504491
extra_passes.len(),
492+
llvm_plugins.as_ptr().cast(),
493+
llvm_plugins.len(),
505494
);
506495
result.into_result().map_err(|()| llvm_err(diag_handler, "failed to run LLVM passes"))
507496
}
@@ -530,7 +519,10 @@ pub(crate) unsafe fn optimize(
530519
}
531520

532521
if let Some(opt_level) = config.opt_level {
533-
if should_use_new_llvm_pass_manager(cgcx, config) {
522+
if llvm_util::should_use_new_llvm_pass_manager(
523+
&config.new_llvm_pass_manager,
524+
&cgcx.target_arch,
525+
) {
534526
let opt_stage = match cgcx.lto {
535527
Lto::Fat => llvm::OptStage::PreLinkFatLTO,
536528
Lto::Thin | Lto::ThinLocal => llvm::OptStage::PreLinkThinLTO,

Diff for: compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2313,6 +2313,8 @@ extern "C" {
23132313
end_callback: SelfProfileAfterPassCallback,
23142314
ExtraPasses: *const c_char,
23152315
ExtraPassesLen: size_t,
2316+
LLVMPlugins: *const c_char,
2317+
LLVMPluginsLen: size_t,
23162318
) -> LLVMRustResult;
23172319
pub fn LLVMRustPrintModule(
23182320
M: &Module,

Diff for: compiler/rustc_codegen_llvm/src/llvm_util.rs

+24-8
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,20 @@ unsafe fn configure_llvm(sess: &Session) {
121121

122122
llvm::LLVMInitializePasses();
123123

124-
// Register LLVM plugins by loading them into the compiler process.
125-
for plugin in &sess.opts.debugging_opts.llvm_plugins {
126-
let lib = Library::new(plugin).unwrap_or_else(|e| bug!("couldn't load plugin: {}", e));
127-
debug!("LLVM plugin loaded successfully {:?} ({})", lib, plugin);
128-
129-
// Intentionally leak the dynamic library. We can't ever unload it
130-
// since the library can make things that will live arbitrarily long.
131-
mem::forget(lib);
124+
// Use the legacy plugin registration if we don't use the new pass manager
125+
if !should_use_new_llvm_pass_manager(
126+
&sess.opts.debugging_opts.new_llvm_pass_manager,
127+
&sess.target.arch,
128+
) {
129+
// Register LLVM plugins by loading them into the compiler process.
130+
for plugin in &sess.opts.debugging_opts.llvm_plugins {
131+
let lib = Library::new(plugin).unwrap_or_else(|e| bug!("couldn't load plugin: {}", e));
132+
debug!("LLVM plugin loaded successfully {:?} ({})", lib, plugin);
133+
134+
// Intentionally leak the dynamic library. We can't ever unload it
135+
// since the library can make things that will live arbitrarily long.
136+
mem::forget(lib);
137+
}
132138
}
133139

134140
rustc_llvm::initialize_available_targets();
@@ -411,3 +417,13 @@ pub fn tune_cpu(sess: &Session) -> Option<&str> {
411417
let name = sess.opts.debugging_opts.tune_cpu.as_ref()?;
412418
Some(handle_native(name))
413419
}
420+
421+
pub(crate) fn should_use_new_llvm_pass_manager(user_opt: &Option<bool>, target_arch: &str) -> bool {
422+
// The new pass manager is enabled by default for LLVM >= 13.
423+
// This matches Clang, which also enables it since Clang 13.
424+
425+
// FIXME: There are some perf issues with the new pass manager
426+
// when targeting s390x, so it is temporarily disabled for that
427+
// arch, see https://github.com/rust-lang/rust/issues/89609
428+
user_opt.unwrap_or_else(|| target_arch != "s390x" && llvm_util::get_version() >= (13, 0, 0))
429+
}

Diff for: compiler/rustc_codegen_ssa/src/back/write.rs

+2
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ pub struct ModuleConfig {
113113
pub inline_threshold: Option<u32>,
114114
pub new_llvm_pass_manager: Option<bool>,
115115
pub emit_lifetime_markers: bool,
116+
pub llvm_plugins: Vec<String>,
116117
}
117118

118119
impl ModuleConfig {
@@ -260,6 +261,7 @@ impl ModuleConfig {
260261
inline_threshold: sess.opts.cg.inline_threshold,
261262
new_llvm_pass_manager: sess.opts.debugging_opts.new_llvm_pass_manager,
262263
emit_lifetime_markers: sess.emit_lifetime_markers(),
264+
llvm_plugins: if_regular!(sess.opts.debugging_opts.llvm_plugins.clone(), vec![]),
263265
}
264266
}
265267

Diff for: compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

+17-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/Object/ObjectFile.h"
1818
#include "llvm/Object/IRObjectFile.h"
1919
#include "llvm/Passes/PassBuilder.h"
20+
#include "llvm/Passes/PassPlugin.h"
2021
#include "llvm/Passes/StandardInstrumentations.h"
2122
#include "llvm/Support/CBindingWrapping.h"
2223
#include "llvm/Support/FileSystem.h"
@@ -753,7 +754,8 @@ LLVMRustOptimizeWithNewPassManager(
753754
void* LlvmSelfProfiler,
754755
LLVMRustSelfProfileBeforePassCallback BeforePassCallback,
755756
LLVMRustSelfProfileAfterPassCallback AfterPassCallback,
756-
const char *ExtraPasses, size_t ExtraPassesLen) {
757+
const char *ExtraPasses, size_t ExtraPassesLen,
758+
const char *LLVMPlugins, size_t LLVMPluginsLen) {
757759
Module *TheModule = unwrap(ModuleRef);
758760
TargetMachine *TM = unwrap(TMRef);
759761
OptimizationLevel OptLevel = fromRust(OptLevelRust);
@@ -924,6 +926,20 @@ LLVMRustOptimizeWithNewPassManager(
924926
}
925927
}
926928

929+
if (LLVMPluginsLen) {
930+
auto PluginsStr = StringRef(LLVMPlugins, LLVMPluginsLen);
931+
SmallVector<StringRef> Plugins;
932+
PluginsStr.split(Plugins, ',', -1, false);
933+
for (auto PluginPath: Plugins) {
934+
auto Plugin = PassPlugin::Load(PluginPath.str());
935+
if (!Plugin) {
936+
LLVMRustSetLastError(("Failed to load pass plugin" + PluginPath.str()).c_str());
937+
continue;
938+
}
939+
Plugin->registerPassBuilderCallbacks(PB);
940+
}
941+
}
942+
927943
#if LLVM_VERSION_GE(13, 0)
928944
ModulePassManager MPM;
929945
#else

0 commit comments

Comments
 (0)