Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cf5e0cb

Browse files
adelejjehclaude
andcommitted
device-libs: Guard trig reduction quadrant index against NaN UB
Guard the `(int)fn & 0x3` quadrant index computation in trig reduction functions against NaN input. `fptosi NaN` is UB in C and produces `poison` in LLVM IR, which the compiler exploits during constant-folding to return garbage from `cos(inf)`, `sin(inf)`, etc. Fix by adding an isnan check: `isnan(fn) ? 0 : ((int)fn & 0x3)`. The AMDGPU backend folds away the guard at codegen since v_cvt_i32_f32 already returns 0 for NaN (see llvm#200960). Fixes: LCOMPILER-2150 Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent 03e8eaa commit cf5e0cb

6 files changed

Lines changed: 7 additions & 7 deletions

File tree

amd/device-libs/ocml/src/trigpiredD.cl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ MATH_PRIVATE(trigpired)(double x)
1717

1818
struct redret ret;
1919
ret.hi = MATH_MAD(t, -0.5, x);
20-
ret.i = (int)t & 0x3;
20+
ret.i = BUILTIN_ISNAN_F64(t) ? 0 : ((int)t & 0x3);
2121
return ret;
2222
}
2323

amd/device-libs/ocml/src/trigpiredF.cl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ MATH_PRIVATE(trigpired)(float x)
1717

1818
struct redret ret;
1919
ret.hi = MATH_MAD(t, -0.5f, x);
20-
ret.i = (int)t & 0x3;
20+
ret.i = BUILTIN_ISNAN_F32(t) ? 0 : ((int)t & 0x3);
2121
return ret;
2222
}
2323

amd/device-libs/ocml/src/trigpiredH.cl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ MATH_PRIVATE(trigpired)(half x)
1717

1818
struct redret ret;
1919
ret.hi = MATH_MAD(t, -0.5h, x);
20-
ret.i = (short)t & (short)0x3;
20+
ret.i = BUILTIN_ISNAN_F16(t) ? (short)0 : ((short)t & (short)0x3);
2121
return ret;
2222
}
2323

amd/device-libs/ocml/src/trigredH.cl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ MATH_PRIVATE(trigred)(half hx)
2121

2222
struct redret ret;
2323
ret.hi = (half)BUILTIN_MAD_F32(fn, -pb2_c, BUILTIN_MAD_F32(fn, -pb2_b, BUILTIN_MAD_F32(fn, -pb2_a, x)));
24-
ret.i = (int)fn & 0x3;
24+
ret.i = BUILTIN_ISNAN_F32(fn) ? 0 : ((int)fn & 0x3);
2525
return ret;
2626
}
2727

amd/device-libs/ocml/src/trigredsmallD.cl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ MATH_PRIVATE(trigredsmall)(double x)
3030
struct redret ret;
3131
ret.hi = rh;
3232
ret.lo = rt;
33-
ret.i = (int)dn & 0x3;
33+
ret.i = BUILTIN_ISNAN_F64(dn) ? 0 : ((int)dn & 0x3);
3434
return ret;
3535
}
3636

amd/device-libs/ocml/src/trigredsmallF.cl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ mad_reduce(float x)
5353

5454
struct redret ret;
5555
ret.hi = MATH_MAD(-piby2_l, fn, r);
56-
ret.i = (int)fn & 0x3;
56+
ret.i = BUILTIN_ISNAN_F32(fn) ? 0 : ((int)fn & 0x3);
5757
return ret;
5858
#endif
5959
}
@@ -87,7 +87,7 @@ fma_reduce(float x)
8787
ret.hi = r;
8888
#endif
8989

90-
ret.i =(int)fn & 0x3;
90+
ret.i = BUILTIN_ISNAN_F32(fn) ? 0 : ((int)fn & 0x3);
9191
return ret;
9292
}
9393

0 commit comments

Comments
 (0)