Skip to content

Commit 5c982d1

Browse files
committed
81 pass
1 parent 14f9431 commit 5c982d1

4 files changed

Lines changed: 453 additions & 163 deletions

File tree

ext/ze_runtime-sys/src/runner/ze_runner.c

Lines changed: 1 addition & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -192,76 +192,7 @@ ze_runner_result_t run_spirv_kernel(
192192

193193
if (kern_result != ZE_RESULT_SUCCESS) {
194194
DEBUG_PRINT("Failed to create kernel: %d", kern_result);
195-
196-
// Enhanced error reporting for kernel creation failure
197-
DEBUG_PRINT("=== KERNEL CREATION ERROR DETAILS ===");
198-
DEBUG_PRINT("Kernel name: %s", name);
199-
DEBUG_PRINT("SPIR-V module size: %zu bytes", spirv_size);
200-
201-
// Try to check if the kernel name exists in the module
202-
// Note: Level Zero doesn't provide a direct way to enumerate kernel names
203-
// This is left here as a comment for future enhancement if API adds this capability
204-
/*
205-
uint32_t kernel_count = 0;
206-
ze_result_t count_result = zeModuleGetKernelNames(module, &kernel_count, NULL);
207-
if (count_result == ZE_RESULT_SUCCESS && kernel_count > 0) {
208-
DEBUG_PRINT("Module contains %d kernels", kernel_count);
209-
210-
// Get kernel names
211-
const char** kernel_names = (const char**)malloc(kernel_count * sizeof(const char*));
212-
if (kernel_names) {
213-
if (zeModuleGetKernelNames(module, &kernel_count, kernel_names) == ZE_RESULT_SUCCESS) {
214-
DEBUG_PRINT("Available kernels in module:");
215-
for (uint32_t i = 0; i < kernel_count; i++) {
216-
DEBUG_PRINT(" - %s", kernel_names[i]);
217-
}
218-
}
219-
free(kernel_names);
220-
}
221-
}
222-
*/
223-
DEBUG_PRINT("Error details:");
224-
225-
// Check error code specifics
226-
if (kern_result == ZE_RESULT_ERROR_INVALID_ARGUMENT) {
227-
DEBUG_PRINT(" Error: Invalid argument (kernel descriptor or kernel handle is NULL)");
228-
}
229-
else if (kern_result == ZE_RESULT_ERROR_INVALID_KERNEL_NAME) {
230-
DEBUG_PRINT(" Error: Invalid kernel name (kernel '%s' not found in module)", name);
231-
232-
// Print the first few bytes of the SPIR-V for debugging
233-
DEBUG_PRINT("SPIR-V header (first 16 bytes, if available):");
234-
if (spirv_data && spirv_size >= 16) {
235-
const unsigned char* data = (const unsigned char*)spirv_data;
236-
DEBUG_PRINT(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X",
237-
data[0], data[1], data[2], data[3],
238-
data[4], data[5], data[6], data[7],
239-
data[8], data[9], data[10], data[11],
240-
data[12], data[13], data[14], data[15]);
241-
}
242-
243-
// Custom error code for kernel creation failure
244-
// Forward a custom error code to identify kernel name issues
245-
zeModuleDestroy(module);
246-
zeCommandListDestroy(command_list);
247-
zeContextDestroy(context);
248-
free(devices);
249-
free(drivers);
250-
return 2013265937; // Custom error code for kernel name not found
251-
}
252-
else if (kern_result == ZE_RESULT_ERROR_MODULE_BUILD_FAILURE) {
253-
DEBUG_PRINT(" Error: Module build failure");
254-
}
255-
else {
256-
DEBUG_PRINT(" Error: Unknown error code: %d", kern_result);
257-
}
258-
259-
zeModuleDestroy(module);
260-
zeCommandListDestroy(command_list);
261-
zeContextDestroy(context);
262-
free(devices);
263-
free(drivers);
264-
return ZE_RUNNER_ERROR_KERNEL;
195+
return ZE_RUNNER_ERROR_KERNEL_EXECUTION;
265196
}
266197

267198
DEBUG_PRINT("Kernel created successfully");

fix_spirv.sh

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,45 @@ set -e
33
LLVM_IR_FILE="$1"
44
SPIRV_FILE="$2"
55
STRIP_NAME=$(basename "$LLVM_IR_FILE" .ll)
6-
# Disassemble the LLVM IR
7-
llvm-dis-18 "$LLVM_IR_FILE" -o /tmp/temp$STRIP_NAME.ll
6+
7+
# Create a temporary file
8+
TEMP_LL_FILE="/tmp/temp$STRIP_NAME.ll"
9+
10+
# Check if the input file is already in text format
11+
if [[ "$LLVM_IR_FILE" == *.ll ]]; then
12+
# Already in text format, just copy it
13+
cp "$LLVM_IR_FILE" "$TEMP_LL_FILE"
14+
else
15+
# Disassemble the LLVM IR
16+
llvm-dis-18 "$LLVM_IR_FILE" -o "$TEMP_LL_FILE"
17+
fi
818

919
# Fix address spaces - ensure all global variables have explicit address space 0
10-
sed -i -E 's/@_([0-9]+) = internal global/@_\1 = internal addrspace(1) global/g' /tmp/temp$STRIP_NAME.ll
11-
sed -i 's/addrspace(5)/addrspace(0)/g' /tmp/temp$STRIP_NAME.ll
12-
sed -i 's/addrspace(4) byref/addrspace(1) byref/g' /tmp/temp$STRIP_NAME.ll
13-
14-
# 同时也需要修改加载这些参数的指令
15-
sed -i 's/load i64, ptr addrspace(4) %"\([0-9]\+\)", align/load i64, ptr addrspace(1) %"\1", align/g' /tmp/temp$STRIP_NAME.ll
16-
17-
# 更新元数据中的函数签名(如果有)
18-
sed -i 's/!0 = !{.*ptr addrspace(4)/!0 = !{ptr @vector_extract, !"vector_extract"}/g' /tmp/temp$STRIP_NAME.ll
19-
sed -i 's/amdgpu_kernel/spir_kernel/g' /tmp/temp$STRIP_NAME.ll
20-
# 为inttoptr到addrspace(1)的每个实例创建新的变量序列
21-
sed -i 's/%"\([0-9]\+\)" = inttoptr \(.*\) to ptr addrspace(1)/%"\1_tmp" = inttoptr \2 to ptr addrspace(4)\n %"\1" = addrspacecast ptr addrspace(4) %"\1_tmp" to ptr addrspace(1)/g' /tmp/temp$STRIP_NAME.ll
20+
sed -i -E 's/@_([0-9]+) = internal global/@_\1 = internal addrspace(1) global/g' "$TEMP_LL_FILE"
21+
sed -i 's/addrspace(5)/addrspace(0)/g' "$TEMP_LL_FILE"
22+
sed -i 's/addrspace(4) byref/addrspace(1) byref/g' "$TEMP_LL_FILE"
23+
24+
# Update metadata for function signatures
25+
sed -i 's/amdgpu_kernel/spir_kernel/g' "$TEMP_LL_FILE"
26+
27+
# Fix inttoptr to addrspace(1) conversions
28+
sed -i 's/%"\([0-9]\+\)" = inttoptr \(.*\) to ptr addrspace(1)/%"\1_tmp" = inttoptr \2 to ptr addrspace(4)\n %"\1" = addrspacecast ptr addrspace(4) %"\1_tmp" to ptr addrspace(1)/g' "$TEMP_LL_FILE"
29+
30+
# Fix the specific issue with direct addrspacecast from default address space to addrspace(1)
31+
# First replace pattern like:
32+
# %2 = inttoptr i64 %"42" to ptr
33+
# %"49" = addrspacecast ptr %2 to ptr addrspace(1)
34+
# Find all inttoptr without address space specification and add addrspace(0)
35+
sed -i 's/\(%[0-9]\+\) = inttoptr \(.*\) to ptr$/\1 = inttoptr \2 to ptr addrspace(0)/g' "$TEMP_LL_FILE"
36+
sed -i 's/\(%"[0-9]\+"\) = addrspacecast ptr \(%[0-9]\+\) to ptr/\1 = addrspacecast ptr addrspace(4) \2 to ptr/g' "$TEMP_LL_FILE"
37+
38+
# Then find all addrspacecast from ptr to ptr addrspace(1) and replace with double cast through addrspace(0)
39+
sed -i 's/\(%"[0-9]\+"\) = addrspacecast ptr \(%[0-9]\+\) to ptr addrspace(1)/\1_gen = addrspacecast ptr addrspace(0) \2 to ptr addrspace(0)\n \1 = addrspacecast ptr addrspace(0) \1_gen to ptr addrspace(1)/g' "$TEMP_LL_FILE"
40+
41+
sed -i 's/\(%[0-9]\+\) = addrspacecast ptr addrspace(3) \(%"[0-9]\+"\) to ptr\n\(%[0-9]\+\) = addrspacecast ptr addrspace(3) \(%"[0-9]\+"\) to ptr/\1 = addrspacecast ptr addrspace(3) \2 to ptr\n\3 = addrspacecast ptr addrspace(3) \4 to ptr/g' "$TEMP_LL_FILE"
42+
sed -i 's/\(%[0-9]\+\) = addrspacecast ptr addrspace(3) \(%"[0-9]\+"\) to ptr/\1 = addrspacecast ptr addrspace(3) \2 to ptr addrspace(4)/g' "$TEMP_LL_FILE"
43+
2244
# Reassemble and convert to SPIR-V
23-
llvm-as-18 /tmp/temp$STRIP_NAME.ll -o /tmp/temp$STRIP_NAME.bc
24-
llvm-spirv-18 /tmp/temp$STRIP_NAME.bc -o "$SPIRV_FILE"
45+
TEMP_BC_FILE="/tmp/temp$STRIP_NAME.bc"
46+
llvm-as-18 "$TEMP_LL_FILE" -o "$TEMP_BC_FILE"
47+
llvm-spirv-18 "$TEMP_BC_FILE" -o "$SPIRV_FILE"

0 commit comments

Comments
 (0)