| Issue |
240288
|
| Summary |
[libc][GPU] GPU libc data model mismatches with host on Windows
|
| Labels |
libc
|
| Assignees |
|
| Reporter |
jinge90
|
We tested GPU libc on Intel GPU + L0 backend on Windows, some functions using 'long' type have runtime failures exposing a general issue which we believe also impacts AMD and NV platforms on Windows.
The scenario is we use GPU libc and libm in OpenMP offloading and SYCL toolchain, taking OpenMP offloading as example, for following code snippet(scalbln.c):
```c
double scalbln(double, long);
#pragma omp declare target
double foo(double d, long l) {
double res = scalbln(d + 0.5, l);
return res;
}
#pragma omp end declare target
```
Building with latest clang for amd and nv, then dump IR:
clang.exe -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa -Xopenmp-target=amdgcn-amd-amdhsa -march=gfx906 --offload-device-only -S -emit-llvm -o scalbln-amd.ll scalbln.c -O2 -nogpulib
```llvm
source_filename = "scalbln.c"
target datalayout = "e-m:e-p:64:64-p1:64:64-p2:32:32-p3:32:32-p4:64:64-p5:32:32-p6:32:32-p7:160:256:256:32-p8:128:128:128:48-p9:192:256:256:32-p10:32:32-p11:32:32-p12:32:32-p13:32:32-p14:32:32-p15:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64-S32-A5-G1-ni:7:8:9"
target triple = "amdgpu9.06-amd-amdhsa"
; Function Attrs: convergent nounwind uwtable
define hidden double @foo(double noundef %0, i32 noundef %1) local_unnamed_addr #0 {
%3 = fadd double %0, 5.000000e-01
%4 = tail call double @scalbln(double noundef %3, i32 noundef %1) #2
ret double %4
}
; Function Attrs: convergent
declare hidden double @scalbln(double noundef, i32 noundef) local_unnamed_addr #1
attributes #0 = { convergent nounwind uwtable "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
attributes #1 = { convergent "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
attributes #2 = { convergent nounwind }
!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7}
!llvm.ident = !{!8}
!0 = !{i32 1, !"amdhsa_code_object_version", i32 600}
!1 = !{i32 1, !"wchar_size", i32 2}
!2 = !{i32 2, !"debugger-tuning", !"gdb"}
!3 = !{i32 7, !"openmp", i32 51}
!4 = !{i32 7, !"openmp-device", i32 51}
!5 = !{i32 8, !"PIC Level", i32 2}
!6 = !{i32 7, !"uwtable", i32 2}
```
clang.exe -fopenmp -fopenmp-targets=nvptx64-nvidia-cuda -Xopenmp-target -march=sm_80 --cuda-device-only -S -emit-llvm -o scalbln-nv.ll scalbln.c -O2
```llvm
; ModuleID = 'scalbln.c'
source_filename = "scalbln.c"
target datalayout = "e-p6:32:32-i64:64-i128:128-i256:256-v16:16-v32:32-n16:32:64"
target triple = "nvptx64-nvidia-cuda"
@__omp_rtl_debug_kind = weak_odr hidden local_unnamed_addr constant i32 0
@__omp_rtl_assume_teams_oversubscription = weak_odr hidden local_unnamed_addr constant i32 0
@__omp_rtl_assume_threads_oversubscription = weak_odr hidden local_unnamed_addr constant i32 0
@__omp_rtl_assume_no_thread_state = weak_odr hidden local_unnamed_addr constant i32 0
@__omp_rtl_assume_no_nested_parallelism = weak_odr hidden local_unnamed_addr constant i32 0
; Function Attrs: convergent mustprogress nofree nounwind willreturn memory(none)
define hidden double @foo(double noundef %0, i32 noundef %1) local_unnamed_addr #0 {
%3 = fadd double %0, 5.000000e-01
%4 = tail call double @scalbln(double noundef %3, i32 noundef %1) #1
ret double %4
}
; Function Attrs: convergent mustprogress nofree nounwind willreturn memory(none)
declare dso_local double @scalbln(double noundef, i32 noundef) local_unnamed_addr #0
attributes #0 = { convergent mustprogress nofree nounwind willreturn memory(none) "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="sm_80" }
attributes #1 = { convergent nounwind willreturn memory(none) }
!llvm.module.flags = !{!0, !1, !2, !3, !4, !5}
!llvm.ident = !{!6}
!0 = !{i32 1, !"wchar_size", i32 2}
!1 = !{i32 2, !"debugger-tuning", !"gdb"}
!2 = !{i32 4, !"nvvm-reflect-ftz", i32 0}
!3 = !{i32 7, !"openmp", i32 51}
!4 = !{i32 7, !"openmp-device", i32 51}
!5 = !{i32 7, !"frame-pointer", i32 2}
!6 = !{!"clang version 24.0.0git (https://github.com/llvm/llvm-project.git bd7fe233446c29573c1fb03869b63b7b34f4f3f1)"}
```
As we can see, the 'long' type parameter is 'i32' in LLVM IR level which aligns with Windows host.
GPU libc and libm will provide definitions for scalbln, when we dump libm.bc IR for amd and nv on Windows:
AMD:
```llvm
; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(inaccessiblemem: readwrite)
define hidden noundef double @scalbln(double noundef %0, **i64 noundef %1**) #3 !guid !487 {
%3 = alloca i8, align 1, addrspace(5)
%4 = alloca i8, align 1, addrspace(5)
%5 = alloca i8, align 1, addrspace(5)
%6 = alloca i8, align 1, addrspace(5)
%7 = alloca i8, align 1, addrspace(5)
%8 = alloca i8, align 1, addrspace(5)
%9 = alloca i8, align 1, addrspace(5)
%10 = alloca i8, align 1, addrspace(5)
%11 = alloca i8, align 1, addrspace(5)
%12 = alloca i8, align 1, addrspace(5)
%13 = alloca i8, align 1, addrspace(5)
%14 = alloca i8, align 1, addrspace(5)
%15 = alloca i8, align 1, addrspace(5)
%16 = alloca i8, align 1, addrspace(5)
%17 = alloca i8, align 1, addrspace(5)
%18 = alloca i8, align 1, addrspace(5)
%19 = alloca i8, align 1, addrspace(5)
...
```
NV:
```llvm
; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(inaccessiblemem: readwrite)
define hidden noundef double @scalbln(double noundef %0, **i64 noundef %1**) #3 !guid !531 {
%3 = bitcast double %0 to i64
%4 = icmp eq i64 %1, 0
br i1 %4, label %115, label %5
5: ; preds = %2
%6 = fcmp oeq double %0, 0.000000e+00
%7 = and i64 %3, 9218868437227405312
%8 = icmp eq i64 %7, 9218868437227405312
%9 = or i1 %6, %8
br i1 %9, label %115, label %10, !prof !136
...
```
As we can see in the IR, 'long' type is 'i64' in LLVM IR level for Windows GPU libc/libm. So, when using GPU libc on Windows for OpenMP offloading or SYCL compiler, the caller and callee will have abi mismatch for functions such as scalbln, scalblnf, labs, ldiv...
The root cause for this issue is, when building offloading device code, clang will also pass '-aux-triple ${host-triple}' to device compilation and device compilation will honor '-aux-triple' and aligns data model between device and host. However, GPU libc/libm is not built in offloading mode, it is built directly with 'clang --target=amdgcn-amd-amdhsa|nvptx64-nvidia-cuda|spirv64-unknown-unknown -flto ....', no '-aux-triple' is passed to device compilation, so it uses 64-bit long type in the code snippet above.
I tried to add '-aux-triple x86_64-pc-windows-msvc' to CFLAGS for building GPU libc on Windows but found this flag was silently ignored by clang. The reason is, this usages of this flag in current clang sources are all surrounded by checking for offloading flags, so in direct GPU compilation, this flag is not used. In order to quicky enable GPU libc testing on Windows on our pre-ci systems, I applied a workaround to make clang to honor 'aux-triple' in direct GPU compilation in our downstream compiler: https://github.com/intel/llvm/pull/23721
This issue will not only impact long type on Windows, wchar_t will have problems too.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs