-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[CIR] Add undef handling to enable global lambdas #169721
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -240,7 +240,7 @@ class CIRAttrToValue { | |
| .Case<cir::IntAttr, cir::FPAttr, cir::ConstComplexAttr, | ||
| cir::ConstArrayAttr, cir::ConstRecordAttr, cir::ConstVectorAttr, | ||
| cir::ConstPtrAttr, cir::GlobalViewAttr, cir::TypeInfoAttr, | ||
| cir::VTableAttr, cir::ZeroAttr>( | ||
| cir::UndefAttr, cir::VTableAttr, cir::ZeroAttr>( | ||
| [&](auto attrT) { return visitCirAttr(attrT); }) | ||
| .Default([&](auto attrT) { return mlir::Value(); }); | ||
| } | ||
|
|
@@ -254,6 +254,7 @@ class CIRAttrToValue { | |
| mlir::Value visitCirAttr(cir::ConstVectorAttr attr); | ||
| mlir::Value visitCirAttr(cir::GlobalViewAttr attr); | ||
| mlir::Value visitCirAttr(cir::TypeInfoAttr attr); | ||
| mlir::Value visitCirAttr(cir::UndefAttr attr); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This list could/should probably be tablegen'ed/macro'ed too (in a future patch). |
||
| mlir::Value visitCirAttr(cir::VTableAttr attr); | ||
| mlir::Value visitCirAttr(cir::ZeroAttr attr); | ||
|
|
||
|
|
@@ -591,6 +592,13 @@ mlir::Value CIRAttrToValue::visitCirAttr(cir::TypeInfoAttr typeInfoAttr) { | |
| return result; | ||
| } | ||
|
|
||
| /// UndefAttr visitor. | ||
| mlir::Value CIRAttrToValue::visitCirAttr(cir::UndefAttr undefAttr) { | ||
| mlir::Location loc = parentOp->getLoc(); | ||
erichkeane marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return mlir::LLVM::UndefOp::create( | ||
| rewriter, loc, converter->convertType(undefAttr.getType())); | ||
| } | ||
|
|
||
| // VTableAttr visitor. | ||
| mlir::Value CIRAttrToValue::visitCirAttr(cir::VTableAttr vtableArr) { | ||
| mlir::Type llvmTy = converter->convertType(vtableArr.getType()); | ||
|
|
@@ -2046,9 +2054,11 @@ CIRToLLVMGlobalOpLowering::matchAndRewriteRegionInitializedGlobal( | |
| cir::GlobalOp op, mlir::Attribute init, | ||
| mlir::ConversionPatternRewriter &rewriter) const { | ||
| // TODO: Generalize this handling when more types are needed here. | ||
| assert((isa<cir::ConstArrayAttr, cir::ConstRecordAttr, cir::ConstVectorAttr, | ||
| cir::ConstPtrAttr, cir::ConstComplexAttr, cir::GlobalViewAttr, | ||
| cir::TypeInfoAttr, cir::VTableAttr, cir::ZeroAttr>(init))); | ||
| assert( | ||
| (isa<cir::ConstArrayAttr, cir::ConstRecordAttr, cir::ConstVectorAttr, | ||
| cir::ConstPtrAttr, cir::ConstComplexAttr, cir::GlobalViewAttr, | ||
| cir::TypeInfoAttr, cir::UndefAttr, cir::VTableAttr, cir::ZeroAttr>( | ||
| init))); | ||
|
|
||
| // TODO(cir): once LLVM's dialect has proper equivalent attributes this | ||
| // should be updated. For now, we use a custom op to initialize globals | ||
|
|
@@ -2106,8 +2116,8 @@ mlir::LogicalResult CIRToLLVMGlobalOpLowering::matchAndRewrite( | |
| } else if (mlir::isa<cir::ConstArrayAttr, cir::ConstVectorAttr, | ||
| cir::ConstRecordAttr, cir::ConstPtrAttr, | ||
| cir::ConstComplexAttr, cir::GlobalViewAttr, | ||
| cir::TypeInfoAttr, cir::VTableAttr, cir::ZeroAttr>( | ||
| init.value())) { | ||
| cir::TypeInfoAttr, cir::UndefAttr, cir::VTableAttr, | ||
| cir::ZeroAttr>(init.value())) { | ||
| // TODO(cir): once LLVM's dialect has proper equivalent attributes this | ||
| // should be updated. For now, we use a custom op to initialize globals | ||
| // to the appropriate value. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its a Little weird to me that this is a hand-typed list here, and that we don't have some sort of tablegen to get this list? Perhaps a task for a future patch :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could possibly do that in a way similar to what @Lancern did here: #159390
I'm not sure it's worth the effort though. I don't expect there to ever be a huge number of cases that need to be handled here. This doesn't cover every CIR-specific attribute, only attributes that have a reason (and a way) to be converted to a value in the LLVM dialect. If we had a tablegen for this, we'd still need to provide a
CIRAttrToValue::visitCirAttrimplementation for anything that wasn't trivially convertible (asIntAttrandFPAttrare).There are two more in the incubator that we haven't upstreamed yet, bool and poison. I was going to say that I didn't know why we aren't seeing problems related to bool being missing, but I just discovered that it's because we didn't have a test for it (bool initializers for globals are being lowered incorrectly!). I'll fix that shortly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I guess if we're 'near the end' here, that is probably fine. This just looked like the kind of list that would get bad really fast as we grew.
One thing we've done in the past is have the declaration of things like
visitCirAttrtablegen'ed, and leave it to the linker to let you know when you've forgotten that. But the lack of compile-time errors on forgetting something like this in a list is unfortunate.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On closer inspection, the problem with boolean global initializers is something in the LLVM dialect to LLVM IR translation for the constant. We don't appear to be hitting this visitor for BoolAttr in any case I've found. Maybe it isn't needed.