Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CIR][CodeGen] Fix array initialization in CIRGenExprAgg #852

Merged
merged 6 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion clang/lib/CIR/CodeGen/CIRGenExprAgg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,6 @@ void AggExprEmitter::buildArrayInit(Address DestPtr, mlir::cir::ArrayType AType,
uint64_t NumInitElements = Args.size();

uint64_t NumArrayElements = AType.getSize();
assert(NumInitElements != 0 && "expected at least one initializaed value");
assert(NumInitElements <= NumArrayElements);

QualType elementType =
Expand Down
38 changes: 38 additions & 0 deletions clang/test/CIR/CodeGen/array-init.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++17 -fclangir -emit-cir %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s

typedef struct {
int a;
int b[2];
} A;

int bar() {
return 42;
}

void foo() {
A a = {bar(), {}};
}
// CHECK: %0 = cir.alloca !ty_A, !cir.ptr<!ty_A>, ["a", init]
// CHECK: %1 = cir.alloca !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>, ["arrayinit.temp", init]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to use regexes for SSA values and struct definitions, see examples in clang/test/CIR/CodeGen/abstract-cond.c and eslewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

// CHECK: %2 = cir.get_member %0[0] {name = "a"} : !cir.ptr<!ty_A> -> !cir.ptr<!s32i>
// CHECK: %3 = cir.call @_Z3barv() : () -> !s32i
// CHECK: cir.store %3, %2 : !s32i, !cir.ptr<!s32i>
// CHECK: %4 = cir.get_member %0[1] {name = "b"} : !cir.ptr<!ty_A> -> !cir.ptr<!cir.array<!s32i x 2>>
// CHECK: %5 = cir.cast(array_to_ptrdecay, %4 : !cir.ptr<!cir.array<!s32i x 2>>), !cir.ptr<!s32i>
// CHECK: cir.store %5, %1 : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
// CHECK: %6 = cir.const #cir.int<2> : !s64i
// CHECK: %7 = cir.ptr_stride(%5 : !cir.ptr<!s32i>, %6 : !s64i), !cir.ptr<!s32i>
// CHECK: cir.do {
// CHECK: %8 = cir.load %1 : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i>
// CHECK: %9 = cir.const #cir.int<0> : !s32i
// CHECK: cir.store %9, %8 : !s32i, !cir.ptr<!s32i>
// CHECK: %10 = cir.const #cir.int<1> : !s64i
// CHECK: %11 = cir.ptr_stride(%8 : !cir.ptr<!s32i>, %10 : !s64i), !cir.ptr<!s32i>
// CHECK: cir.store %11, %1 : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
// CHECK: cir.yield
// CHECK: } while {
// CHECK: %8 = cir.load %1 : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i>
// CHECK: %9 = cir.cmp(ne, %8, %7) : !cir.ptr<!s32i>, !cir.bool
// CHECK: cir.condition(%9)
// CHECK: }
Loading