[clang] Add clang atomic control options and attribute (PR #114841)

2024-11-07 Thread Yaxun Liu via cfe-commits


@@ -0,0 +1,30 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -fsyntax-only -verify -fcuda-is-device %s
+// RUN: %clang_cc1 -fsyntax-only -verify -fcuda-is-device %s \
+// RUN:   
-fatomic=no_fine_grained_memory:off,no_remote_memory:on,ignore_denormal_mode:on
+
+#include "Inputs/cuda.h"
+
+[[clang::atomic(!no_remote_memory)]] // expected-error {{'atomic' attribute 
cannot be applied to a declaration}}
+__device__ __host__ void test_location(float *a) {
+  __scoped_atomic_fetch_add(a, 1, __ATOMIC_RELAXED, __MEMORY_SCOPE_SYSTEM);
+  [[clang::atomic(!no_remote_memory)]] int x; // expected-error {{'atomic' 
attribute cannot be applied to a declaration}}
+}
+
+__device__ __host__ void test_invalid_option(float *a) {
+  [[clang::atomic(fast)]] { // expected-error {{invalid argument 'fast' to 
atomic attribute; valid options are: 'no_remote_memory', 
'no_fine_grained_memory', 'ignore_denormal_mode' (optionally prefixed with 
'!')}}
+__scoped_atomic_fetch_add(a, 1, __ATOMIC_RELAXED, __MEMORY_SCOPE_SYSTEM);
+  }
+}
+
+__device__ __host__ void test_invalid_value(float *a) {
+  [[clang::atomic(no_remote_memory(default))]] { // expected-error {{expected 
')'}} expected-note {{to match this '('}}
+__scoped_atomic_fetch_add(a, 1, __ATOMIC_RELAXED, __MEMORY_SCOPE_SYSTEM);
+  }
+}
+
+__device__ __host__ void test_invalid_format(float *a) {
+  [[clang::atomic(no_remote_memory=on)]] { // expected-error {{expected ')'}} 
expected-note {{to match this '('}}
+__scoped_atomic_fetch_add(a, 1, __ATOMIC_RELAXED, __MEMORY_SCOPE_SYSTEM);
+  }
+}

yxsamliu wrote:

fixed

https://github.com/llvm/llvm-project/pull/114841
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add clang atomic control options and attribute (PR #114841)

2024-11-07 Thread Yaxun Liu via cfe-commits


@@ -0,0 +1,30 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -fsyntax-only -verify -fcuda-is-device %s
+// RUN: %clang_cc1 -fsyntax-only -verify -fcuda-is-device %s \
+// RUN:   
-fatomic=no_fine_grained_memory:off,no_remote_memory:on,ignore_denormal_mode:on
+
+#include "Inputs/cuda.h"
+
+[[clang::atomic(!no_remote_memory)]] // expected-error {{'atomic' attribute 
cannot be applied to a declaration}}
+__device__ __host__ void test_location(float *a) {
+  __scoped_atomic_fetch_add(a, 1, __ATOMIC_RELAXED, __MEMORY_SCOPE_SYSTEM);
+  [[clang::atomic(!no_remote_memory)]] int x; // expected-error {{'atomic' 
attribute cannot be applied to a declaration}}
+}
+
+__device__ __host__ void test_invalid_option(float *a) {
+  [[clang::atomic(fast)]] { // expected-error {{invalid argument 'fast' to 
atomic attribute; valid options are: 'no_remote_memory', 
'no_fine_grained_memory', 'ignore_denormal_mode' (optionally prefixed with 
'!')}}
+__scoped_atomic_fetch_add(a, 1, __ATOMIC_RELAXED, __MEMORY_SCOPE_SYSTEM);
+  }
+}
+
+__device__ __host__ void test_invalid_value(float *a) {
+  [[clang::atomic(no_remote_memory(default))]] { // expected-error {{expected 
')'}} expected-note {{to match this '('}}
+__scoped_atomic_fetch_add(a, 1, __ATOMIC_RELAXED, __MEMORY_SCOPE_SYSTEM);
+  }
+}
+
+__device__ __host__ void test_invalid_format(float *a) {
+  [[clang::atomic(no_remote_memory=on)]] { // expected-error {{expected ')'}} 
expected-note {{to match this '('}}
+__scoped_atomic_fetch_add(a, 1, __ATOMIC_RELAXED, __MEMORY_SCOPE_SYSTEM);
+  }
+}

