Skip to content

Commit c9c4e6e

Browse files
authored
Reland [mlir][amdgpu] Add common gpu mem space conversions to convert-amdgpu-to-rocdl (#171599)
Reland #171543 Added missing GPU lib `MLIRGPUToGPURuntimeTransforms`.
1 parent 8c49509 commit c9c4e6e

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

mlir/lib/Conversion/AMDGPUToROCDL/AMDGPUToROCDL.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "mlir/Conversion/AMDGPUToROCDL/AMDGPUToROCDL.h"
1010

11+
#include "mlir/Conversion/GPUCommon/GPUCommonPass.h"
1112
#include "mlir/Conversion/LLVMCommon/ConversionTarget.h"
1213
#include "mlir/Conversion/LLVMCommon/Pattern.h"
1314
#include "mlir/Conversion/LLVMCommon/TypeConverter.h"
@@ -2730,6 +2731,18 @@ struct ConvertAMDGPUToROCDLPass
27302731
});
27312732

27322733
populateAMDGPUToROCDLConversionPatterns(converter, patterns, *maybeChipset);
2734+
populateGpuMemorySpaceAttributeConversions(
2735+
converter, [](gpu::AddressSpace space) {
2736+
switch (space) {
2737+
case gpu::AddressSpace::Global:
2738+
return 1;
2739+
case gpu::AddressSpace::Workgroup:
2740+
return 3;
2741+
case gpu::AddressSpace::Private:
2742+
return 5;
2743+
}
2744+
llvm_unreachable("unknown address space enum value");
2745+
});
27332746
LLVMConversionTarget target(getContext());
27342747
target.addIllegalDialect<::mlir::amdgpu::AMDGPUDialect>();
27352748
target.addLegalDialect<::mlir::LLVM::LLVMDialect>();

mlir/lib/Conversion/AMDGPUToROCDL/CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ add_mlir_conversion_library(MLIRAMDGPUToROCDL
1111
Core
1212

1313
LINK_LIBS PUBLIC
14-
MLIRLLVMCommonConversion
15-
MLIRLLVMDialect
16-
MLIRROCDLDialect
1714
MLIRAMDGPUDialect
1815
MLIRAMDGPUUtils
16+
MLIRGPUToGPURuntimeTransforms
17+
MLIRLLVMCommonConversion
18+
MLIRLLVMDialect
1919
MLIRPass
20+
MLIRROCDLDialect
2021
MLIRTransforms
2122
)

mlir/test/Conversion/AMDGPUToROCDL/load_lds.mlir

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: mlir-opt %s -convert-amdgpu-to-rocdl=chipset=gfx942 | FileCheck %s
2-
// RUN: mlir-opt %s -convert-amdgpu-to-rocdl=chipset=gfx950 | FileCheck %s
1+
// RUN: mlir-opt %s --convert-amdgpu-to-rocdl=chipset=gfx942 | FileCheck %s
2+
// RUN: mlir-opt %s --convert-amdgpu-to-rocdl=chipset=gfx950 | FileCheck %s
33

44
#gpu_global_addrspace = 1
55
#gpu_lds_addrspace = 3
@@ -43,6 +43,44 @@ func.func @global_load_to_rocdl_f32(%global : memref<128x72xf32, #gpu_global_add
4343
func.return
4444
}
4545

46+
// CHECK-LABEL: func @global_load_to_rocdl_wg_mem
47+
// CHECK-SAME: (%[[ARG0:.*]]: memref<128x72xf32>)
48+
func.func @global_load_to_rocdl_wg_mem(%global : memref<128x72xf32>) {
49+
%c0 = arith.constant 0 : index
50+
%c12 = arith.constant 12 : index
51+
%c32 = arith.constant 32 : index
52+
%alloc = memref.alloc() : memref<64x64xf32, #gpu.address_space<workgroup>>
53+
// CHECK: %[[GLOBAL_DESC:.*]] = builtin.unrealized_conversion_cast %[[ARG0]]
54+
55+
// CHECK: %[[C0:.*]] = arith.constant 0 : index
56+
// CHECK: %[[IC0:.*]] = builtin.unrealized_conversion_cast %c0 : index to i64
57+
// CHECK: %[[C12:.*]] = arith.constant 12 : index
58+
// CHECK: %[[IC12:.*]] = builtin.unrealized_conversion_cast %[[C12]]
59+
// CHECK: %[[C32:.*]] = arith.constant 32 : index
60+
// CHECK: %[[IC32:.*]] = builtin.unrealized_conversion_cast %[[C32]]
61+
62+
// CHECK: %[[ALLOC:.*]] = memref.alloc()
63+
// CHECK: %[[LDS_DESC:.*]] = builtin.unrealized_conversion_cast
64+
// CHECK: %[[GLOBAL_BASE:.*]] = llvm.extractvalue %[[GLOBAL_DESC]][1]
65+
66+
// CHECK: %[[C72:.*]] = llvm.mlir.constant(72 : index) : i64
67+
// CHECK: %[[MUL:.*]] = llvm.mul %[[IC12]], %[[C72]] : i64
68+
// CHECK: %[[SRC_OFFSET:.*]] = llvm.add %[[MUL]], %[[IC0]] : i64
69+
70+
// CHECK: %[[GLOBAL_PTR:.*]] = llvm.getelementptr %[[GLOBAL_BASE]][%[[SRC_OFFSET]]]
71+
// CHECK: %[[LDS_BASE:.*]] = llvm.extractvalue %[[LDS_DESC]][1]
72+
73+
// CHECK: %[[C64:.*]] = llvm.mlir.constant(64 : index) : i64
74+
// CHECK: %[[MUL_2:.*]] = llvm.mul %[[IC32]], %[[C64]] : i64
75+
// CHECK: %[[DST_OFFSET:.*]] = llvm.add %[[MUL_2]], %[[IC0]] : i64
76+
77+
// CHECK: %[[LDS_PTR:.*]] = llvm.getelementptr %[[LDS_BASE]][%[[DST_OFFSET]]]
78+
// CHECK: rocdl.load.to.lds %[[GLOBAL_PTR]], %[[LDS_PTR]], 4
79+
amdgpu.gather_to_lds %global[%c12, %c0], %alloc[%c32, %c0]
80+
: f32, memref<128x72xf32>, memref<64x64xf32, #gpu.address_space<workgroup>>
81+
func.return
82+
}
83+
4684
// CHECK-LABEL: func @global_load_to_rocdl_i8
4785
// CHECK-SAME: (%[[ARG0:.*]]: memref<128x72xi8, 1>)
4886
func.func @global_load_to_rocdl_i8(%global : memref<128x72xi8, #gpu_global_addrspace>) {

0 commit comments

Comments
 (0)