1
Fork 0

Add a codegen option to allow loading LLVM pass plugins

This commit is contained in:
Axel Cohen 2021-11-19 17:01:41 +01:00
parent 4a7fb971c9
commit 97cf461b8f
6 changed files with 30 additions and 1 deletions

View file

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

View file

@ -2304,6 +2304,8 @@ extern "C" {
end_callback: SelfProfileAfterPassCallback, end_callback: SelfProfileAfterPassCallback,
ExtraPasses: *const c_char, ExtraPasses: *const c_char,
ExtraPassesLen: size_t, ExtraPassesLen: size_t,
PassPlugins: *const c_char,
PassPluginsLen: size_t,
) -> LLVMRustResult; ) -> LLVMRustResult;
pub fn LLVMRustPrintModule( pub fn LLVMRustPrintModule(
M: &'a Module, M: &'a Module,

View file

@ -74,6 +74,8 @@ pub enum BitcodeSection {
pub struct ModuleConfig { pub struct ModuleConfig {
/// Names of additional optimization passes to run. /// Names of additional optimization passes to run.
pub passes: Vec<String>, pub passes: Vec<String>,
/// Paths of LLVM pass plugins to load.
pub pass_plugins: Vec<String>,
/// Some(level) to optimize at a certain level, or None to run /// Some(level) to optimize at a certain level, or None to run
/// absolutely no optimizations (used for the metadata module). /// absolutely no optimizations (used for the metadata module).
pub opt_level: Option<config::OptLevel>, pub opt_level: Option<config::OptLevel>,
@ -170,6 +172,8 @@ impl ModuleConfig {
ModuleConfig { ModuleConfig {
passes: if_regular!(sess.opts.cg.passes.clone(), vec![]), passes: if_regular!(sess.opts.cg.passes.clone(), vec![]),
pass_plugins: if_regular!(sess.opts.cg.pass_plugins.clone(), vec![]),
opt_level: opt_level_and_size, opt_level: opt_level_and_size,
opt_size: opt_level_and_size, opt_size: opt_level_and_size,

View file

@ -587,6 +587,7 @@ fn test_codegen_options_tracking_hash() {
tracked!(overflow_checks, Some(true)); tracked!(overflow_checks, Some(true));
tracked!(panic, Some(PanicStrategy::Abort)); tracked!(panic, Some(PanicStrategy::Abort));
tracked!(passes, vec![String::from("1"), String::from("2")]); tracked!(passes, vec![String::from("1"), String::from("2")]);
tracked!(pass_plugins, vec![String::from("1"), String::from("2")]);
tracked!(prefer_dynamic, true); tracked!(prefer_dynamic, true);
tracked!(profile_generate, SwitchWithOptPath::Enabled(None)); tracked!(profile_generate, SwitchWithOptPath::Enabled(None));
tracked!(profile_use, Some(PathBuf::from("abc"))); tracked!(profile_use, Some(PathBuf::from("abc")));

View file

@ -17,6 +17,7 @@
#include "llvm/Object/ObjectFile.h" #include "llvm/Object/ObjectFile.h"
#include "llvm/Object/IRObjectFile.h" #include "llvm/Object/IRObjectFile.h"
#include "llvm/Passes/PassBuilder.h" #include "llvm/Passes/PassBuilder.h"
#include "llvm/Passes/PassPlugin.h"
#include "llvm/Passes/StandardInstrumentations.h" #include "llvm/Passes/StandardInstrumentations.h"
#include "llvm/Support/CBindingWrapping.h" #include "llvm/Support/CBindingWrapping.h"
#include "llvm/Support/FileSystem.h" #include "llvm/Support/FileSystem.h"
@ -753,7 +754,8 @@ LLVMRustOptimizeWithNewPassManager(
void* LlvmSelfProfiler, void* LlvmSelfProfiler,
LLVMRustSelfProfileBeforePassCallback BeforePassCallback, LLVMRustSelfProfileBeforePassCallback BeforePassCallback,
LLVMRustSelfProfileAfterPassCallback AfterPassCallback, LLVMRustSelfProfileAfterPassCallback AfterPassCallback,
const char *ExtraPasses, size_t ExtraPassesLen) { const char *ExtraPasses, size_t ExtraPassesLen,
const char *PassPlugins, size_t PassPluginsLen) {
Module *TheModule = unwrap(ModuleRef); Module *TheModule = unwrap(ModuleRef);
TargetMachine *TM = unwrap(TMRef); TargetMachine *TM = unwrap(TMRef);
OptimizationLevel OptLevel = fromRust(OptLevelRust); OptimizationLevel OptLevel = fromRust(OptLevelRust);
@ -924,6 +926,20 @@ LLVMRustOptimizeWithNewPassManager(
} }
} }
if (PassPluginsLen) {
auto PluginsStr = StringRef(PassPlugins, PassPluginsLen);
SmallVector<StringRef> Plugins;
PluginsStr.split(Plugins, ' ', -1, false);
for (auto PluginPath: Plugins) {
auto Plugin = PassPlugin::Load(PluginPath.str());
if (!Plugin) {
LLVMRustSetLastError(("Failed to load pass plugin" + PluginPath.str()).c_str());
continue;
}
Plugin->registerPassBuilderCallbacks(PB);
}
}
#if LLVM_VERSION_GE(13, 0) #if LLVM_VERSION_GE(13, 0)
ModulePassManager MPM; ModulePassManager MPM;
#else #else

View file

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