Skip to content

Commit 97ed201

Browse files
authored
[clang][bytecode] Use ExtendingDecl mechanism for primitives as well (#128141)
... when creating the temporary variables for a MaterializeTemporaryExpr.
1 parent af64f0a commit 97ed201

File tree

3 files changed

+319
-43
lines changed

3 files changed

+319
-43
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -567,8 +567,8 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
567567
// Location for the SubExpr.
568568
// Since SubExpr is of complex type, visiting it results in a pointer
569569
// anyway, so we just create a temporary pointer variable.
570-
unsigned SubExprOffset = allocateLocalPrimitive(
571-
SubExpr, PT_Ptr, /*IsConst=*/true, /*IsExtended=*/false);
570+
unsigned SubExprOffset =
571+
allocateLocalPrimitive(SubExpr, PT_Ptr, /*IsConst=*/true);
572572
if (!this->visit(SubExpr))
573573
return false;
574574
if (!this->emitSetLocal(PT_Ptr, SubExprOffset, CE))
@@ -611,8 +611,8 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
611611

612612
const auto *VT = CE->getType()->getAs<VectorType>();
613613
PrimType ElemT = classifyPrim(SubExpr->getType());
614-
unsigned ElemOffset = allocateLocalPrimitive(
615-
SubExpr, ElemT, /*IsConst=*/true, /*IsExtended=*/false);
614+
unsigned ElemOffset =
615+
allocateLocalPrimitive(SubExpr, ElemT, /*IsConst=*/true);
616616

617617
// Prepare a local variable for the scalar value.
618618
if (!this->visit(SubExpr))
@@ -1104,7 +1104,7 @@ bool Compiler<Emitter>::VisitComplexBinOp(const BinaryOperator *E) {
11041104
PrimType ResultElemT = this->classifyComplexElementType(E->getType());
11051105
unsigned ResultOffset = ~0u;
11061106
if (!DiscardResult)
1107-
ResultOffset = this->allocateLocalPrimitive(E, PT_Ptr, true, false);
1107+
ResultOffset = this->allocateLocalPrimitive(E, PT_Ptr, /*IsConst=*/true);
11081108

11091109
// Save result pointer in ResultOffset
11101110
if (!this->DiscardResult) {
@@ -1178,14 +1178,14 @@ bool Compiler<Emitter>::VisitComplexBinOp(const BinaryOperator *E) {
11781178

11791179
// Evaluate LHS and save value to LHSOffset.
11801180
if (LHSType->isAnyComplexType()) {
1181-
LHSOffset = this->allocateLocalPrimitive(LHS, PT_Ptr, true, false);
1181+
LHSOffset = this->allocateLocalPrimitive(LHS, PT_Ptr, /*IsConst=*/true);
11821182
if (!this->visit(LHS))
11831183
return false;
11841184
if (!this->emitSetLocal(PT_Ptr, LHSOffset, E))
11851185
return false;
11861186
} else {
11871187
PrimType LHST = classifyPrim(LHSType);
1188-
LHSOffset = this->allocateLocalPrimitive(LHS, LHST, true, false);
1188+
LHSOffset = this->allocateLocalPrimitive(LHS, LHST, /*IsConst=*/true);
11891189
if (!this->visit(LHS))
11901190
return false;
11911191
if (!this->emitSetLocal(LHST, LHSOffset, E))
@@ -1195,14 +1195,14 @@ bool Compiler<Emitter>::VisitComplexBinOp(const BinaryOperator *E) {
11951195
// Same with RHS.
11961196
unsigned RHSOffset;
11971197
if (RHSType->isAnyComplexType()) {
1198-
RHSOffset = this->allocateLocalPrimitive(RHS, PT_Ptr, true, false);
1198+
RHSOffset = this->allocateLocalPrimitive(RHS, PT_Ptr, /*IsConst=*/true);
11991199
if (!this->visit(RHS))
12001200
return false;
12011201
if (!this->emitSetLocal(PT_Ptr, RHSOffset, E))
12021202
return false;
12031203
} else {
12041204
PrimType RHST = classifyPrim(RHSType);
1205-
RHSOffset = this->allocateLocalPrimitive(RHS, RHST, true, false);
1205+
RHSOffset = this->allocateLocalPrimitive(RHS, RHST, /*IsConst=*/true);
12061206
if (!this->visit(RHS))
12071207
return false;
12081208
if (!this->emitSetLocal(RHST, RHSOffset, E))
@@ -1342,14 +1342,16 @@ bool Compiler<Emitter>::VisitVectorBinOp(const BinaryOperator *E) {
13421342
PrimType ResultElemT = this->classifyVectorElementType(E->getType());
13431343

13441344
// Evaluate LHS and save value to LHSOffset.
1345-
unsigned LHSOffset = this->allocateLocalPrimitive(LHS, PT_Ptr, true, false);
1345+
unsigned LHSOffset =
1346+
this->allocateLocalPrimitive(LHS, PT_Ptr, /*IsConst=*/true);
13461347
if (!this->visit(LHS))
13471348
return false;
13481349
if (!this->emitSetLocal(PT_Ptr, LHSOffset, E))
13491350
return false;
13501351

13511352
// Evaluate RHS and save value to RHSOffset.
1352-
unsigned RHSOffset = this->allocateLocalPrimitive(RHS, PT_Ptr, true, false);
1353+
unsigned RHSOffset =
1354+
this->allocateLocalPrimitive(RHS, PT_Ptr, /*IsConst=*/true);
13531355
if (!this->visit(RHS))
13541356
return false;
13551357
if (!this->emitSetLocal(PT_Ptr, RHSOffset, E))
@@ -2710,8 +2712,8 @@ bool Compiler<Emitter>::VisitMaterializeTemporaryExpr(
27102712
// For everyhing else, use local variables.
27112713
if (SubExprT) {
27122714
bool IsConst = SubExpr->getType().isConstQualified();
2713-
unsigned LocalIndex = allocateLocalPrimitive(E, *SubExprT, IsConst,
2714-
/*IsExtended=*/true);
2715+
unsigned LocalIndex =
2716+
allocateLocalPrimitive(E, *SubExprT, IsConst, E->getExtendingDecl());
27152717
if (!this->visit(SubExpr))
27162718
return false;
27172719
if (!this->emitSetLocal(*SubExprT, LocalIndex, E))
@@ -2781,7 +2783,7 @@ bool Compiler<Emitter>::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
27812783
unsigned LocalIndex;
27822784

27832785
if (T)
2784-
LocalIndex = this->allocateLocalPrimitive(Init, *T, false, false);
2786+
LocalIndex = this->allocateLocalPrimitive(Init, *T, /*IsConst=*/false);
27852787
else if (std::optional<unsigned> MaybeIndex = this->allocateLocal(Init))
27862788
LocalIndex = *MaybeIndex;
27872789
else
@@ -3337,8 +3339,8 @@ bool Compiler<Emitter>::VisitCXXNewExpr(const CXXNewExpr *E) {
33373339
PrimType SizeT = classifyPrim(Stripped->getType());
33383340

33393341
// Save evaluated array size to a variable.
3340-
unsigned ArrayLen = allocateLocalPrimitive(
3341-
Stripped, SizeT, /*IsConst=*/false, /*IsExtended=*/false);
3342+
unsigned ArrayLen =
3343+
allocateLocalPrimitive(Stripped, SizeT, /*IsConst=*/false);
33423344
if (!this->visit(Stripped))
33433345
return false;
33443346
if (!this->emitSetLocal(SizeT, ArrayLen, E))
@@ -3417,8 +3419,8 @@ bool Compiler<Emitter>::VisitCXXNewExpr(const CXXNewExpr *E) {
34173419
}
34183420

34193421
// Create loop variables.
3420-
unsigned Iter = allocateLocalPrimitive(
3421-
Stripped, SizeT, /*IsConst=*/false, /*IsExtended=*/false);
3422+
unsigned Iter =
3423+
allocateLocalPrimitive(Stripped, SizeT, /*IsConst=*/false);
34223424
if (!this->emitConst(StaticInitElems, SizeT, E))
34233425
return false;
34243426
if (!this->emitSetLocal(SizeT, Iter, E))
@@ -3669,8 +3671,8 @@ template <class Emitter>
36693671
bool Compiler<Emitter>::VisitAddrLabelExpr(const AddrLabelExpr *E) {
36703672
assert(E->getType()->isVoidPointerType());
36713673

3672-
unsigned Offset = allocateLocalPrimitive(
3673-
E->getLabel(), PT_Ptr, /*IsConst=*/true, /*IsExtended=*/false);
3674+
unsigned Offset =
3675+
allocateLocalPrimitive(E->getLabel(), PT_Ptr, /*IsConst=*/true);
36743676

36753677
return this->emitGetLocal(PT_Ptr, Offset, E);
36763678
}
@@ -3685,7 +3687,8 @@ bool Compiler<Emitter>::VisitConvertVectorExpr(const ConvertVectorExpr *E) {
36853687
QualType SrcType = Src->getType();
36863688
PrimType SrcElemT = classifyVectorElementType(SrcType);
36873689

3688-
unsigned SrcOffset = this->allocateLocalPrimitive(Src, PT_Ptr, true, false);
3690+
unsigned SrcOffset =
3691+
this->allocateLocalPrimitive(Src, PT_Ptr, /*IsConst=*/true);
36893692
if (!this->visit(Src))
36903693
return false;
36913694
if (!this->emitSetLocal(PT_Ptr, SrcOffset, E))
@@ -3728,8 +3731,8 @@ bool Compiler<Emitter>::VisitShuffleVectorExpr(const ShuffleVectorExpr *E) {
37283731
// Save both input vectors to a local variable.
37293732
unsigned VectorOffsets[2];
37303733
for (unsigned I = 0; I != 2; ++I) {
3731-
VectorOffsets[I] = this->allocateLocalPrimitive(
3732-
Vecs[I], PT_Ptr, /*IsConst=*/true, /*IsExtended=*/false);
3734+
VectorOffsets[I] =
3735+
this->allocateLocalPrimitive(Vecs[I], PT_Ptr, /*IsConst=*/true);
37333736
if (!this->visit(Vecs[I]))
37343737
return false;
37353738
if (!this->emitSetLocal(PT_Ptr, VectorOffsets[I], E))
@@ -3781,8 +3784,7 @@ bool Compiler<Emitter>::VisitExtVectorElementExpr(
37813784
}
37823785

37833786
// Create a local variable for the base.
3784-
unsigned BaseOffset = allocateLocalPrimitive(Base, PT_Ptr, /*IsConst=*/true,
3785-
/*IsExtended=*/false);
3787+
unsigned BaseOffset = allocateLocalPrimitive(Base, PT_Ptr, /*IsConst=*/true);
37863788
if (!this->visit(Base))
37873789
return false;
37883790
if (!this->emitSetLocal(PT_Ptr, BaseOffset, E))
@@ -4194,9 +4196,8 @@ bool Compiler<Emitter>::emitConst(const APSInt &Value, const Expr *E) {
41944196
}
41954197

41964198
template <class Emitter>
4197-
unsigned Compiler<Emitter>::allocateLocalPrimitive(DeclTy &&Src, PrimType Ty,
4198-
bool IsConst,
4199-
bool IsExtended) {
4199+
unsigned Compiler<Emitter>::allocateLocalPrimitive(
4200+
DeclTy &&Src, PrimType Ty, bool IsConst, const ValueDecl *ExtendingDecl) {
42004201
// Make sure we don't accidentally register the same decl twice.
42014202
if (const auto *VD =
42024203
dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>())) {
@@ -4213,7 +4214,10 @@ unsigned Compiler<Emitter>::allocateLocalPrimitive(DeclTy &&Src, PrimType Ty,
42134214
Scope::Local Local = this->createLocal(D);
42144215
if (auto *VD = dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>()))
42154216
Locals.insert({VD, Local});
4216-
VarScope->add(Local, IsExtended);
4217+
if (ExtendingDecl)
4218+
VarScope->addExtended(Local, ExtendingDecl);
4219+
else
4220+
VarScope->add(Local, false);
42174221
return Local.Offset;
42184222
}
42194223

@@ -4781,7 +4785,7 @@ bool Compiler<Emitter>::VisitCallExpr(const CallExpr *E) {
47814785
// decl as the function pointer.
47824786
const Expr *Callee = E->getCallee();
47834787
CalleeOffset =
4784-
this->allocateLocalPrimitive(Callee, PT_MemberPtr, true, false);
4788+
this->allocateLocalPrimitive(Callee, PT_MemberPtr, /*IsConst=*/true);
47854789
if (!this->visit(Callee))
47864790
return false;
47874791
if (!this->emitSetLocal(PT_MemberPtr, *CalleeOffset, E))
@@ -4803,7 +4807,8 @@ bool Compiler<Emitter>::VisitCallExpr(const CallExpr *E) {
48034807
return this->emitKill(E);
48044808
} else if (!FuncDecl) {
48054809
const Expr *Callee = E->getCallee();
4806-
CalleeOffset = this->allocateLocalPrimitive(Callee, PT_FnPtr, true, false);
4810+
CalleeOffset =
4811+
this->allocateLocalPrimitive(Callee, PT_FnPtr, /*IsConst=*/true);
48074812
if (!this->visit(Callee))
48084813
return false;
48094814
if (!this->emitSetLocal(PT_FnPtr, *CalleeOffset, E))
@@ -5405,7 +5410,8 @@ bool Compiler<Emitter>::visitSwitchStmt(const SwitchStmt *S) {
54055410

54065411
LabelTy EndLabel = this->getLabel();
54075412
OptLabelTy DefaultLabel = std::nullopt;
5408-
unsigned CondVar = this->allocateLocalPrimitive(Cond, CondT, true, false);
5413+
unsigned CondVar =
5414+
this->allocateLocalPrimitive(Cond, CondT, /*IsConst=*/true);
54095415

54105416
if (const auto *CondInit = S->getInit())
54115417
if (!visitStmt(CondInit))
@@ -6068,7 +6074,8 @@ bool Compiler<Emitter>::VisitComplexUnaryOperator(const UnaryOperator *E) {
60686074
// The offset of the temporary, if we created one.
60696075
unsigned SubExprOffset = ~0u;
60706076
auto createTemp = [=, &SubExprOffset]() -> bool {
6071-
SubExprOffset = this->allocateLocalPrimitive(SubExpr, PT_Ptr, true, false);
6077+
SubExprOffset =
6078+
this->allocateLocalPrimitive(SubExpr, PT_Ptr, /*IsConst=*/true);
60726079
if (!this->visit(SubExpr))
60736080
return false;
60746081
return this->emitSetLocal(PT_Ptr, SubExprOffset, E);
@@ -6182,7 +6189,7 @@ bool Compiler<Emitter>::VisitVectorUnaryOperator(const UnaryOperator *E) {
61826189

61836190
// The offset of the temporary, if we created one.
61846191
unsigned SubExprOffset =
6185-
this->allocateLocalPrimitive(SubExpr, PT_Ptr, true, false);
6192+
this->allocateLocalPrimitive(SubExpr, PT_Ptr, /*IsConst=*/true);
61866193
if (!this->visit(SubExpr))
61876194
return false;
61886195
if (!this->emitSetLocal(PT_Ptr, SubExprOffset, E))
@@ -6555,16 +6562,15 @@ bool Compiler<Emitter>::emitComplexComparison(const Expr *LHS, const Expr *RHS,
65556562
if (LHS->getType()->isAnyComplexType()) {
65566563
LHSIsComplex = true;
65576564
ElemT = classifyComplexElementType(LHS->getType());
6558-
LHSOffset = allocateLocalPrimitive(LHS, PT_Ptr, /*IsConst=*/true,
6559-
/*IsExtended=*/false);
6565+
LHSOffset = allocateLocalPrimitive(LHS, PT_Ptr, /*IsConst=*/true);
65606566
if (!this->visit(LHS))
65616567
return false;
65626568
if (!this->emitSetLocal(PT_Ptr, LHSOffset, E))
65636569
return false;
65646570
} else {
65656571
LHSIsComplex = false;
65666572
PrimType LHST = classifyPrim(LHS->getType());
6567-
LHSOffset = this->allocateLocalPrimitive(LHS, LHST, true, false);
6573+
LHSOffset = this->allocateLocalPrimitive(LHS, LHST, /*IsConst=*/true);
65686574
if (!this->visit(LHS))
65696575
return false;
65706576
if (!this->emitSetLocal(LHST, LHSOffset, E))
@@ -6576,16 +6582,15 @@ bool Compiler<Emitter>::emitComplexComparison(const Expr *LHS, const Expr *RHS,
65766582
if (RHS->getType()->isAnyComplexType()) {
65776583
RHSIsComplex = true;
65786584
ElemT = classifyComplexElementType(RHS->getType());
6579-
RHSOffset = allocateLocalPrimitive(RHS, PT_Ptr, /*IsConst=*/true,
6580-
/*IsExtended=*/false);
6585+
RHSOffset = allocateLocalPrimitive(RHS, PT_Ptr, /*IsConst=*/true);
65816586
if (!this->visit(RHS))
65826587
return false;
65836588
if (!this->emitSetLocal(PT_Ptr, RHSOffset, E))
65846589
return false;
65856590
} else {
65866591
RHSIsComplex = false;
65876592
PrimType RHST = classifyPrim(RHS->getType());
6588-
RHSOffset = this->allocateLocalPrimitive(RHS, RHST, true, false);
6593+
RHSOffset = this->allocateLocalPrimitive(RHS, RHST, /*IsConst=*/true);
65896594
if (!this->visit(RHS))
65906595
return false;
65916596
if (!this->emitSetLocal(RHST, RHSOffset, E))
@@ -6762,8 +6767,8 @@ bool Compiler<Emitter>::emitBuiltinBitCast(const CastExpr *E) {
67626767
if (!this->visit(SubExpr))
67636768
return false;
67646769
} else if (std::optional<PrimType> FromT = classify(SubExpr)) {
6765-
unsigned TempOffset = allocateLocalPrimitive(
6766-
SubExpr, *FromT, /*IsConst=*/true, /*IsExtended=*/false);
6770+
unsigned TempOffset =
6771+
allocateLocalPrimitive(SubExpr, *FromT, /*IsConst=*/true);
67676772
if (!this->visit(SubExpr))
67686773
return false;
67696774
if (!this->emitSetLocal(*FromT, TempOffset, E))

clang/lib/AST/ByteCode/Compiler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,
303303

304304
/// Creates a local primitive value.
305305
unsigned allocateLocalPrimitive(DeclTy &&Decl, PrimType Ty, bool IsConst,
306-
bool IsExtended = false);
306+
const ValueDecl *ExtendingDecl = nullptr);
307307

308308
/// Allocates a space storing a local given its type.
309309
std::optional<unsigned>

0 commit comments

Comments
 (0)