Author: Michael Liao
Date: 2026-03-27T02:26:25-04:00
New Revision: 3d6c0a683c30d2d4014b13549c944e9143c48df7

URL: 
https://github.com/llvm/llvm-project/commit/3d6c0a683c30d2d4014b13549c944e9143c48df7
DIFF: 
https://github.com/llvm/llvm-project/commit/3d6c0a683c30d2d4014b13549c944e9143c48df7.diff

LOG: [CIR] Match codegen change on global temporary

- Match codgen chagne from cbe9891b44d3d1c15bd8f632d6d84e486751e530

Added: 
    

Modified: 
    clang/lib/CIR/CodeGen/CIRGenModule.cpp
    clang/test/CIR/CodeGenCXX/global-refs.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp 
b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
index b4f27595cc1bf..de68927089873 100644
--- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
@@ -3245,10 +3245,15 @@ CIRGenModule::getAddrOfGlobalTemporary(const 
MaterializeTemporaryExpr *mte,
          "not a global temporary");
   const auto *varDecl = cast<VarDecl>(mte->getExtendingDecl());
 
-  // If we're not materializing a subobject of the temporary, keep the
-  // cv-qualifiers from the type of the MaterializeTemporaryExpr.
+  // Use the MaterializeTemporaryExpr's type if it has the same unqualified
+  // base type as Init. This preserves cv-qualifiers (e.g. const from a
+  // constexpr or const-ref binding) that skipRValueSubobjectAdjustments may
+  // have dropped via NoOp casts, while correctly falling back to Init's type
+  // when a real subobject adjustment changed the type (e.g. member access or
+  // base-class cast in C++98), where E->getType() reflects the reference type,
+  // not the actual storage type.
   QualType materializedType = init->getType();
-  if (init == mte->getSubExpr())
+  if (getASTContext().hasSameUnqualifiedType(mte->getType(), materializedType))
     materializedType = mte->getType();
 
   CharUnits align = getASTContext().getTypeAlignInChars(materializedType);

diff  --git a/clang/test/CIR/CodeGenCXX/global-refs.cpp 
b/clang/test/CIR/CodeGenCXX/global-refs.cpp
index 8749bb3019224..50dd9432f1ef2 100644
--- a/clang/test/CIR/CodeGenCXX/global-refs.cpp
+++ b/clang/test/CIR/CodeGenCXX/global-refs.cpp
@@ -26,9 +26,9 @@ int &globalIntRef = globalInt;
 // LLVM: @globalIntRef = constant ptr @globalInt, align 8
 
 const int &constGlobalIntRef = 5;
-// CIR: cir.global "private" external @_ZGR17constGlobalIntRef_ = #cir.int<5> 
: !s32i {alignment = 4 : i64}
+// CIR: cir.global "private" constant external @_ZGR17constGlobalIntRef_ = 
#cir.int<5> : !s32i {alignment = 4 : i64}
 // CIR: cir.global constant external @constGlobalIntRef = 
#cir.global_view<@_ZGR17constGlobalIntRef_> : !cir.ptr<!s32i> {alignment = 8 : 
i64}
-// LLVM: @_ZGR17constGlobalIntRef_ = {{.*}}global i32 5, align 4
+// LLVM: @_ZGR17constGlobalIntRef_ = {{.*}}constant i32 5, align 4
 // LLVM: @constGlobalIntRef = constant ptr @_ZGR17constGlobalIntRef_, align 8
 
 DefCtor defCtor{};


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to