1 //===- NVVMIntrRange.cpp - Set !range metadata for NVVM intrinsics --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass adds appropriate !range metadata for calls to NVVM
11 // intrinsics that return a limited range of values.
13 //===----------------------------------------------------------------------===//
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/InstIterator.h"
18 #include "llvm/IR/Intrinsics.h"
19 #include "llvm/IR/Instructions.h"
23 #define DEBUG_TYPE "nvvm-intr-range"
25 namespace llvm { void initializeNVVMIntrRangePass(PassRegistry &); }
27 // Add !range metadata based on limits of given SM variant.
28 static cl::opt<unsigned> NVVMIntrRangeSM("nvvm-intr-range-sm", cl::init(20),
29 cl::Hidden, cl::desc("SM variant"));
32 class NVVMIntrRange : public FunctionPass {
36 } MaxBlockSize, MaxGridSize;
40 NVVMIntrRange() : NVVMIntrRange(NVVMIntrRangeSM) {}
41 NVVMIntrRange(unsigned int SmVersion) : FunctionPass(ID) {
42 MaxBlockSize.x = 1024;
43 MaxBlockSize.y = 1024;
46 MaxGridSize.x = SmVersion >= 30 ? 0x7fffffff : 0xffff;
47 MaxGridSize.y = 0xffff;
48 MaxGridSize.z = 0xffff;
50 initializeNVVMIntrRangePass(*PassRegistry::getPassRegistry());
53 bool runOnFunction(Function &) override;
57 FunctionPass *llvm::createNVVMIntrRangePass(unsigned int SmVersion) {
58 return new NVVMIntrRange(SmVersion);
61 char NVVMIntrRange::ID = 0;
62 INITIALIZE_PASS(NVVMIntrRange, "nvvm-intr-range",
63 "Add !range metadata to NVVM intrinsics.", false, false)
65 // Adds the passed-in [Low,High) range information as metadata to the
66 // passed-in call instruction.
67 static bool addRangeMetadata(uint64_t Low, uint64_t High, CallInst *C) {
68 // This call already has range metadata, nothing to do.
69 if (C->getMetadata(LLVMContext::MD_range))
72 LLVMContext &Context = C->getParent()->getContext();
73 IntegerType *Int32Ty = Type::getInt32Ty(Context);
74 Metadata *LowAndHigh[] = {
75 ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Low)),
76 ConstantAsMetadata::get(ConstantInt::get(Int32Ty, High))};
77 C->setMetadata(LLVMContext::MD_range, MDNode::get(Context, LowAndHigh));
81 bool NVVMIntrRange::runOnFunction(Function &F) {
82 // Go through the calls in this function.
84 for (Instruction &I : instructions(F)) {
85 CallInst *Call = dyn_cast<CallInst>(&I);
89 if (Function *Callee = Call->getCalledFunction()) {
90 switch (Callee->getIntrinsicID()) {
92 case Intrinsic::nvvm_read_ptx_sreg_tid_x:
93 Changed |= addRangeMetadata(0, MaxBlockSize.x, Call);
95 case Intrinsic::nvvm_read_ptx_sreg_tid_y:
96 Changed |= addRangeMetadata(0, MaxBlockSize.y, Call);
98 case Intrinsic::nvvm_read_ptx_sreg_tid_z:
99 Changed |= addRangeMetadata(0, MaxBlockSize.z, Call);
103 case Intrinsic::nvvm_read_ptx_sreg_ntid_x:
104 Changed |= addRangeMetadata(1, MaxBlockSize.x+1, Call);
106 case Intrinsic::nvvm_read_ptx_sreg_ntid_y:
107 Changed |= addRangeMetadata(1, MaxBlockSize.y+1, Call);
109 case Intrinsic::nvvm_read_ptx_sreg_ntid_z:
110 Changed |= addRangeMetadata(1, MaxBlockSize.z+1, Call);
114 case Intrinsic::nvvm_read_ptx_sreg_ctaid_x:
115 Changed |= addRangeMetadata(0, MaxGridSize.x, Call);
117 case Intrinsic::nvvm_read_ptx_sreg_ctaid_y:
118 Changed |= addRangeMetadata(0, MaxGridSize.y, Call);
120 case Intrinsic::nvvm_read_ptx_sreg_ctaid_z:
121 Changed |= addRangeMetadata(0, MaxGridSize.z, Call);
125 case Intrinsic::nvvm_read_ptx_sreg_nctaid_x:
126 Changed |= addRangeMetadata(1, MaxGridSize.x+1, Call);
128 case Intrinsic::nvvm_read_ptx_sreg_nctaid_y:
129 Changed |= addRangeMetadata(1, MaxGridSize.y+1, Call);
131 case Intrinsic::nvvm_read_ptx_sreg_nctaid_z:
132 Changed |= addRangeMetadata(1, MaxGridSize.z+1, Call);
135 // warp size is constant 32.
136 case Intrinsic::nvvm_read_ptx_sreg_warpsize:
137 Changed |= addRangeMetadata(32, 32+1, Call);
140 // Lane ID is [0..warpsize)
141 case Intrinsic::nvvm_read_ptx_sreg_laneid:
142 Changed |= addRangeMetadata(0, 32, Call);