This is an archive of the discontinued LLVM Phabricator instance.

[clang] fix consteval ctor code generation assert
AbandonedPublic

Authored by luken-google on Jan 13 2023, 6:52 AM.

Details

Summary

Fixes GH#53983.

Diff Detail

Event Timeline

luken-google created this revision.Jan 13 2023, 6:52 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 13 2023, 6:52 AM
luken-google requested review of this revision.Jan 13 2023, 6:52 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 13 2023, 6:52 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

See: https://github.com/llvm/llvm-project/issues/53983

I can add a release note and some testing, but wanted feedback that this was the right approach. The problem is when generating code for the constructor for the bar element inside of MyStruct, Dest has an elementType of Base, but Builder.CreateStructGEP assumes the {i8, i8} layout implied by Base. which causes an assert trying to emit code for the second element. So, I've added logic to make sure the layout of STy and Dest are identical, which fixes the assert but doesn't break any of the existing testing around aggregate stores.

AFAICT the AST and lowering all looks correct, and this is an issue in code generation. But this is a new area of clang for me so I'd welcome suggestions about if there is a better fix or if the fix should be in a different place. Thanks!

The change seems reasonable. Could you add a test with a crasher repro and some validation of the generated IR?

Sorry, didn't see your comment when submitting mine.

I think the offending code is clearly wrong here and adding the isLayoutIdentical seems like the right thing to do.

This looks like a small change and I'm happy to LGTM this myself.
However, I also have very little experience with codegen, you might want to find someone more experienced with it to have a more informed look. @jyknight maybe?

clang/lib/CodeGen/CGCall.cpp
1338

Is it guaranteed that Dest.getElementType() is never something other than StructType?

Adds test and release note, refactors to avoid possible null pointer deref.

Add newline at end of test file.

clang/lib/CodeGen/CGCall.cpp
1338

Yikes, no. And checking the cast as another condition broke a bunch of other cases. This works much better.

luken-google edited reviewers, added: jyknight; removed: usaxena95.Jan 17 2023, 5:51 AM
luken-google removed a subscriber: jyknight.
ilya-biryukov added inline comments.Jan 17 2023, 7:07 AM
clang/lib/CodeGen/CGCall.cpp
1338

Intuitively, I feel like canLosslesslyBitCastTo should be used in conjuction with something like Builder.CreateBitcast and after a quick skim through existing I can't find any uses with CreateStore in the codebase. This leads me to think that we need to call a different function here. As mentioned earlier, I may be completely wrong as I don't have much experience with codegen.

I will let @jyknight sanity-check my intuition.

luken-google abandoned this revision.Feb 23 2023, 7:42 AM

seems to have fixed itself..