https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/221945
This does not increase the size of `Record::Field` but lets us avoid classifying fields all the time. >From eed21344a3704d21dd6c39b095e49b5bfa8bff2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timm=20B=C3=A4der?= <[email protected]> Date: Tue, 8 Sep 2026 11:12:15 +0200 Subject: [PATCH] [clang][bytecode] Save Record::Field PrimType --- clang/lib/AST/ByteCode/Compiler.cpp | 27 +++++++++++------------- clang/lib/AST/ByteCode/InterpBuiltin.cpp | 2 +- clang/lib/AST/ByteCode/Program.cpp | 5 +++-- clang/lib/AST/ByteCode/Record.h | 7 ++++-- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp index 5ef09d2fcf004..daa5307c92298 100644 --- a/clang/lib/AST/ByteCode/Compiler.cpp +++ b/clang/lib/AST/ByteCode/Compiler.cpp @@ -1210,9 +1210,8 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *E) { const Record *R = this->getRecord(E->getType()); assert(R); const Record::Field *RF = R->getField(UnionField); - QualType FieldType = RF->Decl->getType(); - if (OptPrimType PT = classify(FieldType)) { + if (OptPrimType PT = RF->T) { if (!this->visit(SubExpr)) return false; if (RF->isBitField()) @@ -3695,11 +3694,11 @@ bool Compiler<Emitter>::VisitTypeTraitExpr(const TypeTraitExpr *E) { if (!R || R->getNumFields() == 0) return false; const Record::Field *Field = R->getField(0U); - PrimType FieldT = classifyPrim(Field->Decl->getType()); - if (!this->emitConst(CmpInfo.getValueInfo(Result)->getIntValue(), FieldT, + assert(Field->T); + if (!this->emitConst(CmpInfo.getValueInfo(Result)->getIntValue(), *Field->T, E)) return false; - return this->emitInitField(FieldT, Field->Offset, E); + return this->emitInitField(*Field->T, Field->Offset, E); } PrimType T = classifyPrim(E->getType()); @@ -4027,11 +4026,9 @@ bool Compiler<Emitter>::VisitSourceLocExpr(const SourceLocExpr *E) { const Record::Field *F = R->getField(I); const APValue &FieldValue = V.getStructField(I); - PrimType FieldT = classifyPrim(F->Decl->getType()); - - if (!this->visitAPValue(FieldValue, FieldT, E)) + if (!this->visitAPValue(FieldValue, *F->T, E)) return false; - if (!this->emitInitField(FieldT, F->Offset, E)) + if (!this->emitInitField(*F->T, F->Offset, E)) return false; } @@ -4849,7 +4846,7 @@ bool Compiler<Emitter>::VisitCXXStdInitializerListExpr( if (!this->emitInitFieldPtr(R->getField(0u)->Offset, E)) return false; - PrimType SecondFieldT = classifyPrim(R->getField(1u)->Decl->getType()); + PrimType SecondFieldT = *R->getField(1u)->T; if (isIntegerOrBoolType(SecondFieldT)) { if (!this->emitConst(ArrayType->getSize(), SecondFieldT, E)) return false; @@ -5946,7 +5943,7 @@ bool Compiler<Emitter>::visitAPValueInitializer(const APValue &Val, const Record::Field *RF = R->getField(I); QualType FieldType = RF->Decl->getType(); // Fields. - if (OptPrimType PT = classify(FieldType)) { + if (OptPrimType PT = RF->T) { if (!this->visitAPValue(F, *PT, Info)) return false; if (!this->emitInitField(*PT, RF->Offset, Info)) @@ -5995,7 +5992,7 @@ bool Compiler<Emitter>::visitAPValueInitializer(const APValue &Val, const Record::Field *RF = R->getField(UnionField); QualType FieldType = RF->Decl->getType(); - if (OptPrimType PT = classify(FieldType)) { + if (OptPrimType PT = RF->T) { if (!this->visitAPValue(F, *PT, Info)) return false; if (RF->isBitField()) @@ -8926,7 +8923,7 @@ bool Compiler<Emitter>::emitHLSLAggregateSplat(PrimType SrcT, continue; QualType FieldType = F.Decl->getType(); - if (OptPrimType FieldT = classify(FieldType)) { + if (OptPrimType FieldT = F.T) { if (!this->emitGetLocal(SrcT, SrcOffset, E)) return false; if (!this->emitPrimCast(SrcT, *FieldT, FieldType, E)) @@ -9114,7 +9111,7 @@ bool Compiler<Emitter>::emitHLSLFlattenAggregate( if (!this->emitGetPtrFieldPop(F.Offset, E)) return false; - if (OptPrimType FieldT = classify(FieldType)) { + if (OptPrimType FieldT = F.T) { if (!this->emitLoadPop(*FieldT, E)) return false; if (!saveToLocal(*FieldT)) @@ -9227,7 +9224,7 @@ bool Compiler<Emitter>::emitHLSLConstructAggregate( continue; QualType FieldType = F.Decl->getType(); - if (OptPrimType FieldT = classify(FieldType)) { + if (OptPrimType FieldT = F.T) { if (!loadAndCast(*FieldT, FieldType)) return false; if (F.isBitField()) { diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp index b78ce614e290d..c3da474d06285 100644 --- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp +++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp @@ -6867,7 +6867,7 @@ static bool copyRecord(InterpState &S, CodePtr OpPC, PtrView Src, PtrView Dest, auto copyField = [&](const Record::Field &F, bool Activate) -> bool { PtrView DestField = Dest.atField(F.Offset); - if (OptPrimType FT = S.Ctx.classify(F.Decl->getType())) { + if (OptPrimType FT = F.T) { TYPE_SWITCH(*FT, { DestField.deref<T>() = Src.atField(F.Offset).deref<T>(); if (Src.atField(F.Offset).isInitialized()) diff --git a/clang/lib/AST/ByteCode/Program.cpp b/clang/lib/AST/ByteCode/Program.cpp index 137ca8c03fad5..f7bb9540570a7 100644 --- a/clang/lib/AST/ByteCode/Program.cpp +++ b/clang/lib/AST/ByteCode/Program.cpp @@ -325,7 +325,8 @@ Record *Program::getOrCreateRecord(const RecordDecl *RD) { const bool IsMutable = FD->isMutable(); const bool IsVolatile = FT.isVolatileQualified(); const Descriptor *Desc; - if (OptPrimType T = Ctx.classify(FT)) { + OptPrimType T = Ctx.classify(FT); + if (T) { Desc = createDescriptor(FD, *T, nullptr, IsConst, /*IsTemporary=*/false, IsMutable, IsVolatile); HasPtrField = HasPtrField || (T == PT_Ptr); @@ -339,7 +340,7 @@ Record *Program::getOrCreateRecord(const RecordDecl *RD) { } else { Desc = allocateDescriptor(FD); } - Fields.emplace_back(FD, Desc, BaseSize); + Fields.emplace_back(FD, Desc, BaseSize, T); BaseSize += align(Desc->getAllocSize()); } diff --git a/clang/lib/AST/ByteCode/Record.h b/clang/lib/AST/ByteCode/Record.h index a32ac5d14bf81..f03927bf71ffe 100644 --- a/clang/lib/AST/ByteCode/Record.h +++ b/clang/lib/AST/ByteCode/Record.h @@ -13,6 +13,7 @@ #ifndef LLVM_CLANG_AST_INTERP_RECORD_H #define LLVM_CLANG_AST_INTERP_RECORD_H +#include "PrimType.h" #include "clang/AST/Decl.h" #include "clang/AST/DeclCXX.h" @@ -29,6 +30,7 @@ class Record final { const FieldDecl *Decl; const Descriptor *Desc; unsigned Offset; + OptPrimType T; bool IsBitField; bool IsUnnamedBitField; @@ -39,8 +41,9 @@ class Record final { return Decl->getBitWidthValue(); } - Field(const FieldDecl *D, const Descriptor *Desc, unsigned Offset) - : Decl(D), Desc(Desc), Offset(Offset) { + Field(const FieldDecl *D, const Descriptor *Desc, unsigned Offset, + OptPrimType T) + : Decl(D), Desc(Desc), Offset(Offset), T(T) { IsBitField = Decl->isBitField(); IsUnnamedBitField = IsBitField && Decl->isUnnamedBitField(); } _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
