[Mlir-commits] [mlir] [MLIR][GPU-LLVM] Add GPU to LLVM-SPV address space mapping (PR #102621)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Fri Aug 9 07:14:41 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir-gpu
Author: Victor Perez (victor-eds)
<details>
<summary>Changes</summary>
Implement mapping:
- `global`: 1
- `workgroup`: 3
- `private`: 0
Add `addressSpaceToStorageClass`, mapping GPU address spaces to SPIR-V storage classes to be able to use SPIR-V's
`storageClassToAddressSpace`, mapping SPIR-V storage classes to LLVM address spaces according to our mapping above *by definition*.
---
Full diff: https://github.com/llvm/llvm-project/pull/102621.diff
6 Files Affected:
- (added) mlir/include/mlir/Conversion/GPUCommon/AttrToSPIRVConverter.h (+18)
- (modified) mlir/include/mlir/Conversion/GPUToLLVMSPV/GPUToLLVMSPVPass.h (+5)
- (added) mlir/lib/Conversion/GPUCommon/AttrToSPIRVConverter.cpp (+23)
- (modified) mlir/lib/Conversion/GPUCommon/CMakeLists.txt (+1)
- (modified) mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp (+13-1)
- (modified) mlir/test/Conversion/GPUToLLVMSPV/gpu-to-llvm-spv.mlir (+12)
``````````diff
diff --git a/mlir/include/mlir/Conversion/GPUCommon/AttrToSPIRVConverter.h b/mlir/include/mlir/Conversion/GPUCommon/AttrToSPIRVConverter.h
new file mode 100644
index 00000000000000..e1dcedba094c03
--- /dev/null
+++ b/mlir/include/mlir/Conversion/GPUCommon/AttrToSPIRVConverter.h
@@ -0,0 +1,18 @@
+//===- AttrToSPIRVConverter.h - GPU attributes conversion to SPIR-V - C++ -===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+#ifndef MLIR_CONVERSION_GPUCOMMON_ATTRTOSPIRVCONVERTER_H_
+#define MLIR_CONVERSION_GPUCOMMON_ATTRTOSPIRVCONVERTER_H_
+
+#include <mlir/Dialect/GPU/IR/GPUDialect.h>
+#include <mlir/Dialect/SPIRV/IR/SPIRVEnums.h>
+
+namespace mlir {
+spirv::StorageClass addressSpaceToStorageClass(gpu::AddressSpace addressSpace);
+} // namespace mlir
+
+#endif // MLIR_CONVERSION_GPUCOMMON_ATTRTOSPIRVCONVERTER_H_
diff --git a/mlir/include/mlir/Conversion/GPUToLLVMSPV/GPUToLLVMSPVPass.h b/mlir/include/mlir/Conversion/GPUToLLVMSPV/GPUToLLVMSPVPass.h
index e156c3093e21be..20c9f9c394525d 100644
--- a/mlir/include/mlir/Conversion/GPUToLLVMSPV/GPUToLLVMSPVPass.h
+++ b/mlir/include/mlir/Conversion/GPUToLLVMSPV/GPUToLLVMSPVPass.h
@@ -16,12 +16,17 @@ class DialectRegistry;
class LLVMTypeConverter;
class RewritePatternSet;
class Pass;
+class TypeConverter;
#define GEN_PASS_DECL_CONVERTGPUOPSTOLLVMSPVOPS
#include "mlir/Conversion/Passes.h.inc"
void populateGpuToLLVMSPVConversionPatterns(LLVMTypeConverter &converter,
RewritePatternSet &patterns);
+
+/// Populates memory space attribute conversion rules for lowering
+/// gpu.address_space to integer values.
+void populateGpuMemorySpaceAttributeConversions(TypeConverter &typeConverter);
} // namespace mlir
#endif // MLIR_CONVERSION_GPUTOLLVMSPV_GPUTOLLVMSPVPASS_H_
diff --git a/mlir/lib/Conversion/GPUCommon/AttrToSPIRVConverter.cpp b/mlir/lib/Conversion/GPUCommon/AttrToSPIRVConverter.cpp
new file mode 100644
index 00000000000000..fcabad32974acb
--- /dev/null
+++ b/mlir/lib/Conversion/GPUCommon/AttrToSPIRVConverter.cpp
@@ -0,0 +1,23 @@
+//===- AttrToSPIRVConverter.cpp - GPU attributes conversion to SPIR-V - C++===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include <mlir/Conversion/GPUCommon/AttrToSPIRVConverter.h>
+
+namespace mlir {
+spirv::StorageClass addressSpaceToStorageClass(gpu::AddressSpace addressSpace) {
+ switch (addressSpace) {
+ case gpu::AddressSpace::Global:
+ return spirv::StorageClass::CrossWorkgroup;
+ case gpu::AddressSpace::Workgroup:
+ return spirv::StorageClass::Workgroup;
+ case gpu::AddressSpace::Private:
+ return spirv::StorageClass::Private;
+ }
+ llvm_unreachable("Unhandled storage class");
+}
+} // namespace mlir
diff --git a/mlir/lib/Conversion/GPUCommon/CMakeLists.txt b/mlir/lib/Conversion/GPUCommon/CMakeLists.txt
index 55bd168b83a0ad..ce914c0ea3dd8a 100644
--- a/mlir/lib/Conversion/GPUCommon/CMakeLists.txt
+++ b/mlir/lib/Conversion/GPUCommon/CMakeLists.txt
@@ -15,6 +15,7 @@ if (MLIR_ENABLE_ROCM_CONVERSIONS)
endif()
add_mlir_conversion_library(MLIRGPUToGPURuntimeTransforms
+ AttrToSPIRVConverter.cpp
GPUToLLVMConversion.cpp
GPUOpsLowering.cpp
diff --git a/mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp b/mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp
index 36e4a6a38a68e4..a13694f795239f 100644
--- a/mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp
+++ b/mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp
@@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
+#include "mlir/Conversion/GPUCommon/AttrToSPIRVConverter.h"
+#include "mlir/Conversion/GPUCommon/GPUCommonPass.h"
#include "mlir/Conversion/GPUToLLVMSPV/GPUToLLVMSPVPass.h"
#include "../GPUCommon/GPUOpsLowering.h"
@@ -87,6 +89,8 @@ static LLVM::CallOp createSPIRVBuiltinCall(Location loc,
}
namespace {
+inline constexpr spirv::ClientAPI clientAPI = spirv::ClientAPI::OpenCL;
+
//===----------------------------------------------------------------------===//
// Barriers
//===----------------------------------------------------------------------===//
@@ -328,6 +332,7 @@ struct GPUToLLVMSPVConversionPass final
gpu::ReturnOp, gpu::ShuffleOp, gpu::ThreadIdOp>();
populateGpuToLLVMSPVConversionPatterns(converter, patterns);
+ populateGpuMemorySpaceAttributeConversions(converter);
if (failed(applyPartialConversion(getOperation(), target,
std::move(patterns))))
@@ -349,7 +354,6 @@ void populateGpuToLLVMSPVConversionPatterns(LLVMTypeConverter &typeConverter,
LaunchConfigOpConversion<gpu::BlockDimOp>,
LaunchConfigOpConversion<gpu::ThreadIdOp>,
LaunchConfigOpConversion<gpu::GlobalIdOp>>(typeConverter);
- constexpr spirv::ClientAPI clientAPI = spirv::ClientAPI::OpenCL;
MLIRContext *context = &typeConverter.getContext();
unsigned privateAddressSpace =
storageClassToAddressSpace(clientAPI, spirv::StorageClass::Function);
@@ -366,4 +370,12 @@ void populateGpuToLLVMSPVConversionPatterns(LLVMTypeConverter &typeConverter,
LLVM::CConv::SPIR_KERNEL, LLVM::CConv::SPIR_FUNC,
/*encodeWorkgroupAttributionsAsArguments=*/true});
}
+
+void populateGpuMemorySpaceAttributeConversions(TypeConverter &typeConverter) {
+ populateGpuMemorySpaceAttributeConversions(
+ typeConverter, [](gpu::AddressSpace space) -> unsigned {
+ return storageClassToAddressSpace(clientAPI,
+ addressSpaceToStorageClass(space));
+ });
+}
} // namespace mlir
diff --git a/mlir/test/Conversion/GPUToLLVMSPV/gpu-to-llvm-spv.mlir b/mlir/test/Conversion/GPUToLLVMSPV/gpu-to-llvm-spv.mlir
index 8e133288b832b6..1d6af1a3a9c367 100644
--- a/mlir/test/Conversion/GPUToLLVMSPV/gpu-to-llvm-spv.mlir
+++ b/mlir/test/Conversion/GPUToLLVMSPV/gpu-to-llvm-spv.mlir
@@ -503,3 +503,15 @@ gpu.module @kernels {
gpu.return
}
}
+
+// -----
+
+gpu.module @kernels {
+// CHECK-LABEL: llvm.func spir_funccc @address_spaces(
+// CHECK-SAME: {{.*}}: !llvm.ptr<1>
+// CHECK-SAME: {{.*}}: !llvm.ptr<3>
+// CHECK-SAME: {{.*}}: !llvm.ptr
+ gpu.func @address_spaces(%arg0: memref<f32, #gpu.address_space<global>>, %arg1: memref<f32, #gpu.address_space<workgroup>>, %arg2: memref<f32, #gpu.address_space<private>>) {
+ gpu.return
+ }
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/102621
More information about the Mlir-commits
mailing list