42 #define NVVM_REFLECT_FUNCTION "__nvvm_reflect"
46 #define DEBUG_TYPE "nvptx-reflect"
57 NVVMReflect() : NVVMReflect(
StringMap<int>()) {}
65 bool runOnFunction(
Function &)
override;
74 return new NVVMReflect(Mapping);
79 cl::desc(
"NVVM reflection, enabled by default"));
83 "Replace occurrences of __nvvm_reflect() calls with 0/1",
false,
86 static cl::list<std::
string>
87 ReflectList("nvvm-reflect-list", cl::value_desc("
name=<
int>"), cl::
Hidden,
88 cl::desc("
A list of
string=num assignments"),
97 void NVVMReflect::setVarMap() {
98 for (
unsigned i = 0, e = ReflectList.size();
i != e; ++
i) {
99 DEBUG(
dbgs() <<
"Option : " << ReflectList[
i] <<
"\n");
102 for (
unsigned j = 0, ej = NameValList.
size(); j != ej; ++j) {
104 NameValList[j].split(NameValPair,
'=');
105 assert(NameValPair.
size() == 2 &&
"name=val expected");
106 std::stringstream ValStream(NameValPair[1]);
109 assert((!(ValStream.fail())) &&
"integer value expected");
110 VarMap[NameValPair[0]] = Val;
115 bool NVVMReflect::runOnFunction(
Function &
F) {
122 "_reflect's return type should be integer");
165 "Wrong number of operands to __nvvm_reflect function");
170 if (
const CallInst *ConvCall = dyn_cast<CallInst>(Str)) {
172 Str = ConvCall->getArgOperand(0);
174 assert(isa<ConstantExpr>(Str) &&
175 "Format of __nvvm__reflect function not recognized");
179 assert(isa<Constant>(Sym) &&
180 "Format of __nvvm_reflect function not recognized");
182 const Value *Operand = cast<Constant>(Sym)->getOperand(0);
183 if (
const GlobalVariable *GV = dyn_cast<GlobalVariable>(Operand)) {
186 assert(GV->hasInitializer() &&
187 "Format of _reflect function not recognized");
188 const Constant *Initializer = GV->getInitializer();
189 Operand = Initializer;
192 assert(isa<ConstantDataSequential>(Operand) &&
193 "Format of _reflect function not recognized");
194 assert(cast<ConstantDataSequential>(Operand)->isCString() &&
195 "Format of _reflect function not recognized");
197 StringRef ReflectArg = cast<ConstantDataSequential>(Operand)->getAsString();
198 ReflectArg = ReflectArg.
substr(0, ReflectArg.
size() - 1);
199 DEBUG(
dbgs() <<
"Arg of _reflect : " << ReflectArg <<
"\n");
202 auto Iter = VarMap.find(ReflectArg);
203 if (Iter != VarMap.end())
204 ReflectVal = Iter->second;
205 else if (ReflectArg ==
"__CUDA_FTZ") {
207 if (
auto *
Flag = mdconst::extract_or_null<ConstantInt>(
209 ReflectVal =
Flag->getSExtValue();
216 I->eraseFromParent();
218 return ToRemove.size() > 0;
void push_back(const T &Elt)
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
FunctionPass * createNVVMReflectPass()
unsigned getNumOperands() const
This class represents a function call, abstracting a target machine's calling convention.
Type * getReturnType() const
Returns the type of the ret val.
StringRef getName() const
Return a constant reference to the value's name.
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.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Flag
These should be considered private to the implementation of the MCInstrDesc class.
initializer< Ty > init(const Ty &Val)
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
void initializeNVVMReflectPass(PassRegistry &)
This is an important base class in LLVM.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
#define NVVM_REFLECT_FUNCTION
FunctionPass class - This class is used to implement most global optimizations.
Value * getOperand(unsigned i) const
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Metadata * getModuleFlag(StringRef Key) const
Return the corresponding value if Key appears in module flags, otherwise return null.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
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.
Function * getCalledFunction() const
Return the function called, or null if this is an indirect function invocation.
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th call argument.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
bool isIntegerTy() const
True if this is an instance of IntegerType.
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
StringRef - Represent a constant reference to a string, i.e.
inst_range instructions(Function *F)
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")