-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[MLIR][GPU] Lower subgroup query ops in gpu-to-llvm-spv #108839
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
Conversation
These ops are: * gpu.subgroup_id * gpu.lane_id * gpu.num_subgroups * gpu.subgroup_size Signed-off-by: Finlay Marno <[email protected]>
@llvm/pr-subscribers-mlir-gpu @llvm/pr-subscribers-mlir Author: Finlay (FMarno) ChangesThese ops are:
Full diff: https://github.com/llvm/llvm-project/pull/108839.diff 2 Files Affected:
diff --git a/mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp b/mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp
index 41a3ac76df4b78..f9d92f850df68b 100644
--- a/mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp
+++ b/mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp
@@ -316,6 +316,43 @@ struct GPUShuffleConversion final : ConvertOpToLLVMPattern<gpu::ShuffleOp> {
}
};
+//===----------------------------------------------------------------------===//
+// Subgroup query ops.
+//===----------------------------------------------------------------------===//
+
+template <typename SubgroupOp>
+struct GPUSubgroupOpConversion final : ConvertOpToLLVMPattern<SubgroupOp> {
+ using ConvertOpToLLVMPattern<SubgroupOp>::ConvertOpToLLVMPattern;
+
+ LogicalResult
+ matchAndRewrite(SubgroupOp op, typename SubgroupOp::Adaptor adaptor,
+ ConversionPatternRewriter &rewriter) const final {
+ constexpr StringRef funcName = [] {
+ if constexpr (std::is_same_v<SubgroupOp, gpu::SubgroupIdOp>) {
+ return "_Z16get_sub_group_id";
+ } else if constexpr (std::is_same_v<SubgroupOp, gpu::LaneIdOp>) {
+ return "_Z22get_sub_group_local_id";
+ } else if constexpr (std::is_same_v<SubgroupOp, gpu::NumSubgroupsOp>) {
+ return "_Z18get_num_sub_groups";
+ } else if constexpr (std::is_same_v<SubgroupOp, gpu::SubgroupSizeOp>) {
+ return "_Z18get_sub_group_size";
+ }
+ }();
+
+ Operation *moduleOp =
+ op->template getParentWithTrait<OpTrait::SymbolTable>();
+ Type resultType = rewriter.getI32Type();
+ LLVM::LLVMFuncOp func =
+ lookupOrCreateSPIRVFn(moduleOp, funcName, {}, resultType,
+ /*isMemNone=*/false, /*isConvergent=*/false);
+
+ Location loc = op->getLoc();
+ Value result = createSPIRVBuiltinCall(loc, rewriter, func, {}).getResult();
+ rewriter.replaceOp(op, result);
+ return success();
+ }
+};
+
//===----------------------------------------------------------------------===//
// GPU To LLVM-SPV Pass.
//===----------------------------------------------------------------------===//
@@ -335,9 +372,11 @@ struct GPUToLLVMSPVConversionPass final
LLVMTypeConverter converter(context, options);
LLVMConversionTarget target(*context);
- target.addIllegalOp<gpu::BarrierOp, gpu::BlockDimOp, gpu::BlockIdOp,
- gpu::GPUFuncOp, gpu::GlobalIdOp, gpu::GridDimOp,
- gpu::ReturnOp, gpu::ShuffleOp, gpu::ThreadIdOp>();
+ target.addIllegalOp<gpu::ThreadIdOp, gpu::BlockIdOp, gpu::GlobalIdOp,
+ gpu::BlockDimOp, gpu::GridDimOp, gpu::BarrierOp,
+ gpu::GPUFuncOp, gpu::ReturnOp, gpu::ShuffleOp,
+ gpu::SubgroupIdOp, gpu::LaneIdOp, gpu::NumSubgroupsOp,
+ gpu::SubgroupSizeOp>();
populateGpuToLLVMSPVConversionPatterns(converter, patterns);
populateGpuMemorySpaceAttributeConversions(converter);
@@ -370,7 +409,11 @@ void populateGpuToLLVMSPVConversionPatterns(LLVMTypeConverter &typeConverter,
LaunchConfigOpConversion<gpu::GridDimOp>,
LaunchConfigOpConversion<gpu::BlockDimOp>,
LaunchConfigOpConversion<gpu::ThreadIdOp>,
- LaunchConfigOpConversion<gpu::GlobalIdOp>>(typeConverter);
+ LaunchConfigOpConversion<gpu::GlobalIdOp>,
+ GPUSubgroupOpConversion<gpu::SubgroupIdOp>,
+ GPUSubgroupOpConversion<gpu::LaneIdOp>,
+ GPUSubgroupOpConversion<gpu::NumSubgroupsOp>,
+ GPUSubgroupOpConversion<gpu::SubgroupSizeOp>>(typeConverter);
MLIRContext *context = &typeConverter.getContext();
unsigned privateAddressSpace =
gpuAddressSpaceToOCLAddressSpace(gpu::AddressSpace::Private);
diff --git a/mlir/test/Conversion/GPUToLLVMSPV/gpu-to-llvm-spv.mlir b/mlir/test/Conversion/GPUToLLVMSPV/gpu-to-llvm-spv.mlir
index 860bb60726352d..c06648f117e8f1 100644
--- a/mlir/test/Conversion/GPUToLLVMSPV/gpu-to-llvm-spv.mlir
+++ b/mlir/test/Conversion/GPUToLLVMSPV/gpu-to-llvm-spv.mlir
@@ -563,3 +563,28 @@ gpu.module @kernels {
gpu.return
}
}
+
+// -----
+
+// Lowering of subgroup query operations
+
+// CHECK-DAG: llvm.func spir_funccc @_Z18get_sub_group_size() -> i32 attributes {no_unwind, will_return}
+// CHECK-DAG: llvm.func spir_funccc @_Z18get_num_sub_groups() -> i32 attributes {no_unwind, will_return}
+// CHECK-DAG: llvm.func spir_funccc @_Z22get_sub_group_local_id() -> i32 attributes {no_unwind, will_return}
+// CHECK-DAG: llvm.func spir_funccc @_Z16get_sub_group_id() -> i32 attributes {no_unwind, will_return}
+
+
+gpu.module @subgroup_operations {
+// CHECK-LABEL: @gpu_subgroup
+ func.func @gpu_subgroup() {
+ // CHECK: llvm.call spir_funccc @_Z16get_sub_group_id() {no_unwind, will_return} : () -> i32
+ %0 = gpu.subgroup_id : index
+ // CHECK: llvm.call spir_funccc @_Z22get_sub_group_local_id() {no_unwind, will_return} : () -> i32
+ %1 = gpu.lane_id
+ // CHECK: llvm.call spir_funccc @_Z18get_num_sub_groups() {no_unwind, will_return} : () -> i32
+ %2 = gpu.num_subgroups : index
+ // CHECK: llvm.call spir_funccc @_Z18get_sub_group_size() {no_unwind, will_return} : () -> i32
+ %3 = gpu.subgroup_size : index
+ return
+ }
+}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a minor. Nice one!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: Sort alphabetically illegal ops and patterns
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No objections from me
If there are no objections, I will merge this tomorrow. Let me know if you would like more time. |
These ops are: * gpu.subgroup_id * gpu.lane_id * gpu.num_subgroups * gpu.subgroup_size --------- Signed-off-by: Finlay Marno <[email protected]>
These ops are: