LLVM  10.0.0
NVVMIntrRange.cpp
Go to the documentation of this file.
1 //===- NVVMIntrRange.cpp - Set !range metadata for NVVM intrinsics --------===//
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 adds appropriate !range metadata for calls to NVVM
10 // intrinsics that return a limited range of values.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "NVPTX.h"
15 #include "llvm/IR/Constants.h"
16 #include "llvm/IR/InstIterator.h"
17 #include "llvm/IR/Instructions.h"
18 #include "llvm/IR/Intrinsics.h"
19 #include "llvm/IR/IntrinsicsNVPTX.h"
21 
22 using namespace llvm;
23 
24 #define DEBUG_TYPE "nvvm-intr-range"
25 
26 namespace llvm { void initializeNVVMIntrRangePass(PassRegistry &); }
27 
28 // Add !range metadata based on limits of given SM variant.
29 static cl::opt<unsigned> NVVMIntrRangeSM("nvvm-intr-range-sm", cl::init(20),
30  cl::Hidden, cl::desc("SM variant"));
31 
32 namespace {
33 class NVVMIntrRange : public FunctionPass {
34  private:
35  struct {
36  unsigned x, y, z;
37  } MaxBlockSize, MaxGridSize;
38 
39  public:
40  static char ID;
41  NVVMIntrRange() : NVVMIntrRange(NVVMIntrRangeSM) {}
42  NVVMIntrRange(unsigned int SmVersion) : FunctionPass(ID) {
43  MaxBlockSize.x = 1024;
44  MaxBlockSize.y = 1024;
45  MaxBlockSize.z = 64;
46 
47  MaxGridSize.x = SmVersion >= 30 ? 0x7fffffff : 0xffff;
48  MaxGridSize.y = 0xffff;
49  MaxGridSize.z = 0xffff;
50 
52  }
53 
54  bool runOnFunction(Function &) override;
55 };
56 }
57 
58 FunctionPass *llvm::createNVVMIntrRangePass(unsigned int SmVersion) {
59  return new NVVMIntrRange(SmVersion);
60 }
61 
62 char NVVMIntrRange::ID = 0;
63 INITIALIZE_PASS(NVVMIntrRange, "nvvm-intr-range",
64  "Add !range metadata to NVVM intrinsics.", false, false)
65 
66 // Adds the passed-in [Low,High) range information as metadata to the
67 // passed-in call instruction.
68 static bool addRangeMetadata(uint64_t Low, uint64_t High, CallInst *C) {
69  // This call already has range metadata, nothing to do.
70  if (C->getMetadata(LLVMContext::MD_range))
71  return false;
72 
78  C->setMetadata(LLVMContext::MD_range, MDNode::get(Context, LowAndHigh));
79  return true;
80 }
81 
83  // Go through the calls in this function.
84  bool Changed = false;
85  for (Instruction &I : instructions(F)) {
86  CallInst *Call = dyn_cast<CallInst>(&I);
87  if (!Call)
88  continue;
89 
90  if (Function *Callee = Call->getCalledFunction()) {
91  switch (Callee->getIntrinsicID()) {
92  // Index within block
93  case Intrinsic::nvvm_read_ptx_sreg_tid_x:
94  Changed |= addRangeMetadata(0, MaxBlockSize.x, Call);
95  break;
96  case Intrinsic::nvvm_read_ptx_sreg_tid_y:
97  Changed |= addRangeMetadata(0, MaxBlockSize.y, Call);
98  break;
99  case Intrinsic::nvvm_read_ptx_sreg_tid_z:
100  Changed |= addRangeMetadata(0, MaxBlockSize.z, Call);
101  break;
102 
103  // Block size
104  case Intrinsic::nvvm_read_ptx_sreg_ntid_x:
105  Changed |= addRangeMetadata(1, MaxBlockSize.x+1, Call);
106  break;
107  case Intrinsic::nvvm_read_ptx_sreg_ntid_y:
108  Changed |= addRangeMetadata(1, MaxBlockSize.y+1, Call);
109  break;
110  case Intrinsic::nvvm_read_ptx_sreg_ntid_z:
111  Changed |= addRangeMetadata(1, MaxBlockSize.z+1, Call);
112  break;
113 
114  // Index within grid
115  case Intrinsic::nvvm_read_ptx_sreg_ctaid_x:
116  Changed |= addRangeMetadata(0, MaxGridSize.x, Call);
117  break;
118  case Intrinsic::nvvm_read_ptx_sreg_ctaid_y:
119  Changed |= addRangeMetadata(0, MaxGridSize.y, Call);
120  break;
121  case Intrinsic::nvvm_read_ptx_sreg_ctaid_z:
122  Changed |= addRangeMetadata(0, MaxGridSize.z, Call);
123  break;
124 
125  // Grid size
126  case Intrinsic::nvvm_read_ptx_sreg_nctaid_x:
127  Changed |= addRangeMetadata(1, MaxGridSize.x+1, Call);
128  break;
129  case Intrinsic::nvvm_read_ptx_sreg_nctaid_y:
130  Changed |= addRangeMetadata(1, MaxGridSize.y+1, Call);
131  break;
132  case Intrinsic::nvvm_read_ptx_sreg_nctaid_z:
133  Changed |= addRangeMetadata(1, MaxGridSize.z+1, Call);
134  break;
135 
136  // warp size is constant 32.
137  case Intrinsic::nvvm_read_ptx_sreg_warpsize:
138  Changed |= addRangeMetadata(32, 32+1, Call);
139  break;
140 
141  // Lane ID is [0..warpsize)
142  case Intrinsic::nvvm_read_ptx_sreg_laneid:
143  Changed |= addRangeMetadata(0, 32, Call);
144  break;
145 
146  default:
147  break;
148  }
149  }
150  }
151 
152  return Changed;
153 }
uint64_t CallInst * C
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
LLVMContext & Context
This class represents lattice values for constants.
Definition: AllocatorList.h:23
This class represents a function call, abstracting a target machine&#39;s calling convention.
F(f)
LLVMContext & getContext() const
Get the context in which this basic block lives.
Definition: BasicBlock.cpp:32
uint64_t High
Metadata * LowAndHigh[]
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:244
static ConstantAsMetadata * get(Constant *C)
Definition: Metadata.h:410
void initializeNVVMIntrRangePass(PassRegistry &)
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1174
static bool runOnFunction(Function &F, bool PostInlining)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:435
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:63
INITIALIZE_PASS(NVVMIntrRange, "nvvm-intr-range", "Add !range metadata to NVVM intrinsics.", false, false) static bool addRangeMetadata(uint64_t Low
This file contains the declarations for the subclasses of Constant, which represent the different fla...
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:282
Class to represent integer types.
Definition: DerivedTypes.h:40
static cl::opt< unsigned > NVVMIntrRangeSM("nvvm-intr-range-sm", cl::init(20), cl::Hidden, cl::desc("SM variant"))
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1224
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:704
amdgpu Simplify well known AMD library false FunctionCallee Callee
FunctionPass * createNVVMIntrRangePass(unsigned int SmVersion)
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:180
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation.
Definition: InstrTypes.h:1305
#define I(x, y, z)
Definition: MD5.cpp:58
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
inst_range instructions(Function *F)
Definition: InstIterator.h:133
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:38
Root of the metadata hierarchy.
Definition: Metadata.h:58
IntegerType * Int32Ty
const BasicBlock * getParent() const
Definition: Instruction.h:66