Skip to content

[clang][bytecode] Avoid classifying in visitArrayElemInit() #139674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,11 +570,11 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
return false;
}

PrimType T = classifyPrim(SubExpr->getType());
// Init the complex value to {SubExpr, 0}.
if (!this->visitArrayElemInit(0, SubExpr))
if (!this->visitArrayElemInit(0, SubExpr, T))
return false;
// Zero-init the second element.
PrimType T = classifyPrim(SubExpr->getType());
if (!this->visitZeroInitializer(T, SubExpr->getType(), SubExpr))
return false;
return this->emitInitElem(T, 1, SubExpr);
Expand Down Expand Up @@ -772,7 +772,7 @@ bool Compiler<Emitter>::VisitImaginaryLiteral(const ImaginaryLiteral *E) {
return false;
if (!this->emitInitElem(SubExprT, 0, SubExpr))
return false;
return this->visitArrayElemInit(1, SubExpr);
return this->visitArrayElemInit(1, SubExpr, SubExprT);
}

template <class Emitter>
Expand Down Expand Up @@ -1886,6 +1886,7 @@ bool Compiler<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
if (!this->emitCheckArraySize(NumElems, E))
return false;

std::optional<PrimType> InitT = classify(CAT->getElementType());
unsigned ElementIndex = 0;
for (const Expr *Init : Inits) {
if (const auto *EmbedS =
Expand All @@ -1905,7 +1906,7 @@ bool Compiler<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
if (!EmbedS->doForEachDataElement(Eval, ElementIndex))
return false;
} else {
if (!this->visitArrayElemInit(ElementIndex, Init))
if (!this->visitArrayElemInit(ElementIndex, Init, InitT))
return false;
++ElementIndex;
}
Expand All @@ -1915,7 +1916,7 @@ bool Compiler<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
// FIXME: This should go away.
if (ArrayFiller) {
for (; ElementIndex != NumElems; ++ElementIndex) {
if (!this->visitArrayElemInit(ElementIndex, ArrayFiller))
if (!this->visitArrayElemInit(ElementIndex, ArrayFiller, InitT))
return false;
}
}
Expand Down Expand Up @@ -1998,13 +1999,13 @@ bool Compiler<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
/// Pointer to the array(not the element!) must be on the stack when calling
/// this.
template <class Emitter>
bool Compiler<Emitter>::visitArrayElemInit(unsigned ElemIndex,
const Expr *Init) {
if (std::optional<PrimType> T = classify(Init->getType())) {
bool Compiler<Emitter>::visitArrayElemInit(unsigned ElemIndex, const Expr *Init,
std::optional<PrimType> InitT) {
if (InitT) {
// Visit the primitive element like normal.
if (!this->visit(Init))
return false;
return this->emitInitElem(*T, ElemIndex, Init);
return this->emitInitElem(*InitT, ElemIndex, Init);
}

InitLinkScope<Emitter> ILS(this, InitLink::Elem(ElemIndex));
Expand Down Expand Up @@ -2298,6 +2299,7 @@ bool Compiler<Emitter>::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E) {
// Investigate compiling this to a loop.
const Expr *SubExpr = E->getSubExpr();
size_t Size = E->getArraySize().getZExtValue();
std::optional<PrimType> SubExprT = classify(SubExpr);

// So, every iteration, we execute an assignment here
// where the LHS is on the stack (the target array)
Expand All @@ -2306,7 +2308,7 @@ bool Compiler<Emitter>::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E) {
ArrayIndexScope<Emitter> IndexScope(this, I);
BlockScope<Emitter> BS(this);

if (!this->visitArrayElemInit(I, SubExpr))
if (!this->visitArrayElemInit(I, SubExpr, SubExprT))
return false;
if (!BS.destroyLocals())
return false;
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/AST/ByteCode/Compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,8 @@ class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,

bool visitInitList(ArrayRef<const Expr *> Inits, const Expr *ArrayFiller,
const Expr *E);
bool visitArrayElemInit(unsigned ElemIndex, const Expr *Init);
bool visitArrayElemInit(unsigned ElemIndex, const Expr *Init,
std::optional<PrimType> InitT);

/// Creates a local primitive value.
unsigned allocateLocalPrimitive(DeclTy &&Decl, PrimType Ty, bool IsConst,
Expand Down