[llvm] [SPIR-V] Move MIR rewrites out of SPIRVModuleAnalysis (PR #229736)
Arseniy Obolenskiy via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 7 05:04:59 PDT 2026
https://github.com/aobolensk created https://github.com/llvm/llvm-project/pull/229736
Analysis pass must not change the IR it analyzes
Move all modifications from there to a new separate spirv-prepare-module-analysis pass
Discussed in https://github.com/llvm/llvm-project/pull/227376
>From 9e0d5f3ced91e35976bc6bc03c286809d9c090c0 Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Wed, 7 Oct 2026 14:02:58 +0200
Subject: [PATCH] [SPIR-V] Move MIR rewrites out of SPIRVModuleAnalysis
Analysis pass must not change the IR it analyzes
---
llvm/lib/Target/SPIRV/SPIRV.h | 11 ++
llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp | 7 +
.../Target/SPIRV/SPIRVCodeGenPassBuilder.cpp | 6 +
llvm/lib/Target/SPIRV/SPIRVMCInstLower.cpp | 13 +-
llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp | 151 +++++++++---------
llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.h | 7 +-
.../SPIRV/SPIRVNonSemanticDebugHandler.cpp | 4 +-
llvm/lib/Target/SPIRV/SPIRVPassRegistry.def | 1 +
llvm/lib/Target/SPIRV/SPIRVTargetMachine.cpp | 6 +
llvm/lib/Target/SPIRV/SPIRVUtils.cpp | 10 --
llvm/lib/Target/SPIRV/SPIRVUtils.h | 2 -
llvm/test/CodeGen/SPIRV/llc-pipeline.ll | 2 +
12 files changed, 130 insertions(+), 90 deletions(-)
diff --git a/llvm/lib/Target/SPIRV/SPIRV.h b/llvm/lib/Target/SPIRV/SPIRV.h
index 1c8e881c307b7..80706bf477065 100644
--- a/llvm/lib/Target/SPIRV/SPIRV.h
+++ b/llvm/lib/Target/SPIRV/SPIRV.h
@@ -146,6 +146,16 @@ class SPIRVPostLegalizerPass
FunctionPass *createSPIRVPostLegalizerLegacyPass();
+/// Rewrites MIR into the form SPIRVModuleAnalysis expects.
+class SPIRVPrepareModuleAnalysisPass
+ : public RequiredPassInfoMixin<SPIRVPrepareModuleAnalysisPass> {
+public:
+ PreservedAnalyses run(MachineFunction &MF,
+ MachineFunctionAnalysisManager &MFAM);
+};
+
+FunctionPass *createSPIRVPrepareModuleAnalysisLegacyPass();
+
class SPIRVEmitIntrinsicsPass
: public RequiredPassInfoMixin<SPIRVEmitIntrinsicsPass> {
const SPIRVTargetMachine &TM;
@@ -184,6 +194,7 @@ void initializeSPIRVConvergenceRegionAnalysisWrapperPassPass(PassRegistry &);
void initializeSPIRVPreLegalizerLegacyPass(PassRegistry &);
void initializeSPIRVPreLegalizerCombinerLegacyPass(PassRegistry &);
void initializeSPIRVPostLegalizerLegacyPass(PassRegistry &);
+void initializeSPIRVPrepareModuleAnalysisLegacyPass(PassRegistry &);
void initializeSPIRVStructurizerPass(PassRegistry &);
void initializeSPIRVCBufferAccessLegacyPass(PassRegistry &);
void initializeSPIRVPushConstantAccessLegacyPass(PassRegistry &);
diff --git a/llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp b/llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp
index 685a4f553ca2a..3d7a18bb6d161 100644
--- a/llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp
@@ -929,6 +929,13 @@ void SPIRVAsmPrinter::outputModuleSections() {
outputDebugSourceAndStrings(*M);
// 7b. Debug: all OpName and all OpMemberName.
outputModuleSection(SPIRV::MB_DebugNames);
+ for (const auto &[Reg, Name] : MAI->MBBNames) {
+ MCInst Inst;
+ Inst.setOpcode(SPIRV::OpName);
+ Inst.addOperand(MCOperand::createReg(Reg));
+ addStringImm(Name, Inst);
+ outputMCInst(Inst);
+ }
// 7c. Debug: all OpModuleProcessed instructions.
outputModuleSection(SPIRV::MB_DebugModuleProcessed);
// xxx. SPV_INTEL_memory_access_aliasing instructions go before 8.
diff --git a/llvm/lib/Target/SPIRV/SPIRVCodeGenPassBuilder.cpp b/llvm/lib/Target/SPIRV/SPIRVCodeGenPassBuilder.cpp
index 6481a20217342..ae9396aa9a40f 100644
--- a/llvm/lib/Target/SPIRV/SPIRVCodeGenPassBuilder.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVCodeGenPassBuilder.cpp
@@ -73,6 +73,8 @@ class SPIRVCodeGenPassBuilder : public CodeGenPassBuilder {
Error addOptimizedRegAlloc(PassManagerWrapper &PMW) override;
+ void addPreEmitPass2(PassManagerWrapper &PMW) override;
+
void addAsmPrinterBegin(PassManagerWrapper &PMW) override;
void addAsmPrinter(PassManagerWrapper &PMW) override;
void addAsmPrinterEnd(PassManagerWrapper &PMW) override;
@@ -189,6 +191,10 @@ Error SPIRVCodeGenPassBuilder::addOptimizedRegAlloc(PassManagerWrapper &PMW) {
return Error::success();
}
+void SPIRVCodeGenPassBuilder::addPreEmitPass2(PassManagerWrapper &PMW) {
+ addMachineFunctionPass(SPIRVPrepareModuleAnalysisPass(), PMW);
+}
+
void SPIRVCodeGenPassBuilder::addAsmPrinterBegin(PassManagerWrapper &PMW) {
addModulePass(SPIRVAsmPrinterBeginPass(), PMW);
}
diff --git a/llvm/lib/Target/SPIRV/SPIRVMCInstLower.cpp b/llvm/lib/Target/SPIRV/SPIRVMCInstLower.cpp
index 51ce580765db3..fd8d94de2b260 100644
--- a/llvm/lib/Target/SPIRV/SPIRVMCInstLower.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVMCInstLower.cpp
@@ -13,6 +13,7 @@
#include "SPIRVMCInstLower.h"
#include "SPIRVModuleAnalysis.h"
+#include "SPIRVSubtarget.h"
#include "SPIRVUtils.h"
#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/IR/Constants.h"
@@ -21,7 +22,7 @@ using namespace llvm;
void SPIRVMCInstLower::lower(const MachineInstr *MI, MCInst &OutMI,
SPIRV::ModuleAnalysisInfo *MAI) const {
- OutMI.setOpcode(MI->getOpcode());
+ OutMI.setOpcode(MI->isPHI() ? SPIRV::OpPhi : MI->getOpcode());
// Propagate previously set flags
if (MI->getAsmPrinterFlags() & SPIRV::ASM_PRINTER_WIDTH16)
OutMI.setFlags(SPIRV::INST_PRINTER_WIDTH16);
@@ -71,5 +72,15 @@ void SPIRVMCInstLower::lower(const MachineInstr *MI, MCInst &OutMI,
}
OutMI.addOperand(MCOp);
+
+ // OpPhi takes the result type right after the result.
+ if (i == 0 && MI->isPHI()) {
+ SPIRVGlobalRegistry *GR =
+ MF->getSubtarget<SPIRVSubtarget>().getSPIRVGlobalRegistry();
+ Register TypeReg =
+ GR->getSPIRVTypeID(GR->getSPIRVTypeForVReg(MO.getReg(), MF));
+ OutMI.addOperand(
+ MCOperand::createReg(MAI->getRegisterAlias(MF, TypeReg)));
+ }
}
}
diff --git a/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp b/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp
index 0ba4174615b56..17378ccda09d4 100644
--- a/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp
@@ -148,6 +148,7 @@ void SPIRVModuleAnalysisImpl::setBaseInfo(const Module &M) {
MAI.MS[i].clear();
MAI.RegisterAliasTable.clear();
MAI.InstrsToDelete.clear();
+ MAI.MBBNames.clear();
MAI.GlobalObjMap.clear();
MAI.GlobalVarList.clear();
MAI.ExtInstSetMap.clear();
@@ -2871,21 +2872,27 @@ static void handleMIFlagDecoration(
MachineInstr &I, const SPIRVSubtarget &ST, const SPIRVInstrInfo &TII,
SPIRV::RequirementHandler &Reqs, const SPIRVGlobalRegistry *GR,
SPIRV::FPFastMathDefaultInfoVector &FPFastMathDefaultInfoVec) {
+ // Insert after I so that the decorated register is defined before its use.
+ MachineBasicBlock::iterator InsertPt = std::next(I.getIterator());
+ auto Decorate = [&](SPIRV::Decoration::Decoration Dec,
+ ArrayRef<uint32_t> DecArgs) {
+ MachineIRBuilder MIRBuilder(*I.getParent(), InsertPt);
+ MIRBuilder.setDebugLoc(I.getDebugLoc());
+ buildOpDecorate(I.getOperand(0).getReg(), MIRBuilder, Dec, DecArgs);
+ };
if (TII.canUseIntegerWrapDecoration(I)) {
if (I.getFlag(MachineInstr::MIFlag::NoSWrap) &&
getSymbolicOperandRequirements(
SPIRV::OperandCategory::DecorationOperand,
SPIRV::Decoration::NoSignedWrap, ST, Reqs)
.IsSatisfiable)
- buildOpDecorate(I.getOperand(0).getReg(), I, TII,
- SPIRV::Decoration::NoSignedWrap, {});
+ Decorate(SPIRV::Decoration::NoSignedWrap, {});
if (I.getFlag(MachineInstr::MIFlag::NoUWrap) &&
getSymbolicOperandRequirements(
SPIRV::OperandCategory::DecorationOperand,
SPIRV::Decoration::NoUnsignedWrap, ST, Reqs)
.IsSatisfiable)
- buildOpDecorate(I.getOperand(0).getReg(), I, TII,
- SPIRV::Decoration::NoUnsignedWrap, {});
+ Decorate(SPIRV::Decoration::NoUnsignedWrap, {});
}
// In Kernel environments, FPFastMathMode on OpExtInst is valid per core
// spec. For other instruction types, SPV_KHR_float_controls2 is required.
@@ -2938,33 +2945,11 @@ static void handleMIFlagDecoration(
if (FMFlags == SPIRV::FPFastMathMode::None && !Emit)
return;
}
- if (isFastMathModeAvailable(ST)) {
- Register DstReg = I.getOperand(0).getReg();
- buildOpDecorate(DstReg, I, TII, SPIRV::Decoration::FPFastMathMode,
- {FMFlags});
- }
-}
-
-// Walk all functions and add decorations related to MI flags.
-static void addDecorations(const Module &M, const SPIRVInstrInfo &TII,
- MachineFunctionGetter GetMF,
- const SPIRVSubtarget &ST,
- SPIRV::ModuleAnalysisInfo &MAI,
- const SPIRVGlobalRegistry *GR) {
- for (const Function &F : M) {
- MachineFunction *MF = GetMF(F);
- if (!MF)
- continue;
-
- for (auto &MBB : *MF)
- for (auto &MI : MBB)
- handleMIFlagDecoration(MI, ST, TII, MAI.Reqs, GR,
- MAI.FPFastMathDefaultInfoMap[&F]);
- }
+ if (isFastMathModeAvailable(ST))
+ Decorate(SPIRV::Decoration::FPFastMathMode, {FMFlags});
}
-static void addMBBNames(const Module &M, const SPIRVInstrInfo &TII,
- MachineFunctionGetter GetMF, const SPIRVSubtarget &ST,
+static void addMBBNames(const Module &M, MachineFunctionGetter GetMF,
SPIRV::ModuleAnalysisInfo &MAI) {
for (const Function &F : M) {
MachineFunction *MF = GetMF(F);
@@ -2974,45 +2959,20 @@ static void addMBBNames(const Module &M, const SPIRVInstrInfo &TII,
.getFnAttribute(SPIRV_BACKEND_SERVICE_FUN_NAME)
.isValid())
continue;
- MachineRegisterInfo &MRI = MF->getRegInfo();
- for (auto &MBB : *MF) {
+ for (const auto &MBB : *MF) {
if (!MBB.hasName() || MBB.empty())
continue;
- // Emit basic block names.
- Register Reg = MRI.createGenericVirtualRegister(LLT::scalar(64));
- MRI.setRegClass(Reg, &SPIRV::IDRegClass);
- buildOpName(Reg, MBB.getName(), *std::prev(MBB.end()), TII);
- MCRegister GlobalReg = MAI.getOrCreateMBBRegister(MBB);
- MAI.setRegisterAlias(MF, Reg, GlobalReg);
- }
- }
-}
-
-// patching Instruction::PHI to SPIRV::OpPhi
-static void patchPhis(const Module &M, SPIRVGlobalRegistry *GR,
- const SPIRVInstrInfo &TII, MachineFunctionGetter GetMF) {
- for (const Function &F : M) {
- MachineFunction *MF = GetMF(F);
- if (!MF)
- continue;
- for (auto &MBB : *MF) {
- for (MachineInstr &MI : MBB.phis()) {
- MI.setDesc(TII.get(SPIRV::OpPhi));
- Register ResTypeReg = GR->getSPIRVTypeID(
- GR->getSPIRVTypeForVReg(MI.getOperand(0).getReg(), MF));
- MI.insert(MI.operands_begin() + 1,
- {MachineOperand::CreateReg(ResTypeReg, false)});
- }
+ MAI.MBBNames.emplace_back(MAI.getOrCreateMBBRegister(MBB), MBB.getName());
}
-
- MF->getProperties().setNoPHIs();
}
}
static SPIRV::FPFastMathDefaultInfoVector &getOrCreateFPFastMathDefaultInfoVec(
- const Module &M, SPIRV::ModuleAnalysisInfo &MAI, const Function *F) {
- auto it = MAI.FPFastMathDefaultInfoMap.find(F);
- if (it != MAI.FPFastMathDefaultInfoMap.end())
+ const Module &M,
+ SPIRV::FPFastMathDefaultInfoMapTy &FPFastMathDefaultInfoMap,
+ const Function *F) {
+ auto it = FPFastMathDefaultInfoMap.find(F);
+ if (it != FPFastMathDefaultInfoMap.end())
return it->second;
// If the map does not contain the entry, create a new one. Initialize it to
@@ -3025,7 +2985,7 @@ static SPIRV::FPFastMathDefaultInfoVector &getOrCreateFPFastMathDefaultInfoVec(
SPIRV::FPFastMathMode::None);
FPFastMathDefaultInfoVec.emplace_back(Type::getDoubleTy(M.getContext()),
SPIRV::FPFastMathMode::None);
- return MAI.FPFastMathDefaultInfoMap[F] = std::move(FPFastMathDefaultInfoVec);
+ return FPFastMathDefaultInfoMap[F] = std::move(FPFastMathDefaultInfoVec);
}
static SPIRV::FPFastMathDefaultInfo &getFPFastMathDefaultInfo(
@@ -3042,9 +3002,10 @@ static SPIRV::FPFastMathDefaultInfo &getFPFastMathDefaultInfo(
return FPFastMathDefaultInfoVec[Index];
}
-static void collectFPFastMathDefaults(const Module &M,
- SPIRV::ModuleAnalysisInfo &MAI,
- const SPIRVSubtarget &ST) {
+static void collectFPFastMathDefaults(
+ const Module &M,
+ SPIRV::FPFastMathDefaultInfoMapTy &FPFastMathDefaultInfoMap,
+ const SPIRVSubtarget &ST, const Function *OnlyF = nullptr) {
if (!ST.canUseExtension(SPIRV::Extension::SPV_KHR_float_controls2))
return;
@@ -3063,6 +3024,8 @@ static void collectFPFastMathDefaults(const Module &M,
assert(MDN->getNumOperands() >= 2 && "Expected at least 2 operands");
const Function *F = cast<Function>(
cast<ConstantAsMetadata>(MDN->getOperand(0))->getValue());
+ if (OnlyF && F != OnlyF)
+ continue;
const auto EM =
cast<ConstantInt>(
cast<ConstantAsMetadata>(MDN->getOperand(1))->getValue())
@@ -3077,7 +3040,7 @@ static void collectFPFastMathDefaults(const Module &M,
cast<ConstantAsMetadata>(MDN->getOperand(3))->getValue())
->getZExtValue();
SPIRV::FPFastMathDefaultInfoVector &FPFastMathDefaultInfoVec =
- getOrCreateFPFastMathDefaultInfoVec(M, MAI, F);
+ getOrCreateFPFastMathDefaultInfoVec(M, FPFastMathDefaultInfoMap, F);
SPIRV::FPFastMathDefaultInfo &Info =
getFPFastMathDefaultInfo(FPFastMathDefaultInfoVec, T);
Info.FastMathFlags = Flags;
@@ -3089,7 +3052,7 @@ static void collectFPFastMathDefaults(const Module &M,
// We need to save this info for every possible FP type, i.e. {half,
// float, double, fp128}.
SPIRV::FPFastMathDefaultInfoVector &FPFastMathDefaultInfoVec =
- getOrCreateFPFastMathDefaultInfoVec(M, MAI, F);
+ getOrCreateFPFastMathDefaultInfoVec(M, FPFastMathDefaultInfoMap, F);
for (SPIRV::FPFastMathDefaultInfo &Info : FPFastMathDefaultInfoVec) {
Info.ContractionOff = true;
}
@@ -3102,7 +3065,7 @@ static void collectFPFastMathDefaults(const Module &M,
->getZExtValue();
// We need to save this info only for the FP type with TargetWidth.
SPIRV::FPFastMathDefaultInfoVector &FPFastMathDefaultInfoVec =
- getOrCreateFPFastMathDefaultInfoVec(M, MAI, F);
+ getOrCreateFPFastMathDefaultInfoVec(M, FPFastMathDefaultInfoMap, F);
int Index = SPIRV::FPFastMathDefaultInfoVector::
computeFPFastMathDefaultInfoVecIndex(TargetWidth);
assert(Index >= 0 && Index < 3 &&
@@ -3123,11 +3086,8 @@ SPIRVModuleAnalysisImpl::SPIRVModuleAnalysisImpl(const SPIRVSubtarget &ST,
void SPIRVModuleAnalysisImpl::run(const Module &M) {
setBaseInfo(M);
- patchPhis(M, GR, *TII, GetMF);
-
- addMBBNames(M, *TII, GetMF, *ST, MAI);
- collectFPFastMathDefaults(M, MAI, *ST);
- addDecorations(M, *TII, GetMF, *ST, MAI, GR);
+ addMBBNames(M, GetMF, MAI);
+ collectFPFastMathDefaults(M, MAI.FPFastMathDefaultInfoMap, *ST);
collectReqs(M, MAI, GetMF, *ST);
@@ -3186,3 +3146,48 @@ SPIRVModuleAnalysis::run(Module &M, ModuleAnalysisManager &MAM) {
.run(M);
return MAI;
}
+
+static void prepareModuleAnalysis(MachineFunction &MF) {
+ const auto &ST = MF.getSubtarget<SPIRVSubtarget>();
+ SPIRVGlobalRegistry *GR = ST.getSPIRVGlobalRegistry();
+ const SPIRVInstrInfo &TII = *ST.getInstrInfo();
+ SPIRV::RequirementHandler Reqs;
+ Reqs.initAvailableCapabilities(ST);
+ SPIRV::FPFastMathDefaultInfoMapTy FPFastMathDefaultInfoMap;
+ const Function &F = MF.getFunction();
+ collectFPFastMathDefaults(*F.getParent(), FPFastMathDefaultInfoMap, ST, &F);
+ // Add decorations related to MI flags.
+ for (auto &MBB : MF)
+ for (auto &MI : make_early_inc_range(MBB))
+ handleMIFlagDecoration(MI, ST, TII, Reqs, GR,
+ FPFastMathDefaultInfoMap[&F]);
+}
+
+namespace {
+class SPIRVPrepareModuleAnalysisLegacy : public MachineFunctionPass {
+public:
+ static char ID;
+ SPIRVPrepareModuleAnalysisLegacy() : MachineFunctionPass(ID) {}
+ bool runOnMachineFunction(MachineFunction &MF) override {
+ prepareModuleAnalysis(MF);
+ return true;
+ }
+};
+} // namespace
+
+char SPIRVPrepareModuleAnalysisLegacy::ID = 0;
+
+INITIALIZE_PASS(SPIRVPrepareModuleAnalysisLegacy,
+ "spirv-prepare-module-analysis",
+ "SPIRV prepare module analysis", false, false)
+
+FunctionPass *llvm::createSPIRVPrepareModuleAnalysisLegacyPass() {
+ return new SPIRVPrepareModuleAnalysisLegacy();
+}
+
+PreservedAnalyses
+SPIRVPrepareModuleAnalysisPass::run(MachineFunction &MF,
+ MachineFunctionAnalysisManager &MFAM) {
+ prepareModuleAnalysis(MF);
+ return getMachineFunctionPassPreservedAnalyses();
+}
diff --git a/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.h b/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.h
index 6f939894337b0..579befc945449 100644
--- a/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.h
+++ b/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.h
@@ -130,6 +130,8 @@ using InstrList = SmallVector<const MachineInstr *>;
using LocalToGlobalRegTable = std::map<Register, MCRegister>;
using RegisterAliasMapTy =
std::map<const MachineFunction *, LocalToGlobalRegTable>;
+using FPFastMathDefaultInfoMapTy =
+ DenseMap<const Function *, FPFastMathDefaultInfoVector>;
// The struct contains results of the module analysis and methods
// to access them.
@@ -159,13 +161,14 @@ struct ModuleAnalysisInfo {
InstrList MS[NUM_MODULE_SECTIONS];
// The table maps MBB number to SPIR-V unique ID register.
DenseMap<std::pair<const MachineFunction *, int>, MCRegister> BBNumToRegMap;
+ // Basic block IDs and names to emit as OpName.
+ SmallVector<std::pair<MCRegister, StringRef>> MBBNames;
// The table maps function pointers to their default FP fast math info. It can
// be assumed that the SmallVector is sorted by the bit width of the type. The
// first element is the smallest bit width, and the last element is the
// largest bit width, therefore, we will have {half, float, double} in
// the order of their bit widths.
- DenseMap<const Function *, SPIRV::FPFastMathDefaultInfoVector>
- FPFastMathDefaultInfoMap;
+ FPFastMathDefaultInfoMapTy FPFastMathDefaultInfoMap;
MCRegister getGlobalObjReg(const GlobalObject *GO) {
assert(GO && "GlobalObject is null");
diff --git a/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.cpp b/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.cpp
index 05e79d8f8fb98..7eaba16ed1c99 100644
--- a/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.cpp
@@ -1776,7 +1776,7 @@ static std::optional<Register> getDebugValueReg(const MachineInstr &MI) {
return std::nullopt;
const MachineInstr *Def = MI.getMF()->getRegInfo().getUniqueVRegDef(ValueReg);
- if (!Def || Def->isPseudo() || Def->isMetaInstruction() ||
+ if (!Def || (Def->isPseudo() && !Def->isPHI()) || Def->isMetaInstruction() ||
Def->getNumOperands() == 0 || !Def->getOperand(0).isReg() ||
!Def->getOperand(0).isDef() || Def->getOperand(0).getReg() != ValueReg)
return std::nullopt;
@@ -1803,7 +1803,7 @@ static bool isDebugLocTarget(const MachineInstr *MI,
case SPIRV::OpFunctionParameter:
case SPIRV::OpFunctionEnd:
case SPIRV::OpLabel:
- case SPIRV::OpPhi:
+ case TargetOpcode::PHI:
return false;
default:
return true;
diff --git a/llvm/lib/Target/SPIRV/SPIRVPassRegistry.def b/llvm/lib/Target/SPIRV/SPIRVPassRegistry.def
index 6236c03f5cbef..de658699c9aa1 100644
--- a/llvm/lib/Target/SPIRV/SPIRVPassRegistry.def
+++ b/llvm/lib/Target/SPIRV/SPIRVPassRegistry.def
@@ -57,4 +57,5 @@ MACHINE_FUNCTION_PASS("spirv-asm-printer", SPIRVAsmPrinterPass())
MACHINE_FUNCTION_PASS("spirv-postlegalizer", SPIRVPostLegalizerPass())
MACHINE_FUNCTION_PASS("spirv-prelegalizer", SPIRVPreLegalizerPass())
MACHINE_FUNCTION_PASS("spirv-prelegalizer-combiner", SPIRVPreLegalizerCombinerPass())
+MACHINE_FUNCTION_PASS("spirv-prepare-module-analysis", SPIRVPrepareModuleAnalysisPass())
#undef MACHINE_FUNCTION_PASS
diff --git a/llvm/lib/Target/SPIRV/SPIRVTargetMachine.cpp b/llvm/lib/Target/SPIRV/SPIRVTargetMachine.cpp
index ddf9889c5c521..47ee6347fe258 100644
--- a/llvm/lib/Target/SPIRV/SPIRVTargetMachine.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVTargetMachine.cpp
@@ -58,6 +58,7 @@ extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSPIRVTarget() {
initializeSPIRVRegularizerLegacyPass(PR);
initializeSPIRVPreLegalizerLegacyPass(PR);
initializeSPIRVPostLegalizerLegacyPass(PR);
+ initializeSPIRVPrepareModuleAnalysisLegacyPass(PR);
initializeSPIRVMergeRegionExitTargetsLegacyPass(PR);
initializeSPIRVEmitIntrinsicsLegacyPass(PR);
initializeSPIRVPrepareFunctionsLegacyPass(PR);
@@ -119,6 +120,7 @@ class SPIRVPassConfig : public TargetPassConfig {
void addOptimizedRegAlloc() override {}
void addPostRegAlloc() override;
+ void addPreEmitPass2() override;
private:
const SPIRVTargetMachine &TM;
@@ -157,6 +159,10 @@ void SPIRVPassConfig::addPostRegAlloc() {
TargetPassConfig::addPostRegAlloc();
}
+void SPIRVPassConfig::addPreEmitPass2() {
+ addPass(createSPIRVPrepareModuleAnalysisLegacyPass());
+}
+
TargetTransformInfo
SPIRVTargetMachine::getTargetTransformInfo(const Function &F) const {
return TargetTransformInfo(std::make_unique<SPIRVTTIImpl>(this, F));
diff --git a/llvm/lib/Target/SPIRV/SPIRVUtils.cpp b/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
index 800f3a4d0c4df..a9fbd4f2efcbb 100644
--- a/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
@@ -227,16 +227,6 @@ void buildOpName(Register Target, StringRef Name,
}
}
-void buildOpName(Register Target, StringRef Name, MachineInstr &I,
- const SPIRVInstrInfo &TII) {
- if (!Name.empty()) {
- auto MIB =
- BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(SPIRV::OpName))
- .addUse(Target);
- addStringImm(Name, MIB);
- }
-}
-
static void finishBuildOpDecorate(MachineInstrBuilder &MIB,
ArrayRef<uint32_t> DecArgs,
StringRef StrImm) {
diff --git a/llvm/lib/Target/SPIRV/SPIRVUtils.h b/llvm/lib/Target/SPIRV/SPIRVUtils.h
index 83c37eb8afb50..1531149a5845d 100644
--- a/llvm/lib/Target/SPIRV/SPIRVUtils.h
+++ b/llvm/lib/Target/SPIRV/SPIRVUtils.h
@@ -196,8 +196,6 @@ void addNumImm(const APInt &Imm, MachineInstrBuilder &MIB);
// Add an OpName instruction for the given target register.
void buildOpName(Register Target, StringRef Name, MachineIRBuilder &MIRBuilder);
-void buildOpName(Register Target, StringRef Name, MachineInstr &I,
- const SPIRVInstrInfo &TII);
// Add an OpDecorate instruction for the given Reg.
void buildOpDecorate(Register Reg, MachineIRBuilder &MIRBuilder,
diff --git a/llvm/test/CodeGen/SPIRV/llc-pipeline.ll b/llvm/test/CodeGen/SPIRV/llc-pipeline.ll
index 2aa5a18fdab94..9d77bd0d59786 100644
--- a/llvm/test/CodeGen/SPIRV/llc-pipeline.ll
+++ b/llvm/test/CodeGen/SPIRV/llc-pipeline.ll
@@ -83,6 +83,7 @@
; SPIRV-O0-NEXT: Lazy Machine Block Frequency Analysis
; SPIRV-O0-NEXT: Machine Optimization Remark Emitter
; SPIRV-O0-NEXT: Stack Frame Layout Analysis
+; SPIRV-O0-NEXT: SPIRV prepare module analysis
; SPIRV-O0-NEXT: SPIRV module analysis
; SPIRV-O0-NEXT: FunctionPass Manager
; SPIRV-O0-NEXT: Lazy Machine Block Frequency Analysis
@@ -223,6 +224,7 @@
; SPIRV-Opt-NEXT: Lazy Machine Block Frequency Analysis
; SPIRV-Opt-NEXT: Machine Optimization Remark Emitter
; SPIRV-Opt-NEXT: Stack Frame Layout Analysis
+; SPIRV-Opt-NEXT: SPIRV prepare module analysis
; SPIRV-Opt-NEXT: SPIRV module analysis
; SPIRV-Opt-NEXT: FunctionPass Manager
; SPIRV-Opt-NEXT: Lazy Machine Block Frequency Analysis
More information about the llvm-commits
mailing list