[clang] 4a2757d - Make globals used for array initialization codegen constant

Hans Wennborg via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 17 01:28:06 PDT 2023


Author: Hans Wennborg
Date: 2023-03-17T09:23:34+01:00
New Revision: 4a2757d80f0af48e65d90e7eaf268f78bcfa997f

URL: https://github.com/llvm/llvm-project/commit/4a2757d80f0af48e65d90e7eaf268f78bcfa997f
DIFF: https://github.com/llvm/llvm-project/commit/4a2757d80f0af48e65d90e7eaf268f78bcfa997f.diff

LOG: Make globals used for array initialization codegen constant

As pointed out in D133835 these globals will never be written to
(they're only used for trivially copyable types), so they can always be
constant.

Differential revision: https://reviews.llvm.org/D146211

Added: 
    

Modified: 
    clang/lib/CodeGen/CGExprAgg.cpp
    clang/test/CodeGen/init.c
    clang/test/CodeGen/label-array-aggregate-init.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index 983c350129f86..33d2295fa98ee 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -532,9 +532,8 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType,
             Emitter.tryEmitForInitializer(ExprToVisit, AS, ArrayQTy)) {
       auto GV = new llvm::GlobalVariable(
           CGM.getModule(), C->getType(),
-          CGM.isTypeConstant(ArrayQTy, /* ExcludeCtor= */ true,
-                             /* ExcludeDtor= */ false),
-          llvm::GlobalValue::PrivateLinkage, C, "constinit",
+          /* isConstant= */ true, llvm::GlobalValue::PrivateLinkage, C,
+          "constinit",
           /* InsertBefore= */ nullptr, llvm::GlobalVariable::NotThreadLocal,
           CGM.getContext().getTargetAddressSpace(AS));
       Emitter.finalize(GV);

diff  --git a/clang/test/CodeGen/init.c b/clang/test/CodeGen/init.c
index 0a2d739e1a80d..5ebd6853733f8 100644
--- a/clang/test/CodeGen/init.c
+++ b/clang/test/CodeGen/init.c
@@ -10,7 +10,7 @@ unsigned v2[2][3] = {[0 ... 1][0 ... 1] = 2222, 3333};
 
 // CHECK-DAG: [1 x %struct.M] [%struct.M { [2 x %struct.I] [%struct.I { [3 x i32] [i32 4, i32 4, i32 0] }, %struct.I { [3 x i32] [i32 4, i32 4, i32 5] }] }],
 // CHECK-DAG: [2 x [3 x i32]] {{[[][[]}}3 x i32] [i32 2222, i32 2222, i32 0], [3 x i32] [i32 2222, i32 2222, i32 3333]],
-// CHECK-DAG: [[INIT14:.*]] = private global [16 x i32] [i32 0, i32 0, i32 0, i32 0, i32 0, i32 17, i32 17, i32 17, i32 17, i32 17, i32 17, i32 17, i32 0, i32 0, i32 0, i32 0], align 4
+// CHECK-DAG: [[INIT14:.*]] = private constant [16 x i32] [i32 0, i32 0, i32 0, i32 0, i32 0, i32 17, i32 17, i32 17, i32 17, i32 17, i32 17, i32 17, i32 0, i32 0, i32 0, i32 0], align 4
 
 void f1(void) {
   // Scalars in braces.

diff  --git a/clang/test/CodeGen/label-array-aggregate-init.c b/clang/test/CodeGen/label-array-aggregate-init.c
index 00a20d5d204aa..4039b925f21b7 100644
--- a/clang/test/CodeGen/label-array-aggregate-init.c
+++ b/clang/test/CodeGen/label-array-aggregate-init.c
@@ -1,6 +1,6 @@
 // RUN: %clang_cc1 -triple x86_64-windows-msvc -emit-llvm %s -o - | FileCheck %s
 
-// CHECK: @constinit = private global [3 x ptr] [ptr blockaddress(@main, %L), ptr null, ptr null]
+// CHECK: @constinit = private constant [3 x ptr] [ptr blockaddress(@main, %L), ptr null, ptr null]
 
 void receivePtrs(void **);
 


        


More information about the cfe-commits mailing list