Skip to content
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
31 changes: 30 additions & 1 deletion clang/lib/CIR/CodeGen/CIRGenBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,35 @@ class CIRGenBuilderTy : public mlir::OpBuilder {
return mlir::cir::ConstArrayAttr::get(arrayTy, attrs);
}

mlir::Attribute getConstStructOrZeroAttr(mlir::ArrayAttr arrayAttr,
bool packed = false,
mlir::Type type = {}) {
llvm::SmallVector<mlir::Type, 8> members;
auto structTy = type.dyn_cast<mlir::cir::StructType>();
assert(structTy && "expected cir.struct");
assert(!packed && "unpacked struct is NYI");

// Collect members and check if they are all zero.
bool isZero = true;
for (auto &attr : arrayAttr) {
const auto typedAttr = attr.dyn_cast<mlir::TypedAttr>();
members.push_back(typedAttr.getType());
isZero &= isNullValue(typedAttr);
}

// Struct type not specified: create type from members.
if (!structTy)
structTy = getType<mlir::cir::StructType>(
members, mlir::StringAttr::get(getContext()),
/*body=*/true, packed,
/*ast=*/std::nullopt);

// Return zero or anonymous constant struct.
if (isZero)
return mlir::cir::ZeroAttr::get(getContext(), structTy);
return mlir::cir::ConstStructAttr::get(structTy, arrayAttr);
}

mlir::cir::ConstStructAttr getAnonConstStruct(mlir::ArrayAttr arrayAttr,
bool packed = false,
mlir::Type ty = {}) {
Expand Down Expand Up @@ -186,7 +215,7 @@ class CIRGenBuilderTy : public mlir::OpBuilder {
// TODO(cir): Once we have CIR float types, replace this by something like a
// NullableValueInterface to allow for type-independent queries.
bool isNullValue(mlir::Attribute attr) const {
if (attr.isa<mlir::cir::ZeroAttr>())
if (attr.isa<mlir::cir::ZeroAttr, mlir::cir::NullAttr>())
return true;

// TODO(cir): introduce char type in CIR and check for that instead.
Expand Down
17 changes: 4 additions & 13 deletions clang/lib/CIR/CodeGen/CIRGenExprConst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,9 @@ mlir::Attribute ConstantAggregateBuilder::buildFrom(

// TODO(cir): emit a #cir.zero if all elements are null values.
auto &builder = CGM.getBuilder();
return builder.getAnonConstStruct(
mlir::ArrayAttr::get(builder.getContext(),
Packed ? PackedElems : UnpackedElems),
Packed, DesiredTy);
auto arrAttr = mlir::ArrayAttr::get(builder.getContext(),
Packed ? PackedElems : UnpackedElems);
return builder.getConstStructOrZeroAttr(arrAttr, Packed, DesiredTy);
}

void ConstantAggregateBuilder::condense(CharUnits Offset,
Expand Down Expand Up @@ -1461,14 +1460,6 @@ mlir::Attribute ConstantEmitter::tryEmitPrivate(const APValue &Value,
const ArrayType *ArrayTy = CGM.getASTContext().getAsArrayType(DestType);
unsigned NumElements = Value.getArraySize();
unsigned NumInitElts = Value.getArrayInitializedElts();
auto isNullValue = [&](mlir::Attribute f) {
// TODO(cir): introduce char type in CIR and check for that instead.
auto intVal = f.dyn_cast_or_null<mlir::cir::IntAttr>();
assert(intVal && "not implemented");
if (intVal.getValue() == 0)
return true;
return false;
};

// Emit array filler, if there is one.
mlir::Attribute Filler;
Expand All @@ -1481,7 +1472,7 @@ mlir::Attribute ConstantEmitter::tryEmitPrivate(const APValue &Value,

// Emit initializer elements.
SmallVector<mlir::TypedAttr, 16> Elts;
if (Filler && isNullValue(Filler))
if (Filler && builder.isNullValue(Filler))
Elts.reserve(NumInitElts + 1);
else
Elts.reserve(NumElements);
Expand Down
6 changes: 6 additions & 0 deletions clang/test/CIR/CodeGen/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,9 @@ int multidim(int i, int j) {
// Should globally zero-initialize null arrays.
int globalNullArr[] = {0, 0};
// CHECK: cir.global external @globalNullArr = #cir.zero : !cir.array<!s32i x 2>

// Should implicitly zero-initialize global array elements.
struct S {
int i;
} arr[3] = {{1}};
// CHECK: cir.global external @arr = #cir.const_array<[#cir.const_struct<{#cir.int<1> : !s32i}> : !ty_22struct2ES22, #cir.zero : !ty_22struct2ES22, #cir.zero : !ty_22struct2ES22]> : !cir.array<!ty_22struct2ES22 x 3>