LLVM  17.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/PassManager.h"
33 #include "llvm/IR/Type.h"
34 #include "llvm/Pass.h"
36 #include "llvm/Support/Debug.h"
39 #include "llvm/Transforms/Scalar.h"
40 #include <sstream>
41 #include <string>
42 #define NVVM_REFLECT_FUNCTION "__nvvm_reflect"
43 #define NVVM_REFLECT_OCL_FUNCTION "__nvvm_reflect_ocl"
44 
45 using namespace llvm;
46 
47 #define DEBUG_TYPE "nvptx-reflect"
48 
49 namespace llvm { void initializeNVVMReflectPass(PassRegistry &); }
50 
51 namespace {
52 class NVVMReflect : public FunctionPass {
53 public:
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 
69 static cl::opt<bool>
70 NVVMReflectEnabled("nvvm-reflect-enable", cl::init(true), cl::Hidden,
71  cl::desc("NVVM reflection, enabled by default"));
72 
73 char NVVMReflect::ID = 0;
74 INITIALIZE_PASS(NVVMReflect, "nvvm-reflect",
75  "Replace occurrences of __nvvm_reflect() calls with 0/1", false,
76  false)
77 
78 static bool runNVVMReflect(Function &F, unsigned SmVersion) {
79  if (!NVVMReflectEnabled)
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 
91 
92  // Go through the calls in this function. Each call to __nvvm_reflect or
93  // llvm.nvvm.reflect should be a CallInst with a ConstantArray argument.
94  // First validate that. If the c-string corresponding to the ConstantArray can
95  // be found successfully, see if it can be found in VarMap. If so, replace the
96  // uses of CallInst with the value found in VarMap. If not, replace the use
97  // with value 0.
98 
99  // The IR for __nvvm_reflect calls differs between CUDA versions.
100  //
101  // CUDA 6.5 and earlier uses this sequence:
102  // %ptr = tail call i8* @llvm.nvvm.ptr.constant.to.gen.p0i8.p4i8
103  // (i8 addrspace(4)* getelementptr inbounds
104  // ([8 x i8], [8 x i8] addrspace(4)* @str, i32 0, i32 0))
105  // %reflect = tail call i32 @__nvvm_reflect(i8* %ptr)
106  //
107  // The value returned by Sym->getOperand(0) is a Constant with a
108  // ConstantDataSequential operand which can be converted to string and used
109  // for lookup.
110  //
111  // CUDA 7.0 does it slightly differently:
112  // %reflect = call i32 @__nvvm_reflect(i8* addrspacecast
113  // (i8 addrspace(1)* getelementptr inbounds
114  // ([8 x i8], [8 x i8] addrspace(1)* @str, i32 0, i32 0) to i8*))
115  //
116  // In this case, we get a Constant with a GlobalVariable operand and we need
117  // to dig deeper to find its initializer with the string we'll use for lookup.
119  CallInst *Call = dyn_cast<CallInst>(&I);
120  if (!Call)
121  continue;
122  Function *Callee = Call->getCalledFunction();
123  if (!Callee || (Callee->getName() != NVVM_REFLECT_FUNCTION &&
124  Callee->getName() != NVVM_REFLECT_OCL_FUNCTION &&
125  Callee->getIntrinsicID() != Intrinsic::nvvm_reflect))
126  continue;
127 
128  // FIXME: Improve error handling here and elsewhere in this pass.
129  assert(Call->getNumOperands() == 2 &&
130  "Wrong number of operands to __nvvm_reflect function");
131 
132  // In cuda 6.5 and earlier, we will have an extra constant-to-generic
133  // conversion of the string.
134  const Value *Str = Call->getArgOperand(0);
135  if (const CallInst *ConvCall = dyn_cast<CallInst>(Str)) {
136  // FIXME: Add assertions about ConvCall.
137  Str = ConvCall->getArgOperand(0);
138  }
139  // Pre opaque pointers we have a constant expression wrapping the constant
140  // string.
141  Str = Str->stripPointerCasts();
142  assert(isa<Constant>(Str) &&
143  "Format of __nvvm_reflect function not recognized");
144 
145  const Value *Operand = cast<Constant>(Str)->getOperand(0);
146  if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Operand)) {
147  // For CUDA-7.0 style __nvvm_reflect calls, we need to find the operand's
148  // initializer.
149  assert(GV->hasInitializer() &&
150  "Format of _reflect function not recognized");
151  const Constant *Initializer = GV->getInitializer();
152  Operand = Initializer;
153  }
154 
155  assert(isa<ConstantDataSequential>(Operand) &&
156  "Format of _reflect function not recognized");
157  assert(cast<ConstantDataSequential>(Operand)->isCString() &&
158  "Format of _reflect function not recognized");
159 
160  StringRef ReflectArg = cast<ConstantDataSequential>(Operand)->getAsString();
161  ReflectArg = ReflectArg.substr(0, ReflectArg.size() - 1);
162  LLVM_DEBUG(dbgs() << "Arg of _reflect : " << ReflectArg << "\n");
163 
164  int ReflectVal = 0; // The default value is 0
165  if (ReflectArg == "__CUDA_FTZ") {
166  // Try to pull __CUDA_FTZ from the nvvm-reflect-ftz module flag. Our
167  // choice here must be kept in sync with AutoUpgrade, which uses the same
168  // technique to detect whether ftz is enabled.
169  if (auto *Flag = mdconst::extract_or_null<ConstantInt>(
170  F.getParent()->getModuleFlag("nvvm-reflect-ftz")))
171  ReflectVal = Flag->getSExtValue();
172  } else if (ReflectArg == "__CUDA_ARCH") {
173  ReflectVal = SmVersion * 10;
174  }
175  Call->replaceAllUsesWith(ConstantInt::get(Call->getType(), ReflectVal));
176  ToRemove.push_back(Call);
177  }
178 
179  for (Instruction *I : ToRemove)
180  I->eraseFromParent();
181 
182  return ToRemove.size() > 0;
183 }
184 
186  return runNVVMReflect(F, SmVersion);
187 }
188 
190 
193  return runNVVMReflect(F, SmVersion) ? PreservedAnalyses::none()
195 }
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::AArch64PACKey::ID
ID
Definition: AArch64BaseInfo.h:824
Scalar.h
InstIterator.h
llvm::Function
Definition: Function.h:59
Pass.h
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1199
llvm::GlobalVariable
Definition: GlobalVariable.h:39
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:138
llvm::PreservedAnalyses::none
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:155
Module.h
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
F
#define F(x, y, z)
Definition: MD5.cpp:55
NVPTX.h
NVVM_REFLECT_FUNCTION
#define NVVM_REFLECT_FUNCTION
Definition: NVVMReflect.cpp:42
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
CommandLine.h
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:24
Constants.h
Intrinsics.h
llvm::StringRef::substr
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:559
llvm::Instruction
Definition: Instruction.h:41
llvm::MCID::Flag
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:148
llvm::PassRegistry
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:38
llvm::ConstantInt::get
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:887
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
Type.h
llvm::cl::opt< bool >
NVVMReflectEnabled
static cl::opt< bool > NVVMReflectEnabled("nvvm-reflect-enable", cl::init(true), cl::Hidden, cl::desc("NVVM reflection, enabled by default"))
llvm::instructions
inst_range instructions(Function *F)
Definition: InstIterator.h:133
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::createNVVMReflectPass
FunctionPass * createNVVMReflectPass(unsigned int SmVersion)
Definition: NVVMReflect.cpp:65
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:445
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::initializeNVVMReflectPass
void initializeNVVMReflectPass(PassRegistry &)
SmVersion
unsigned SmVersion
Definition: NVVMReflect.cpp:78
Callee
amdgpu Simplify well known AMD library false FunctionCallee Callee
Definition: AMDGPULibCalls.cpp:187
llvm::StringRef::size
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
runOnFunction
static bool runOnFunction(Function &F, bool PostInlining)
Definition: EntryExitInstrumenter.cpp:85
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
llvm::NVVMReflectPass
Definition: NVPTX.h:61
ToRemove
SmallVector< Instruction *, 4 > ToRemove
Definition: NVVMReflect.cpp:90
Function.h
PassManager.h
llvm::NVVMReflectPass::NVVMReflectPass
NVVMReflectPass()
Definition: NVVMReflect.cpp:189
Instructions.h
raw_os_ostream.h
SmallVector.h
INITIALIZE_PASS
INITIALIZE_PASS(NVVMReflect, "nvvm-reflect", "Replace occurrences of __nvvm_reflect() calls with 0/1", false, false) static bool runNVVMReflect(Function &F
DerivedTypes.h
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:42
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:308
llvm::CallInst
This class represents a function call, abstracting a target machine's calling convention.
Definition: Instructions.h:1485
StringMap.h
llvm::cl::desc
Definition: CommandLine.h:411
raw_ostream.h
llvm::NVVMReflectPass::run
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: NVVMReflect.cpp:191
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
Debug.h
NVVM_REFLECT_OCL_FUNCTION
#define NVVM_REFLECT_OCL_FUNCTION
Definition: NVVMReflect.cpp:43