Skip to content

Commit 1c1d918

Browse files
committed
95 pass
1 parent 5c982d1 commit 1c1d918

7 files changed

Lines changed: 59 additions & 18 deletions

File tree

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,17 @@ ze_runner_result_t run_spirv_kernel(
128128

129129
DEBUG_PRINT("Command list created successfully");
130130

131+
// 设置编译标志,启用原子操作支持
132+
const char* build_flags = "-ze-intel-enable-atomics";
133+
131134
// 创建模块
132135
ze_module_desc_t module_desc = {
133136
.stype = ZE_STRUCTURE_TYPE_MODULE_DESC,
134137
.pNext = NULL,
135138
.format = ZE_MODULE_FORMAT_IL_SPIRV,
136139
.inputSize = spirv_size,
137140
.pInputModule = spirv_data,
138-
.pBuildFlags = NULL,
141+
.pBuildFlags = build_flags,
139142
.pConstants = NULL
140143
};
141144

@@ -192,6 +195,11 @@ ze_runner_result_t run_spirv_kernel(
192195

193196
if (kern_result != ZE_RESULT_SUCCESS) {
194197
DEBUG_PRINT("Failed to create kernel: %d", kern_result);
198+
zeModuleDestroy(module);
199+
zeCommandListDestroy(command_list);
200+
zeContextDestroy(context);
201+
free(devices);
202+
free(drivers);
195203
return ZE_RUNNER_ERROR_KERNEL_EXECUTION;
196204
}
197205

fix_spirv.sh

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,25 @@ else
1515
# Disassemble the LLVM IR
1616
llvm-dis-18 "$LLVM_IR_FILE" -o "$TEMP_LL_FILE"
1717
fi
18-
18+
# Fix call
1919
# Fix address spaces - ensure all global variables have explicit address space 0
2020
sed -i -E 's/@_([0-9]+) = internal global/@_\1 = internal addrspace(1) global/g' "$TEMP_LL_FILE"
2121
sed -i 's/addrspace(5)/addrspace(0)/g' "$TEMP_LL_FILE"
2222
sed -i 's/addrspace(4) byref/addrspace(1) byref/g' "$TEMP_LL_FILE"
2323

24+
# 修复uinc_wrap原子操作 - 将其替换为add操作
25+
sed -i 's/atomicrmw uinc_wrap/atomicrmw add/g' "$TEMP_LL_FILE"
26+
2427
# Update metadata for function signatures
2528
sed -i 's/amdgpu_kernel/spir_kernel/g' "$TEMP_LL_FILE"
2629

2730
# Fix inttoptr to addrspace(1) conversions
2831
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"
2932

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"
33+
# Fix inttoptr with non-quoted variable names (%2 instead of %"2")
34+
sed -i 's/\(%[0-9]\+\) = inttoptr \(.*\) to ptr$/\1 = inttoptr \2 to ptr addrspace(4)/g' "$TEMP_LL_FILE"
35+
36+
# Fix addrspacecast instructions for non-quoted variables
3637
sed -i 's/\(%"[0-9]\+"\) = addrspacecast ptr \(%[0-9]\+\) to ptr/\1 = addrspacecast ptr addrspace(4) \2 to ptr/g' "$TEMP_LL_FILE"
3738

3839
# Then find all addrspacecast from ptr to ptr addrspace(1) and replace with double cast through addrspace(0)
@@ -41,7 +42,21 @@ sed -i 's/\(%"[0-9]\+"\) = addrspacecast ptr \(%[0-9]\+\) to ptr addrspace(1)/\1
4142
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"
4243
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"
4344

44-
# Reassemble and convert to SPIR-V
45+
# Fix non-quoted variable inttoptr (already moved up to execute earlier)
46+
# sed -i 's/\(%[0-9]\+\) = inttoptr \(.*\) to ptr$/\1 = inttoptr \2 to ptr addrspace(4)/g' "$TEMP_LL_FILE"
47+
sed -i 's/%"75" = addrspacecast ptr addrspace(4) %2 to ptr addrspace(4)/%"75" = addrspacecast ptr %2 to ptr addrspace(4)/g' "$TEMP_LL_FILE"
48+
49+
# Reassemble
4550
TEMP_BC_FILE="/tmp/temp$STRIP_NAME.bc"
4651
llvm-as-18 "$TEMP_LL_FILE" -o "$TEMP_BC_FILE"
47-
llvm-spirv-18 "$TEMP_BC_FILE" -o "$SPIRV_FILE"
52+
53+
# 将库文件转换为文本格式,避免属性组不兼容问题
54+
LIB_FILE="/home/try/Documents/ZLUDA/ptx/lib/zluda_ptx_ze_impl.bc"
55+
56+
# 尝试反汇编库文件,如果失败则跳过合并步骤
57+
COMBINED_BC_FILE="/tmp/combined$STRIP_NAME.bc"
58+
echo /opt/intel/oneapi/2025.1/bin/compiler/llvm-link "$LIB_FILE" "$TEMP_BC_FILE" -o "$COMBINED_BC_FILE"
59+
60+
/opt/intel/oneapi/2025.1/bin/compiler/llvm-link "$LIB_FILE" "$TEMP_BC_FILE" -o "$COMBINED_BC_FILE"
61+
# 使用合并后的文件生成SPIR-V
62+
/opt/intel/oneapi/2025.1/bin/compiler/llvm-spirv "$COMBINED_BC_FILE" -o "$SPIRV_FILE" --spirv-ext=+all,-SPV_KHR_untyped_pointers

llvm_zluda/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ fn main() {
4545
.unwrap();
4646
println!("cargo:rustc-link-arg={ldflags}");
4747
println!("cargo:rustc-link-search=native={libdir}");
48+
println!("cargo:rustc-link-search=native={libdir}/../../../../../../../ext/llvm-project/build/lib");
4849
for lib in system_libs.split_ascii_whitespace() {
4950
println!("cargo:rustc-link-arg={lib}");
5051
}
@@ -128,4 +129,5 @@ fn link_llvm_components(components: String) {
128129
};
129130
println!("cargo:rustc-link-lib={component}");
130131
}
132+
println!("cargo:rustc-link-lib=LLVMTarget");
131133
}

llvm_zluda/src/lib.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
#pragma GCC diagnostic ignored "-Wunused-parameter"
33
#include <llvm-c/Core.h>
44
#include <llvm-c/DebugInfo.h>
5+
#include <llvm-c/Target.h>
56
#include <llvm/ADT/PointerUnion.h>
67
#include <llvm/IR/DIBuilder.h>
78
#include <llvm/IR/DebugInfoMetadata.h>
89
#include <llvm/IR/IRBuilder.h>
910
#include <llvm/IR/Instructions.h>
1011
#include <llvm/IR/Type.h>
1112

12-
1313
#pragma GCC diagnostic pop
1414

1515
using namespace llvm;
@@ -212,5 +212,9 @@ LLVMZludaInsertDeclareAtEnd(LLVMBuilderRef Builder, LLVMValueRef Storage,
212212

213213
return wrap(Call);
214214
}
215+
unsigned long long LLVMZludaSizeOfTypeInBits(LLVMTargetDataRef TD,
216+
LLVMTypeRef Ty) {
217+
return LLVMSizeOfTypeInBits(TD, Ty);
218+
}
215219

216220
LLVM_C_EXTERN_C_END

llvm_zluda/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,6 @@ extern "C" {
9393
DL: LLVMMetadataRef,
9494
InsertAtEnd: LLVMBasicBlockRef,
9595
) -> LLVMValueRef;
96+
97+
pub fn LLVMZludaSizeOfTypeInBits(TD: LLVMContextRef, Ty: LLVMTypeRef) -> u64;
9698
}

