Author: Timm Baeder Date: 2026-10-15T10:55:51+02:00 New Revision: 493c91d3e25cda8809a18041aa44d57c3ddd499e
URL: https://github.com/llvm/llvm-project/commit/493c91d3e25cda8809a18041aa44d57c3ddd499e DIFF: https://github.com/llvm/llvm-project/commit/493c91d3e25cda8809a18041aa44d57c3ddd499e.diff LOG: [clang][bytecode] Use `ConstantExpr` result for non-primitive APValues (#231886) Create a local variable and initialize it from the `APValue`. Added: Modified: clang/lib/AST/ByteCode/Compiler.cpp clang/test/AST/ByteCode/cxx20.cpp clang/test/AST/static-compound-literals-reeval.cpp clang/test/SemaCXX/source_location.cpp Removed: ################################################################################ diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp index 6ba5099a78b87..1875eade83673 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 47d2fad17f4dc..d9200d18898d1 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 29e21798b9295..07c92a785264b 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 8049430738654..663eece399d1f 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__ ); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
