blob: 82befe4b101b2ab105255c398005f9add4423caf [file] [log] [blame]
Justin Holewinski30d56a72014-04-09 15:39:15 +00001//===-- NVPTXReplaceImageHandles.cpp - Replace image handles for Fermi ----===//
2//
3// The LLVM Compiler Infrastructure
4//
Eli Bendersky8e131f82015-07-08 16:33:21 +00005// This file is distributed under the University of Illinois Open Source
Justin Holewinski30d56a72014-04-09 15:39:15 +00006// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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.
13//
14//===----------------------------------------------------------------------===//
15
16#include "NVPTX.h"
17#include "NVPTXMachineFunctionInfo.h"
Justin Holewinski9a2350e2014-07-17 11:59:04 +000018#include "NVPTXSubtarget.h"
Eric Christopherbeffc4e2015-02-19 00:08:23 +000019#include "NVPTXTargetMachine.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000020#include "llvm/ADT/DenseSet.h"
Justin Holewinski30d56a72014-04-09 15:39:15 +000021#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineFunctionPass.h"
23#include "llvm/CodeGen/MachineRegisterInfo.h"
24#include "llvm/Support/raw_ostream.h"
Justin Holewinski30d56a72014-04-09 15:39:15 +000025
26using namespace llvm;
27
28namespace {
29class NVPTXReplaceImageHandles : public MachineFunctionPass {
30private:
31 static char ID;
32 DenseSet<MachineInstr *> InstrsToRemove;
33
34public:
35 NVPTXReplaceImageHandles();
36
Benjamin Kramer8c90fd72014-09-03 11:41:21 +000037 bool runOnMachineFunction(MachineFunction &MF) override;
Justin Holewinski9a2350e2014-07-17 11:59:04 +000038
Mehdi Amini117296c2016-10-01 02:56:57 +000039 StringRef getPassName() const override {
Justin Holewinski9a2350e2014-07-17 11:59:04 +000040 return "NVPTX Replace Image Handles";
41 }
Justin Holewinski30d56a72014-04-09 15:39:15 +000042private:
43 bool processInstr(MachineInstr &MI);
44 void replaceImageHandle(MachineOperand &Op, MachineFunction &MF);
Justin Holewinski9a2350e2014-07-17 11:59:04 +000045 bool findIndexForHandle(MachineOperand &Op, MachineFunction &MF,
46 unsigned &Idx);
Justin Holewinski30d56a72014-04-09 15:39:15 +000047};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000048}
Justin Holewinski30d56a72014-04-09 15:39:15 +000049
50char NVPTXReplaceImageHandles::ID = 0;
51
52NVPTXReplaceImageHandles::NVPTXReplaceImageHandles()
53 : MachineFunctionPass(ID) {}
54
55bool NVPTXReplaceImageHandles::runOnMachineFunction(MachineFunction &MF) {
56 bool Changed = false;
57 InstrsToRemove.clear();
58
59 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end(); BI != BE;
60 ++BI) {
61 for (MachineBasicBlock::iterator I = (*BI).begin(), E = (*BI).end();
62 I != E; ++I) {
63 MachineInstr &MI = *I;
64 Changed |= processInstr(MI);
65 }
66 }
67
68 // Now clean up any handle-access instructions
69 // This is needed in debug mode when code cleanup passes are not executed,
70 // but we need the handle access to be eliminated because they are not
71 // valid instructions when image handles are disabled.
72 for (DenseSet<MachineInstr *>::iterator I = InstrsToRemove.begin(),
73 E = InstrsToRemove.end(); I != E; ++I) {
74 (*I)->eraseFromParent();
75 }
Justin Holewinski30d56a72014-04-09 15:39:15 +000076 return Changed;
77}
78
79bool NVPTXReplaceImageHandles::processInstr(MachineInstr &MI) {
80 MachineFunction &MF = *MI.getParent()->getParent();
Justin Holewinski9a2350e2014-07-17 11:59:04 +000081 const MCInstrDesc &MCID = MI.getDesc();
82
83 if (MCID.TSFlags & NVPTXII::IsTexFlag) {
Justin Holewinski30d56a72014-04-09 15:39:15 +000084 // This is a texture fetch, so operand 4 is a texref and operand 5 is
85 // a samplerref
86 MachineOperand &TexHandle = MI.getOperand(4);
Justin Holewinski30d56a72014-04-09 15:39:15 +000087 replaceImageHandle(TexHandle, MF);
Justin Holewinski9a2350e2014-07-17 11:59:04 +000088
89 if (!(MCID.TSFlags & NVPTXII::IsTexModeUnifiedFlag)) {
90 MachineOperand &SampHandle = MI.getOperand(5);
91 replaceImageHandle(SampHandle, MF);
92 }
Justin Holewinski30d56a72014-04-09 15:39:15 +000093
94 return true;
Justin Holewinski9a2350e2014-07-17 11:59:04 +000095 } else if (MCID.TSFlags & NVPTXII::IsSuldMask) {
96 unsigned VecSize =
97 1 << (((MCID.TSFlags & NVPTXII::IsSuldMask) >> NVPTXII::IsSuldShift) - 1);
98
99 // For a surface load of vector size N, the Nth operand will be the surfref
100 MachineOperand &SurfHandle = MI.getOperand(VecSize);
Justin Holewinski30d56a72014-04-09 15:39:15 +0000101
102 replaceImageHandle(SurfHandle, MF);
103
104 return true;
Justin Holewinski9a2350e2014-07-17 11:59:04 +0000105 } else if (MCID.TSFlags & NVPTXII::IsSustFlag) {
Justin Holewinski30d56a72014-04-09 15:39:15 +0000106 // This is a surface store, so operand 0 is a surfref
107 MachineOperand &SurfHandle = MI.getOperand(0);
108
109 replaceImageHandle(SurfHandle, MF);
110
111 return true;
Justin Holewinski9a2350e2014-07-17 11:59:04 +0000112 } else if (MCID.TSFlags & NVPTXII::IsSurfTexQueryFlag) {
Justin Holewinski30d56a72014-04-09 15:39:15 +0000113 // This is a query, so operand 1 is a surfref/texref
114 MachineOperand &Handle = MI.getOperand(1);
115
116 replaceImageHandle(Handle, MF);
117
Eli Bendersky8e131f82015-07-08 16:33:21 +0000118 return true;
Justin Holewinski30d56a72014-04-09 15:39:15 +0000119 }
Justin Holewinski9a2350e2014-07-17 11:59:04 +0000120
121 return false;
Justin Holewinski30d56a72014-04-09 15:39:15 +0000122}
123
124void NVPTXReplaceImageHandles::
125replaceImageHandle(MachineOperand &Op, MachineFunction &MF) {
Justin Holewinski9a2350e2014-07-17 11:59:04 +0000126 unsigned Idx;
127 if (findIndexForHandle(Op, MF, Idx)) {
128 Op.ChangeToImmediate(Idx);
129 }
130}
131
132bool NVPTXReplaceImageHandles::
133findIndexForHandle(MachineOperand &Op, MachineFunction &MF, unsigned &Idx) {
Justin Holewinski30d56a72014-04-09 15:39:15 +0000134 const MachineRegisterInfo &MRI = MF.getRegInfo();
135 NVPTXMachineFunctionInfo *MFI = MF.getInfo<NVPTXMachineFunctionInfo>();
Justin Holewinski9a2350e2014-07-17 11:59:04 +0000136
137 assert(Op.isReg() && "Handle is not in a reg?");
138
Justin Holewinski30d56a72014-04-09 15:39:15 +0000139 // Which instruction defines the handle?
Justin Holewinski9a2350e2014-07-17 11:59:04 +0000140 MachineInstr &TexHandleDef = *MRI.getVRegDef(Op.getReg());
Justin Holewinski30d56a72014-04-09 15:39:15 +0000141
142 switch (TexHandleDef.getOpcode()) {
143 case NVPTX::LD_i64_avar: {
144 // The handle is a parameter value being loaded, replace with the
145 // parameter symbol
Eric Christopherbeffc4e2015-02-19 00:08:23 +0000146 const NVPTXTargetMachine &TM =
147 static_cast<const NVPTXTargetMachine &>(MF.getTarget());
148 if (TM.getDrvInterface() == NVPTX::CUDA) {
Justin Holewinski9a2350e2014-07-17 11:59:04 +0000149 // For CUDA, we preserve the param loads coming from function arguments
150 return false;
151 }
152
Justin Holewinski30d56a72014-04-09 15:39:15 +0000153 assert(TexHandleDef.getOperand(6).isSymbol() && "Load is not a symbol!");
154 StringRef Sym = TexHandleDef.getOperand(6).getSymbolName();
155 std::string ParamBaseName = MF.getName();
156 ParamBaseName += "_param_";
157 assert(Sym.startswith(ParamBaseName) && "Invalid symbol reference");
158 unsigned Param = atoi(Sym.data()+ParamBaseName.size());
159 std::string NewSym;
160 raw_string_ostream NewSymStr(NewSym);
Matthias Braunf1caa282017-12-15 22:22:58 +0000161 NewSymStr << MF.getName() << "_param_" << Param;
Justin Holewinski9a2350e2014-07-17 11:59:04 +0000162
Justin Holewinski30d56a72014-04-09 15:39:15 +0000163 InstrsToRemove.insert(&TexHandleDef);
Justin Holewinski9a2350e2014-07-17 11:59:04 +0000164 Idx = MFI->getImageHandleSymbolIndex(NewSymStr.str().c_str());
165 return true;
Justin Holewinski30d56a72014-04-09 15:39:15 +0000166 }
167 case NVPTX::texsurf_handles: {
168 // The handle is a global variable, replace with the global variable name
169 assert(TexHandleDef.getOperand(1).isGlobal() && "Load is not a global!");
170 const GlobalValue *GV = TexHandleDef.getOperand(1).getGlobal();
171 assert(GV->hasName() && "Global sampler must be named!");
Justin Holewinski30d56a72014-04-09 15:39:15 +0000172 InstrsToRemove.insert(&TexHandleDef);
Justin Holewinski9a2350e2014-07-17 11:59:04 +0000173 Idx = MFI->getImageHandleSymbolIndex(GV->getName().data());
174 return true;
175 }
176 case NVPTX::nvvm_move_i64:
177 case TargetOpcode::COPY: {
178 bool Res = findIndexForHandle(TexHandleDef.getOperand(1), MF, Idx);
179 if (Res) {
180 InstrsToRemove.insert(&TexHandleDef);
181 }
182 return Res;
Justin Holewinski30d56a72014-04-09 15:39:15 +0000183 }
184 default:
185 llvm_unreachable("Unknown instruction operating on handle");
186 }
187}
188
189MachineFunctionPass *llvm::createNVPTXReplaceImageHandlesPass() {
190 return new NVPTXReplaceImageHandles();
191}