Skip to content

Commit

Permalink
[MLIR][NVVM] Add support for mapa MLIR Ops (#124514)
Browse files Browse the repository at this point in the history
Adds `mapa` and `mapa.shared.cluster` MLIR Ops to generate mapa
instructions.

`mapa` - Map the address of the shared variable in the target CTA.

- `mapa` - source is a register containing generic address pointing to
shared memory.
- `mapa.shared.cluster` - source is a shared memory variable or a
register containing a valid shared memory address.

PTX Spec Reference:

https://docs.nvidia.com/cuda/parallel-thread-execution/#data-movement-and-conversion-instructions-mapa
  • Loading branch information
Wolfram70 authored Jan 30, 2025
1 parent 1f38d38 commit ab9e447
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
22 changes: 22 additions & 0 deletions mlir/include/mlir/Dialect/LLVMIR/NVVMOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -2539,6 +2539,28 @@ def NVVM_GriddepcontrolLaunchDependentsOp
}];
}

//===----------------------------------------------------------------------===//
// NVVM Mapa Op
//===----------------------------------------------------------------------===//

def NVVM_MapaOp: NVVM_Op<"mapa",
[TypesMatchWith<"`res` and `a` should have the same type",
"a", "res", "$_self">]> {
let results = (outs AnyTypeOf<[LLVM_PointerGeneric, LLVM_PointerShared]>:$res);
let arguments = (ins AnyTypeOf<[LLVM_PointerGeneric, LLVM_PointerShared]>:$a, I32:$b);

string llvmBuilder = [{
int addrSpace = llvm::cast<LLVMPointerType>(op.getA().getType()).getAddressSpace();

bool isSharedMemory = addrSpace == NVVM::NVVMMemorySpace::kSharedMemorySpace;

auto intId = isSharedMemory? llvm::Intrinsic::nvvm_mapa_shared_cluster : llvm::Intrinsic::nvvm_mapa;
$res = createIntrinsicCall(builder, intId, {$a, $b});
}];

let assemblyFormat = "$a`,` $b attr-dict `:` type($a) `->` type($res)";
}

def NVVM_Exit : NVVM_Op<"exit"> {
let summary = "Exit Op";
let description = [{
Expand Down
8 changes: 8 additions & 0 deletions mlir/test/Dialect/LLVMIR/invalid.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,14 @@ func.func @cp_async(%arg0: !llvm.ptr<3>, %arg1: !llvm.ptr<1>) {

// -----

func.func @mapa(%a: !llvm.ptr, %b : i32) {
// expected-error @below {{`res` and `a` should have the same type}}
%0 = nvvm.mapa %a, %b: !llvm.ptr -> !llvm.ptr<3>
return
}

// -----

func.func @gep_struct_variable(%arg0: !llvm.ptr, %arg1: i32, %arg2: i32) {
// expected-error @below {{op expected index 1 indexing a struct to be constant}}
llvm.getelementptr %arg0[%arg1, %arg1] : (!llvm.ptr, i32, i32) -> !llvm.ptr, !llvm.struct<(i32)>
Expand Down
9 changes: 9 additions & 0 deletions mlir/test/Dialect/LLVMIR/nvvm.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,15 @@ func.func @griddepcontrol_launch_dependents()
return
}

// CHECK-LABEL: @mapa
func.func @mapa(%a: !llvm.ptr, %a_shared: !llvm.ptr<3>, %b : i32) {
// CHECK: nvvm.mapa %{{.*}}
%0 = nvvm.mapa %a, %b: !llvm.ptr -> !llvm.ptr
// CHECK: nvvm.mapa %{{.*}}
%1 = nvvm.mapa %a_shared, %b: !llvm.ptr<3> -> !llvm.ptr<3>
return
}

// -----

// Just check these don't emit errors.
Expand Down
10 changes: 10 additions & 0 deletions mlir/test/Target/LLVMIR/nvvmir.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -773,3 +773,13 @@ llvm.func @nvvm_griddepcontrol_launch_dependents() {
nvvm.griddepcontrol.launch.dependents
llvm.return
}

// -----
// CHECK-LABEL: @nvvm_mapa
llvm.func @nvvm_mapa(%a: !llvm.ptr, %a_shared: !llvm.ptr<3>, %b : i32) {
// CHECK-LLVM: call ptr @llvm.nvvm.mapa(ptr %{{.*}}, i32 %{{.*}})
%0 = nvvm.mapa %a, %b: !llvm.ptr -> !llvm.ptr
// CHECK-LLVM: call ptr addrspace(3) @llvm.nvvm.mapa.shared.cluster(ptr addrspace(3) %{{.*}}, i32 %{{.*}})
%1 = nvvm.mapa %a_shared, %b: !llvm.ptr<3> -> !llvm.ptr<3>
llvm.return
}

0 comments on commit ab9e447

Please sign in to comment.