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

Empty union in initialized with undef values in OG and with zeroinitializer in CIR #1272

Open
bcardosolopes opened this issue Jan 9, 2025 · 7 comments
Assignees
Labels
IR difference A difference in ClangIR-generated LLVM IR that could complicate reusing original CodeGen tests

Comments

@bcardosolopes
Copy link
Member

There are some differences from the OG LLVM for unions for C++ code...

The empty union in initialized with undef values in OG and with zeroinitializer in CIR:

union U { int a; long long b;};
union U u = {};
// OG
@u = dso_local global { i32, [4 x i8] } { i32 0, [4 x i8] undef }, align 8

// CIR enabled
@u = global { i32, [4 x i8] } zeroinitializer, align  8

This is a difference - and soon or later we may want to fix it.

Originally posted by @gitoleg in #1257 (comment)

@bcardosolopes bcardosolopes added the IR difference A difference in ClangIR-generated LLVM IR that could complicate reusing original CodeGen tests label Jan 9, 2025
@beamandala
Copy link

Hi @bcardosolopes, I believe the discrepancy is because of this block (lines 1203 - 1214) in CIRGenModule.cpp.

I'm new to ClangIR and was wondering if this was a good issue to start contributing on.

@bcardosolopes
Copy link
Member Author

I'm new to ClangIR and was wondering if this was a good issue to start contributing on.

Yep, assigning this to you! Thanks

@beamandala
Copy link

beamandala commented Feb 24, 2025

Hi, I've been looking into this and trying out different changes and have been trying to modify this function to handle unions in a different way but it isn't working as expected.

Inside the if statement checking if T is zero initializable, I added another check to see if T is a union type but this check is always evaluating to false and I'm not sure why (I'm running on the code in the issue).

if (T->isUnionType())

Do you have any idea why this is happening and what I can do to fix it?

@bcardosolopes
Copy link
Member Author

this check is always evaluating to false and I'm not sure why

I recommend running the example with clang under a debugger!

@beamandala
Copy link

I've ran it using the debugger but I'm not sure how to determine where the issue is. How can I get details about the T which is a QualType obj and determine what it is since isUnionType() is returning false? Printing doesn't returning anything helpful so I was wondering if you had any tips.

(lldb) p T
(clang::QualType) {
  Value = {
    Value = (Data = "\U00000010\xe9\x85(\U00000001")
  }
}

@smeenai
Copy link
Collaborator

smeenai commented Feb 26, 2025

Many LLVM and Clang types have a dump function which prints useful information about them. p T.dump() should work here.

@beamandala
Copy link

Thank you! That gave me some more information. It looks like by the time it gets to CIRGenModule::emitNullConstant, it's working on initializing the union's fields which is why T->isUnionType() is returning false. I'll look into it more to see where I can better address this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IR difference A difference in ClangIR-generated LLVM IR that could complicate reusing original CodeGen tests
Projects
None yet
Development

No branches or pull requests

3 participants