21#include "llvm/IR/IRBuilder.h"
22#include "llvm/IR/IntrinsicsNVPTX.h"
23#include "llvm/Support/Casting.h"
24#include "llvm/Support/ErrorHandling.h"
36LogicalResult NVVM::FenceProxyOp::verify() {
37 if (getKind() == NVVM::ProxyKind::async_shared && !getSpace().has_value()) {
38 return emitOpError() <<
"async_shared fence requires space attribute";
40 if (getKind() != NVVM::ProxyKind::async_shared && getSpace().has_value()) {
41 return emitOpError() <<
"only async_shared fence can have space attribute";
46LogicalResult NVVM::FenceProxyAcquireOp::verify() {
47 if (getFromProxy() != NVVM::ProxyKind::GENERIC)
48 return emitOpError(
"uni-directional proxies only support generic for "
49 "from_proxy attribute");
51 if (getToProxy() != NVVM::ProxyKind::TENSORMAP)
52 return emitOpError(
"uni-directional proxies only support tensormap "
53 "for to_proxy attribute");
57LogicalResult NVVM::FenceProxyReleaseOp::verify() {
58 if (getFromProxy() != NVVM::ProxyKind::GENERIC)
59 return emitOpError(
"uni-directional proxies only support generic for "
60 "from_proxy attribute");
62 if (getToProxy() != NVVM::ProxyKind::TENSORMAP)
63 return emitOpError(
"uni-directional proxies only support tensormap "
64 "for to_proxy attribute");
68LogicalResult NVVM::FenceProxySyncRestrictOp::verify() {
69 if (getFromProxy() != NVVM::ProxyKind::GENERIC)
70 return emitOpError(
"only generic is support for from_proxy attribute");
72 if (getToProxy() != NVVM::ProxyKind::async)
73 return emitOpError(
"only async is supported for to_proxy attribute");
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");
87 return emitOpError(
"match.sync 'any' returns an i32");
93LogicalResult MatchSyncOp::inferReturnTypes(
94 MLIRContext *context, std::optional<Location> location,
96 if (adaptor.getKind() == NVVM::MatchSyncKind::all)
97 inferredReturnTypes.push_back(LLVM::LLVMStructType::getLiteral(
99 {IntegerType::get(context, 32), IntegerType::get(context, 1)}));
101 inferredReturnTypes.push_back(IntegerType::get(context, 32));
105LogicalResult NVVM::VoteSyncOp::verify() {
106 if (getKind() == NVVM::VoteSyncKind::ballot) {
107 if (!
getType().isInteger(32)) {
108 return emitOpError(
"vote.sync 'ballot' returns an i32");
112 return emitOpError(
"vote.sync 'any', 'all' and 'uni' returns an i1");
118LogicalResult VoteSyncOp::inferReturnTypes(
119 MLIRContext *context, std::optional<Location> location,
121 unsigned width = adaptor.getKind() == NVVM::VoteSyncKind::ballot ? 32 : 1;
122 inferredReturnTypes.push_back(IntegerType::get(context, width));
126LogicalResult NVVM::ReduxOp::verify() {
129 if (!reduxType.
isF32()) {
131 return emitOpError(
"abs attribute is supported only for f32 type");
133 return emitOpError(
"nan attribute is supported only for f32 type");
136 NVVM::ReductionKind kind = getKind();
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:
147 return emitOpError(
"'")
148 << kind <<
"' reduction kind unsupported with " << reduxType
149 <<
" type. Only supported type is 'i32'.";
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'.";
163LogicalResult NVVM::ClusterLaunchControlQueryCancelOp::verify() {
164 switch (getQueryType()) {
165 case NVVM::ClusterLaunchControlQueryType::IS_CANCELED:
167 return emitOpError(
"is_canceled query type returns an i1");
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");
181LogicalResult ClusterLaunchControlQueryCancelOp::inferReturnTypes(
182 MLIRContext *context, std::optional<Location> location,
183 ClusterLaunchControlQueryCancelOp::Adaptor adaptor,
186 adaptor.getQueryType() == NVVM::ClusterLaunchControlQueryType::IS_CANCELED
189 inferredReturnTypes.push_back(IntegerType::get(context, width));
202 return aligned ? llvm::Intrinsic::nvvm_barrier_cta_sync_aligned_count
203 : llvm::Intrinsic::nvvm_barrier_cta_sync_count;
205 return aligned ? llvm::Intrinsic::nvvm_barrier_cta_sync_aligned_all
206 : llvm::Intrinsic::nvvm_barrier_cta_sync_all;
211static llvm::Intrinsic::ID
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;
224 llvm_unreachable(
"unknown BarrierReduction kind");
229 auto thisOp = cast<NVVM::BarrierOp>(op);
230 llvm::Value *barrierId = thisOp.getBarrierId()
232 : builder.getInt32(0);
233 bool hasCount =
static_cast<bool>(thisOp.getNumberOfThreads());
234 llvm::Intrinsic::ID
id =
238 args.push_back(mt.
lookupValue(thisOp.getNumberOfThreads()));
239 return {id, std::move(args)};
244 auto thisOp = cast<NVVM::BarrierArriveOp>(op);
245 llvm::Value *barrierId = thisOp.getBarrierId()
247 : builder.getInt32(0);
248 llvm::Value *numThreads = mt.
lookupValue(thisOp.getNumberOfThreads());
249 llvm::Intrinsic::ID
id =
251 ? llvm::Intrinsic::nvvm_barrier_cta_arrive_aligned_count
252 : llvm::Intrinsic::nvvm_barrier_cta_arrive_count;
253 return {id, {barrierId, numThreads}};
258 auto thisOp = cast<NVVM::BarrierReductionOp>(op);
260 thisOp.getAligned(), thisOp.getReductionOp());
261 llvm::Value *barrierId = thisOp.getBarrierId()
263 : builder.getInt32(0);
266 builder.CreateICmpNE(mt.
lookupValue(thisOp.getReductionPredicate()),
267 builder.getInt32(0))};
268 return {id, std::move(args)};
273 auto curOp = cast<NVVM::ClusterLaunchControlTryCancelOp>(op);
275 args.push_back(mt.
lookupValue(curOp.getSmemAddress()));
276 args.push_back(mt.
lookupValue(curOp.getMbarrier()));
278 llvm::Intrinsic::ID intrinsicID =
281 nvvm_clusterlaunchcontrol_try_cancel_async_multicast_shared
282 : llvm::Intrinsic::nvvm_clusterlaunchcontrol_try_cancel_async_shared;
284 return {intrinsicID, args};
287NVVM::IDArgPair ClusterLaunchControlQueryCancelOp::getIntrinsicIDAndArgs(
289 auto curOp = cast<NVVM::ClusterLaunchControlQueryCancelOp>(op);
291 args.push_back(mt.
lookupValue(curOp.getTryCancelResponse()));
293 llvm::Intrinsic::ID intrinsicID;
295 switch (curOp.getQueryType()) {
296 case NVVM::ClusterLaunchControlQueryType::IS_CANCELED:
298 llvm::Intrinsic::nvvm_clusterlaunchcontrol_query_cancel_is_canceled;
300 case NVVM::ClusterLaunchControlQueryType::GET_FIRST_CTA_ID_X:
301 intrinsicID = llvm::Intrinsic::
302 nvvm_clusterlaunchcontrol_query_cancel_get_first_ctaid_x;
304 case NVVM::ClusterLaunchControlQueryType::GET_FIRST_CTA_ID_Y:
305 intrinsicID = llvm::Intrinsic::
306 nvvm_clusterlaunchcontrol_query_cancel_get_first_ctaid_y;
308 case NVVM::ClusterLaunchControlQueryType::GET_FIRST_CTA_ID_Z:
309 intrinsicID = llvm::Intrinsic::
310 nvvm_clusterlaunchcontrol_query_cancel_get_first_ctaid_z;
313 return {intrinsicID, args};
316#define GET_OP_CLASSES
317#include "mlir/Dialect/LLVMIR/NVVMSyncOps.cpp.inc"
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.
Operation is the basic unit of execution within MLIR.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
bool isInteger() const
Return true if this is an integer type (with the specified width).
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.