@@ -61,14 +61,14 @@ class NVVMReflect {
6161 // Map from reflect function call arguments to the value to replace the call
6262 // with. Should include __CUDA_FTZ and __CUDA_ARCH values.
6363 StringMap<unsigned > ReflectMap;
64- bool handleReflectFunction (Module &M, StringRef ReflectName);
64+ bool handleReflectFunction (Module &M, const StringRef ReflectName);
6565 void populateReflectMap (Module &M);
66- void foldReflectCall (CallInst *Call, Constant *NewValue);
66+ void foldReflectCall (CallInst *const Call, Constant *const NewValue);
6767
6868public:
6969 // __CUDA_FTZ is assigned in `runOnModule` by checking nvvm-reflect-ftz module
7070 // metadata.
71- explicit NVVMReflect (unsigned SmVersion)
71+ explicit NVVMReflect (const unsigned SmVersion)
7272 : ReflectMap({{CUDA_ARCH_NAME, SmVersion * 10 }}) {}
7373 bool runOnModule (Module &M);
7474};
@@ -78,12 +78,12 @@ class NVVMReflectLegacyPass : public ModulePass {
7878
7979public:
8080 static char ID;
81- NVVMReflectLegacyPass (unsigned SmVersion) : ModulePass(ID), Impl(SmVersion) {}
81+ NVVMReflectLegacyPass (const unsigned SmVersion) : ModulePass(ID), Impl(SmVersion) {}
8282 bool runOnModule (Module &M) override ;
8383};
8484} // namespace
8585
86- ModulePass *llvm::createNVVMReflectPass (unsigned SmVersion) {
86+ ModulePass *llvm::createNVVMReflectPass (const unsigned SmVersion) {
8787 LLVM_DEBUG (dbgs () << " Creating NVVMReflectPass with SM version " << SmVersion
8888 << " \n " );
8989 return new NVVMReflectLegacyPass (SmVersion);
@@ -110,65 +110,61 @@ static cl::list<std::string> ReflectList(
110110// Set the VarMap with, first, the value of __CUDA_FTZ from module metadata, and
111111// then the key/value pairs from the command line.
112112void NVVMReflect::populateReflectMap (Module &M) {
113- if (auto *Flag = mdconst::extract_or_null<ConstantInt>(
113+ if (const auto *const Flag = mdconst::extract_or_null<ConstantInt>(
114114 M.getModuleFlag (CUDA_FTZ_MODULE_NAME)))
115115 ReflectMap[CUDA_FTZ_NAME] = Flag->getSExtValue ();
116116
117- for (StringRef Option : ReflectList) {
117+ for (const auto & Option : ReflectList) {
118118 LLVM_DEBUG (dbgs () << " ReflectOption : " << Option << " \n " );
119- auto [Name, Val] = Option.split (' =' );
119+ StringRef OptionRef (Option);
120+ auto [Name, Val] = OptionRef.split (' =' );
120121 if (Name.empty ())
121- report_fatal_error (" Empty name in nvvm-reflect-add option '" + Option +
122- " '" );
122+ report_fatal_error (Twine (" Empty name in nvvm-reflect-add option '" ) + Option + " '" );
123123 if (Val.empty ())
124- report_fatal_error (" Missing value in nvvm-reflect-add option '" + Option +
125- " '" );
124+ report_fatal_error (Twine (" Missing value in nvvm-reflect-add option '" ) + Option + " '" );
126125 unsigned ValInt;
127126 if (!to_integer (Val.trim (), ValInt, 10 ))
128- report_fatal_error (" integer value expected in nvvm-reflect-add option '" +
129- Option + " '" );
127+ report_fatal_error (Twine (" integer value expected in nvvm-reflect-add option '" ) + Option + " '" );
130128 ReflectMap[Name] = ValInt;
131129 }
132130}
133131
134132// / Process a reflect function by finding all its calls and replacing them with
135133// / appropriate constant values. For __CUDA_FTZ, uses the module flag value.
136134// / For __CUDA_ARCH, uses SmVersion * 10. For all other strings, uses 0.
137- bool NVVMReflect::handleReflectFunction (Module &M, StringRef ReflectName) {
138- Function *F = M.getFunction (ReflectName);
135+ bool NVVMReflect::handleReflectFunction (Module &M, const StringRef ReflectName) {
136+ Function *const F = M.getFunction (ReflectName);
139137 if (!F)
140138 return false ;
141139 assert (F->isDeclaration () && " _reflect function should not have a body" );
142140 assert (F->getReturnType ()->isIntegerTy () &&
143141 " _reflect's return type should be integer" );
144142
145- bool Changed = F->getNumUses () > 0 ;
146- for (User *U : make_early_inc_range (F->users ())) {
143+ const bool Changed = F->getNumUses () > 0 ;
144+ for (User *const U : make_early_inc_range (F->users ())) {
147145 // Reflect function calls look like:
148146 // @arch = private unnamed_addr addrspace(1) constant [12 x i8]
149147 // c"__CUDA_ARCH\00" call i32 @__nvvm_reflect(ptr addrspacecast (ptr
150148 // addrspace(1) @arch to ptr)) We need to extract the string argument from
151149 // the call (i.e. "__CUDA_ARCH")
152- CallInst * Call = dyn_cast<CallInst>(U);
150+ auto * const Call = dyn_cast<CallInst>(U);
153151 if (!Call)
154- report_fatal_error (
155- " __nvvm_reflect can only be used in a call instruction" );
152+ report_fatal_error (" __nvvm_reflect can only be used in a call instruction" );
156153 if (Call->getNumOperands () != 2 )
157154 report_fatal_error (" __nvvm_reflect requires exactly one argument" );
158155
159- const Value *GlobalStr = Call->getArgOperand (0 )->stripPointerCasts ();
160- if (!isa<Constant>(GlobalStr))
156+ const auto *const GlobalStr =
157+ dyn_cast<Constant>(Call->getArgOperand (0 )->stripPointerCasts ());
158+ if (!GlobalStr)
161159 report_fatal_error (" __nvvm_reflect argument must be a constant string" );
162160
163- const Value * ConstantStr = cast<Constant >(GlobalStr)-> getOperand ( 0 );
164- if (!isa<ConstantDataSequential>( ConstantStr) )
161+ const auto * const ConstantStr = dyn_cast<ConstantDataSequential >(GlobalStr);
162+ if (!ConstantStr)
165163 report_fatal_error (" __nvvm_reflect argument must be a string constant" );
166- if (!cast<ConstantDataSequential>(ConstantStr)->isCString ())
167- report_fatal_error (
168- " __nvvm_reflect argument must be a null-terminated string" );
164+ if (!ConstantStr->isCString ())
165+ report_fatal_error (" __nvvm_reflect argument must be a null-terminated string" );
169166
170- StringRef ReflectArg =
171- cast<ConstantDataSequential>(ConstantStr)->getAsString ().drop_back ();
167+ const StringRef ReflectArg = ConstantStr->getAsString ().drop_back ();
172168 if (ReflectArg.empty ())
173169 report_fatal_error (" __nvvm_reflect argument cannot be empty" );
174170 // Now that we have extracted the string argument, we can look it up in the
@@ -180,7 +176,7 @@ bool NVVMReflect::handleReflectFunction(Module &M, StringRef ReflectName) {
180176 LLVM_DEBUG (dbgs () << " Replacing call of reflect function " << F->getName ()
181177 << " (" << ReflectArg << " ) with value " << ReflectVal
182178 << " \n " );
183- Constant * NewValue = ConstantInt::get (Call->getType (), ReflectVal);
179+ auto * const NewValue = ConstantInt::get (Call->getType (), ReflectVal);
184180 foldReflectCall (Call, NewValue);
185181 Call->eraseFromParent ();
186182 }
@@ -190,23 +186,23 @@ bool NVVMReflect::handleReflectFunction(Module &M, StringRef ReflectName) {
190186 return Changed;
191187}
192188
193- void NVVMReflect::foldReflectCall (CallInst *Call, Constant *NewValue) {
189+ void NVVMReflect::foldReflectCall (CallInst *const Call, Constant *const NewValue) {
194190 SmallVector<Instruction *, 8 > Worklist;
195191 // Replace an instruction with a constant and add all users of the instruction
196192 // to the worklist
197- auto ReplaceInstructionWithConst = [&](Instruction *I, Constant *C) {
198- for (User * U : I->users ())
199- if (Instruction * UI = dyn_cast<Instruction>(U))
193+ auto ReplaceInstructionWithConst = [&](Instruction *const I, Constant *const C) {
194+ for (auto * const U : I->users ())
195+ if (auto * const UI = dyn_cast<Instruction>(U))
200196 Worklist.push_back (UI);
201197 I->replaceAllUsesWith (C);
202198 };
203199
204200 ReplaceInstructionWithConst (Call, NewValue);
205201
206- auto DL = Call->getModule ()->getDataLayout ();
202+ const auto & DL = Call->getModule ()->getDataLayout ();
207203 while (!Worklist.empty ()) {
208- Instruction * I = Worklist.pop_back_val ();
209- if (Constant * C = ConstantFoldInstruction (I, DL)) {
204+ auto * const I = Worklist.pop_back_val ();
205+ if (auto * const C = ConstantFoldInstruction (I, DL)) {
210206 ReplaceInstructionWithConst (I, C);
211207 if (isInstructionTriviallyDead (I))
212208 I->eraseFromParent ();
0 commit comments