Skip to content

Commit 17d8334

Browse files
committed
[OpenMP] Allow <math.h> to go first in C++-mode in target regions
If we are in C++ mode and include <math.h> (not <cmath>) first, we still need to make sure <cmath> is read first. The problem otherwise is that we haven't seen the declarations of the math.h functions when the system math.h includes our cmath overlay. However, our cmath overlay, or better the underlying overlay, e.g. CUDA, uses the math.h functions. Since we haven't declared them yet we get errors. CUDA avoids this by eagerly declaring all math functions (in the __device__ space) but we cannot do this. Instead we break the dependence by forcing cmath to go first. Reviewed By: JonChesterfield Differential Revision: https://reviews.llvm.org/D77774
1 parent 03ff643 commit 17d8334

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

clang/lib/Headers/openmp_wrappers/math.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@
77
*===-----------------------------------------------------------------------===
88
*/
99

10+
// If we are in C++ mode and include <math.h> (not <cmath>) first, we still need
11+
// to make sure <cmath> is read first. The problem otherwise is that we haven't
12+
// seen the declarations of the math.h functions when the system math.h includes
13+
// our cmath overlay. However, our cmath overlay, or better the underlying
14+
// overlay, e.g. CUDA, uses the math.h functions. Since we haven't declared them
15+
// yet we get errors. CUDA avoids this by eagerly declaring all math functions
16+
// (in the __device__ space) but we cannot do this. Instead we break the
17+
// dependence by forcing cmath to go first. While our cmath will in turn include
18+
// this file, the cmath guards will prevent recursion.
19+
#ifdef __cplusplus
20+
#include <cmath>
21+
#endif
22+
1023
#ifndef __CLANG_OPENMP_MATH_H__
1124
#define __CLANG_OPENMP_MATH_H__
1225

clang/test/Headers/Inputs/include/math.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,7 @@ float ynf(int __a, float __b);
197197
* math functions.
198198
*/
199199
#define HUGE_VAL (__builtin_huge_val())
200+
201+
#ifdef __cplusplus
202+
#include <cmath>
203+
#endif

clang/test/Headers/nvptx_device_math_sincos.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
// REQUIRES: nvptx-registered-target
22
// RUN: %clang_cc1 -internal-isystem %S/Inputs/include -fopenmp -triple powerpc64le-unknown-unknown -fopenmp-targets=nvptx64-nvidia-cuda -emit-llvm-bc %s -o %t-ppc-host.bc
3-
// RUN: %clang_cc1 -internal-isystem %S/../../lib/Headers/openmp_wrappers -include __clang_openmp_device_functions.h -internal-isystem %S/Inputs/include -fopenmp -triple nvptx64-nvidia-cuda -aux-triple powerpc64le-unknown-unknown -fopenmp-targets=nvptx64-nvidia-cuda -emit-llvm %s -fopenmp-is-device -fopenmp-host-ir-file-path %t-ppc-host.bc -o - | FileCheck %s
3+
// RUN: %clang_cc1 -internal-isystem %S/../../lib/Headers/openmp_wrappers -include __clang_openmp_device_functions.h -internal-isystem %S/Inputs/include -fopenmp -triple nvptx64-nvidia-cuda -aux-triple powerpc64le-unknown-unknown -fopenmp-targets=nvptx64-nvidia-cuda -emit-llvm %s -fopenmp-is-device -fopenmp-host-ir-file-path %t-ppc-host.bc -o - | FileCheck %s
4+
// RUN: %clang_cc1 -internal-isystem %S/../../lib/Headers/openmp_wrappers -DCMATH -include __clang_openmp_device_functions.h -internal-isystem %S/Inputs/include -fopenmp -triple nvptx64-nvidia-cuda -aux-triple powerpc64le-unknown-unknown -fopenmp-targets=nvptx64-nvidia-cuda -emit-llvm %s -fopenmp-is-device -fopenmp-host-ir-file-path %t-ppc-host.bc -o - | FileCheck %s
45

6+
#ifdef CMATH
57
#include <cmath>
8+
#else
9+
#include <math.h>
10+
#endif
611

712
// 4 calls to sincos(f), all translated to __nv_sincos calls:
813

0 commit comments

Comments
 (0)