[Mlir-commits] [mlir] 8fb09c8 - [mlir][gpu] Add GPU subgroup MMA extract and insert operations (#139048)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Wed May 28 12:40:20 PDT 2025
Author: Hsiangkai Wang
Date: 2025-05-28T20:40:17+01:00
New Revision: 8fb09c8d09bdbc1af98cb7138411029223731be8
URL: https://github.com/llvm/llvm-project/commit/8fb09c8d09bdbc1af98cb7138411029223731be8
DIFF: https://github.com/llvm/llvm-project/commit/8fb09c8d09bdbc1af98cb7138411029223731be8.diff
LOG: [mlir][gpu] Add GPU subgroup MMA extract and insert operations (#139048)
- Introduced `gpu.subgroup_mma_extract` operation to extract values from
`!gpu.mma_matrix` by invocation and indices.
- Introduced `gpu.subgroup_mma_insert` operation to insert values into
`!gpu.mma_matrix` by invocation and indices.
- Updated the conversion patterns to SPIR-V for both extract and insert
operations.
- Added test cases to validate the new operations in the GPU to SPIR-V
conversion.
RFC:
https://discourse.llvm.org/t/rfc-add-gpu-operations-to-permute-data-in-2-loaded-mma-matrix/86148?u=hsiangkai
Added:
Modified:
mlir/include/mlir/Dialect/GPU/IR/GPUOps.td
mlir/lib/Conversion/GPUToSPIRV/WmmaOpsToSPIRV.cpp
mlir/test/Conversion/GPUToSPIRV/wmma-ops-to-spirv-khr-coop-matrix.mlir
mlir/test/Dialect/GPU/ops.mlir
Removed:
################################################################################
diff --git a/mlir/include/mlir/Dialect/GPU/IR/GPUOps.td b/mlir/include/mlir/Dialect/GPU/IR/GPUOps.td
index 8d83d02e27c33..caf2d4268da43 100644
--- a/mlir/include/mlir/Dialect/GPU/IR/GPUOps.td
+++ b/mlir/include/mlir/Dialect/GPU/IR/GPUOps.td
@@ -1921,6 +1921,95 @@ def GPU_SubgroupMmaConstantMatrixOp : GPU_Op<"subgroup_mma_constant_matrix",
}];
}
+def GPU_SubgroupMmaExtractThreadLocalOp : GPU_Op<"subgroup_mma_extract_thread_local",
+ [Pure,
+ TypesMatchWith<"value type matches element type of mma_matrix",
+ "matrix", "res",
+ "::llvm::cast<gpu::MMAMatrixType>($_self).getElementType()">]>{
+
+ let summary = "Extract a value from GPU warp by invocation and indices";
+
+ let description = [{
+ The `gpu.subgroup_mma_extract_thread_local` operation extracts a value from `!gpu.mma_matrix`
+ that is stored at subgroup level.
+
+ This operation takes `!gpu.mma_matrix` as its first operand. It is the source
+ matrix across a subgroup. The op returns a scalar value stored in the invocation
+ in the subgroup.
+
+ Since `matrix` is packed into the the threads within a subgroup, `indices` are
+ the indices into the values stored by each thread. That is, an index of 0 (or [0, 0])
+ does not necessarily refer to the first element of the matrix, but the first element
+ that a particular thread holds.
+
+ The mapping of matrix elements to threads is not defined by this operation and may
+ not be defined by some lowerings (such as the lowering to SPIR-V). However, if the
+ size of the subgroup is S, then `subgroup_mma_extract_thread_local` at each index in
+ `[0, (M * N) / S)` will have the entire matrix extracted across the subgroup.
+
+ Example:
+
+ ```mlir
+ %c0 = arith.constant 0 : index
+ %val = gpu.subgroup_mma_extract_thread_local %m[%c0] : !gpu.mma_matrix<16x16xf32, "AOp"> -> f32
+ ```
+ }];
+
+ let arguments = (ins GPU_MMAMatrix:$matrix, Variadic<Index>:$indices);
+
+ let results = (outs AnyIntegerOrFloat:$res);
+
+ let assemblyFormat = [{
+ $matrix`[`$indices`]` attr-dict `:` type($matrix) `->` type($res)
+ }];
+}
+
+def GPU_SubgroupMmaInsertThreadLocalOp : GPU_Op<"subgroup_mma_insert_thread_local",
+ [Pure,
+ TypesMatchWith<"value type matches element type of mma_matrix",
+ "matrix", "value",
+ "::llvm::cast<gpu::MMAMatrixType>($_self).getElementType()"> ]>{
+
+ let summary = "Insert a value into GPU warp by invocation and indices";
+
+ let description = [{
+ The `gpu.subgroup_mma_insert_thread_local` operation inserts a value to `!gpu.mma_matrix`
+ that is stored at subgroup level.
+
+ This operation takes scalar value as its first operand and `!gpu.mma_matrix`
+ as its second operand. The op inserts the scalar value to the matrix.
+
+ Since `matrix` is packed into the the threads within a subgroup, `indices` are
+ the indices into the values stored by each thread. That is, an index of 0 (or [0, 0])
+ does not necessarily refer to the first element of the matrix, but the first element
+ that a particular thread holds.
+
+ The mapping of matrix elements to threads is not defined by this operation and may
+ not be defined by some lowerings (such as the lowering to SPIR-V). However, if the
+ size of the subgroup is S, then `subgroup_mma_insert_thread_local` at each index in
+ `[0, (M * N) / S)` will have the entire matrix inserted across the subgroup.
+
+ The op returns `!gpu.mma_matrix` with the updated value.
+
+ Example:
+
+ ```mlir
+ %c0 = arith.constant 0 : index
+ %s0 = gpu.subgroup_mma_insert_thread_local %val, %m[%c0] : f16, !gpu.mma_matrix<16x16xf16, "COp">
+ -> !gpu.mma_matrix<16x16xf16, "COp">
+ ```
+ }];
+
+ let arguments = (ins AnyIntegerOrFloat:$value, GPU_MMAMatrix:$matrix,
+ Variadic<Index>:$indices);
+
+ let results = (outs GPU_MMAMatrix:$res);
+
+ let assemblyFormat = [{
+ $value`,` $matrix`[`$indices`]` attr-dict `:` type($value)`,` type($matrix) `->` type($res)
+ }];
+}
+
def GPU_ElementwiseOpAddF : I32EnumAttrCase<"ADDF", 0, "addf">;
def GPU_ElementwiseOpMulF : I32EnumAttrCase<"MULF", 1, "mulf">;
def GPU_ElementwiseOpSUBF : I32EnumAttrCase<"SUBF", 2, "subf">;
diff --git a/mlir/lib/Conversion/GPUToSPIRV/WmmaOpsToSPIRV.cpp b/mlir/lib/Conversion/GPUToSPIRV/WmmaOpsToSPIRV.cpp
index df2da138d3b52..d2f5e35853550 100644
--- a/mlir/lib/Conversion/GPUToSPIRV/WmmaOpsToSPIRV.cpp
+++ b/mlir/lib/Conversion/GPUToSPIRV/WmmaOpsToSPIRV.cpp
@@ -111,6 +111,68 @@ struct WmmaConstantOpToSPIRVLowering final
}
};
+/// Converts GPU MMA ExtractOp to CompositeExtract SPIR-V KHR/NV cooperative
+/// matrix ops.
+struct WmmaExtractOpToSPIRVLowering final
+ : OpConversionPattern<gpu::SubgroupMmaExtractThreadLocalOp> {
+ using OpConversionPattern::OpConversionPattern;
+
+ LogicalResult
+ matchAndRewrite(gpu::SubgroupMmaExtractThreadLocalOp op, OpAdaptor adaptor,
+ ConversionPatternRewriter &rewriter) const override {
+ Value matrix = adaptor.getMatrix();
+ auto coopType =
+ getTypeConverter()->convertType<spirv::CooperativeMatrixType>(
+ matrix.getType());
+ if (!coopType)
+ return rewriter.notifyMatchFailure(op, "type conversion failed");
+
+ SmallVector<int32_t> intValues;
+ for (Value val : op.getIndices()) {
+ if (auto constOp = val.getDefiningOp<arith::ConstantIndexOp>()) {
+ intValues.push_back(static_cast<int32_t>(constOp.value()));
+ } else {
+ return rewriter.notifyMatchFailure(op, "indices must be constants");
+ }
+ }
+
+ Type elementType = coopType.getElementType();
+ rewriter.replaceOpWithNewOp<spirv::CompositeExtractOp>(
+ op, elementType, matrix, rewriter.getI32ArrayAttr(intValues));
+ return success();
+ }
+};
+
+/// Converts GPU MMA InsertOp to CompositeInsert SPIR-V KHR/NV cooperative
+/// matrix ops.
+struct WmmaInsertOpToSPIRVLowering final
+ : OpConversionPattern<gpu::SubgroupMmaInsertThreadLocalOp> {
+ using OpConversionPattern::OpConversionPattern;
+
+ LogicalResult
+ matchAndRewrite(gpu::SubgroupMmaInsertThreadLocalOp op, OpAdaptor adaptor,
+ ConversionPatternRewriter &rewriter) const override {
+ Value value = adaptor.getValue();
+ Value matrix = adaptor.getMatrix();
+ auto coopType = getTypeConverter()->convertType(matrix.getType());
+ if (!coopType)
+ return rewriter.notifyMatchFailure(op, "type conversion failed");
+
+ SmallVector<int32_t> intValues;
+ for (Value val : op.getIndices()) {
+ if (auto constOp = val.getDefiningOp<arith::ConstantIndexOp>()) {
+ intValues.push_back(static_cast<int32_t>(constOp.value()));
+ } else {
+ return rewriter.notifyMatchFailure(op, "indices must be constants");
+ }
+ }
+
+ rewriter.replaceOpWithNewOp<spirv::CompositeInsertOp>(
+ op, coopType, value, matrix, rewriter.getI32ArrayAttr(intValues));
+ return success();
+ }
+};
+
/// Converts elementwise ops to SPIR-V cooperative matrix elementwise ops for
/// the default case.
struct WmmaElementwiseOpToSPIRVDefaultLowering final
@@ -296,6 +358,7 @@ void mlir::populateGpuWMMAToSPIRVCoopMatrixKHRConversionPatterns(
MLIRContext *context = patterns.getContext();
patterns.add<khr::WmmaLoadOpToSPIRVLowering, khr::WmmaMmaOpToSPIRVLowering,
khr::WmmaStoreOpToSPIRVLowering, WmmaConstantOpToSPIRVLowering,
+ WmmaExtractOpToSPIRVLowering, WmmaInsertOpToSPIRVLowering,
WmmaElementwiseOpToSPIRVDefaultLowering>(converter, context);
// Give the following patterns higher benefit to prevail over the default one.
patterns.add<WmmaElementwiseOpToSPIRVScalarMulLowering>(converter, context,
diff --git a/mlir/test/Conversion/GPUToSPIRV/wmma-ops-to-spirv-khr-coop-matrix.mlir b/mlir/test/Conversion/GPUToSPIRV/wmma-ops-to-spirv-khr-coop-matrix.mlir
index 477f344b1ae5f..7ef3711ebe28b 100644
--- a/mlir/test/Conversion/GPUToSPIRV/wmma-ops-to-spirv-khr-coop-matrix.mlir
+++ b/mlir/test/Conversion/GPUToSPIRV/wmma-ops-to-spirv-khr-coop-matrix.mlir
@@ -93,6 +93,33 @@ module attributes {
gpu.return
}
+ // CHECK-LABEL: spirv.func @gpu_wmma_extract_thread_local_op
+ // CHECK-SAME: %[[ARG0:.+]]: !spirv.coopmatrix<16x16xf32, Subgroup, MatrixA>
+ gpu.func @gpu_wmma_extract_thread_local_op(%m: !gpu.mma_matrix<16x16xf32, "AOp">,
+ %ptr: memref<16x16xf32, #spirv.storage_class<StorageBuffer>>) kernel
+ attributes {spirv.entry_point_abi = #spirv.entry_point_abi<workgroup_size = [32, 4, 1]>} {
+ // CHECK: spirv.CompositeExtract %[[ARG0]][0 : i32] : !spirv.coopmatrix<16x16xf32, Subgroup, MatrixA>
+ %c0 = arith.constant 0 : index
+ %val = gpu.subgroup_mma_extract_thread_local %m[%c0] : !gpu.mma_matrix<16x16xf32, "AOp"> -> f32
+ memref.store %val, %ptr[%c0, %c0] : memref<16x16xf32, #spirv.storage_class<StorageBuffer>>
+ gpu.return
+ }
+
+ // CHECK-LABEL: spirv.func @gpu_wmma_insert_thread_local_op
+ // CHECK-SAME: %[[ARG0:.+]]: f16
+ // CHECK-SAME: %[[ARG1:.+]]: !spirv.coopmatrix<16x16xf16, Subgroup, MatrixAcc>
+ gpu.func @gpu_wmma_insert_thread_local_op(%val: f16,
+ %m: !gpu.mma_matrix<16x16xf16, "COp">,
+ %ptr: memref<16x16xf16, #spirv.storage_class<StorageBuffer>>) kernel
+ attributes {spirv.entry_point_abi = #spirv.entry_point_abi<workgroup_size = [32, 4, 1]>} {
+ // CHECK: spirv.CompositeInsert %[[ARG0]], %[[ARG1]][0 : i32] : f16 into !spirv.coopmatrix<16x16xf16, Subgroup, MatrixAcc>
+ %c0 = arith.constant 0 : index
+ %s0 = gpu.subgroup_mma_insert_thread_local %val, %m[%c0] : f16, !gpu.mma_matrix<16x16xf16, "COp"> -> !gpu.mma_matrix<16x16xf16, "COp">
+ gpu.subgroup_mma_store_matrix %s0, %ptr[%c0,%c0] {leadDimension = 16 : index} :
+ !gpu.mma_matrix<16x16xf16, "COp">, memref<16x16xf16, #spirv.storage_class<StorageBuffer>>
+ gpu.return
+ }
+
// CHECK-LABEL: spirv.func @gpu_wmma_elementwise_op_default
// CHECK-SAME: !spirv.coopmatrix<16x16xf16, Subgroup, MatrixAcc>
// CHECK-SAME: !spirv.coopmatrix<16x16xf16, Subgroup, MatrixAcc>
diff --git a/mlir/test/Dialect/GPU/ops.mlir b/mlir/test/Dialect/GPU/ops.mlir
index 99915c493ea46..9dbe16774f517 100644
--- a/mlir/test/Dialect/GPU/ops.mlir
+++ b/mlir/test/Dialect/GPU/ops.mlir
@@ -430,6 +430,20 @@ module attributes {gpu.container_module} {
gpu.wait [%token16]
return
}
+
+ // CHECK-LABEL: func @extract_insert_mma
+ func.func @extract_insert_mma(%src : !gpu.mma_matrix<16x16xf32, "COp">,
+ %ptr: memref<16x16xf32>) {
+ %zero = arith.constant 0.0 : f32
+ %c0 = arith.constant 0 : index
+ // CHECK: gpu.subgroup_mma_extract_thread_local
+ %val = gpu.subgroup_mma_extract_thread_local %src[%c0] : !gpu.mma_matrix<16x16xf32, "COp"> -> f32
+ %m = gpu.subgroup_mma_constant_matrix %zero : !gpu.mma_matrix<16x16xf32, "COp">
+ // CHECK: gpu.subgroup_mma_insert_thread_local
+ %s0 = gpu.subgroup_mma_insert_thread_local %val, %m[%c0] : f32, !gpu.mma_matrix<16x16xf32, "COp"> -> !gpu.mma_matrix<16x16xf32, "COp">
+ gpu.subgroup_mma_store_matrix %s0, %ptr[%c0, %c0] {leadDimension = 16 : index} : !gpu.mma_matrix<16x16xf32, "COp">, memref<16x16xf32>
+ return
+ }
}
// Just check that this doesn't crash.
More information about the Mlir-commits
mailing list