1 //===-- NVPTXReplaceImageHandles.cpp - Replace image handles for Fermi ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // On Fermi, image handles are not supported. To work around this, we traverse
11 // the machine code and replace image handles with concrete symbols. For this
12 // to work reliably, inlining of all function call must be performed.
14 //===----------------------------------------------------------------------===//
17 #include "NVPTXMachineFunctionInfo.h"
18 #include "NVPTXSubtarget.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineFunctionPass.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/ADT/DenseSet.h"
28 class NVPTXReplaceImageHandles : public MachineFunctionPass {
31 DenseSet<MachineInstr *> InstrsToRemove;
34 NVPTXReplaceImageHandles();
36 bool runOnMachineFunction(MachineFunction &MF);
38 const char *getPassName() const override {
39 return "NVPTX Replace Image Handles";
42 bool processInstr(MachineInstr &MI);
43 void replaceImageHandle(MachineOperand &Op, MachineFunction &MF);
44 bool findIndexForHandle(MachineOperand &Op, MachineFunction &MF,
49 char NVPTXReplaceImageHandles::ID = 0;
51 NVPTXReplaceImageHandles::NVPTXReplaceImageHandles()
52 : MachineFunctionPass(ID) {}
54 bool NVPTXReplaceImageHandles::runOnMachineFunction(MachineFunction &MF) {
56 InstrsToRemove.clear();
58 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end(); BI != BE;
60 for (MachineBasicBlock::iterator I = (*BI).begin(), E = (*BI).end();
62 MachineInstr &MI = *I;
63 Changed |= processInstr(MI);
67 // Now clean up any handle-access instructions
68 // This is needed in debug mode when code cleanup passes are not executed,
69 // but we need the handle access to be eliminated because they are not
70 // valid instructions when image handles are disabled.
71 for (DenseSet<MachineInstr *>::iterator I = InstrsToRemove.begin(),
72 E = InstrsToRemove.end(); I != E; ++I) {
73 (*I)->eraseFromParent();
78 bool NVPTXReplaceImageHandles::processInstr(MachineInstr &MI) {
79 MachineFunction &MF = *MI.getParent()->getParent();
80 const MCInstrDesc &MCID = MI.getDesc();
82 if (MCID.TSFlags & NVPTXII::IsTexFlag) {
83 // This is a texture fetch, so operand 4 is a texref and operand 5 is
85 MachineOperand &TexHandle = MI.getOperand(4);
86 replaceImageHandle(TexHandle, MF);
88 if (!(MCID.TSFlags & NVPTXII::IsTexModeUnifiedFlag)) {
89 MachineOperand &SampHandle = MI.getOperand(5);
90 replaceImageHandle(SampHandle, MF);
94 } else if (MCID.TSFlags & NVPTXII::IsSuldMask) {
96 1 << (((MCID.TSFlags & NVPTXII::IsSuldMask) >> NVPTXII::IsSuldShift) - 1);
98 // For a surface load of vector size N, the Nth operand will be the surfref
99 MachineOperand &SurfHandle = MI.getOperand(VecSize);
101 replaceImageHandle(SurfHandle, MF);
104 } else if (MCID.TSFlags & NVPTXII::IsSustFlag) {
105 // This is a surface store, so operand 0 is a surfref
106 MachineOperand &SurfHandle = MI.getOperand(0);
108 replaceImageHandle(SurfHandle, MF);
111 } else if (MCID.TSFlags & NVPTXII::IsSurfTexQueryFlag) {
112 // This is a query, so operand 1 is a surfref/texref
113 MachineOperand &Handle = MI.getOperand(1);
115 replaceImageHandle(Handle, MF);
123 void NVPTXReplaceImageHandles::
124 replaceImageHandle(MachineOperand &Op, MachineFunction &MF) {
126 if (findIndexForHandle(Op, MF, Idx)) {
127 Op.ChangeToImmediate(Idx);
131 bool NVPTXReplaceImageHandles::
132 findIndexForHandle(MachineOperand &Op, MachineFunction &MF, unsigned &Idx) {
133 const MachineRegisterInfo &MRI = MF.getRegInfo();
134 NVPTXMachineFunctionInfo *MFI = MF.getInfo<NVPTXMachineFunctionInfo>();
136 assert(Op.isReg() && "Handle is not in a reg?");
138 // Which instruction defines the handle?
139 MachineInstr &TexHandleDef = *MRI.getVRegDef(Op.getReg());
141 switch (TexHandleDef.getOpcode()) {
142 case NVPTX::LD_i64_avar: {
143 // The handle is a parameter value being loaded, replace with the
145 const NVPTXSubtarget &ST = MF.getTarget().getSubtarget<NVPTXSubtarget>();
146 if (ST.getDrvInterface() == NVPTX::CUDA) {
147 // For CUDA, we preserve the param loads coming from function arguments
151 assert(TexHandleDef.getOperand(6).isSymbol() && "Load is not a symbol!");
152 StringRef Sym = TexHandleDef.getOperand(6).getSymbolName();
153 std::string ParamBaseName = MF.getName();
154 ParamBaseName += "_param_";
155 assert(Sym.startswith(ParamBaseName) && "Invalid symbol reference");
156 unsigned Param = atoi(Sym.data()+ParamBaseName.size());
158 raw_string_ostream NewSymStr(NewSym);
159 NewSymStr << MF.getFunction()->getName() << "_param_" << Param;
161 InstrsToRemove.insert(&TexHandleDef);
162 Idx = MFI->getImageHandleSymbolIndex(NewSymStr.str().c_str());
165 case NVPTX::texsurf_handles: {
166 // The handle is a global variable, replace with the global variable name
167 assert(TexHandleDef.getOperand(1).isGlobal() && "Load is not a global!");
168 const GlobalValue *GV = TexHandleDef.getOperand(1).getGlobal();
169 assert(GV->hasName() && "Global sampler must be named!");
170 InstrsToRemove.insert(&TexHandleDef);
171 Idx = MFI->getImageHandleSymbolIndex(GV->getName().data());
174 case NVPTX::nvvm_move_i64:
175 case TargetOpcode::COPY: {
176 bool Res = findIndexForHandle(TexHandleDef.getOperand(1), MF, Idx);
178 InstrsToRemove.insert(&TexHandleDef);
183 llvm_unreachable("Unknown instruction operating on handle");
187 MachineFunctionPass *llvm::createNVPTXReplaceImageHandlesPass() {
188 return new NVPTXReplaceImageHandles();