ptx/src/pass/emit_llvm.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,15 @@ use std::{i8, ptr};
3333
use super::*;
3434
use llvm_zluda::analysis::{LLVMVerifierFailureAction, LLVMVerifyModule};
3535
use llvm_zluda::bit_writer::LLVMWriteBitcodeToMemoryBuffer;
36-
use llvm_zluda::{core::*, *};
37-
use llvm_zluda::{prelude::*, LLVMZludaBuildAtomicRMW};
38-
use llvm_zluda::{LLVMCallConv, LLVMZludaBuildAlloca};
36+
use llvm_zluda::prelude::*;
37+
use llvm_zluda::target::{LLVMGetModuleDataLayout, LLVMSizeOfTypeInBits};
38+
use llvm_zluda::{core::*, LLVMAtomicOrdering, LLVMIntPredicate, LLVMRealPredicate};
39+
use llvm_zluda::{
40+
LLVMAttributeFunctionIndex, LLVMCallConv, LLVMZludaAtomicRMWBinOp, LLVMZludaBuildAlloca,
41+
LLVMZludaBuildAtomicCmpXchg, LLVMZludaBuildAtomicRMW, LLVMZludaBuildFence,
42+
LLVMZludaFastMathAllowReciprocal, LLVMZludaFastMathApproxFunc, LLVMZludaFastMathNone,
43+
LLVMZludaSetFastMathFlags,
44+
};
3945

