23#include "llvm/IR/IRBuilder.h"
24#include "llvm/IR/IntrinsicsNVPTX.h"
25#include "llvm/Support/Casting.h"
26#include "llvm/Support/FormatVariadic.h"
49 NVVMMemorySpace targetAS) {
50 unsigned AS =
static_cast<unsigned>(targetAS);
51 return builder.CreateAddrSpaceCast(
52 ptr, llvm::PointerType::get(builder.getContext(), AS));
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");
67 bool hasRetValue =
static_cast<bool>(retVal);
68 if (isSharedCluster && hasRetValue)
70 "mbarrier in shared_cluster space cannot return any value");
72 if (multicastMask && !isSharedCluster)
73 return op->
emitError(
"multicast requires shared::cluster address space");
78LogicalResult MBarrierArriveOp::verify() {
80 getRes(), getMulticastMask());
83LogicalResult MBarrierArriveDropOp::verify() {
85 getRes(), getMulticastMask());
88LogicalResult MBarrierArriveExpectTxOp::verify() {
93 if (getScope() != NVVM::MemScopeKind::CTA)
94 return emitError(
"mbarrier scope must be CTA when using predicate");
97 return emitError(
"mbarrier in shared_cluster space is not supported when "
101 return emitError(
"return-value is not supported when using predicate");
103 if (getRelaxed() ==
true)
104 return emitError(
"mbarrier with relaxed semantics is not supported when "
108 getRes(), getMulticastMask());
111LogicalResult MBarrierArriveDropExpectTxOp::verify() {
113 getRes(), getMulticastMask());
126 inferredReturnTypes.push_back(IntegerType::get(context, 64));
131MBarrierArriveOp::inferReturnTypes(
MLIRContext *context,
132 std::optional<Location> location,
133 MBarrierArriveOp::Adaptor adaptor,
136 inferredReturnTypes);
139LogicalResult MBarrierArriveDropOp::inferReturnTypes(
140 MLIRContext *context, std::optional<Location> location,
141 MBarrierArriveDropOp::Adaptor adaptor,
144 inferredReturnTypes);
147LogicalResult MBarrierArriveExpectTxOp::inferReturnTypes(
148 MLIRContext *context, std::optional<Location> location,
149 MBarrierArriveExpectTxOp::Adaptor adaptor,
153 if (adaptor.getPredicate())
156 inferredReturnTypes);
159LogicalResult MBarrierArriveDropExpectTxOp::inferReturnTypes(
160 MLIRContext *context, std::optional<Location> location,
161 MBarrierArriveDropExpectTxOp::Adaptor adaptor,
164 inferredReturnTypes);
174 return inferred == actual;
185bool MBarrierArriveExpectTxOp::isCompatibleReturnTypes(
TypeRange l,
190bool MBarrierArriveDropExpectTxOp::isCompatibleReturnTypes(
TypeRange l,
195LogicalResult MBarrierExpectTxOp::verify() {
197 Value{}, getMulticastMask());
200LogicalResult MBarrierCompleteTxOp::verify() {
202 Value{}, getMulticastMask());
205LogicalResult MBarrierTestWaitOp::verify() {
209LogicalResult MBarrierTryWaitOp::verify() {
217std::string NVVM::MBarrierInitOp::getPtx() {
221 getLayout() == 1 ? std::string(
".layout::v1") : std::string();
223 return llvm::formatv(
"mbarrier.init{0}{1}.b64 [%0], %1;", layout, space)
227std::string NVVM::MBarrierArriveExpectTxOp::getPtx() {
230 ? std::string(
"mbarrier.arrive.expect_tx.shared.b64 _, [%0], %1;")
231 : std::string(
"mbarrier.arrive.expect_tx.b64 _, [%0], %1;");
234std::string NVVM::MBarrierTryWaitParityOp::getPtx() {
236 llvm::StringRef space = isShared ?
".shared" :
"";
238 return llvm::formatv(
"{\n\t"
239 ".reg .pred P1; \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"
253bool MBarrierInitOp::getAsmValues(
260 for (
auto val : getOperands())
268 auto thisOp = cast<NVVM::MBarrierInitOp>(op);
272 return {llvm::Intrinsic::nvvm_mbarrier_init,
274 builder.getInt32(thisOp.getLayout())}};
279 auto thisOp = cast<NVVM::MBarrierInvalOp>(op);
281 llvm::Intrinsic::ID
id = isShared
282 ? llvm::Intrinsic::nvvm_mbarrier_inval_shared
283 : llvm::Intrinsic::nvvm_mbarrier_inval;
290 auto thisOp = cast<NVVM::MBarrierCheckLayoutOp>(op);
293 llvm::Intrinsic::nvvm_mbarrier_check_layout,
294 {mt.
lookupValue(thisOp.getAddr()), builder.getInt32(thisOp.getLayout())}};
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));
309 auto thisOp = cast<NVVM::MBarrierExpectTxOp>(op);
312 bool isClusterScope = thisOp.getScope() == NVVM::MemScopeKind::CLUSTER;
315 size_t index = ((isClusterScope ? 1 : 0) << 1) | (isClusterSpace ? 1 : 0);
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};
326 args.push_back(mt.
lookupValue(thisOp.getTxcount()));
331 return {IDs[
index], std::move(args)};
336 auto thisOp = cast<NVVM::MBarrierCompleteTxOp>(op);
339 bool isClusterScope = thisOp.getScope() == NVVM::MemScopeKind::CLUSTER;
342 size_t index = ((isClusterScope ? 1 : 0) << 1) | (isClusterSpace ? 1 : 0);
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};
353 args.push_back(mt.
lookupValue(thisOp.getTxcount()));
358 return {IDs[
index], std::move(args)};
363 auto thisOp = cast<NVVM::MBarrierArriveOp>(op);
366 bool isClusterScope = thisOp.getScope() == NVVM::MemScopeKind::CLUSTER;
369 size_t index = ((isClusterScope ? 1 : 0) << 1) | (isClusterSpace ? 1 : 0);
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,
381 nvvm_mbarrier_arrive_relaxed_scope_cluster_space_cluster};
382 auto id = thisOp.getRelaxed() ? relaxedIDs[
index] : IDs[
index];
386 llvm::Value *mbar = mt.
lookupValue(thisOp.getAddr());
393 bool hasCount =
static_cast<bool>(thisOp.getCount());
395 (
id == llvm::Intrinsic::nvvm_mbarrier_arrive_scope_cta_space_cta))
396 return {llvm::Intrinsic::nvvm_mbarrier_arrive_shared, {mbar}};
402 : llvm::ConstantInt::get(llvm::Type::getInt32Ty(ctx), 1);
407 return {id, std::move(args)};
412 auto thisOp = cast<NVVM::MBarrierArriveDropOp>(op);
415 bool isClusterScope = thisOp.getScope() == NVVM::MemScopeKind::CLUSTER;
418 size_t index = ((isClusterScope ? 1 : 0) << 1) | (isClusterSpace ? 1 : 0);
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,
428 nvvm_mbarrier_arrive_drop_relaxed_scope_cta_space_cluster,
430 nvvm_mbarrier_arrive_drop_relaxed_scope_cluster_space_cta,
432 nvvm_mbarrier_arrive_drop_relaxed_scope_cluster_space_cluster};
433 auto id = thisOp.getRelaxed() ? relaxedIDs[
index] : IDs[
index];
437 llvm::Value *mbar = mt.
lookupValue(thisOp.getAddr());
444 bool hasCount =
static_cast<bool>(thisOp.getCount());
446 (
id == llvm::Intrinsic::nvvm_mbarrier_arrive_drop_scope_cta_space_cta))
447 return {llvm::Intrinsic::nvvm_mbarrier_arrive_drop_shared, {mbar}};
453 : llvm::ConstantInt::get(llvm::Type::getInt32Ty(ctx), 1);
458 return {id, std::move(args)};
461bool MBarrierArriveExpectTxOp::getAsmValues(
468 for (
auto val : getOperands())
476 auto thisOp = cast<NVVM::MBarrierArriveExpectTxOp>(op);
479 bool isClusterScope = thisOp.getScope() == NVVM::MemScopeKind::CLUSTER;
482 size_t index = ((isClusterScope ? 1 : 0) << 1) | (isClusterSpace ? 1 : 0);
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};
496 auto id = thisOp.getRelaxed() ? relaxedIDs[
index] : IDs[
index];
499 llvm::Value *txcount = mt.
lookupValue(thisOp.getTxcount());
500 llvm::Value *mbar = mt.
lookupValue(thisOp.getAddr());
508 return {id, std::move(args)};
513 auto thisOp = cast<NVVM::MBarrierArriveDropExpectTxOp>(op);
516 bool isClusterScope = thisOp.getScope() == NVVM::MemScopeKind::CLUSTER;
519 size_t index = ((isClusterScope ? 1 : 0) << 1) | (isClusterSpace ? 1 : 0);
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};
533 auto id = thisOp.getRelaxed() ? relaxedIDs[
index] : IDs[
index];
536 llvm::Value *txcount = mt.
lookupValue(thisOp.getTxcount());
537 llvm::Value *mbar = mt.
lookupValue(thisOp.getAddr());
545 return {id, std::move(args)};
550 auto thisOp = cast<NVVM::MBarrierArriveNocompleteOp>(op);
552 llvm::Intrinsic::ID
id =
553 isShared ? llvm::Intrinsic::nvvm_mbarrier_arrive_noComplete_shared
554 : llvm::Intrinsic::nvvm_mbarrier_arrive_noComplete;
560 return {id, std::move(args)};
565 auto thisOp = cast<NVVM::MBarrierArriveDropNocompleteOp>(op);
567 llvm::Intrinsic::ID
id =
568 isShared ? llvm::Intrinsic::nvvm_mbarrier_arrive_drop_noComplete_shared
569 : llvm::Intrinsic::nvvm_mbarrier_arrive_drop_noComplete;
575 return {id, std::move(args)};
580 auto thisOp = cast<NVVM::MBarrierTestWaitOp>(op);
581 bool isPhaseParity = thisOp.getStateOrPhase().getType().isInteger(32);
582 bool isClusterScope = thisOp.getScope() == NVVM::MemScopeKind::CLUSTER;
585 size_t index = ((isClusterScope ? 1 : 0) << 1) | (isPhaseParity ? 1 : 0);
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};
599 auto id = thisOp.getRelaxed() ? relaxedIDs[
index] : IDs[
index];
602 llvm::Value *mbar = mt.
lookupValue(thisOp.getAddr());
603 llvm::Value *input = mt.
lookupValue(thisOp.getStateOrPhase());
608 return {id, {mbar, input}};
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());
620 size_t index = ((hasTicks ? 1 : 0) << 2) | ((isClusterScope ? 1 : 0) << 1) |
621 (isPhaseParity ? 1 : 0);
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};
643 auto id = thisOp.getRelaxed() ? relaxedIDs[
index] : IDs[
index];
646 llvm::Value *mbar = mt.
lookupValue(thisOp.getAddr());
653 args.push_back(mbar);
654 args.push_back(mt.
lookupValue(thisOp.getStateOrPhase()));
658 return {id, std::move(args)};
663 auto thisOp = cast<NVVM::CpAsyncMBarrierArriveOp>(op);
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;
671 id = isShared ? llvm::Intrinsic::nvvm_cp_async_mbarrier_arrive_shared
672 : llvm::Intrinsic::nvvm_cp_async_mbarrier_arrive;
678#define GET_OP_CLASSES
679#include "mlir/Dialect/LLVMIR/NVVMMBarrierOps.cpp.inc"
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.
Operation is the basic unit of execution within MLIR.
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.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
@ Read
Read register with no modifier.
bool isPtrInAddrSpace(mlir::Value ptr, NVVMMemorySpace targetAS)
Returns whether ptr is a pointer into the address space targetAS.
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.
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.