Skip to content

Commit 8fcae21

Browse files
committed
Add a workitem level gemm test for XeGPU.
1 parent dd56eb0 commit 8fcae21

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// RUN: mlir-opt %s --gpu-lower-to-xevm-pipeline="xegpu-op-level=workitem" \
2+
// RUN: | mlir-runner \
3+
// RUN: --shared-libs=%mlir_levelzero_runtime \
4+
// RUN: --shared-libs=%mlir_runner_utils \
5+
// RUN: --entry-point-result=void \
6+
// RUN: | FileCheck %s
7+
8+
module @gemm attributes {gpu.container_module} {
9+
gpu.module @kernel {
10+
gpu.func @simple_gemm(%a: memref<256x256xf16>, %b: memref<256x256xf16>, %c: memref<256x256xf32>) kernel {
11+
%c0 = arith.constant 0 : index
12+
%c1 = arith.constant 1 : index
13+
%c8 = arith.constant 8 : index
14+
%c16 = arith.constant 16 : index
15+
%c32 = arith.constant 32 : index
16+
%c256 = arith.constant 256 : index
17+
%block_x = gpu.block_id x
18+
%block_y = gpu.block_id y
19+
%x_block_offset = arith.muli %block_x, %c8 : index
20+
%y_block_offset = arith.muli %block_y, %c16 : index
21+
22+
%c_tdesc = xegpu.create_nd_tdesc %c : memref<256x256xf32> -> !xegpu.tensor_desc<8x16xf32>
23+
%c_init_value = xegpu.load_nd %c_tdesc[%x_block_offset, %y_block_offset] : !xegpu.tensor_desc<8x16xf32> -> vector<8xf32>
24+
%a_tdesc = xegpu.create_nd_tdesc %a : memref<256x256xf16> -> !xegpu.tensor_desc<8x16xf16>
25+
%b_tdesc = xegpu.create_nd_tdesc %b : memref<256x256xf16> -> !xegpu.tensor_desc<16x16xf16>
26+
27+
%r = scf.for %k = %c0 to %c256 step %c16 iter_args(%arg_c = %c_init_value) -> (vector<8xf32>) {
28+
29+
%a_val = xegpu.load_nd %a_tdesc[%x_block_offset, %k] : !xegpu.tensor_desc<8x16xf16> -> vector<8xf16>
30+
%b_val = xegpu.load_nd %b_tdesc[%k, %y_block_offset] : !xegpu.tensor_desc<16x16xf16> -> vector<16xf16>
31+
%dpas = xegpu.dpas %a_val, %b_val, %arg_c : vector<8xf16>, vector<16xf16>, vector<8xf32> -> vector<8xf32>
32+
scf.yield %dpas : vector<8xf32>
33+
}
34+
xegpu.store_nd %r, %c_tdesc[%x_block_offset, %y_block_offset] <{l1_hint = #xegpu.cache_hint<write_back>, l2_hint = #xegpu.cache_hint<uncached>}>: vector<8xf32>, !xegpu.tensor_desc<8x16xf32>
35+
gpu.return
36+
}
37+
}
38+
39+
func.func @test(%a : memref<256x256xf16>, %b : memref<256x256xf16>, %c : memref<256x256xf32>) -> memref<256x256xf32> attributes {llvm.emit_c_interface} {
40+
%c1 = arith.constant 1 : index
41+
%c16 = arith.constant 16 : index
42+
%c32 = arith.constant 32 : index
43+
%memref_a = gpu.alloc () : memref<256x256xf16>
44+
gpu.memcpy %memref_a, %a : memref<256x256xf16>, memref<256x256xf16>
45+
%memref_b = gpu.alloc () : memref<256x256xf16>
46+
gpu.memcpy %memref_b, %b : memref<256x256xf16>, memref<256x256xf16>
47+
%memref_c = gpu.alloc () : memref<256x256xf32>
48+
gpu.memcpy %memref_c, %c : memref<256x256xf32>, memref<256x256xf32>
49+
gpu.launch_func @kernel::@simple_gemm blocks in (%c32, %c16, %c1) threads in (%c16, %c1, %c1) args(%memref_a : memref<256x256xf16>, %memref_b : memref<256x256xf16>, %memref_c : memref<256x256xf32>)
50+
gpu.wait // Wait for the kernel to finish.
51+
gpu.memcpy %c, %memref_c : memref<256x256xf32>, memref<256x256xf32>
52+
gpu.dealloc %memref_a : memref<256x256xf16>
53+
gpu.dealloc %memref_b : memref<256x256xf16>
54+
gpu.dealloc %memref_c : memref<256x256xf32>
55+
return %c : memref<256x256xf32>
56+
}
57+
58+
func.func @main() attributes {llvm.emit_c_interface} {
59+
%c0 = arith.constant 0 : index
60+
%c1 = arith.constant 1 : index
61+
%c1_f16 = arith.constant 1.0 : f16
62+
%c2_f16 = arith.constant 2.0 : f16
63+
%c256 = arith.constant 256 : index
64+
%cf_0 = arith.constant 0.0 : f16
65+
%cf_1 = arith.constant 1.0 : f16
66+
%A = memref.alloc() : memref<256x256xf16>
67+
%B = memref.alloc() : memref<256x256xf16>
68+
%C = memref.alloc() : memref<256x256xf32>
69+
%C_ref = memref.alloc() : memref<256x256xf32>
70+
%c_gen_int = arith.constant 0 : i1
71+
%cf_lower = arith.constant -0.5 : f32
72+
%cf_upper = arith.constant 0.5 : f32
73+
74+
// Initialize matrix A ; A[i, j] = j
75+
scf.for %i = %c0 to %c256 step %c1 {
76+
scf.for %j = %c0 to %c256 step %c1 {
77+
%t = index.castu %j : index to i16
78+
%val = arith.uitofp %t : i16 to f16
79+
memref.store %val, %A[%i, %j] : memref<256x256xf16>
80+
}
81+
}
82+
83+
// Initialize the B matrix.
84+
// Make matrix B an identity matrix.
85+
scf.for %i = %c0 to %c256 step %c1 {
86+
scf.for %j = %c0 to %c256 step %c1 {
87+
%i_i32 = index.castu %i : index to i32
88+
%j_i32 = index.castu %j : index to i32
89+
%i_j_same = arith.cmpi eq, %i_i32, %j_i32 : i32
90+
91+
scf.if %i_j_same {
92+
memref.store %cf_1, %B[%i, %j] : memref<256x256xf16>
93+
} else {
94+
memref.store %cf_0, %B[%i, %j] : memref<256x256xf16>
95+
}
96+
}
97+
}
98+
99+
// Initialize matrix C and C_ref ; C[i, j] = 0
100+
%c0_f32 = arith.constant 0.0 : f32
101+
scf.for %i = %c0 to %c256 step %c1 {
102+
scf.for %j = %c0 to %c256 step %c1 {
103+
memref.store %c0_f32, %C[%i, %j] : memref<256x256xf32>
104+
memref.store %c0_f32, %C_ref[%i, %j] : memref<256x256xf32>
105+
}
106+
}
107+
108+
// Run GPU version.
109+
%2 = call @test(%A, %B, %C) : (memref<256x256xf16>, memref<256x256xf16>, memref<256x256xf32>) -> memref<256x256xf32>
110+
%gpu_result_cast = memref.cast %2 : memref<256x256xf32> to memref<*xf32>
111+
112+
// CHECK: Unranked Memref base@ = 0x{{[0-9a-f]+}}
113+
// CHECK-COUNT-256: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255]
114+
call @printMemrefF32(%gpu_result_cast) : (memref<*xf32>) -> ()
115+
memref.dealloc %A : memref<256x256xf16>
116+
memref.dealloc %B : memref<256x256xf16>
117+
memref.dealloc %C : memref<256x256xf32>
118+
memref.dealloc %C_ref : memref<256x256xf32>
119+
return
120+
}
121+
func.func private @printMemrefF16(memref<*xf16>) attributes {llvm.emit_c_interface}
122+
func.func private @printMemrefF32(memref<*xf32>) attributes {llvm.emit_c_interface}
123+
}

0 commit comments

Comments
 (0)