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/IR/PassManager.h"
21 #include "llvm/Support/CommandLine.h"
25 #define DEBUG_TYPE "nvvm-intr-range"
27 namespace llvm
{ void initializeNVVMIntrRangePass(PassRegistry
&); }
29 // Add !range metadata based on limits of given SM variant.
30 static cl::opt
<unsigned> NVVMIntrRangeSM("nvvm-intr-range-sm", cl::init(20),
31 cl::Hidden
, cl::desc("SM variant"));
34 class NVVMIntrRange
: public FunctionPass
{
40 NVVMIntrRange() : NVVMIntrRange(NVVMIntrRangeSM
) {}
41 NVVMIntrRange(unsigned int SmVersion
)
42 : FunctionPass(ID
), SmVersion(SmVersion
) {
44 initializeNVVMIntrRangePass(*PassRegistry::getPassRegistry());
47 bool runOnFunction(Function
&) override
;
51 FunctionPass
*llvm::createNVVMIntrRangePass(unsigned int SmVersion
) {
52 return new NVVMIntrRange(SmVersion
);
55 char NVVMIntrRange::ID
= 0;
56 INITIALIZE_PASS(NVVMIntrRange
, "nvvm-intr-range",
57 "Add !range metadata to NVVM intrinsics.", false, false)
59 // Adds the passed-in [Low,High) range information as metadata to the
60 // passed-in call instruction.
61 static bool addRangeMetadata(uint64_t Low
, uint64_t High
, CallInst
*C
) {
62 // This call already has range metadata, nothing to do.
63 if (C
->getMetadata(LLVMContext::MD_range
))
66 LLVMContext
&Context
= C
->getParent()->getContext();
67 IntegerType
*Int32Ty
= Type::getInt32Ty(Context
);
68 Metadata
*LowAndHigh
[] = {
69 ConstantAsMetadata::get(ConstantInt::get(Int32Ty
, Low
)),
70 ConstantAsMetadata::get(ConstantInt::get(Int32Ty
, High
))};
71 C
->setMetadata(LLVMContext::MD_range
, MDNode::get(Context
, LowAndHigh
));
75 static bool runNVVMIntrRange(Function
&F
, unsigned SmVersion
) {
78 } MaxBlockSize
, MaxGridSize
;
79 MaxBlockSize
.x
= 1024;
80 MaxBlockSize
.y
= 1024;
83 MaxGridSize
.x
= SmVersion
>= 30 ? 0x7fffffff : 0xffff;
84 MaxGridSize
.y
= 0xffff;
85 MaxGridSize
.z
= 0xffff;
87 // Go through the calls in this function.
89 for (Instruction
&I
: instructions(F
)) {
90 CallInst
*Call
= dyn_cast
<CallInst
>(&I
);
94 if (Function
*Callee
= Call
->getCalledFunction()) {
95 switch (Callee
->getIntrinsicID()) {
97 case Intrinsic::nvvm_read_ptx_sreg_tid_x
:
98 Changed
|= addRangeMetadata(0, MaxBlockSize
.x
, Call
);
100 case Intrinsic::nvvm_read_ptx_sreg_tid_y
:
101 Changed
|= addRangeMetadata(0, MaxBlockSize
.y
, Call
);
103 case Intrinsic::nvvm_read_ptx_sreg_tid_z
:
104 Changed
|= addRangeMetadata(0, MaxBlockSize
.z
, Call
);
108 case Intrinsic::nvvm_read_ptx_sreg_ntid_x
:
109 Changed
|= addRangeMetadata(1, MaxBlockSize
.x
+1, Call
);
111 case Intrinsic::nvvm_read_ptx_sreg_ntid_y
:
112 Changed
|= addRangeMetadata(1, MaxBlockSize
.y
+1, Call
);
114 case Intrinsic::nvvm_read_ptx_sreg_ntid_z
:
115 Changed
|= addRangeMetadata(1, MaxBlockSize
.z
+1, Call
);
119 case Intrinsic::nvvm_read_ptx_sreg_ctaid_x
:
120 Changed
|= addRangeMetadata(0, MaxGridSize
.x
, Call
);
122 case Intrinsic::nvvm_read_ptx_sreg_ctaid_y
:
123 Changed
|= addRangeMetadata(0, MaxGridSize
.y
, Call
);
125 case Intrinsic::nvvm_read_ptx_sreg_ctaid_z
:
126 Changed
|= addRangeMetadata(0, MaxGridSize
.z
, Call
);
130 case Intrinsic::nvvm_read_ptx_sreg_nctaid_x
:
131 Changed
|= addRangeMetadata(1, MaxGridSize
.x
+1, Call
);
133 case Intrinsic::nvvm_read_ptx_sreg_nctaid_y
:
134 Changed
|= addRangeMetadata(1, MaxGridSize
.y
+1, Call
);
136 case Intrinsic::nvvm_read_ptx_sreg_nctaid_z
:
137 Changed
|= addRangeMetadata(1, MaxGridSize
.z
+1, Call
);
140 // warp size is constant 32.
141 case Intrinsic::nvvm_read_ptx_sreg_warpsize
:
142 Changed
|= addRangeMetadata(32, 32+1, Call
);
145 // Lane ID is [0..warpsize)
146 case Intrinsic::nvvm_read_ptx_sreg_laneid
:
147 Changed
|= addRangeMetadata(0, 32, Call
);
159 bool NVVMIntrRange::runOnFunction(Function
&F
) {
160 return runNVVMIntrRange(F
, SmVersion
);
163 NVVMIntrRangePass::NVVMIntrRangePass() : NVVMIntrRangePass(NVVMIntrRangeSM
) {}
165 PreservedAnalyses
NVVMIntrRangePass::run(Function
&F
,
166 FunctionAnalysisManager
&AM
) {
167 return runNVVMIntrRange(F
, SmVersion
) ? PreservedAnalyses::none()
168 : PreservedAnalyses::all();