Skip to content

Commit ce748da

Browse files
committed
[LLVM][NewPM] Add a C API for setting the PassBuilder AA pipeline.
1 parent f0f5afe commit ce748da

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

llvm/include/llvm-c/Transforms/PassBuilder.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ void LLVMPassBuilderOptionsSetVerifyEach(LLVMPassBuilderOptionsRef Options,
7272
void LLVMPassBuilderOptionsSetDebugLogging(LLVMPassBuilderOptionsRef Options,
7373
LLVMBool DebugLogging);
7474

75+
/**
76+
* Specify a custom alias analysis pipeline for the PassBuilder to be used
77+
* instead of the default one. The string argument is not copied; the caller
78+
* is responsible for ensuring it outlives the PassBuilderOptions instance.
79+
*/
80+
void LLVMPassBuilderOptionsSetAAPipeline(LLVMPassBuilderOptionsRef Options,
81+
const char *AAPipeline);
82+
7583
void LLVMPassBuilderOptionsSetLoopInterleaving(
7684
LLVMPassBuilderOptionsRef Options, LLVMBool LoopInterleaving);
7785

llvm/lib/Passes/PassBuilderBindings.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "llvm-c/Transforms/PassBuilder.h"
15+
#include "llvm/Analysis/AliasAnalysis.h"
1516
#include "llvm/IR/Module.h"
1617
#include "llvm/IR/Verifier.h"
1718
#include "llvm/Passes/PassBuilder.h"
@@ -28,11 +29,14 @@ class LLVMPassBuilderOptions {
2829
public:
2930
explicit LLVMPassBuilderOptions(
3031
bool DebugLogging = false, bool VerifyEach = false,
32+
const char *AAPipeline = nullptr,
3133
PipelineTuningOptions PTO = PipelineTuningOptions())
32-
: DebugLogging(DebugLogging), VerifyEach(VerifyEach), PTO(PTO) {}
34+
: DebugLogging(DebugLogging), VerifyEach(VerifyEach),
35+
AAPipeline(AAPipeline), PTO(PTO) {}
3336

3437
bool DebugLogging;
3538
bool VerifyEach;
39+
const char *AAPipeline;
3640
PipelineTuningOptions PTO;
3741
};
3842
} // namespace llvm
@@ -60,6 +64,14 @@ LLVMErrorRef LLVMRunPasses(LLVMModuleRef M, const char *Passes,
6064
FunctionAnalysisManager FAM;
6165
CGSCCAnalysisManager CGAM;
6266
ModuleAnalysisManager MAM;
67+
if (PassOpts->AAPipeline) {
68+
// If we have a custom AA pipeline, we need to register it _before_ calling
69+
// registerFunctionAnalyses, or the default alias analysis pipeline is used.
70+
AAManager AA;
71+
if (auto Err = PB.parseAAPipeline(AA, PassOpts->AAPipeline))
72+
return wrap(std::move(Err));
73+
FAM.registerPass([&] { return std::move(AA); });
74+
}
6375
PB.registerLoopAnalyses(LAM);
6476
PB.registerFunctionAnalyses(FAM);
6577
PB.registerCGSCCAnalyses(CGAM);
@@ -94,6 +106,11 @@ void LLVMPassBuilderOptionsSetDebugLogging(LLVMPassBuilderOptionsRef Options,
94106
unwrap(Options)->DebugLogging = DebugLogging;
95107
}
96108

109+
void LLVMPassBuilderOptionsSetAAPipeline(LLVMPassBuilderOptionsRef Options,
110+
const char *AAPipeline) {
111+
unwrap(Options)->AAPipeline = AAPipeline;
112+
}
113+
97114
void LLVMPassBuilderOptionsSetLoopInterleaving(
98115
LLVMPassBuilderOptionsRef Options, LLVMBool LoopInterleaving) {
99116
unwrap(Options)->PTO.LoopInterleaving = LoopInterleaving;

llvm/unittests/Passes/PassBuilderBindings/PassBuilderBindingsTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ TEST_F(PassBuilderCTest, Basic) {
6060
LLVMPassBuilderOptionsSetLoopUnrolling(Options, 1);
6161
LLVMPassBuilderOptionsSetVerifyEach(Options, 1);
6262
LLVMPassBuilderOptionsSetDebugLogging(Options, 0);
63+
LLVMPassBuilderOptionsSetAAPipeline(Options, "basic-aa");
6364
if (LLVMErrorRef E = LLVMRunPasses(Module, "default<O2>", TM, Options)) {
6465
char *Msg = LLVMGetErrorMessage(E);
6566
LLVMConsumeError(E);

0 commit comments

Comments
 (0)