Skip to content

Commit 114f5ee

Browse files
[Backport to llvm_release_220] Update OCL nan builtin translation to work with bfloat type (#3638)
Backport of PR #3558 into `llvm_release_220`. All commits applied cleanly. Co-authored-by: Viktoria Maximova <[email protected]>
1 parent 4fab464 commit 114f5ee

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

lib/SPIRV/SPIRVUtil.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2660,7 +2660,7 @@ class OpenCLStdToSPIRVFriendlyIRMangleInfo : public BuiltinFuncMangleInfo {
26602660
public:
26612661
OpenCLStdToSPIRVFriendlyIRMangleInfo(OCLExtOpKind ExtOpId,
26622662
ArrayRef<Type *> ArgTys, Type *RetTy)
2663-
: ExtOpId(ExtOpId), ArgTys(ArgTys) {
2663+
: ExtOpId(ExtOpId), ArgTys(ArgTys), RetTy(RetTy) {
26642664

26652665
std::string Postfix = "";
26662666
if (needRetTypePostfix())
@@ -2676,6 +2676,11 @@ class OpenCLStdToSPIRVFriendlyIRMangleInfo : public BuiltinFuncMangleInfo {
26762676
case OpenCLLIB::Vloada_halfn:
26772677
case OpenCLLIB::Vloadn:
26782678
return true;
2679+
case OpenCLLIB::Nan:
2680+
// Only add return type mangling for bfloat16 to disambiguate from half
2681+
// (both are represented as i16 in LLVM). Float and half use traditional
2682+
// naming for backward compatibility.
2683+
return RetTy->getScalarType()->isBFloatTy();
26792684
default:
26802685
return false;
26812686
}
@@ -2741,6 +2746,7 @@ class OpenCLStdToSPIRVFriendlyIRMangleInfo : public BuiltinFuncMangleInfo {
27412746
private:
27422747
OCLExtOpKind ExtOpId;
27432748
ArrayRef<Type *> ArgTys;
2749+
Type *RetTy;
27442750
};
27452751
} // namespace
27462752

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

0 commit comments

Comments
 (0)