[llvm] [NFCi][AsmPrinter] Refactor getting analyses to callbacks (PR #182793)
Aiden Grossman via llvm-commits
llvm-commits at lists.llvm.org
Sun Feb 22 20:11:33 PST 2026
https://github.com/boomanaiden154 created https://github.com/llvm/llvm-project/pull/182793
As part of making AsmPrinter work with the new pass manager, we need to
be able to override how we get analyses. This patch does that by
refactoring getting all analyses/other related functionality to
callbacks that are set by default by can be overriden later (like by a
NewPM wrapper pass).
>From f759d60287b00362402dfc025fe85dbaed06944b Mon Sep 17 00:00:00 2001
From: Aiden Grossman <aidengrossman at google.com>
Date: Mon, 23 Feb 2026 04:11:21 +0000
Subject: [PATCH] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20initia?=
=?UTF-8?q?l=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Created using spr 1.3.7
---
llvm/include/llvm/CodeGen/AsmPrinter.h | 15 +++-
llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp | 96 +++++++++++++---------
2 files changed, 69 insertions(+), 42 deletions(-)
diff --git a/llvm/include/llvm/CodeGen/AsmPrinter.h b/llvm/include/llvm/CodeGen/AsmPrinter.h
index fa08bfa476fae..88dc7ea1dfdea 100644
--- a/llvm/include/llvm/CodeGen/AsmPrinter.h
+++ b/llvm/include/llvm/CodeGen/AsmPrinter.h
@@ -169,6 +169,18 @@ class LLVM_ABI AsmPrinter : public MachineFunctionPass {
Debug = 2 ///< Emit .debug_frame
};
+ // Callbacks to get analyses to allow portability between the new and
+ // legacy pass managers.
+ // TODO(boomanaiden154): Remove these and use a more native solution once
+ // we drop support for the legacy PM.
+ std::function<MachineModuleInfo *()> GetMMI;
+ std::function<MachineOptimizationRemarkEmitter *(MachineFunction &)> GetORE;
+ std::function<MachineDominatorTree *(MachineFunction &)> GetMDT;
+ std::function<MachineLoopInfo *(MachineFunction &)> GetMLI;
+ std::function<void(Module &)> BeginGCAssembly;
+ std::function<void(Module &)> FinishGCAssembly;
+ std::function<void(Module &)> EmitStackMaps;
+
private:
MCSymbol *CurrentFnEnd = nullptr;
@@ -582,9 +594,6 @@ class LLVM_ABI AsmPrinter : public MachineFunctionPass {
/// emit the proxies we previously omitted in EmitGlobalVariable.
void emitGlobalGOTEquivs();
- /// Emit the stack maps.
- void emitStackMaps();
-
//===------------------------------------------------------------------===//
// Overridable Hooks
//===------------------------------------------------------------------===//
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 5bd848603ef5e..05f3c4f7d79a1 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -413,6 +413,56 @@ AsmPrinter::AsmPrinter(TargetMachine &tm, std::unique_ptr<MCStreamer> Streamer,
VerboseAsm = OutStreamer->isVerboseAsm();
DwarfUsesRelocationsAcrossSections =
MAI->doesDwarfUseRelocationsAcrossSections();
+ GetMMI = [this]() {
+ auto *MMIWP = getAnalysisIfAvailable<MachineModuleInfoWrapperPass>();
+ return MMIWP ? &MMIWP->getMMI() : nullptr;
+ };
+ GetORE = [this](MachineFunction &MF) {
+ return &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
+ };
+ GetMDT = [this](MachineFunction &MF) {
+ auto *MDTWrapper =
+ getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>();
+ return MDTWrapper ? &MDTWrapper->getDomTree() : nullptr;
+ };
+ GetMLI = [this](MachineFunction &MF) {
+ auto *MLIWrapper = getAnalysisIfAvailable<MachineLoopInfoWrapperPass>();
+ return MLIWrapper ? &MLIWrapper->getLI() : nullptr;
+ };
+ BeginGCAssembly = [this](Module &M) {
+ GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
+ assert(MI && "AsmPrinter didn't require GCModuleInfo?");
+ for (const auto &I : *MI)
+ if (GCMetadataPrinter *MP = getOrCreateGCPrinter(*I))
+ MP->beginAssembly(M, *MI, *this);
+ };
+ FinishGCAssembly = [this](Module &M) {
+ GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
+ assert(MI && "AsmPrinter didn't require GCModuleInfo?");
+ for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E;)
+ if (GCMetadataPrinter *MP = getOrCreateGCPrinter(**--I))
+ MP->finishAssembly(M, *MI, *this);
+ };
+ EmitStackMaps = [this](Module &M) {
+ GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
+ assert(MI && "AsmPrinter didn't require GCModuleInfo?");
+ bool NeedsDefault = false;
+ if (MI->begin() == MI->end())
+ // No GC strategy, use the default format.
+ NeedsDefault = true;
+ else
+ for (const auto &I : *MI) {
+ if (GCMetadataPrinter *MP = getOrCreateGCPrinter(*I))
+ if (MP->emitStackMaps(SM, *this))
+ continue;
+ // The strategy doesn't have printer or doesn't emit custom stack maps.
+ // Use the default format.
+ NeedsDefault = true;
+ }
+
+ if (NeedsDefault)
+ SM.serializeToStackMapSection();
+ };
}
AsmPrinter::~AsmPrinter() {
@@ -487,8 +537,7 @@ void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
}
bool AsmPrinter::doInitialization(Module &M) {
- auto *MMIWP = getAnalysisIfAvailable<MachineModuleInfoWrapperPass>();
- MMI = MMIWP ? &MMIWP->getMMI() : nullptr;
+ MMI = GetMMI();
HasSplitStack = false;
HasNoSplitStack = false;
DbgInfoAvailable = !M.debug_compile_units().empty();
@@ -568,11 +617,7 @@ bool AsmPrinter::doInitialization(Module &M) {
OutStreamer->emitXCOFFRenameDirective(XSym, XSym->getSymbolTableName());
}
- GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
- assert(MI && "AsmPrinter didn't require GCModuleInfo?");
- for (const auto &I : *MI)
- if (GCMetadataPrinter *MP = getOrCreateGCPrinter(*I))
- MP->beginAssembly(M, *MI, *this);
+ BeginGCAssembly(M);
// Emit module-level inline asm if it exists.
if (!M.getModuleInlineAsm().empty()) {
@@ -1979,8 +2024,7 @@ void AsmPrinter::emitFunctionBody() {
if (isVerbose()) {
// Get MachineDominatorTree or compute it on the fly if it's unavailable
- auto MDTWrapper = getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>();
- MDT = MDTWrapper ? &MDTWrapper->getDomTree() : nullptr;
+ MDT = GetMDT(*MF);
if (!MDT) {
OwnedMDT = std::make_unique<MachineDominatorTree>();
OwnedMDT->recalculate(*MF);
@@ -1988,8 +2032,7 @@ void AsmPrinter::emitFunctionBody() {
}
// Get MachineLoopInfo or compute it on the fly if it's unavailable
- auto *MLIWrapper = getAnalysisIfAvailable<MachineLoopInfoWrapperPass>();
- MLI = MLIWrapper ? &MLIWrapper->getLI() : nullptr;
+ MLI = GetMLI(*MF);
if (!MLI) {
OwnedMLI = std::make_unique<MachineLoopInfo>();
OwnedMLI->analyze(*MDT);
@@ -2876,7 +2919,7 @@ bool AsmPrinter::doFinalization(Module &M) {
// text sections come after debug info has been emitted. This matters for
// stack maps as they are arbitrary data, and may even have a custom format
// through user plugins.
- emitStackMaps();
+ EmitStackMaps(M);
// Print aliases in topological order, that is, for each alias a = b,
// b must be printed before a.
@@ -2952,11 +2995,7 @@ bool AsmPrinter::doFinalization(Module &M) {
}
}
- GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
- assert(MI && "AsmPrinter didn't require GCModuleInfo?");
- for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
- if (GCMetadataPrinter *MP = getOrCreateGCPrinter(**--I))
- MP->finishAssembly(M, *MI, *this);
+ FinishGCAssembly(M);
// Emit llvm.ident metadata in an '.ident' directive.
emitModuleIdents(M);
@@ -3096,7 +3135,7 @@ void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
CurrentFnSymForSize = CurrentFnBegin;
}
- ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
+ ORE = GetORE(MF);
}
namespace {
@@ -4787,27 +4826,6 @@ GCMetadataPrinter *AsmPrinter::getOrCreateGCPrinter(GCStrategy &S) {
report_fatal_error("no GCMetadataPrinter registered for GC: " + Twine(Name));
}
-void AsmPrinter::emitStackMaps() {
- GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
- assert(MI && "AsmPrinter didn't require GCModuleInfo?");
- bool NeedsDefault = false;
- if (MI->begin() == MI->end())
- // No GC strategy, use the default format.
- NeedsDefault = true;
- else
- for (const auto &I : *MI) {
- if (GCMetadataPrinter *MP = getOrCreateGCPrinter(*I))
- if (MP->emitStackMaps(SM, *this))
- continue;
- // The strategy doesn't have printer or doesn't emit custom stack maps.
- // Use the default format.
- NeedsDefault = true;
- }
-
- if (NeedsDefault)
- SM.serializeToStackMapSection();
-}
-
void AsmPrinter::addAsmPrinterHandler(
std::unique_ptr<AsmPrinterHandler> Handler) {
Handlers.insert(Handlers.begin(), std::move(Handler));
More information about the llvm-commits
mailing list