1 //===- NVVMIntrRange.cpp - Set !range metadata for NVVM intrinsics --------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This pass adds appropriate !range metadata for calls to NVVM 11 // intrinsics that return a limited range of values. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "NVPTX.h" 16 #include "llvm/IR/Constants.h" 17 #include "llvm/IR/InstIterator.h" 18 #include "llvm/IR/Intrinsics.h" 19 #include "llvm/IR/Instructions.h" 20 21 using namespace llvm; 22 23 #define DEBUG_TYPE "nvvm-intr-range" 24 25 namespace llvm { void initializeNVVMIntrRangePass(PassRegistry &); } 26 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")); 30 31 namespace { 32 class NVVMIntrRange : public FunctionPass { 33 private: 34 struct { 35 unsigned x, y, z; 36 } MaxBlockSize, MaxGridSize; 37 38 public: 39 static char ID; 40 NVVMIntrRange() : NVVMIntrRange(NVVMIntrRangeSM) {} 41 NVVMIntrRange(unsigned int SmVersion) : FunctionPass(ID) { 42 MaxBlockSize.x = 1024; 43 MaxBlockSize.y = 1024; 44 MaxBlockSize.z = 64; 45 46 MaxGridSize.x = SmVersion >= 30 ? 0x7fffffff : 0xffff; 47 MaxGridSize.y = 0xffff; 48 MaxGridSize.z = 0xffff; 49 50 initializeNVVMIntrRangePass(*PassRegistry::getPassRegistry()); 51 } 52 53 bool runOnFunction(Function &) override; 54 }; 55 } 56 57 FunctionPass *llvm::createNVVMIntrRangePass(unsigned int SmVersion) { 58 return new NVVMIntrRange(SmVersion); 59 } 60 61 char NVVMIntrRange::ID = 0; 62 INITIALIZE_PASS(NVVMIntrRange, "nvvm-intr-range", 63 "Add !range metadata to NVVM intrinsics.", false, false) 64 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 LLVMContext &Context = C->getParent()->getContext(); 69 IntegerType *Int32Ty = Type::getInt32Ty(Context); 70 Metadata *LowAndHigh[] = { 71 ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Low)), 72 ConstantAsMetadata::get(ConstantInt::get(Int32Ty, High))}; 73 C->setMetadata(LLVMContext::MD_range, MDNode::get(Context, LowAndHigh)); 74 return true; 75 } 76 77 bool NVVMIntrRange::runOnFunction(Function &F) { 78 // Go through the calls in this function. 79 bool Changed = false; 80 for (Instruction &I : instructions(F)) { 81 CallInst *Call = dyn_cast<CallInst>(&I); 82 if (!Call) 83 continue; 84 85 if (Function *Callee = Call->getCalledFunction()) { 86 switch (Callee->getIntrinsicID()) { 87 // Index within block 88 case Intrinsic::nvvm_read_ptx_sreg_tid_x: 89 Changed |= addRangeMetadata(0, MaxBlockSize.x, Call); 90 break; 91 case Intrinsic::nvvm_read_ptx_sreg_tid_y: 92 Changed |= addRangeMetadata(0, MaxBlockSize.y, Call); 93 break; 94 case Intrinsic::nvvm_read_ptx_sreg_tid_z: 95 Changed |= addRangeMetadata(0, MaxBlockSize.z, Call); 96 break; 97 98 // Block size 99 case Intrinsic::nvvm_read_ptx_sreg_ntid_x: 100 Changed |= addRangeMetadata(1, MaxBlockSize.x+1, Call); 101 break; 102 case Intrinsic::nvvm_read_ptx_sreg_ntid_y: 103 Changed |= addRangeMetadata(1, MaxBlockSize.y+1, Call); 104 break; 105 case Intrinsic::nvvm_read_ptx_sreg_ntid_z: 106 Changed |= addRangeMetadata(1, MaxBlockSize.z+1, Call); 107 break; 108 109 // Index within grid 110 case Intrinsic::nvvm_read_ptx_sreg_ctaid_x: 111 Changed |= addRangeMetadata(0, MaxGridSize.x, Call); 112 break; 113 case Intrinsic::nvvm_read_ptx_sreg_ctaid_y: 114 Changed |= addRangeMetadata(0, MaxGridSize.y, Call); 115 break; 116 case Intrinsic::nvvm_read_ptx_sreg_ctaid_z: 117 Changed |= addRangeMetadata(0, MaxGridSize.z, Call); 118 break; 119 120 // Grid size 121 case Intrinsic::nvvm_read_ptx_sreg_nctaid_x: 122 Changed |= addRangeMetadata(1, MaxGridSize.x+1, Call); 123 break; 124 case Intrinsic::nvvm_read_ptx_sreg_nctaid_y: 125 Changed |= addRangeMetadata(1, MaxGridSize.y+1, Call); 126 break; 127 case Intrinsic::nvvm_read_ptx_sreg_nctaid_z: 128 Changed |= addRangeMetadata(1, MaxGridSize.z+1, Call); 129 break; 130 131 // warp size is constant 32. 132 case Intrinsic::nvvm_read_ptx_sreg_warpsize: 133 Changed |= addRangeMetadata(32, 32+1, Call); 134 break; 135 136 // Lane ID is [0..warpsize) 137 case Intrinsic::nvvm_read_ptx_sreg_laneid: 138 Changed |= addRangeMetadata(0, 32, Call); 139 break; 140 141 default: 142 break; 143 } 144 } 145 } 146 147 return Changed; 148 } 149