@@ -58,7 +58,6 @@ void initializeNVVMReflectLegacyPassPass(PassRegistry &);
5858
5959namespace {
6060class NVVMReflect {
61- private:
6261 // Map from reflect function call arguments to the value to replace the call
6362 // with. Should include __CUDA_FTZ and __CUDA_ARCH values.
6463 StringMap<int > ReflectMap;
@@ -73,11 +72,8 @@ class NVVMReflect {
7372 : ReflectMap({{CUDA_ARCH_NAME, SmVersion * 10 }}) {}
7473 bool runOnModule (Module &M);
7574};
76- } // namespace
7775
78- namespace {
7976class NVVMReflectLegacyPass : public ModulePass {
80- private:
8177 NVVMReflect Impl;
8278
8379public:
@@ -122,15 +118,15 @@ void NVVMReflect::populateReflectMap(Module &M) {
122118 LLVM_DEBUG (dbgs () << " ReflectOption : " << Option << " \n " );
123119 auto [Name, Val] = Option.split (' =' );
124120 if (Name.empty ())
125- report_fatal_error (" Empty name in nvvm-reflect-list option '" + Option +
121+ report_fatal_error (" Empty name in nvvm-reflect-add option '" + Option +
126122 " '" );
127123 if (Val.empty ())
128- report_fatal_error (" Missing value in nvvm-reflect- option '" + Option +
124+ report_fatal_error (" Missing value in nvvm-reflect-add option '" + Option +
129125 " '" );
130126 int ValInt;
131127 if (!to_integer (Val.trim (), ValInt, 10 ))
132128 report_fatal_error (
133- " integer value expected in nvvm-reflect-list option '" + Option +
129+ " integer value expected in nvvm-reflect-add option '" + Option +
134130 " '" );
135131 ReflectMap[Name] = ValInt;
136132 }
@@ -154,10 +150,10 @@ bool NVVMReflect::handleReflectFunction(Module &M, StringRef ReflectName) {
154150 // c"__CUDA_ARCH\00" call i32 @__nvvm_reflect(ptr addrspacecast (ptr
155151 // addrspace(1) @arch to ptr)) We need to extract the string argument from
156152 // the call (i.e. "__CUDA_ARCH")
157- if (!isa<CallInst>(U))
153+ CallInst *Call = dyn_cast<CallInst>(U);
154+ if (!Call)
158155 report_fatal_error (
159156 " __nvvm_reflect can only be used in a call instruction" );
160- CallInst *Call = cast<CallInst>(U);
161157 if (Call->getNumOperands () != 2 )
162158 report_fatal_error (" __nvvm_reflect requires exactly one argument" );
163159
@@ -173,9 +169,7 @@ bool NVVMReflect::handleReflectFunction(Module &M, StringRef ReflectName) {
173169 " __nvvm_reflect argument must be a null-terminated string" );
174170
175171 StringRef ReflectArg =
176- cast<ConstantDataSequential>(ConstantStr)->getAsString ();
177- // Remove the null terminator from the string
178- ReflectArg = ReflectArg.substr (0 , ReflectArg.size () - 1 );
172+ cast<ConstantDataSequential>(ConstantStr)->getAsString ().drop_back ();
179173 if (ReflectArg.empty ())
180174 report_fatal_error (" __nvvm_reflect argument cannot be empty" );
181175 // Now that we have extracted the string argument, we can look it up in the
@@ -202,19 +196,18 @@ void NVVMReflect::foldReflectCall(CallInst *Call, Constant *NewValue) {
202196 // Replace an instruction with a constant and add all users of the instruction
203197 // to the worklist
204198 auto ReplaceInstructionWithConst = [&](Instruction *I, Constant *C) {
205- for (User *U : I->users ()) {
199+ for (User *U : I->users ())
206200 if (Instruction *UI = dyn_cast<Instruction>(U))
207201 Worklist.push_back (UI);
208- }
209202 I->replaceAllUsesWith (C);
210203 };
211204
212205 ReplaceInstructionWithConst (Call, NewValue);
213206
207+ auto DL = Call->getModule ()->getDataLayout ();
214208 while (!Worklist.empty ()) {
215209 Instruction *I = Worklist.pop_back_val ();
216- if (Constant *C =
217- ConstantFoldInstruction (I, Call->getModule ()->getDataLayout ())) {
210+ if (Constant *C = ConstantFoldInstruction (I, DL)) {
218211 ReplaceInstructionWithConst (I, C);
219212 if (isInstructionTriviallyDead (I))
220213 I->eraseFromParent ();
0 commit comments