[llvm] [CodeGen][NewPM] Port `AsmPrinter` to new pass manager (PR #99320)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Feb 15 00:29:35 PST 2025
https://github.com/paperchalice updated https://github.com/llvm/llvm-project/pull/99320
>From dfccdac1765a02f2a2010aeccedad2aab44a9701 Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Sun, 14 Jul 2024 21:12:07 +0800
Subject: [PATCH 1/2] [GC] Support bidirectional iterator in GCStrategyMap
---
llvm/include/llvm/CodeGen/GCMetadata.h | 33 ++++++++++++++++++-
llvm/include/llvm/CodeGen/GCMetadataPrinter.h | 5 +++
llvm/lib/CodeGen/GCMetadata.cpp | 22 +++++++++----
llvm/lib/CodeGen/ShadowStackGCLowering.cpp | 2 +-
4 files changed, 53 insertions(+), 9 deletions(-)
diff --git a/llvm/include/llvm/CodeGen/GCMetadata.h b/llvm/include/llvm/CodeGen/GCMetadata.h
index ca6a511185c7c..f22aa2c54f401 100644
--- a/llvm/include/llvm/CodeGen/GCMetadata.h
+++ b/llvm/include/llvm/CodeGen/GCMetadata.h
@@ -151,15 +151,46 @@ class GCFunctionInfo {
size_t live_size(const iterator &p) const { return roots_size(); }
};
-struct GCStrategyMap {
+class GCStrategyMap {
StringMap<std::unique_ptr<GCStrategy>> StrategyMap;
+ SmallVector<GCStrategy *, 1> StrategyList; // For bidirectional iterator.
+ using StrategyListT = SmallVector<GCStrategy *, 1>;
+ FunctionAnalysisManager *FAM = nullptr;
+
+public:
GCStrategyMap() = default;
GCStrategyMap(GCStrategyMap &&) = default;
+ GCStrategyMap(FunctionAnalysisManager &FAM) : FAM(&FAM) {}
/// Handle invalidation explicitly.
bool invalidate(Module &M, const PreservedAnalyses &PA,
ModuleAnalysisManager::Invalidator &Inv);
+
+ GCFunctionInfo &getFunctionInfo(Function &F);
+
+ bool empty() const { return StrategyMap.empty(); }
+
+ bool contains(StringRef Name) const { return StrategyMap.contains(Name); }
+
+ /// Insert a new strategy if it is not existed, otherwise do nothing.
+ void insert(StringRef Name, std::unique_ptr<GCStrategy> Strategy) {
+ auto &S = StrategyMap[Name];
+ if (!S) {
+ S = std::move(Strategy);
+ StrategyList.push_back(S.get());
+ }
+ }
+
+ GCStrategy &at(StringRef Name) { return *StrategyMap.at(Name); }
+
+ // This class must support bidirectional iterator which is used by AsmPrinter.
+ using iterator = StrategyListT::iterator;
+ iterator begin() { return StrategyList.begin(); }
+ iterator end() { return StrategyList.end(); }
+ using reverse_iterator = StrategyListT::reverse_iterator;
+ reverse_iterator rbegin() { return StrategyList.rbegin(); }
+ reverse_iterator rend() { return StrategyList.rend(); }
};
/// An analysis pass which caches information about the entire Module.
diff --git a/llvm/include/llvm/CodeGen/GCMetadataPrinter.h b/llvm/include/llvm/CodeGen/GCMetadataPrinter.h
index 9d421be8313f0..edaa32f3576d8 100644
--- a/llvm/include/llvm/CodeGen/GCMetadataPrinter.h
+++ b/llvm/include/llvm/CodeGen/GCMetadataPrinter.h
@@ -27,8 +27,11 @@ class AsmPrinter;
class GCMetadataPrinter;
class GCModuleInfo;
class GCStrategy;
+class GCStrategyMap;
class Module;
class StackMaps;
+template <typename IRUnitT, typename... ExtraArgTs> class AnalysisManager;
+using ModuleAnalysisManager = AnalysisManager<Module>;
/// GCMetadataPrinterRegistry - The GC assembly printer registry uses all the
/// defaults from Registry.
@@ -58,10 +61,12 @@ class GCMetadataPrinter {
/// Called before the assembly for the module is generated by
/// the AsmPrinter (but after target specific hooks.)
virtual void beginAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) {}
+ virtual void beginAssembly(Module &M, GCStrategyMap &Map, AsmPrinter &AP) {}
/// Called after the assembly for the module is generated by
/// the AsmPrinter (but before target specific hooks)
virtual void finishAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) {}
+ virtual void finishAssembly(Module &M, GCStrategyMap &Map, AsmPrinter &AP) {}
/// Called when the stack maps are generated. Return true if
/// stack maps with a custom format are generated. Otherwise
diff --git a/llvm/lib/CodeGen/GCMetadata.cpp b/llvm/lib/CodeGen/GCMetadata.cpp
index f33008c9e0f23..2c09312bace9e 100644
--- a/llvm/lib/CodeGen/GCMetadata.cpp
+++ b/llvm/lib/CodeGen/GCMetadata.cpp
@@ -23,6 +23,10 @@ using namespace llvm;
bool GCStrategyMap::invalidate(Module &M, const PreservedAnalyses &PA,
ModuleAnalysisManager::Invalidator &) {
+ auto PAC = PA.getChecker<CollectorMetadataAnalysis>();
+ if (PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>())
+ return false;
+
for (const auto &F : M) {
if (F.isDeclaration() || !F.hasGC())
continue;
@@ -36,17 +40,22 @@ AnalysisKey CollectorMetadataAnalysis::Key;
CollectorMetadataAnalysis::Result
CollectorMetadataAnalysis::run(Module &M, ModuleAnalysisManager &MAM) {
- Result R;
- auto &Map = R.StrategyMap;
+ auto &FAM = MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
+ Result R(FAM);
for (auto &F : M) {
if (F.isDeclaration() || !F.hasGC())
continue;
- if (auto GCName = F.getGC(); !Map.contains(GCName))
- Map[GCName] = getGCStrategy(GCName);
+ auto GCName = F.getGC();
+ R.insert(GCName, getGCStrategy(GCName));
}
return R;
}
+GCFunctionInfo &llvm::GCStrategyMap::getFunctionInfo(Function &F) {
+ assert(FAM && "Need initialize!");
+ return FAM->getResult<GCFunctionAnalysis>(F);
+}
+
AnalysisKey GCFunctionAnalysis::Key;
GCFunctionAnalysis::Result
@@ -59,9 +68,8 @@ GCFunctionAnalysis::run(Function &F, FunctionAnalysisManager &FAM) {
MAMProxy.cachedResultExists<CollectorMetadataAnalysis>(*F.getParent()) &&
"This pass need module analysis `collector-metadata`!");
auto &Map =
- MAMProxy.getCachedResult<CollectorMetadataAnalysis>(*F.getParent())
- ->StrategyMap;
- GCFunctionInfo Info(F, *Map[F.getGC()]);
+ *MAMProxy.getCachedResult<CollectorMetadataAnalysis>(*F.getParent());
+ GCFunctionInfo Info(F, Map.at(F.getGC()));
return Info;
}
diff --git a/llvm/lib/CodeGen/ShadowStackGCLowering.cpp b/llvm/lib/CodeGen/ShadowStackGCLowering.cpp
index 60c8372577a93..828b16b327c59 100644
--- a/llvm/lib/CodeGen/ShadowStackGCLowering.cpp
+++ b/llvm/lib/CodeGen/ShadowStackGCLowering.cpp
@@ -109,7 +109,7 @@ class ShadowStackGCLowering : public FunctionPass {
PreservedAnalyses ShadowStackGCLoweringPass::run(Module &M,
ModuleAnalysisManager &MAM) {
auto &Map = MAM.getResult<CollectorMetadataAnalysis>(M);
- if (Map.StrategyMap.contains("shadow-stack"))
+ if (Map.contains("shadow-stack"))
return PreservedAnalyses::all();
ShadowStackGCLoweringImpl Impl;
>From 09ba0ab40e1f98896dcb84f5b9238f0f6020f22c Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Thu, 18 Jul 2024 11:19:31 +0800
Subject: [PATCH 2/2] [CodeGen][NewPM] Port `AsmPrinter` to new pass manager
---
llvm/include/llvm/CodeGen/AsmPrinter.h | 102 ++++++++++-
llvm/include/llvm/MC/TargetRegistry.h | 32 +++-
llvm/include/llvm/Passes/CodeGenPassBuilder.h | 63 ++++---
llvm/include/llvm/Passes/PassBuilder.h | 15 ++
llvm/include/llvm/Target/TargetMachine.h | 4 +-
llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp | 165 ++++++++++++++----
llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp | 6 +-
llvm/lib/Passes/PassBuilder.cpp | 7 +
llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp | 15 +-
.../lib/Target/AMDGPU/AMDGPUTargetMachine.cpp | 19 +-
llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.h | 12 +-
llvm/lib/Target/AMDGPU/R600TargetMachine.cpp | 14 +-
llvm/lib/Target/AMDGPU/R600TargetMachine.h | 4 +-
llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp | 7 +-
llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp | 16 ++
llvm/lib/Target/X86/CMakeLists.txt | 1 +
llvm/lib/Target/X86/X86CodeGenPassBuilder.cpp | 25 ++-
llvm/lib/Target/X86/X86MCInstLower.cpp | 8 +-
llvm/lib/Target/X86/X86TargetMachine.h | 4 +-
llvm/tools/llc/NewPMDriver.cpp | 5 +-
.../unittests/CodeGen/AsmPrinterDwarfTest.cpp | 3 +-
21 files changed, 404 insertions(+), 123 deletions(-)
diff --git a/llvm/include/llvm/CodeGen/AsmPrinter.h b/llvm/include/llvm/CodeGen/AsmPrinter.h
index 3da63af5ba571..9dbffa612a355 100644
--- a/llvm/include/llvm/CodeGen/AsmPrinter.h
+++ b/llvm/include/llvm/CodeGen/AsmPrinter.h
@@ -16,11 +16,13 @@
#define LLVM_CODEGEN_ASMPRINTER_H
#include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/CodeGen/DwarfStringPoolEntry.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
+#include "llvm/CodeGen/MachinePassManager.h"
#include "llvm/CodeGen/StackMaps.h"
#include "llvm/DebugInfo/CodeView/CodeView.h"
#include "llvm/IR/InlineAsm.h"
@@ -84,7 +86,9 @@ class RemarkStreamer;
}
/// This class is intended to be used as a driving class for all asm writers.
-class AsmPrinter : public MachineFunctionPass {
+/// Use lightweight RefCountedBase here because AsmPrinter is shared only in
+/// pass manager.
+class AsmPrinter : public RefCountedBase<AsmPrinter> {
public:
/// Target machine description.
TargetMachine &TM;
@@ -180,8 +184,6 @@ class AsmPrinter : public MachineFunctionPass {
/// List of symbols to be inserted into PC sections.
DenseMap<const MDNode *, SmallVector<const MCSymbol *>> PCSectionsSymbols;
- static char ID;
-
protected:
MCSymbol *CurrentFnBegin = nullptr;
@@ -201,6 +203,10 @@ class AsmPrinter : public MachineFunctionPass {
StackMaps SM;
+ MachineFunctionPass *P = nullptr;
+ ModuleAnalysisManager *MAM = nullptr;
+ MachineFunctionAnalysisManager *MFAM = nullptr;
+
private:
/// If generated on the fly this own the instance.
std::unique_ptr<MachineDominatorTree> OwnedMDT;
@@ -235,7 +241,7 @@ class AsmPrinter : public MachineFunctionPass {
explicit AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer);
public:
- ~AsmPrinter() override;
+ virtual ~AsmPrinter();
DwarfDebug *getDwarfDebug() { return DD; }
DwarfDebug *getDwarfDebug() const { return DD; }
@@ -375,24 +381,31 @@ class AsmPrinter : public MachineFunctionPass {
// MachineFunctionPass Implementation.
//===------------------------------------------------------------------===//
+ virtual StringRef getPassName() const;
+
/// Record analysis usage.
- void getAnalysisUsage(AnalysisUsage &AU) const override;
+ virtual void getAnalysisUsage(AnalysisUsage &AU) const;
/// Set up the AsmPrinter when we are working on a new module. If your pass
/// overrides this, it must make sure to explicitly call this implementation.
- bool doInitialization(Module &M) override;
+ virtual bool doInitialization(Module &M);
/// Shut down the asmprinter. If you override this in your pass, you must make
/// sure to call it explicitly.
- bool doFinalization(Module &M) override;
+ virtual bool doFinalization(Module &M);
/// Emit the specified function out to the OutStreamer.
- bool runOnMachineFunction(MachineFunction &MF) override {
+ virtual bool runOnMachineFunction(MachineFunction &MF) {
SetupMachineFunction(MF);
emitFunctionBody();
return false;
}
+ void setPass(MachineFunctionPass *Pass) { P = Pass; }
+ void setMAM(ModuleAnalysisManager &AM) { MAM = &AM; }
+ void clearMAM() { MAM = nullptr; }
+ void setMFAM(MachineFunctionAnalysisManager &AM) { MFAM = &AM; }
+
//===------------------------------------------------------------------===//
// Coarse grained IR lowering routines.
//===------------------------------------------------------------------===//
@@ -523,6 +536,7 @@ class AsmPrinter : public MachineFunctionPass {
/// Emit the stack maps.
void emitStackMaps();
+ void emitStackMaps(Module &M); // For new pass manager version.
//===------------------------------------------------------------------===//
// Overridable Hooks
@@ -922,6 +936,78 @@ class AsmPrinter : public MachineFunctionPass {
}
};
+class AsmPrinterInitializePass
+ : public PassInfoMixin<AsmPrinterInitializePass> {
+ IntrusiveRefCntPtr<AsmPrinter> Printer;
+
+public:
+ explicit AsmPrinterInitializePass(IntrusiveRefCntPtr<AsmPrinter> AP)
+ : Printer(std::move(AP)) {}
+
+ PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
+};
+
+class AsmPrinterPass : public PassInfoMixin<AsmPrinterPass> {
+ IntrusiveRefCntPtr<AsmPrinter> Printer;
+
+public:
+ explicit AsmPrinterPass(IntrusiveRefCntPtr<AsmPrinter> AP)
+ : Printer(std::move(AP)) {}
+
+ PreservedAnalyses run(MachineFunction &MF,
+ MachineFunctionAnalysisManager &MFAM);
+};
+
+class AsmPrinterFinalizePass : public PassInfoMixin<AsmPrinterFinalizePass> {
+ IntrusiveRefCntPtr<AsmPrinter> Printer;
+
+public:
+ explicit AsmPrinterFinalizePass(IntrusiveRefCntPtr<AsmPrinter> AP)
+ : Printer(std::move(AP)) {}
+
+ PreservedAnalyses run(Module &M, ModuleAnalysisManager &);
+};
+
+class AsmPrinterLegacy : public MachineFunctionPass {
+ std::unique_ptr<AsmPrinter> Printer;
+
+public:
+ static char ID;
+
+ explicit AsmPrinterLegacy(std::unique_ptr<AsmPrinter> AP);
+
+ AsmPrinter &getPrinter() { return *Printer; }
+
+ //===------------------------------------------------------------------===//
+ // MachineFunctionPass Implementation.
+ //===------------------------------------------------------------------===//
+
+ /// Record analysis usage.
+ void getAnalysisUsage(AnalysisUsage &AU) const override {
+ MachineFunctionPass::getAnalysisUsage(AU);
+ Printer->getAnalysisUsage(AU);
+ }
+
+ /// Set up the AsmPrinter when we are working on a new module. If your pass
+ /// overrides this, it must make sure to explicitly call this implementation.
+ bool doInitialization(Module &M) override {
+ return Printer->doInitialization(M);
+ }
+
+ /// Shut down the asmprinter. If you override this in your pass, you must make
+ /// sure to call it explicitly.
+ bool doFinalization(Module &M) override { return Printer->doFinalization(M); }
+
+ /// Emit the specified function out to the OutStreamer.
+ bool runOnMachineFunction(MachineFunction &MF) override {
+ return Printer->runOnMachineFunction(MF);
+ }
+
+ StringRef getPassName() const override { return Printer->getPassName(); }
+};
+
+AsmPrinterLegacy *createAsmPrinterLegacy(std::unique_ptr<AsmPrinter> AP);
+
} // end namespace llvm
#endif // LLVM_CODEGEN_ASMPRINTER_H
diff --git a/llvm/include/llvm/MC/TargetRegistry.h b/llvm/include/llvm/MC/TargetRegistry.h
index 42d510c17bce3..21ad9fa0da144 100644
--- a/llvm/include/llvm/MC/TargetRegistry.h
+++ b/llvm/include/llvm/MC/TargetRegistry.h
@@ -37,6 +37,7 @@
namespace llvm {
class AsmPrinter;
+class AsmPrinterLegacy;
class MCAsmBackend;
class MCAsmInfo;
class MCAsmParser;
@@ -58,6 +59,8 @@ class MCTargetStreamer;
class raw_ostream;
class TargetMachine;
class TargetOptions;
+
+AsmPrinterLegacy *createAsmPrinterLegacy(std::unique_ptr<AsmPrinter> AP);
namespace mca {
class CustomBehaviour;
class InstrPostProcess;
@@ -166,8 +169,12 @@ class Target {
// If it weren't for layering issues (this header is in llvm/Support, but
// depends on MC?) this should take the Streamer by value rather than rvalue
// reference.
- using AsmPrinterCtorTy = AsmPrinter *(*)(
- TargetMachine &TM, std::unique_ptr<MCStreamer> &&Streamer);
+ using AsmPrinterLegacyCtorTy =
+ AsmPrinterLegacy *(*)(TargetMachine &TM,
+ std::unique_ptr<MCStreamer> &&Streamer);
+ using AsmPrinterCtorTy =
+ AsmPrinter *(*)(TargetMachine &TM,
+ std::unique_ptr<MCStreamer> &&Streamer);
using MCAsmBackendCtorTy = MCAsmBackend *(*)(const Target &T,
const MCSubtargetInfo &STI,
const MCRegisterInfo &MRI,
@@ -288,6 +295,7 @@ class Target {
/// AsmPrinterCtorFn - Construction function for this target's AsmPrinter,
/// if registered.
+ AsmPrinterLegacyCtorTy AsmPrinterLegacyCtorFn;
AsmPrinterCtorTy AsmPrinterCtorFn;
/// MCDisassemblerCtorFn - Construction function for this target's
@@ -487,6 +495,16 @@ class Target {
/// createAsmPrinter - Create a target specific assembly printer pass. This
/// takes ownership of the MCStreamer object.
+ AsmPrinterLegacy *
+ createAsmPrinterLegacy(TargetMachine &TM,
+ std::unique_ptr<MCStreamer> &&Streamer) const {
+ if (!AsmPrinterLegacyCtorFn)
+ return nullptr;
+ return AsmPrinterLegacyCtorFn(TM, std::move(Streamer));
+ }
+
+ /// Same as `createAsmPrinter`, but only create AsmPrinter, for new pass
+ /// manager.
AsmPrinter *createAsmPrinter(TargetMachine &TM,
std::unique_ptr<MCStreamer> &&Streamer) const {
if (!AsmPrinterCtorFn)
@@ -862,6 +880,9 @@ struct TargetRegistry {
///
/// @param T - The target being registered.
/// @param Fn - A function to construct an AsmPrinter for the target.
+ static void RegisterAsmPrinter(Target &T, Target::AsmPrinterLegacyCtorTy Fn) {
+ T.AsmPrinterLegacyCtorFn = Fn;
+ }
static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
T.AsmPrinterCtorFn = Fn;
}
@@ -1311,9 +1332,16 @@ template <class MCAsmParserImpl> struct RegisterMCAsmParser {
template <class AsmPrinterImpl> struct RegisterAsmPrinter {
RegisterAsmPrinter(Target &T) {
TargetRegistry::RegisterAsmPrinter(T, &Allocator);
+ TargetRegistry::RegisterAsmPrinter(T, &AllocatorLegacy);
}
private:
+ static AsmPrinterLegacy *
+ AllocatorLegacy(TargetMachine &TM, std::unique_ptr<MCStreamer> &&Streamer) {
+ auto *P = new AsmPrinterImpl(TM, std::move(Streamer));
+ return createAsmPrinterLegacy(std::unique_ptr<AsmPrinter>(P));
+ }
+
static AsmPrinter *Allocator(TargetMachine &TM,
std::unique_ptr<MCStreamer> &&Streamer) {
return new AsmPrinterImpl(TM, std::move(Streamer));
diff --git a/llvm/include/llvm/Passes/CodeGenPassBuilder.h b/llvm/include/llvm/Passes/CodeGenPassBuilder.h
index a84164bed46ce..2caeb2f9e5917 100644
--- a/llvm/include/llvm/Passes/CodeGenPassBuilder.h
+++ b/llvm/include/llvm/Passes/CodeGenPassBuilder.h
@@ -22,6 +22,7 @@
#include "llvm/Analysis/ScopedNoAliasAA.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
+#include "llvm/CodeGen/AsmPrinter.h"
#include "llvm/CodeGen/AssignmentTrackingAnalysis.h"
#include "llvm/CodeGen/CallBrPrepare.h"
#include "llvm/CodeGen/CodeGenPrepare.h"
@@ -76,6 +77,7 @@
#include "llvm/IRPrinter/IRPrintingPasses.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCTargetOptions.h"
+#include "llvm/Passes/PassBuilder.h"
#include "llvm/Support/CodeGen.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Error.h"
@@ -133,9 +135,8 @@ namespace llvm {
template <typename DerivedT, typename TargetMachineT> class CodeGenPassBuilder {
public:
explicit CodeGenPassBuilder(TargetMachineT &TM,
- const CGPassBuilderOption &Opts,
- PassInstrumentationCallbacks *PIC)
- : TM(TM), Opt(Opts), PIC(PIC) {
+ const CGPassBuilderOption &Opts, PassBuilder &PB)
+ : TM(TM), Opt(Opts), PB(PB), PIC(PB.getPassInstrumentationCallbacks()) {
// Target could set CGPassBuilderOption::MISchedPostRA to true to achieve
// substitutePass(&PostRASchedulerID, &PostMachineSchedulerID)
@@ -152,8 +153,8 @@ template <typename DerivedT, typename TargetMachineT> class CodeGenPassBuilder {
}
Error buildPipeline(ModulePassManager &MPM, raw_pwrite_stream &Out,
- raw_pwrite_stream *DwoOut,
- CodeGenFileType FileType) const;
+ raw_pwrite_stream *DwoOut, CodeGenFileType FileType,
+ MCContext &Ctx) const;
PassInstrumentationCallbacks *getPassInstrumentationCallbacks() const {
return PIC;
@@ -270,7 +271,9 @@ template <typename DerivedT, typename TargetMachineT> class CodeGenPassBuilder {
TargetMachineT &TM;
CGPassBuilderOption Opt;
+ PassBuilder &PB;
PassInstrumentationCallbacks *PIC;
+ mutable IntrusiveRefCntPtr<AsmPrinter> PrinterImpl;
template <typename TMC> TMC &getTM() const { return static_cast<TMC &>(TM); }
CodeGenOptLevel getOptLevel() const { return TM.getOptLevel(); }
@@ -533,7 +536,7 @@ template <typename DerivedT, typename TargetMachineT> class CodeGenPassBuilder {
template <typename Derived, typename TargetMachineT>
Error CodeGenPassBuilder<Derived, TargetMachineT>::buildPipeline(
ModulePassManager &MPM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut,
- CodeGenFileType FileType) const {
+ CodeGenFileType FileType, MCContext &Ctx) const {
auto StartStopInfo = TargetPassConfig::getStartStopInfo(*PIC);
if (!StartStopInfo)
return StartStopInfo.takeError();
@@ -542,6 +545,14 @@ Error CodeGenPassBuilder<Derived, TargetMachineT>::buildPipeline(
bool PrintAsm = TargetPassConfig::willCompleteCodeGenPipeline();
bool PrintMIR = !PrintAsm && FileType != CodeGenFileType::Null;
+ if (PrintAsm) {
+ Expected<std::unique_ptr<MCStreamer>> MCStreamerOrErr =
+ TM.createMCStreamer(Out, DwoOut, FileType, Ctx);
+ if (auto Err = MCStreamerOrErr.takeError())
+ return Err;
+ PrinterImpl = PB.getAsmPrinter(std::move(*MCStreamerOrErr));
+ }
+
{
AddIRPass addIRPass(MPM, derived());
addIRPass(RequireAnalysisPass<MachineModuleAnalysis, Module>());
@@ -550,29 +561,33 @@ Error CodeGenPassBuilder<Derived, TargetMachineT>::buildPipeline(
addISelPasses(addIRPass);
}
- AddMachinePass addPass(MPM, derived());
+ // Ensure we destruct `addPass`.
+ {
+ AddMachinePass addPass(MPM, derived());
- if (PrintMIR)
- addPass(PrintMIRPreparePass(Out), /*Force=*/true);
+ if (PrintMIR)
+ addPass(PrintMIRPreparePass(Out), /*Force=*/true);
- if (auto Err = addCoreISelPasses(addPass))
- return std::move(Err);
+ if (auto Err = addCoreISelPasses(addPass))
+ return std::move(Err);
- if (auto Err = derived().addMachinePasses(addPass))
- return std::move(Err);
+ if (auto Err = derived().addMachinePasses(addPass))
+ return std::move(Err);
- if (!Opt.DisableVerify)
- addPass(MachineVerifierPass());
+ if (!Opt.DisableVerify)
+ addPass(MachineVerifierPass());
- if (PrintAsm) {
- derived().addAsmPrinter(
- addPass, [this, &Out, DwoOut, FileType](MCContext &Ctx) {
- return this->TM.createMCStreamer(Out, DwoOut, FileType, Ctx);
- });
+ if (PrintAsm)
+ addPass(AsmPrinterPass(PrinterImpl));
+
+ if (PrintMIR)
+ addPass(PrintMIRPass(Out), /*Force=*/true);
}
- if (PrintMIR)
- addPass(PrintMIRPass(Out), /*Force=*/true);
+ {
+ AddIRPass addIRPass(MPM, derived());
+ addIRPass(AsmPrinterFinalizePass(PrinterImpl));
+ }
return verifyStartStop(*StartStopInfo);
}
@@ -680,6 +695,10 @@ void CodeGenPassBuilder<Derived, TargetMachineT>::addIRPasses(
addPass(ExpandMemCmpPass(&TM));
}
+ // This should be the last IR module pass.
+ if (TargetPassConfig::willCompleteCodeGenPipeline())
+ addPass(AsmPrinterInitializePass(PrinterImpl));
+
// Run GC lowering passes for builtin collectors
// TODO: add a pass insertion point here
addPass(GCLoweringPass());
diff --git a/llvm/include/llvm/Passes/PassBuilder.h b/llvm/include/llvm/Passes/PassBuilder.h
index 1b54855a5c6f4..10f230854545c 100644
--- a/llvm/include/llvm/Passes/PassBuilder.h
+++ b/llvm/include/llvm/Passes/PassBuilder.h
@@ -31,6 +31,7 @@
namespace llvm {
class StringRef;
+class AsmPrinter;
class AAManager;
class TargetMachine;
class ModuleSummaryIndex;
@@ -605,6 +606,12 @@ class PassBuilder {
RegClassFilterParsingCallbacks.push_back(C);
}
+ void registerAsmPrinterCreationCallback(
+ const std::function<
+ IntrusiveRefCntPtr<AsmPrinter>(std::unique_ptr<MCStreamer>)> &C) {
+ AsmPrinterCreationCallback = C;
+ }
+
/// Register a callback for a top-level pipeline entry.
///
/// If the PassManager type is not given at the top level of the pipeline
@@ -713,6 +720,11 @@ class PassBuilder {
StringRef OptionName,
StringRef PassName);
+ // Create target asm printer directly, this can bypass
+ // LLVMInitializeXXXAsmPrinter.
+ IntrusiveRefCntPtr<AsmPrinter>
+ getAsmPrinter(std::unique_ptr<MCStreamer> Streamer);
+
private:
// O1 pass pipeline
FunctionPassManager
@@ -837,6 +849,9 @@ class PassBuilder {
// Callbacks to parse `filter` parameter in register allocation passes
SmallVector<std::function<RegAllocFilterFunc(StringRef)>, 2>
RegClassFilterParsingCallbacks;
+ // Callback to create `AsmPrinterPass`.
+ std::function<IntrusiveRefCntPtr<AsmPrinter>(std::unique_ptr<MCStreamer>)>
+ AsmPrinterCreationCallback;
};
/// This utility template takes care of adding require<> and invalidate<>
diff --git a/llvm/include/llvm/Target/TargetMachine.h b/llvm/include/llvm/Target/TargetMachine.h
index fe1dbbd44f8eb..a3e9327eff50d 100644
--- a/llvm/include/llvm/Target/TargetMachine.h
+++ b/llvm/include/llvm/Target/TargetMachine.h
@@ -450,8 +450,8 @@ class TargetMachine {
virtual Error buildCodeGenPipeline(ModulePassManager &, raw_pwrite_stream &,
raw_pwrite_stream *, CodeGenFileType,
- const CGPassBuilderOption &,
- PassInstrumentationCallbacks *) {
+ const CGPassBuilderOption &, MCContext &,
+ PassBuilder &) {
return make_error<StringError>("buildCodeGenPipeline is not overridden",
inconvertibleErrorCode());
}
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 44b10c3ef9972..9ff349e769dee 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -182,7 +182,39 @@ static cl::opt<bool> PrintLatency(
STATISTIC(EmittedInsts, "Number of machine instrs printed");
-char AsmPrinter::ID = 0;
+PreservedAnalyses AsmPrinterInitializePass::run(Module &M,
+ ModuleAnalysisManager &MAM) {
+ Printer->setMAM(MAM);
+ Printer->doInitialization(M);
+ Printer->clearMAM();
+ return PreservedAnalyses::all();
+}
+
+PreservedAnalyses AsmPrinterPass::run(MachineFunction &MF,
+ MachineFunctionAnalysisManager &MFAM) {
+ Printer->setMFAM(MFAM);
+ Printer->runOnMachineFunction(MF);
+ return PreservedAnalyses::all();
+}
+
+PreservedAnalyses AsmPrinterFinalizePass::run(Module &M,
+ ModuleAnalysisManager &MAM) {
+ Printer->setMAM(MAM);
+ Printer->doFinalization(M);
+ Printer->clearMAM();
+ return PreservedAnalyses::all();
+}
+
+char AsmPrinterLegacy::ID = 0;
+
+AsmPrinterLegacy::AsmPrinterLegacy(std::unique_ptr<AsmPrinter> AP)
+ : MachineFunctionPass(ID), Printer(std::move(AP)) {
+ Printer->setPass(this);
+}
+
+AsmPrinterLegacy *llvm::createAsmPrinterLegacy(std::unique_ptr<AsmPrinter> AP) {
+ return new AsmPrinterLegacy(std::move(AP));
+}
namespace {
class AddrLabelMapCallbackPtr final : CallbackVH {
@@ -356,6 +388,8 @@ void AddrLabelMapCallbackPtr::allUsesReplacedWith(Value *V2) {
Map->UpdateForRAUWBlock(cast<BasicBlock>(getValPtr()), cast<BasicBlock>(V2));
}
+StringRef AsmPrinter::getPassName() const { return "Assembly Printer"; }
+
/// getGVAlignment - Return the alignment to use for the specified global
/// value. This rounds up to the preferred alignment if possible and legal.
Align AsmPrinter::getGVAlignment(const GlobalObject *GV, const DataLayout &DL,
@@ -383,9 +417,8 @@ Align AsmPrinter::getGVAlignment(const GlobalObject *GV, const DataLayout &DL,
}
AsmPrinter::AsmPrinter(TargetMachine &tm, std::unique_ptr<MCStreamer> Streamer)
- : MachineFunctionPass(ID), TM(tm), MAI(tm.getMCAsmInfo()),
- OutContext(Streamer->getContext()), OutStreamer(std::move(Streamer)),
- SM(*this) {
+ : TM(tm), MAI(tm.getMCAsmInfo()), OutContext(Streamer->getContext()),
+ OutStreamer(std::move(Streamer)), SM(*this) {
VerboseAsm = OutStreamer->isVerboseAsm();
DwarfUsesRelocationsAcrossSections =
MAI->doesDwarfUseRelocationsAcrossSections();
@@ -449,7 +482,6 @@ const MCSection *AsmPrinter::getCurrentSection() const {
void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
- MachineFunctionPass::getAnalysisUsage(AU);
AU.addRequired<MachineOptimizationRemarkEmitterPass>();
AU.addRequired<GCModuleInfo>();
AU.addRequired<LazyMachineBlockFrequencyInfoPass>();
@@ -457,8 +489,13 @@ void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
}
bool AsmPrinter::doInitialization(Module &M) {
- auto *MMIWP = getAnalysisIfAvailable<MachineModuleInfoWrapperPass>();
- MMI = MMIWP ? &MMIWP->getMMI() : nullptr;
+ if (P) {
+ auto *MMIWP = P->getAnalysisIfAvailable<MachineModuleInfoWrapperPass>();
+ MMI = MMIWP ? &MMIWP->getMMI() : nullptr;
+ } else {
+ MMI = &MAM->getResult<MachineModuleAnalysis>(M).getMMI();
+ }
+
HasSplitStack = false;
HasNoSplitStack = false;
DbgInfoAvailable = !M.debug_compile_units().empty();
@@ -540,11 +577,18 @@ 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);
+ if (P) {
+ GCModuleInfo *MI = P->getAnalysisIfAvailable<GCModuleInfo>();
+ assert(MI && "AsmPrinter didn't require GCModuleInfo?");
+ for (const auto &I : *MI)
+ if (GCMetadataPrinter *MP = getOrCreateGCPrinter(*I))
+ MP->beginAssembly(M, *MI, *this);
+ } else {
+ auto &MI = MAM->getResult<CollectorMetadataAnalysis>(M);
+ for (const auto &I : MI)
+ if (GCMetadataPrinter *MP = getOrCreateGCPrinter(*I))
+ MP->beginAssembly(M, MI, *this);
+ }
// Emit module-level inline asm if it exists.
if (!M.getModuleInlineAsm().empty()) {
@@ -1513,14 +1557,24 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
OutStreamer->emitULEB128IntValue(
MaybeEntryCount ? MaybeEntryCount->getCount() : 0);
}
- const MachineBlockFrequencyInfo *MBFI =
- Features.BBFreq
- ? &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI()
- : nullptr;
- const MachineBranchProbabilityInfo *MBPI =
- Features.BrProb
- ? &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI()
- : nullptr;
+ const MachineBlockFrequencyInfo *MBFI = Features.BBFreq ? [this]() {
+ if (P) {
+ auto *Wrapper =
+ P->getAnalysisIfAvailable<LazyMachineBlockFrequencyInfoPass>();
+ return Wrapper ? &Wrapper->getBFI() : nullptr;
+ }
+ return &MFAM->getResult<MachineBlockFrequencyAnalysis>(*this->MF);
+ }()
+ : nullptr;
+ const MachineBranchProbabilityInfo *MBPI = Features.BrProb ? [this]() {
+ if (P) {
+ auto *Wrapper = P->getAnalysisIfAvailable<
+ MachineBranchProbabilityInfoWrapperPass>();
+ return Wrapper ? &Wrapper->getMBPI() : nullptr;
+ }
+ return &MFAM->getResult<MachineBranchProbabilityAnalysis>(*this->MF);
+ }()
+ : nullptr;
if (Features.BBFreq || Features.BrProb) {
for (const MachineBasicBlock &MBB : MF) {
@@ -1782,21 +1836,29 @@ 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;
- if (!MDT) {
- OwnedMDT = std::make_unique<MachineDominatorTree>();
- OwnedMDT->recalculate(*MF);
- MDT = OwnedMDT.get();
+ if (P) {
+ auto MDTWrapper =
+ P->getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>();
+ MDT = MDTWrapper ? &MDTWrapper->getDomTree() : nullptr;
+ if (!MDT) {
+ OwnedMDT = std::make_unique<MachineDominatorTree>();
+ OwnedMDT->recalculate(*MF);
+ MDT = OwnedMDT.get();
+ }
+ } else {
+ MDT = &MFAM->getResult<MachineDominatorTreeAnalysis>(*MF);
}
// Get MachineLoopInfo or compute it on the fly if it's unavailable
- auto *MLIWrapper = getAnalysisIfAvailable<MachineLoopInfoWrapperPass>();
+ auto *MLIWrapper = P->getAnalysisIfAvailable<MachineLoopInfoWrapperPass>();
MLI = MLIWrapper ? &MLIWrapper->getLI() : nullptr;
if (!MLI) {
OwnedMLI = std::make_unique<MachineLoopInfo>();
OwnedMLI->analyze(*MDT);
MLI = OwnedMLI.get();
+ } else {
+ MDT = &MFAM->getResult<MachineDominatorTreeAnalysis>(*MF);
+ MLI = &MFAM->getResult<MachineLoopAnalysis>(*MF);
}
}
@@ -2556,7 +2618,10 @@ 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();
+ if (P)
+ emitStackMaps();
+ else
+ emitStackMaps(M);
// Print aliases in topological order, that is, for each alias a = b,
// b must be printed before a.
@@ -2623,11 +2688,18 @@ 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);
+ if (P) {
+ GCModuleInfo *MI = P->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);
+ } else {
+ auto &MI = MAM->getResult<CollectorMetadataAnalysis>(M);
+ for (auto &I : llvm::reverse(MI))
+ if (GCMetadataPrinter *MP = getOrCreateGCPrinter(*I))
+ MP->finishAssembly(M, MI, *this);
+ }
// Emit llvm.ident metadata in an '.ident' directive.
emitModuleIdents(M);
@@ -2755,7 +2827,10 @@ void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
CurrentFnSymForSize = CurrentFnBegin;
}
- ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
+ if (P)
+ ORE = &P->getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
+ else
+ ORE = &MFAM->getResult<MachineOptimizationRemarkEmitterAnalysis>(MF);
}
namespace {
@@ -4434,7 +4509,7 @@ GCMetadataPrinter *AsmPrinter::getOrCreateGCPrinter(GCStrategy &S) {
}
void AsmPrinter::emitStackMaps() {
- GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
+ GCModuleInfo *MI = P->getAnalysisIfAvailable<GCModuleInfo>();
assert(MI && "AsmPrinter didn't require GCModuleInfo?");
bool NeedsDefault = false;
if (MI->begin() == MI->end())
@@ -4454,6 +4529,26 @@ void AsmPrinter::emitStackMaps() {
SM.serializeToStackMapSection();
}
+void AsmPrinter::emitStackMaps(Module &M) {
+ auto &Map = MAM->getResult<CollectorMetadataAnalysis>(M);
+ bool NeedsDefault = false;
+ if (Map.empty())
+ // No GC strategy, use the default format.
+ NeedsDefault = true;
+ else
+ for (const auto &I : Map) {
+ 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));
diff --git a/llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp b/llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp
index 5757ca1b3adf8..dccc545f5178b 100644
--- a/llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp
+++ b/llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp
@@ -136,8 +136,8 @@ bool CodeGenTargetMachineImpl::addAsmPrinter(PassManagerBase &PM,
return true;
// Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
- FunctionPass *Printer =
- getTarget().createAsmPrinter(*this, std::move(*MCStreamerOrErr));
+ AsmPrinterLegacy *Printer =
+ getTarget().createAsmPrinterLegacy(*this, std::move(*MCStreamerOrErr));
if (!Printer)
return true;
@@ -276,7 +276,7 @@ bool CodeGenTargetMachineImpl::addPassesToEmitMC(PassManagerBase &PM,
// Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
FunctionPass *Printer =
- getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
+ getTarget().createAsmPrinterLegacy(*this, std::move(AsmStreamer));
if (!Printer)
return true;
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 9b93ebc36ae10..2222fd9ddcf61 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -77,6 +77,7 @@
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
#include "llvm/Analysis/UniformityAnalysis.h"
+#include "llvm/CodeGen/AsmPrinter.h"
#include "llvm/CodeGen/AssignmentTrackingAnalysis.h"
#include "llvm/CodeGen/AtomicExpand.h"
#include "llvm/CodeGen/BasicBlockSectionsProfileReader.h"
@@ -150,6 +151,7 @@
#include "llvm/IR/SafepointIRVerifier.h"
#include "llvm/IR/Verifier.h"
#include "llvm/IRPrinter/IRPrintingPasses.h"
+#include "llvm/MC/MCStreamer.h"
#include "llvm/Passes/OptimizationLevel.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
@@ -2399,6 +2401,11 @@ PassBuilder::parseRegAllocFilter(StringRef FilterName) {
return std::nullopt;
}
+IntrusiveRefCntPtr<AsmPrinter>
+PassBuilder::getAsmPrinter(std::unique_ptr<MCStreamer> Streamer) {
+ return AsmPrinterCreationCallback(std::move(Streamer));
+}
+
static void printPassName(StringRef PassName, raw_ostream &OS) {
OS << " " << PassName << "\n";
}
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp b/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
index 031d8f0560ff2..4a96509ea42ed 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
@@ -77,17 +77,9 @@ static uint32_t getFPMode(SIModeRegisterDefaults Mode) {
FP_DENORM_MODE_DP(Mode.fpDenormModeDPValue());
}
-static AsmPrinter *
-createAMDGPUAsmPrinterPass(TargetMachine &tm,
- std::unique_ptr<MCStreamer> &&Streamer) {
- return new AMDGPUAsmPrinter(tm, std::move(Streamer));
-}
-
extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAMDGPUAsmPrinter() {
- TargetRegistry::RegisterAsmPrinter(getTheR600Target(),
- llvm::createR600AsmPrinterPass);
- TargetRegistry::RegisterAsmPrinter(getTheGCNTarget(),
- createAMDGPUAsmPrinterPass);
+ RegisterAsmPrinter<R600AsmPrinter> X(getTheR600Target());
+ RegisterAsmPrinter<AMDGPUAsmPrinter> Y(getTheGCNTarget());
}
AMDGPUAsmPrinter::AMDGPUAsmPrinter(TargetMachine &TM,
@@ -642,7 +634,8 @@ bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
if (!IsTargetStreamerInitialized)
initTargetStreamer(*MF.getFunction().getParent());
- ResourceUsage = &getAnalysis<AMDGPUResourceUsageAnalysis>();
+ if (P)
+ ResourceUsage = &P->getAnalysis<AMDGPUResourceUsageAnalysis>();
CurrentProgramInfo.reset(MF);
const AMDGPUMachineFunction *MFI = MF.getInfo<AMDGPUMachineFunction>();
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp b/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
index 96062b30fc012..2adcf5757193d 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
@@ -1038,12 +1038,14 @@ GCNTargetMachine::getTargetTransformInfo(const Function &F) const {
return TargetTransformInfo(GCNTTIImpl(this, F));
}
-Error GCNTargetMachine::buildCodeGenPipeline(
- ModulePassManager &MPM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut,
- CodeGenFileType FileType, const CGPassBuilderOption &Opts,
- PassInstrumentationCallbacks *PIC) {
- AMDGPUCodeGenPassBuilder CGPB(*this, Opts, PIC);
- return CGPB.buildPipeline(MPM, Out, DwoOut, FileType);
+Error GCNTargetMachine::buildCodeGenPipeline(ModulePassManager &MPM,
+ raw_pwrite_stream &Out,
+ raw_pwrite_stream *DwoOut,
+ CodeGenFileType FileType,
+ const CGPassBuilderOption &Opts,
+ MCContext &Ctx, PassBuilder &PB) {
+ AMDGPUCodeGenPassBuilder CGPB(*this, Opts, PB);
+ return CGPB.buildPipeline(MPM, Out, DwoOut, FileType, Ctx);
}
//===----------------------------------------------------------------------===//
@@ -1929,9 +1931,8 @@ bool GCNTargetMachine::parseMachineFunctionInfo(
//===----------------------------------------------------------------------===//
AMDGPUCodeGenPassBuilder::AMDGPUCodeGenPassBuilder(
- GCNTargetMachine &TM, const CGPassBuilderOption &Opts,
- PassInstrumentationCallbacks *PIC)
- : CodeGenPassBuilder(TM, Opts, PIC) {
+ GCNTargetMachine &TM, const CGPassBuilderOption &Opts, PassBuilder &PB)
+ : CodeGenPassBuilder(TM, Opts, PB) {
Opt.RequiresCodeGenSCCOrder = true;
// Exceptions and StackMaps are not supported, so these passes will never do
// anything.
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.h b/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.h
index 24b4da3a68f67..80cca9ab0cd5a 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.h
+++ b/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.h
@@ -96,11 +96,10 @@ class GCNTargetMachine final : public AMDGPUTargetMachine {
bool useIPRA() const override { return true; }
- Error buildCodeGenPipeline(ModulePassManager &MPM, raw_pwrite_stream &Out,
- raw_pwrite_stream *DwoOut,
- CodeGenFileType FileType,
- const CGPassBuilderOption &Opts,
- PassInstrumentationCallbacks *PIC) override;
+ Error buildCodeGenPipeline(ModulePassManager &, raw_pwrite_stream &,
+ raw_pwrite_stream *, CodeGenFileType,
+ const CGPassBuilderOption &, MCContext &,
+ PassBuilder &) override;
void registerMachineRegisterInfoCallback(MachineFunction &MF) const override;
@@ -166,8 +165,7 @@ class AMDGPUCodeGenPassBuilder
public:
AMDGPUCodeGenPassBuilder(GCNTargetMachine &TM,
- const CGPassBuilderOption &Opts,
- PassInstrumentationCallbacks *PIC);
+ const CGPassBuilderOption &Opts, PassBuilder &PB);
void addIRPasses(AddIRPass &) const;
void addCodeGenPrepare(AddIRPass &) const;
diff --git a/llvm/lib/Target/AMDGPU/R600TargetMachine.cpp b/llvm/lib/Target/AMDGPU/R600TargetMachine.cpp
index a9e5327d2c6c8..68dfa5fa3007d 100644
--- a/llvm/lib/Target/AMDGPU/R600TargetMachine.cpp
+++ b/llvm/lib/Target/AMDGPU/R600TargetMachine.cpp
@@ -147,12 +147,14 @@ TargetPassConfig *R600TargetMachine::createPassConfig(PassManagerBase &PM) {
return new R600PassConfig(*this, PM);
}
-Error R600TargetMachine::buildCodeGenPipeline(
- ModulePassManager &MPM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut,
- CodeGenFileType FileType, const CGPassBuilderOption &Opts,
- PassInstrumentationCallbacks *PIC) {
- R600CodeGenPassBuilder CGPB(*this, Opts, PIC);
- return CGPB.buildPipeline(MPM, Out, DwoOut, FileType);
+Error R600TargetMachine::buildCodeGenPipeline(ModulePassManager &MPM,
+ raw_pwrite_stream &Out,
+ raw_pwrite_stream *DwoOut,
+ CodeGenFileType FileType,
+ const CGPassBuilderOption &Opts,
+ MCContext &Ctx, PassBuilder &PB) {
+ R600CodeGenPassBuilder CGPB(*this, Opts, PB);
+ return CGPB.buildPipeline(MPM, Out, DwoOut, FileType, Ctx);
}
MachineFunctionInfo *R600TargetMachine::createMachineFunctionInfo(
diff --git a/llvm/lib/Target/AMDGPU/R600TargetMachine.h b/llvm/lib/Target/AMDGPU/R600TargetMachine.h
index b7f123a07a9c1..b014dedbb59e0 100644
--- a/llvm/lib/Target/AMDGPU/R600TargetMachine.h
+++ b/llvm/lib/Target/AMDGPU/R600TargetMachine.h
@@ -41,8 +41,8 @@ class R600TargetMachine final : public AMDGPUTargetMachine {
Error buildCodeGenPipeline(ModulePassManager &MPM, raw_pwrite_stream &Out,
raw_pwrite_stream *DwoOut,
CodeGenFileType FileType,
- const CGPassBuilderOption &Opt,
- PassInstrumentationCallbacks *PIC) override;
+ const CGPassBuilderOption &Opt, MCContext &Ctx,
+ PassBuilder &PB) override;
const TargetSubtargetInfo *getSubtargetImpl(const Function &) const override;
diff --git a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
index cb756246b8d11..bd509170c8721 100644
--- a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
@@ -411,7 +411,12 @@ void NVPTXAsmPrinter::printReturnValStr(const MachineFunction &MF,
// llvm.loop.unroll.disable or llvm.loop.unroll.count=1.
bool NVPTXAsmPrinter::isLoopHeaderOfNoUnroll(
const MachineBasicBlock &MBB) const {
- MachineLoopInfo &LI = getAnalysis<MachineLoopInfoWrapperPass>().getLI();
+ MachineLoopInfo *LIPtr = nullptr;
+ if (P)
+ LIPtr = &P->getAnalysis<MachineLoopInfoWrapperPass>().getLI();
+ else
+ LIPtr = &MFAM->getResult<MachineLoopAnalysis>(*MF);
+ auto &LI = *LIPtr;
// We insert .pragma "nounroll" only to the loop header.
if (!LI.isLoopHeader(&MBB))
return false;
diff --git a/llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp b/llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp
index 162d11058266f..eba41b4ae6a65 100644
--- a/llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp
+++ b/llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp
@@ -3359,6 +3359,13 @@ createPPCAsmPrinterPass(TargetMachine &tm,
return new PPCLinuxAsmPrinter(tm, std::move(Streamer));
}
+static AsmPrinterLegacy *
+createPPCAsmPrinterLegacyPass(TargetMachine &tm,
+ std::unique_ptr<MCStreamer> &&Streamer) {
+ return new AsmPrinterLegacy(std::unique_ptr<AsmPrinter>(
+ createPPCAsmPrinterPass(tm, std::move(Streamer))));
+}
+
void PPCAIXAsmPrinter::emitModuleCommandLines(Module &M) {
const NamedMDNode *NMD = M.getNamedMetadata("llvm.commandline");
if (!NMD || !NMD->getNumOperands())
@@ -3389,4 +3396,13 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializePowerPCAsmPrinter() {
createPPCAsmPrinterPass);
TargetRegistry::RegisterAsmPrinter(getThePPC64LETarget(),
createPPCAsmPrinterPass);
+
+ TargetRegistry::RegisterAsmPrinter(getThePPC32Target(),
+ createPPCAsmPrinterLegacyPass);
+ TargetRegistry::RegisterAsmPrinter(getThePPC32LETarget(),
+ createPPCAsmPrinterLegacyPass);
+ TargetRegistry::RegisterAsmPrinter(getThePPC64Target(),
+ createPPCAsmPrinterLegacyPass);
+ TargetRegistry::RegisterAsmPrinter(getThePPC64LETarget(),
+ createPPCAsmPrinterLegacyPass);
}
diff --git a/llvm/lib/Target/X86/CMakeLists.txt b/llvm/lib/Target/X86/CMakeLists.txt
index 9553a8619feb5..769ee314cd5a7 100644
--- a/llvm/lib/Target/X86/CMakeLists.txt
+++ b/llvm/lib/Target/X86/CMakeLists.txt
@@ -103,6 +103,7 @@ add_llvm_target(X86CodeGen ${sources}
IRPrinter
Instrumentation
MC
+ Passes
ProfileData
Scalar
SelectionDAG
diff --git a/llvm/lib/Target/X86/X86CodeGenPassBuilder.cpp b/llvm/lib/Target/X86/X86CodeGenPassBuilder.cpp
index d979517e12af6..cf0ba2d9ea5aa 100644
--- a/llvm/lib/Target/X86/X86CodeGenPassBuilder.cpp
+++ b/llvm/lib/Target/X86/X86CodeGenPassBuilder.cpp
@@ -10,9 +10,11 @@
/// TODO: Port CodeGen passes to new pass manager.
//===----------------------------------------------------------------------===//
+#include "X86AsmPrinter.h"
#include "X86ISelDAGToDAG.h"
#include "X86TargetMachine.h"
+#include "llvm/MC/MCCodeEmitter.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/Passes/CodeGenPassBuilder.h"
#include "llvm/Passes/PassBuilder.h"
@@ -26,8 +28,8 @@ class X86CodeGenPassBuilder
public:
explicit X86CodeGenPassBuilder(X86TargetMachine &TM,
const CGPassBuilderOption &Opts,
- PassInstrumentationCallbacks *PIC)
- : CodeGenPassBuilder(TM, Opts, PIC) {}
+ PassBuilder &PB)
+ : CodeGenPassBuilder(TM, Opts, PB) {}
void addPreISel(AddIRPass &addPass) const;
void addAsmPrinter(AddMachinePass &, CreateMCStreamer) const;
Error addInstSelector(AddMachinePass &) const;
@@ -53,12 +55,19 @@ Error X86CodeGenPassBuilder::addInstSelector(AddMachinePass &addPass) const {
void X86TargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) {
#define GET_PASS_REGISTRY "X86PassRegistry.def"
#include "llvm/Passes/TargetPassRegistry.inc"
+
+ PB.registerAsmPrinterCreationCallback(
+ [this](std::unique_ptr<MCStreamer> Streamer) {
+ return makeIntrusiveRefCnt<X86AsmPrinter>(*this, std::move(Streamer));
+ });
}
-Error X86TargetMachine::buildCodeGenPipeline(
- ModulePassManager &MPM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut,
- CodeGenFileType FileType, const CGPassBuilderOption &Opt,
- PassInstrumentationCallbacks *PIC) {
- auto CGPB = X86CodeGenPassBuilder(*this, Opt, PIC);
- return CGPB.buildPipeline(MPM, Out, DwoOut, FileType);
+Error X86TargetMachine::buildCodeGenPipeline(ModulePassManager &MPM,
+ raw_pwrite_stream &Out,
+ raw_pwrite_stream *DwoOut,
+ CodeGenFileType FileType,
+ const CGPassBuilderOption &Opt,
+ MCContext &Ctx, PassBuilder &PB) {
+ auto CGPB = X86CodeGenPassBuilder(*this, Opt, PB);
+ return CGPB.buildPipeline(MPM, Out, DwoOut, FileType, Ctx);
}
diff --git a/llvm/lib/Target/X86/X86MCInstLower.cpp b/llvm/lib/Target/X86/X86MCInstLower.cpp
index 645a9baeba65c..da9ed4d1a6a00 100644
--- a/llvm/lib/Target/X86/X86MCInstLower.cpp
+++ b/llvm/lib/Target/X86/X86MCInstLower.cpp
@@ -2459,8 +2459,12 @@ void X86AsmPrinter::emitInstruction(const MachineInstr *MI) {
// taken) are used as branch hints. Here we add branch taken prefix for
// jump instruction with higher probability than threshold.
if (getSubtarget().hasBranchHint() && EnableBranchHint) {
- const MachineBranchProbabilityInfo *MBPI =
- &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI();
+ const MachineBranchProbabilityInfo *MBPI = nullptr;
+ if (P)
+ MBPI = &P->getAnalysis<MachineBranchProbabilityInfoWrapperPass>()
+ .getMBPI();
+ else
+ MBPI = &MFAM->getResult<MachineBranchProbabilityAnalysis>(*MF);
MachineBasicBlock *DestBB = MI->getOperand(0).getMBB();
BranchProbability EdgeProb =
MBPI->getEdgeProbability(MI->getParent(), DestBB);
diff --git a/llvm/lib/Target/X86/X86TargetMachine.h b/llvm/lib/Target/X86/X86TargetMachine.h
index b8d84a8d20032..e82b812226a13 100644
--- a/llvm/lib/Target/X86/X86TargetMachine.h
+++ b/llvm/lib/Target/X86/X86TargetMachine.h
@@ -73,8 +73,8 @@ class X86TargetMachine final : public CodeGenTargetMachineImpl {
Error buildCodeGenPipeline(ModulePassManager &, raw_pwrite_stream &,
raw_pwrite_stream *, CodeGenFileType,
- const CGPassBuilderOption &,
- PassInstrumentationCallbacks *) override;
+ const CGPassBuilderOption &, MCContext &,
+ PassBuilder &) override;
bool isJIT() const { return IsJIT; }
diff --git a/llvm/tools/llc/NewPMDriver.cpp b/llvm/tools/llc/NewPMDriver.cpp
index 3892fbb8c74f7..6264b75e4ed40 100644
--- a/llvm/tools/llc/NewPMDriver.cpp
+++ b/llvm/tools/llc/NewPMDriver.cpp
@@ -156,8 +156,9 @@ int llvm::compileModuleWithNewPM(
if (MIR->parseMachineFunctions(*M, MAM))
return 1;
} else {
- ExitOnErr(Target->buildCodeGenPipeline(
- MPM, *OS, DwoOut ? &DwoOut->os() : nullptr, FileType, Opt, &PIC));
+ ExitOnErr(
+ Target->buildCodeGenPipeline(MPM, *OS, DwoOut ? &DwoOut->os() : nullptr,
+ FileType, Opt, MMI.getContext(), PB));
}
if (PrintPipelinePasses) {
diff --git a/llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp b/llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp
index 6c08173f78622..062a8fa320bdf 100644
--- a/llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp
+++ b/llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp
@@ -404,7 +404,8 @@ class AsmPrinterHandlerTest : public AsmPrinterFixtureBase {
TargetMachine *TM = &AP->TM;
legacy::PassManager PM;
PM.add(new MachineModuleInfoWrapperPass(TM));
- PM.add(TestPrinter->releaseAP()); // Takes ownership of destroying AP
+ PM.add(new AsmPrinterLegacy(std::unique_ptr<AsmPrinter>(
+ TestPrinter->releaseAP()))); // Takes ownership of destroying AP
LLVMContext Context;
std::unique_ptr<Module> M(new Module("TestModule", Context));
M->setDataLayout(TM->createDataLayout());
More information about the llvm-commits
mailing list