Index: clang/include/clang/Basic/LangOptions.def =================================================================== --- clang/include/clang/Basic/LangOptions.def +++ clang/include/clang/Basic/LangOptions.def @@ -265,7 +265,7 @@ LANGOPT(CUDAIsDevice , 1, 0, "compiling for CUDA device") LANGOPT(CUDAAllowVariadicFunctions, 1, 0, "allowing variadic functions in CUDA device code") LANGOPT(CUDAHostDeviceConstexpr, 1, 1, "treating unattributed constexpr functions as __host__ __device__") -LANGOPT(CUDADeviceApproxTranscendentals, 1, 0, "using approximate transcendental functions") +LANGOPT(GPUDeviceApproxTranscendentals, 1, 0, "using approximate transcendental functions") LANGOPT(GPURelocatableDeviceCode, 1, 0, "generate relocatable device code") LANGOPT(GPUAllowDeviceInit, 1, 0, "allowing device side global init functions for HIP") LANGOPT(GPUMaxThreadsPerBlock, 32, 1024, "default max threads per block for kernel launch bounds for HIP") Index: clang/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ clang/include/clang/Driver/Options.td @@ -6766,11 +6766,12 @@ MarshallingInfoEnum, "SYCL_None">, ShouldParseIf; -defm cuda_approx_transcendentals : BoolFOption<"cuda-approx-transcendentals", - LangOpts<"CUDADeviceApproxTranscendentals">, DefaultFalse, +defm gpu_approx_transcendentals : BoolFOption<"gpu-approx-transcendentals", + LangOpts<"GPUDeviceApproxTranscendentals">, DefaultFalse, PosFlag, NegFlag, - BothFlags<[], " approximate transcendental functions">>, - ShouldParseIf; + BothFlags<[], " approximate transcendental functions">>; +def : Flag<["-"], "fcuda-approx-transcendentals">, Alias; +def : Flag<["-"], "fno-cuda-approx-transcendentals">, Alias; //===----------------------------------------------------------------------===// // Frontend Options - cc1 + fc1 Index: clang/lib/Driver/ToolChains/Clang.cpp =================================================================== --- clang/lib/Driver/ToolChains/Clang.cpp +++ clang/lib/Driver/ToolChains/Clang.cpp @@ -7215,6 +7215,18 @@ auto CUID = cast(SourceAction)->getId(); if (!CUID.empty()) CmdArgs.push_back(Args.MakeArgString(Twine("-cuid=") + Twine(CUID))); + + // -ffast-math turns on -fgpu-approx-transcendentals implicitly, but will + // be overriden by -fno-gpu-approx-transcendentals. + bool UseApproxTranscendentals = Args.hasFlag( + options::OPT_ffast_math, options::OPT_fno_fast_math, false); + if (Args.hasFlag(options::OPT_fgpu_approx_transcendentals, + options::OPT_fno_gpu_approx_transcendentals, + UseApproxTranscendentals)) + CmdArgs.push_back("-fgpu-approx-transcendentals"); + } else { + Args.ClaimAllArgs(options::OPT_fgpu_approx_transcendentals); + Args.ClaimAllArgs(options::OPT_fno_gpu_approx_transcendentals); } if (IsHIP) { Index: clang/lib/Driver/ToolChains/Cuda.cpp =================================================================== --- clang/lib/Driver/ToolChains/Cuda.cpp +++ clang/lib/Driver/ToolChains/Cuda.cpp @@ -803,10 +803,6 @@ CC1Args.append( {"-fcuda-is-device", "-mllvm", "-enable-memcpyopt-without-libcalls"}); - if (DriverArgs.hasFlag(options::OPT_fcuda_approx_transcendentals, - options::OPT_fno_cuda_approx_transcendentals, false)) - CC1Args.push_back("-fcuda-approx-transcendentals"); - // Unsized function arguments used for variadics were introduced in CUDA-9.0 // We still do not support generating code that actually uses variadic // arguments yet, but we do need to allow parsing them as recent CUDA Index: clang/lib/Driver/ToolChains/HIPAMD.cpp =================================================================== --- clang/lib/Driver/ToolChains/HIPAMD.cpp +++ clang/lib/Driver/ToolChains/HIPAMD.cpp @@ -243,10 +243,6 @@ CC1Args.push_back("-fcuda-is-device"); - if (DriverArgs.hasFlag(options::OPT_fcuda_approx_transcendentals, - options::OPT_fno_cuda_approx_transcendentals, false)) - CC1Args.push_back("-fcuda-approx-transcendentals"); - if (!DriverArgs.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc, false)) CC1Args.append({"-mllvm", "-amdgpu-internalize-symbols"}); Index: clang/lib/Driver/ToolChains/HIPSPV.cpp =================================================================== --- clang/lib/Driver/ToolChains/HIPSPV.cpp +++ clang/lib/Driver/ToolChains/HIPSPV.cpp @@ -143,10 +143,6 @@ // TODO: Allow autovectorization when SPIR-V backend arrives. "-mllvm", "-vectorize-loops=false", "-mllvm", "-vectorize-slp=false"}); - if (DriverArgs.hasFlag(options::OPT_fcuda_approx_transcendentals, - options::OPT_fno_cuda_approx_transcendentals, false)) - CC1Args.push_back("-fcuda-approx-transcendentals"); - // Default to "hidden" visibility, as object level linking will not be // supported for the foreseeable future. if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ, Index: clang/lib/Frontend/InitPreprocessor.cpp =================================================================== --- clang/lib/Frontend/InitPreprocessor.cpp +++ clang/lib/Frontend/InitPreprocessor.cpp @@ -1286,10 +1286,13 @@ Builder.defineMacro("__CUDA_ARCH__"); } - // We need to communicate this to our CUDA header wrapper, which in turn - // informs the proper CUDA headers of this choice. - if (LangOpts.CUDADeviceApproxTranscendentals || LangOpts.FastMath) { - Builder.defineMacro("__CLANG_CUDA_APPROX_TRANSCENDENTALS__"); + // We need to communicate this to our CUDA/HIP header wrapper, which in turn + // informs the proper CUDA/HIP headers of this choice. + if (LangOpts.GPUDeviceApproxTranscendentals) { + // Keep this for backward compatibility. + if (!LangOpts.HIP) + Builder.defineMacro("__CLANG_CUDA_APPROX_TRANSCENDENTALS__"); + Builder.defineMacro("__CLANG_GPU_APPROX_TRANSCENDENTALS__"); } // Define a macro indicating that the source file is being compiled with a Index: clang/lib/Headers/__clang_cuda_math.h =================================================================== --- clang/lib/Headers/__clang_cuda_math.h +++ clang/lib/Headers/__clang_cuda_math.h @@ -45,9 +45,9 @@ // libdevice provides fast low precision and slow full-recision implementations // for some functions. Which one gets selected depends on // __CLANG_CUDA_APPROX_TRANSCENDENTALS__ which gets defined by clang if -// -ffast-math or -fcuda-approx-transcendentals are in effect. +// -ffast-math or -fgpu-approx-transcendentals are in effect. #pragma push_macro("__FAST_OR_SLOW") -#if defined(__CLANG_CUDA_APPROX_TRANSCENDENTALS__) +#if defined(__CLANG_GPU_APPROX_TRANSCENDENTALS__) #define __FAST_OR_SLOW(fast, slow) fast #else #define __FAST_OR_SLOW(fast, slow) slow Index: clang/lib/Headers/__clang_cuda_runtime_wrapper.h =================================================================== --- clang/lib/Headers/__clang_cuda_runtime_wrapper.h +++ clang/lib/Headers/__clang_cuda_runtime_wrapper.h @@ -196,12 +196,12 @@ // math_function.hpp uses the __USE_FAST_MATH__ macro to determine whether we // get the slow-but-accurate or fast-but-inaccurate versions of functions like -// sin and exp. This is controlled in clang by -fcuda-approx-transcendentals. +// sin and exp. This is controlled in clang by -fgpu-approx-transcendentals. // // device_functions.hpp uses __USE_FAST_MATH__ for a different purpose (fast vs. // slow divides), so we need to scope our define carefully here. #pragma push_macro("__USE_FAST_MATH__") -#if defined(__CLANG_CUDA_APPROX_TRANSCENDENTALS__) +#if defined(__CLANG_GPU_APPROX_TRANSCENDENTALS__) #define __USE_FAST_MATH__ 1 #endif Index: clang/lib/Headers/__clang_hip_math.h =================================================================== --- clang/lib/Headers/__clang_hip_math.h +++ clang/lib/Headers/__clang_hip_math.h @@ -34,10 +34,10 @@ // Device library provides fast low precision and slow full-recision // implementations for some functions. Which one gets selected depends on -// __CLANG_CUDA_APPROX_TRANSCENDENTALS__ which gets defined by clang if -// -ffast-math or -fcuda-approx-transcendentals are in effect. +// __CLANG_GPU_APPROX_TRANSCENDENTALS__ which gets defined by clang if +// -ffast-math or -fgpu-approx-transcendentals are in effect. #pragma push_macro("__FAST_OR_SLOW") -#if defined(__CLANG_CUDA_APPROX_TRANSCENDENTALS__) +#if defined(__CLANG_GPU_APPROX_TRANSCENDENTALS__) #define __FAST_OR_SLOW(fast, slow) fast #else #define __FAST_OR_SLOW(fast, slow) slow Index: clang/test/Driver/hip-macros.hip =================================================================== --- clang/test/Driver/hip-macros.hip +++ clang/test/Driver/hip-macros.hip @@ -63,3 +63,11 @@ // WARN: warning: feature flag '{{[+|-]}}image-insts' is ignored since the feature is read only [-Winvalid-command-line-argument] // IMAGE-NOT: #define __HIP_NO_IMAGE_SUPPORT // NOIMAGE: #define __HIP_NO_IMAGE_SUPPORT 1 + +// RUN: %clang -E -dM --offload-arch=gfx906 -nogpuinc -nogpulib \ +// RUN: %s 2>&1 | FileCheck --check-prefix=NOAPPROX %s +// RUN: %clang -E -dM --offload-arch=gfx906 -nogpuinc -nogpulib -fgpu-approx-transcendentals \ +// RUN: %s 2>&1 | FileCheck --check-prefix=APPROX %s +// NOAPPROX-NOT: #define __CLANG_GPU_APPROX_TRANSCENDENTALS__ +// APPROX: #define __CLANG_GPU_APPROX_TRANSCENDENTALS__ 1 +// APPROX: #define __CLANG_GPU_APPROX_TRANSCENDENTALS__ 1 Index: clang/test/Driver/hip-options.hip =================================================================== --- clang/test/Driver/hip-options.hip +++ clang/test/Driver/hip-options.hip @@ -169,3 +169,41 @@ // RUN: %clang -### -nogpuinc -nogpulib -fhip-fp32-correctly-rounded-divide-sqrt \ // RUN: --cuda-gpu-arch=gfx906 %s 2>&1 | FileCheck -check-prefixes=CRDS %s // CRDS-NOT: "-f{{(no-)?}}hip-fp32-correctly-rounded-divide-sqrt" + +// Check -fgpu-approx-transcendentals is passed to clang -cc1 but +// (default) -fno-gpu-approx-transcendentals is not. +// -ffast-math implies -fgpu-approx-transcendentals, which can be overridden +// by -fno-gpu-approx-transcendentals. + +// RUN: %clang -### --target=x86_64-unknown-linux-gnu -nogpuinc -nogpulib -fgpu-approx-transcendentals \ +// RUN: --cuda-gpu-arch=gfx906 %s 2>&1 | FileCheck -check-prefix=APPROX %s + +// RUN: %clang -### --target=x86_64-unknown-linux-gnu -nogpuinc -nogpulib -ffast-math \ +// RUN: --cuda-gpu-arch=gfx906 %s 2>&1 | FileCheck -check-prefix=APPROX %s + +// APPROX: clang{{.*}} "-triple" "amdgcn-amd-amdhsa" {{.*}} "-fgpu-approx-transcendentals" +// APPROX: clang{{.*}} "-triple" "x86_64-unknown-linux-gnu" {{.*}} "-fgpu-approx-transcendentals" + +// RUN: %clang -### -nogpuinc -nogpulib -fno-gpu-approx-transcendentals \ +// RUN: --cuda-gpu-arch=gfx906 %s 2>&1 | FileCheck -check-prefix=NOAPPROX %s + +// RUN: %clang -### -nogpuinc -nogpulib \ +// RUN: --cuda-gpu-arch=gfx906 %s 2>&1 | FileCheck -check-prefix=NOAPPROX %s + +// RUN: %clang -### -nogpuinc -nogpulib -ffast-math -fno-fast-math \ +// RUN: --cuda-gpu-arch=gfx906 %s 2>&1 | FileCheck -check-prefix=NOAPPROX %s + +// RUN: %clang -### -nogpuinc -nogpulib -ffast-math -fno-gpu-approx-transcendentals \ +// RUN: --cuda-gpu-arch=gfx906 %s 2>&1 | FileCheck -check-prefix=NOAPPROX %s + +// NOAPPROX-NOT: "-f{{(no-)?}}gpu-approx-transcendentals" + +// Check no warnings for -fgpu-approx-transcendentals. + +// RUN: %clang -### --target=x86_64-unknown-linux-gnu -nogpuinc -nogpulib -fgpu-approx-transcendentals \ +// RUN: --cuda-gpu-arch=gfx906 %s 2>&1 | FileCheck -check-prefix=APPROXNEG %s + +// RUN: %clang -### --target=x86_64-unknown-linux-gnu -nostdinc -nostdlib -fgpu-approx-transcendentals \ +// RUN: -x c++ %s 2>&1 | FileCheck -check-prefix=APPROXNEG %s + +// APPROXNEG-NOT: warning Index: clang/test/Headers/__clang_hip_math.hip =================================================================== --- clang/test/Headers/__clang_hip_math.hip +++ clang/test/Headers/__clang_hip_math.hip @@ -22,7 +22,7 @@ // RUN: -internal-isystem %S/../../lib/Headers/cuda_wrappers \ // RUN: -internal-isystem %S/Inputs/include \ // RUN: -triple amdgcn-amd-amdhsa -aux-triple x86_64-unknown-unknown \ -// RUN: -target-cpu gfx906 -emit-llvm %s -fcuda-is-device -O1 -fcuda-approx-transcendentals -o - \ +// RUN: -target-cpu gfx906 -emit-llvm %s -fcuda-is-device -O1 -fgpu-approx-transcendentals -o - \ // RUN: -D__HIPCC_RTC__ | FileCheck -check-prefixes=CHECK,APPROX %s #define BOOL_TYPE int Index: clang/test/Headers/nvptx_device_math_sin.c =================================================================== --- clang/test/Headers/nvptx_device_math_sin.c +++ clang/test/Headers/nvptx_device_math_sin.c @@ -2,7 +2,7 @@ // RUN: %clang_cc1 -x c -internal-isystem %S/Inputs/include -fopenmp -triple powerpc64le-unknown-unknown -fopenmp-targets=nvptx64-nvidia-cuda -emit-llvm-bc %s -o %t-ppc-host.bc // RUN: %clang_cc1 -x c -include __clang_openmp_device_functions.h -internal-isystem %S/../../lib/Headers/openmp_wrappers -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 --check-prefix=SLOW // RUN: %clang_cc1 -x c -internal-isystem %S/Inputs/include -fopenmp -triple powerpc64le-unknown-unknown -fopenmp-targets=nvptx64-nvidia-cuda -emit-llvm-bc %s -o %t-ppc-host.bc -ffast-math -ffp-contract=fast -// RUN: %clang_cc1 -x c -include __clang_openmp_device_functions.h -internal-isystem %S/../../lib/Headers/openmp_wrappers -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 - -ffast-math -ffp-contract=fast | FileCheck %s --check-prefix=FAST +// RUN: %clang_cc1 -x c -include __clang_openmp_device_functions.h -internal-isystem %S/../../lib/Headers/openmp_wrappers -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 - -fgpu-approx-transcendentals -ffast-math -ffp-contract=fast | FileCheck %s --check-prefix=FAST // expected-no-diagnostics #include Index: clang/test/Headers/nvptx_device_math_sin.cpp =================================================================== --- clang/test/Headers/nvptx_device_math_sin.cpp +++ clang/test/Headers/nvptx_device_math_sin.cpp @@ -2,7 +2,7 @@ // RUN: %clang_cc1 -x c++ -internal-isystem %S/Inputs/include -fopenmp -triple powerpc64le-unknown-unknown -fopenmp-targets=nvptx64-nvidia-cuda -emit-llvm-bc %s -o %t-ppc-host.bc // RUN: %clang_cc1 -x c++ -include __clang_openmp_device_functions.h -internal-isystem %S/../../lib/Headers/openmp_wrappers -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 --check-prefix=SLOW // RUN: %clang_cc1 -x c++ -internal-isystem %S/Inputs/include -fopenmp -triple powerpc64le-unknown-unknown -fopenmp-targets=nvptx64-nvidia-cuda -emit-llvm-bc %s -o %t-ppc-host.bc -ffast-math -ffp-contract=fast -// RUN: %clang_cc1 -x c++ -include __clang_openmp_device_functions.h -internal-isystem %S/../../lib/Headers/openmp_wrappers -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 - -ffast-math -ffp-contract=fast | FileCheck %s --check-prefix=FAST +// RUN: %clang_cc1 -x c++ -include __clang_openmp_device_functions.h -internal-isystem %S/../../lib/Headers/openmp_wrappers -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 - -fgpu-approx-transcendentals -ffast-math -ffp-contract=fast | FileCheck %s --check-prefix=FAST // expected-no-diagnostics #include