LLVM  11.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"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringMap.h"
24 #include "llvm/IR/Constants.h"
25 #include "llvm/IR/DerivedTypes.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/IR/InstIterator.h"
28 #include "llvm/IR/Instructions.h"
29 #include "llvm/IR/Intrinsics.h"
30 #include "llvm/IR/IntrinsicsNVPTX.h"
31 #include "llvm/IR/Module.h"
32 #include "llvm/IR/Type.h"
33 #include "llvm/Pass.h"
35 #include "llvm/Support/Debug.h"
38 #include "llvm/Transforms/Scalar.h"
39 #include <sstream>
40 #include <string>
41 #define NVVM_REFLECT_FUNCTION "__nvvm_reflect"
42 
43 using namespace llvm;
44 
45 #define DEBUG_TYPE "nvptx-reflect"
46 
47 namespace llvm { void initializeNVVMReflectPass(PassRegistry &); }
48 
49 namespace {
50 class NVVMReflect : public FunctionPass {
51 public:
52  static char ID;
53  unsigned int SmVersion;
54  NVVMReflect() : NVVMReflect(0) {}
55  explicit NVVMReflect(unsigned int Sm) : FunctionPass(ID), SmVersion(Sm) {
57  }
58 
59  bool runOnFunction(Function &) override;
60 };
61 }
62 
63 FunctionPass *llvm::createNVVMReflectPass(unsigned int SmVersion) {
64  return new NVVMReflect(SmVersion);
65 }
66 
67 static cl::opt<bool>
68 NVVMReflectEnabled("nvvm-reflect-enable", cl::init(true), cl::Hidden,
69  cl::desc("NVVM reflection, enabled by default"));
70 
71 char NVVMReflect::ID = 0;
72 INITIALIZE_PASS(NVVMReflect, "nvvm-reflect",
73  "Replace occurrences of __nvvm_reflect() calls with 0/1", false,
74  false)
75 
76 bool NVVMReflect::runOnFunction(Function &F) {
77  if (!NVVMReflectEnabled)
78  return false;
79 
80  if (F.getName() == NVVM_REFLECT_FUNCTION) {
81  assert(F.isDeclaration() && "_reflect function should not have a body");
82  assert(F.getReturnType()->isIntegerTy() &&
83  "_reflect's return type should be integer");
84  return false;
85  }
86 
88 
89  // Go through the calls in this function. Each call to __nvvm_reflect or
90  // llvm.nvvm.reflect should be a CallInst with a ConstantArray argument.
91  // First validate that. If the c-string corresponding to the ConstantArray can
92  // be found successfully, see if it can be found in VarMap. If so, replace the
93  // uses of CallInst with the value found in VarMap. If not, replace the use
94  // with value 0.
95 
96  // The IR for __nvvm_reflect calls differs between CUDA versions.
97  //
98  // CUDA 6.5 and earlier uses this sequence:
99  // %ptr = tail call i8* @llvm.nvvm.ptr.constant.to.gen.p0i8.p4i8
100  // (i8 addrspace(4)* getelementptr inbounds
101  // ([8 x i8], [8 x i8] addrspace(4)* @str, i32 0, i32 0))
102  // %reflect = tail call i32 @__nvvm_reflect(i8* %ptr)
103  //
104  // The value returned by Sym->getOperand(0) is a Constant with a
105  // ConstantDataSequential operand which can be converted to string and used
106  // for lookup.
107  //
108  // CUDA 7.0 does it slightly differently:
109  // %reflect = call i32 @__nvvm_reflect(i8* addrspacecast
110  // (i8 addrspace(1)* getelementptr inbounds
111  // ([8 x i8], [8 x i8] addrspace(1)* @str, i32 0, i32 0) to i8*))
112  //
113  // In this case, we get a Constant with a GlobalVariable operand and we need
114  // to dig deeper to find its initializer with the string we'll use for lookup.
115  for (Instruction &I : instructions(F)) {
116  CallInst *Call = dyn_cast<CallInst>(&I);
117  if (!Call)
118  continue;
119  Function *Callee = Call->getCalledFunction();
120  if (!Callee || (Callee->getName() != NVVM_REFLECT_FUNCTION &&
121  Callee->getIntrinsicID() != Intrinsic::nvvm_reflect))
122  continue;
123 
124  // FIXME: Improve error handling here and elsewhere in this pass.
125  assert(Call->getNumOperands() == 2 &&
126  "Wrong number of operands to __nvvm_reflect function");
127 
128  // In cuda 6.5 and earlier, we will have an extra constant-to-generic
129  // conversion of the string.
130  const Value *Str = Call->getArgOperand(0);
131  if (const CallInst *ConvCall = dyn_cast<CallInst>(Str)) {
132  // FIXME: Add assertions about ConvCall.
133  Str = ConvCall->getArgOperand(0);
134  }
135  assert(isa<ConstantExpr>(Str) &&
136  "Format of __nvvm__reflect function not recognized");
137  const ConstantExpr *GEP = cast<ConstantExpr>(Str);
138 
139  const Value *Sym = GEP->getOperand(0);
140  assert(isa<Constant>(Sym) &&
141  "Format of __nvvm_reflect function not recognized");
142 
143  const Value *Operand = cast<Constant>(Sym)->getOperand(0);
144  if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Operand)) {
145  // For CUDA-7.0 style __nvvm_reflect calls, we need to find the operand's
146  // initializer.
147  assert(GV->hasInitializer() &&
148  "Format of _reflect function not recognized");
149  const Constant *Initializer = GV->getInitializer();
150  Operand = Initializer;
151  }
152 
153  assert(isa<ConstantDataSequential>(Operand) &&
154  "Format of _reflect function not recognized");
155  assert(cast<ConstantDataSequential>(Operand)->isCString() &&
156  "Format of _reflect function not recognized");
157 
158  StringRef ReflectArg = cast<ConstantDataSequential>(Operand)->getAsString();
159  ReflectArg = ReflectArg.substr(0, ReflectArg.size() - 1);
160  LLVM_DEBUG(dbgs() << "Arg of _reflect : " << ReflectArg << "\n");
161 
162  int ReflectVal = 0; // The default value is 0
163  if (ReflectArg == "__CUDA_FTZ") {
164  // Try to pull __CUDA_FTZ from the nvvm-reflect-ftz module flag. Our
165  // choice here must be kept in sync with AutoUpgrade, which uses the same
166  // technique to detect whether ftz is enabled.
167  if (auto *Flag = mdconst::extract_or_null<ConstantInt>(
168  F.getParent()->getModuleFlag("nvvm-reflect-ftz")))
169  ReflectVal = Flag->getSExtValue();
170  } else if (ReflectArg == "__CUDA_ARCH") {
171  ReflectVal = SmVersion * 10;
172  }
173  Call->replaceAllUsesWith(ConstantInt::get(Call->getType(), ReflectVal));
174  ToRemove.push_back(Call);
175  }
176 
177  for (Instruction *I : ToRemove)
178  I->eraseFromParent();
179 
180  return ToRemove.size() > 0;
181 }
LLVM_NODISCARD std::enable_if_t< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type > dyn_cast(const Y &Val)
Definition: Casting.h:334
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
This class represents a function call, abstracting a target machine&#39;s calling convention.
F(f)
Hexagon Common GEP
FunctionPass * createNVVMReflectPass(unsigned int SmVersion)
Definition: NVVMReflect.cpp:63
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1244
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:612
static cl::opt< bool > NVVMReflectEnabled("nvvm-reflect-enable", cl::init(true), cl::Hidden, cl::desc("NVVM reflection, enabled by default"))
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:898
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:244
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:160
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:486
Value * getOperand(unsigned i) const
Definition: User.h:169
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:138
static bool runOnFunction(Function &F, bool PostInlining)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:434
void initializeNVVMReflectPass(PassRegistry &)
This is an important base class in LLVM.
Definition: Constant.h:41
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define NVVM_REFLECT_FUNCTION
Definition: NVVMReflect.cpp:41
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
INITIALIZE_PASS(NVVMReflect, "nvvm-reflect", "Replace occurrences of __nvvm_reflect() calls with 0/1", false, false) bool NVVMReflect
Definition: NVVMReflect.cpp:72
static bool isCString(const Value *Arg)
unsigned getNumOperands() const
Definition: User.h:191
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:883
Module.h This file contains the declarations for the Module class.
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:786
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
Definition: Function.h:195
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
amdgpu Simplify well known AMD library false FunctionCallee Callee
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:270
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation.
Definition: InstrTypes.h:1299
#define I(x, y, z)
Definition: MD5.cpp:59
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:74
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
inst_range instructions(Function *F)
Definition: InstIterator.h:133
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:38
#define LLVM_DEBUG(X)
Definition: Debug.h:122