LLVM 23.0.0git
NVVMIntrRange.cpp
Go to the documentation of this file.
1//===- NVVMIntrRange.cpp - Set range attributes for NVVM intrinsics -------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This pass adds appropriate range attributes for calls to NVVM
10// intrinsics that return a limited range of values.
11//
12//===----------------------------------------------------------------------===//
13
14#include "NVPTX.h"
15#include "NVVMProperties.h"
19#include "llvm/IR/Intrinsics.h"
20#include "llvm/IR/IntrinsicsNVPTX.h"
21#include "llvm/IR/PassManager.h"
22#include <cstdint>
23
24using namespace llvm;
25
26#define DEBUG_TYPE "nvvm-intr-range"
27
28namespace {
29class NVVMIntrRange : public FunctionPass {
30public:
31 static char ID;
32 NVVMIntrRange() : FunctionPass(ID) {}
33
34 bool runOnFunction(Function &) override;
35};
36} // namespace
37
38FunctionPass *llvm::createNVVMIntrRangePass() { return new NVVMIntrRange(); }
39
40char NVVMIntrRange::ID = 0;
41INITIALIZE_PASS(NVVMIntrRange, "nvvm-intr-range",
42 "Add !range metadata to NVVM intrinsics.", false, false)
43
44// Adds the passed-in [Low,High) range information as metadata to the
45// passed-in call instruction.
46static bool addRangeAttr(uint64_t Low, uint64_t High, IntrinsicInst *II) {
47 if (II->getMetadata(LLVMContext::MD_range))
48 return false;
49
50 const uint64_t BitWidth = II->getType()->getIntegerBitWidth();
52
53 if (auto CurrentRange = II->getRange())
54 Range = Range.intersectWith(CurrentRange.value());
55
56 II->addRangeRetAttr(Range);
57 return true;
58}
59
61 struct Vector3 {
62 unsigned X, Y, Z;
63 };
64
65 // All these annotations are only valid for kernel functions.
66 if (!isKernelFunction(F))
67 return false;
68
69 auto ReqNTID = getReqNTID(F);
70 const auto OverallMaxNTID = getOverallMaxNTID(F);
71 const auto OverallClusterRank = getOverallClusterRank(F);
72
73 // If this function lacks any range information, do nothing.
74 if (!(!ReqNTID.empty() || OverallMaxNTID || OverallClusterRank))
75 return false;
76
77 const unsigned MaxNTID =
78 OverallMaxNTID.value_or(std::numeric_limits<unsigned>::max());
79
80 const unsigned FunctionClusterRank =
81 OverallClusterRank.value_or(std::numeric_limits<unsigned>::max());
82
83 // When reqntid is specified, block dimensions are exact compile-time
84 // constants. Otherwise, use maxntid (capped at hardware limits) as upper
85 // bounds.
86 Vector3 MinBlockDim, MaxBlockDim;
87 if (!ReqNTID.empty()) {
88 ReqNTID.resize(3, 1);
89 MinBlockDim = MaxBlockDim = {ReqNTID[0], ReqNTID[1], ReqNTID[2]};
90 } else {
91 MinBlockDim = {1, 1, 1};
92 MaxBlockDim = {std::min(1024u, MaxNTID), std::min(1024u, MaxNTID),
93 std::min(64u, MaxNTID)};
94 }
95
96 // We conservatively use the maximum grid size as an upper bound for the
97 // cluster rank.
98 const Vector3 MaxClusterRank{std::min(0x7fffffffu, FunctionClusterRank),
99 std::min(0xffffu, FunctionClusterRank),
100 std::min(0xffffu, FunctionClusterRank)};
101
102 const auto ProcessIntrinsic = [&](IntrinsicInst *II) -> bool {
103 switch (II->getIntrinsicID()) {
104 // Index within block
105 case Intrinsic::nvvm_read_ptx_sreg_tid_x:
106 return addRangeAttr(0, MaxBlockDim.X, II);
107 case Intrinsic::nvvm_read_ptx_sreg_tid_y:
108 return addRangeAttr(0, MaxBlockDim.Y, II);
109 case Intrinsic::nvvm_read_ptx_sreg_tid_z:
110 return addRangeAttr(0, MaxBlockDim.Z, II);
111
112 // Block size: use single-value range when reqntid is specified;
113 // InstCombine will fold these to constants later.
114 case Intrinsic::nvvm_read_ptx_sreg_ntid_x:
115 return addRangeAttr(MinBlockDim.X, MaxBlockDim.X + 1, II);
116 case Intrinsic::nvvm_read_ptx_sreg_ntid_y:
117 return addRangeAttr(MinBlockDim.Y, MaxBlockDim.Y + 1, II);
118 case Intrinsic::nvvm_read_ptx_sreg_ntid_z:
119 return addRangeAttr(MinBlockDim.Z, MaxBlockDim.Z + 1, II);
120
121 // Cluster size
122 case Intrinsic::nvvm_read_ptx_sreg_cluster_ctaid_x:
123 return addRangeAttr(0, MaxClusterRank.X, II);
124 case Intrinsic::nvvm_read_ptx_sreg_cluster_ctaid_y:
125 return addRangeAttr(0, MaxClusterRank.Y, II);
126 case Intrinsic::nvvm_read_ptx_sreg_cluster_ctaid_z:
127 return addRangeAttr(0, MaxClusterRank.Z, II);
128 case Intrinsic::nvvm_read_ptx_sreg_cluster_nctaid_x:
129 return addRangeAttr(1, MaxClusterRank.X + 1, II);
130 case Intrinsic::nvvm_read_ptx_sreg_cluster_nctaid_y:
131 return addRangeAttr(1, MaxClusterRank.Y + 1, II);
132 case Intrinsic::nvvm_read_ptx_sreg_cluster_nctaid_z:
133 return addRangeAttr(1, MaxClusterRank.Z + 1, II);
134
135 case Intrinsic::nvvm_read_ptx_sreg_cluster_ctarank:
136 if (OverallClusterRank)
137 return addRangeAttr(0, FunctionClusterRank, II);
138 break;
139 case Intrinsic::nvvm_read_ptx_sreg_cluster_nctarank:
140 if (OverallClusterRank)
141 return addRangeAttr(1, FunctionClusterRank + 1, II);
142 break;
143 default:
144 return false;
145 }
146 return false;
147 };
148
149 // Go through the calls in this function.
150 bool Changed = false;
151 for (Instruction &I : instructions(F))
153 Changed |= ProcessIntrinsic(II);
154
155 return Changed;
156}
157
158bool NVVMIntrRange::runOnFunction(Function &F) { return runNVVMIntrRange(F); }
159
Expand Atomic instructions
#define X(NUM, ENUM, NAME)
Definition ELF.h:851
static bool runOnFunction(Function &F, bool PostInlining)
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t High
static bool runNVVMIntrRange(Function &F)
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
Class for arbitrary precision integers.
Definition APInt.h:78
This class represents a range of values.
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
A wrapper class for inspecting calls to intrinsic functions.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:140
Changed
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
Definition Threading.h:280
std::optional< uint64_t > getOverallClusterRank(const Function &F)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
SmallVector< unsigned, 3 > getReqNTID(const Function &F)
constexpr unsigned BitWidth
bool isKernelFunction(const Function &F)
std::optional< uint64_t > getOverallMaxNTID(const Function &F)
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
FunctionPass * createNVVMIntrRangePass()
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)