Skip to content

Commit

Permalink
adds tests, fixes minor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
gitoleg committed Jan 11, 2024
1 parent 08a5c72 commit 73c6676
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
1 change: 0 additions & 1 deletion clang/lib/CIR/CodeGen/CIRGenExprConst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,6 @@ buildArrayConstant(CIRGenModule &CGM, mlir::Type DesiredType,
// Add a zeroinitializer array filler if we have lots of trailing zeroes.
unsigned TrailingZeroes = ArrayBound - NonzeroLength;
if (TrailingZeroes >= 8) {
//assert(0 && "NYE");
assert(Elements.size() >= NonzeroLength &&
"missing initializer for non-zero element");

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CIR/Dialect/IR/CIRDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2297,7 +2297,7 @@ ::mlir::Attribute ConstArrayAttr::parse(::mlir::AsmParser &parser,
}

int zeros = 0;
if (parser.parseComma().succeeded()) {
if (parser.parseOptionalComma().succeeded()) {
if (parser.parseInteger(zeros).failed())
return {};
if (parser.parseOptionalKeyword("zeros"))
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -999,12 +999,12 @@ convertToDenseElementsAttr(mlir::cir::ConstArrayAttr attr,
}

bool hasTrailingZeros(mlir::cir::ConstArrayAttr attr) {
auto array = attr.getElts().cast<mlir::ArrayAttr>();
auto array = attr.getElts().dyn_cast<mlir::ArrayAttr>();
return attr.hasTrailingZeros() ||
std::count_if(array.begin(), array.end(), [](auto elt) {
(array && std::count_if(array.begin(), array.end(), [](auto elt) {
auto ar = dyn_cast<mlir::cir::ConstArrayAttr>(elt);
return ar && hasTrailingZeros(ar);
});
}));
}

std::optional<mlir::Attribute>
Expand Down
10 changes: 10 additions & 0 deletions clang/test/CIR/CodeGen/const-array.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-cir %s -o - | FileCheck %s

void foo() {
int a[10] = {1};
}

// CHECK: cir.func {{.*@foo}}
// CHECK: %0 = cir.alloca !cir.array<!s32i x 10>, cir.ptr <!cir.array<!s32i x 10>>, ["a"] {alignment = 16 : i64}
// CHECK: %1 = cir.const(#cir.const_array<[#cir.int<1> : !s32i], 9 zeros> : !cir.array<!s32i x 10>) : !cir.array<!s32i x 10>
// CHECK: cir.store %1, %0 : !cir.array<!s32i x 10>, cir.ptr <!cir.array<!s32i x 10>>
14 changes: 14 additions & 0 deletions clang/test/CIR/Lowering/const.cir
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,18 @@ module {
// CHECK: cir.llvmir.zeroinit : !llvm.array<3 x i32>
cir.return
}

cir.func @testArrWithTrailingZeros() {
%0 = cir.alloca !cir.array<!s32i x 10>, cir.ptr <!cir.array<!s32i x 10>>, ["a"] {alignment = 16 : i64}
%1 = cir.const(#cir.const_array<[#cir.int<1> : !s32i], 9 zeros> : !cir.array<!s32i x 10>) : !cir.array<!s32i x 10>
cir.store %1, %0 : !cir.array<!s32i x 10>, cir.ptr <!cir.array<!s32i x 10>>
cir.return
}
// CHECK: llvm.func @testArrWithTrailingZeros()
// CHECK: %0 = llvm.mlir.constant(1 : index) : i64
// CHECK: %1 = llvm.alloca %0 x !llvm.array<10 x i32> {alignment = 16 : i64} : (i64) -> !llvm.ptr
// CHECK: %2 = cir.llvmir.zeroinit : !llvm.array<10 x i32>
// CHECK: %3 = llvm.mlir.constant(1 : i32) : i32
// CHECK: %4 = llvm.insertvalue %3, %2[0] : !llvm.array<10 x i32>

}

0 comments on commit 73c6676

Please sign in to comment.