Skip to content

Commit

Permalink
Auto merge of rust-lang#116486 - van-ema:master, r=nikic
Browse files Browse the repository at this point in the history
Fix to register analysis passes with -Zllvm-plugins at link-time

This PR fixes an unexpected behavior of the `-Zllvm-plugins` flag. It allows to run an out-of-tree pass as part of LTO.
However, analysis passes are registered before the plugin is loaded. As a result an analysis pass, which is passed as a plugin, is not registered. This causes the LLVM PassManager to fail when the analysis pass is queried from a transformation pass  [(here)](https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/IR/PassManager.h#L776).

This fix mimics the bahavior in [LLVM LTOBackend.cpp](https://github.com/llvm/llvm-project/blob/main/llvm/lib/LTO/LTOBackend.cpp#L273) by loading the plugin before the analysis passes are registered.

Tested with rustc 1.60 and 1.65 and LLVM-13.0.1.
  • Loading branch information
bors committed Oct 8, 2023
2 parents fdf32ee + 5048f81 commit 1516ca1
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,20 @@ LLVMRustOptimize(
CGSCCAnalysisManager CGAM;
ModuleAnalysisManager MAM;

if (LLVMPluginsLen) {
auto PluginsStr = StringRef(LLVMPlugins, LLVMPluginsLen);
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());
return LLVMRustResult::Failure;
}
Plugin->registerPassBuilderCallbacks(PB);
}
}

FAM.registerPass([&] { return PB.buildDefaultAAPipeline(); });

Triple TargetTriple(TheModule->getTargetTriple());
Expand Down Expand Up @@ -918,20 +932,6 @@ LLVMRustOptimize(
}
}

if (LLVMPluginsLen) {
auto PluginsStr = StringRef(LLVMPlugins, LLVMPluginsLen);
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());
return LLVMRustResult::Failure;
}
Plugin->registerPassBuilderCallbacks(PB);
}
}

ModulePassManager MPM;
bool NeedThinLTOBufferPasses = UseThinLTOBuffers;
if (!NoPrepopulatePasses) {
Expand Down

0 comments on commit 1516ca1

Please sign in to comment.