Index: clang/include/clang/Basic/TargetInfo.h =================================================================== --- clang/include/clang/Basic/TargetInfo.h +++ clang/include/clang/Basic/TargetInfo.h @@ -218,6 +218,8 @@ unsigned HasAArch64SVETypes : 1; + unsigned AllowAMDGPUUnsafeFPAtomics : 1; + unsigned ARMCDECoprocMask : 8; unsigned MaxOpenCLWorkGroupSize; @@ -857,6 +859,10 @@ /// available on this target. bool hasAArch64SVETypes() const { return HasAArch64SVETypes; } + /// Returns whether or not the AMDGPU unsafe floating point atomics are + /// allowed. + bool allowAMDGPUUnsafeFPAtomics() const { return AllowAMDGPUUnsafeFPAtomics; } + /// For ARM targets returns a mask defining which coprocessors are configured /// as Custom Datapath. uint32_t getARMCDECoprocMask() const { return ARMCDECoprocMask; } Index: clang/include/clang/Basic/TargetOptions.h =================================================================== --- clang/include/clang/Basic/TargetOptions.h +++ clang/include/clang/Basic/TargetOptions.h @@ -75,6 +75,9 @@ /// address space. bool NVPTXUseShortPointers = false; + /// \brief If enabled, allow AMDGPU unsafe floating point atomics. + bool AllowAMDGPUUnsafeFPAtomics = false; + // The code model to be used as specified by the user. Corresponds to // CodeModel::Model enum defined in include/llvm/Support/CodeGen.h, plus // "default" for the case when the user has not explicitly specified a Index: clang/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ clang/include/clang/Driver/Options.td @@ -2543,6 +2543,11 @@ HelpText<"Specify XNACK mode (AMDGPU only)">; def mno_xnack : Flag<["-"], "mno-xnack">, Group; +def munsafe_fp_atomics : Flag<["-"], "munsafe-fp-atomics">, Group, + HelpText<"Enable unsafe floating point atomic instructions (AMDGPU only)">, + Flags<[CC1Option]>; +def mno_unsafe_fp_atomics : Flag<["-"], "mno-unsafe-fp-atomics">, Group; + def faltivec : Flag<["-"], "faltivec">, Group, Flags<[NoXarchOption]>; def fno_altivec : Flag<["-"], "fno-altivec">, Group, Flags<[NoXarchOption]>; def maltivec : Flag<["-"], "maltivec">, Group; Index: clang/lib/Basic/TargetInfo.cpp =================================================================== --- clang/lib/Basic/TargetInfo.cpp +++ clang/lib/Basic/TargetInfo.cpp @@ -115,6 +115,7 @@ HasBuiltinMSVaList = false; IsRenderScriptTarget = false; HasAArch64SVETypes = false; + AllowAMDGPUUnsafeFPAtomics = false; ARMCDECoprocMask = 0; // Default to no types using fpret. Index: clang/lib/Basic/Targets/AMDGPU.cpp =================================================================== --- clang/lib/Basic/Targets/AMDGPU.cpp +++ clang/lib/Basic/Targets/AMDGPU.cpp @@ -323,6 +323,7 @@ HasLegalHalfType = true; HasFloat16 = true; WavefrontSize = GPUFeatures & llvm::AMDGPU::FEATURE_WAVE32 ? 32 : 64; + AllowAMDGPUUnsafeFPAtomics = Opts.AllowAMDGPUUnsafeFPAtomics; // Set pointer width and alignment for target address space 0. PointerWidth = PointerAlign = DataLayout->getPointerSizeInBits(); Index: clang/lib/CodeGen/TargetInfo.cpp =================================================================== --- clang/lib/CodeGen/TargetInfo.cpp +++ clang/lib/CodeGen/TargetInfo.cpp @@ -9080,6 +9080,9 @@ if (NumVGPR != 0) F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR)); } + + if (M.getContext().getTargetInfo().allowAMDGPUUnsafeFPAtomics()) + F->addFnAttr("amdgpu-unsafe-fp-atomics", "true"); } unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const { Index: clang/lib/Driver/ToolChains/Clang.cpp =================================================================== --- clang/lib/Driver/ToolChains/Clang.cpp +++ clang/lib/Driver/ToolChains/Clang.cpp @@ -6217,6 +6217,11 @@ } HandleAmdgcnLegacyOptions(D, Args, CmdArgs); + if (Triple.isAMDGPU()) { + if (Args.hasFlag(options::OPT_munsafe_fp_atomics, + options::OPT_mno_unsafe_fp_atomics)) + CmdArgs.push_back("-munsafe-fp-atomics"); + } // For all the host OpenMP offloading compile jobs we need to pass the targets // information using -fopenmp-targets= option. Index: clang/lib/Frontend/CompilerInvocation.cpp =================================================================== --- clang/lib/Frontend/CompilerInvocation.cpp +++ clang/lib/Frontend/CompilerInvocation.cpp @@ -3719,6 +3719,9 @@ Opts.ForceEnableInt128 = Args.hasArg(OPT_fforce_enable_int128); Opts.NVPTXUseShortPointers = Args.hasFlag( options::OPT_fcuda_short_ptr, options::OPT_fno_cuda_short_ptr, false); + Opts.AllowAMDGPUUnsafeFPAtomics = + Args.hasFlag(options::OPT_munsafe_fp_atomics, + options::OPT_mno_unsafe_fp_atomics, false); if (Arg *A = Args.getLastArg(options::OPT_target_sdk_version_EQ)) { llvm::VersionTuple Version; if (Version.tryParse(A->getValue())) Index: clang/test/CodeGenCUDA/amdgpu-func-attrs.cu =================================================================== --- /dev/null +++ clang/test/CodeGenCUDA/amdgpu-func-attrs.cu @@ -0,0 +1,22 @@ +// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa \ +// RUN: -fcuda-is-device -emit-llvm -o - -x hip %s \ +// RUN: | FileCheck -check-prefixes=NO-UNSAFE-FP-ATOMICS %s +// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa \ +// RUN: -fcuda-is-device -emit-llvm -o - -x hip %s \ +// RUN: -munsafe-fp-atomics \ +// RUN: | FileCheck -check-prefixes=UNSAFE-FP-ATOMICS %s +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm \ +// RUN: -o - -x hip %s -munsafe-fp-atomics \ +// RUN: | FileCheck -check-prefix=NO-UNSAFE-FP-ATOMICS %s + +#include "Inputs/cuda.h" + +__device__ void test() { +// UNSAFE-FP-ATOMICS: define void @_Z4testv() [[ATTR:#[0-9]+]] +} + + +// Make sure this is silently accepted on other targets. +// NO-UNSAFE-FP-ATOMICS-NOT: "amdgpu-unsafe-fp-atomics" + +// UNSAFE-FP-ATOMICS-DAG: attributes [[ATTR]] = {{.*}}"amdgpu-unsafe-fp-atomics"="true" Index: clang/test/Driver/hip-options.hip =================================================================== --- clang/test/Driver/hip-options.hip +++ clang/test/Driver/hip-options.hip @@ -31,3 +31,7 @@ // HOST-NOT: clang{{.*}} "-fcuda-is-device" {{.*}} "-debug-info-kind={{.*}}" // HOST-NOT: clang{{.*}} "-fcuda-is-device" {{.*}} "-debug-info-kind={{.*}}" // HOST: clang{{.*}} "-debug-info-kind={{.*}}" + +// RUN: %clang -### -nogpuinc -nogpulib -munsafe-fp-atomics \ +// RUN: --cuda-gpu-arch=gfx906 %s 2>&1 | FileCheck -check-prefix=UNSAFE-FP-ATOMICS %s +// UNSAFE-FP-ATOMICS: clang{{.*}} "-triple" "amdgcn-amd-amdhsa" {{.*}} "-munsafe-fp-atomics"