[clang] [RFC] Add clang atomic control options and pragmas (PR #102569)

via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 8 22:18:23 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-hlsl

@llvm/pr-subscribers-coroutines

Author: Yaxun (Sam) Liu (yxsamliu)

<details>
<summary>Changes</summary>

This RFC proposes the addition of an atomic pragma to Clang, designed to provide a more flexible mechanism for users to specify how atomic operations should be handled during the lowering process in LLVM IR. Currently, the atomicrmw instruction in LLVM IR can be lowered to either atomic instructions or CAS loops, depending on whether the target supports atomic instructions for a specific operation type or alignment. However, there are cases where the decision-making process for lowering an atomicrmw instruction cannot be fully expressed by the existing IR.

For instance, consider a scenario where a floating-point atomic add instruction does not conform to IEEE denormal mode requirements on a particular subtarget. Even though this non-conformance exists, users might still prefer the corresponding IR to be lowered to atomic instructions if they are unconcerned about denormal mode. This means that the backend needs to be informed through IR whether to ignore the floating-point denormal mode during the lowering process. Another example involves an atomic instruction that may not function correctly for specific memory types, such as memory accessed through PCIe, which only supports atomic integer add, exchange, or compare-and-swap operations. To ensure correct and efficient lowering of atomicrmw instructions, the backend must be aware of the memory type involved.

To convey this necessary information to the backend, we propose adding target-specific metadata to atomicrmw instructions in IR. Since this information is provided by users, a flexible mechanism is needed to allow them to specify these details in the source code. To achieve this, we introduce a pragma in the format of This pragma allows users to specify one, two, or all three options and must be placed at the beginning of a compound statement. The pragma can also be nested, with inner pragmas overriding the options specified in outer compound statements 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 pragma, 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.

The design of this atomic pragma and the associated compiler options are intended to be target-neutral, enabling potential reuse across different targets. While a target might choose not to emit metadata for some or all of these options, or might add new options to the pragma, the overall design is inspired by Clang's floating-point pragma, which conveys extra information to the backend about how floating-point instructions should be lowered. Importantly, the metadata introduced by this pragma in the IR can be dropped without affecting the correctness of the program, as it is primarily intended to improve performance.

In terms of implementation, the atomic pragma is represented in the AST by trailing data in CompoundStmt. The parser in Clang maintains an atomic options stack in Sema, which is updated whenever the atomic pragma is encountered. When a CompoundStmt is created, it includes the current atomic options. RAII is employed to save and restore atomic options when transitioning between outer and inner CompoundStmts.

During code generation in Clang, the CodeGenModule maintains the current atomic options, which are used to emit the relevant metadata for atomic instructions. As with the parsing phase, RAII is used to manage the saving and restoring of atomic options when entering and exiting nested CompoundStmts. This ensures that the correct metadata is generated in the IR, reflecting the user's specified options accurately.

---

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


47 Files Affected:

- (modified) clang/include/clang/AST/Stmt.h (+38-4) 
- (modified) clang/include/clang/AST/TextNodeDumper.h (+1) 
- (added) clang/include/clang/Basic/AtomicOptions.def (+19) 
- (modified) clang/include/clang/Basic/DiagnosticDriverKinds.td (+7) 
- (modified) clang/include/clang/Basic/DiagnosticParseKinds.td (+9) 
- (modified) clang/include/clang/Basic/LangOptions.h (+167) 
- (modified) clang/include/clang/Basic/PragmaKinds.h (+7) 
- (modified) clang/include/clang/Basic/TargetInfo.h (+6) 
- (modified) clang/include/clang/Basic/TokenKinds.def (+2) 
- (modified) clang/include/clang/Driver/Options.td (+8) 
- (modified) clang/include/clang/Parse/Parser.h (+5) 
- (modified) clang/include/clang/Sema/Sema.h (+36-2) 
- (modified) clang/lib/AST/ASTImporter.cpp (+4-1) 
- (modified) clang/lib/AST/Stmt.cpp (+16-7) 
- (modified) clang/lib/AST/StmtPrinter.cpp (+23) 
- (modified) clang/lib/AST/TextNodeDumper.cpp (+9) 
- (modified) clang/lib/Analysis/BodyFarm.cpp (+2-1) 
- (modified) clang/lib/Basic/LangOptions.cpp (+52) 
- (modified) clang/lib/Basic/Targets/AMDGPU.cpp (+7) 
- (modified) clang/lib/CodeGen/CGCoroutine.cpp (+2-1) 
- (modified) clang/lib/CodeGen/CGStmt.cpp (+2) 
- (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 (+9-12) 
- (modified) clang/lib/Driver/ToolChains/Clang.cpp (+26) 
- (modified) clang/lib/Parse/ParsePragma.cpp (+147) 
- (modified) clang/lib/Parse/ParseStmt.cpp (+11) 
- (modified) clang/lib/Parse/Parser.cpp (+3) 
- (modified) clang/lib/Sema/HLSLExternalSemaSource.cpp (+6-6) 
- (modified) clang/lib/Sema/Sema.cpp (+9-1) 
- (modified) clang/lib/Sema/SemaAttr.cpp (+18) 
- (modified) clang/lib/Sema/SemaCoroutine.cpp (+2-1) 
- (modified) clang/lib/Sema/SemaDeclCXX.cpp (+2) 
- (modified) clang/lib/Sema/SemaExprCXX.cpp (+3-3) 
- (modified) clang/lib/Sema/SemaOpenMP.cpp (+12-9) 
- (modified) clang/lib/Sema/SemaStmt.cpp (+2-1) 
- (modified) clang/lib/Sema/TreeTransform.h (+3) 
- (modified) clang/lib/Serialization/ASTReaderStmt.cpp (+3-1) 
- (added) clang/test/AST/ast-dump-atomic-options.hip (+80) 
- (modified) clang/test/CodeGen/AMDGPU/amdgpu-atomic-float.c (+108-228) 
- (modified) clang/test/CodeGenCUDA/amdgpu-atomic-ops.cu (+76-83) 
- (added) clang/test/CodeGenCUDA/atomic-options.hip (+449) 
- (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 (+28) 


``````````diff
diff --git a/clang/include/clang/AST/Stmt.h b/clang/include/clang/AST/Stmt.h
index bbd7634bcc3bfb..5f8580e8752de6 100644
--- a/clang/include/clang/AST/Stmt.h
+++ b/clang/include/clang/AST/Stmt.h
@@ -152,6 +152,11 @@ class alignas(void *) Stmt {
     LLVM_PREFERRED_TYPE(bool)
     unsigned HasFPFeatures : 1;
 
+    /// True if the compound statement has one or more pragmas that set some
+    /// atomic options.
+    LLVM_PREFERRED_TYPE(bool)
+    unsigned HasAtomicOptions : 1;
+
     unsigned NumStmts;
   };
 
@@ -1603,7 +1608,8 @@ class NullStmt : public Stmt {
 /// CompoundStmt - This represents a group of statements like { stmt stmt }.
 class CompoundStmt final
     : public Stmt,
-      private llvm::TrailingObjects<CompoundStmt, Stmt *, FPOptionsOverride> {
+      private llvm::TrailingObjects<CompoundStmt, Stmt *, FPOptionsOverride,
+                                    AtomicOptionsOverride> {
   friend class ASTStmtReader;
   friend TrailingObjects;
 
@@ -1614,7 +1620,8 @@ class CompoundStmt final
   SourceLocation RBraceLoc;
 
   CompoundStmt(ArrayRef<Stmt *> Stmts, FPOptionsOverride FPFeatures,
-               SourceLocation LB, SourceLocation RB);
+               AtomicOptionsOverride AtomicOptions, SourceLocation LB,
+               SourceLocation RB);
   explicit CompoundStmt(EmptyShell Empty) : Stmt(CompoundStmtClass, Empty) {}
 
   void setStmts(ArrayRef<Stmt *> Stmts);
@@ -1625,13 +1632,24 @@ class CompoundStmt final
     *getTrailingObjects<FPOptionsOverride>() = F;
   }
 
+  /// Set AtomicOptionsOverride in trailing storage. Used only by Serialization.
+  void setStoredAtomicOptions(AtomicOptionsOverride A) {
+    assert(hasStoredAtomicOptions());
+    *getTrailingObjects<AtomicOptionsOverride>() = A;
+  }
+
   size_t numTrailingObjects(OverloadToken<Stmt *>) const {
     return CompoundStmtBits.NumStmts;
   }
 
+  size_t numTrailingObjects(OverloadToken<FPOptionsOverride>) const {
+    return CompoundStmtBits.HasFPFeatures;
+  }
+
 public:
   static CompoundStmt *Create(const ASTContext &C, ArrayRef<Stmt *> Stmts,
-                              FPOptionsOverride FPFeatures, SourceLocation LB,
+                              FPOptionsOverride FPFeatures,
+                              AtomicOptionsOverride, SourceLocation LB,
                               SourceLocation RB);
 
   // Build an empty compound statement with a location.
@@ -1641,16 +1659,20 @@ class CompoundStmt final
       : Stmt(CompoundStmtClass), LBraceLoc(Loc), RBraceLoc(EndLoc) {
     CompoundStmtBits.NumStmts = 0;
     CompoundStmtBits.HasFPFeatures = 0;
+    CompoundStmtBits.HasAtomicOptions = 0;
   }
 
   // Build an empty compound statement.
   static CompoundStmt *CreateEmpty(const ASTContext &C, unsigned NumStmts,
-                                   bool HasFPFeatures);
+                                   bool HasFPFeatures, bool HasAtomicOptions);
 
   bool body_empty() const { return CompoundStmtBits.NumStmts == 0; }
   unsigned size() const { return CompoundStmtBits.NumStmts; }
 
   bool hasStoredFPFeatures() const { return CompoundStmtBits.HasFPFeatures; }
+  bool hasStoredAtomicOptions() const {
+    return CompoundStmtBits.HasAtomicOptions;
+  }
 
   /// Get FPOptionsOverride from trailing storage.
   FPOptionsOverride getStoredFPFeatures() const {
@@ -1663,6 +1685,18 @@ class CompoundStmt final
     return hasStoredFPFeatures() ? getStoredFPFeatures() : FPOptionsOverride();
   }
 
+  /// Get AtomicOptionsOverride from trailing storage.
+  AtomicOptionsOverride getStoredAtomicOptions() const {
+    assert(hasStoredAtomicOptions());
+    return *getTrailingObjects<AtomicOptionsOverride>();
+  }
+
+  /// Get the stored AtomicOptionsOverride or default if not stored.
+  AtomicOptionsOverride getStoredAtomicOptionsOrDefault() const {
+    return hasStoredAtomicOptions() ? getStoredAtomicOptions()
+                                    : AtomicOptionsOverride();
+  }
+
   using body_iterator = Stmt **;
   using body_range = llvm::iterator_range<body_iterator>;
 
diff --git a/clang/include/clang/AST/TextNodeDumper.h b/clang/include/clang/AST/TextNodeDumper.h
index 39dd1f515c9eb3..f9b47df8db70e5 100644
--- a/clang/include/clang/AST/TextNodeDumper.h
+++ b/clang/include/clang/AST/TextNodeDumper.h
@@ -157,6 +157,7 @@ class TextNodeDumper
 
   const char *getCommandName(unsigned CommandID);
   void printFPOptions(FPOptionsOverride FPO);
+  void printAtomicOptions(AtomicOptionsOverride AO);
 
   void dumpAPValueChildren(const APValue &Value, QualType Ty,
                            const APValue &(*IdxToChildFun)(const APValue &,
diff --git a/clang/include/clang/Basic/AtomicOptions.def b/clang/include/clang/Basic/AtomicOptions.def
new file mode 100644
index 00000000000000..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/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index 3d8240f8357b40..38f0a0365a8300 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -301,6 +301,13 @@ def err_drv_invalid_int_value : Error<"invalid integral value '%1' in '%0'">;
 def err_drv_invalid_value_with_suggestion : Error<
     "invalid value '%1' in '%0', expected one of: %2">;
 def err_drv_alignment_not_power_of_two : Error<"alignment is not a power of 2 in '%0'">;
+
+def err_drv_invalid_atomic_option : Error<
+  "invalid argument '%0' to -fatomic=; must be a "
+  "comma-separated list of key:value pairs, where allowed keys are "
+  "'no_fine_grained_memory', 'no_remote_memory', 'ignore_denormal_mode', "
+  "and values are 'on' or 'off', and each key must be unique">;
+
 def err_drv_invalid_remap_file : Error<
     "invalid option '%0' not of the form <from-file>;<to-file>">;
 def err_drv_invalid_gcc_install_dir : Error<"'%0' does not contain a GCC installation">;
diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td
index f8d50d12bb9351..647d1e208472c3 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -1272,6 +1272,9 @@ def warn_pragma_init_seg_unsupported_target : Warning<
 def err_pragma_file_or_compound_scope : Error<
   "'#pragma %0' can only appear at file scope or at the start of a "
   "compound statement">;
+// - #pragma restricted to start of compound statement
+def err_pragma_compound_scope : Error<
+  "'#pragma %0' can only appear at the start of a compound statement">;
 // - #pragma stdc unknown
 def ext_stdc_pragma_ignored : ExtWarn<"unknown pragma in STDC namespace">,
    InGroup<UnknownPragmas>;
@@ -1655,6 +1658,12 @@ def err_pragma_fp_invalid_argument : Error<
   "'ignore', 'maytrap' or 'strict'|"
   "'source', 'double' or 'extended'}2">;
 
+def err_pragma_atomic_invalid_option : Error<
+  "%select{invalid|missing}0 option%select{ %1|}0; expected 'no_remote_memory', 'no_fine_grained_memory', or 'ignore_denormal_mode'">;
+
+def err_pragma_atomic_invalid_argument : Error<
+  "unexpected argument '%0' to '#pragma clang atomic %1'; expected 'on' or 'off'">;
+
 def err_pragma_invalid_keyword : Error<
   "invalid argument; expected 'enable'%select{|, 'full'}0%select{|, 'assume_safety'}1 or 'disable'">;
 def err_pragma_pipeline_invalid_keyword : Error<
diff --git a/clang/include/clang/Basic/LangOptions.h b/clang/include/clang/Basic/LangOptions.h
index 91f1c2f2e6239e..617b0ed74603c8 100644
--- a/clang/include/clang/Basic/LangOptions.h
+++ b/clang/include/clang/Basic/LangOptions.h
@@ -579,6 +579,10 @@ class LangOptions : public LangOptionsBase {
   // WebAssembly target.
   bool NoWasmOpt = false;
 
+  /// The default atomic codegen options specified by command line in the
+  /// format of key:{on|off}.
+  std::vector<std::string> AtomicOptionsAsWritten;
+
   LangOptions();
 
   /// Set language defaults for the given input language and
@@ -1034,6 +1038,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
+    setNoRemoteMemory(LO.NoRemoteMemoryAccess);
+    setNoFineGrainedMemory(LO.NoFineGrainedMemoryAccess);
+    setIgnoreDenormalMode(LO.IgnoreDenormals);
+#endif
+  }
+
+  bool operator==(AtomicOptions other) const { return Value == other.Value; }
+
+  /// Return the default value of AtomicOptions that's used when trailing
+  /// storage isn't required.
+  static AtomicOptions defaultWithoutTrailingStorage(const LangOptions &LO);
+
+  storage_type getAsOpaqueInt() const { return Value; }
+  static AtomicOptions getFromOpaqueInt(storage_type Value) {
+    AtomicOptions Opts;
+    Opts.Value = Value;
+    return Opts;
+  }
+
+  /// Return difference with the given option set.
+  AtomicOptionsOverride getChangesFrom(const AtomicOptions &Base) const;
+
+  void applyChanges(AtomicOptionsOverride AO);
+
+#define OPTION(NAME, TYPE, WIDTH, PREVIOUS)                                    \
+  TYPE get##NAME() const {                                                     \
+    return static_cast<TYPE>((Value & NAME##Mask) >> NAME##Shift);             \
+  }                                                                            \
+  void set##NAME(TYPE value) {                                                 \
+    Value = (Value & ~NAME##Mask) | (storage_type(value) << NAME##Shift);      \
+  }
+#include "clang/Basic/AtomicOptions.def"
+  LLVM_DUMP_METHOD void dump();
+};
+
+/// Represents difference between two AtomicOptions values.
+class AtomicOptionsOverride {
+  AtomicOptions Options = AtomicOptions::getFromOpaqueInt(0);
+  AtomicOptions::storage_type OverrideMask = 0;
+
+public:
+  /// The type suitable for storing values of AtomicOptionsOverride. Must be
+  /// twice as wide as bit size of AtomicOption.
+  using storage_type = uint32_t;
+  static_assert(sizeof(storage_type) >= 2 * sizeof(AtomicOptions::storage_type),
+                "Too short type for AtomicOptionsOverride");
+
+  /// Bit mask selecting bits of OverrideMask in serialized representation of
+  /// AtomicOptionsOverride.
+  static constexpr storage_type OverrideMaskBits =
+      (static_cast<storage_type>(1) << AtomicOptions::StorageBitSize) - 1;
+
+  AtomicOptionsOverride() {}
+  AtomicOptionsOverride(const LangOptions &LO);
+  AtomicOptionsOverride(AtomicOptions AO)
+      : Options(AO), OverrideMask(OverrideMaskBits) {}
+  AtomicOptionsOverride(AtomicOptions AO, AtomicOptions::storage_type Mask)
+      : Options(AO), OverrideMask(Mask) {}
+
+  bool requiresTrailingStorage() const { return OverrideMask != 0; }
+
+  storage_type getAsOpaqueInt() const {
+    return (static_cast<storage_type>(Options.getAsOpaqueInt())
+            << AtomicOptions::StorageBitSize) |
+           OverrideMask;
+  }
+
+  static AtomicOptionsOverride getFromOpaqueInt(storage_type I) {
+    AtomicOptionsOverride Opts;
+    Opts.OverrideMask = I & OverrideMaskBits;
+    Opts.Options =
+        AtomicOptions::getFromOpaqueInt(I >> AtomicOptions::StorageBitSize);
+    return Opts;
+  }
+
+  AtomicOptions applyOverrides(AtomicOptions Base) {
+    AtomicOptions Result = AtomicOptions::getFromOpaqueInt(
+        (Base.getAsOpaqueInt() & ~OverrideMask) |
+        (Options.getAsOpaqueInt() & OverrideMask));
+    return Result;
+  }
+
+  AtomicOptions applyOverrides(const LangOptions &LO) {
+    return applyOverrides(AtomicOptions(LO));
+  }
+
+  bool operator==(AtomicOptionsOverride other) const {
+    return Options == other.Options && OverrideMask == other.OverrideMask;
+  }
+  bool operator!=(AtomicOptionsOverride other) const {
+    return !(*this == other);
+  }
+
+#define OPTION(NAME, TYPE, WIDTH, PREVIOUS)                                    \
+  bool has##NAME##Override() const {                                           \
+    return OverrideMask & AtomicOptions::NAME##Mask;                           \
+  }                                                                            \
+  TYPE get##NAME##Override() const {                                           \
+    assert(has##NAME##Override());                                             \
+    return Options.get##NAME();                                                \
+  }                                                                            \
+  void clear##NAME##Override() {                                               \
+    Options.set##NAME(TYPE(0));                                                \
+    OverrideMask &= ~AtomicOptions::NAME##Mask;                                \
+  }                                                                            \
+  void set##NAME##Override(TYPE value) {                                       \
+    Options.set##NAME(value);                                                  \
+    OverrideMask |= AtomicOptions::NAME##Mask;                                 \
+  }
+#include "clang/Basic/AtomicOptions.def"
+
+  LLVM_DUMP_METHOD void dump();
+};
+
+inline AtomicOptionsOverride
+AtomicOptions::getChangesFrom(const AtomicOptions &Base) const {
+  if (Value == Base.Value)
+    return AtomicOptionsOverride();
+  return getChangesSlow(Base);
+}
+
+inline void AtomicOptions::applyChanges(AtomicOptionsOverride AO) {
+  *this = AO.applyOverrides(*this);
+}
+
 /// Describes the kind of translation unit being processed.
 enum TranslationUnitKind {
   /// The translation unit is a complete translation unit.
diff --git a/clang/include/clang/Basic/PragmaKinds.h b/clang/include/clang/Basic/PragmaKinds.h
index 42f049f7323d2d..bec3140b0866bc 100644
--- a/clang/include/clang/Basic/PragmaKinds.h
+++ b/clang/include/clang/Basic/PragmaKinds.h
@@ -42,6 +42,13 @@ enum PragmaFPKind {
   PFK_Exceptions,  // #pragma clang fp exceptions
   PFK_EvalMethod   // #pragma clang fp eval_method
 };
+
+enum PragmaAtomicKind {
+  PAK_NoRemoteMemory,      // #prama clang atomic begin(no_remote_memory:on)
+  PAK_NoFineGrainedMemory, // #pragma clang atomic
+                           // begin(no_fine_grained_memory:on)
+  PAK_IgnoreDenormalMode, // #pragma clang atomic begin(ignore_denormal_mode:on)
+};
 }
 
 #endif
diff --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h
index a58fb5f9792720..7a7fe1268dbf50 100644
--- a/clang/include/clang/Basic/TargetInfo.h
+++ b/clang/include/clang/Basic/TargetInfo.h
@@ -296,6 +296,9 @@ class TargetInfo : public TransferrableTargetInfo,
   // in function attributes in IR.
   llvm::StringSet<> ReadOnlyFeatures;
 
+  // Default atomic options
+  AtomicOptions AtomicOpts;
+
 public:
   /// Construct a target for the given options.
   ///
@@ -1680,6 +1683,9 @@ class TargetInfo : public TransferrableTargetInfo,
     return CC_C;
   }
 
+  /// Get the default atomic options.
+  AtomicOptions getAtomicOpts() const { return AtomicOpts; }
+
   enum CallingConvCheckResult {
     CCCR_OK,
     CCCR_Warning,
diff --git a/clang/include/clang/Basic/TokenKinds.def b/clang/include/clang/Basic/TokenKinds.def
index 421dbb413fed93..b94aa8e2595a7f 100644
--- a/clang/include/clang/Basic/TokenKinds.def
+++ b/clang/include/clang/Basic/TokenKinds.def
@@ -999,6 +999,8 @@ PRAGMA_ANNOTATION(pragma_loop_hint)
 
 PRAGMA_ANNOTATION(pragma_fp)
 
+PRAGMA_ANNOTATION(pragma_atomic)
+
 // Annotation for the attribute pragma directives - #pragma clang attribute ...
 PRAGMA_ANNOTATION(pragma_attribute)
 
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index e196c3dc5cb3be..902129fe59fd2a 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -2303,6 +2303,14 @@ def fsymbol_partition_EQ : Joined<["-"], "fsymbol-partition=">, Group<f_Group>,
   Visibility<[ClangOption, CC1Option]>,
   MarshallingInfoString<CodeGenOpts<"SymbolPartition">>;
 
+def fatomic_EQ : CommaJoined<["-"], "fatomic=">, Group<f_Group>,
+  Visibility<[ClangOption, CC1Option]>,
+  HelpText<"Specify atomic codegen options as a comma-separated list of "
+           "key:value pairs, allowed keys and values are "
+           "no_fine_grained_memory:on|off, no_remote_memory:on|off, "
+           "ignore_denormal_mode:on|off">,
+  MarshallingInfoStringVector<LangOpts<"AtomicOptionsAsWritten">>;
+
 defm memory_profile : OptInCC1FFlag<"memory-profile", "Enable", "Disable", " heap memory profiling">;
 def fmemory_profile_EQ : Joined<["-"], "fmemory-profile=">,
     Group<f_Group>, Visibility<[ClangOption, CC1Option]>,
diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h
index 39c5f588167ede..ec86ecc2e2cdb2 100644
--- a/clang/include/clang/Parse/Parser.h
+++ b/clang/include/clang/Parse/Parser.h
@@ -213,6 +213,7 @@ class Parser : public CodeCompletionHandler {
   std::unique_ptr<PragmaHandler> UnrollAndJamHintHandler;
   std::unique_ptr<PragmaHandler> NoUnrollAndJamHintHandler;
   std::unique_ptr<PragmaHandler> FPHandler;
+  std::unique_ptr<PragmaHandler> AtomicHandler;
   std::unique_ptr<PragmaHandler> STDCFenvAccessHandler;
   std::unique_ptr<PragmaHandler> STDCFenvRoundHandler;
   std::unique_ptr<PragmaHandler> STDCCXLIMITHandler;
@@ -837,6 +838,10 @@ class Parser : public CodeCompletionHandler {
   /// #pragma clang fp ...
   void HandlePragmaFP();
 
+  /// \brief Handle the annotation token produced for
+  /// #pragma clang atomic ...
+  void HandlePragmaAtomic();
+
   /// Handle the annotation token produced for
   /// #pragma OPENCL EXTENSION...
   void HandlePragmaOpenCLExtension();
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index b7bd6c2433efd6..0c37ba7549e915 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -1738,6 +1738,23 @@ class Sema final : public SemaBase {
     return result;
   }
 
+  // This stack tracks the current state of Sema.CurAtomicFeatures.
+  PragmaStack<AtomicOptionsOverride> AtomicPragmaStack;
+
+  AtomicOptionsOverride getCurAtomicOptionsOverrides() {
+    AtomicOptionsOverride Result;
+    if (!AtomicPragmaStack.hasValue()) {
+      Result = AtomicOptionsOverride();
+    } else {
+      Result = AtomicPragmaStack.CurrentValue;
+    }
+    return Result;
+  }
+
+  void setCurAtomicOptionsOverrides(AtomicOptionsOverride AO) {
+    AtomicPragmaStac...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/102569


More information about the cfe-commits mailing list