MLIR 24.0.0git
NVVMMBarrierOps.cpp
Go to the documentation of this file.
1//===- NVVMMBarrierOps.cpp - NVVM dialect mbarrier 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 `MBarrier` of the NVVM dialect.
10//
11//===----------------------------------------------------------------------===//
12
14
15#include "IR/NVVMOps.h"
17#include "mlir/IR/Diagnostics.h"
18#include "mlir/IR/MLIRContext.h"
19#include "mlir/IR/Operation.h"
21#include "mlir/IR/Types.h"
23#include "llvm/IR/IRBuilder.h"
24#include "llvm/IR/IntrinsicsNVPTX.h"
25#include "llvm/Support/Casting.h"
26#include "llvm/Support/FormatVariadic.h"
27
28#include <optional>
29#include <string>
30#include <utility>
31
32using namespace mlir;
33using namespace mlir::NVVM;
34
35//===----------------------------------------------------------------------===//
36// Helper/Utility methods
37//===----------------------------------------------------------------------===//
38
40 return isPtrInAddrSpace(ptr, NVVMMemorySpace::Generic);
41}
42
44 return isPtrInAddrSpace(ptr, NVVMMemorySpace::SharedCluster);
45}
46
47static llvm::Value *castPtrToAddrSpace(llvm::IRBuilderBase &builder,
48 llvm::Value *ptr,
49 NVVMMemorySpace targetAS) {
50 unsigned AS = static_cast<unsigned>(targetAS);
51 return builder.CreateAddrSpaceCast(
52 ptr, llvm::PointerType::get(builder.getContext(), AS));
53}
54
55//===----------------------------------------------------------------------===//
56// Verifier methods
57//===----------------------------------------------------------------------===//
58
59static LogicalResult verifyMBarrierArriveLikeOp(Operation *op, Value addr,
60 NVVM::MemScopeKind scope,
61 Value retVal = nullptr,
62 Value multicastMask = nullptr) {
63 if (scope != NVVM::MemScopeKind::CTA && scope != NVVM::MemScopeKind::CLUSTER)
64 return op->emitError("mbarrier scope must be either CTA or Cluster");
65
66 bool isSharedCluster = isPtrInSharedClusterSpace(addr);
67 bool hasRetValue = static_cast<bool>(retVal);
68 if (isSharedCluster && hasRetValue)
69 return op->emitError(
70 "mbarrier in shared_cluster space cannot return any value");
71
72 if (multicastMask && !isSharedCluster)
73 return op->emitError("multicast requires shared::cluster address space");
74
75 return success();
76}
77
78LogicalResult MBarrierArriveOp::verify() {
79 return verifyMBarrierArriveLikeOp(getOperation(), getAddr(), getScope(),
80 getRes(), getMulticastMask());
81}
82
83LogicalResult MBarrierArriveDropOp::verify() {
84 return verifyMBarrierArriveLikeOp(getOperation(), getAddr(), getScope(),
85 getRes(), getMulticastMask());
86}
87
88LogicalResult MBarrierArriveExpectTxOp::verify() {
89 // The inline-ptx version of this Op does not support all features.
90 // With predicate, this Op lowers to inline-ptx. So, verify and
91 // error-out if there are unsupported features.
92 if (getPredicate()) {
93 if (getScope() != NVVM::MemScopeKind::CTA)
94 return emitError("mbarrier scope must be CTA when using predicate");
95
96 if (isPtrInSharedClusterSpace(getAddr()))
97 return emitError("mbarrier in shared_cluster space is not supported when "
98 "using predicate");
99
100 if (getRes())
101 return emitError("return-value is not supported when using predicate");
102
103 if (getRelaxed() == true)
104 return emitError("mbarrier with relaxed semantics is not supported when "
105 "using predicate");
106 }
107 return verifyMBarrierArriveLikeOp(getOperation(), getAddr(), getScope(),
108 getRes(), getMulticastMask());
109}
110
111LogicalResult MBarrierArriveDropExpectTxOp::verify() {
112 return verifyMBarrierArriveLikeOp(getOperation(), getAddr(), getScope(),
113 getRes(), getMulticastMask());
114}
115
116//===----------------------------------------------------------------------===//
117// inferReturnTypes for mbarrier arrive-like ops
118//===----------------------------------------------------------------------===//
119
120/// Only shared_cluster (ptr<7>) produces zero results; all other address
121/// spaces (including generic) return i64.
122static LogicalResult
124 SmallVectorImpl<Type> &inferredReturnTypes) {
125 if (!isPtrInSharedClusterSpace(addr))
126 inferredReturnTypes.push_back(IntegerType::get(context, 64));
127 return success();
128}
129
130LogicalResult
131MBarrierArriveOp::inferReturnTypes(MLIRContext *context,
132 std::optional<Location> location,
133 MBarrierArriveOp::Adaptor adaptor,
134 SmallVectorImpl<Type> &inferredReturnTypes) {
135 return inferMBarrierArriveResultTypes(context, adaptor.getAddr(),
136 inferredReturnTypes);
137}
138
139LogicalResult MBarrierArriveDropOp::inferReturnTypes(
140 MLIRContext *context, std::optional<Location> location,
141 MBarrierArriveDropOp::Adaptor adaptor,
142 SmallVectorImpl<Type> &inferredReturnTypes) {
143 return inferMBarrierArriveResultTypes(context, adaptor.getAddr(),
144 inferredReturnTypes);
145}
146
147LogicalResult MBarrierArriveExpectTxOp::inferReturnTypes(
148 MLIRContext *context, std::optional<Location> location,
149 MBarrierArriveExpectTxOp::Adaptor adaptor,
150 SmallVectorImpl<Type> &inferredReturnTypes) {
151 // Predicate forces no return value (inline PTX path).
152 // Note: predicate + shared_cluster is rejected by the verifier separately.
153 if (adaptor.getPredicate())
154 return success();
155 return inferMBarrierArriveResultTypes(context, adaptor.getAddr(),
156 inferredReturnTypes);
157}
158
159LogicalResult MBarrierArriveDropExpectTxOp::inferReturnTypes(
160 MLIRContext *context, std::optional<Location> location,
161 MBarrierArriveDropExpectTxOp::Adaptor adaptor,
162 SmallVectorImpl<Type> &inferredReturnTypes) {
163 return inferMBarrierArriveResultTypes(context, adaptor.getAddr(),
164 inferredReturnTypes);
165}
166
167/// For ops with optional results, allow the user to omit the result even when
168/// inference would produce one. This preserves backward compatibility: the
169/// result can be silently discarded (e.g., for fire-and-forget arrive ops).
171 TypeRange actual) {
172 if (actual.empty())
173 return true;
174 return inferred == actual;
175}
176
177bool MBarrierArriveOp::isCompatibleReturnTypes(TypeRange l, TypeRange r) {
179}
180
181bool MBarrierArriveDropOp::isCompatibleReturnTypes(TypeRange l, TypeRange r) {
183}
184
185bool MBarrierArriveExpectTxOp::isCompatibleReturnTypes(TypeRange l,
186 TypeRange r) {
188}
189
190bool MBarrierArriveDropExpectTxOp::isCompatibleReturnTypes(TypeRange l,
191 TypeRange r) {
193}
194
195LogicalResult MBarrierExpectTxOp::verify() {
196 return verifyMBarrierArriveLikeOp(getOperation(), getAddr(), getScope(),
197 /*retVal=*/Value{}, getMulticastMask());
198}
199
200LogicalResult MBarrierCompleteTxOp::verify() {
201 return verifyMBarrierArriveLikeOp(getOperation(), getAddr(), getScope(),
202 /*retVal=*/Value{}, getMulticastMask());
203}
204
205LogicalResult MBarrierTestWaitOp::verify() {
206 return verifyMBarrierArriveLikeOp(getOperation(), getAddr(), getScope());
207}
208
209LogicalResult MBarrierTryWaitOp::verify() {
210 return verifyMBarrierArriveLikeOp(getOperation(), getAddr(), getScope());
211}
212
213//===----------------------------------------------------------------------===//
214// getPtx methods
215//===----------------------------------------------------------------------===//
216
217std::string NVVM::MBarrierInitOp::getPtx() {
218 std::string space = isPtrInSharedCTASpace(getAddr()) ? ".shared" : "";
219 // Layout v0 is the default, so it is emitted as a plain mbarrier.init.
220 std::string layout =
221 getLayout() == 1 ? std::string(".layout::v1") : std::string();
222
223 return llvm::formatv("mbarrier.init{0}{1}.b64 [%0], %1;", layout, space)
224 .str();
225}
226
227std::string NVVM::MBarrierArriveExpectTxOp::getPtx() {
228 bool isShared = isPtrInSharedCTASpace(getAddr());
229 return isShared
230 ? std::string("mbarrier.arrive.expect_tx.shared.b64 _, [%0], %1;")
231 : std::string("mbarrier.arrive.expect_tx.b64 _, [%0], %1;");
232}
233
234std::string NVVM::MBarrierTryWaitParityOp::getPtx() {
235 bool isShared = isPtrInSharedCTASpace(getAddr());
236 llvm::StringRef space = isShared ? ".shared" : "";
237
238 return llvm::formatv("{\n\t"
239 ".reg .pred P1; \n\t"
240 "LAB_WAIT: \n\t"
241 "mbarrier.try_wait.parity{0}.b64 P1, [%0], %1, %2; \n\t"
242 "@P1 bra.uni DONE; \n\t"
243 "bra.uni LAB_WAIT; \n\t"
244 "DONE: \n\t"
245 "}",
246 space);
247}
248
249//===----------------------------------------------------------------------===//
250// getIntrinsicID/getIntrinsicIDAndArgs methods
251//===----------------------------------------------------------------------===//
252
253bool MBarrierInitOp::getAsmValues(
254 RewriterBase &rewriter,
255 llvm::SmallVectorImpl<std::pair<mlir::Value, mlir::NVVM::PTXRegisterMod>>
256 &asmValues) {
257 // Add all the operands but not the attrs to the asmValues list.
258 // The layout attr is already baked into the PTX string by getPtx(), so
259 // passing it along here too would shift the operand numbering.
260 for (auto val : getOperands())
261 asmValues.push_back({val, mlir::NVVM::PTXRegisterMod::Read});
262
263 return false;
264}
265
266mlir::NVVM::IDArgPair MBarrierInitOp::getIntrinsicIDAndArgs(
267 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
268 auto thisOp = cast<NVVM::MBarrierInitOp>(op);
269
270 // The intrinsic is overloaded on the mbarrier pointer, so the address space
271 // selects the generic or shared::cta form on its own.
272 return {llvm::Intrinsic::nvvm_mbarrier_init,
273 {mt.lookupValue(thisOp.getAddr()), mt.lookupValue(thisOp.getCount()),
274 builder.getInt32(thisOp.getLayout())}};
275}
276
277mlir::NVVM::IDArgPair MBarrierInvalOp::getIntrinsicIDAndArgs(
278 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
279 auto thisOp = cast<NVVM::MBarrierInvalOp>(op);
280 bool isShared = isPtrInSharedCTASpace(thisOp.getAddr());
281 llvm::Intrinsic::ID id = isShared
282 ? llvm::Intrinsic::nvvm_mbarrier_inval_shared
283 : llvm::Intrinsic::nvvm_mbarrier_inval;
284
285 return {id, {mt.lookupValue(thisOp.getAddr())}};
286}
287
288mlir::NVVM::IDArgPair MBarrierCheckLayoutOp::getIntrinsicIDAndArgs(
289 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
290 auto thisOp = cast<NVVM::MBarrierCheckLayoutOp>(op);
291
292 return {
293 llvm::Intrinsic::nvvm_mbarrier_check_layout,
294 {mt.lookupValue(thisOp.getAddr()), builder.getInt32(thisOp.getLayout())}};
295}
296
297static void appendMBarrierMulticastArgs(Value multicastMask,
299 llvm::IRBuilderBase &builder,
301 bool hasMulticastMask = static_cast<bool>(multicastMask);
302 args.push_back(hasMulticastMask ? mt.lookupValue(multicastMask)
303 : builder.getInt32(0));
304 args.push_back(builder.getInt1(hasMulticastMask));
305}
306
307mlir::NVVM::IDArgPair MBarrierExpectTxOp::getIntrinsicIDAndArgs(
308 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
309 auto thisOp = cast<NVVM::MBarrierExpectTxOp>(op);
310
311 bool isClusterSpace = isPtrInSharedClusterSpace(thisOp.getAddr());
312 bool isClusterScope = thisOp.getScope() == NVVM::MemScopeKind::CLUSTER;
313 // bit-0: Space
314 // bit-1: Scope
315 size_t index = ((isClusterScope ? 1 : 0) << 1) | (isClusterSpace ? 1 : 0);
316
317 static constexpr llvm::Intrinsic::ID IDs[] = {
318 llvm::Intrinsic::nvvm_mbarrier_expect_tx_scope_cta_space_cta,
319 llvm::Intrinsic::nvvm_mbarrier_expect_tx_scope_cta_space_cluster,
320 llvm::Intrinsic::nvvm_mbarrier_expect_tx_scope_cluster_space_cta,
321 llvm::Intrinsic::nvvm_mbarrier_expect_tx_scope_cluster_space_cluster};
322
323 // Fill the Intrinsic Args
325 args.push_back(mt.lookupValue(thisOp.getAddr()));
326 args.push_back(mt.lookupValue(thisOp.getTxcount()));
327
328 if (isClusterSpace)
329 appendMBarrierMulticastArgs(thisOp.getMulticastMask(), mt, builder, args);
330
331 return {IDs[index], std::move(args)};
332}
333
334mlir::NVVM::IDArgPair MBarrierCompleteTxOp::getIntrinsicIDAndArgs(
335 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
336 auto thisOp = cast<NVVM::MBarrierCompleteTxOp>(op);
337
338 bool isClusterSpace = isPtrInSharedClusterSpace(thisOp.getAddr());
339 bool isClusterScope = thisOp.getScope() == NVVM::MemScopeKind::CLUSTER;
340 // bit-0: Space
341 // bit-1: Scope
342 size_t index = ((isClusterScope ? 1 : 0) << 1) | (isClusterSpace ? 1 : 0);
343
344 static constexpr llvm::Intrinsic::ID IDs[] = {
345 llvm::Intrinsic::nvvm_mbarrier_complete_tx_scope_cta_space_cta,
346 llvm::Intrinsic::nvvm_mbarrier_complete_tx_scope_cta_space_cluster,
347 llvm::Intrinsic::nvvm_mbarrier_complete_tx_scope_cluster_space_cta,
348 llvm::Intrinsic::nvvm_mbarrier_complete_tx_scope_cluster_space_cluster};
349
350 // Fill the Intrinsic Args
352 args.push_back(mt.lookupValue(thisOp.getAddr()));
353 args.push_back(mt.lookupValue(thisOp.getTxcount()));
354
355 if (isClusterSpace)
356 appendMBarrierMulticastArgs(thisOp.getMulticastMask(), mt, builder, args);
357
358 return {IDs[index], std::move(args)};
359}
360
361mlir::NVVM::IDArgPair MBarrierArriveOp::getIntrinsicIDAndArgs(
362 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
363 auto thisOp = cast<NVVM::MBarrierArriveOp>(op);
364
365 bool isClusterSpace = isPtrInSharedClusterSpace(thisOp.getAddr());
366 bool isClusterScope = thisOp.getScope() == NVVM::MemScopeKind::CLUSTER;
367 // bit-0: Space
368 // bit-1: Scope
369 size_t index = ((isClusterScope ? 1 : 0) << 1) | (isClusterSpace ? 1 : 0);
370
371 static constexpr llvm::Intrinsic::ID IDs[] = {
372 llvm::Intrinsic::nvvm_mbarrier_arrive_scope_cta_space_cta,
373 llvm::Intrinsic::nvvm_mbarrier_arrive_scope_cta_space_cluster,
374 llvm::Intrinsic::nvvm_mbarrier_arrive_scope_cluster_space_cta,
375 llvm::Intrinsic::nvvm_mbarrier_arrive_scope_cluster_space_cluster};
376 static constexpr llvm::Intrinsic::ID relaxedIDs[] = {
377 llvm::Intrinsic::nvvm_mbarrier_arrive_relaxed_scope_cta_space_cta,
378 llvm::Intrinsic::nvvm_mbarrier_arrive_relaxed_scope_cta_space_cluster,
379 llvm::Intrinsic::nvvm_mbarrier_arrive_relaxed_scope_cluster_space_cta,
380 llvm::Intrinsic::
381 nvvm_mbarrier_arrive_relaxed_scope_cluster_space_cluster};
382 auto id = thisOp.getRelaxed() ? relaxedIDs[index] : IDs[index];
383
384 // Tidy-up the Intrinsic Args
385 bool needCast = isPtrInGenericSpace(thisOp.getAddr());
386 llvm::Value *mbar = mt.lookupValue(thisOp.getAddr());
387 if (needCast)
388 mbar = castPtrToAddrSpace(builder, mbar, NVVMMemorySpace::Shared);
389
390 // We have the most basic mbarrier.arrive supported on sm_80.
391 // It supports: Space=cta, scope=cta, No relaxed, No explicit count.
392 // So, only for this combination use the legacy intrinsic.
393 bool hasCount = static_cast<bool>(thisOp.getCount());
394 if (!hasCount &&
395 (id == llvm::Intrinsic::nvvm_mbarrier_arrive_scope_cta_space_cta))
396 return {llvm::Intrinsic::nvvm_mbarrier_arrive_shared, {mbar}};
397
398 // When count is not explicitly specified, the default is 1.
399 llvm::LLVMContext &ctx = mt.getLLVMContext();
400 llvm::Value *count =
401 hasCount ? mt.lookupValue(thisOp.getCount())
402 : llvm::ConstantInt::get(llvm::Type::getInt32Ty(ctx), 1);
403
404 llvm::SmallVector<llvm::Value *> args = {mbar, count};
405 if (isClusterSpace)
406 appendMBarrierMulticastArgs(thisOp.getMulticastMask(), mt, builder, args);
407 return {id, std::move(args)};
408}
409
410mlir::NVVM::IDArgPair MBarrierArriveDropOp::getIntrinsicIDAndArgs(
411 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
412 auto thisOp = cast<NVVM::MBarrierArriveDropOp>(op);
413
414 bool isClusterSpace = isPtrInSharedClusterSpace(thisOp.getAddr());
415 bool isClusterScope = thisOp.getScope() == NVVM::MemScopeKind::CLUSTER;
416 // bit-0: Space
417 // bit-1: Scope
418 size_t index = ((isClusterScope ? 1 : 0) << 1) | (isClusterSpace ? 1 : 0);
419
420 static constexpr llvm::Intrinsic::ID IDs[] = {
421 llvm::Intrinsic::nvvm_mbarrier_arrive_drop_scope_cta_space_cta,
422 llvm::Intrinsic::nvvm_mbarrier_arrive_drop_scope_cta_space_cluster,
423 llvm::Intrinsic::nvvm_mbarrier_arrive_drop_scope_cluster_space_cta,
424 llvm::Intrinsic::nvvm_mbarrier_arrive_drop_scope_cluster_space_cluster};
425 static constexpr llvm::Intrinsic::ID relaxedIDs[] = {
426 llvm::Intrinsic::nvvm_mbarrier_arrive_drop_relaxed_scope_cta_space_cta,
427 llvm::Intrinsic::
428 nvvm_mbarrier_arrive_drop_relaxed_scope_cta_space_cluster,
429 llvm::Intrinsic::
430 nvvm_mbarrier_arrive_drop_relaxed_scope_cluster_space_cta,
431 llvm::Intrinsic::
432 nvvm_mbarrier_arrive_drop_relaxed_scope_cluster_space_cluster};
433 auto id = thisOp.getRelaxed() ? relaxedIDs[index] : IDs[index];
434
435 // Tidy-up the Intrinsic Args
436 bool needCast = isPtrInGenericSpace(thisOp.getAddr());
437 llvm::Value *mbar = mt.lookupValue(thisOp.getAddr());
438 if (needCast)
439 mbar = castPtrToAddrSpace(builder, mbar, NVVMMemorySpace::Shared);
440
441 // We have the most basic mbarrier.arrive_drop supported on sm_80.
442 // It supports: Space=cta, scope=cta, No relaxed, No explicit count.
443 // So, only for this combination use the legacy intrinsic.
444 bool hasCount = static_cast<bool>(thisOp.getCount());
445 if (!hasCount &&
446 (id == llvm::Intrinsic::nvvm_mbarrier_arrive_drop_scope_cta_space_cta))
447 return {llvm::Intrinsic::nvvm_mbarrier_arrive_drop_shared, {mbar}};
448
449 // When count is not explicitly specified, the default is 1.
450 llvm::LLVMContext &ctx = mt.getLLVMContext();
451 llvm::Value *count =
452 hasCount ? mt.lookupValue(thisOp.getCount())
453 : llvm::ConstantInt::get(llvm::Type::getInt32Ty(ctx), 1);
454
455 llvm::SmallVector<llvm::Value *> args = {mbar, count};
456 if (isClusterSpace)
457 appendMBarrierMulticastArgs(thisOp.getMulticastMask(), mt, builder, args);
458 return {id, std::move(args)};
459}
460
461bool MBarrierArriveExpectTxOp::getAsmValues(
462 RewriterBase &rewriter,
463 llvm::SmallVectorImpl<std::pair<mlir::Value, mlir::NVVM::PTXRegisterMod>>
464 &asmValues) {
465 // Add all the operands but not the attrs to the asmValues list.
466 // The attrs here are used to generate the right variants for
467 // intrinsics-lowering. So, we ignore them while generating inline-PTX.
468 for (auto val : getOperands())
469 asmValues.push_back({val, mlir::NVVM::PTXRegisterMod::Read});
470
471 return false;
472}
473
474mlir::NVVM::IDArgPair MBarrierArriveExpectTxOp::getIntrinsicIDAndArgs(
475 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
476 auto thisOp = cast<NVVM::MBarrierArriveExpectTxOp>(op);
477
478 bool isClusterSpace = isPtrInSharedClusterSpace(thisOp.getAddr());
479 bool isClusterScope = thisOp.getScope() == NVVM::MemScopeKind::CLUSTER;
480 // bit-0: Space
481 // bit-1: Scope
482 size_t index = ((isClusterScope ? 1 : 0) << 1) | (isClusterSpace ? 1 : 0);
483
484 // clang-format off
485 static constexpr llvm::Intrinsic::ID IDs[] = {
486 llvm::Intrinsic::nvvm_mbarrier_arrive_expect_tx_scope_cta_space_cta,
487 llvm::Intrinsic::nvvm_mbarrier_arrive_expect_tx_scope_cta_space_cluster,
488 llvm::Intrinsic::nvvm_mbarrier_arrive_expect_tx_scope_cluster_space_cta,
489 llvm::Intrinsic::nvvm_mbarrier_arrive_expect_tx_scope_cluster_space_cluster};
490 static constexpr llvm::Intrinsic::ID relaxedIDs[] = {
491 llvm::Intrinsic::nvvm_mbarrier_arrive_expect_tx_relaxed_scope_cta_space_cta,
492 llvm::Intrinsic::nvvm_mbarrier_arrive_expect_tx_relaxed_scope_cta_space_cluster,
493 llvm::Intrinsic::nvvm_mbarrier_arrive_expect_tx_relaxed_scope_cluster_space_cta,
494 llvm::Intrinsic::nvvm_mbarrier_arrive_expect_tx_relaxed_scope_cluster_space_cluster};
495 // clang-format on
496 auto id = thisOp.getRelaxed() ? relaxedIDs[index] : IDs[index];
497
498 // Tidy-up the Intrinsic Args
499 llvm::Value *txcount = mt.lookupValue(thisOp.getTxcount());
500 llvm::Value *mbar = mt.lookupValue(thisOp.getAddr());
501 bool needCast = isPtrInGenericSpace(thisOp.getAddr());
502 if (needCast)
503 mbar = castPtrToAddrSpace(builder, mbar, NVVMMemorySpace::Shared);
504
505 llvm::SmallVector<llvm::Value *> args = {mbar, txcount};
506 if (isClusterSpace)
507 appendMBarrierMulticastArgs(thisOp.getMulticastMask(), mt, builder, args);
508 return {id, std::move(args)};
509}
510
511mlir::NVVM::IDArgPair MBarrierArriveDropExpectTxOp::getIntrinsicIDAndArgs(
512 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
513 auto thisOp = cast<NVVM::MBarrierArriveDropExpectTxOp>(op);
514
515 bool isClusterSpace = isPtrInSharedClusterSpace(thisOp.getAddr());
516 bool isClusterScope = thisOp.getScope() == NVVM::MemScopeKind::CLUSTER;
517 // bit-0: Space
518 // bit-1: Scope
519 size_t index = ((isClusterScope ? 1 : 0) << 1) | (isClusterSpace ? 1 : 0);
520
521 // clang-format off
522 static constexpr llvm::Intrinsic::ID IDs[] = {
523 llvm::Intrinsic::nvvm_mbarrier_arrive_drop_expect_tx_scope_cta_space_cta,
524 llvm::Intrinsic::nvvm_mbarrier_arrive_drop_expect_tx_scope_cta_space_cluster,
525 llvm::Intrinsic::nvvm_mbarrier_arrive_drop_expect_tx_scope_cluster_space_cta,
526 llvm::Intrinsic::nvvm_mbarrier_arrive_drop_expect_tx_scope_cluster_space_cluster};
527 static constexpr llvm::Intrinsic::ID relaxedIDs[] = {
528 llvm::Intrinsic::nvvm_mbarrier_arrive_drop_expect_tx_relaxed_scope_cta_space_cta,
529 llvm::Intrinsic::nvvm_mbarrier_arrive_drop_expect_tx_relaxed_scope_cta_space_cluster,
530 llvm::Intrinsic::nvvm_mbarrier_arrive_drop_expect_tx_relaxed_scope_cluster_space_cta,
531 llvm::Intrinsic::nvvm_mbarrier_arrive_drop_expect_tx_relaxed_scope_cluster_space_cluster};
532 // clang-format on
533 auto id = thisOp.getRelaxed() ? relaxedIDs[index] : IDs[index];
534
535 // Tidy-up the Intrinsic Args
536 llvm::Value *txcount = mt.lookupValue(thisOp.getTxcount());
537 llvm::Value *mbar = mt.lookupValue(thisOp.getAddr());
538 bool needCast = isPtrInGenericSpace(thisOp.getAddr());
539 if (needCast)
540 mbar = castPtrToAddrSpace(builder, mbar, NVVMMemorySpace::Shared);
541
542 llvm::SmallVector<llvm::Value *> args = {mbar, txcount};
543 if (isClusterSpace)
544 appendMBarrierMulticastArgs(thisOp.getMulticastMask(), mt, builder, args);
545 return {id, std::move(args)};
546}
547
548mlir::NVVM::IDArgPair MBarrierArriveNocompleteOp::getIntrinsicIDAndArgs(
549 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
550 auto thisOp = cast<NVVM::MBarrierArriveNocompleteOp>(op);
551 bool isShared = isPtrInSharedCTASpace(thisOp.getAddr());
552 llvm::Intrinsic::ID id =
553 isShared ? llvm::Intrinsic::nvvm_mbarrier_arrive_noComplete_shared
554 : llvm::Intrinsic::nvvm_mbarrier_arrive_noComplete;
555 // Fill the Intrinsic Args
557 args.push_back(mt.lookupValue(thisOp.getAddr()));
558 args.push_back(mt.lookupValue(thisOp.getCount()));
559
560 return {id, std::move(args)};
561}
562
563mlir::NVVM::IDArgPair MBarrierArriveDropNocompleteOp::getIntrinsicIDAndArgs(
564 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
565 auto thisOp = cast<NVVM::MBarrierArriveDropNocompleteOp>(op);
566 bool isShared = isPtrInSharedCTASpace(thisOp.getAddr());
567 llvm::Intrinsic::ID id =
568 isShared ? llvm::Intrinsic::nvvm_mbarrier_arrive_drop_noComplete_shared
569 : llvm::Intrinsic::nvvm_mbarrier_arrive_drop_noComplete;
570 // Fill the Intrinsic Args
572 args.push_back(mt.lookupValue(thisOp.getAddr()));
573 args.push_back(mt.lookupValue(thisOp.getCount()));
574
575 return {id, std::move(args)};
576}
577
578mlir::NVVM::IDArgPair MBarrierTestWaitOp::getIntrinsicIDAndArgs(
579 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
580 auto thisOp = cast<NVVM::MBarrierTestWaitOp>(op);
581 bool isPhaseParity = thisOp.getStateOrPhase().getType().isInteger(32);
582 bool isClusterScope = thisOp.getScope() == NVVM::MemScopeKind::CLUSTER;
583 // bit-0: isPhaseParity
584 // bit-1: Scope
585 size_t index = ((isClusterScope ? 1 : 0) << 1) | (isPhaseParity ? 1 : 0);
586
587 // clang-format off
588 static constexpr llvm::Intrinsic::ID IDs[] = {
589 llvm::Intrinsic::nvvm_mbarrier_test_wait_scope_cta_space_cta,
590 llvm::Intrinsic::nvvm_mbarrier_test_wait_parity_scope_cta_space_cta,
591 llvm::Intrinsic::nvvm_mbarrier_test_wait_scope_cluster_space_cta,
592 llvm::Intrinsic::nvvm_mbarrier_test_wait_parity_scope_cluster_space_cta};
593 static constexpr llvm::Intrinsic::ID relaxedIDs[] = {
594 llvm::Intrinsic::nvvm_mbarrier_test_wait_relaxed_scope_cta_space_cta,
595 llvm::Intrinsic::nvvm_mbarrier_test_wait_parity_relaxed_scope_cta_space_cta,
596 llvm::Intrinsic::nvvm_mbarrier_test_wait_relaxed_scope_cluster_space_cta,
597 llvm::Intrinsic::nvvm_mbarrier_test_wait_parity_relaxed_scope_cluster_space_cta};
598 // clang-format on
599 auto id = thisOp.getRelaxed() ? relaxedIDs[index] : IDs[index];
600
601 // Tidy-up the Intrinsic Args
602 llvm::Value *mbar = mt.lookupValue(thisOp.getAddr());
603 llvm::Value *input = mt.lookupValue(thisOp.getStateOrPhase());
604 bool needCast = isPtrInGenericSpace(thisOp.getAddr());
605 if (needCast)
606 mbar = castPtrToAddrSpace(builder, mbar, NVVMMemorySpace::Shared);
607
608 return {id, {mbar, input}};
609}
610
611mlir::NVVM::IDArgPair MBarrierTryWaitOp::getIntrinsicIDAndArgs(
612 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
613 auto thisOp = cast<NVVM::MBarrierTryWaitOp>(op);
614 bool isPhaseParity = thisOp.getStateOrPhase().getType().isInteger(32);
615 bool isClusterScope = thisOp.getScope() == NVVM::MemScopeKind::CLUSTER;
616 bool hasTicks = static_cast<bool>(thisOp.getTicks());
617 // bit-0: isPhaseParity
618 // bit-1: Scope
619 // bit-2: hasTicks
620 size_t index = ((hasTicks ? 1 : 0) << 2) | ((isClusterScope ? 1 : 0) << 1) |
621 (isPhaseParity ? 1 : 0);
622
623 // clang-format off
624 static constexpr llvm::Intrinsic::ID IDs[] = {
625 llvm::Intrinsic::nvvm_mbarrier_try_wait_scope_cta_space_cta,
626 llvm::Intrinsic::nvvm_mbarrier_try_wait_parity_scope_cta_space_cta,
627 llvm::Intrinsic::nvvm_mbarrier_try_wait_scope_cluster_space_cta,
628 llvm::Intrinsic::nvvm_mbarrier_try_wait_parity_scope_cluster_space_cta,
629 llvm::Intrinsic::nvvm_mbarrier_try_wait_tl_scope_cta_space_cta,
630 llvm::Intrinsic::nvvm_mbarrier_try_wait_parity_tl_scope_cta_space_cta,
631 llvm::Intrinsic::nvvm_mbarrier_try_wait_tl_scope_cluster_space_cta,
632 llvm::Intrinsic::nvvm_mbarrier_try_wait_parity_tl_scope_cluster_space_cta};
633 static constexpr llvm::Intrinsic::ID relaxedIDs[] = {
634 llvm::Intrinsic::nvvm_mbarrier_try_wait_relaxed_scope_cta_space_cta,
635 llvm::Intrinsic::nvvm_mbarrier_try_wait_parity_relaxed_scope_cta_space_cta,
636 llvm::Intrinsic::nvvm_mbarrier_try_wait_relaxed_scope_cluster_space_cta,
637 llvm::Intrinsic::nvvm_mbarrier_try_wait_parity_relaxed_scope_cluster_space_cta,
638 llvm::Intrinsic::nvvm_mbarrier_try_wait_tl_relaxed_scope_cta_space_cta,
639 llvm::Intrinsic::nvvm_mbarrier_try_wait_parity_tl_relaxed_scope_cta_space_cta,
640 llvm::Intrinsic::nvvm_mbarrier_try_wait_tl_relaxed_scope_cluster_space_cta,
641 llvm::Intrinsic::nvvm_mbarrier_try_wait_parity_tl_relaxed_scope_cluster_space_cta};
642 // clang-format on
643 auto id = thisOp.getRelaxed() ? relaxedIDs[index] : IDs[index];
644
645 // Tidy-up the mbarrier pointer
646 llvm::Value *mbar = mt.lookupValue(thisOp.getAddr());
647 bool needCast = isPtrInGenericSpace(thisOp.getAddr());
648 if (needCast)
649 mbar = castPtrToAddrSpace(builder, mbar, NVVMMemorySpace::Shared);
650
651 // Fill the Intrinsic Args
653 args.push_back(mbar);
654 args.push_back(mt.lookupValue(thisOp.getStateOrPhase()));
655 if (hasTicks)
656 args.push_back(mt.lookupValue(thisOp.getTicks()));
657
658 return {id, std::move(args)};
659}
660
661mlir::NVVM::IDArgPair CpAsyncMBarrierArriveOp::getIntrinsicIDAndArgs(
662 Operation &op, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder) {
663 auto thisOp = cast<NVVM::CpAsyncMBarrierArriveOp>(op);
664 bool isShared = isPtrInSharedCTASpace(thisOp.getAddr());
665
666 llvm::Intrinsic::ID id;
667 if (thisOp.getNoinc()) {
668 id = isShared ? llvm::Intrinsic::nvvm_cp_async_mbarrier_arrive_noinc_shared
669 : llvm::Intrinsic::nvvm_cp_async_mbarrier_arrive_noinc;
670 } else {
671 id = isShared ? llvm::Intrinsic::nvvm_cp_async_mbarrier_arrive_shared
672 : llvm::Intrinsic::nvvm_cp_async_mbarrier_arrive;
673 }
674
675 return {id, {mt.lookupValue(thisOp.getAddr())}};
676}
677
678#define GET_OP_CLASSES
679#include "mlir/Dialect/LLVMIR/NVVMMBarrierOps.cpp.inc"
return success()
static bool isCompatibleReturnTypesOptionalResult(TypeRange inferred, TypeRange actual)
For ops with optional results, allow the user to omit the result even when inference would produce on...
static llvm::Value * castPtrToAddrSpace(llvm::IRBuilderBase &builder, llvm::Value *ptr, NVVMMemorySpace targetAS)
static bool isPtrInSharedClusterSpace(mlir::Value ptr)
static void appendMBarrierMulticastArgs(Value multicastMask, LLVM::ModuleTranslation &mt, llvm::IRBuilderBase &builder, SmallVectorImpl< llvm::Value * > &args)
static LogicalResult verifyMBarrierArriveLikeOp(Operation *op, Value addr, NVVM::MemScopeKind scope, Value retVal=nullptr, Value multicastMask=nullptr)
static bool isPtrInGenericSpace(mlir::Value ptr)
static LogicalResult inferMBarrierArriveResultTypes(MLIRContext *context, Value addr, SmallVectorImpl< Type > &inferredReturnTypes)
Only shared_cluster (ptr<7>) produces zero results; all other address spaces (including generic) retu...
Implementation class for module translation.
llvm::Value * lookupValue(Value value) const
Finds an LLVM IR value corresponding to the given MLIR value.
llvm::LLVMContext & getLLVMContext() const
Returns the LLVM context in which the IR is being constructed.
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
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
This class provides an abstraction over the various different ranges of value types.
Definition TypeRange.h:40
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
@ Read
Read register with no modifier.
bool isPtrInAddrSpace(mlir::Value ptr, NVVMMemorySpace targetAS)
Returns whether ptr is a pointer into the address space targetAS.
Definition NVVMOps.h:29
std::pair< llvm::Intrinsic::ID, llvm::SmallVector< llvm::Value * > > IDArgPair
A pair type of LLVM's Intrinsic ID and args (which are llvm values).
bool isPtrInSharedCTASpace(mlir::Value ptr)
Returns whether ptr is a pointer into the shared memory of the CTA.
Definition NVVMOps.h:35
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.