blob: b9c02c43114124cfc7680866b970c5660bddc511 [file] [log] [blame]
Artem Belevich49e9a812016-05-26 17:02:56 +00001//===- 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
21using namespace llvm;
22
23#define DEBUG_TYPE "nvvm-intr-range"
24
25namespace llvm { void initializeNVVMIntrRangePass(PassRegistry &); }
26
27// Add !range metadata based on limits of given SM variant.
28static cl::opt<unsigned> NVVMIntrRangeSM("nvvm-intr-range-sm", cl::init(20),
29 cl::Hidden, cl::desc("SM variant"));
30
31namespace {
32class 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) {}
Artem Belevich11f69ba2016-05-26 17:29:20 +000041 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
Artem Belevich49e9a812016-05-26 17:02:56 +000050 initializeNVVMIntrRangePass(*PassRegistry::getPassRegistry());
51 }
52
53 bool runOnFunction(Function &) override;
54};
55}
56
57FunctionPass *llvm::createNVVMIntrRangePass(unsigned int SmVersion) {
58 return new NVVMIntrRange(SmVersion);
59}
60
61char NVVMIntrRange::ID = 0;
62INITIALIZE_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.
67static 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
77bool 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
Artem Belevich49e9a812016-05-26 17:02:56 +000088 case Intrinsic::nvvm_read_ptx_sreg_tid_x:
89 Changed |= addRangeMetadata(0, MaxBlockSize.x, Call);
90 break;
Artem Belevich49e9a812016-05-26 17:02:56 +000091 case Intrinsic::nvvm_read_ptx_sreg_tid_y:
92 Changed |= addRangeMetadata(0, MaxBlockSize.y, Call);
93 break;
Artem Belevich49e9a812016-05-26 17:02:56 +000094 case Intrinsic::nvvm_read_ptx_sreg_tid_z:
95 Changed |= addRangeMetadata(0, MaxBlockSize.z, Call);
96 break;
97
98 // Block size
Artem Belevich49e9a812016-05-26 17:02:56 +000099 case Intrinsic::nvvm_read_ptx_sreg_ntid_x:
100 Changed |= addRangeMetadata(1, MaxBlockSize.x+1, Call);
101 break;
Artem Belevich49e9a812016-05-26 17:02:56 +0000102 case Intrinsic::nvvm_read_ptx_sreg_ntid_y:
103 Changed |= addRangeMetadata(1, MaxBlockSize.y+1, Call);
104 break;
Artem Belevich49e9a812016-05-26 17:02:56 +0000105 case Intrinsic::nvvm_read_ptx_sreg_ntid_z:
106 Changed |= addRangeMetadata(1, MaxBlockSize.z+1, Call);
107 break;
108
109 // Index within grid
Artem Belevich49e9a812016-05-26 17:02:56 +0000110 case Intrinsic::nvvm_read_ptx_sreg_ctaid_x:
111 Changed |= addRangeMetadata(0, MaxGridSize.x, Call);
112 break;
Artem Belevich49e9a812016-05-26 17:02:56 +0000113 case Intrinsic::nvvm_read_ptx_sreg_ctaid_y:
114 Changed |= addRangeMetadata(0, MaxGridSize.y, Call);
115 break;
Artem Belevich49e9a812016-05-26 17:02:56 +0000116 case Intrinsic::nvvm_read_ptx_sreg_ctaid_z:
117 Changed |= addRangeMetadata(0, MaxGridSize.z, Call);
118 break;
119
120 // Grid size
Artem Belevich49e9a812016-05-26 17:02:56 +0000121 case Intrinsic::nvvm_read_ptx_sreg_nctaid_x:
122 Changed |= addRangeMetadata(1, MaxGridSize.x+1, Call);
123 break;
Artem Belevich49e9a812016-05-26 17:02:56 +0000124 case Intrinsic::nvvm_read_ptx_sreg_nctaid_y:
125 Changed |= addRangeMetadata(1, MaxGridSize.y+1, Call);
126 break;
Artem Belevich49e9a812016-05-26 17:02:56 +0000127 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)
Justin Bognera466cc32016-07-07 16:40:17 +0000137 case Intrinsic::nvvm_read_ptx_sreg_laneid:
Artem Belevich49e9a812016-05-26 17:02:56 +0000138 Changed |= addRangeMetadata(0, 32, Call);
139 break;
140
141 default:
142 break;
143 }
144 }
145 }
146
147 return Changed;
148}