Skip to content

Commit 1516ca1

Browse files
committed
Auto merge of rust-lang#116486 - van-ema:master, r=nikic
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.
2 parents fdf32ee + 5048f81 commit 1516ca1

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

+14-14
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,20 @@ LLVMRustOptimize(
795795
CGSCCAnalysisManager CGAM;
796796
ModuleAnalysisManager MAM;
797797

798+
if (LLVMPluginsLen) {
799+
auto PluginsStr = StringRef(LLVMPlugins, LLVMPluginsLen);
800+
SmallVector<StringRef> Plugins;
801+
PluginsStr.split(Plugins, ',', -1, false);
802+
for (auto PluginPath: Plugins) {
803+
auto Plugin = PassPlugin::Load(PluginPath.str());
804+
if (!Plugin) {
805+
LLVMRustSetLastError(("Failed to load pass plugin" + PluginPath.str()).c_str());
806+
return LLVMRustResult::Failure;
807+
}
808+
Plugin->registerPassBuilderCallbacks(PB);
809+
}
810+
}
811+
798812
FAM.registerPass([&] { return PB.buildDefaultAAPipeline(); });
799813

800814
Triple TargetTriple(TheModule->getTargetTriple());
@@ -918,20 +932,6 @@ LLVMRustOptimize(
918932
}
919933
}
920934

921-
if (LLVMPluginsLen) {
922-
auto PluginsStr = StringRef(LLVMPlugins, LLVMPluginsLen);
923-
SmallVector<StringRef> Plugins;
924-
PluginsStr.split(Plugins, ',', -1, false);
925-
for (auto PluginPath: Plugins) {
926-
auto Plugin = PassPlugin::Load(PluginPath.str());
927-
if (!Plugin) {
928-
LLVMRustSetLastError(("Failed to load pass plugin" + PluginPath.str()).c_str());
929-
return LLVMRustResult::Failure;
930-
}
931-
Plugin->registerPassBuilderCallbacks(PB);
932-
}
933-
}
934-
935935
ModulePassManager MPM;
936936
bool NeedThinLTOBufferPasses = UseThinLTOBuffers;
937937
if (!NoPrepopulatePasses) {

0 commit comments

Comments
 (0)