Skip to content

Crash when lowering multiple dimention array #957

@ChuanqiXu9

Description

@ChuanqiXu9

Reproducer:

// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll
// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM

unsigned char table[10][3] =
{
    {1,0},
    {7,6,5},
};

the interesting part is, if we change the first dimension 10 to any number lower, it won't crash.

The crash information comes from:

return mlir::DenseElementsAttr::get(mlir::RankedTensorType::get(dims, type),
llvm::ArrayRef(values));

where the MLIR will assert the size of values will be the same with tensor. The shape of the tensor is correct. However, we will insert values to values in

template <typename AttrTy, typename StorageTy>
void convertToDenseElementsAttrImpl(mlir::cir::ConstArrayAttr attr,
llvm::SmallVectorImpl<StorageTy> &values) {
if (auto stringAttr = mlir::dyn_cast<mlir::StringAttr>(attr.getElts())) {
if (auto arrayType = mlir::dyn_cast<mlir::cir::ArrayType>(attr.getType())) {
for (auto element : stringAttr) {
auto intAttr = mlir::cir::IntAttr::get(arrayType.getEltType(), element);
values.push_back(mlir::dyn_cast<AttrTy>(intAttr).getValue());
}
return;
}
}
auto arrayAttr = mlir::cast<mlir::ArrayAttr>(attr.getElts());
for (auto eltAttr : arrayAttr) {
if (auto valueAttr = mlir::dyn_cast<AttrTy>(eltAttr)) {
values.push_back(valueAttr.getValue());
} else if (auto subArrayAttr =
mlir::dyn_cast<mlir::cir::ConstArrayAttr>(eltAttr)) {
convertToDenseElementsAttrImpl<AttrTy>(subArrayAttr, values);
if (mlir::dyn_cast<mlir::StringAttr>(subArrayAttr.getElts()))
fillTrailingZeros(subArrayAttr, values);
} else if (auto zeroAttr = mlir::dyn_cast<mlir::cir::ZeroAttr>(eltAttr)) {
unsigned numStoredZeros = 0;
auto nestTy =
getNestedTypeAndElemQuantity(zeroAttr.getType(), numStoredZeros);
values.insert(values.end(), numStoredZeros,
getZeroInitFromType<StorageTy>(nestTy));
} else {
llvm_unreachable("unknown element in ConstArrayAttr");
}
}
// Only fill in trailing zeros at the local cir.array level where the element
// type isn't another array (for the mult-dim case).
fillTrailingZeros(attr, values);
}
and it looks like the logics are somehow not correct so that it didn't generate values with enough elements.

I think we should resize values in the first place and assign values to the corresponding slot.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions