[clang] [clang][bytecode] Use `ConstantExpr` result for non-primitive APValues (PR #231886)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 14 22:45:18 PDT 2026


https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/231886

>From a9d100add4b37df1a51de28d8c81a3a793ac3720 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Wed, 14 Oct 2026 13:12:46 +0200
Subject: [PATCH] asdf

---
 clang/lib/AST/ByteCode/Compiler.cpp           | 57 +++++++++++++++++--
 clang/test/AST/ByteCode/cxx20.cpp             | 13 +++++
 .../AST/static-compound-literals-reeval.cpp   |  1 +
 clang/test/SemaCXX/source_location.cpp        | 12 ++--
 4 files changed, 72 insertions(+), 11 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 6ba5099a78b877..1875eade836734 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -2786,17 +2786,25 @@ bool Compiler<Emitter>::VisitConstantExpr(const ConstantExpr *E) {
   if (!E->hasAPValueResult())
     return this->delegate(E->getSubExpr());
 
+  // Emitting an APValue never has side effects.
+  if (DiscardResult)
+    return true;
+
   if (OptPrimType T = classify(E)) {
     // Try to emit the APValue directly, without visiting the subexpr.
     // This will only fail if we can't emit the APValue, so won't emit any
     // diagnostics or any double values.
-    if (DiscardResult)
-      return true;
     return this->visitAPValue(E->getAPValueResult(), *T, E);
   }
 
-  // Fall back to the subexpr for non-primitive APValues.
-  return this->delegate(E->getSubExpr());
+  if (!Initializing) {
+    UnsignedOrNone LocalIndex = allocateLocal(E);
+    if (!LocalIndex)
+      return false;
+    if (!this->emitGetPtrLocal(*LocalIndex, E))
+      return false;
+  }
+  return this->visitAPValueInitializer(E->getAPValueResult(), E, E->getType());
 }
 
 template <class Emitter>
@@ -6234,8 +6242,47 @@ bool Compiler<Emitter>::visitAPValueInitializer(const APValue &Val,
     }
     return true;
   }
-  // TODO: Other types.
+  if (Val.isComplexFloat()) {
+    assert(T->isComplexType());
+    if (!this->emitFloat(Val.getComplexFloatReal(), Info))
+      return false;
+    if (!this->emitInitElemFloat(0, Info))
+      return false;
+    if (!this->emitFloat(Val.getComplexFloatImag(), Info))
+      return false;
+    if (!this->emitInitElemFloat(1, Info))
+      return false;
+    return true;
+  }
+  if (Val.isComplexInt()) {
+    assert(T->isComplexIntegerType());
+    PrimType IntT = classifyPrim(T->castAs<ComplexType>()->getElementType());
+    if (!this->emitConst(Val.getComplexIntReal(), IntT, Info))
+      return false;
+    if (!this->emitInitElem(IntT, 0, Info))
+      return false;
+    if (!this->emitConst(Val.getComplexIntImag(), IntT, Info))
+      return false;
+    if (!this->emitInitElem(IntT, 1, Info))
+      return false;
+    return true;
+  }
+  if (Val.isVector()) {
+    assert(T->isVectorType());
+    PrimType ElemT = classifyPrim(T->castAs<VectorType>()->getElementType());
+    for (unsigned I = 0, N = Val.getVectorLength(); I != N; ++I) {
+      const APValue &Elem = Val.getVectorElt(I);
+      if (Elem.isIndeterminate())
+        continue;
+      if (!this->visitAPValue(Elem, ElemT, Info))
+        return false;
+      if (!this->emitInitElem(ElemT, I, Info))
+        return false;
+    }
+    return true;
+  }
 
+  // TODO: Other types.
   return false;
 }
 
diff --git a/clang/test/AST/ByteCode/cxx20.cpp b/clang/test/AST/ByteCode/cxx20.cpp
index 47d2fad17f4dc2..d9200d18898d13 100644
--- a/clang/test/AST/ByteCode/cxx20.cpp
+++ b/clang/test/AST/ByteCode/cxx20.cpp
@@ -1564,3 +1564,16 @@ namespace FailInTrivialCopy {
   static_assert(foo(), ""); // both-error {{not an integral constant expression}} \
                             // both-note {{in call to}}
 }
