Skip to content

Commit

Permalink
[CIR][CodeGen] handle zero init padding case
Browse files Browse the repository at this point in the history
  • Loading branch information
gitoleg committed Dec 25, 2024
1 parent ea89bf7 commit 21f9977
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions clang/lib/CIR/CodeGen/CIRGenExprConst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
// This contains code to emit Constant Expr nodes as LLVM code.
//
//===----------------------------------------------------------------------===//
#include <iostream>

#include "Address.h"
#include "CIRGenCXXABI.h"
#include "CIRGenCstEmitter.h"
Expand Down Expand Up @@ -50,11 +48,11 @@ mlir::TypedAttr getPadding(CIRGenModule &CGM, CharUnits size) {
auto arSize = size.getQuantity();
auto &bld = CGM.getBuilder();
if (size > CharUnits::One()) {
SmallVector<mlir::Attribute, 4> elts(arSize, bld.getZeroAttr(eltTy));
SmallVector<mlir::Attribute, 4> elts(arSize, bld.getZeroAttr(eltTy));
return bld.getConstArray(mlir::ArrayAttr::get(bld.getContext(), elts),
bld.getArrayType(eltTy, arSize));
} else {
return cir::ZeroAttr::get(bld.getContext(), eltTy);
return cir::ZeroAttr::get(bld.getContext(), eltTy);
}
}

Expand Down Expand Up @@ -161,7 +159,7 @@ static void replace(Container &C, size_t BeginOff, size_t EndOff, Range Vals) {
}

bool ConstantAggregateBuilder::add(mlir::Attribute A, CharUnits Offset,
bool AllowOverwrite) {
bool AllowOverwrite) {
// FIXME(cir): migrate most of this file to use mlir::TypedAttr directly.
mlir::TypedAttr C = mlir::dyn_cast<mlir::TypedAttr>(A);
assert(C && "expected typed attribute");
Expand Down Expand Up @@ -387,7 +385,7 @@ mlir::Attribute ConstantAggregateBuilder::buildFrom(
CharUnits AlignedSize = Size.alignTo(Align);

bool Packed = false;
ArrayRef<mlir::Attribute> UnpackedElems = Elems;
ArrayRef<mlir::Attribute> UnpackedElems = Elems;

llvm::SmallVector<mlir::Attribute, 32> UnpackedElemStorage;
if (DesiredSize < AlignedSize || DesiredSize.alignTo(Align) != DesiredSize) {
Expand All @@ -398,7 +396,7 @@ mlir::Attribute ConstantAggregateBuilder::buildFrom(
// is ignored if we choose a packed layout.)
UnpackedElemStorage.assign(UnpackedElems.begin(), UnpackedElems.end());
UnpackedElemStorage.push_back(Utils.getPadding(DesiredSize - Size));
UnpackedElems = UnpackedElemStorage;
UnpackedElems = UnpackedElemStorage;
}

// If we don't have a natural layout, insert padding as necessary.
Expand Down Expand Up @@ -650,15 +648,8 @@ bool ConstStructBuilder::Build(InitListExpr *ILE, bool AllowOverwrite) {
Expr *Init = nullptr;
if (ElementNo < ILE->getNumInits())
Init = ILE->getInit(ElementNo++);

if (Init && isa<NoInitExpr>(Init)) {
if (ZeroInitPadding &&
!DoZeroInitPadding(Layout, FieldNo, *Field, AllowOverwrite, SizeSoFar,
ZeroFieldSize))
return false;

if (Init && isa<NoInitExpr>(Init))
continue;
}

// Zero-sized fields are not emitted, but their initializers may still
// prevent emission of this struct as a constant.
Expand All @@ -667,7 +658,7 @@ bool ConstStructBuilder::Build(InitListExpr *ILE, bool AllowOverwrite) {
return false;
continue;
}

if (ZeroInitPadding &&
!DoZeroInitPadding(Layout, FieldNo, *Field, AllowOverwrite, SizeSoFar,
ZeroFieldSize))
Expand Down Expand Up @@ -819,15 +810,15 @@ bool ConstStructBuilder::Build(const APValue &Val, const RecordDecl *RD,
bool ConstStructBuilder::DoZeroInitPadding(
const ASTRecordLayout &Layout, unsigned FieldNo, const FieldDecl &Field,
bool AllowOverwrite, CharUnits &SizeSoFar, bool &ZeroFieldSize) {

uint64_t StartBitOffset = Layout.getFieldOffset(FieldNo);
CharUnits StartOffset = CGM.getASTContext().toCharUnitsFromBits(StartBitOffset);
if (SizeSoFar < StartOffset) {
if (!AppendBytes(SizeSoFar, getPadding(CGM, StartOffset - SizeSoFar),
AllowOverwrite))
return false;
}

if (!Field.isBitField()) {
CharUnits FieldSize = CGM.getASTContext().getTypeSizeInChars(Field.getType());
SizeSoFar = StartOffset + FieldSize;
Expand Down

0 comments on commit 21f9977

Please sign in to comment.