[clang] [CIR] Initialization of global atomic variables (PR #194239)

Sirui Mu via cfe-commits cfe-commits at lists.llvm.org
Sun Apr 26 08:11:12 PDT 2026


https://github.com/Lancern created https://github.com/llvm/llvm-project/pull/194239

This patch adds support for initializing global atomic variables. It does not include support for tail paddings, which will be added in later patches.

Assisted-by: Codex / GPT-5.5

>From af2951eea8c3ca9bd92546c42279f8ac719d27dd Mon Sep 17 00:00:00 2001
From: Sirui Mu <msrlancern at gmail.com>
Date: Sun, 26 Apr 2026 23:09:40 +0800
Subject: [PATCH] [CIR] Initialization of global atomic variables

This patch adds support for initializing global atomic variables. It does not
include support for tail paddings, which will be added in later patches.

Assisted-by: Codex / GPT-5.5
---
 clang/include/clang/CIR/MissingFeatures.h    |  1 +
 clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp | 21 +++++++++++---------
 clang/test/CIR/CodeGen/atomic.c              | 11 ++++++++++
 3 files changed, 24 insertions(+), 9 deletions(-)

diff --git a/clang/include/clang/CIR/MissingFeatures.h b/clang/include/clang/CIR/MissingFeatures.h
index 634d4dd83b3f8..44e750129a070 100644
--- a/clang/include/clang/CIR/MissingFeatures.h
+++ b/clang/include/clang/CIR/MissingFeatures.h
@@ -173,6 +173,7 @@ struct MissingFeatures {
   static bool atomicUseLibCall() { return false; }
   static bool atomicMicrosoftVolatile() { return false; }
   static bool atomicOpenMP() { return false; }
+  static bool atomicInitTailPadding() { return false; }
 
   // Global ctor handling
   static bool globalCtorLexOrder() { return false; }
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp b/clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp
index a8b24fb686088..98b4787d74ee3 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp
@@ -1739,21 +1739,24 @@ mlir::Attribute ConstantEmitter::emitNullForMemory(mlir::Location loc,
 
 mlir::Attribute ConstantEmitter::emitForMemory(mlir::Attribute c,
                                                QualType destType) {
-  // For an _Atomic-qualified constant, we may need to add tail padding.
-  if (destType->getAs<AtomicType>()) {
-    cgm.errorNYI("emitForMemory: atomic type");
-    return {};
-  }
-
-  return c;
+  return emitForMemory(cgm, c, destType);
 }
 
 mlir::Attribute ConstantEmitter::emitForMemory(CIRGenModule &cgm,
                                                mlir::Attribute c,
                                                QualType destType) {
   // For an _Atomic-qualified constant, we may need to add tail padding.
-  if (destType->getAs<AtomicType>()) {
-    cgm.errorNYI("atomic constants");
+  if (const auto *at = destType->getAs<AtomicType>()) {
+    QualType destValueType = at->getValueType();
+    c = emitForMemory(cgm, c, destValueType);
+
+    uint64_t innerSize = cgm.getASTContext().getTypeSize(destValueType);
+    uint64_t outerSize = cgm.getASTContext().getTypeSize(destType);
+    if (innerSize == outerSize)
+      return c;
+
+    assert(innerSize < outerSize && "emitted over-large constant for atomic");
+    cgm.errorNYI("emitForMemory: tail padding in atomic initializer");
   }
 
   return c;
diff --git a/clang/test/CIR/CodeGen/atomic.c b/clang/test/CIR/CodeGen/atomic.c
index abce1166cf9a2..e6e45e787a37c 100644
--- a/clang/test/CIR/CodeGen/atomic.c
+++ b/clang/test/CIR/CodeGen/atomic.c
@@ -5,6 +5,17 @@
 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value -emit-llvm %s -o %t.ll
 // RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG
 
+_Atomic int g1;
+_Atomic int g2 = 42;
+// CIR: cir.global external @g2 = #cir.int<42> : !s32i {alignment = 4 : i64}
+// CIR: cir.global external @g1 = #cir.int<0> : !s32i {alignment = 4 : i64}
+
+// LLVM: @g2 = global i32 42, align 4
+// LLVM: @g1 = global i32 0, align 4
+
+// OGCG: @g2 = global i32 42, align 4
+// OGCG: @g1 = global i32 0, align 4
+
 void f1(void) {
   _Atomic(int) x = 42;
 }



More information about the cfe-commits mailing list