1 //===- NVVMIntrRange.cpp - Set !range metadata for NVVM intrinsics --------===//
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
7 //===----------------------------------------------------------------------===//
9 // This pass adds appropriate !range metadata for calls to NVVM
10 // intrinsics that return a limited range of values.
12 //===----------------------------------------------------------------------===//
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"
24 #define DEBUG_TYPE "nvvm-intr-range"
26 namespace llvm { void initializeNVVMIntrRangePass(PassRegistry &); }
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"));
33 class NVVMIntrRange : public FunctionPass {
37 } MaxBlockSize, MaxGridSize;
41 NVVMIntrRange() : NVVMIntrRange(NVVMIntrRangeSM) {}
42 NVVMIntrRange(unsigned int SmVersion) : FunctionPass(ID) {
43 MaxBlockSize.x = 1024;
44 MaxBlockSize.y = 1024;
47 MaxGridSize.x = SmVersion >= 30 ? 0x7fffffff : 0xffff;
48 MaxGridSize.y = 0xffff;
49 MaxGridSize.z = 0xffff;
51 initializeNVVMIntrRangePass(*PassRegistry::getPassRegistry());
54 bool runOnFunction(Function &) override;
58 FunctionPass *llvm::createNVVMIntrRangePass(unsigned int SmVersion) {
59 return new NVVMIntrRange(SmVersion);
62 char NVVMIntrRange::ID = 0;
63 INITIALIZE_PASS(NVVMIntrRange, "nvvm-intr-range",
64 "Add !range metadata to NVVM intrinsics.", false, false)
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))
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));
82 bool NVVMIntrRange::runOnFunction(Function &F) {
83 // Go through the calls in this function.
85 for (Instruction &I : instructions(F)) {
86 CallInst *Call = dyn_cast<CallInst>(&I);
90 if (Function *Callee = Call->getCalledFunction()) {
91 switch (Callee->getIntrinsicID()) {
93 case Intrinsic::nvvm_read_ptx_sreg_tid_x:
94 Changed |= addRangeMetadata(0, MaxBlockSize.x, Call);
96 case Intrinsic::nvvm_read_ptx_sreg_tid_y:
97 Changed |= addRangeMetadata(0, MaxBlockSize.y, Call);
99 case Intrinsic::nvvm_read_ptx_sreg_tid_z:
100 Changed |= addRangeMetadata(0, MaxBlockSize.z, Call);
104 case Intrinsic::nvvm_read_ptx_sreg_ntid_x:
105 Changed |= addRangeMetadata(1, MaxBlockSize.x+1, Call);
107 case Intrinsic::nvvm_read_ptx_sreg_ntid_y:
108 Changed |= addRangeMetadata(1, MaxBlockSize.y+1, Call);
110 case Intrinsic::nvvm_read_ptx_sreg_ntid_z:
111 Changed |= addRangeMetadata(1, MaxBlockSize.z+1, Call);
115 case Intrinsic::nvvm_read_ptx_sreg_ctaid_x:
116 Changed |= addRangeMetadata(0, MaxGridSize.x, Call);
118 case Intrinsic::nvvm_read_ptx_sreg_ctaid_y:
119 Changed |= addRangeMetadata(0, MaxGridSize.y, Call);
121 case Intrinsic::nvvm_read_ptx_sreg_ctaid_z:
122 Changed |= addRangeMetadata(0, MaxGridSize.z, Call);
126 case Intrinsic::nvvm_read_ptx_sreg_nctaid_x:
127 Changed |= addRangeMetadata(1, MaxGridSize.x+1, Call);
129 case Intrinsic::nvvm_read_ptx_sreg_nctaid_y:
130 Changed |= addRangeMetadata(1, MaxGridSize.y+1, Call);
132 case Intrinsic::nvvm_read_ptx_sreg_nctaid_z:
133 Changed |= addRangeMetadata(1, MaxGridSize.z+1, Call);
136 // warp size is constant 32.
137 case Intrinsic::nvvm_read_ptx_sreg_warpsize:
138 Changed |= addRangeMetadata(32, 32+1, Call);
141 // Lane ID is [0..warpsize)
142 case Intrinsic::nvvm_read_ptx_sreg_laneid:
143 Changed |= addRangeMetadata(0, 32, Call);