Skip to content

Commit 93715bd

Browse files
committed
[CIR][LLVMLowering] Fix handling of dense array conversions from const arrays
We were lacking handling of trailing zeros for constant arrays.
1 parent 4dd722a commit 93715bd

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

+14-1
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,7 @@ template <> mlir::APFloat getZeroInitFromType(mlir::Type Ty) {
10361036
llvm_unreachable("NYI");
10371037
}
10381038

1039-
// return the nested type and quiantity of elements for cir.array type.
1039+
// return the nested type and quantity of elements for cir.array type.
10401040
// e.g: for !cir.array<!cir.array<!s32i x 3> x 1>
10411041
// it returns !s32i as return value and stores 3 to elemQuantity.
10421042
mlir::Type getNestedTypeAndElemQuantity(mlir::Type Ty, unsigned &elemQuantity) {
@@ -1072,6 +1072,19 @@ void convertToDenseElementsAttrImpl(mlir::cir::ConstArrayAttr attr,
10721072
llvm_unreachable("unknown element in ConstArrayAttr");
10731073
}
10741074
}
1075+
1076+
// Only fill in trailing zeros at the local cir.array level where the element
1077+
// type isn't another array (for the mult-dim case).
1078+
auto numTrailingZeros = attr.getTrailingZerosNum();
1079+
if (numTrailingZeros) {
1080+
auto localArrayTy = attr.getType().dyn_cast<mlir::cir::ArrayType>();
1081+
assert(localArrayTy && "expected !cir.array");
1082+
1083+
auto nestTy = localArrayTy.getEltType();
1084+
if (!nestTy.isa<mlir::cir::ArrayType>())
1085+
values.insert(values.end(), localArrayTy.getSize() - numTrailingZeros,
1086+
getZeroInitFromType<StorageTy>(nestTy));
1087+
}
10751088
}
10761089

10771090
template <typename AttrTy, typename StorageTy>
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: cir-translate %s -cir-to-llvmir -o - | FileCheck %s -check-prefix=LLVM
2+
3+
!u8i = !cir.int<u, 8>
4+
5+
module {
6+
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>
7+
// LLVM: @normal_url_char = internal global [4 x i8] c"\00\01\00\00"
8+
9+
cir.func @c0() -> !cir.ptr<!cir.array<!u8i x 4>> {
10+
%0 = cir.get_global @normal_url_char : cir.ptr <!cir.array<!u8i x 4>>
11+
cir.return %0 : !cir.ptr<!cir.array<!u8i x 4>>
12+
}
13+
// LLVM: define ptr @c0()
14+
// LLVM: ret ptr @normal_url_char
15+
}

0 commit comments

Comments
 (0)