MLIR 24.0.0git
NVVMSyncOps.cpp
Go to the documentation of this file.
1//===- NVVMSyncOps.cpp - NVVM dialect synchronization ops -----------------===//
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 file defines the ops of op partition `Sync` of the NVVM dialect.
10//
11//===----------------------------------------------------------------------===//
12
14
16#include "mlir/IR/Diagnostics.h"
17#include "mlir/IR/MLIRContext.h"
18#include "mlir/IR/Operation.h"
19#include "mlir/IR/Types.h"
21#include "llvm/IR/IRBuilder.h"
22#include "llvm/IR/IntrinsicsNVPTX.h"
23#include "llvm/Support/Casting.h"
24#include "llvm/Support/ErrorHandling.h"
25
26#include <optional>
27#include <utility>
28
29using namespace mlir;
30using namespace mlir::NVVM;
31
32//===----------------------------------------------------------------------===//
33// Verifier methods
34//===----------------------------------------------------------------------===//
35
36LogicalResult NVVM::FenceProxyOp::verify() {
37 if (getKind() == NVVM::ProxyKind::async_shared && !getSpace().has_value()) {
38 return emitOpError() << "async_shared fence requires space attribute";
39 }
40 if (getKind() != NVVM::ProxyKind::async_shared && getSpace().has_value()) {
41 return emitOpError() << "only async_shared fence can have space attribute";
42 }
43 return success();
44}
45
46LogicalResult NVVM::FenceProxyAcquireOp::verify() {
47 if (getFromProxy() != NVVM::ProxyKind::GENERIC)
48 return emitOpError("uni-directional proxies only support generic for "
49 "from_proxy attribute");
50
51 if (getToProxy() != NVVM::ProxyKind::TENSORMAP)
52 return emitOpError("uni-directional proxies only support tensormap "
53 "for to_proxy attribute");
54 return success();
55}
56
57LogicalResult NVVM::FenceProxyReleaseOp::verify() {
58 if (getFromProxy() != NVVM::ProxyKind::GENERIC)
59 return emitOpError("uni-directional proxies only support generic for "
60 "from_proxy attribute");
61
62 if (getToProxy() != NVVM::ProxyKind::TENSORMAP)
63 return emitOpError("uni-directional proxies only support tensormap "
64 "for to_proxy attribute");
65 return success();
66}
67
68LogicalResult NVVM::FenceProxySyncRestrictOp::verify() {
69 if (getFromProxy() != NVVM::ProxyKind::GENERIC)
70 return emitOpError("only generic is support for from_proxy attribute");
71
72 if (getToProxy() != NVVM::ProxyKind::async)
73 return emitOpError("only async is supported for to_proxy attribute");
74 return success();
75}
76
77LogicalResult NVVM::MatchSyncOp::verify() {
78 if (getKind() == NVVM::MatchSyncKind::all) {
79 auto type = llvm::dyn_cast<LLVM::LLVMStructType>(getType());
80 if (!type || type.getBody().size() != 2 ||
81 !type.getBody()[0].isInteger(32) || !type.getBody()[1].isInteger(1)) {
82 return emitOpError("match.sync 'all' returns a two element struct with "
83 "first element as i32 and second element as i1");
84 }
85 } else {
86 if (!getType().isInteger(32)) {
87 return emitOpError("match.sync 'any' returns an i32");
88 }
89 }
90 return success();
91}
92
93LogicalResult MatchSyncOp::inferReturnTypes(
94 MLIRContext *context, std::optional<Location> location,
95 MatchSyncOp::Adaptor adaptor, SmallVectorImpl<Type> &inferredReturnTypes) {
96 if (adaptor.getKind() == NVVM::MatchSyncKind::all)
97 inferredReturnTypes.push_back(LLVM::LLVMStructType::getLiteral(
98 context,
99 {IntegerType::get(context, 32), IntegerType::get(context, 1)}));
100 else
101 inferredReturnTypes.push_back(IntegerType::get(context, 32));
102 return success();
103}
104
105LogicalResult NVVM::VoteSyncOp::verify() {
106 if (getKind() == NVVM::VoteSyncKind::ballot) {
107 if (!getType().isInteger(32)) {
108 return emitOpError("vote.sync 'ballot' returns an i32");
109 }
110 } else {
111 if (!getType().isInteger(1)) {
112 return emitOpError("vote.sync 'any', 'all' and 'uni' returns an i1");
113 }
114 }
115 return success();
116}
117
118LogicalResult VoteSyncOp::inferReturnTypes(
119 MLIRContext *context, std::optional<Location> location,
120 VoteSyncOp::Adaptor adaptor, SmallVectorImpl<Type> &inferredReturnTypes) {
121 unsigned width = adaptor.getKind() == NVVM::VoteSyncKind::ballot ? 32 : 1;
122 inferredReturnTypes.push_back(IntegerType::get(context, width));
123 return success();
124}
125
126LogicalResult NVVM::ReduxOp::verify() {
127 mlir::Type reduxType = getType();
128
129 if (!reduxType.isF32()) {
130 if (getAbs())
131 return emitOpError("abs attribute is supported only for f32 type");
132 if (getNan())
133 return emitOpError("nan attribute is supported only for f32 type");
134 }
135
136 NVVM::ReductionKind kind = getKind();
137 switch (kind) {
138 case NVVM::ReductionKind::ADD:
139 case NVVM::ReductionKind::AND:
140 case NVVM::ReductionKind::OR:
141 case NVVM::ReductionKind::XOR:
142 case NVVM::ReductionKind::MAX:
143 case NVVM::ReductionKind::MIN:
144 case NVVM::ReductionKind::UMAX:
145 case NVVM::ReductionKind::UMIN:
146 if (!reduxType.isInteger(32))
147 return emitOpError("'")
148 << kind << "' reduction kind unsupported with " << reduxType
149 << " type. Only supported type is 'i32'.";
150 break;
151 case NVVM::ReductionKind::FMIN:
152 case NVVM::ReductionKind::FMAX:
153 if (!reduxType.isF32())
154 return emitOpError("'")
155 << kind << "' reduction kind unsupported with " << reduxType
156 << " type. Only supported type is 'f32'.";
157 break;
158 }
159
160 return success();
161}
162
163LogicalResult NVVM::ClusterLaunchControlQueryCancelOp::verify() {
164 switch (getQueryType()) {
165 case NVVM::ClusterLaunchControlQueryType::IS_CANCELED:
166 if (!getType().isInteger(1))
167 return emitOpError("is_canceled query type returns an i1");
168 break;
169 case NVVM::ClusterLaunchControlQueryType::GET_FIRST_CTA_ID_X:
170 case NVVM::ClusterLaunchControlQueryType::GET_FIRST_CTA_ID_Y:
171 case NVVM::ClusterLaunchControlQueryType::GET_FIRST_CTA_ID_Z:
172 if (!getType().isInteger(32)) {
173 return emitOpError("get_first_cta_id_x, get_first_cta_id_y, "
174 "get_first_cta_id_z query types return an i32");
175 }
176 break;
177 }
178 return success();
179}
180
181LogicalResult ClusterLaunchControlQueryCancelOp::inferReturnTypes(
182 MLIRContext *context, std::optional<Location> location,
183 ClusterLaunchControlQueryCancelOp::Adaptor adaptor,
184 SmallVectorImpl<Type> &inferredReturnTypes) {
185 unsigned width =
186 adaptor.getQueryType() == NVVM::ClusterLaunchControlQueryType::IS_CANCELED
187 ? 1
188 : 32;
189 inferredReturnTypes.push_back(IntegerType::get(context, width));
190 return success();
191}
192
193//===----------------------------------------------------------------------===//
194// getIntrinsicID/getIntrinsicIDAndArgs methods
195//===----------------------------------------------------------------------===//
196
197/// Maps the (aligned, hasCount) pair to the `@llvm.nvvm.barrier.cta.sync.*`
198/// intrinsic ID.
199static llvm::Intrinsic::ID getBarrierSyncIntrinsic(bool aligned,
200 bool hasCount) {
201 if (hasCount) {
202 return aligned ? llvm::Intrinsic::nvvm_barrier_cta_sync_aligned_count
203 : llvm::Intrinsic::nvvm_barrier_cta_sync_count;
204 }
205 return aligned ? llvm::Intrinsic::nvvm_barrier_cta_sync_aligned_all
206 : llvm::Intrinsic::nvvm_barrier_cta_sync_all;
207}
208
209/// Maps the (aligned, kind) pair to the `@llvm.nvvm.barrier.cta.red.*`
210/// intrinsic ID.
211static llvm::Intrinsic::ID
212getBarrierReductionIntrinsic(bool aligned, NVVM::BarrierReduction kind) {
213 switch (kind) {
214 case NVVM::BarrierReduction::AND:
215 return aligned ? llvm::Intrinsic::nvvm_barrier_cta_red_and_aligned_all
216 : llvm::Intrinsic::nvvm_barrier_cta_red_and_all;
217 case NVVM::BarrierReduction::OR:
218 return aligned ? llvm::Intrinsic::nvvm_barrier_cta_red_or_aligned_all
219 : llvm::Intrinsic::nvvm_barrier_cta_red_or_all;
220 case NVVM::BarrierReduction::POPC:
221 return aligned ? llvm::Intrinsic::nvvm_barrier_cta_red_popc_aligned_all
222 : llvm::Intrinsic::nvvm_barrier_cta_red_popc_all;
223 }
224 llvm_unreachable("unknown BarrierReduction kind");
225}
226
227mlir::NVVM::IDArgPair NVVM::BarrierOp::getIntrinsicIDAndArgs(
228 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
229 auto thisOp = cast<NVVM::BarrierOp>(op);
230 llvm::Value *barrierId = thisOp.getBarrierId()
231 ? mt.lookupValue(thisOp.getBarrierId())
232 : builder.getInt32(0);
233 bool hasCount = static_cast<bool>(thisOp.getNumberOfThreads());
234 llvm::Intrinsic::ID id =
235 getBarrierSyncIntrinsic(thisOp.getAligned(), hasCount);
236 llvm::SmallVector<llvm::Value *> args = {barrierId};
237 if (hasCount)
238 args.push_back(mt.lookupValue(thisOp.getNumberOfThreads()));
239 return {id, std::move(args)};
240}
241
242mlir::NVVM::IDArgPair NVVM::BarrierArriveOp::getIntrinsicIDAndArgs(
243 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
244 auto thisOp = cast<NVVM::BarrierArriveOp>(op);
245 llvm::Value *barrierId = thisOp.getBarrierId()
246 ? mt.lookupValue(thisOp.getBarrierId())
247 : builder.getInt32(0);
248 llvm::Value *numThreads = mt.lookupValue(thisOp.getNumberOfThreads());
249 llvm::Intrinsic::ID id =
250 thisOp.getAligned()
251 ? llvm::Intrinsic::nvvm_barrier_cta_arrive_aligned_count
252 : llvm::Intrinsic::nvvm_barrier_cta_arrive_count;
253 return {id, {barrierId, numThreads}};
254}
255
256mlir::NVVM::IDArgPair NVVM::BarrierReductionOp::getIntrinsicIDAndArgs(
257 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
258 auto thisOp = cast<NVVM::BarrierReductionOp>(op);
259 llvm::Intrinsic::ID id = getBarrierReductionIntrinsic(
260 thisOp.getAligned(), thisOp.getReductionOp());
261 llvm::Value *barrierId = thisOp.getBarrierId()
262 ? mt.lookupValue(thisOp.getBarrierId())
263 : builder.getInt32(0);
265 barrierId,
266 builder.CreateICmpNE(mt.lookupValue(thisOp.getReductionPredicate()),
267 builder.getInt32(0))};
268 return {id, std::move(args)};
269}
270
271NVVM::IDArgPair ClusterLaunchControlTryCancelOp::getIntrinsicIDAndArgs(
272 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
273 auto curOp = cast<NVVM::ClusterLaunchControlTryCancelOp>(op);
275 args.push_back(mt.lookupValue(curOp.getSmemAddress()));
276 args.push_back(mt.lookupValue(curOp.getMbarrier()));
277
278 llvm::Intrinsic::ID intrinsicID =
279 curOp.getMulticast()
280 ? llvm::Intrinsic::
281 nvvm_clusterlaunchcontrol_try_cancel_async_multicast_shared
282 : llvm::Intrinsic::nvvm_clusterlaunchcontrol_try_cancel_async_shared;
283
284 return {intrinsicID, args};
285}
286
287NVVM::IDArgPair ClusterLaunchControlQueryCancelOp::getIntrinsicIDAndArgs(
288 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
289 auto curOp = cast<NVVM::ClusterLaunchControlQueryCancelOp>(op);
291 args.push_back(mt.lookupValue(curOp.getTryCancelResponse()));
292
293 llvm::Intrinsic::ID intrinsicID;
294
295 switch (curOp.getQueryType()) {
296 case NVVM::ClusterLaunchControlQueryType::IS_CANCELED:
297 intrinsicID =
298 llvm::Intrinsic::nvvm_clusterlaunchcontrol_query_cancel_is_canceled;
299 break;
300 case NVVM::ClusterLaunchControlQueryType::GET_FIRST_CTA_ID_X:
301 intrinsicID = llvm::Intrinsic::
302 nvvm_clusterlaunchcontrol_query_cancel_get_first_ctaid_x;
303 break;
304 case NVVM::ClusterLaunchControlQueryType::GET_FIRST_CTA_ID_Y:
305 intrinsicID = llvm::Intrinsic::
306 nvvm_clusterlaunchcontrol_query_cancel_get_first_ctaid_y;
307 break;
308 case NVVM::ClusterLaunchControlQueryType::GET_FIRST_CTA_ID_Z:
309 intrinsicID = llvm::Intrinsic::
310 nvvm_clusterlaunchcontrol_query_cancel_get_first_ctaid_z;
311 break;
312 }
313 return {intrinsicID, args};
314}
315
316#define GET_OP_CLASSES
317#include "mlir/Dialect/LLVMIR/NVVMSyncOps.cpp.inc"
return success()
static llvm::Intrinsic::ID getBarrierReductionIntrinsic(bool aligned, NVVM::BarrierReduction kind)
Maps the (aligned, kind) pair to the @llvm.nvvm.barrier.cta.red.
static llvm::Intrinsic::ID getBarrierSyncIntrinsic(bool aligned, bool hasCount)
Maps the (aligned, hasCount) pair to the @llvm.nvvm.barrier.cta.sync.
Implementation class for module translation.
llvm::Value * lookupValue(Value value) const
Finds an LLVM IR value corresponding to the given MLIR value.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:71
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
bool isF32() const
Definition Types.cpp:40
bool isInteger() const
Return true if this is an integer type (with the specified width).
Definition Types.cpp:58
std::pair< llvm::Intrinsic::ID, llvm::SmallVector< llvm::Value * > > IDArgPair
A pair type of LLVM's Intrinsic ID and args (which are llvm values).
Include the generated interface declarations.
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
Definition Utils.cpp:311