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][Lowering] Fix lowering for multi dimensional array #851

Merged
merged 17 commits into from
Sep 18, 2024

Conversation

bruteforceboy
Copy link
Contributor

This PR fixes the lowering for multi dimensional arrays.

Consider the following code snippet test.c:

void foo() {
  char arr[4][1] = {"a", "b", "c", "d"};
}

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

~/clangir/llvm/include/llvm/Support/Casting.h:566: decltype(auto) llvm::cast(const From&) [with To = mlir::ArrayAttr; From = mlir::Attribute]: Assertion `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"' failed.

The bug can be traced back to LoweringHelpers.cpp. It considers the values in the array as integer types, and this causes an error in this case.

This PR updates convertToDenseElementsAttrImpl when the array contains string attributes. I have also added one more similar test. Note that in the tests I used a literal match to avoid matching as regex, so !dbg is useful.

Copy link
Member

@bcardosolopes bcardosolopes left a comment

Choose a reason for hiding this comment

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

LGTM with some minor changes needed.

clang/test/CIR/Lowering/array-init.c Outdated Show resolved Hide resolved
@bruteforceboy
Copy link
Contributor Author

I just made a small change to LoweringHelpers.cpp. I noticed in some cases there were missing trailing zeros. I have added an extra test, charInit1, to array-init.c please check it out.

Copy link
Member

@bcardosolopes bcardosolopes left a comment

Choose a reason for hiding this comment

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

LGTM!

While here, extra question: if one of these arrays has more elements than what's used in the written initialization, is it getting proper zeros at the end? Perhaps add more tests for that in a following up PR?

@bcardosolopes bcardosolopes merged commit 0789637 into llvm:main Sep 18, 2024
5 of 6 checks passed
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