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 "NVPTXSubtarget.h"
23#include "llvm/ADT/SmallVector.h"
24#include "llvm/ADT/StringExtras.h"
25#include "llvm/Analysis/ConstantFolding.h"
26#include "llvm/CodeGen/CommandFlags.h"
27#include "llvm/IR/Constants.h"
28#include "llvm/IR/DerivedTypes.h"
29#include "llvm/IR/Function.h"
30#include "llvm/IR/Instructions.h"
31#include "llvm/IR/Intrinsics.h"
32#include "llvm/IR/IntrinsicsNVPTX.h"
33#include "llvm/IR/Module.h"
34#include "llvm/IR/PassManager.h"
35#include "llvm/IR/Type.h"
36#include "llvm/Pass.h"
37#include "llvm/Support/Debug.h"
38#include "llvm/Support/raw_ostream.h"
39#include "llvm/Transforms/Scalar.h"
40#include "llvm/Transforms/Utils/BasicBlockUtils.h"
41#include "llvm/Transforms/Utils/Local.h"
42
43using namespace llvm;
44
45#define DEBUG_TYPE "nvvm-reflect"
46
47#define NVVM_REFLECT_FUNCTION "__nvvm_reflect"
48#define NVVM_REFLECT_OCL_FUNCTION "__nvvm_reflect_ocl"
49// Argument of reflect call to retrive arch number.
50#define CUDA_ARCH_NAME "__CUDA_ARCH"
51// Argument of reflect call to retrive ftz mode.
52#define CUDA_FTZ_NAME "__CUDA_FTZ"
53// Name of module metadata where ftz mode is stored.
54#define CUDA_FTZ_MODULE_NAME "nvvm-reflect-ftz"
55
56namespace {
57class NVVMReflect {
58 const NVPTXOptions &CLOpts;
59 // Map from reflect function call arguments to the value to replace the call
60 // with. Should include __CUDA_FTZ and __CUDA_ARCH values.
61 StringMap<unsigned> ReflectMap;
62 bool handleReflectFunction(Module &M, StringRef ReflectName);
63 void populateReflectMap(Module &M);
64 void foldReflectCall(CallInst *Call, Constant *NewValue);
65
66public:
67 // __CUDA_FTZ is assigned in `runOnModule` by checking nvvm-reflect-ftz module
68 // metadata.
69 NVVMReflect(unsigned SmVersion, const NVPTXOptions &CLOpts)
70 : CLOpts(CLOpts), ReflectMap({{CUDA_ARCH_NAME, SmVersion * 10}}) {}
71 bool runOnModule(Module &M);
72};
73
74class NVVMReflectLegacyPass : public ModulePass {
75 unsigned SmVersion;
76
77public:
78 static char ID;
79 NVVMReflectLegacyPass(unsigned SmVersion)
80 : ModulePass(ID), SmVersion(SmVersion) {}
81 bool runOnModule(Module &M) override;
82};
83} // namespace
84
85ModulePass *llvm::createNVVMReflectPass(unsigned SmVersion) {
86 return new NVVMReflectLegacyPass(SmVersion);
87}
88
89char NVVMReflectLegacyPass::ID = 0;
90INITIALIZE_PASS(NVVMReflectLegacyPass, "nvvm-reflect",
91 "Replace occurrences of __nvvm_reflect() calls with 0/1", false,
92 false)
93
94// Set the ReflectMap with, first, the value of __CUDA_FTZ from module metadata,
95// and then the key/value pairs from the command line.
96void NVVMReflect::populateReflectMap(Module &M) {
97 if (auto *Flag = mdconst::extract_or_null<ConstantInt>(
98 MD: M.getModuleFlag(CUDA_FTZ_MODULE_NAME)))
99 ReflectMap[CUDA_FTZ_NAME] = Flag->getSExtValue();
100
101 for (StringRef Option : CLOpts.nvvm_reflect_add) {
102 LLVM_DEBUG(dbgs() << "ReflectOption : " << Option << "\n");
103 auto [Name, Val] = Option.split(Separator: '=');
104 if (Name.empty())
105 report_fatal_error(reason: "Empty name in nvvm-reflect-add option '" + Option +
106 "'");
107 if (Val.empty())
108 report_fatal_error(reason: "Missing value in nvvm-reflect-add option '" + Option +
109 "'");
110 unsigned ValInt;
111 if (!to_integer(S: Val.trim(), Num&: ValInt, Base: 10))
112 report_fatal_error(reason: "integer value expected in nvvm-reflect-add option '" +
113 Option + "'");
114 ReflectMap[Name] = ValInt;
115 }
116}
117
118/// Process a reflect function by finding all its calls and replacing them with
119/// appropriate constant values. For __CUDA_FTZ, uses the module flag value.
120/// For __CUDA_ARCH, uses SmVersion * 10. For all other strings, uses 0.
121bool NVVMReflect::handleReflectFunction(Module &M, StringRef ReflectName) {
122 Function *F = M.getFunction(Name: ReflectName);
123 if (!F)
124 return false;
125 assert(F->isDeclaration() && "_reflect function should not have a body");
126 assert(F->getReturnType()->isIntegerTy() &&
127 "_reflect's return type should be integer");
128
129 const bool Changed = !F->use_empty();
130 for (User *U : make_early_inc_range(Range: F->users())) {
131 // Reflect function calls look like:
132 // @arch = private unnamed_addr addrspace(1) constant [12 x i8]
133 // c"__CUDA_ARCH\00" call i32 @__nvvm_reflect(ptr addrspacecast (ptr
134 // addrspace(1) @arch to ptr)) We need to extract the string argument from
135 // the call (i.e. "__CUDA_ARCH")
136 auto *Call = dyn_cast<CallInst>(Val: U);
137 if (!Call)
138 report_fatal_error(
139 reason: "__nvvm_reflect can only be used in a call instruction");
140 if (Call->getNumOperands() != 2)
141 report_fatal_error(reason: "__nvvm_reflect requires exactly one argument");
142
143 auto *GlobalStr =
144 dyn_cast<Constant>(Val: Call->getArgOperand(i: 0)->stripPointerCasts());
145 if (!GlobalStr)
146 report_fatal_error(reason: "__nvvm_reflect argument must be a constant string");
147
148 auto *ConstantStr =
149 dyn_cast<ConstantDataSequential>(Val: GlobalStr->getOperand(i: 0));
150 if (!ConstantStr)
151 report_fatal_error(reason: "__nvvm_reflect argument must be a string constant");
152 if (!ConstantStr->isCString())
153 report_fatal_error(
154 reason: "__nvvm_reflect argument must be a null-terminated string");
155
156 StringRef ReflectArg = ConstantStr->getAsString().drop_back();
157 if (ReflectArg.empty())
158 report_fatal_error(reason: "__nvvm_reflect argument cannot be empty");
159 // Now that we have extracted the string argument, we can look it up in the
160 // ReflectMap
161 unsigned ReflectVal = 0; // The default value is 0
162 if (ReflectMap.contains(Key: ReflectArg))
163 ReflectVal = ReflectMap[ReflectArg];
164
165 LLVM_DEBUG(dbgs() << "Replacing call of reflect function " << F->getName()
166 << "(" << ReflectArg << ") with value " << ReflectVal
167 << "\n");
168 auto *NewValue = ConstantInt::get(Ty: Call->getType(), V: ReflectVal);
169 foldReflectCall(Call, NewValue);
170 Call->eraseFromParent();
171 }
172
173 // Remove the __nvvm_reflect function from the module
174 F->eraseFromParent();
175 return Changed;
176}
177
178void NVVMReflect::foldReflectCall(CallInst *Call, Constant *NewValue) {
179 SmallVector<Instruction *, 8> Worklist;
180 // Replace an instruction with a constant and add all users of the instruction
181 // to the worklist
182 auto ReplaceInstructionWithConst = [&](Instruction *I, Constant *C) {
183 for (User *U : I->users())
184 if (auto *UI = dyn_cast<Instruction>(Val: U))
185 Worklist.push_back(Elt: UI);
186 I->replaceAllUsesWith(V: C);
187 };
188
189 ReplaceInstructionWithConst(Call, NewValue);
190
191 auto &DL = Call->getDataLayout();
192 while (!Worklist.empty()) {
193 auto *I = Worklist.pop_back_val();
194 if (Constant *C = ConstantFoldInstruction(I, DL)) {
195 ReplaceInstructionWithConst(I, C);
196 if (isInstructionTriviallyDead(I))
197 I->eraseFromParent();
198 } else if (I->isTerminator()) {
199 ConstantFoldTerminator(BB: I->getParent());
200 }
201 }
202}
203
204bool NVVMReflect::runOnModule(Module &M) {
205 if (!CLOpts.nvvm_reflect_enable)
206 return false;
207 populateReflectMap(M);
208 bool Changed = false;
209 Changed |= handleReflectFunction(M, NVVM_REFLECT_FUNCTION);
210 Changed |= handleReflectFunction(M, NVVM_REFLECT_OCL_FUNCTION);
211 Changed |=
212 handleReflectFunction(M, ReflectName: Intrinsic::getName(id: Intrinsic::nvvm_reflect));
213 return Changed;
214}
215
216bool NVVMReflectLegacyPass::runOnModule(Module &M) {
217 return NVVMReflect(SmVersion, getNVPTXOptions(M)).runOnModule(M);
218}
219
220PreservedAnalyses NVVMReflectPass::run(Module &M, ModuleAnalysisManager &AM) {
221 return NVVMReflect(SmVersion, getNVPTXOptions(M)).runOnModule(M)
222 ? PreservedAnalyses::none()
223 : PreservedAnalyses::all();
224}
225