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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

bruteforceboy
Copy link
Contributor

@bruteforceboy bruteforceboy commented Sep 18, 2024

Mistakenly closed #850

#850 (review)

This PR fixes array initialization for expression arguments.

Consider the following code snippet test.c:

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

int bar() {
  return 42;
}

void foo() {
  A a = {bar(), {}};
}

When ran with bin/clang test.c -Xclang -fclangir -Xclang -emit-cir -S -o -, It produces the following error:

~/clangir/clang/lib/CIR/CodeGen/CIRGenExprAgg.cpp:483: void {anonymous}::AggExprEmitter::buildArrayInit(cir::Address, mlir::cir::ArrayType, clang::QualType, clang::Expr*, llvm::ArrayRef<clang::Expr*>, clang::Expr*): Assertion `NumInitElements != 0' failed.

The error can be traced back to CIRGenExprAgg.cpp, and the fix is simple. It is possible to have an empty array initialization as an expression argument!

@bcardosolopes
Copy link
Member

It happens. This is missing the address of the review comments from the closed PR though.

@bruteforceboy
Copy link
Contributor Author

It happens. This is missing the address of the review comments from the closed PR though.

I am not sure if I got that, but I added a link to the review from the last PR.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants