31#include "llvm/IR/IntrinsicsNVPTX.h"
45#define DEBUG_TYPE "nvvm-reflect"
47#define NVVM_REFLECT_FUNCTION "__nvvm_reflect"
48#define NVVM_REFLECT_OCL_FUNCTION "__nvvm_reflect_ocl"
50#define CUDA_ARCH_NAME "__CUDA_ARCH"
52#define CUDA_FTZ_NAME "__CUDA_FTZ"
54#define CUDA_FTZ_MODULE_NAME "nvvm-reflect-ftz"
62 void populateReflectMap(
Module &M);
68 explicit NVVMReflect(
unsigned SmVersion)
70 bool runOnModule(
Module &M);
73class NVVMReflectLegacyPass :
public ModulePass {
78 NVVMReflectLegacyPass(
unsigned SmVersion) :
ModulePass(
ID), Impl(SmVersion) {}
79 bool runOnModule(
Module &M)
override;
84 return new NVVMReflectLegacyPass(SmVersion);
89 cl::desc(
"NVVM reflection, enabled by default"));
91char NVVMReflectLegacyPass::ID = 0;
93 "Replace occurrences of __nvvm_reflect() calls with 0/1",
false,
100static
cl::list<
std::
string> ReflectList(
101 "nvvm-reflect-add",
cl::value_desc(
"name=<int>"),
cl::Hidden,
102 cl::desc(
"A key=value pair. Replace __nvvm_reflect(name) with value."),
107void NVVMReflect::populateReflectMap(
Module &M) {
114 auto [Name, Val] = Option.split(
'=');
125 ReflectMap[Name] = ValInt;
132bool NVVMReflect::handleReflectFunction(
Module &M,
StringRef ReflectName) {
136 assert(
F->isDeclaration() &&
"_reflect function should not have a body");
137 assert(
F->getReturnType()->isIntegerTy() &&
138 "_reflect's return type should be integer");
140 const bool Changed = !
F->use_empty();
150 "__nvvm_reflect can only be used in a call instruction");
163 if (!ConstantStr->isCString())
165 "__nvvm_reflect argument must be a null-terminated string");
167 StringRef ReflectArg = ConstantStr->getAsString().drop_back();
168 if (ReflectArg.
empty())
172 unsigned ReflectVal = 0;
173 if (ReflectMap.
contains(ReflectArg))
174 ReflectVal = ReflectMap[ReflectArg];
176 LLVM_DEBUG(
dbgs() <<
"Replacing call of reflect function " <<
F->getName()
177 <<
"(" << ReflectArg <<
") with value " << ReflectVal
179 auto *NewValue = ConstantInt::get(
Call->
getType(), ReflectVal);
180 foldReflectCall(
Call, NewValue);
185 F->eraseFromParent();
189void NVVMReflect::foldReflectCall(CallInst *
Call, Constant *NewValue) {
190 SmallVector<Instruction *, 8> Worklist;
194 for (User *U :
I->users())
197 I->replaceAllUsesWith(
C);
200 ReplaceInstructionWithConst(
Call, NewValue);
203 while (!Worklist.
empty()) {
206 ReplaceInstructionWithConst(
I,
C);
208 I->eraseFromParent();
209 }
else if (
I->isTerminator()) {
215bool NVVMReflect::runOnModule(
Module &M) {
218 populateReflectMap(M);
227bool NVVMReflectLegacyPass::runOnModule(
Module &M) {
228 return Impl.runOnModule(M);
for(const MachineOperand &MO :llvm::drop_begin(OldMI.operands(), Desc.getNumOperands()))
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
Machine Check Debug Module
static cl::opt< std::string > GlobalStr("nvptx-lower-global-ctor-dtor-id", cl::desc("Override unique ID of ctor/dtor globals."), cl::init(""), cl::Hidden)
#define CUDA_FTZ_MODULE_NAME
#define NVVM_REFLECT_OCL_FUNCTION
#define NVVM_REFLECT_FUNCTION
static cl::opt< bool > NVVMReflectEnabled("nvvm-reflect-enable", cl::init(true), cl::Hidden, cl::desc("NVVM reflection, enabled by default"))
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
This file defines the SmallVector class.
Value * getArgOperand(unsigned i) const
This class represents a function call, abstracting a target machine's calling convention.
This is an important base class in LLVM.
LLVM_ABI const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
A Module instance is used to store all the information related to an LLVM module.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
void push_back(const T &Elt)
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
bool contains(StringRef Key) const
contains - Return true if the element is in the map, false otherwise.
Represent a constant reference to a string, i.e.
constexpr bool empty() const
Check if the string is empty.
unsigned getNumOperands() const
Type * getType() const
All values are typed, get the type of this value.
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ C
The default llvm calling convention, compatible with C.
LLVM_ABI StringRef getName(ID id)
Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
This namespace contains all of the command line option processing machinery.
initializer< Ty > init(const Ty &Val)
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract_or_null(Y &&MD)
Extract a Value from Metadata, allowing null.
friend class Instruction
Iterator for Instructions in a `BasicBlock.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI 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...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI Constant * ConstantFoldInstruction(const Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstruction - Try to constant fold the specified instruction.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
LLVM_ABI 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.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
ModulePass * createNVVMReflectPass(unsigned int SmVersion)
bool to_integer(StringRef S, N &Num, unsigned Base=0)
Convert the string S to an integer of the specified type using the radix Base. If Base is 0,...
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Implement std::hash so that hash_code can be used in STL containers.
PreservedAnalyses run(Module &F, ModuleAnalysisManager &AM)