[clang] 1395cde - Fix codegen for initialization of global atomics

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 25 08:37:15 PDT 2023


Author: Aaron Ballman
Date: 2023-04-25T11:37:06-04:00
New Revision: 1395cde24b3641e284bb1daae7d56c189a2635e3

URL: https://github.com/llvm/llvm-project/commit/1395cde24b3641e284bb1daae7d56c189a2635e3
DIFF: https://github.com/llvm/llvm-project/commit/1395cde24b3641e284bb1daae7d56c189a2635e3.diff

LOG: Fix codegen for initialization of global atomics

This amends 2e275e24355cb224981f9beb2b026a3169fc7232. That commit added
a null to pointer cast kind when determining whether the expression can
be a valid constant initializer, but failed to update the constant
expression evaluator to perform the evaluation. This commit updates the
constant expression evaluator to handle that cast kind.

Added: 
    

Modified: 
    clang/lib/AST/ExprConstant.cpp
    clang/test/CodeGen/atomic.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 6bfb3a37b243..49572b0f5869 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -14839,6 +14839,7 @@ class AtomicExprEvaluator :
     switch (E->getCastKind()) {
     default:
       return ExprEvaluatorBaseTy::VisitCastExpr(E);
+    case CK_NullToPointer:
     case CK_NonAtomicToAtomic:
       return This ? EvaluateInPlace(Result, Info, *This, E->getSubExpr())
                   : Evaluate(Result, Info, E->getSubExpr());

diff  --git a/clang/test/CodeGen/atomic.c b/clang/test/CodeGen/atomic.c
index f232d5b63ac7..69f06eedb7ca 100644
--- a/clang/test/CodeGen/atomic.c
+++ b/clang/test/CodeGen/atomic.c
@@ -1,5 +1,9 @@
 // RUN: %clang_cc1 %s -emit-llvm -o - -triple=i686-apple-darwin9 | FileCheck %s
 
+// CHECK: @[[GLOB_POINTER:.+]] = internal global ptr null
+// CHECK: @[[GLOB_INT:.+]] = internal global i32 0
+// CHECK: @[[GLOB_FLT:.+]] = internal global float {{[0e\+-\.]+}}, align
+
 int atomic(void) {
   // non-sensical test for sync functions
   int old;
@@ -118,3 +122,19 @@ void addrspace(int  __attribute__((address_space(256))) * P) {
   __sync_xor_and_fetch(P, 123);
   // CHECK: atomicrmw xor ptr addrspace(256){{.*}}, i32 123 seq_cst, align 4
 }
+
+// Ensure that global initialization of atomics is correct.
+static _Atomic(int *) glob_pointer = (void *)0;
+static _Atomic int glob_int = 0;
+static _Atomic float glob_flt = 0.0f;
+
+void force_global_uses(void) {
+  (void)glob_pointer;
+  // CHECK: %[[LOCAL_INT:.+]] = load atomic i32, ptr @[[GLOB_POINTER]] seq_cst
+  // CHECK-NEXT: inttoptr i32 %[[LOCAL_INT]] to ptr
+  (void)glob_int;
+  // CHECK: load atomic i32, ptr @[[GLOB_INT]] seq_cst
+  (void)glob_flt;
+  // CHECK: %[[LOCAL_FLT:.+]] = load atomic i32, ptr @[[GLOB_FLT]] seq_cst
+  // CHECK-NEXT: bitcast i32 %[[LOCAL_FLT]] to float
+}


        


More information about the cfe-commits mailing list