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 std::optional<uint64_t> OverallMaxNTID = getOverallMaxNTID(F);
71 auto ClusterDim = getClusterDim(F);
72 const std::optional<unsigned> MaxClusterRank = getMaxClusterRank(F);
73
74 // If this function lacks any range information, do nothing.
75 if (ReqNTID.empty() && !OverallMaxNTID && ClusterDim.empty() &&
76 !MaxClusterRank)
77 return false;
78
79 const uint64_t MaxNTID =
80 OverallMaxNTID.value_or(std::numeric_limits<uint64_t>::max());
81
82 // When reqntid is specified, block dimensions are exact compile-time
83 // constants. Otherwise, use maxntid (capped at hardware limits) as upper
84 // bounds.
85 Vector3 MinBlockDim, MaxBlockDim;
86 if (!ReqNTID.empty()) {
87 ReqNTID.resize(3, 1);
88 MinBlockDim = MaxBlockDim = {ReqNTID[0], ReqNTID[1], ReqNTID[2]};
89 } else {
90 MinBlockDim = {1, 1, 1};
91 MaxBlockDim = {static_cast<unsigned>(std::min(uint64_t{1024}, MaxNTID)),
92 static_cast<unsigned>(std::min(uint64_t{1024}, MaxNTID)),
93 static_cast<unsigned>(std::min(uint64_t{64}, MaxNTID))};
94 }
95
96 const bool HasClusterInfo = !ClusterDim.empty() || MaxClusterRank;
97
98 // When cluster_dim is specified, cluster dimensions are exact compile-time
99 // constants. Otherwise, use maxclusterrank (capped at hardware limits) as
100 // upper bounds.
101 Vector3 MinClusterDim, MaxClusterDim;
102 uint64_t MinClusterSize, MaxClusterSize;
103 if (!ClusterDim.empty()) {
104 ClusterDim.resize(3, 1);
105 MinClusterDim =
106 MaxClusterDim = {ClusterDim[0], ClusterDim[1], ClusterDim[2]};
107 MinClusterSize = MaxClusterSize =
108 ClusterDim[0] * ClusterDim[1] * ClusterDim[2];
109 } else {
110 const unsigned MaxNctaPerCluster =
111 MaxClusterRank.value_or(std::numeric_limits<unsigned>::max());
112 MinClusterDim = {1, 1, 1};
113 MaxClusterDim = {std::min(0x7fffffffu, MaxNctaPerCluster),
114 std::min(0xffffu, MaxNctaPerCluster),
115 std::min(0xffffu, MaxNctaPerCluster)};
116 MinClusterSize = 1;
117 MaxClusterSize = MaxNctaPerCluster;
118 }
119
120 const auto ProcessIntrinsic = [&](IntrinsicInst *II) -> bool {
121 switch (II->getIntrinsicID()) {
122 // Index within block
123 case Intrinsic::nvvm_read_ptx_sreg_tid_x:
124 return addRangeAttr(0, MaxBlockDim.X, II);
125 case Intrinsic::nvvm_read_ptx_sreg_tid_y:
126 return addRangeAttr(0, MaxBlockDim.Y, II);
127 case Intrinsic::nvvm_read_ptx_sreg_tid_z:
128 return addRangeAttr(0, MaxBlockDim.Z, II);
129
130 // Block size: use single-value range when reqntid is specified;
131 // InstCombine will fold these to constants later.
132 case Intrinsic::nvvm_read_ptx_sreg_ntid_x:
133 return addRangeAttr(MinBlockDim.X, MaxBlockDim.X + 1, II);
134 case Intrinsic::nvvm_read_ptx_sreg_ntid_y:
135 return addRangeAttr(MinBlockDim.Y, MaxBlockDim.Y + 1, II);
136 case Intrinsic::nvvm_read_ptx_sreg_ntid_z:
137 return addRangeAttr(MinBlockDim.Z, MaxBlockDim.Z + 1, II);
138
139 // Cluster size: use single-value ranges when cluster_dim is specified;
140 // InstCombine will fold cluster_nctaid.* / cluster_nctarank to constants
141 // later.
142 case Intrinsic::nvvm_read_ptx_sreg_cluster_ctaid_x:
143 return addRangeAttr(0, MaxClusterDim.X, II);
144 case Intrinsic::nvvm_read_ptx_sreg_cluster_ctaid_y:
145 return addRangeAttr(0, MaxClusterDim.Y, II);
146 case Intrinsic::nvvm_read_ptx_sreg_cluster_ctaid_z:
147 return addRangeAttr(0, MaxClusterDim.Z, II);
148 case Intrinsic::nvvm_read_ptx_sreg_cluster_nctaid_x:
149 return addRangeAttr(MinClusterDim.X, MaxClusterDim.X + 1, II);
150 case Intrinsic::nvvm_read_ptx_sreg_cluster_nctaid_y:
151 return addRangeAttr(MinClusterDim.Y, MaxClusterDim.Y + 1, II);
152 case Intrinsic::nvvm_read_ptx_sreg_cluster_nctaid_z:
153 return addRangeAttr(MinClusterDim.Z, MaxClusterDim.Z + 1, II);
154
155 case Intrinsic::nvvm_read_ptx_sreg_cluster_ctarank:
156 return HasClusterInfo && addRangeAttr(0, MaxClusterSize, II);
157 case Intrinsic::nvvm_read_ptx_sreg_cluster_nctarank:
158 return HasClusterInfo &&
159 addRangeAttr(MinClusterSize, MaxClusterSize + 1, II);
160 default:
161 return false;
162 }
163 };
164
165 // Go through the calls in this function.
166 bool Changed = false;
167 for (Instruction &I : instructions(F))
169 Changed |= ProcessIntrinsic(II);
170
171 return Changed;
172}
173
174bool NVVMIntrRange::runOnFunction(Function &F) { return runNVVMIntrRange(F); }
175
Expand Atomic instructions
#define X(NUM, ENUM, NAME)
Definition ELF.h:853
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
Check if the string is empty.
Definition StringRef.h:141
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
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)
std::optional< unsigned > getMaxClusterRank(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()
SmallVector< unsigned, 3 > getClusterDim(const Function &F)
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)