diff --git a/llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp b/llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp index a7d135745b97..5f2cb30a1fc8 100644 --- a/llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp +++ b/llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp @@ -26,9 +26,8 @@ using namespace llvm; SPIRVCallLowering::SPIRVCallLowering(const SPIRVTargetLowering &TLI, - SPIRVGlobalRegistry *GR, - SPIRVGeneralDuplicatesTracker *DT) - : CallLowering(&TLI), GR(GR), DT(DT) {} + SPIRVGlobalRegistry *GR) + : CallLowering(&TLI), GR(GR) {} bool SPIRVCallLowering::lowerReturn(MachineIRBuilder &MIRBuilder, const Value *Val, ArrayRef VRegs, @@ -113,7 +112,7 @@ bool SPIRVCallLowering::lowerFormalArguments(MachineIRBuilder &MIRBuilder, auto MRI = MIRBuilder.getMRI(); Register FuncVReg = MRI->createGenericVirtualRegister(LLT::scalar(32)); if (F.isDeclaration()) - DT->add(&F, &MIRBuilder.getMF(), FuncVReg); + GR->add(&F, &MIRBuilder.getMF(), FuncVReg); MRI->setRegClass(FuncVReg, &SPIRV::IDRegClass); auto *FTy = F.getFunctionType(); @@ -238,7 +237,7 @@ bool SPIRVCallLowering::lowerCall(MachineIRBuilder &MIRBuilder, static_cast(Info.Callee.getGlobal()); Register FuncVReg; if (CF->isDeclaration() && - (DT->find(CF, &MIRBuilder.getMF(), FuncVReg) == false)) { + (GR->find(CF, &MIRBuilder.getMF(), FuncVReg) == false)) { // Emit the type info and forward function declaration to the first MBB // to ensure VReg definition dependencies are valid across all MBBs. MachineIRBuilder FirstBlockBuilder; diff --git a/llvm/lib/Target/SPIRV/SPIRVCallLowering.h b/llvm/lib/Target/SPIRV/SPIRVCallLowering.h index 7e96616c83a5..44a161b73ea2 100644 --- a/llvm/lib/Target/SPIRV/SPIRVCallLowering.h +++ b/llvm/lib/Target/SPIRV/SPIRVCallLowering.h @@ -25,11 +25,9 @@ class SPIRVCallLowering : public CallLowering { private: // Used to create and assign function, argument, and return type information SPIRVGlobalRegistry *GR; - SPIRVGeneralDuplicatesTracker *DT; public: - SPIRVCallLowering(const SPIRVTargetLowering &TLI, SPIRVGlobalRegistry *GR, - SPIRVGeneralDuplicatesTracker *DT); + SPIRVCallLowering(const SPIRVTargetLowering &TLI, SPIRVGlobalRegistry *GR); // Built OpReturn or OpReturnValue bool lowerReturn(MachineIRBuilder &MIRBuiler, const Value *Val, diff --git a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp index 42333055f965..ecd051c060fc 100644 --- a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp +++ b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp @@ -22,9 +22,8 @@ #include "SPIRVUtils.h" using namespace llvm; -SPIRVGlobalRegistry::SPIRVGlobalRegistry(SPIRVGeneralDuplicatesTracker &DT, - unsigned int PointerSize) - : DT(DT), PointerSize(PointerSize) {} +SPIRVGlobalRegistry::SPIRVGlobalRegistry(unsigned int PointerSize) + : PointerSize(PointerSize) {} SPIRVType * SPIRVGlobalRegistry::assignTypeToVReg(const Type *Type, Register VReg, diff --git a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h index 4a7663b7b459..6dd4ad3211c2 100644 --- a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h +++ b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h @@ -34,7 +34,7 @@ class SPIRVGlobalRegistry { // type-declaring ones) DenseMap> VRegToTypeMap; - SPIRVGeneralDuplicatesTracker &DT; + SPIRVGeneralDuplicatesTracker DT; DenseMap SPIRVToLLVMType; @@ -114,15 +114,42 @@ class SPIRVGlobalRegistry { return InstrsToDelete.contains(MI); } - SPIRVGeneralDuplicatesTracker *getDT() const { return &DT; } + void add(const Constant *C, MachineFunction *MF, Register R) { + DT.add(C, MF, R); + } + + void add(const GlobalValue *GV, MachineFunction *MF, Register R) { + DT.add(GV, MF, R); + } + + void add(const Function *F, MachineFunction *MF, Register R) { + DT.add(F, MF, R); + } + + bool find(const Constant *C, MachineFunction *MF, Register &R) { + return DT.find(C, MF, R); + } + + bool find(const GlobalValue *GV, MachineFunction *MF, Register &R) { + return DT.find(GV, MF, R); + } + + bool find(const Function *F, MachineFunction *MF, Register &R) { + return DT.find(F, MF, R); + } + + template + const MapVector> & + getAllUses() { + return DT.get()->getAllUses(); + } // This interface is for walking the map in GlobalTypesAndRegNumPass. SpecialInstrMapTy &getSpecialTypesAndConstsMap() { return SpecialTypesAndConstsMap; } - SPIRVGlobalRegistry(SPIRVGeneralDuplicatesTracker &DT, - unsigned int PointerSize); + SPIRVGlobalRegistry(unsigned int PointerSize); MachineFunction *CurMF; diff --git a/llvm/lib/Target/SPIRV/SPIRVGlobalTypesAndRegNumPass.cpp b/llvm/lib/Target/SPIRV/SPIRVGlobalTypesAndRegNumPass.cpp index 94cebb7e28e8..5fa29c13880d 100644 --- a/llvm/lib/Target/SPIRV/SPIRVGlobalTypesAndRegNumPass.cpp +++ b/llvm/lib/Target/SPIRV/SPIRVGlobalTypesAndRegNumPass.cpp @@ -197,12 +197,11 @@ static void initMetaBlockBuilder(Module &M, MachineModuleInfo &MMI) { template static void fillLocalAliasTables(SPIRVGlobalRegistry *GR, MetaBlockType MBType) { - const SPIRVDuplicatesTracker *DT = GR->getDT()->get(); setMetaBlock(MBType); MachineRegisterInfo &MRI = getMetaMF()->getRegInfo(); // Make meta registers for entries of DT. - for (auto &CU : DT->getAllUses()) { + for (auto &CU : GR->getAllUses()) { Register MetaReg = MRI.createVirtualRegister(&SPIRV::IDRegClass); for (auto &U : CU.second) { auto *MF = U.first; @@ -266,10 +265,9 @@ static void hoistGlobalOp(Register Reg, MachineFunction *MF, template static void hoistGlobalOps(SPIRVGlobalRegistry *GR, MetaBlockType MBType) { - const SPIRVDuplicatesTracker *DT = GR->getDT()->get(); setMetaBlock(MBType); // Hoist instructions from DT. - for (auto &CU : DT->getAllUses()) { + for (auto &CU : GR->getAllUses()) { for (auto &U : CU.second) { auto *MF = U.first; auto Reg = U.second; @@ -298,11 +296,10 @@ static void hoistGlobalOps(SPIRVGlobalRegistry *GR, MetaBlockType MBType) { // TODO: consider replacing this with explicit OpFunctionParameter generation // here instead handling it in CallLowering. static void hoistGlobalOpsFunction(SPIRVGlobalRegistry *GR) { - const SPIRVDuplicatesTracker *DT = GR->getDT()->get(); setMetaBlock(MB_ExtFuncDecls); MachineRegisterInfo &MRI = getMetaMF()->getRegInfo(); - for (auto &CU : DT->getAllUses()) { + for (auto &CU : GR->getAllUses()) { for (auto &U : CU.second) { auto *MF = U.first; auto Reg = U.second; @@ -743,9 +740,7 @@ static void processGlobalUnrefVars(Module &M, MachineModuleInfo &MMI, const SPIRVSubtarget &ST) { // Walk over each MF's DT and select all unreferenced global variables. SmallVector GlobalVarList; - auto *DT = GR->getDT(); - const SPIRVDuplicatesTracker *GVDT = DT->get(); - auto Map = GVDT->getAllUses(); + auto Map = GR->getAllUses(); for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E;) { GlobalVariable *GV = &*I++; diff --git a/llvm/lib/Target/SPIRV/SPIRVIRTranslator.cpp b/llvm/lib/Target/SPIRV/SPIRVIRTranslator.cpp index 92eb90730657..d7213700eee5 100644 --- a/llvm/lib/Target/SPIRV/SPIRVIRTranslator.cpp +++ b/llvm/lib/Target/SPIRV/SPIRVIRTranslator.cpp @@ -246,8 +246,7 @@ using namespace llvm; // return ResVRegs; // } -static void addConstantsToTrack(MachineFunction &MF, - SPIRVGeneralDuplicatesTracker *DT) { +static void addConstantsToTrack(MachineFunction &MF, SPIRVGlobalRegistry *GR) { auto &MRI = MF.getRegInfo(); DenseMap RegsAlreadyAddedToDT; std::vector ToErase, ToEraseComposites; @@ -262,21 +261,21 @@ static void addConstantsToTrack(MachineFunction &MF, ->getValue()); Register Reg; if (auto *GV = dyn_cast(Const)) { - if (DT->find(GV, &MF, Reg) == false) { - DT->add(GV, &MF, MI.getOperand(2).getReg()); + if (GR->find(GV, &MF, Reg) == false) { + GR->add(GV, &MF, MI.getOperand(2).getReg()); } else RegsAlreadyAddedToDT[&MI] = Reg; } else { - if (DT->find(Const, &MF, Reg) == false) { + if (GR->find(Const, &MF, Reg) == false) { if (auto *ConstVec = dyn_cast(Const)) { auto *BuildVec = MRI.getVRegDef(MI.getOperand(2).getReg()); assert(BuildVec && BuildVec->getOpcode() == TargetOpcode::G_BUILD_VECTOR); for (unsigned i = 0; i < ConstVec->getNumElements(); ++i) - DT->add(ConstVec->getElementAsConstant(i), &MF, + GR->add(ConstVec->getElementAsConstant(i), &MF, BuildVec->getOperand(1 + i).getReg()); } - DT->add(Const, &MF, MI.getOperand(2).getReg()); + GR->add(Const, &MF, MI.getOperand(2).getReg()); } else { RegsAlreadyAddedToDT[&MI] = Reg; // This MI is unused and will be removed. If the MI uses @@ -507,13 +506,12 @@ bool SPIRVIRTranslator::runOnMachineFunction(MachineFunction &MF) { // Initialize the type registry const auto *ST = static_cast(&MF.getSubtarget()); GR = ST->getSPIRVGlobalRegistry(); - DT = ST->getSPIRVDuplicatesTracker(); GR->setCurrentFunc(MF); // Run the regular IRTranslator bool Success = IRTranslator::runOnMachineFunction(MF); - addConstantsToTrack(MF, DT); + addConstantsToTrack(MF, GR); foldConstantsIntoIntrinsics(MF); generateAssignInstrs(MF, GR); diff --git a/llvm/lib/Target/SPIRV/SPIRVIRTranslator.h b/llvm/lib/Target/SPIRV/SPIRVIRTranslator.h index 287263b89070..e574b60134b5 100644 --- a/llvm/lib/Target/SPIRV/SPIRVIRTranslator.h +++ b/llvm/lib/Target/SPIRV/SPIRVIRTranslator.h @@ -23,7 +23,6 @@ namespace llvm { class SPIRVIRTranslator : public IRTranslator { private: SPIRVGlobalRegistry *GR; - SPIRVGeneralDuplicatesTracker *DT; // Generate OpVariables with linkage data and their initializers if necessary // bool buildGlobalValue(Register Reg, const GlobalValue *GV, diff --git a/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp b/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp index a5cc62e17f77..3c68dec41d1d 100644 --- a/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp +++ b/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp @@ -50,7 +50,6 @@ class SPIRVInstructionSelector : public InstructionSelector { const SPIRVRegisterInfo &TRI; const SPIRVRegisterBankInfo &RBI; SPIRVGlobalRegistry &GR; - SPIRVGeneralDuplicatesTracker &DT; public: SPIRVInstructionSelector(const SPIRVTargetMachine &TM, @@ -222,7 +221,6 @@ SPIRVInstructionSelector::SPIRVInstructionSelector( const SPIRVRegisterBankInfo &RBI) : InstructionSelector(), STI(ST), TII(*ST.getInstrInfo()), TRI(*ST.getRegisterInfo()), RBI(RBI), GR(*ST.getSPIRVGlobalRegistry()), - DT(*ST.getSPIRVDuplicatesTracker()), #define GET_GLOBALISEL_PREDICATES_INIT #include "SPIRVGenGlobalISel.inc" #undef GET_GLOBALISEL_PREDICATES_INIT @@ -1115,9 +1113,9 @@ SPIRVInstructionSelector::buildI32Constant(uint32_t Val, // Find a constant in DT or build a new one. auto ConstInt = ConstantInt::get(LLVMTy, Val); Register NewReg; - if (DT.find(ConstInt, &MIRBuilder.getMF(), NewReg) == false) { + if (GR.find(ConstInt, &MIRBuilder.getMF(), NewReg) == false) { NewReg = MRI->createGenericVirtualRegister(LLT::scalar(32)); - DT.add(ConstInt, &MIRBuilder.getMF(), NewReg); + GR.add(ConstInt, &MIRBuilder.getMF(), NewReg); MachineInstr *MI; if (Val == 0) MI = MIRBuilder.buildInstr(SPIRV::OpConstantNull) diff --git a/llvm/lib/Target/SPIRV/SPIRVSubtarget.cpp b/llvm/lib/Target/SPIRV/SPIRVSubtarget.cpp index 89be3df697b9..facfda863b45 100644 --- a/llvm/lib/Target/SPIRV/SPIRVSubtarget.cpp +++ b/llvm/lib/Target/SPIRV/SPIRVSubtarget.cpp @@ -75,10 +75,9 @@ SPIRVSubtarget::SPIRVSubtarget(const Triple &TT, const std::string &CPU, initAvailableExtInstSets(TT); initAvailableCapabilities(TT); - DT.reset(new SPIRVGeneralDuplicatesTracker()); - GR.reset(new SPIRVGlobalRegistry(*DT.get(), PointerSize)); + GR.reset(new SPIRVGlobalRegistry(PointerSize)); - CallLoweringInfo.reset(new SPIRVCallLowering(TLInfo, GR.get(), DT.get())); + CallLoweringInfo.reset(new SPIRVCallLowering(TLInfo, GR.get())); Legalizer.reset(new SPIRVLegalizerInfo(*this)); auto *RBI = new SPIRVRegisterBankInfo(); diff --git a/llvm/lib/Target/SPIRV/SPIRVSubtarget.h b/llvm/lib/Target/SPIRV/SPIRVSubtarget.h index 8b425c5d3a66..04dac2504d30 100644 --- a/llvm/lib/Target/SPIRV/SPIRVSubtarget.h +++ b/llvm/lib/Target/SPIRV/SPIRVSubtarget.h @@ -14,11 +14,11 @@ #define LLVM_LIB_TARGET_SPIRV_SPIRVSUBTARGET_H #include "SPIRVCallLowering.h" -#include "SPIRVGlobalRegistry.h" #include "SPIRVEnums.h" #include "SPIRVExtInsts.h" #include "SPIRVExtensions.h" #include "SPIRVFrameLowering.h" +#include "SPIRVGlobalRegistry.h" #include "SPIRVISelLowering.h" #include "SPIRVInstrInfo.h" #include "SPIRVRegisterBankInfo.h" @@ -57,7 +57,6 @@ class SPIRVSubtarget : public SPIRVGenSubtargetInfo { std::set AvailableExtInstSets; std::set AvailableCaps; - std::unique_ptr DT; std::unique_ptr GR; SPIRVInstrInfo InstrInfo; @@ -109,10 +108,6 @@ class SPIRVSubtarget : public SPIRVGenSubtargetInfo { SPIRVGlobalRegistry *getSPIRVGlobalRegistry() const { return GR.get(); } - SPIRVGeneralDuplicatesTracker *getSPIRVDuplicatesTracker() const { - return DT.get(); - } - const CallLowering *getCallLowering() const override { return CallLoweringInfo.get(); }