Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/NVPTX/NVVMReflect.cpp
Line
Count
Source (jump to first uncovered line)
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 "llvm/ADT/SmallVector.h"
23
#include "llvm/ADT/StringMap.h"
24
#include "llvm/IR/Constants.h"
25
#include "llvm/IR/DerivedTypes.h"
26
#include "llvm/IR/Function.h"
27
#include "llvm/IR/InstIterator.h"
28
#include "llvm/IR/Instructions.h"
29
#include "llvm/IR/Intrinsics.h"
30
#include "llvm/IR/Module.h"
31
#include "llvm/IR/Type.h"
32
#include "llvm/Pass.h"
33
#include "llvm/Support/CommandLine.h"
34
#include "llvm/Support/Debug.h"
35
#include "llvm/Support/raw_os_ostream.h"
36
#include "llvm/Support/raw_ostream.h"
37
#include "llvm/Transforms/Scalar.h"
38
#include <sstream>
39
#include <string>
40
20.9k
#define NVVM_REFLECT_FUNCTION "__nvvm_reflect"
41
42
using namespace llvm;
43
44
#define DEBUG_TYPE "nvptx-reflect"
45
46
namespace llvm { void initializeNVVMReflectPass(PassRegistry &); }
47
48
namespace {
49
class NVVMReflect : public FunctionPass {
50
public:
51
  static char ID;
52
  unsigned int SmVersion;
53
5
  NVVMReflect() : NVVMReflect(0) {}
54
393
  explicit NVVMReflect(unsigned int Sm) : FunctionPass(ID), SmVersion(Sm) {
55
393
    initializeNVVMReflectPass(*PassRegistry::getPassRegistry());
56
393
  }
57
58
  bool runOnFunction(Function &) override;
59
};
60
}
61
62
388
FunctionPass *llvm::createNVVMReflectPass(unsigned int SmVersion) {
63
388
  return new NVVMReflect(SmVersion);
64
388
}
65
66
static cl::opt<bool>
67
NVVMReflectEnabled("nvvm-reflect-enable", cl::init(true), cl::Hidden,
68
                   cl::desc("NVVM reflection, enabled by default"));
69
70
char NVVMReflect::ID = 0;
71
INITIALIZE_PASS(NVVMReflect, "nvvm-reflect",
72
                "Replace occurrences of __nvvm_reflect() calls with 0/1", false,
73
                false)
