Skip to content

Commit

Permalink
[AMDGPU] Add option -munsafe-fp-atomics
Browse files Browse the repository at this point in the history
Add an option -munsafe-fp-atomics for AMDGPU target.

When enabled, clang adds function attribute "amdgpu-unsafe-fp-atomics"
to any functions for amdgpu target. This allows amdgpu backend to use
unsafe fp atomic instructions in these functions.

Differential Revision: https://reviews.llvm.org/D91546
  • Loading branch information
yxsamliu committed Nov 17, 2020
1 parent 69cd776 commit 3f4b589
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 0 deletions.
6 changes: 6 additions & 0 deletions clang/include/clang/Basic/TargetInfo.h
Expand Up @@ -218,6 +218,8 @@ class TargetInfo : public virtual TransferrableTargetInfo,

unsigned HasAArch64SVETypes : 1;

unsigned AllowAMDGPUUnsafeFPAtomics : 1;

unsigned ARMCDECoprocMask : 8;

unsigned MaxOpenCLWorkGroupSize;
Expand Down Expand Up @@ -857,6 +859,10 @@ class TargetInfo : public virtual TransferrableTargetInfo,
/// 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; }
Expand Down
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/TargetOptions.h
Expand Up @@ -75,6 +75,9 @@ class TargetOptions {
/// 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
Expand Down
5 changes: 5 additions & 0 deletions clang/include/clang/Driver/Options.td
Expand Up @@ -2546,6 +2546,11 @@ def mxnack : Flag<["-"], "mxnack">, Group<m_amdgpu_Features_Group>,
HelpText<"Specify XNACK mode (AMDGPU only)">;
def mno_xnack : Flag<["-"], "mno-xnack">, Group<m_amdgpu_Features_Group>;

def munsafe_fp_atomics : Flag<["-"], "munsafe-fp-atomics">, Group<m_Group>,
HelpText<"Enable unsafe floating point atomic instructions (AMDGPU only)">,
Flags<[CC1Option]>;
def mno_unsafe_fp_atomics : Flag<["-"], "mno-unsafe-fp-atomics">, Group<m_Group>;

def faltivec : Flag<["-"], "faltivec">, Group<f_Group>, Flags<[NoXarchOption]>;
def fno_altivec : Flag<["-"], "fno-altivec">, Group<f_Group>, Flags<[NoXarchOption]>;
def maltivec : Flag<["-"], "maltivec">, Group<m_ppc_Features_Group>;
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Basic/TargetInfo.cpp
Expand Up @@ -115,6 +115,7 @@ TargetInfo::TargetInfo(const llvm::Triple &T) : TargetOpts(), Triple(T) {
HasBuiltinMSVaList = false;
IsRenderScriptTarget = false;
HasAArch64SVETypes = false;
AllowAMDGPUUnsafeFPAtomics = false;
ARMCDECoprocMask = 0;

// Default to no types using fpret.
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Basic/Targets/AMDGPU.cpp
Expand Up @@ -323,6 +323,7 @@ AMDGPUTargetInfo::AMDGPUTargetInfo(const llvm::Triple &Triple,
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();
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/CodeGen/TargetInfo.cpp
Expand Up @@ -9080,6 +9080,9 @@ void AMDGPUTargetCodeGenInfo::setTargetAttributes(
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 {
Expand Down
5 changes: 5 additions & 0 deletions clang/lib/Driver/ToolChains/Clang.cpp
Expand Up @@ -6217,6 +6217,11 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
}

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.
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Frontend/CompilerInvocation.cpp
Expand Up @@ -3747,6 +3747,9 @@ static void ParseTargetArgs(TargetOptions &Opts, ArgList &Args,
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()))
Expand Down
22 changes: 22 additions & 0 deletions 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"
4 changes: 4 additions & 0 deletions clang/test/Driver/hip-options.hip
Expand Up @@ -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"

0 comments on commit 3f4b589

Please sign in to comment.