Skip to content

Commit a88f3a3

Browse files
authored
[Clang] Add __CLANG_GPU_DISABLE_MATH_WRAPPERS macro for offloading math (llvm#98234)
Summary: Currently we replace all math calls with vendor specific ones. This patch introduces a macro `__CLANG_GPU_DISABLE_MATH_WRAPPERS` that when defined will disable this. I went this route instead of a flag for two reasons. One, I think we have too many flags as is, and we already have `-nogpuinc` to cover disabling these wrappers entirely, so this would be a really specific subset of that. Second, these math headers aren't easily decoupled by simply not including a single header from the clang driver layer. There's the cmath and the regular math forward declares it would disable as well. Note, this currently causes errors because the GPU `libm` doesn't have `powi`, that's an NVIDIA extension I'll add to LLVM libm.
1 parent e9b7983 commit a88f3a3

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

clang/lib/Headers/__clang_cuda_math.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
#error "This file is for CUDA compilation only."
1313
#endif
1414

15+
// The __CLANG_GPU_DISABLE_MATH_WRAPPERS macro provides a way to let standard
16+
// libcalls reach the link step instead of being eagerly replaced.
17+
#ifndef __CLANG_GPU_DISABLE_MATH_WRAPPERS
18+
1519
#ifndef __OPENMP_NVPTX__
1620
#if CUDA_VERSION < 9000
1721
#error This file is intended to be used with CUDA-9+ only.
@@ -345,4 +349,5 @@ __DEVICE__ float ynf(int __a, float __b) { return __nv_ynf(__a, __b); }
345349
#pragma pop_macro("__DEVICE_VOID__")
346350
#pragma pop_macro("__FAST_OR_SLOW")
347351

352+
#endif // __CLANG_GPU_DISABLE_MATH_WRAPPERS
348353
#endif // __CLANG_CUDA_MATH_H__

clang/lib/Headers/__clang_hip_math.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
#error "This file is for HIP and OpenMP AMDGCN device compilation only."
1414
#endif
1515

16+
// The __CLANG_GPU_DISABLE_MATH_WRAPPERS macro provides a way to let standard
17+
// libcalls reach the link step instead of being eagerly replaced.
18+
#ifndef __CLANG_GPU_DISABLE_MATH_WRAPPERS
19+
1620
#if !defined(__HIPCC_RTC__)
1721
#include <limits.h>
1822
#include <stdint.h>
@@ -1321,4 +1325,5 @@ __host__ inline static int max(int __arg1, int __arg2) {
13211325
#pragma pop_macro("__RETURN_TYPE")
13221326
#pragma pop_macro("__FAST_OR_SLOW")
13231327

1328+
#endif // __CLANG_GPU_DISABLE_MATH_WRAPPERS
13241329
#endif // __CLANG_HIP_MATH_H__
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 5
2+
// RUN: %clang_cc1 -include __clang_hip_runtime_wrapper.h \
3+
// RUN: -internal-isystem %S/../../lib/Headers/cuda_wrappers \
4+
// RUN: -internal-isystem %S/Inputs/include \
5+
// RUN: -triple amdgcn-amd-amdhsa -aux-triple x86_64-unknown-unknown \
6+
// RUN: -target-cpu gfx906 -emit-llvm %s -fcuda-is-device -o - \
7+
// RUN: -D __CLANG_GPU_DISABLE_MATH_WRAPPERS | FileCheck -check-prefix=AMDGPU %s
8+
9+
// RUN: %clang_cc1 -include __clang_cuda_runtime_wrapper.h \
10+
// RUN: -internal-isystem %S/../../lib/Headers/cuda_wrappers \
11+
// RUN: -internal-isystem %S/Inputs/include \
12+
// RUN: -triple nvptx64-nvidia-cuda -aux-triple x86_64-unknown-unknown \
13+
// RUN: -target-cpu sm_90 -emit-llvm %s -fcuda-is-device -o - \
14+
// RUN: -D __CLANG_GPU_DISABLE_MATH_WRAPPERS | FileCheck -check-prefix=NVPTX %s
15+
16+
extern "C" double sin(double x);
17+
18+
// AMDGPU-LABEL: define dso_local noundef double @_Z3food(
19+
// AMDGPU-SAME: double noundef [[X:%.*]]) #[[ATTR0:[0-9]+]] {
20+
// AMDGPU-NEXT: [[ENTRY:.*:]]
21+
// AMDGPU-NEXT: [[RETVAL:%.*]] = alloca double, align 8, addrspace(5)
22+
// AMDGPU-NEXT: [[X_ADDR:%.*]] = alloca double, align 8, addrspace(5)
23+
// AMDGPU-NEXT: [[RETVAL_ASCAST:%.*]] = addrspacecast ptr addrspace(5) [[RETVAL]] to ptr
24+
// AMDGPU-NEXT: [[X_ADDR_ASCAST:%.*]] = addrspacecast ptr addrspace(5) [[X_ADDR]] to ptr
25+
// AMDGPU-NEXT: store double [[X]], ptr [[X_ADDR_ASCAST]], align 8
26+
// AMDGPU-NEXT: [[TMP0:%.*]] = load double, ptr [[X_ADDR_ASCAST]], align 8
27+
// AMDGPU-NEXT: [[TMP1:%.*]] = call double @llvm.sin.f64(double [[TMP0]])
28+
// AMDGPU-NEXT: ret double [[TMP1]]
29+
//
30+
// NVPTX-LABEL: define dso_local noundef double @_Z3food(
31+
// NVPTX-SAME: double noundef [[X:%.*]]) #[[ATTR0:[0-9]+]] {
32+
// NVPTX-NEXT: [[ENTRY:.*:]]
33+
// NVPTX-NEXT: [[X_ADDR:%.*]] = alloca double, align 8
34+
// NVPTX-NEXT: store double [[X]], ptr [[X_ADDR]], align 8
35+
// NVPTX-NEXT: [[TMP0:%.*]] = load double, ptr [[X_ADDR]], align 8
36+
// NVPTX-NEXT: [[TMP1:%.*]] = call double @llvm.sin.f64(double [[TMP0]])
37+
// NVPTX-NEXT: ret double [[TMP1]]
38+
//
39+
double foo(double x) {
40+
return sin(x);
41+
}

0 commit comments

Comments
 (0)