From b2c5ff3ed5c6a7436622d2670d7ee327a5b14f1c Mon Sep 17 00:00:00 2001 From: Bruno Cardoso Lopes Date: Tue, 16 Apr 2024 18:26:59 -0700 Subject: [PATCH] [CIR][LLVMLowering] Fix handling of dense array conversions from const arrays We were lacking handling of trailing zeros for constant arrays. --- .../lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 15 ++++++++++++++- clang/test/CIR/Lowering/const-array.cir | 15 +++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 clang/test/CIR/Lowering/const-array.cir diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 1aeb03f90b61..5509534a0db3 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -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 x 1> // it returns !s32i as return value and stores 3 to elemQuantity. mlir::Type getNestedTypeAndElemQuantity(mlir::Type Ty, unsigned &elemQuantity) { @@ -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(); + assert(localArrayTy && "expected !cir.array"); + + auto nestTy = localArrayTy.getEltType(); + if (!nestTy.isa()) + values.insert(values.end(), localArrayTy.getSize() - numTrailingZeros, + getZeroInitFromType(nestTy)); + } } template diff --git a/clang/test/CIR/Lowering/const-array.cir b/clang/test/CIR/Lowering/const-array.cir new file mode 100644 index 000000000000..7aff779a04fa --- /dev/null +++ b/clang/test/CIR/Lowering/const-array.cir @@ -0,0 +1,15 @@ +// RUN: cir-translate %s -cir-to-llvmir -o - | FileCheck %s -check-prefix=LLVM + +!u8i = !cir.int + +module { + cir.global "private" internal @normal_url_char = #cir.const_array<[#cir.int<0> : !u8i, #cir.int<1> : !u8i], trailing_zeros> : !cir.array + // LLVM: @normal_url_char = internal global [4 x i8] c"\00\01\00\00" + + cir.func @c0() -> !cir.ptr> { + %0 = cir.get_global @normal_url_char : cir.ptr > + cir.return %0 : !cir.ptr> + } + // LLVM: define ptr @c0() + // LLVM: ret ptr @normal_url_char +}