+
+namespace DiscardedConstevalStruct {
+  struct S { int x; };
+  consteval S make() { return S{42}; }
+
+  void discard_cast() {
+    (void)make();
+  }
+
+  int discard_comma() {
+    return (make(), 0); // both-warning {{left operand of comma operator has no effect}}
+  }
+}
diff --git a/clang/test/AST/static-compound-literals-reeval.cpp b/clang/test/AST/static-compound-literals-reeval.cpp
index 29e21798b9295c..07c92a785264b6 100644
--- a/clang/test/AST/static-compound-literals-reeval.cpp
+++ b/clang/test/AST/static-compound-literals-reeval.cpp
@@ -1,5 +1,6 @@
 // Test that we can successfully compile this code, especially under ASAN.
 // RUN: %clang_cc1 -emit-llvm -std=c++20 %s -o- | FileCheck %s
+// RUN: %clang_cc1 -emit-llvm -std=c++20 %s -o- -fexperimental-new-constant-interpreter | FileCheck %s
 struct RR { int r; };
 struct Z { int x; const RR* y; int z; };
 constinit Z z = { 10, (const RR[1]){__builtin_constant_p(z.x)}, z.y->r };
diff --git a/clang/test/SemaCXX/source_location.cpp b/clang/test/SemaCXX/source_location.cpp
index 8049430738654c..663eece399d1f7 100644
--- a/clang/test/SemaCXX/source_location.cpp
+++ b/clang/test/SemaCXX/source_location.cpp
@@ -4,11 +4,11 @@
 // RUN: %clang_cc1 -std=c++1z -fcxx-exceptions -fms-extensions -DMS -fexceptions -fms-compatibility -verify %s
 // RUN: %clang_cc1 -std=c++2a -fcxx-exceptions -fms-extensions -DMS -DUSE_CONSTEVAL -fexceptions -fms-compatibility -verify %s
 //
-// RUN: %clang_cc1 -std=c++1z -fcxx-exceptions -fexceptions -fexperimental-new-constant-interpreter -DNEW_INTERP -verify %s
-// RUN: %clang_cc1 -std=c++2a -fcxx-exceptions -DUSE_CONSTEVAL -fexceptions -fexperimental-new-constant-interpreter -DNEW_INTERP -verify %s
-// RUN: %clang_cc1 -std=c++2b -fcxx-exceptions -DUSE_CONSTEVAL -DPAREN_INIT -fexceptions -fexperimental-new-constant-interpreter -DNEW_INTERP -verify %s
-// RUN: %clang_cc1 -std=c++1z -fcxx-exceptions -fms-extensions -DMS -fexceptions -fexperimental-new-constant-interpreter -DNEW_INTERP -fms-compatibility -verify %s
-// RUN: %clang_cc1 -std=c++2a -fcxx-exceptions -fms-extensions -DMS -DUSE_CONSTEVAL -fexceptions -fexperimental-new-constant-interpreter -DNEW_INTERP -verify -fms-compatibility %s
+// RUN: %clang_cc1 -std=c++1z -fcxx-exceptions -fexceptions -fexperimental-new-constant-interpreter -verify %s
+// RUN: %clang_cc1 -std=c++2a -fcxx-exceptions -DUSE_CONSTEVAL -fexceptions -fexperimental-new-constant-interpreter -verify %s
+// RUN: %clang_cc1 -std=c++2b -fcxx-exceptions -DUSE_CONSTEVAL -DPAREN_INIT -fexceptions -fexperimental-new-constant-interpreter -verify %s
+// RUN: %clang_cc1 -std=c++1z -fcxx-exceptions -fms-extensions -DMS -fexceptions -fexperimental-new-constant-interpreter -fms-compatibility -verify %s
+// RUN: %clang_cc1 -std=c++2a -fcxx-exceptions -fms-extensions -DMS -DUSE_CONSTEVAL -fexceptions -fexperimental-new-constant-interpreter -verify -fms-compatibility %s
 // expected-no-diagnostics
 
 #define assert(...) ((__VA_ARGS__) ? ((void)0) : throw 42)
@@ -874,7 +874,7 @@ constexpr int test_init_capture(int a =
                 [b = SL::current().line()] { return b; }()) {
   return a;
 }
-#if defined(USE_CONSTEVAL) && !defined(NEW_INTERP)
+#if defined(USE_CONSTEVAL)
 static_assert(test_init_capture() == __LINE__ - 4);
 #else
 static_assert(test_init_capture() == __LINE__ );



More information about the cfe-commits mailing list