LLVM 24.0.0git
NVPTXReplaceImageHandles.cpp
Go to the documentation of this file.
1//===-- NVPTXReplaceImageHandles.cpp - Replace image handles for Fermi ----===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// On Fermi, image handles are not supported. To work around this, we traverse
10// the machine code and replace image handles with concrete symbols. For this
11// to work reliably, inlining of all function call must be performed.
12//
13//===----------------------------------------------------------------------===//
15#include "NVPTX.h"
17#include "NVPTXTargetMachine.h"
18#include "llvm/ADT/DenseSet.h"
22
23using namespace llvm;
24
25namespace {
26class NVPTXReplaceImageHandles {
27 DenseSet<MachineInstr *> InstrsToRemove;
28
29public:
30 bool run(MachineFunction &MF);
31
32private:
33 bool processInstr(MachineInstr &MI);
34 bool replaceImageHandle(MachineOperand &Op, MachineFunction &MF);
35};
36} // namespace
37
38bool NVPTXReplaceImageHandles::run(MachineFunction &MF) {
39 bool Changed = false;
40 InstrsToRemove.clear();
41
42 for (MachineBasicBlock &MBB : MF)
43 for (MachineInstr &MI : MBB)
45
46 // Now clean up any handle-access instructions
47 // This is needed in debug mode when code cleanup passes are not executed,
48 // but we need the handle access to be eliminated because they are not
49 // valid instructions when image handles are disabled.
50 for (MachineInstr *MI : InstrsToRemove) {
51 unsigned DefReg = MI->getOperand(0).getReg();
52 // Only these that are not used can be removed.
53 if (MF.getRegInfo().use_nodbg_empty(DefReg))
54 MI->eraseFromParent();
55 }
56 return Changed;
57}
58
59bool NVPTXReplaceImageHandles::processInstr(MachineInstr &MI) {
60 MachineFunction &MF = *MI.getParent()->getParent();
61 const MCInstrDesc &MCID = MI.getDesc();
62
63 if (MCID.TSFlags & NVPTXII::IsTexFlag) {
64 // This is a texture fetch, so operand 4 is a texref and operand 5 is
65 // a samplerref
66 MachineOperand &TexHandle = MI.getOperand(4);
67 bool Changed = replaceImageHandle(TexHandle, MF);
68
70 MachineOperand &SampHandle = MI.getOperand(5);
71 Changed |= replaceImageHandle(SampHandle, MF);
72 }
73
74 return Changed;
75 }
76 if (MCID.TSFlags & NVPTXII::IsSuldMask) {
77 unsigned VecSize =
79 1);
80
81 // For a surface load of vector size N, the Nth operand will be the surfref
82 MachineOperand &SurfHandle = MI.getOperand(VecSize);
83
84 return replaceImageHandle(SurfHandle, MF);
85 }
86 if (MCID.TSFlags & NVPTXII::IsSustFlag) {
87 // This is a surface store, so operand 0 is a surfref
88 MachineOperand &SurfHandle = MI.getOperand(0);
89
90 return replaceImageHandle(SurfHandle, MF);
91 }
93 // This is a query, so operand 1 is a surfref/texref
94 MachineOperand &Handle = MI.getOperand(1);
95
96 return replaceImageHandle(Handle, MF);
97 }
98
99 return false;
100}
101
102bool NVPTXReplaceImageHandles::replaceImageHandle(MachineOperand &Op,
103 MachineFunction &MF) {
104 const MachineRegisterInfo &MRI = MF.getRegInfo();
105 NVPTXMachineFunctionInfo *MFI = MF.getInfo<NVPTXMachineFunctionInfo>();
106
107 assert(Op.isReg() && "Handle is not in a reg?");
108
109 // Which instruction defines the handle?
110 MachineInstr &TexHandleDef = *MRI.getVRegDef(Op.getReg());
111
112 switch (TexHandleDef.getOpcode()) {
113 case NVPTX::LD_i64: {
114 // The handle is a parameter value being loaded, replace with the
115 // parameter symbol
116 const auto &TM = static_cast<const NVPTXTargetMachine &>(MF.getTarget());
117 if (TM.getDrvInterface() == NVPTX::CUDA)
118 // For CUDA, we preserve the param loads coming from function arguments
119 return false;
120
121 assert(TexHandleDef.getOperand(7).isMCSymbol() && "Load is not a symbol!");
122 MCSymbol *Sym = TexHandleDef.getOperand(7).getMCSymbol();
123 InstrsToRemove.insert(&TexHandleDef);
124 Op.ChangeToMCSymbol(Sym);
125 MFI->addImageHandleSymbol(Sym);
126 return true;
127 }
128 case NVPTX::texsurf_handles: {
129 // The handle is a global variable, replace with the global variable name
130 assert(TexHandleDef.getOperand(1).isGlobal() && "Load is not a global!");
131 const GlobalValue *GV = TexHandleDef.getOperand(1).getGlobal();
132 assert(GV->hasName() && "Global sampler must be named!");
133 InstrsToRemove.insert(&TexHandleDef);
134 Op.ChangeToGA(GV, 0);
135 return true;
136 }
137 case NVPTX::nvvm_move_i64:
138 case TargetOpcode::COPY: {
139 bool Res = replaceImageHandle(TexHandleDef.getOperand(1), MF);
140 if (Res)
141 InstrsToRemove.insert(&TexHandleDef);
142 return Res;
143 }
144 default:
145 llvm_unreachable("Unknown instruction operating on handle");
146 }
147}
148
149namespace {
150class NVPTXReplaceImageHandlesLegacyPass : public MachineFunctionPass {
151public:
152 static char ID;
153 NVPTXReplaceImageHandlesLegacyPass() : MachineFunctionPass(ID) {}
154
155 bool runOnMachineFunction(MachineFunction &MF) override {
156 return NVPTXReplaceImageHandles().run(MF);
157 }
158
159 StringRef getPassName() const override {
160 return "NVPTX Replace Image Handles";
161 }
162
163 void getAnalysisUsage(AnalysisUsage &AU) const override {
164 AU.setPreservesCFG();
166 }
167};
168} // namespace
169
170char NVPTXReplaceImageHandlesLegacyPass::ID = 0;
171
173 return new NVPTXReplaceImageHandlesLegacyPass();
174}
175
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
This file defines the DenseSet and SmallDenseSet classes.
IRTranslator LLVM IR MI
MachineRegisterInfo & MRI
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:280
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
const GlobalValue * getGlobal() const
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
MCSymbol * getMCSymbol() const
LLVM_ABI LLVM_READONLY MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
void addImageHandleSymbol(const MCSymbol *Symbol)
Record that Symbol's handle was replaced with an image reference.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
bool hasName() const
Definition Value.h:263
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
This is an optimization pass for GlobalISel generic memory operations.
MachineFunctionPass * createNVPTXReplaceImageHandlesLegacyPass()
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
void processInstr(MachineInstr &MI, MachineIRBuilder &MIB, MachineRegisterInfo &MRI, SPIRVGlobalRegistry *GR, SPIRVTypeInst KnownResType)
DWARFExpression::Operation Op