Skip to content

Commit 3f0254a

Browse files
[Backport to llvm_release_210] Update OCL nan builtin translation to work with bfloat type (#3639)
Backport of PR #3558 into `llvm_release_210`. All commits applied cleanly. --------- Co-authored-by: Viktoria Maximova <viktoria.maksimova@intel.com>
1 parent 24a586a commit 3f0254a

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

lib/SPIRV/SPIRVUtil.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2614,7 +2614,7 @@ class OpenCLStdToSPIRVFriendlyIRMangleInfo : public BuiltinFuncMangleInfo {
26142614
public:
26152615
OpenCLStdToSPIRVFriendlyIRMangleInfo(OCLExtOpKind ExtOpId,
26162616
ArrayRef<Type *> ArgTys, Type *RetTy)
2617-
: ExtOpId(ExtOpId), ArgTys(ArgTys) {
2617+
: ExtOpId(ExtOpId), ArgTys(ArgTys), RetTy(RetTy) {
26182618

26192619
std::string Postfix = "";
26202620
if (needRetTypePostfix())
@@ -2630,6 +2630,11 @@ class OpenCLStdToSPIRVFriendlyIRMangleInfo : public BuiltinFuncMangleInfo {
26302630
case OpenCLLIB::Vloada_halfn:
26312631
case OpenCLLIB::Vloadn:
26322632
return true;
2633+
case OpenCLLIB::Nan:
2634+
// Only add return type mangling for bfloat16 to disambiguate from half
2635+
// (both are represented as i16 in LLVM). Float and half use traditional
2636+
// naming for backward compatibility.
2637+
return RetTy->getScalarType()->isBFloatTy();
26332638
default:
26342639
return false;
26352640
}
@@ -2695,6 +2700,7 @@ class OpenCLStdToSPIRVFriendlyIRMangleInfo : public BuiltinFuncMangleInfo {
26952700
private:
26962701
OCLExtOpKind ExtOpId;
26972702
ArrayRef<Type *> ArgTys;
2703+
Type *RetTy;
26982704
};
26992705
} // namespace
27002706

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
; RUN: llvm-as %s -o %t.bc
2+
; RUN: llvm-spirv %t.bc --spirv-ext=+SPV_KHR_bfloat16 -o %t.spv
3+
; RUN: llvm-spirv %t.spv -to-text -o %t.spt
4+
; RUN: FileCheck < %t.spt %s --check-prefix=CHECK-SPIRV
5+
6+
; RUN: llvm-spirv -r %t.spv --spirv-target-env=SPV-IR -o - | llvm-dis -o %t.rev.ll
7+
; RUN: FileCheck < %t.rev.ll %s --check-prefixes=CHECK-SPV-IR
8+
9+
; Check OpenCL built-in nan translation.
10+
; Verify it's possible to distinguish between bfloat and half versions.
11+
12+
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-n8:16:32:64-G1"
13+
target triple = "spir64"
14+
15+
; CHECK-SPIRV: TypeFloat [[#BFLOAT:]] 16 0 {{$}}
16+
; CHECK-SPIRV: TypeFloat [[#HALF:]] 16 {{$}}
17+
; CHECK-SPIRV: ExtInst [[#BFLOAT]] [[#]] [[#]] nan
18+
; CHECK-SPIRV: ExtInst [[#HALF]] [[#]] [[#]] nan
19+
20+
; CHECK-SPV-IR: call spir_func bfloat @_Z22__spirv_ocl_nan_RDF16bt(
21+
; CHECK-SPV-IR: call spir_func half @_Z15__spirv_ocl_nant(
22+
23+
define dso_local spir_kernel void @test_bfloat(ptr addrspace(1) align 2 %a, i16 %b) {
24+
entry:
25+
%call = tail call spir_func bfloat @_Z23__spirv_ocl_nan__RDF16bt(i16 %b)
26+
%call2 = tail call spir_func half @_Z22__spirv_ocl_nan__Rhalft(i16 %b)
27+
ret void
28+
}
29+
30+
declare spir_func bfloat @_Z23__spirv_ocl_nan__RDF16bt(i16)
31+
declare spir_func half @_Z22__spirv_ocl_nan__Rhalft(i16)
32+
33+
34+
!opencl.ocl.version = !{!0}
35+
36+
!0 = !{i32 3, i32 0}

0 commit comments

Comments
 (0)