74
75
3.55k
bool NVVMReflect::runOnFunction(Function &F) {
76
3.55k
  if (!NVVMReflectEnabled)
77
0
    return false;
78
3.55k
79
3.55k
  if (F.getName() == NVVM_REFLECT_FUNCTION) {
80
0
    assert(F.isDeclaration() && "_reflect function should not have a body");
81
0
    assert(F.getReturnType()->isIntegerTy() &&
82
0
           "_reflect's return type should be integer");
83
0
    return false;
84
0
  }
85
3.55k
86
3.55k
  SmallVector<Instruction *, 4> ToRemove;
87
3.55k
88
3.55k
  // Go through the calls in this function.  Each call to __nvvm_reflect or
89
3.55k
  // llvm.nvvm.reflect should be a CallInst with a ConstantArray argument.
90
3.55k
  // First validate that. If the c-string corresponding to the ConstantArray can
91
3.55k
  // be found successfully, see if it can be found in VarMap. If so, replace the
92
3.55k
  // uses of CallInst with the value found in VarMap. If not, replace the use
93
3.55k
  // with value 0.
94
3.55k
95
3.55k
  // The IR for __nvvm_reflect calls differs between CUDA versions.
96
3.55k
  //
97
3.55k
  // CUDA 6.5 and earlier uses this sequence:
98
3.55k
  //    %ptr = tail call i8* @llvm.nvvm.ptr.constant.to.gen.p0i8.p4i8
99
3.55k
  //        (i8 addrspace(4)* getelementptr inbounds
100
3.55k
  //           ([8 x i8], [8 x i8] addrspace(4)* @str, i32 0, i32 0))
101
3.55k
  //    %reflect = tail call i32 @__nvvm_reflect(i8* %ptr)
102
3.55k
  //
103
3.55k
  // The value returned by Sym->getOperand(0) is a Constant with a
104
3.55k
  // ConstantDataSequential operand which can be converted to string and used
105
3.55k
  // for lookup.
106
3.55k
  //
107
3.55k
  // CUDA 7.0 does it slightly differently:
108
3.55k
  //   %reflect = call i32 @__nvvm_reflect(i8* addrspacecast
109
3.55k
  //        (i8 addrspace(1)* getelementptr inbounds
110
3.55k
  //           ([8 x i8], [8 x i8] addrspace(1)* @str, i32 0, i32 0) to i8*))
111
3.55k
  //
112
3.55k
  // In this case, we get a Constant with a GlobalVariable operand and we need
113
3.55k
  // to dig deeper to find its initializer with the string we'll use for lookup.
114
97.9k
  for (Instruction &I : instructions(F)) {
115
97.9k
    CallInst *Call = dyn_cast<CallInst>(&I);
116
97.9k
    if (!Call)
117
89.2k
      continue;
118
8.77k
    Function *Callee = Call->getCalledFunction();
119
8.77k
    if (!Callee || 
(8.68k
Callee->getName() != 8.68k
NVVM_REFLECT_FUNCTION8.68k
&&
120
8.68k
                    
Callee->getIntrinsicID() != Intrinsic::nvvm_reflect8.67k
))
121
8.76k
      continue;
122
10
123
10
    // FIXME: Improve error handling here and elsewhere in this pass.
124
10
    assert(Call->getNumOperands() == 2 &&
125
10
           "Wrong number of operands to __nvvm_reflect function");
126
10
127
10
    // In cuda 6.5 and earlier, we will have an extra constant-to-generic
128
10
    // conversion of the string.
129
10
    const Value *Str = Call->getArgOperand(0);
130
10
    if (const CallInst *ConvCall = dyn_cast<CallInst>(Str)) {
131
4
      // FIXME: Add assertions about ConvCall.
132
4
      Str = ConvCall->getArgOperand(0);
133
4
    }
134
10
    assert(isa<ConstantExpr>(Str) &&
135
10
           "Format of __nvvm__reflect function not recognized");
136
10
    const ConstantExpr *GEP = cast<ConstantExpr>(Str);
137
10
138
10
    const Value *Sym = GEP->getOperand(0);
139
10
    assert(isa<Constant>(Sym) &&
140
10
           "Format of __nvvm_reflect function not recognized");
141
10
142
10
    const Value *Operand = cast<Constant>(Sym)->getOperand(0);
143
10
    if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Operand)) {
144
6
      // For CUDA-7.0 style __nvvm_reflect calls, we need to find the operand's
145
6
      // initializer.
146
6
      assert(GV->hasInitializer() &&
147
6
             "Format of _reflect function not recognized");
148
6
      const Constant *Initializer = GV->getInitializer();
149
6
      Operand = Initializer;
150
6
    }
151
10
152
10
    assert(isa<ConstantDataSequential>(Operand) &&
153
10
           "Format of _reflect function not recognized");
154
10
    assert(cast<ConstantDataSequential>(Operand)->isCString() &&
155
10
           "Format of _reflect function not recognized");
156
10
157
10
    StringRef ReflectArg = cast<ConstantDataSequential>(Operand)->getAsString();
158
10
    ReflectArg = ReflectArg.substr(0, ReflectArg.size() - 1);
159
10
    LLVM_DEBUG(dbgs() << "Arg of _reflect : " << ReflectArg << "\n");
160
10
161
10
    int ReflectVal = 0; // The default value is 0
162
10
    if (ReflectArg == "__CUDA_FTZ") {
163
7
      // Try to pull __CUDA_FTZ from the nvvm-reflect-ftz module flag.  Our
164
7
      // choice here must be kept in sync with AutoUpgrade, which uses the same
165
7
      // technique to detect whether ftz is enabled.
166
7
      if (auto *Flag = mdconst::extract_or_null<ConstantInt>(
167
7
              F.getParent()->getModuleFlag("nvvm-reflect-ftz")))
168
7
        ReflectVal = Flag->getSExtValue();
169
7
    } else 
if (3
ReflectArg == "__CUDA_ARCH"3
) {
170
2
      ReflectVal = SmVersion * 10;
171
2
    }
172
10
    Call->replaceAllUsesWith(ConstantInt::get(Call->getType(), ReflectVal));
173
10
    ToRemove.push_back(Call);
174
10
  }
175
3.55k
176
3.55k
  for (Instruction *I : ToRemove)
177
10
    I->eraseFromParent();
178
3.55k
179
3.55k
  return ToRemove.size() > 0;
180
3.55k
}