Skip to content

Commit 23a30e6

Browse files
[compiler-rt][ubsan] Add support for f16 (#129624)
LLVM supports long double <-> f16 conversions so we can remove the old FIXME.
1 parent 15770a1 commit 23a30e6

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

compiler-rt/lib/ubsan/ubsan_value.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,11 @@ FloatMax Value::getFloatValue() const {
120120
CHECK(getType().isFloatTy());
121121
if (isInlineFloat()) {
122122
switch (getType().getFloatBitWidth()) {
123-
#if 0
124-
// FIXME: OpenCL / NEON 'half' type. LLVM can't lower the conversion
125-
// from '__fp16' to 'long double'.
126-
case 16: {
127-
__fp16 Value;
128-
internal_memcpy(&Value, &Val, 4);
129-
return Value;
130-
}
131-
#endif
123+
case 16: {
124+
__fp16 Value;
125+
internal_memcpy(&Value, &Val, 2);
126+
return Value;
127+
}
132128
case 32: {
133129
float Value;
134130
#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
@@ -146,7 +142,7 @@ FloatMax Value::getFloatValue() const {
146142
internal_memcpy(&Value, &Val, 8);
147143
return Value;
148144
}
149-
}
145+
}
150146
} else {
151147
switch (getType().getFloatBitWidth()) {
152148
case 64: return *reinterpret_cast<double*>(Val);

compiler-rt/test/ubsan/TestCases/Float/cast-overflow.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// RUN: %run %t 5 2>&1 | FileCheck %s --check-prefix=CHECK-5
99
// RUN: %run %t 6 2>&1 | FileCheck %s --check-prefix=CHECK-6
1010
// RUN: %run %t 7 2>&1 | FileCheck %s --check-prefix=CHECK-7
11+
// RUN: %run %t 8 2>&1 | FileCheck %s --check-prefix=CHECK-8
1112

1213
// Issue #41838
1314
// XFAIL: sparc-target-arch && target={{.*solaris.*}}
@@ -132,12 +133,17 @@ int main(int argc, char **argv) {
132133
}
133134
case '5': {
134135
// CHECK-5: {{.*}}cast-overflow.cpp:[[@LINE+1]]:27: runtime error: {{.*}} is outside the range of representable values of type 'int'
136+
static int test_int = (__fp16)Inf;
137+
return 0;
138+
}
139+
case '6': {
140+
// CHECK-6: {{.*}}cast-overflow.cpp:[[@LINE+1]]:27: runtime error: {{.*}} is outside the range of representable values of type 'int'
135141
static int test_int = NaN;
136142
return 0;
137143
}
138144
// Integer -> floating point overflow.
139-
case '6': {
140-
// CHECK-6: cast-overflow.cpp:[[@LINE+2]]:{{27: runtime error: 3.40282e\+38 is outside the range of representable values of type 'int'| __int128 not supported}}
145+
case '7': {
146+
// CHECK-7: cast-overflow.cpp:[[@LINE+2]]:{{27: runtime error: 3.40282e\+38 is outside the range of representable values of type 'int'| __int128 not supported}}
141147
#if defined(__SIZEOF_INT128__) && !defined(_WIN32)
142148
static int test_int = (float)(FloatMaxAsUInt128 + 1);
143149
return 0;
@@ -148,9 +154,9 @@ int main(int argc, char **argv) {
148154
return 0;
149155
#endif
150156
}
151-
case '7': {
157+
case '8': {
152158
volatile long double ld = 300.0;
153-
// CHECK-7: {{.*}}cast-overflow.cpp:[[@LINE+1]]:14: runtime error: 300 is outside the range of representable values of type 'char'
159+
// CHECK-8: {{.*}}cast-overflow.cpp:[[@LINE+1]]:14: runtime error: 300 is outside the range of representable values of type 'char'
154160
char c = ld;
155161
// `c` is allowed to contain UNDEF, thus we should not use
156162
// its value as an exit code.

0 commit comments

Comments
 (0)