Author: Keshav Vinayak Jha Date: 2026-11-10T08:27:58+05:30 New Revision: dbb0418043e246d31aaab324dbe0c1e7097be391
URL: https://github.com/llvm/llvm-project/commit/dbb0418043e246d31aaab324dbe0c1e7097be391 DIFF: https://github.com/llvm/llvm-project/commit/dbb0418043e246d31aaab324dbe0c1e7097be391.diff LOG: [HIP] Add __exp2f to the HIP math header (#236328) Add `__exp2f` to `__clang_hip_math.h`, next to `__expf` and `__exp10f`. It lowers to `llvm.amdgcn.exp2`, which is a bare `v_exp_f32`. `exp2f` goes through `llvm.exp2`. For f32, `AMDGPU` wraps `v_exp_f32` in denormal handling unless f32 denormals are flushed. Kernels that know their inputs stay above -126, such as the softmax in flash-attention, only need the instruction. `__expf` and `__exp10f` already wrap the same builtin, so this fills in the missing member of that family. **CUDA does not define `__exp2f`, so this is a HIP-only extension.** Assisted-by: Claude code Signed-off-by: Keshav Vinayak Jha <[email protected]> Added: Modified: clang/docs/ReleaseNotes.md clang/lib/Headers/__clang_hip_math.h clang/test/Headers/__clang_hip_math.hip Removed: ################################################################################ diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index ae351da057cc8d..9c36e1e1679457 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -1412,6 +1412,11 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and link-time failures. `dynamic_cast<void*>` is still allowed because it does not use RTTI. +- The HIP math header now provides `__exp2f`, a fast `exp2` intrinsic that + lowers to a single `v_exp_f32` without the denormal handling that `exp2f` + adds, so results in the denormal range may be flushed to zero. CUDA has no + `__exp2f`, so it is a HIP-only extension. + #### CUDA Support - Fixed missing diagnostics for wrong-side functions reached through constructor diff --git a/clang/lib/Headers/__clang_hip_math.h b/clang/lib/Headers/__clang_hip_math.h index c1fc112c95a4c1..2b673ed4c5b015 100644 --- a/clang/lib/Headers/__clang_hip_math.h +++ b/clang/lib/Headers/__clang_hip_math.h @@ -166,6 +166,9 @@ float __exp10f(float __x) { return __builtin_amdgcn_exp2f(__log2_10 * __x); } +__DEVICE__ +float __exp2f(float __x) { return __builtin_amdgcn_exp2f(__x); } + __DEVICE__ float __expf(float __x) { const float __log2_e = 0x1.715476p+0; diff --git a/clang/test/Headers/__clang_hip_math.hip b/clang/test/Headers/__clang_hip_math.hip index 8440b3fa08d37a..9a004ac06f083b 100644 --- a/clang/test/Headers/__clang_hip_math.hip +++ b/clang/test/Headers/__clang_hip_math.hip @@ -8335,6 +8335,40 @@ extern "C" __device__ float test___exp10f(float x) { return __exp10f(x); } +// DEFAULT-LABEL: define dso_local noundef float @test___exp2f( +// DEFAULT-SAME: float noundef [[X:%.*]]) local_unnamed_addr #[[ATTR3]] { +// DEFAULT-NEXT: [[ENTRY:.*:]] +// DEFAULT-NEXT: [[TMP0:%.*]] = tail call contract noundef float @llvm.amdgcn.exp2.f32(float [[X]]) +// DEFAULT-NEXT: ret float [[TMP0]] +// +// FINITEONLY-LABEL: define dso_local noundef nofpclass(nan inf) float @test___exp2f( +// FINITEONLY-SAME: float noundef nofpclass(nan inf) [[X:%.*]]) local_unnamed_addr #[[ATTR3]] { +// FINITEONLY-NEXT: [[ENTRY:.*:]] +// FINITEONLY-NEXT: [[TMP0:%.*]] = tail call nnan ninf contract noundef nofpclass(nan inf) float @llvm.amdgcn.exp2.f32(float nofpclass(nan inf) [[X]]) +// FINITEONLY-NEXT: ret float [[TMP0]] +// +// APPROX-LABEL: define dso_local noundef float @test___exp2f( +// APPROX-SAME: float noundef [[X:%.*]]) local_unnamed_addr #[[ATTR3]] { +// APPROX-NEXT: [[ENTRY:.*:]] +// APPROX-NEXT: [[TMP0:%.*]] = tail call contract noundef float @llvm.amdgcn.exp2.f32(float [[X]]) +// APPROX-NEXT: ret float [[TMP0]] +// +// NCRDIV-LABEL: define dso_local noundef float @test___exp2f( +// NCRDIV-SAME: float noundef [[X:%.*]]) local_unnamed_addr #[[ATTR3]] { +// NCRDIV-NEXT: [[ENTRY:.*:]] +// NCRDIV-NEXT: [[TMP0:%.*]] = tail call contract noundef float @llvm.amdgcn.exp2.f32(float [[X]]) +// NCRDIV-NEXT: ret float [[TMP0]] +// +// AMDGCNSPIRV-LABEL: define spir_func noundef float @test___exp2f( +// AMDGCNSPIRV-SAME: float noundef [[X:%.*]]) local_unnamed_addr addrspace(4) #[[ATTR3]] { +// AMDGCNSPIRV-NEXT: [[ENTRY:.*:]] +// AMDGCNSPIRV-NEXT: [[TMP0:%.*]] = tail call contract noundef addrspace(4) float @llvm.amdgcn.exp2.f32(float [[X]]) +// AMDGCNSPIRV-NEXT: ret float [[TMP0]] +// +extern "C" __device__ float test___exp2f(float x) { + return __exp2f(x); +} + // DEFAULT-LABEL: define dso_local noundef float @test___expf( // DEFAULT-SAME: float noundef [[X:%.*]]) local_unnamed_addr #[[ATTR3]] { // DEFAULT-NEXT: [[ENTRY:.*:]] _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
