[llvm] [LLVM][NewPM] Add a C API for setting the PassBuilder AA pipeline. (PR #102482)

Tim Besard via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 8 07:44:58 PDT 2024


https://github.com/maleadt created https://github.com/llvm/llvm-project/pull/102482

This PR adds a field to the pass builder options struct, `AAPipeline`, exposed through a C API `LLVMPassBuilderOptionsSetAAPipeline`, that is used to set an alias analysis pipeline to be used in stead of the default one.

cc @nikic
x-ref https://discourse.llvm.org/t/newpm-c-api-questions/80598

>From f92c5907ec805479356503088bd9bc353ccf63b2 Mon Sep 17 00:00:00 2001
From: Tim Besard <tim.besard at gmail.com>
Date: Thu, 8 Aug 2024 16:42:46 +0200
Subject: [PATCH] [LLVM][NewPM] Add a C API for setting the PassBuilder AA
 pipeline.

---
 llvm/include/llvm-c/Transforms/PassBuilder.h  |  7 +++++++
 llvm/lib/Passes/PassBuilderBindings.cpp       | 20 ++++++++++++++++++-
 .../PassBuilderBindingsTest.cpp               |  1 +
 3 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/llvm/include/llvm-c/Transforms/PassBuilder.h b/llvm/include/llvm-c/Transforms/PassBuilder.h
index d0466dd7fc0a12..3ec444d9b7f457 100644
--- a/llvm/include/llvm-c/Transforms/PassBuilder.h
+++ b/llvm/include/llvm-c/Transforms/PassBuilder.h
@@ -72,6 +72,13 @@ void LLVMPassBuilderOptionsSetVerifyEach(LLVMPassBuilderOptionsRef Options,
 void LLVMPassBuilderOptionsSetDebugLogging(LLVMPassBuilderOptionsRef Options,
                                            LLVMBool DebugLogging);
 
+/**
+ * Specify a custom alias analysis pipeline for the PassBuilder
+ * to be used instead of the default one.
+ */
+void LLVMPassBuilderOptionsSetAAPipeline(LLVMPassBuilderOptionsRef Options,
+                                         const char *AAPipeline);
+
 void LLVMPassBuilderOptionsSetLoopInterleaving(
     LLVMPassBuilderOptionsRef Options, LLVMBool LoopInterleaving);
 
diff --git a/llvm/lib/Passes/PassBuilderBindings.cpp b/llvm/lib/Passes/PassBuilderBindings.cpp
index b80dc0231ed5fd..289be0a1932bfb 100644
--- a/llvm/lib/Passes/PassBuilderBindings.cpp
+++ b/llvm/lib/Passes/PassBuilderBindings.cpp
@@ -12,6 +12,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm-c/Transforms/PassBuilder.h"
+#include "llvm/Analysis/AliasAnalysis.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/Verifier.h"
 #include "llvm/Passes/PassBuilder.h"
@@ -28,11 +29,14 @@ class LLVMPassBuilderOptions {
 public:
   explicit LLVMPassBuilderOptions(
       bool DebugLogging = false, bool VerifyEach = false,
+      const char *AAPipeline = nullptr,
       PipelineTuningOptions PTO = PipelineTuningOptions())
-      : DebugLogging(DebugLogging), VerifyEach(VerifyEach), PTO(PTO) {}
+      : DebugLogging(DebugLogging), VerifyEach(VerifyEach),
+        AAPipeline(AAPipeline), PTO(PTO) {}
 
   bool DebugLogging;
   bool VerifyEach;
+  const char *AAPipeline;
   PipelineTuningOptions PTO;
 };
 } // namespace llvm
@@ -61,6 +65,15 @@ LLVMErrorRef LLVMRunPasses(LLVMModuleRef M, const char *Passes,
   CGSCCAnalysisManager CGAM;
   ModuleAnalysisManager MAM;
   PB.registerLoopAnalyses(LAM);
+  if (PassOpts->AAPipeline) {
+    // If we have a custom AA pipeline, we need to register it _before_ calling
+    // registerFunctionAnalyses, or the default alias analysis pipeline is used.
+    AAManager AA;
+    if (auto Err = PB.parseAAPipeline(AA, PassOpts->AAPipeline)) {
+      return wrap(std::move(Err));
+    }
+    FAM.registerPass([&] { return std::move(AA); });
+  }
   PB.registerFunctionAnalyses(FAM);
   PB.registerCGSCCAnalyses(CGAM);
   PB.registerModuleAnalyses(MAM);
@@ -94,6 +107,11 @@ void LLVMPassBuilderOptionsSetDebugLogging(LLVMPassBuilderOptionsRef Options,
   unwrap(Options)->DebugLogging = DebugLogging;
 }
 
+void LLVMPassBuilderOptionsSetAAPipeline(LLVMPassBuilderOptionsRef Options,
+                                         const char *AAPipeline) {
+  unwrap(Options)->AAPipeline = AAPipeline;
+}
+
 void LLVMPassBuilderOptionsSetLoopInterleaving(
     LLVMPassBuilderOptionsRef Options, LLVMBool LoopInterleaving) {
   unwrap(Options)->PTO.LoopInterleaving = LoopInterleaving;
diff --git a/llvm/unittests/Passes/PassBuilderBindings/PassBuilderBindingsTest.cpp b/llvm/unittests/Passes/PassBuilderBindings/PassBuilderBindingsTest.cpp
index ffa3fdaf6e7e6f..2b06033f0c3fa2 100644
--- a/llvm/unittests/Passes/PassBuilderBindings/PassBuilderBindingsTest.cpp
+++ b/llvm/unittests/Passes/PassBuilderBindings/PassBuilderBindingsTest.cpp
@@ -60,6 +60,7 @@ TEST_F(PassBuilderCTest, Basic) {
   LLVMPassBuilderOptionsSetLoopUnrolling(Options, 1);
   LLVMPassBuilderOptionsSetVerifyEach(Options, 1);
   LLVMPassBuilderOptionsSetDebugLogging(Options, 0);
+  LLVMPassBuilderOptionsSetAAPipeline(Options, "basic-aa");
   if (LLVMErrorRef E = LLVMRunPasses(Module, "default<O2>", TM, Options)) {
     char *Msg = LLVMGetErrorMessage(E);
     LLVMConsumeError(E);



More information about the llvm-commits mailing list