[clang] [clang][bytecode] Handle discarded OpaqueValueExpr (PR #187982)

via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 23 00:01:02 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

<details>
<summary>Changes</summary>

... if they are already cached.

---
Full diff: https://github.com/llvm/llvm-project/pull/187982.diff


2 Files Affected:

- (modified) clang/lib/AST/ByteCode/Compiler.cpp (+12-9) 
- (modified) clang/test/AST/ByteCode/arrays.cpp (+12) 


``````````diff
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index a881d77a73cbd..5e029643fb0d4 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -2709,6 +2709,7 @@ bool Compiler<Emitter>::VisitMemberExpr(const MemberExpr *E) {
 
 template <class Emitter>
 bool Compiler<Emitter>::VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E) {
+  assert(!DiscardResult);
   // ArrayIndex might not be set if a ArrayInitIndexExpr is being evaluated
   // stand-alone, e.g. via EvaluateAsInt().
   if (!ArrayIndex)
@@ -2753,12 +2754,17 @@ bool Compiler<Emitter>::VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
   if (!SourceExpr)
     return false;
 
-  if (Initializing)
+  if (Initializing) {
+    assert(!DiscardResult);
     return this->visitInitializer(SourceExpr);
+  }
 
   PrimType SubExprT = classify(SourceExpr).value_or(PT_Ptr);
-  if (auto It = OpaqueExprs.find(E); It != OpaqueExprs.end())
+  if (auto It = OpaqueExprs.find(E); It != OpaqueExprs.end()) {
+    if (DiscardResult)
+      return true;
     return this->emitGetLocal(SubExprT, It->second, E);
+  }
 
   if (!this->visit(SourceExpr))
     return false;
@@ -2770,16 +2776,13 @@ bool Compiler<Emitter>::VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
   if (!this->emitSetLocal(SubExprT, LocalIndex, E))
     return false;
 
-  // Here the local variable is created but the value is removed from the stack,
-  // so we put it back if the caller needs it.
-  if (!DiscardResult) {
-    if (!this->emitGetLocal(SubExprT, LocalIndex, E))
-      return false;
-  }
-
   // This is cleaned up when the local variable is destroyed.
   OpaqueExprs.insert({E, LocalIndex});
 
+  // Here the local variable is created but the value is removed from the stack,
+  // so we put it back if the caller needs it.
+  if (!DiscardResult)
+    return this->emitGetLocal(SubExprT, LocalIndex, E);
   return true;
 }
 
diff --git a/clang/test/AST/ByteCode/arrays.cpp b/clang/test/AST/ByteCode/arrays.cpp
index d83ae97fc8213..cb5b7ad99070e 100644
--- a/clang/test/AST/ByteCode/arrays.cpp
+++ b/clang/test/AST/ByteCode/arrays.cpp
@@ -835,3 +835,15 @@ namespace MultiDimConstructExpr {
   constexpr b d;
   static_assert(d.m[2][1].p == &d.m[2][1]);
 }
+
+namespace MultiDimArrayInitLoop {
+  struct S {
+    float x[2][2];
+  };
+  struct T {
+    S y;
+  };
+
+  constexpr S s = {1};
+  constexpr T t = {s};
+}

``````````

</details>


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


More information about the cfe-commits mailing list