[clang] [clang][bytecode] Use `ConstantExpr` result for non-primitive APValues (PR #231886)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Oct 14 05:20:42 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
Create a local variable and initialize it from the `APValue`.
---
Full diff: https://github.com/llvm/llvm-project/pull/231886.diff
2 Files Affected:
- (modified) clang/lib/AST/ByteCode/Compiler.cpp (+46-3)
- (modified) clang/test/AST/static-compound-literals-reeval.cpp (+1)
``````````diff
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 6ba5099a78b877..a37c606bec8b6f 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -2795,8 +2795,15 @@ bool Compiler<Emitter>::VisitConstantExpr(const ConstantExpr *E) {
return this->visitAPValue(E->getAPValueResult(), *T, E);
}
- // Fall back to the subexpr for non-primitive APValues.
- return this->delegate(E->getSubExpr());
+ assert(!DiscardResult);
+ 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 +6241,44 @@ 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->getAs<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 IntT = classifyPrim(T->getAs<VectorType>()->getElementType());
+ for (unsigned I = 0, N = Val.getVectorLength(); I != N; ++I) {
+ if (!this->visitAPValue(Val.getVectorElt(I), IntT, Info))
+ return false;
+ if (!this->emitInitElem(IntT, I, Info))
+ return false;
+ }
+ return true;
+ }
+ // TODO: Other types.
return false;
}
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 };
``````````
</details>
https://github.com/llvm/llvm-project/pull/231886
More information about the cfe-commits
mailing list