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::ptx_read_tid_x: 89 case Intrinsic::nvvm_read_ptx_sreg_tid_x: 90 Changed |= addRangeMetadata(0, MaxBlockSize.x, Call); 91 break; 92 case Intrinsic::ptx_read_tid_y: 93 case Intrinsic::nvvm_read_ptx_sreg_tid_y: 94 Changed |= addRangeMetadata(0, MaxBlockSize.y, Call); 95 break; 96 case Intrinsic::ptx_read_tid_z: 97 case Intrinsic::nvvm_read_ptx_sreg_tid_z: 98 Changed |= addRangeMetadata(0, MaxBlockSize.z, Call); 99 break; 100 101 // Block size 102 case Intrinsic::ptx_read_ntid_x: 103 case Intrinsic::nvvm_read_ptx_sreg_ntid_x: 104 Changed |= addRangeMetadata(1, MaxBlockSize.x+1, Call); 105 break; 106 case Intrinsic::ptx_read_ntid_y: 107 case Intrinsic::nvvm_read_ptx_sreg_ntid_y: 108 Changed |= addRangeMetadata(1, MaxBlockSize.y+1, Call); 109 break; 110 case Intrinsic::ptx_read_ntid_z: 111 case Intrinsic::nvvm_read_ptx_sreg_ntid_z: 112 Changed |= addRangeMetadata(1, MaxBlockSize.z+1, Call); 113 break; 114 115 // Index within grid 116 case Intrinsic::ptx_read_ctaid_x: 117 case Intrinsic::nvvm_read_ptx_sreg_ctaid_x: 118 Changed |= addRangeMetadata(0, MaxGridSize.x, Call); 119 break; 120 case Intrinsic::ptx_read_ctaid_y: 121 case Intrinsic::nvvm_read_ptx_sreg_ctaid_y: 122 Changed |= addRangeMetadata(0, MaxGridSize.y, Call); 123 break; 124 case Intrinsic::ptx_read_ctaid_z: 125 case Intrinsic::nvvm_read_ptx_sreg_ctaid_z: 126 Changed |= addRangeMetadata(0, MaxGridSize.z, Call); 127 break; 128 129 // Grid size 130 case Intrinsic::ptx_read_nctaid_x: 131 case Intrinsic::nvvm_read_ptx_sreg_nctaid_x: 132 Changed |= addRangeMetadata(1, MaxGridSize.x+1, Call); 133 break; 134 case Intrinsic::ptx_read_nctaid_y: 135 case Intrinsic::nvvm_read_ptx_sreg_nctaid_y: 136 Changed |= addRangeMetadata(1, MaxGridSize.y+1, Call); 137 break; 138 case Intrinsic::ptx_read_nctaid_z: 139 case Intrinsic::nvvm_read_ptx_sreg_nctaid_z: 140 Changed |= addRangeMetadata(1, MaxGridSize.z+1, Call); 141 break; 142 143 // warp size is constant 32. 144 case Intrinsic::nvvm_read_ptx_sreg_warpsize: 145 Changed |= addRangeMetadata(32, 32+1, Call); 146 break; 147 148 // Lane ID is [0..warpsize) 149 case Intrinsic::ptx_read_laneid: 150 Changed |= addRangeMetadata(0, 32, Call); 151 break; 152 153 default: 154 break; 155 } 156 } 157 } 158 159 return Changed; 160 } 161