yxsamliu wrote:

will fix

https://github.com/llvm/llvm-project/pull/114841
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add clang atomic control options and attribute (PR #114841)

2024-11-07 Thread Yaxun Liu via cfe-commits


@@ -569,19 +569,21 @@ void AMDGPUTargetCodeGenInfo::setTargetAtomicMetadata(
 AtomicInst.setMetadata(llvm::LLVMContext::MD_noalias_addrspace, ASRange);
   }
 
-  if (!RMW || !CGF.getTarget().allowAMDGPUUnsafeFPAtomics())
+  if (!RMW)
 return;
 
-  // TODO: Introduce new, more controlled options that also work for integers,
-  // and deprecate allowAMDGPUUnsafeFPAtomics.
-  llvm::AtomicRMWInst::BinOp RMWOp = RMW->getOperation();
-  if (llvm::AtomicRMWInst::isFPOperation(RMWOp)) {
-llvm::MDNode *Empty = llvm::MDNode::get(CGF.getLLVMContext(), {});
+  AtomicOptions AO = CGF.CGM.getAtomicOpts();
+  llvm::MDNode *Empty = llvm::MDNode::get(CGF.getLLVMContext(), {});
+  if (AO.getNoFineGrainedMemory())
 RMW->setMetadata("amdgpu.no.fine.grained.memory", Empty);
-
-if (RMWOp == llvm::AtomicRMWInst::FAdd && RMW->getType()->isFloatTy())
-  RMW->setMetadata("amdgpu.ignore.denormal.mode", Empty);
-  }
+  if (AO.getNoRemoteMemory())
+RMW->setMetadata("amdgpu.no.remote.memory", Empty);
+
+  if ((AO.getIgnoreDenormalMode() ||
+   CGF.getTarget().allowAMDGPUUnsafeFPAtomics()) &&

yxsamliu wrote:

I will remove the use of CGF.getTarget().allowAMDGPUUnsafeFPAtomics() here 
since it should override the default value for IgnoreDenormalMode attribute, 
which I have done in Basic/Targets/AMDGPU.cpp.

amdgpu.no.fine.grained.memory is on by default, therefore does not need to be 
implied by allowAMDGPUUnsafeFPAtomics

https://github.com/llvm/llvm-project/pull/114841
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add clang atomic control options and attribute (PR #114841)

2024-11-07 Thread Yaxun Liu via cfe-commits


@@ -0,0 +1,19 @@
+//===--- AtomicOptions.def - Atomic Options database -*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+// This file defines the Atomic language options. Users of this file
+// must define the OPTION macro to make use of this information.
+#ifndef OPTION
+#  error Define the OPTION macro to handle atomic language options
+#endif
+
+// OPTION(name, type, width, previousName)
+OPTION(NoRemoteMemory, bool, 1, First)
+OPTION(NoFineGrainedMemory, bool, 1, NoRemoteMemory)
+OPTION(IgnoreDenormalMode, bool, 1, NoFineGrainedMemory)
+
+#undef OPTION

yxsamliu wrote:

will fix

https://github.com/llvm/llvm-project/pull/114841
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add clang atomic control options and attribute (PR #114841)

2024-11-07 Thread Yaxun Liu via cfe-commits


@@ -1093,6 +1097,169 @@ inline void FPOptions::applyChanges(FPOptionsOverride 
FPO) {
   *this = FPO.applyOverrides(*this);
 }
 
+/// Atomic control options
+class AtomicOptionsOverride;
+class AtomicOptions {
+public:
+  using storage_type = uint16_t;
+
+  static constexpr unsigned StorageBitSize = 8 * sizeof(storage_type);
+
+  static constexpr storage_type FirstShift = 0, FirstWidth = 0;
+#define OPTION(NAME, TYPE, WIDTH, PREVIOUS)
\
+  static constexpr storage_type NAME##Shift =  
\
+  PREVIOUS##Shift + PREVIOUS##Width;   
\
+  static constexpr storage_type NAME##Width = WIDTH;   
\
+  static constexpr storage_type NAME##Mask = ((1 << NAME##Width) - 1)  
\
+ << NAME##Shift;
+#include "clang/Basic/AtomicOptions.def"
+
+  static constexpr storage_type TotalWidth = 0
+#define OPTION(NAME, TYPE, WIDTH, PREVIOUS) +WIDTH
+#include "clang/Basic/AtomicOptions.def"
+  ;
+  static_assert(TotalWidth <= StorageBitSize,
+"Too short type for AtomicOptions");
+
+private:
+  storage_type Value;
+
+  AtomicOptionsOverride getChangesSlow(const AtomicOptions &Base) const;
+
+public:
+  AtomicOptions() : Value(0) {
+setNoRemoteMemory(false);
+setNoFineGrainedMemory(false);
+setIgnoreDenormalMode(false);
+  }
+  explicit AtomicOptions(const LangOptions &LO) {
+Value = 0;
+#if 0

yxsamliu wrote:

will remove this ctor. The relevant language options actually define an 
AtomicOptionsOverride object, which has a ctor for that.

https://github.com/llvm/llvm-project/pull/114841
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add clang atomic control options and attribute (PR #114841)

2024-11-04 Thread Artem Belevich via cfe-commits


@@ -1093,6 +1097,169 @@ inline void FPOptions::applyChanges(FPOptionsOverride 
FPO) {
   *this = FPO.applyOverrides(*this);
 }
 
+/// Atomic control options
+class AtomicOptionsOverride;
+class AtomicOptions {
+public:
+  using storage_type = uint16_t;
+
+  static constexpr unsigned StorageBitSize = 8 * sizeof(storage_type);
+
+  static constexpr storage_type FirstShift = 0, FirstWidth = 0;
+#define OPTION(NAME, TYPE, WIDTH, PREVIOUS)
\
+  static constexpr storage_type NAME##Shift =  
\
+  PREVIOUS##Shift + PREVIOUS##Width;   
\
+  static constexpr storage_type NAME##Width = WIDTH;   
\
+  static constexpr storage_type NAME##Mask = ((1 << NAME##Width) - 1)  
\
+ << NAME##Shift;
+#include "clang/Basic/AtomicOptions.def"
+
+  static constexpr storage_type TotalWidth = 0
+#define OPTION(NAME, TYPE, WIDTH, PREVIOUS) +WIDTH
+#include "clang/Basic/AtomicOptions.def"
+  ;
+  static_assert(TotalWidth <= StorageBitSize,
+"Too short type for AtomicOptions");
+
+private:
+  storage_type Value;
+
+  AtomicOptionsOverride getChangesSlow(const AtomicOptions &Base) const;
+
+public:
+  AtomicOptions() : Value(0) {
+setNoRemoteMemory(false);
+setNoFineGrainedMemory(false);
+setIgnoreDenormalMode(false);
+  }
+  explicit AtomicOptions(const LangOptions &LO) {
+Value = 0;
+#if 0

Artem-B wrote:

What's the plan for the ifdefed-out lines?

https://github.com/llvm/llvm-project/pull/114841
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add clang atomic control options and attribute (PR #114841)

2024-11-04 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang-driver

Author: Yaxun (Sam) Liu (yxsamliu)


Changes

Add option and statement attribute for controlling emitting of target-specific 
metadata to atomicrmw instructions in IR.

The RFC for this attribute and option is
https://discourse.llvm.org/t/rfc-add-clang-atomic-control-options-and-pragmas/80641,
 Originally a pragma was proposed, then it was changed to clang attribute.

This attribute allows users to specify one, two, or all three options and must 
be applied to a compound statement. The attribute can also be nested, with 
inner attributes overriding the options specified by outer attributes or the 
target's default options. These options will then determine the target-specific 
metadata added to atomic instructions in the IR.

In addition to the attribute, a new compiler option is introduced: 
-fatomic=no_remote_memory:{on|off},no_fine_grained_memory:{on|off},ignore_denormal_mode{on|off}.
 This compiler option allows users to override the target's default options 
through the Clang driver and front end.

In terms of implementation, the atomic attribute is represented in the AST by 
the existing AttributedStmt, with minimal changes to AST and Sema.

During code generation in Clang, the CodeGenModule maintains the current atomic 
options, which are used to emit the relevant metadata for atomic instructions. 
RAII is used to manage the saving and restoring of atomic options when entering 
and exiting nested AttributedStmt.

---

Patch is 140.60 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/114841.diff


26 Files Affected:

- (added) clang/include/clang/Basic/AtomicOptions.def (+19) 
- (modified) clang/include/clang/Basic/Attr.td (+56) 
- (modified) clang/include/clang/Basic/DiagnosticDriverKinds.td (+7) 
- (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+2) 
- (modified) clang/include/clang/Basic/LangOptions.h (+167) 
- (modified) clang/include/clang/Basic/TargetInfo.h (+6) 
- (modified) clang/include/clang/Driver/Options.td (+8) 
- (modified) clang/include/clang/Parse/Parser.h (+5) 
- (modified) clang/lib/Basic/LangOptions.cpp (+52) 
- (modified) clang/lib/Basic/Targets/AMDGPU.cpp (+7) 
- (modified) clang/lib/CodeGen/CGStmt.cpp (+5) 
- (modified) clang/lib/CodeGen/CodeGenFunction.h (+17) 
- (modified) clang/lib/CodeGen/CodeGenModule.cpp (+2-1) 
- (modified) clang/lib/CodeGen/CodeGenModule.h (+8) 
- (modified) clang/lib/CodeGen/Targets/AMDGPU.cpp (+12-10) 
- (modified) clang/lib/Driver/ToolChains/Clang.cpp (+26) 
- (modified) clang/lib/Parse/ParseDecl.cpp (+71) 
- (modified) clang/lib/Sema/SemaStmtAttr.cpp (+43) 
- (added) clang/test/AST/ast-dump-atomic-options.hip (+102) 
- (modified) clang/test/CodeGenCUDA/amdgpu-atomic-ops.cu (+95-100) 
- (modified) clang/test/CodeGenCUDA/atomic-ops.cu (+100-100) 
- (added) clang/test/CodeGenCUDA/atomic-options.hip (+456) 
- (added) clang/test/Driver/atomic-options.hip (+31) 
- (modified) clang/test/OpenMP/amdgpu-unsafe-fp-atomics.cpp (+6-4) 
- (added) clang/test/Parser/Inputs/cuda.h (+54) 
- (added) clang/test/Parser/atomic-options.hip (+30) 


``diff
diff --git a/clang/include/clang/Basic/AtomicOptions.def 
b/clang/include/clang/Basic/AtomicOptions.def
new file mode 100644
index 00..4cf2dab581c8b4
--- /dev/null
+++ b/clang/include/clang/Basic/AtomicOptions.def
@@ -0,0 +1,19 @@
+//===--- AtomicOptions.def - Atomic Options database -*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+// This file defines the Atomic language options. Users of this file
+// must define the OPTION macro to make use of this information.
+#ifndef OPTION
+#  error Define the OPTION macro to handle atomic language options
+#endif
+
+// OPTION(name, type, width, previousName)
+OPTION(NoRemoteMemory, bool, 1, First)
+OPTION(NoFineGrainedMemory, bool, 1, NoRemoteMemory)
+OPTION(IgnoreDenormalMode, bool, 1, NoFineGrainedMemory)
+
+#undef OPTION
\ No newline at end of file
diff --git a/clang/include/clang/Basic/Attr.td 
b/clang/include/clang/Basic/Attr.td
index 156fbd1c4442eb..6b5fea1965aec3 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -4838,3 +4838,59 @@ def ClspvLibclcBuiltin: InheritableAttr {
   let Documentation = [ClspvLibclcBuiltinDoc];
   let SimpleHandler = 1;
 }
+
+def Atomic : StmtAttr {
+  let Spellings = [Clang<"atomic">];
+  let Args = [
+EnumArgument<"NoRemoteMemory", "NoRemoteMemoryTy", /*IsString*/ false,
+  ["no_remote_memory", "!no_remote_memory", ""],
+  ["NoRemoteMemoryOn", "NoRemoteMemoryOff", "NoRemoteMemoryUnset"]>,
+EnumArgument<"NoFineGrainedMemory", "NoFineGrainedMemoryTy", /*IsString*/ 
false,
+  ["no_fine_grain

[clang] Add clang atomic control options and attribute (PR #114841)

2024-11-04 Thread Matt Arsenault via cfe-commits


@@ -0,0 +1,19 @@
+//===--- AtomicOptions.def - Atomic Options database -*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+// This file defines the Atomic language options. Users of this file
+// must define the OPTION macro to make use of this information.
+#ifndef OPTION
+#  error Define the OPTION macro to handle atomic language options
+#endif
+
+// OPTION(name, type, width, previousName)
+OPTION(NoRemoteMemory, bool, 1, First)
+OPTION(NoFineGrainedMemory, bool, 1, NoRemoteMemory)
+OPTION(IgnoreDenormalMode, bool, 1, NoFineGrainedMemory)
+
+#undef OPTION

arsenm wrote:

Missing end of file line end

https://github.com/llvm/llvm-project/pull/114841
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add clang atomic control options and attribute (PR #114841)

2024-11-04 Thread Matt Arsenault via cfe-commits


@@ -569,19 +569,21 @@ void AMDGPUTargetCodeGenInfo::setTargetAtomicMetadata(
 AtomicInst.setMetadata(llvm::LLVMContext::MD_noalias_addrspace, ASRange);
   }
 
-  if (!RMW || !CGF.getTarget().allowAMDGPUUnsafeFPAtomics())
+  if (!RMW)
 return;
 
-  // TODO: Introduce new, more controlled options that also work for integers,
-  // and deprecate allowAMDGPUUnsafeFPAtomics.
-  llvm::AtomicRMWInst::BinOp RMWOp = RMW->getOperation();
-  if (llvm::AtomicRMWInst::isFPOperation(RMWOp)) {
-llvm::MDNode *Empty = llvm::MDNode::get(CGF.getLLVMContext(), {});
+  AtomicOptions AO = CGF.CGM.getAtomicOpts();
+  llvm::MDNode *Empty = llvm::MDNode::get(CGF.getLLVMContext(), {});
+  if (AO.getNoFineGrainedMemory())
 RMW->setMetadata("amdgpu.no.fine.grained.memory", Empty);
-
-if (RMWOp == llvm::AtomicRMWInst::FAdd && RMW->getType()->isFloatTy())
-  RMW->setMetadata("amdgpu.ignore.denormal.mode", Empty);
-  }
+  if (AO.getNoRemoteMemory())
+RMW->setMetadata("amdgpu.no.remote.memory", Empty);
+
+  if ((AO.getIgnoreDenormalMode() ||
+   CGF.getTarget().allowAMDGPUUnsafeFPAtomics()) &&

arsenm wrote:

allowAMDGPUUnsafeFPAtomics() is no longer implying 
amdgpu.no.fine.grained.memory?

https://github.com/llvm/llvm-project/pull/114841
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add clang atomic control options and attribute (PR #114841)

2024-11-04 Thread Matt Arsenault via cfe-commits


@@ -0,0 +1,30 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -fsyntax-only -verify -fcuda-is-device %s
+// RUN: %clang_cc1 -fsyntax-only -verify -fcuda-is-device %s \
+// RUN:   
-fatomic=no_fine_grained_memory:off,no_remote_memory:on,ignore_denormal_mode:on
+
+#include "Inputs/cuda.h"
+
+[[clang::atomic(!no_remote_memory)]] // expected-error {{'atomic' attribute 
cannot be applied to a declaration}}
+__device__ __host__ void test_location(float *a) {
+  __scoped_atomic_fetch_add(a, 1, __ATOMIC_RELAXED, __MEMORY_SCOPE_SYSTEM);
+  [[clang::atomic(!no_remote_memory)]] int x; // expected-error {{'atomic' 
attribute cannot be applied to a declaration}}
+}
+
+__device__ __host__ void test_invalid_option(float *a) {
+  [[clang::atomic(fast)]] { // expected-error {{invalid argument 'fast' to 
atomic attribute; valid options are: 'no_remote_memory', 
'no_fine_grained_memory', 'ignore_denormal_mode' (optionally prefixed with 
'!')}}
+__scoped_atomic_fetch_add(a, 1, __ATOMIC_RELAXED, __MEMORY_SCOPE_SYSTEM);
+  }
+}
+
+__device__ __host__ void test_invalid_value(float *a) {
+  [[clang::atomic(no_remote_memory(default))]] { // expected-error {{expected 
')'}} expected-note {{to match this '('}}
+__scoped_atomic_fetch_add(a, 1, __ATOMIC_RELAXED, __MEMORY_SCOPE_SYSTEM);
+  }
+}
+
+__device__ __host__ void test_invalid_format(float *a) {
+  [[clang::atomic(no_remote_memory=on)]] { // expected-error {{expected ')'}} 
expected-note {{to match this '('}}
+__scoped_atomic_fetch_add(a, 1, __ATOMIC_RELAXED, __MEMORY_SCOPE_SYSTEM);
+  }
+}

arsenm wrote:

Missing newline 

https://github.com/llvm/llvm-project/pull/114841
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add clang atomic control options and attribute (PR #114841)

2024-11-04 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-backend-amdgpu

Author: Yaxun (Sam) Liu (yxsamliu)


Changes

Add option and statement attribute for controlling emitting of target-specific 
metadata to atomicrmw instructions in IR.

The RFC for this attribute and option is
https://discourse.llvm.org/t/rfc-add-clang-atomic-control-options-and-pragmas/80641,
 Originally a pragma was proposed, then it was changed to clang attribute.

This attribute allows users to specify one, two, or all three options and must 
be applied to a compound statement. The attribute can also be nested, with 
inner attributes overriding the options specified by outer attributes or the 
target's default options. These options will then determine the target-specific 
metadata added to atomic instructions in the IR.

In addition to the attribute, a new compiler option is introduced: 
-fatomic=no_remote_memory:{on|off},no_fine_grained_memory:{on|off},ignore_denormal_mode{on|off}.
 This compiler option allows users to override the target's default options 
through the Clang driver and front end.

In terms of implementation, the atomic attribute is represented in the AST by 
the existing AttributedStmt, with minimal changes to AST and Sema.

During code generation in Clang, the CodeGenModule maintains the current atomic 
options, which are used to emit the relevant metadata for atomic instructions. 
RAII is used to manage the saving and restoring of atomic options when entering 
and exiting nested AttributedStmt.

---

Patch is 140.60 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/114841.diff


26 Files Affected:

- (added) clang/include/clang/Basic/AtomicOptions.def (+19) 
- (modified) clang/include/clang/Basic/Attr.td (+56) 
- (modified) clang/include/clang/Basic/DiagnosticDriverKinds.td (+7) 
- (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+2) 
- (modified) clang/include/clang/Basic/LangOptions.h (+167) 
- (modified) clang/include/clang/Basic/TargetInfo.h (+6) 
- (modified) clang/include/clang/Driver/Options.td (+8) 
- (modified) clang/include/clang/Parse/Parser.h (+5) 
- (modified) clang/lib/Basic/LangOptions.cpp (+52) 
- (modified) clang/lib/Basic/Targets/AMDGPU.cpp (+7) 
- (modified) clang/lib/CodeGen/CGStmt.cpp (+5) 
- (modified) clang/lib/CodeGen/CodeGenFunction.h (+17) 
- (modified) clang/lib/CodeGen/CodeGenModule.cpp (+2-1) 
- (modified) clang/lib/CodeGen/CodeGenModule.h (+8) 
- (modified) clang/lib/CodeGen/Targets/AMDGPU.cpp (+12-10) 
- (modified) clang/lib/Driver/ToolChains/Clang.cpp (+26) 
- (modified) clang/lib/Parse/ParseDecl.cpp (+71) 
- (modified) clang/lib/Sema/SemaStmtAttr.cpp (+43) 
- (added) clang/test/AST/ast-dump-atomic-options.hip (+102) 
- (modified) clang/test/CodeGenCUDA/amdgpu-atomic-ops.cu (+95-100) 
- (modified) clang/test/CodeGenCUDA/atomic-ops.cu (+100-100) 
- (added) clang/test/CodeGenCUDA/atomic-options.hip (+456) 
- (added) clang/test/Driver/atomic-options.hip (+31) 
- (modified) clang/test/OpenMP/amdgpu-unsafe-fp-atomics.cpp (+6-4) 
- (added) clang/test/Parser/Inputs/cuda.h (+54) 
- (added) clang/test/Parser/atomic-options.hip (+30) 


``diff
diff --git a/clang/include/clang/Basic/AtomicOptions.def 
b/clang/include/clang/Basic/AtomicOptions.def
new file mode 100644
index 00..4cf2dab581c8b4
--- /dev/null
+++ b/clang/include/clang/Basic/AtomicOptions.def
@@ -0,0 +1,19 @@
+//===--- AtomicOptions.def - Atomic Options database -*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+// This file defines the Atomic language options. Users of this file
+// must define the OPTION macro to make use of this information.
+#ifndef OPTION
+#  error Define the OPTION macro to handle atomic language options
+#endif
+
+// OPTION(name, type, width, previousName)
+OPTION(NoRemoteMemory, bool, 1, First)
+OPTION(NoFineGrainedMemory, bool, 1, NoRemoteMemory)
+OPTION(IgnoreDenormalMode, bool, 1, NoFineGrainedMemory)
+
+#undef OPTION
\ No newline at end of file
diff --git a/clang/include/clang/Basic/Attr.td 
b/clang/include/clang/Basic/Attr.td
index 156fbd1c4442eb..6b5fea1965aec3 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -4838,3 +4838,59 @@ def ClspvLibclcBuiltin: InheritableAttr {
   let Documentation = [ClspvLibclcBuiltinDoc];
   let SimpleHandler = 1;
 }
+
+def Atomic : StmtAttr {
+  let Spellings = [Clang<"atomic">];
+  let Args = [
+EnumArgument<"NoRemoteMemory", "NoRemoteMemoryTy", /*IsString*/ false,
+  ["no_remote_memory", "!no_remote_memory", ""],
+  ["NoRemoteMemoryOn", "NoRemoteMemoryOff", "NoRemoteMemoryUnset"]>,
+EnumArgument<"NoFineGrainedMemory", "NoFineGrainedMemoryTy", /*IsString*/ 
false,
+  ["no_fine_gra

[clang] Add clang atomic control options and attribute (PR #114841)

2024-11-04 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: Yaxun (Sam) Liu (yxsamliu)


Changes

Add option and statement attribute for controlling emitting of target-specific 
metadata to atomicrmw instructions in IR.

The RFC for this attribute and option is
https://discourse.llvm.org/t/rfc-add-clang-atomic-control-options-and-pragmas/80641,
 Originally a pragma was proposed, then it was changed to clang attribute.

This attribute allows users to specify one, two, or all three options and must 
be applied to a compound statement. The attribute can also be nested, with 
inner attributes overriding the options specified by outer attributes or the 
target's default options. These options will then determine the target-specific 
metadata added to atomic instructions in the IR.

In addition to the attribute, a new compiler option is introduced: 
-fatomic=no_remote_memory:{on|off},no_fine_grained_memory:{on|off},ignore_denormal_mode{on|off}.
 This compiler option allows users to override the target's default options 
through the Clang driver and front end.

In terms of implementation, the atomic attribute is represented in the AST by 
the existing AttributedStmt, with minimal changes to AST and Sema.

During code generation in Clang, the CodeGenModule maintains the current atomic 
options, which are used to emit the relevant metadata for atomic instructions. 
RAII is used to manage the saving and restoring of atomic options when entering 
and exiting nested AttributedStmt.

---

Patch is 140.60 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/114841.diff


26 Files Affected:

- (added) clang/include/clang/Basic/AtomicOptions.def (+19) 
- (modified) clang/include/clang/Basic/Attr.td (+56) 
- (modified) clang/include/clang/Basic/DiagnosticDriverKinds.td (+7) 
- (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+2) 
- (modified) clang/include/clang/Basic/LangOptions.h (+167) 
- (modified) clang/include/clang/Basic/TargetInfo.h (+6) 
- (modified) clang/include/clang/Driver/Options.td (+8) 
- (modified) clang/include/clang/Parse/Parser.h (+5) 
- (modified) clang/lib/Basic/LangOptions.cpp (+52) 
- (modified) clang/lib/Basic/Targets/AMDGPU.cpp (+7) 
- (modified) clang/lib/CodeGen/CGStmt.cpp (+5) 
- (modified) clang/lib/CodeGen/CodeGenFunction.h (+17) 
- (modified) clang/lib/CodeGen/CodeGenModule.cpp (+2-1) 
- (modified) clang/lib/CodeGen/CodeGenModule.h (+8) 
- (modified) clang/lib/CodeGen/Targets/AMDGPU.cpp (+12-10) 
- (modified) clang/lib/Driver/ToolChains/Clang.cpp (+26) 
- (modified) clang/lib/Parse/ParseDecl.cpp (+71) 
- (modified) clang/lib/Sema/SemaStmtAttr.cpp (+43) 
- (added) clang/test/AST/ast-dump-atomic-options.hip (+102) 
- (modified) clang/test/CodeGenCUDA/amdgpu-atomic-ops.cu (+95-100) 
- (modified) clang/test/CodeGenCUDA/atomic-ops.cu (+100-100) 
- (added) clang/test/CodeGenCUDA/atomic-options.hip (+456) 
- (added) clang/test/Driver/atomic-options.hip (+31) 
- (modified) clang/test/OpenMP/amdgpu-unsafe-fp-atomics.cpp (+6-4) 
- (added) clang/test/Parser/Inputs/cuda.h (+54) 
- (added) clang/test/Parser/atomic-options.hip (+30) 


``diff
diff --git a/clang/include/clang/Basic/AtomicOptions.def 
b/clang/include/clang/Basic/AtomicOptions.def
new file mode 100644
index 00..4cf2dab581c8b4
--- /dev/null
+++ b/clang/include/clang/Basic/AtomicOptions.def
@@ -0,0 +1,19 @@
+//===--- AtomicOptions.def - Atomic Options database -*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+// This file defines the Atomic language options. Users of this file
+// must define the OPTION macro to make use of this information.
+#ifndef OPTION
+#  error Define the OPTION macro to handle atomic language options
+#endif
+
+// OPTION(name, type, width, previousName)
+OPTION(NoRemoteMemory, bool, 1, First)
+OPTION(NoFineGrainedMemory, bool, 1, NoRemoteMemory)
+OPTION(IgnoreDenormalMode, bool, 1, NoFineGrainedMemory)
+
+#undef OPTION
\ No newline at end of file
diff --git a/clang/include/clang/Basic/Attr.td 
b/clang/include/clang/Basic/Attr.td
index 156fbd1c4442eb..6b5fea1965aec3 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -4838,3 +4838,59 @@ def ClspvLibclcBuiltin: InheritableAttr {
   let Documentation = [ClspvLibclcBuiltinDoc];
   let SimpleHandler = 1;
 }
+
+def Atomic : StmtAttr {
+  let Spellings = [Clang<"atomic">];
+  let Args = [
+EnumArgument<"NoRemoteMemory", "NoRemoteMemoryTy", /*IsString*/ false,
+  ["no_remote_memory", "!no_remote_memory", ""],
+  ["NoRemoteMemoryOn", "NoRemoteMemoryOff", "NoRemoteMemoryUnset"]>,
+EnumArgument<"NoFineGrainedMemory", "NoFineGrainedMemoryTy", /*IsString*/ 
false,
+  ["no_fine_grained_memo