Skip to content

Commit 7c72f85

Browse files
committed
Reapply "[clang][bytecode] Allocate IntegralAP and Floating types using an allocator (llvm#144246)"
This reverts commit 57828fe.
1 parent 998e3bb commit 7c72f85

17 files changed

+930
-343
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 63 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,8 @@ bool Compiler<Emitter>::VisitFloatingLiteral(const FloatingLiteral *E) {
759759
if (DiscardResult)
760760
return true;
761761

762-
return this->emitConstFloat(E->getValue(), E);
762+
APFloat F = E->getValue();
763+
return this->emitFloat(F, E);
763764
}
764765

765766
template <class Emitter>
@@ -4196,8 +4197,10 @@ bool Compiler<Emitter>::visitZeroInitializer(PrimType T, QualType QT,
41964197
nullptr, E);
41974198
case PT_MemberPtr:
41984199
return this->emitNullMemberPtr(0, nullptr, E);
4199-
case PT_Float:
4200-
return this->emitConstFloat(APFloat::getZero(Ctx.getFloatSemantics(QT)), E);
4200+
case PT_Float: {
4201+
APFloat F = APFloat::getZero(Ctx.getFloatSemantics(QT));
4202+
return this->emitFloat(F, E);
4203+
}
42014204
case PT_FixedPoint: {
42024205
auto Sem = Ctx.getASTContext().getFixedPointSemantics(E->getType());
42034206
return this->emitConstFixedPoint(FixedPoint::zero(Sem), E);
@@ -4685,10 +4688,7 @@ VarCreationState Compiler<Emitter>::visitVarDecl(const VarDecl *VD,
46854688
if (!visitInitializer(Init))
46864689
return false;
46874690

4688-
if (!this->emitFinishInit(Init))
4689-
return false;
4690-
4691-
return this->emitPopPtr(Init);
4691+
return this->emitFinishInitGlobal(Init);
46924692
};
46934693

46944694
DeclScope<Emitter> LocalScope(this, VD);
@@ -4709,51 +4709,45 @@ VarCreationState Compiler<Emitter>::visitVarDecl(const VarDecl *VD,
47094709
return false;
47104710

47114711
return !Init || (checkDecl() && initGlobal(*GlobalIndex));
4712-
} else {
4713-
InitLinkScope<Emitter> ILS(this, InitLink::Decl(VD));
4714-
4715-
if (VarT) {
4716-
unsigned Offset = this->allocateLocalPrimitive(
4717-
VD, *VarT, VD->getType().isConstQualified(), nullptr,
4718-
ScopeKind::Block, IsConstexprUnknown);
4719-
if (Init) {
4720-
// If this is a toplevel declaration, create a scope for the
4721-
// initializer.
4722-
if (Toplevel) {
4723-
LocalScope<Emitter> Scope(this);
4724-
if (!this->visit(Init))
4725-
return false;
4726-
return this->emitSetLocal(*VarT, Offset, VD) && Scope.destroyLocals();
4727-
} else {
4728-
if (!this->visit(Init))
4729-
return false;
4730-
return this->emitSetLocal(*VarT, Offset, VD);
4731-
}
4732-
}
4733-
} else {
4734-
if (std::optional<unsigned> Offset =
4735-
this->allocateLocal(VD, VD->getType(), nullptr, ScopeKind::Block,
4736-
IsConstexprUnknown)) {
4737-
if (!Init)
4738-
return true;
4712+
}
4713+
// Local variables.
4714+
InitLinkScope<Emitter> ILS(this, InitLink::Decl(VD));
47394715

4740-
if (!this->emitGetPtrLocal(*Offset, Init))
4716+
if (VarT) {
4717+
unsigned Offset = this->allocateLocalPrimitive(
4718+
VD, *VarT, VD->getType().isConstQualified(), nullptr, ScopeKind::Block,
4719+
IsConstexprUnknown);
4720+
if (Init) {
4721+
// If this is a toplevel declaration, create a scope for the
4722+
// initializer.
4723+
if (Toplevel) {
4724+
LocalScope<Emitter> Scope(this);
4725+
if (!this->visit(Init))
47414726
return false;
4742-
4743-
if (!visitInitializer(Init))
4727+
return this->emitSetLocal(*VarT, Offset, VD) && Scope.destroyLocals();
4728+
} else {
4729+
if (!this->visit(Init))
47444730
return false;
4731+
return this->emitSetLocal(*VarT, Offset, VD);
4732+
}
4733+
}
4734+
} else {
4735+
if (std::optional<unsigned> Offset = this->allocateLocal(
4736+
VD, VD->getType(), nullptr, ScopeKind::Block, IsConstexprUnknown)) {
4737+
if (!Init)
4738+
return true;
47454739

4746-
if (!this->emitFinishInit(Init))
4747-
return false;
4740+
if (!this->emitGetPtrLocal(*Offset, Init))
4741+
return false;
47484742

4749-
return this->emitPopPtr(Init);
4750-
}
4751-
return false;
4743+
if (!visitInitializer(Init))
4744+
return false;
4745+
4746+
return this->emitFinishInitPop(Init);
47524747
}
4753-
return true;
4748+
return false;
47544749
}
4755-
4756-
return false;
4750+
return true;
47574751
}
47584752

47594753
template <class Emitter>
@@ -4762,8 +4756,10 @@ bool Compiler<Emitter>::visitAPValue(const APValue &Val, PrimType ValType,
47624756
assert(!DiscardResult);
47634757
if (Val.isInt())
47644758
return this->emitConst(Val.getInt(), ValType, E);
4765-
else if (Val.isFloat())
4766-
return this->emitConstFloat(Val.getFloat(), E);
4759+
else if (Val.isFloat()) {
4760+
APFloat F = Val.getFloat();
4761+
return this->emitFloat(F, E);
4762+
}
47674763

47684764
if (Val.isLValue()) {
47694765
if (Val.isNullPointer())
@@ -6144,8 +6140,10 @@ bool Compiler<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
61446140
const auto &TargetSemantics = Ctx.getFloatSemantics(E->getType());
61456141
if (!this->emitLoadFloat(E))
61466142
return false;
6147-
if (!this->emitConstFloat(llvm::APFloat(TargetSemantics, 1), E))
6143+
APFloat F(TargetSemantics, 1);
6144+
if (!this->emitFloat(F, E))
61486145
return false;
6146+
61496147
if (!this->emitAddf(getFPOptions(E), E))
61506148
return false;
61516149
if (!this->emitStoreFloat(E))
@@ -6187,8 +6185,10 @@ bool Compiler<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
61876185
const auto &TargetSemantics = Ctx.getFloatSemantics(E->getType());
61886186
if (!this->emitLoadFloat(E))
61896187
return false;
6190-
if (!this->emitConstFloat(llvm::APFloat(TargetSemantics, 1), E))
6188+
APFloat F(TargetSemantics, 1);
6189+
if (!this->emitFloat(F, E))
61916190
return false;
6191+
61926192
if (!this->emitSubf(getFPOptions(E), E))
61936193
return false;
61946194
if (!this->emitStoreFloat(E))
@@ -6964,6 +6964,20 @@ bool Compiler<Emitter>::emitDummyPtr(const DeclTy &D, const Expr *E) {
69646964
return true;
69656965
}
69666966

6967+
template <class Emitter>
6968+
bool Compiler<Emitter>::emitFloat(const APFloat &F, const Expr *E) {
6969+
assert(!DiscardResult && "Should've been checked before");
6970+
6971+
if (Floating::singleWord(F.getSemantics()))
6972+
return this->emitConstFloat(Floating(F), E);
6973+
6974+
APInt I = F.bitcastToAPInt();
6975+
return this->emitConstFloat(
6976+
Floating(const_cast<uint64_t *>(I.getRawData()),
6977+
llvm::APFloatBase::SemanticsToEnum(F.getSemantics())),
6978+
E);
6979+
}
6980+
69676981
// This function is constexpr if and only if To, From, and the types of
69686982
// all subobjects of To and From are types T such that...
69696983
// (3.1) - is_union_v<T> is false;

clang/lib/AST/ByteCode/Compiler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,
395395
bool emitRecordDestruction(const Record *R, SourceInfo Loc);
396396
bool emitDestruction(const Descriptor *Desc, SourceInfo Loc);
397397
bool emitDummyPtr(const DeclTy &D, const Expr *E);
398+
bool emitFloat(const APFloat &F, const Expr *E);
398399
unsigned collectBaseOffset(const QualType BaseType,
399400
const QualType DerivedType);
400401
bool emitLambdaStaticInvokerBody(const CXXMethodDecl *MD);

clang/lib/AST/ByteCode/Descriptor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ Descriptor::Descriptor(const DeclTy &D, PrimType Type, MetadataSize MD,
368368
bool IsTemporary, bool IsConst, UnknownSize)
369369
: Source(D), ElemSize(primSize(Type)), Size(UnknownSizeMark),
370370
MDSize(MD.value_or(0)),
371-
AllocSize(MDSize + sizeof(InitMapPtr) + alignof(void *)),
371+
AllocSize(MDSize + sizeof(InitMapPtr) + alignof(void *)), PrimT(Type),
372372
IsConst(IsConst), IsMutable(false), IsTemporary(IsTemporary),
373373
IsArray(true), CtorFn(getCtorArrayPrim(Type)),
374374
DtorFn(getDtorArrayPrim(Type)), MoveFn(getMoveArrayPrim(Type)) {

clang/lib/AST/ByteCode/Disasm.cpp

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,34 +50,56 @@ inline static std::string printArg(Program &P, CodePtr &OpPC) {
5050
}
5151

5252
template <> inline std::string printArg<Floating>(Program &P, CodePtr &OpPC) {
53-
auto F = Floating::deserialize(*OpPC);
54-
OpPC += align(F.bytesToSerialize());
53+
auto Sem = Floating::deserializeSemantics(*OpPC);
5554

56-
std::string Result;
57-
llvm::raw_string_ostream SS(Result);
58-
SS << F;
59-
return Result;
55+
unsigned BitWidth = llvm::APFloatBase::semanticsSizeInBits(
56+
llvm::APFloatBase::EnumToSemantics(Sem));
57+
auto Memory =
58+
std::make_unique<uint64_t[]>(llvm::APInt::getNumWords(BitWidth));
59+
Floating Result(Memory.get(), Sem);
60+
Floating::deserialize(*OpPC, &Result);
61+
62+
OpPC += align(Result.bytesToSerialize());
63+
64+
std::string S;
65+
llvm::raw_string_ostream SS(S);
66+
SS << Result;
67+
return S;
6068
}
6169

6270
template <>
6371
inline std::string printArg<IntegralAP<false>>(Program &P, CodePtr &OpPC) {
64-
auto F = IntegralAP<false>::deserialize(*OpPC);
65-
OpPC += align(F.bytesToSerialize());
66-
67-
std::string Result;
68-
llvm::raw_string_ostream SS(Result);
69-
SS << F;
70-
return Result;
72+
using T = IntegralAP<false>;
73+
unsigned BitWidth = T::deserializeSize(*OpPC);
74+
auto Memory =
75+
std::make_unique<uint64_t[]>(llvm::APInt::getNumWords(BitWidth));
76+
77+
T Result(Memory.get(), BitWidth);
78+
T::deserialize(*OpPC, &Result);
79+
80+
OpPC += Result.bytesToSerialize();
81+
std::string Str;
82+
llvm::raw_string_ostream SS(Str);
83+
SS << Result;
84+
return Str;
7185
}
86+
7287
template <>
7388
inline std::string printArg<IntegralAP<true>>(Program &P, CodePtr &OpPC) {
74-
auto F = IntegralAP<true>::deserialize(*OpPC);
75-
OpPC += align(F.bytesToSerialize());
89+
using T = IntegralAP<true>;
90+
unsigned BitWidth = T::deserializeSize(*OpPC);
91+
auto Memory =
92+
std::make_unique<uint64_t[]>(llvm::APInt::getNumWords(BitWidth));
7693

77-
std::string Result;
78-
llvm::raw_string_ostream SS(Result);
79-
SS << F;
80-
return Result;
94+
T Result(Memory.get(), BitWidth);
95+
T::deserialize(*OpPC, &Result);
96+
97+
std::string Str;
98+
llvm::raw_string_ostream SS(Str);
99+
SS << Result;
100+
101+
OpPC += Result.bytesToSerialize();
102+
return Str;
81103
}
82104

83105
template <> inline std::string printArg<FixedPoint>(Program &P, CodePtr &OpPC) {

0 commit comments

Comments
 (0)