4046
const LLVM_UNNAMED: &CStr = c"";
4147
// https://llvm.org/docs/AMDGPUUsage.html#address-spaces
@@ -434,7 +440,7 @@ impl<'a, 'input> ModuleEmitContext<'a, 'input> {
434440
&mut self,
435441
type_: &ast::Type,
436442
array_init: &[u8],
437-
global: *mut llvm_zluda::LLVMValue,
443+
global: LLVMValueRef,
438444
) -> Result<(), TranslateError> {
439445
match type_ {
440446
ast::Type::Array(None, scalar, dimensions) => {
@@ -1357,7 +1363,7 @@ impl<'a> MethodEmitContext<'a> {
13571363
if repack.is_extract {
13581364
let src = self.resolver.value(repack.packed)?;
13591365
for (index, dst) in repack.unpacked.iter().enumerate() {
1360-
let index: *mut LLVMValue = unsafe { LLVMConstInt(i8_type, index as _, 0) };
1366+
let index: LLVMValueRef = unsafe { LLVMConstInt(i8_type, index as _, 0) };
13611367
self.resolver.with_result(*dst, |dst| unsafe {
13621368
LLVMBuildExtractElement(self.builder, src, index, dst)
13631369
});
@@ -1723,8 +1729,9 @@ impl<'a> MethodEmitContext<'a> {
17231729

17241730
// Check if bitcast is valid between these types
17251731
// Bitcast requires same size types
1726-
let src_size = unsafe { LLVMSizeOfTypeInBits(self.context, src_type) };
1727-
let dst_size = unsafe { LLVMSizeOfTypeInBits(self.context, dst_type) };
1732+
let data_layout = unsafe { LLVMGetModuleDataLayout(self.module) };
1733+
let src_size = unsafe { LLVMSizeOfTypeInBits(data_layout, src_type) };
1734+
let dst_size = unsafe { LLVMSizeOfTypeInBits(data_layout, dst_type) };
17281735

17291736
if src_size == dst_size {
17301737
// Same size, we can use bitcast

ptx/src/pass/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ mod replace_instructions_with_function_calls;
2929
mod replace_known_functions;
3030
mod resolve_function_pointers;
3131

32+
#[cfg(feature = "amd")]
3233
static ZLUDA_PTX_IMPL: &'static [u8] = include_bytes!("../../lib/zluda_ptx_impl.bc");
34+
#[cfg(feature = "intel")]
35+
static ZLUDA_PTX_IMPL: &'static [u8] = include_bytes!("../../lib/zluda_ptx_ze_impl.bc");
3336
const ZLUDA_PTX_PREFIX: &'static str = "__zluda_ptx_impl_";
3437

3538
quick_error! {

0 commit comments

Comments
 (0)