Skip to content

Commit c80d759

Browse files
committed
[mlir][NVVM] Add nvvm.membar operation
1 parent ebeb36b commit c80d759

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

mlir/include/mlir/Dialect/LLVMIR/NVVMOps.td

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,39 @@ def NVVM_FenceProxyAcquireOp : NVVM_Op<"fence.proxy.acquire">,
12361236
let hasVerifier = 1;
12371237
}
12381238

1239+
// Attrs describing the level of the Memory Operation
1240+
def MemLevelCTA : I32EnumAttrCase<"CTA", 0, "cta">;
1241+
def MemLevelGL : I32EnumAttrCase<"GL", 1, "gl">;
1242+
def MemLevelSys : I32EnumAttrCase<"SYS", 2, "sys">;
1243+
1244+
def MemLevelKind
1245+
: I32EnumAttr<
1246+
"MemLevelKind",
1247+
"NVVM Memory Level kind", [MemLevelCTA, MemLevelGL, MemLevelSys]> {
1248+
let genSpecializedAttr = 0;
1249+
let cppNamespace = "::mlir::NVVM";
1250+
}
1251+
def MemLevelKindAttr : EnumAttr<NVVM_Dialect, MemLevelKind, "mem_level"> {
1252+
let assemblyFormat = "`<` $value `>`";
1253+
}
1254+
1255+
def NVVM_MembarOp : NVVM_Op<"membar">,
1256+
Arguments<(ins MemLevelKindAttr:$level)> {
1257+
let summary = "Memory barrier operation";
1258+
let description = [{
1259+
`member` operation guarantees that prior memory accesses requested by this
1260+
thread are performed at the specified `level`, before later memory
1261+
operations requested by this thread following the membar instruction.
1262+
1263+
[For more information, see PTX ISA](https://docs.nvidia.com/cuda/parallel-thread-execution/#parallel-synchronization-and-communication-instructions-membar)
1264+
}];
1265+
1266+
let assemblyFormat = "$level attr-dict";
1267+
let llvmBuilder = [{
1268+
createIntrinsicCall(builder, getMembarLevelID($level), {});
1269+
}];
1270+
}
1271+
12391272
def NVVM_FenceProxyReleaseOp : NVVM_Op<"fence.proxy.release">,
12401273
Arguments<(ins MemScopeKindAttr:$scope,
12411274
DefaultValuedAttr<ProxyKindAttr,

mlir/lib/Target/LLVMIR/Dialect/NVVM/NVVMToLLVMIRTranslation.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,21 @@ static unsigned getUnidirectionalFenceProxyID(NVVM::ProxyKind fromProxy,
291291
llvm_unreachable("Unsupported proxy kinds");
292292
}
293293

294+
static unsigned getMembarLevelID(NVVM::MemLevelKind level) {
295+
switch (level) {
296+
case NVVM::MemLevelKind::CTA: {
297+
return llvm::Intrinsic::nvvm_membar_cta;
298+
}
299+
case NVVM::MemLevelKind::GL: {
300+
return llvm::Intrinsic::nvvm_membar_gl;
301+
}
302+
case NVVM::MemLevelKind::SYS: {
303+
return llvm::Intrinsic::nvvm_membar_sys;
304+
}
305+
}
306+
llvm_unreachable("Unknown level for memory barrier");
307+
}
308+
294309
#define TCGEN05LD(SHAPE, NUM) llvm::Intrinsic::nvvm_tcgen05_ld_##SHAPE##_##NUM
295310

296311
static llvm::Intrinsic::ID

mlir/test/Target/LLVMIR/nvvmir.mlir

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,3 +975,16 @@ llvm.func @nanosleep() {
975975
nvvm.nanosleep 4000
976976
llvm.return
977977
}
978+
979+
// -----
980+
981+
// CHECK-lABEL: @memorybarrier()
982+
llvm.func @memorybarrier() {
983+
// CHECK: call void @llvm.nvvm.membar.cta()
984+
nvvm.membar #nvvm.mem_level<cta>
985+
// CHECK: call void @llvm.nvvm.membar.gl()
986+
nvvm.membar #nvvm.mem_level<gl>
987+
// CHECK: call void @llvm.nvvm.membar.sys()
988+
nvvm.membar #nvvm.mem_level<sys>
989+
llvm.return
990+
}

0 commit comments

Comments
 (0)