-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[CIR] Lowering to LLVM for global pointers #125619
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| #ifndef LLVM_CLANG_CIR_DIALECT_IR_CIRATTRVISITOR_H | ||
| #define LLVM_CLANG_CIR_DIALECT_IR_CIRATTRVISITOR_H | ||
|
|
||
| #include "clang/CIR/Dialect/IR/CIRAttrs.h" | ||
|
|
||
| namespace cir { | ||
|
|
||
| template <typename ImplClass, typename RetTy> class CirAttrVisitor { | ||
| public: | ||
| // FIXME: Create a TableGen list to automatically handle new attributes | ||
| template <typename... Args> | ||
| RetTy visit(mlir::Attribute attr, Args &&...args) { | ||
|
||
| if (const auto intAttr = mlir::dyn_cast<cir::IntAttr>(attr)) | ||
| return static_cast<ImplClass *>(this)->visitCirIntAttr( | ||
|
||
| intAttr, std::forward<Args>(args)...); | ||
| if (const auto fltAttr = mlir::dyn_cast<cir::FPAttr>(attr)) | ||
| return static_cast<ImplClass *>(this)->visitCirFPAttr( | ||
| fltAttr, std::forward<Args>(args)...); | ||
| if (const auto ptrAttr = mlir::dyn_cast<cir::ConstPtrAttr>(attr)) | ||
| return static_cast<ImplClass *>(this)->visitCirConstPtrAttr( | ||
| ptrAttr, std::forward<Args>(args)...); | ||
| llvm_unreachable("unhandled attribute type"); | ||
| } | ||
|
|
||
| // If the implementation chooses not to implement a certain visit | ||
| // method, fall back to the parent. | ||
| template <typename... Args> | ||
| RetTy visitCirIntAttr(cir::IntAttr attr, Args &&...args) { | ||
| return static_cast<ImplClass *>(this)->visitCirAttr( | ||
erichkeane marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| attr, std::forward<Args>(args)...); | ||
| } | ||
| template <typename... Args> | ||
| RetTy visitCirFPAttr(cir::FPAttr attr, Args &&...args) { | ||
| return static_cast<ImplClass *>(this)->visitCirAttr( | ||
| attr, std::forward<Args>(args)...); | ||
| } | ||
| template <typename... Args> | ||
| RetTy visitCirConstPtrAttr(cir::ConstPtrAttr attr, Args &&...args) { | ||
| return static_cast<ImplClass *>(this)->visitCirAttr( | ||
| attr, std::forward<Args>(args)...); | ||
| } | ||
|
|
||
| template <typename... Args> | ||
| RetTy visitCirAttr(mlir::Attribute attr, Args &&...args) { | ||
| return RetTy(); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace cir | ||
|
|
||
| #endif // LLVM_CLANG_CIR_DIALECT_IR_CIRATTRVISITOR_H | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -24,6 +24,7 @@ | |||||
| #include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h" | ||||||
| #include "mlir/Target/LLVMIR/Export.h" | ||||||
| #include "mlir/Transforms/DialectConversion.h" | ||||||
| #include "clang/CIR/Dialect/IR/CIRAttrVisitor.h" | ||||||
| #include "clang/CIR/Dialect/IR/CIRDialect.h" | ||||||
| #include "clang/CIR/MissingFeatures.h" | ||||||
| #include "llvm/IR/Module.h" | ||||||
|
|
@@ -35,6 +36,54 @@ using namespace llvm; | |||||
| namespace cir { | ||||||
| namespace direct { | ||||||
|
|
||||||
| class CIRAttrToValue : public CirAttrVisitor<CIRAttrToValue, mlir::Value> { | ||||||
| public: | ||||||
| mlir::Value lowerCirAttrAsValue(mlir::Operation *parentOp, | ||||||
| mlir::Attribute attr, | ||||||
| mlir::ConversionPatternRewriter &rewriter, | ||||||
| const mlir::TypeConverter *converter, | ||||||
| mlir::DataLayout const &dataLayout) { | ||||||
| return visit(attr, parentOp, rewriter, converter, dataLayout); | ||||||
| } | ||||||
|
|
||||||
| mlir::Value visitCirIntAttr(cir::IntAttr intAttr, mlir::Operation *parentOp, | ||||||
| mlir::ConversionPatternRewriter &rewriter, | ||||||
|
||||||
| const mlir::TypeConverter *converter, | ||||||
| mlir::DataLayout const &dataLayout) { | ||||||
| auto loc = parentOp->getLoc(); | ||||||
|
||||||
| auto loc = parentOp->getLoc(); | |
| Location loc = parentOp->getLoc(); |
Outdated
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.
I don't believe this passes our use of auto policy here. Unless the type is named Loc :)
EDIT: Looked, it isnt:
| const auto loc = op.getLoc(); | |
| const Location loc = op.getLoc(); |
Outdated
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.
I think I'd prefer this get extracted to a function in an anonymous namespace (or a static function). It does quite a lot of work for a lambda.
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.
It was a separate function in the incubator. The reason I moved it to a lambda is that there was a lot of state information needed that would either need to be passed in as arguments or recreated. The state is per-global so it can't just be stored in the CIRToLLVMGlobalOpLowering object. It's the set of arguments that are passed to replaceOpWithNewOp()
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.
Oh! I see, I missed how much it was capturing. Can you link me to the incubator version?
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.
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.
It's not quite as bad there because everything is coming from the op object. It was worse here because of all the placeholders for things the op doesn't have yet here. Still the incubator implementation retrieves a lot of this in multiple places and only calls the function in this scope.
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, hrmph. That is unfortunately a much nicer version as a separate function. Can we have a FIXME to move this once dataLayout, typeConverter, and init.value get moved? (or whatever else needs to be moved?). It would be great if we could do that sooner, but I see what you mean.
Outdated
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.
Hmm... this already changing to make these not 'equal' in some way is causing me consternation. I realize this is the reason for an inability to skip the visitor, which I see the reasoning for here, but this function is going to QUICKLY get unreadable without some sort of better organization.
We should noodle on a way to better organize this into something that isn't going to get unwieldy. And by 'we' I believe that is a group-homework-assignment :)
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.
OK. Looking at the incubator implementation (which is a huge series of if-else-if statements), I see that there are basically two categories -- (1) types for which the init value is updated directly before falling through to the replaceOpWithNewOp() call, and (2) types for which a region-initialized global is required. I think I can restructure the code around this decision and make it much clearer.
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.
<3 Feel free to do so as a followup and not here if you'd prefer. This is more of a "we should fix this before it gets bad", not that it is bad yet.
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.
That's a good idea Andy
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.
This file probably needs our common comment header.