yaxunl created this revision. yaxunl added reviewers: tra, MaskRay, b-sumner. Herald added subscribers: mattd, carlosgalvezp. Herald added a project: All. yaxunl requested review of this revision.
Rename `-fcuda-approx-transcendentals` as `-fgpu-approx-transcendentals` and pass it to both device and host clang -cc1. Fix its interaction with `-ffast-math` to allow `-fno-gpu-approx-transcendentals` to override the implicit `-fcuda-approx-transcendentals` due to `-ffast-math`. Rename the predefined macro to be `__CLANG_[CUDA|HIP]_APPROX_TRANSCENDENTALS__` depending on the language. Emit the macro for both device and host compilation. https://reviews.llvm.org/D154797 Files: clang/include/clang/Basic/LangOptions.def clang/include/clang/Driver/Options.td clang/lib/Driver/ToolChains/Clang.cpp clang/lib/Driver/ToolChains/Cuda.cpp clang/lib/Driver/ToolChains/HIPAMD.cpp clang/lib/Driver/ToolChains/HIPSPV.cpp clang/lib/Frontend/InitPreprocessor.cpp clang/lib/Headers/__clang_cuda_math.h clang/lib/Headers/__clang_cuda_runtime_wrapper.h clang/lib/Headers/__clang_hip_math.h clang/test/Driver/hip-macros.hip clang/test/Driver/hip-options.hip clang/test/Headers/__clang_hip_math.hip
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/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-prefixes=APPROX %s + +// RUN: %clang -### --target=x86_64-unknown-linux-gnu -nogpuinc -nogpulib -ffast-math \ +// RUN: --cuda-gpu-arch=gfx906 %s 2>&1 | FileCheck -check-prefixes=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-prefixes=NOAPPROX %s + +// RUN: %clang -### -nogpuinc -nogpulib \ +// RUN: --cuda-gpu-arch=gfx906 %s 2>&1 | FileCheck -check-prefixes=NOAPPROX %s + +// RUN: %clang -### -nogpuinc -nogpulib -ffast-math -fno-fast-math \ +// RUN: --cuda-gpu-arch=gfx906 %s 2>&1 | FileCheck -check-prefixes=NOAPPROX %s + +// RUN: %clang -### -nogpuinc -nogpulib -ffast-math -fno-gpu-approx-transcendentals \ +// RUN: --cuda-gpu-arch=gfx906 %s 2>&1 | FileCheck -check-prefixes=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-prefixes=APPROXNEG %s + +// RUN: %clang -### --target=x86_64-unknown-linux-gnu -nostdinc -nostdlib -fgpu-approx-transcendentals \ +// RUN: -x c++ %s 2>&1 | FileCheck -check-prefixes=APPROXNEG %s + +// APPROXNEG-NOT: warning 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_HIP_APPROX_TRANSCENDENTALS__ +// APPROX: #define __CLANG_HIP_APPROX_TRANSCENDENTALS__ 1 +// APPROX: #define __CLANG_HIP_APPROX_TRANSCENDENTALS__ 1 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_HIP_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_HIP_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,7 +196,7 @@ // 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. Index: clang/lib/Headers/__clang_cuda_math.h =================================================================== --- clang/lib/Headers/__clang_cuda_math.h +++ clang/lib/Headers/__clang_cuda_math.h @@ -45,7 +45,7 @@ // 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__) #define __FAST_OR_SLOW(fast, slow) fast Index: clang/lib/Frontend/InitPreprocessor.cpp =================================================================== --- clang/lib/Frontend/InitPreprocessor.cpp +++ clang/lib/Frontend/InitPreprocessor.cpp @@ -1286,10 +1286,11 @@ 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) { + Builder.defineMacro(Twine("__CLANG_") + (LangOpts.HIP ? "HIP" : "CUDA") + + "_APPROX_TRANSCENDENTALS__"); } // Define a macro indicating that the source file is being compiled with a 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/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/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/Clang.cpp =================================================================== --- clang/lib/Driver/ToolChains/Clang.cpp +++ clang/lib/Driver/ToolChains/Clang.cpp @@ -7215,6 +7215,20 @@ auto CUID = cast<InputAction>(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 = false; + if (Args.hasFlag(options::OPT_ffast_math, options::OPT_fno_fast_math, + false)) + UseApproxTranscendentals = true; + 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/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ clang/include/clang/Driver/Options.td @@ -6766,11 +6766,12 @@ MarshallingInfoEnum<LangOpts<"SYCLVersion">, "SYCL_None">, ShouldParseIf<!strconcat(fsycl_is_device.KeyPath, "||", fsycl_is_host.KeyPath)>; -defm cuda_approx_transcendentals : BoolFOption<"cuda-approx-transcendentals", - LangOpts<"CUDADeviceApproxTranscendentals">, DefaultFalse, +defm gpu_approx_transcendentals : BoolFOption<"gpu-approx-transcendentals", + LangOpts<"GPUDeviceApproxTranscendentals">, DefaultFalse, PosFlag<SetTrue, [CC1Option], "Use">, NegFlag<SetFalse, [], "Don't use">, - BothFlags<[], " approximate transcendental functions">>, - ShouldParseIf<fcuda_is_device.KeyPath>; + BothFlags<[], " approximate transcendental functions">>; +def : Flag<["-"], "fcuda-approx-transcendentals">, Alias<fgpu_approx_transcendentals>; +def : Flag<["-"], "fno-cuda-approx-transcendentals">, Alias<fno_gpu_approx_transcendentals>; //===----------------------------------------------------------------------===// // Frontend Options - cc1 + fc1 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")
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits