xref: /aosp_15_r20/external/llvm/lib/Target/NVPTX/NVPTXReplaceImageHandles.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- NVPTXReplaceImageHandles.cpp - Replace image handles for Fermi ----===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // On Fermi, image handles are not supported. To work around this, we traverse
11*9880d681SAndroid Build Coastguard Worker // the machine code and replace image handles with concrete symbols. For this
12*9880d681SAndroid Build Coastguard Worker // to work reliably, inlining of all function call must be performed.
13*9880d681SAndroid Build Coastguard Worker //
14*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
15*9880d681SAndroid Build Coastguard Worker 
16*9880d681SAndroid Build Coastguard Worker #include "NVPTX.h"
17*9880d681SAndroid Build Coastguard Worker #include "NVPTXMachineFunctionInfo.h"
18*9880d681SAndroid Build Coastguard Worker #include "NVPTXSubtarget.h"
19*9880d681SAndroid Build Coastguard Worker #include "NVPTXTargetMachine.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/DenseSet.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunction.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunctionPass.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
25*9880d681SAndroid Build Coastguard Worker 
26*9880d681SAndroid Build Coastguard Worker using namespace llvm;
27*9880d681SAndroid Build Coastguard Worker 
28*9880d681SAndroid Build Coastguard Worker namespace {
29*9880d681SAndroid Build Coastguard Worker class NVPTXReplaceImageHandles : public MachineFunctionPass {
30*9880d681SAndroid Build Coastguard Worker private:
31*9880d681SAndroid Build Coastguard Worker   static char ID;
32*9880d681SAndroid Build Coastguard Worker   DenseSet<MachineInstr *> InstrsToRemove;
33*9880d681SAndroid Build Coastguard Worker 
34*9880d681SAndroid Build Coastguard Worker public:
35*9880d681SAndroid Build Coastguard Worker   NVPTXReplaceImageHandles();
36*9880d681SAndroid Build Coastguard Worker 
37*9880d681SAndroid Build Coastguard Worker   bool runOnMachineFunction(MachineFunction &MF) override;
38*9880d681SAndroid Build Coastguard Worker 
getPassName() const39*9880d681SAndroid Build Coastguard Worker   const char *getPassName() const override {
40*9880d681SAndroid Build Coastguard Worker     return "NVPTX Replace Image Handles";
41*9880d681SAndroid Build Coastguard Worker   }
42*9880d681SAndroid Build Coastguard Worker private:
43*9880d681SAndroid Build Coastguard Worker   bool processInstr(MachineInstr &MI);
44*9880d681SAndroid Build Coastguard Worker   void replaceImageHandle(MachineOperand &Op, MachineFunction &MF);
45*9880d681SAndroid Build Coastguard Worker   bool findIndexForHandle(MachineOperand &Op, MachineFunction &MF,
46*9880d681SAndroid Build Coastguard Worker                           unsigned &Idx);
47*9880d681SAndroid Build Coastguard Worker };
48*9880d681SAndroid Build Coastguard Worker }
49*9880d681SAndroid Build Coastguard Worker 
50*9880d681SAndroid Build Coastguard Worker char NVPTXReplaceImageHandles::ID = 0;
51*9880d681SAndroid Build Coastguard Worker 
NVPTXReplaceImageHandles()52*9880d681SAndroid Build Coastguard Worker NVPTXReplaceImageHandles::NVPTXReplaceImageHandles()
53*9880d681SAndroid Build Coastguard Worker   : MachineFunctionPass(ID) {}
54*9880d681SAndroid Build Coastguard Worker 
runOnMachineFunction(MachineFunction & MF)55*9880d681SAndroid Build Coastguard Worker bool NVPTXReplaceImageHandles::runOnMachineFunction(MachineFunction &MF) {
56*9880d681SAndroid Build Coastguard Worker   bool Changed = false;
57*9880d681SAndroid Build Coastguard Worker   InstrsToRemove.clear();
58*9880d681SAndroid Build Coastguard Worker 
59*9880d681SAndroid Build Coastguard Worker   for (MachineFunction::iterator BI = MF.begin(), BE = MF.end(); BI != BE;
60*9880d681SAndroid Build Coastguard Worker        ++BI) {
61*9880d681SAndroid Build Coastguard Worker     for (MachineBasicBlock::iterator I = (*BI).begin(), E = (*BI).end();
62*9880d681SAndroid Build Coastguard Worker          I != E; ++I) {
63*9880d681SAndroid Build Coastguard Worker       MachineInstr &MI = *I;
64*9880d681SAndroid Build Coastguard Worker       Changed |= processInstr(MI);
65*9880d681SAndroid Build Coastguard Worker     }
66*9880d681SAndroid Build Coastguard Worker   }
67*9880d681SAndroid Build Coastguard Worker 
68*9880d681SAndroid Build Coastguard Worker   // Now clean up any handle-access instructions
69*9880d681SAndroid Build Coastguard Worker   // This is needed in debug mode when code cleanup passes are not executed,
70*9880d681SAndroid Build Coastguard Worker   // but we need the handle access to be eliminated because they are not
71*9880d681SAndroid Build Coastguard Worker   // valid instructions when image handles are disabled.
72*9880d681SAndroid Build Coastguard Worker   for (DenseSet<MachineInstr *>::iterator I = InstrsToRemove.begin(),
73*9880d681SAndroid Build Coastguard Worker        E = InstrsToRemove.end(); I != E; ++I) {
74*9880d681SAndroid Build Coastguard Worker     (*I)->eraseFromParent();
75*9880d681SAndroid Build Coastguard Worker   }
76*9880d681SAndroid Build Coastguard Worker   return Changed;
77*9880d681SAndroid Build Coastguard Worker }
78*9880d681SAndroid Build Coastguard Worker 
processInstr(MachineInstr & MI)79*9880d681SAndroid Build Coastguard Worker bool NVPTXReplaceImageHandles::processInstr(MachineInstr &MI) {
80*9880d681SAndroid Build Coastguard Worker   MachineFunction &MF = *MI.getParent()->getParent();
81*9880d681SAndroid Build Coastguard Worker   const MCInstrDesc &MCID = MI.getDesc();
82*9880d681SAndroid Build Coastguard Worker 
83*9880d681SAndroid Build Coastguard Worker   if (MCID.TSFlags & NVPTXII::IsTexFlag) {
84*9880d681SAndroid Build Coastguard Worker     // This is a texture fetch, so operand 4 is a texref and operand 5 is
85*9880d681SAndroid Build Coastguard Worker     // a samplerref
86*9880d681SAndroid Build Coastguard Worker     MachineOperand &TexHandle = MI.getOperand(4);
87*9880d681SAndroid Build Coastguard Worker     replaceImageHandle(TexHandle, MF);
88*9880d681SAndroid Build Coastguard Worker 
89*9880d681SAndroid Build Coastguard Worker     if (!(MCID.TSFlags & NVPTXII::IsTexModeUnifiedFlag)) {
90*9880d681SAndroid Build Coastguard Worker       MachineOperand &SampHandle = MI.getOperand(5);
91*9880d681SAndroid Build Coastguard Worker       replaceImageHandle(SampHandle, MF);
92*9880d681SAndroid Build Coastguard Worker     }
93*9880d681SAndroid Build Coastguard Worker 
94*9880d681SAndroid Build Coastguard Worker     return true;
95*9880d681SAndroid Build Coastguard Worker   } else if (MCID.TSFlags & NVPTXII::IsSuldMask) {
96*9880d681SAndroid Build Coastguard Worker     unsigned VecSize =
97*9880d681SAndroid Build Coastguard Worker       1 << (((MCID.TSFlags & NVPTXII::IsSuldMask) >> NVPTXII::IsSuldShift) - 1);
98*9880d681SAndroid Build Coastguard Worker 
99*9880d681SAndroid Build Coastguard Worker     // For a surface load of vector size N, the Nth operand will be the surfref
100*9880d681SAndroid Build Coastguard Worker     MachineOperand &SurfHandle = MI.getOperand(VecSize);
101*9880d681SAndroid Build Coastguard Worker 
102*9880d681SAndroid Build Coastguard Worker     replaceImageHandle(SurfHandle, MF);
103*9880d681SAndroid Build Coastguard Worker 
104*9880d681SAndroid Build Coastguard Worker     return true;
105*9880d681SAndroid Build Coastguard Worker   } else if (MCID.TSFlags & NVPTXII::IsSustFlag) {
106*9880d681SAndroid Build Coastguard Worker     // This is a surface store, so operand 0 is a surfref
107*9880d681SAndroid Build Coastguard Worker     MachineOperand &SurfHandle = MI.getOperand(0);
108*9880d681SAndroid Build Coastguard Worker 
109*9880d681SAndroid Build Coastguard Worker     replaceImageHandle(SurfHandle, MF);
110*9880d681SAndroid Build Coastguard Worker 
111*9880d681SAndroid Build Coastguard Worker     return true;
112*9880d681SAndroid Build Coastguard Worker   } else if (MCID.TSFlags & NVPTXII::IsSurfTexQueryFlag) {
113*9880d681SAndroid Build Coastguard Worker     // This is a query, so operand 1 is a surfref/texref
114*9880d681SAndroid Build Coastguard Worker     MachineOperand &Handle = MI.getOperand(1);
115*9880d681SAndroid Build Coastguard Worker 
116*9880d681SAndroid Build Coastguard Worker     replaceImageHandle(Handle, MF);
117*9880d681SAndroid Build Coastguard Worker 
118*9880d681SAndroid Build Coastguard Worker     return true;
119*9880d681SAndroid Build Coastguard Worker   }
120*9880d681SAndroid Build Coastguard Worker 
121*9880d681SAndroid Build Coastguard Worker   return false;
122*9880d681SAndroid Build Coastguard Worker }
123*9880d681SAndroid Build Coastguard Worker 
124*9880d681SAndroid Build Coastguard Worker void NVPTXReplaceImageHandles::
replaceImageHandle(MachineOperand & Op,MachineFunction & MF)125*9880d681SAndroid Build Coastguard Worker replaceImageHandle(MachineOperand &Op, MachineFunction &MF) {
126*9880d681SAndroid Build Coastguard Worker   unsigned Idx;
127*9880d681SAndroid Build Coastguard Worker   if (findIndexForHandle(Op, MF, Idx)) {
128*9880d681SAndroid Build Coastguard Worker     Op.ChangeToImmediate(Idx);
129*9880d681SAndroid Build Coastguard Worker   }
130*9880d681SAndroid Build Coastguard Worker }
131*9880d681SAndroid Build Coastguard Worker 
132*9880d681SAndroid Build Coastguard Worker bool NVPTXReplaceImageHandles::
findIndexForHandle(MachineOperand & Op,MachineFunction & MF,unsigned & Idx)133*9880d681SAndroid Build Coastguard Worker findIndexForHandle(MachineOperand &Op, MachineFunction &MF, unsigned &Idx) {
134*9880d681SAndroid Build Coastguard Worker   const MachineRegisterInfo &MRI = MF.getRegInfo();
135*9880d681SAndroid Build Coastguard Worker   NVPTXMachineFunctionInfo *MFI = MF.getInfo<NVPTXMachineFunctionInfo>();
136*9880d681SAndroid Build Coastguard Worker 
137*9880d681SAndroid Build Coastguard Worker   assert(Op.isReg() && "Handle is not in a reg?");
138*9880d681SAndroid Build Coastguard Worker 
139*9880d681SAndroid Build Coastguard Worker   // Which instruction defines the handle?
140*9880d681SAndroid Build Coastguard Worker   MachineInstr &TexHandleDef = *MRI.getVRegDef(Op.getReg());
141*9880d681SAndroid Build Coastguard Worker 
142*9880d681SAndroid Build Coastguard Worker   switch (TexHandleDef.getOpcode()) {
143*9880d681SAndroid Build Coastguard Worker   case NVPTX::LD_i64_avar: {
144*9880d681SAndroid Build Coastguard Worker     // The handle is a parameter value being loaded, replace with the
145*9880d681SAndroid Build Coastguard Worker     // parameter symbol
146*9880d681SAndroid Build Coastguard Worker     const NVPTXTargetMachine &TM =
147*9880d681SAndroid Build Coastguard Worker         static_cast<const NVPTXTargetMachine &>(MF.getTarget());
148*9880d681SAndroid Build Coastguard Worker     if (TM.getDrvInterface() == NVPTX::CUDA) {
149*9880d681SAndroid Build Coastguard Worker       // For CUDA, we preserve the param loads coming from function arguments
150*9880d681SAndroid Build Coastguard Worker       return false;
151*9880d681SAndroid Build Coastguard Worker     }
152*9880d681SAndroid Build Coastguard Worker 
153*9880d681SAndroid Build Coastguard Worker     assert(TexHandleDef.getOperand(6).isSymbol() && "Load is not a symbol!");
154*9880d681SAndroid Build Coastguard Worker     StringRef Sym = TexHandleDef.getOperand(6).getSymbolName();
155*9880d681SAndroid Build Coastguard Worker     std::string ParamBaseName = MF.getName();
156*9880d681SAndroid Build Coastguard Worker     ParamBaseName += "_param_";
157*9880d681SAndroid Build Coastguard Worker     assert(Sym.startswith(ParamBaseName) && "Invalid symbol reference");
158*9880d681SAndroid Build Coastguard Worker     unsigned Param = atoi(Sym.data()+ParamBaseName.size());
159*9880d681SAndroid Build Coastguard Worker     std::string NewSym;
160*9880d681SAndroid Build Coastguard Worker     raw_string_ostream NewSymStr(NewSym);
161*9880d681SAndroid Build Coastguard Worker     NewSymStr << MF.getFunction()->getName() << "_param_" << Param;
162*9880d681SAndroid Build Coastguard Worker 
163*9880d681SAndroid Build Coastguard Worker     InstrsToRemove.insert(&TexHandleDef);
164*9880d681SAndroid Build Coastguard Worker     Idx = MFI->getImageHandleSymbolIndex(NewSymStr.str().c_str());
165*9880d681SAndroid Build Coastguard Worker     return true;
166*9880d681SAndroid Build Coastguard Worker   }
167*9880d681SAndroid Build Coastguard Worker   case NVPTX::texsurf_handles: {
168*9880d681SAndroid Build Coastguard Worker     // The handle is a global variable, replace with the global variable name
169*9880d681SAndroid Build Coastguard Worker     assert(TexHandleDef.getOperand(1).isGlobal() && "Load is not a global!");
170*9880d681SAndroid Build Coastguard Worker     const GlobalValue *GV = TexHandleDef.getOperand(1).getGlobal();
171*9880d681SAndroid Build Coastguard Worker     assert(GV->hasName() && "Global sampler must be named!");
172*9880d681SAndroid Build Coastguard Worker     InstrsToRemove.insert(&TexHandleDef);
173*9880d681SAndroid Build Coastguard Worker     Idx = MFI->getImageHandleSymbolIndex(GV->getName().data());
174*9880d681SAndroid Build Coastguard Worker     return true;
175*9880d681SAndroid Build Coastguard Worker   }
176*9880d681SAndroid Build Coastguard Worker   case NVPTX::nvvm_move_i64:
177*9880d681SAndroid Build Coastguard Worker   case TargetOpcode::COPY: {
178*9880d681SAndroid Build Coastguard Worker     bool Res = findIndexForHandle(TexHandleDef.getOperand(1), MF, Idx);
179*9880d681SAndroid Build Coastguard Worker     if (Res) {
180*9880d681SAndroid Build Coastguard Worker       InstrsToRemove.insert(&TexHandleDef);
181*9880d681SAndroid Build Coastguard Worker     }
182*9880d681SAndroid Build Coastguard Worker     return Res;
183*9880d681SAndroid Build Coastguard Worker   }
184*9880d681SAndroid Build Coastguard Worker   default:
185*9880d681SAndroid Build Coastguard Worker     llvm_unreachable("Unknown instruction operating on handle");
186*9880d681SAndroid Build Coastguard Worker   }
187*9880d681SAndroid Build Coastguard Worker }
188*9880d681SAndroid Build Coastguard Worker 
createNVPTXReplaceImageHandlesPass()189*9880d681SAndroid Build Coastguard Worker MachineFunctionPass *llvm::createNVPTXReplaceImageHandlesPass() {
190*9880d681SAndroid Build Coastguard Worker   return new NVPTXReplaceImageHandles();
191*9880d681SAndroid Build Coastguard Worker }
192