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"
20 #include "llvm/Support/CommandLine.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 
51      initializeNVVMIntrRangePass(*PassRegistry::getPassRegistry());
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 
73   LLVMContext &Context = C->getParent()->getContext();
74   IntegerType *Int32Ty = Type::getInt32Ty(Context);
75   Metadata *LowAndHigh[] = {
76       ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Low)),
77       ConstantAsMetadata::get(ConstantInt::get(Int32Ty, High))};
78   C->setMetadata(LLVMContext::MD_range, MDNode::get(Context, LowAndHigh));
79   return true;
80 }
81 
82 bool NVVMIntrRange::runOnFunction(Function &F) {
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 }
154