Skip to content

Commit

Permalink
[CIR][LLVMLowering] Fix handling of dense array conversions from cons…
Browse files Browse the repository at this point in the history
…t arrays

We were lacking handling of trailing zeros for constant arrays.
  • Loading branch information
bcardosolopes authored and lanza committed Apr 17, 2024
1 parent 87a61f3 commit b2c5ff3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
15 changes: 14 additions & 1 deletion clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@ template <> mlir::APFloat getZeroInitFromType(mlir::Type Ty) {
llvm_unreachable("NYI");
}

// return the nested type and quiantity of elements for cir.array type.
// return the nested type and quantity of elements for cir.array type.
// e.g: for !cir.array<!cir.array<!s32i x 3> x 1>
// it returns !s32i as return value and stores 3 to elemQuantity.
mlir::Type getNestedTypeAndElemQuantity(mlir::Type Ty, unsigned &elemQuantity) {
Expand Down Expand Up @@ -1072,6 +1072,19 @@ void convertToDenseElementsAttrImpl(mlir::cir::ConstArrayAttr attr,
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).
auto numTrailingZeros = attr.getTrailingZerosNum();
if (numTrailingZeros) {
auto localArrayTy = attr.getType().dyn_cast<mlir::cir::ArrayType>();
assert(localArrayTy && "expected !cir.array");

auto nestTy = localArrayTy.getEltType();
if (!nestTy.isa<mlir::cir::ArrayType>())
values.insert(values.end(), localArrayTy.getSize() - numTrailingZeros,
getZeroInitFromType<StorageTy>(nestTy));
}
}

template <typename AttrTy, typename StorageTy>
Expand Down
15 changes: 15 additions & 0 deletions clang/test/CIR/Lowering/const-array.cir
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: cir-translate %s -cir-to-llvmir -o - | FileCheck %s -check-prefix=LLVM

!u8i = !cir.int<u, 8>

module {
cir.global "private" internal @normal_url_char = #cir.const_array<[#cir.int<0> : !u8i, #cir.int<1> : !u8i], trailing_zeros> : !cir.array<!u8i x 4>
// LLVM: @normal_url_char = internal global [4 x i8] c"\00\01\00\00"

cir.func @c0() -> !cir.ptr<!cir.array<!u8i x 4>> {
%0 = cir.get_global @normal_url_char : cir.ptr <!cir.array<!u8i x 4>>
cir.return %0 : !cir.ptr<!cir.array<!u8i x 4>>
}
// LLVM: define ptr @c0()
// LLVM: ret ptr @normal_url_char
}

0 comments on commit b2c5ff3

Please sign in to comment.