1249259Sdim//===- NVVMReflect.cpp - NVVM Emulate conditional compilation -------------===//
2249259Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6249259Sdim//
7249259Sdim//===----------------------------------------------------------------------===//
8249259Sdim//
9309124Sdim// This pass replaces occurrences of __nvvm_reflect("foo") and llvm.nvvm.reflect
10309124Sdim// with an integer.
11249259Sdim//
12321369Sdim// We choose the value we use by looking at metadata in the module itself.  Note
13321369Sdim// that we intentionally only have one way to choose these values, because other
14321369Sdim// parts of LLVM (particularly, InstCombineCall) rely on being able to predict
15321369Sdim// the values chosen by this pass.
16309124Sdim//
17309124Sdim// If we see an unknown string, we replace its call with 0.
18309124Sdim//
19249259Sdim//===----------------------------------------------------------------------===//
20249259Sdim
21251662Sdim#include "NVPTX.h"
22249259Sdim#include "llvm/ADT/SmallVector.h"
23249259Sdim#include "llvm/ADT/StringMap.h"
24276479Sdim#include "llvm/IR/Constants.h"
25276479Sdim#include "llvm/IR/DerivedTypes.h"
26249259Sdim#include "llvm/IR/Function.h"
27309124Sdim#include "llvm/IR/InstIterator.h"
28276479Sdim#include "llvm/IR/Instructions.h"
29276479Sdim#include "llvm/IR/Intrinsics.h"
30360784Sdim#include "llvm/IR/IntrinsicsNVPTX.h"
31249259Sdim#include "llvm/IR/Module.h"
32249259Sdim#include "llvm/IR/Type.h"
33276479Sdim#include "llvm/Pass.h"
34249259Sdim#include "llvm/Support/CommandLine.h"
35249259Sdim#include "llvm/Support/Debug.h"
36249259Sdim#include "llvm/Support/raw_os_ostream.h"
37288943Sdim#include "llvm/Support/raw_ostream.h"
38249259Sdim#include "llvm/Transforms/Scalar.h"
39249259Sdim#include <sstream>
40249259Sdim#include <string>
41249259Sdim#define NVVM_REFLECT_FUNCTION "__nvvm_reflect"
42249259Sdim
43249259Sdimusing namespace llvm;
44249259Sdim
45276479Sdim#define DEBUG_TYPE "nvptx-reflect"
46276479Sdim
47249259Sdimnamespace llvm { void initializeNVVMReflectPass(PassRegistry &); }
48249259Sdim
49249259Sdimnamespace {
50309124Sdimclass NVVMReflect : public FunctionPass {
51249259Sdimpublic:
52249259Sdim  static char ID;
53344779Sdim  unsigned int SmVersion;
54344779Sdim  NVVMReflect() : NVVMReflect(0) {}
55344779Sdim  explicit NVVMReflect(unsigned int Sm) : FunctionPass(ID), SmVersion(Sm) {
56251662Sdim    initializeNVVMReflectPass(*PassRegistry::getPassRegistry());
57251662Sdim  }
58251662Sdim
59309124Sdim  bool runOnFunction(Function &) override;
60249259Sdim};
61249259Sdim}
62249259Sdim
63344779SdimFunctionPass *llvm::createNVVMReflectPass(unsigned int SmVersion) {
64344779Sdim  return new NVVMReflect(SmVersion);
65344779Sdim}
66251662Sdim
67249259Sdimstatic cl::opt<bool>
68261991SdimNVVMReflectEnabled("nvvm-reflect-enable", cl::init(true), cl::Hidden,
69249259Sdim                   cl::desc("NVVM reflection, enabled by default"));
70249259Sdim
71249259Sdimchar NVVMReflect::ID = 0;
72249259SdimINITIALIZE_PASS(NVVMReflect, "nvvm-reflect",
73276479Sdim                "Replace occurrences of __nvvm_reflect() calls with 0/1", false,
74249259Sdim                false)
75249259Sdim
76309124Sdimbool NVVMReflect::runOnFunction(Function &F) {
77309124Sdim  if (!NVVMReflectEnabled)
78309124Sdim    return false;
79249259Sdim
80309124Sdim  if (F.getName() == NVVM_REFLECT_FUNCTION) {
81309124Sdim    assert(F.isDeclaration() && "_reflect function should not have a body");
82309124Sdim    assert(F.getReturnType()->isIntegerTy() &&
83309124Sdim           "_reflect's return type should be integer");
84309124Sdim    return false;
85309124Sdim  }
86249259Sdim
87309124Sdim  SmallVector<Instruction *, 4> ToRemove;
88288943Sdim
89309124Sdim  // Go through the calls in this function.  Each call to __nvvm_reflect or
90309124Sdim  // llvm.nvvm.reflect should be a CallInst with a ConstantArray argument.
91309124Sdim  // First validate that. If the c-string corresponding to the ConstantArray can
92309124Sdim  // be found successfully, see if it can be found in VarMap. If so, replace the
93309124Sdim  // uses of CallInst with the value found in VarMap. If not, replace the use
94309124Sdim  // with value 0.
95309124Sdim
96309124Sdim  // The IR for __nvvm_reflect calls differs between CUDA versions.
97309124Sdim  //
98288943Sdim  // CUDA 6.5 and earlier uses this sequence:
99288943Sdim  //    %ptr = tail call i8* @llvm.nvvm.ptr.constant.to.gen.p0i8.p4i8
100288943Sdim  //        (i8 addrspace(4)* getelementptr inbounds
101288943Sdim  //           ([8 x i8], [8 x i8] addrspace(4)* @str, i32 0, i32 0))
102288943Sdim  //    %reflect = tail call i32 @__nvvm_reflect(i8* %ptr)
103288943Sdim  //
104309124Sdim  // The value returned by Sym->getOperand(0) is a Constant with a
105288943Sdim  // ConstantDataSequential operand which can be converted to string and used
106288943Sdim  // for lookup.
107288943Sdim  //
108288943Sdim  // CUDA 7.0 does it slightly differently:
109288943Sdim  //   %reflect = call i32 @__nvvm_reflect(i8* addrspacecast
110288943Sdim  //        (i8 addrspace(1)* getelementptr inbounds
111288943Sdim  //           ([8 x i8], [8 x i8] addrspace(1)* @str, i32 0, i32 0) to i8*))
112288943Sdim  //
113288943Sdim  // In this case, we get a Constant with a GlobalVariable operand and we need
114288943Sdim  // to dig deeper to find its initializer with the string we'll use for lookup.
115309124Sdim  for (Instruction &I : instructions(F)) {
116309124Sdim    CallInst *Call = dyn_cast<CallInst>(&I);
117309124Sdim    if (!Call)
118309124Sdim      continue;
119309124Sdim    Function *Callee = Call->getCalledFunction();
120309124Sdim    if (!Callee || (Callee->getName() != NVVM_REFLECT_FUNCTION &&
121309124Sdim                    Callee->getIntrinsicID() != Intrinsic::nvvm_reflect))
122309124Sdim      continue;
123288943Sdim
124309124Sdim    // FIXME: Improve error handling here and elsewhere in this pass.
125309124Sdim    assert(Call->getNumOperands() == 2 &&
126309124Sdim           "Wrong number of operands to __nvvm_reflect function");
127249259Sdim
128309124Sdim    // In cuda 6.5 and earlier, we will have an extra constant-to-generic
129309124Sdim    // conversion of the string.
130309124Sdim    const Value *Str = Call->getArgOperand(0);
131309124Sdim    if (const CallInst *ConvCall = dyn_cast<CallInst>(Str)) {
132309124Sdim      // FIXME: Add assertions about ConvCall.
133276479Sdim      Str = ConvCall->getArgOperand(0);
134276479Sdim    }
135276479Sdim    assert(isa<ConstantExpr>(Str) &&
136309124Sdim           "Format of __nvvm__reflect function not recognized");
137276479Sdim    const ConstantExpr *GEP = cast<ConstantExpr>(Str);
138249259Sdim
139249259Sdim    const Value *Sym = GEP->getOperand(0);
140309124Sdim    assert(isa<Constant>(Sym) &&
141309124Sdim           "Format of __nvvm_reflect function not recognized");
142249259Sdim
143288943Sdim    const Value *Operand = cast<Constant>(Sym)->getOperand(0);
144288943Sdim    if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Operand)) {
145309124Sdim      // For CUDA-7.0 style __nvvm_reflect calls, we need to find the operand's
146288943Sdim      // initializer.
147288943Sdim      assert(GV->hasInitializer() &&
148288943Sdim             "Format of _reflect function not recognized");
149288943Sdim      const Constant *Initializer = GV->getInitializer();
150288943Sdim      Operand = Initializer;
151288943Sdim    }
152249259Sdim
153288943Sdim    assert(isa<ConstantDataSequential>(Operand) &&
154249259Sdim           "Format of _reflect function not recognized");
155288943Sdim    assert(cast<ConstantDataSequential>(Operand)->isCString() &&
156249259Sdim           "Format of _reflect function not recognized");
157249259Sdim
158309124Sdim    StringRef ReflectArg = cast<ConstantDataSequential>(Operand)->getAsString();
159249259Sdim    ReflectArg = ReflectArg.substr(0, ReflectArg.size() - 1);
160341825Sdim    LLVM_DEBUG(dbgs() << "Arg of _reflect : " << ReflectArg << "\n");
161249259Sdim
162249259Sdim    int ReflectVal = 0; // The default value is 0
163321369Sdim    if (ReflectArg == "__CUDA_FTZ") {
164321369Sdim      // Try to pull __CUDA_FTZ from the nvvm-reflect-ftz module flag.  Our
165321369Sdim      // choice here must be kept in sync with AutoUpgrade, which uses the same
166321369Sdim      // technique to detect whether ftz is enabled.
167309124Sdim      if (auto *Flag = mdconst::extract_or_null<ConstantInt>(
168309124Sdim              F.getParent()->getModuleFlag("nvvm-reflect-ftz")))
169309124Sdim        ReflectVal = Flag->getSExtValue();
170344779Sdim    } else if (ReflectArg == "__CUDA_ARCH") {
171344779Sdim      ReflectVal = SmVersion * 10;
172249259Sdim    }
173309124Sdim    Call->replaceAllUsesWith(ConstantInt::get(Call->getType(), ReflectVal));
174309124Sdim    ToRemove.push_back(Call);
175249259Sdim  }
176249259Sdim
177309124Sdim  for (Instruction *I : ToRemove)
178309124Sdim    I->eraseFromParent();
179276479Sdim
180309124Sdim  return ToRemove.size() > 0;
181276479Sdim}
182