LLVM 20.0.0git
NVVMReflect.cpp
Go to the documentation of this file.
1//===- NVVMReflect.cpp - NVVM Emulate conditional compilation -------------===//
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// This pass replaces occurrences of __nvvm_reflect("foo") and llvm.nvvm.reflect
10// with an integer.
11//
12// We choose the value we use by looking at metadata in the module itself. Note
13// that we intentionally only have one way to choose these values, because other
14// parts of LLVM (particularly, InstCombineCall) rely on being able to predict
15// the values chosen by this pass.
16//
17// If we see an unknown string, we replace its call with 0.
18//
19//===----------------------------------------------------------------------===//
20
21#include "NVPTX.h"
24#include "llvm/IR/Constants.h"
26#include "llvm/IR/Function.h"
29#include "llvm/IR/Intrinsics.h"
30#include "llvm/IR/IntrinsicsNVPTX.h"
31#include "llvm/IR/Module.h"
32#include "llvm/IR/PassManager.h"
33#include "llvm/IR/Type.h"
34#include "llvm/Pass.h"
36#include "llvm/Support/Debug.h"
41#include <algorithm>
42#define NVVM_REFLECT_FUNCTION "__nvvm_reflect"
43#define NVVM_REFLECT_OCL_FUNCTION "__nvvm_reflect_ocl"
44
45using namespace llvm;
46
47#define DEBUG_TYPE "nvptx-reflect"
48
49namespace llvm { void initializeNVVMReflectPass(PassRegistry &); }
50
51namespace {
52class NVVMReflect : public FunctionPass {
53public:
54 static char ID;
55 unsigned int SmVersion;
56 NVVMReflect() : NVVMReflect(0) {}
57 explicit NVVMReflect(unsigned int Sm) : FunctionPass(ID), SmVersion(Sm) {
59 }
60
61 bool runOnFunction(Function &) override;
62};
63}
64
66 return new NVVMReflect(SmVersion);
67}
68
69static cl::opt<bool>
70NVVMReflectEnabled("nvvm-reflect-enable", cl::init(true), cl::Hidden,
71 cl::desc("NVVM reflection, enabled by default"));
72
73char NVVMReflect::ID = 0;
74INITIALIZE_PASS(NVVMReflect, "nvvm-reflect",
75 "Replace occurrences of __nvvm_reflect() calls with 0/1", false,
76 false)
77
78static bool runNVVMReflect(Function &F, unsigned SmVersion) {
80 return false;
81
82 if (F.getName() == NVVM_REFLECT_FUNCTION ||
83 F.getName() == NVVM_REFLECT_OCL_FUNCTION) {
84 assert(F.isDeclaration() && "_reflect function should not have a body");
85 assert(F.getReturnType()->isIntegerTy() &&
86 "_reflect's return type should be integer");
87 return false;
88 }
89
92
93 // Go through the calls in this function. Each call to __nvvm_reflect or
94 // llvm.nvvm.reflect should be a CallInst with a ConstantArray argument.
95 // First validate that. If the c-string corresponding to the ConstantArray can
96 // be found successfully, see if it can be found in VarMap. If so, replace the
97 // uses of CallInst with the value found in VarMap. If not, replace the use
98 // with value 0.
99
100 // The IR for __nvvm_reflect calls differs between CUDA versions.
101 //
102 // CUDA 6.5 and earlier uses this sequence:
103 // %ptr = tail call i8* @llvm.nvvm.ptr.constant.to.gen.p0i8.p4i8
104 // (i8 addrspace(4)* getelementptr inbounds
105 // ([8 x i8], [8 x i8] addrspace(4)* @str, i32 0, i32 0))
106 // %reflect = tail call i32 @__nvvm_reflect(i8* %ptr)
107 //
108 // The value returned by Sym->getOperand(0) is a Constant with a
109 // ConstantDataSequential operand which can be converted to string and used
110 // for lookup.
111 //
112 // CUDA 7.0 does it slightly differently:
113 // %reflect = call i32 @__nvvm_reflect(i8* addrspacecast
114 // (i8 addrspace(1)* getelementptr inbounds
115 // ([8 x i8], [8 x i8] addrspace(1)* @str, i32 0, i32 0) to i8*))
116 //
117 // In this case, we get a Constant with a GlobalVariable operand and we need
118 // to dig deeper to find its initializer with the string we'll use for lookup.
120 CallInst *Call = dyn_cast<CallInst>(&I);
121 if (!Call)
122 continue;
123 Function *Callee = Call->getCalledFunction();
124 if (!Callee || (Callee->getName() != NVVM_REFLECT_FUNCTION &&
125 Callee->getName() != NVVM_REFLECT_OCL_FUNCTION &&
126 Callee->getIntrinsicID() != Intrinsic::nvvm_reflect))
127 continue;
128
129 // FIXME: Improve error handling here and elsewhere in this pass.
130 assert(Call->getNumOperands() == 2 &&
131 "Wrong number of operands to __nvvm_reflect function");
132
133 // In cuda 6.5 and earlier, we will have an extra constant-to-generic
134 // conversion of the string.
135 const Value *Str = Call->getArgOperand(0);
136 if (const CallInst *ConvCall = dyn_cast<CallInst>(Str)) {
137 // FIXME: Add assertions about ConvCall.
138 Str = ConvCall->getArgOperand(0);
139 }
140 // Pre opaque pointers we have a constant expression wrapping the constant
141 // string.
142 Str = Str->stripPointerCasts();
143 assert(isa<Constant>(Str) &&
144 "Format of __nvvm_reflect function not recognized");
145
146 const Value *Operand = cast<Constant>(Str)->getOperand(0);
147 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Operand)) {
148 // For CUDA-7.0 style __nvvm_reflect calls, we need to find the operand's
149 // initializer.
150 assert(GV->hasInitializer() &&
151 "Format of _reflect function not recognized");
152 const Constant *Initializer = GV->getInitializer();
153 Operand = Initializer;
154 }
155
156 assert(isa<ConstantDataSequential>(Operand) &&
157 "Format of _reflect function not recognized");
158 assert(cast<ConstantDataSequential>(Operand)->isCString() &&
159 "Format of _reflect function not recognized");
160
161 StringRef ReflectArg = cast<ConstantDataSequential>(Operand)->getAsString();
162 ReflectArg = ReflectArg.substr(0, ReflectArg.size() - 1);
163 LLVM_DEBUG(dbgs() << "Arg of _reflect : " << ReflectArg << "\n");
164
165 int ReflectVal = 0; // The default value is 0
166 if (ReflectArg == "__CUDA_FTZ") {
167 // Try to pull __CUDA_FTZ from the nvvm-reflect-ftz module flag. Our
168 // choice here must be kept in sync with AutoUpgrade, which uses the same
169 // technique to detect whether ftz is enabled.
170 if (auto *Flag = mdconst::extract_or_null<ConstantInt>(
171 F.getParent()->getModuleFlag("nvvm-reflect-ftz")))
172 ReflectVal = Flag->getSExtValue();
173 } else if (ReflectArg == "__CUDA_ARCH") {
174 ReflectVal = SmVersion * 10;
175 }
176
177 // If the immediate user is a simple comparison we want to simplify it.
178 for (User *U : Call->users())
179 if (Instruction *I = dyn_cast<Instruction>(U))
180 ToSimplify.push_back(I);
181
182 Call->replaceAllUsesWith(ConstantInt::get(Call->getType(), ReflectVal));
183 ToRemove.push_back(Call);
184 }
185
186 // The code guarded by __nvvm_reflect may be invalid for the target machine.
187 // Traverse the use-def chain, continually simplifying constant expressions
188 // until we find a terminator that we can then remove.
189 while (!ToSimplify.empty()) {
190 Instruction *I = ToSimplify.pop_back_val();
191 if (Constant *C =
192 ConstantFoldInstruction(I, F.getDataLayout())) {
193 for (User *U : I->users())
194 if (Instruction *I = dyn_cast<Instruction>(U))
195 ToSimplify.push_back(I);
196
197 I->replaceAllUsesWith(C);
199 ToRemove.push_back(I);
200 }
201 } else if (I->isTerminator()) {
202 ConstantFoldTerminator(I->getParent());
203 }
204 }
205
206 // Removing via isInstructionTriviallyDead may add duplicates to the ToRemove
207 // array. Filter out the duplicates before starting to erase from parent.
208 std::sort(ToRemove.begin(), ToRemove.end());
211
213 I->eraseFromParent();
214
215 return ToRemove.size() > 0;
216}
217
218bool NVVMReflect::runOnFunction(Function &F) {
219 return runNVVMReflect(F, SmVersion);
220}
221
223
226 return runNVVMReflect(F, SmVersion) ? PreservedAnalyses::none()
228}
Expand Atomic instructions
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define LLVM_DEBUG(...)
Definition: Debug.h:106
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
auto NewLastIter
#define NVVM_REFLECT_OCL_FUNCTION
Definition: NVVMReflect.cpp:43
SmallVector< Instruction *, 4 > ToSimplify
Definition: NVVMReflect.cpp:91
#define NVVM_REFLECT_FUNCTION
Definition: NVVMReflect.cpp:42
unsigned SmVersion
Definition: NVVMReflect.cpp:78
static cl::opt< bool > NVVMReflectEnabled("nvvm-reflect-enable", cl::init(true), cl::Hidden, cl::desc("NVVM reflection, enabled by default"))
SmallVector< Instruction *, 4 > ToRemove
Definition: NVVMReflect.cpp:90
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
This class represents a function call, abstracting a target machine's calling convention.
This is an important base class in LLVM.
Definition: Constant.h:42
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:37
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: Analysis.h:114
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:571
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:150
LLVM Value Representation.
Definition: Value.h:74
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions=false, const TargetLibraryInfo *TLI=nullptr, DomTreeUpdater *DTU=nullptr)
If a terminator instruction is predicated on a constant value, convert it into an unconditional branc...
Definition: Local.cpp:136
FunctionPass * createNVVMReflectPass(unsigned int SmVersion)
Definition: NVVMReflect.cpp:65
auto unique(Range &&R, Predicate P)
Definition: STLExtras.h:2055
void initializeNVVMReflectPass(PassRegistry &)
bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction will return.
Definition: Local.cpp:406
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
Constant * ConstantFoldInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstruction - Try to constant fold the specified instruction.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)