Skip to content

Commit cb898e2

Browse files
authored
[mlir] Make the print function in CRunnerUtil platform agnostic (llvm#86767)
The platform running on Apple Silicon does not seem to support the negative nan. It causes the test failure where we explicitly specify the negative nan bit pattern and check the output printed by the CRunnerUtil function. We can make the print function in the utility platform agnostic by using the standard library functions (i.e. `std::isnan` and `std::signbit`) so that we can run the test across platforms that do not support the negative bit pattern. I have added two test cases that would fail in the Apple Silicon platform without print function changes. ``` $ uname -a Darwin Kernel Version 23.3.0: Wed Dec 20 21:30:44 PST 2023; root:xnu-10002.81.5~7/RELEASE_ARM64_T6000 arm64 ``` See: https://discourse.llvm.org/t/test-failure-of-sparse-sign-test-in-apple-silicon/77876/3
1 parent bbfa506 commit cb898e2

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

mlir/lib/ExecutionEngine/CRunnerUtils.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,20 @@ void stdSort(uint64_t n, V *p) {
5151
// details of our vectors. Also useful for direct LLVM IR output.
5252
extern "C" void printI64(int64_t i) { fprintf(stdout, "%" PRId64, i); }
5353
extern "C" void printU64(uint64_t u) { fprintf(stdout, "%" PRIu64, u); }
54-
extern "C" void printF32(float f) { fprintf(stdout, "%g", f); }
55-
extern "C" void printF64(double d) { fprintf(stdout, "%lg", d); }
54+
extern "C" void printF32(float f) {
55+
if (std::isnan(f) && std::signbit(f)) {
56+
fprintf(stdout, "-nan");
57+
} else {
58+
fprintf(stdout, "%g", f);
59+
}
60+
}
61+
extern "C" void printF64(double d) {
62+
if (std::isnan(d) && std::signbit(d)) {
63+
fprintf(stdout, "-nan");
64+
} else {
65+
fprintf(stdout, "%lg", d);
66+
}
67+
}
5668
extern "C" void printString(char const *s) { fputs(s, stdout); }
5769
extern "C" void printOpen() { fputs("( ", stdout); }
5870
extern "C" void printClose() { fputs(" )", stdout); }

mlir/test/mlir-cpu-runner/test-expand-math-approx.mlir

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ func.func @func_powff64(%a : f64, %b : f64) {
190190
return
191191
}
192192

193+
func.func @func_powff32(%a : f32, %b : f32) {
194+
%r = math.powf %a, %b : f32
195+
vector.print %r : f32
196+
return
197+
}
198+
193199
func.func @powf() {
194200
// CHECK-NEXT: 16
195201
%a = arith.constant 4.0 : f64
@@ -230,7 +236,17 @@ func.func @powf() {
230236
%j = arith.constant 29385.0 : f64
231237
%j_p = arith.constant 23598.0 : f64
232238
call @func_powff64(%j, %j_p) : (f64, f64) -> ()
233-
return
239+
240+
// CHECK-NEXT: -nan
241+
%k = arith.constant 1.0 : f64
242+
%k_p = arith.constant 0xfff0000001000000 : f64
243+
call @func_powff64(%k, %k_p) : (f64, f64) -> ()
244+
245+
// CHECK-NEXT: -nan
246+
%l = arith.constant 1.0 : f32
247+
%l_p = arith.constant 0xffffffff : f32
248+
call @func_powff32(%l, %l_p) : (f32, f32) -> ()
249+
return
234250
}
235251

236252
// -------------------------------------------------------------------------- //

0 commit comments

Comments
 (0)