Skip to content

[MLIR][NVVM] Update MLIR mapa to reflect new address space #146031

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Aug 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions mlir/include/mlir/Dialect/LLVMIR/NVVMOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -3065,9 +3065,10 @@ def NVVM_GriddepcontrolLaunchDependentsOp
//===----------------------------------------------------------------------===//

def NVVM_MapaOp: NVVM_Op<"mapa",
[TypesMatchWith<"`res` and `a` should have the same type",
"a", "res", "$_self">, NVVMRequiresSM<90>]> {
let results = (outs AnyTypeOf<[LLVM_PointerGeneric, LLVM_PointerShared]>:$res);
[InputAddressIsCombinationOf<["a", "res"],
[[LLVM_PointerShared, LLVM_PointerSharedCluster], [LLVM_PointerGeneric, LLVM_PointerGeneric]],
"Valid address-space check(or mapping) for mapa Op">, NVVMRequiresSM<90>]> {
let results = (outs AnyTypeOf<[LLVM_PointerGeneric, LLVM_PointerSharedCluster]>:$res);
let arguments = (ins AnyTypeOf<[LLVM_PointerGeneric, LLVM_PointerShared]>:$a, I32:$b);

string llvmBuilder = [{
Expand Down
45 changes: 45 additions & 0 deletions mlir/include/mlir/IR/OpBase.td
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,51 @@ class RangedTypesMatchWith<string summary, string lhsArg, string rhsArg,
string transform>
: TypesMatchWith<summary, lhsArg, rhsArg, transform, "llvm::equal">;

// Checks that each inputArg has the same type as the corresponding entry
// in allowedTypes
class InputMatchesTypes<list<string> inputArgs, list<Type> allowedTypes> :
PredOpTrait<"operands {" # !interleave(inputArgs, ", ") # "} match expected types",
!foldl(TruePred, !range(!size(inputArgs)), acc, i,
And<[acc,
SubstLeaves<"$_self", "$" # inputArgs[i] # ".getType()",
allowedTypes[i].predicate>
]>)> {
assert !eq(!size(inputArgs), !size(allowedTypes)),
"inputArgs and allowedTypes lists must have the same length";

list<string> inputArgList = inputArgs;
list<Type> allowedTypeList = allowedTypes;
}

// Checks that inputArgs match one of the allowed type combinations.
// Each combination in allowedCombinations must have the same number of types
// as there are inputArgs.
class InputAddressIsCombinationOf<list<string> inputArgs,
list<list<Type>> allowedCombinations,
string description = ""> :
PredOpTrait<!if(!empty(description),
"operands {" # !interleave(inputArgs, ", ") # "} match one of the allowed type combinations",
description),
Or<!foreach(combination, allowedCombinations,
!foldl(TruePred, !range(!size(inputArgs)), acc, i,
And<[acc,
SubstLeaves<"$_self", "$" # inputArgs[i] # ".getType()",
combination[i].predicate>
]>))>> {
assert !gt(!size(allowedCombinations), 0),
"allowedCombinations must not be empty";

// Validate that each combination has the same number of types as inputArgs
defvar inputArgSize = !size(inputArgs);
defvar validSizes = !foldl(1, allowedCombinations, acc, combination,
!and(acc, !eq(inputArgSize, !size(combination))));
assert validSizes,
"each combination in allowedCombinations must have the same length as inputArgs";

list<string> inputArgList = inputArgs;
list<list<Type>> allowedCombinationList = allowedCombinations;
}

// Type Constraint operand `idx`'s Element type is `type`.
class TCopVTEtIs<int idx, Type type> : And<[
CPred<"$_op.getNumOperands() > " # idx>,
Expand Down
4 changes: 2 additions & 2 deletions mlir/test/Dialect/LLVMIR/invalid.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -1307,8 +1307,8 @@ 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>
// expected-error @below {{'nvvm.mapa' op failed to verify that Valid address-space check(or mapping) for mapa Op}}
%0 = nvvm.mapa %a, %b: !llvm.ptr -> !llvm.ptr<7>
return
}

Expand Down
2 changes: 1 addition & 1 deletion mlir/test/Dialect/LLVMIR/nvvm.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ 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>
%1 = nvvm.mapa %a_shared, %b: !llvm.ptr<3> -> !llvm.ptr<7>
return
}

Expand Down
4 changes: 2 additions & 2 deletions mlir/test/Target/LLVMIR/nvvmir.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -783,8 +783,8 @@ llvm.func @nvvm_griddepcontrol_launch_dependents() {
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>
// CHECK-LLVM: call ptr addrspace(7) @llvm.nvvm.mapa.shared.cluster(ptr addrspace(3) %{{.*}}, i32 %{{.*}})
%1 = nvvm.mapa %a_shared, %b: !llvm.ptr<3> -> !llvm.ptr<7>
llvm.return
}

Expand Down