Skip to content

Commit 97cf461

Browse files
committed
Add a codegen option to allow loading LLVM pass plugins
1 parent 4a7fb97 commit 97cf461

File tree

6 files changed

+30
-1
lines changed

6 files changed

+30
-1
lines changed

compiler/rustc_codegen_llvm/src/back/write.rs

+4
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,8 @@ pub(crate) unsafe fn optimize_with_new_llvm_pass_manager(
470470

471471
let extra_passes = config.passes.join(",");
472472

473+
let pass_plugins = config.pass_plugins.join(" ");
474+
473475
// FIXME: NewPM doesn't provide a facility to pass custom InlineParams.
474476
// We would have to add upstream support for this first, before we can support
475477
// config.inline_threshold and our more aggressive default thresholds.
@@ -499,6 +501,8 @@ pub(crate) unsafe fn optimize_with_new_llvm_pass_manager(
499501
selfprofile_after_pass_callback,
500502
extra_passes.as_ptr().cast(),
501503
extra_passes.len(),
504+
pass_plugins.as_ptr().cast(),
505+
pass_plugins.len(),
502506
);
503507
result.into_result().map_err(|()| llvm_err(diag_handler, "failed to run LLVM passes"))
504508
}

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2304,6 +2304,8 @@ extern "C" {
23042304
end_callback: SelfProfileAfterPassCallback,
23052305
ExtraPasses: *const c_char,
23062306
ExtraPassesLen: size_t,
2307+
PassPlugins: *const c_char,
2308+
PassPluginsLen: size_t,
23072309
) -> LLVMRustResult;
23082310
pub fn LLVMRustPrintModule(
23092311
M: &'a Module,

compiler/rustc_codegen_ssa/src/back/write.rs

+4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ pub enum BitcodeSection {
7474
pub struct ModuleConfig {
7575
/// Names of additional optimization passes to run.
7676
pub passes: Vec<String>,
77+
/// Paths of LLVM pass plugins to load.
78+
pub pass_plugins: Vec<String>,
7779
/// Some(level) to optimize at a certain level, or None to run
7880
/// absolutely no optimizations (used for the metadata module).
7981
pub opt_level: Option<config::OptLevel>,
@@ -170,6 +172,8 @@ impl ModuleConfig {
170172
ModuleConfig {
171173
passes: if_regular!(sess.opts.cg.passes.clone(), vec![]),
172174

175+
pass_plugins: if_regular!(sess.opts.cg.pass_plugins.clone(), vec![]),
176+
173177
opt_level: opt_level_and_size,
174178
opt_size: opt_level_and_size,
175179

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,7 @@ fn test_codegen_options_tracking_hash() {
587587
tracked!(overflow_checks, Some(true));
588588
tracked!(panic, Some(PanicStrategy::Abort));
589589
tracked!(passes, vec![String::from("1"), String::from("2")]);
590+
tracked!(pass_plugins, vec![String::from("1"), String::from("2")]);
590591
tracked!(prefer_dynamic, true);
591592
tracked!(profile_generate, SwitchWithOptPath::Enabled(None));
592593
tracked!(profile_use, Some(PathBuf::from("abc")));

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 *PassPlugins, size_t PassPluginsLen) {
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 (PassPluginsLen) {
930+
auto PluginsStr = StringRef(PassPlugins, PassPluginsLen);
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

compiler/rustc_session/src/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,8 @@ options! {
10331033
"panic strategy to compile crate with"),
10341034
passes: Vec<String> = (Vec::new(), parse_list, [TRACKED],
10351035
"a list of extra LLVM passes to run (space separated)"),
1036+
pass_plugins: Vec<String> = (Vec::new(), parse_list, [TRACKED],
1037+
"a list of LLVM pass plugins to load (space separated)"),
10361038
prefer_dynamic: bool = (false, parse_bool, [TRACKED],
10371039
"prefer dynamic linking to static linking (default: no)"),
10381040
profile_generate: SwitchWithOptPath = (SwitchWithOptPath::Disabled,

0 commit comments

Comments
 (0)