-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[CIR] Upstream initial support for complete record types #135844
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,54 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLVM_CLANG_LIB_CIR_CIRGENRECORDLAYOUT_H | ||
| #define LLVM_CLANG_LIB_CIR_CIRGENRECORDLAYOUT_H | ||
|
|
||
| #include "clang/AST/Decl.h" | ||
| #include "clang/CIR/Dialect/IR/CIRTypes.h" | ||
|
|
||
| namespace clang::CIRGen { | ||
|
|
||
| /// This class handles record and union layout info while lowering AST types | ||
| /// to CIR types. | ||
| /// | ||
| /// These layout objects are only created on demand as CIR generation requires. | ||
| class CIRGenRecordLayout { | ||
| friend class CIRGenTypes; | ||
|
|
||
| CIRGenRecordLayout(const CIRGenRecordLayout &) = delete; | ||
| void operator=(const CIRGenRecordLayout &) = delete; | ||
|
|
||
| private: | ||
| /// The CIR type corresponding to this record layout; used when laying it out | ||
| /// as a complete object. | ||
| cir::RecordType completeObjectType; | ||
|
|
||
| /// Map from (non-bit-field) record field to the corresponding cir record type | ||
| /// field no. This info is populated by the record builder. | ||
| llvm::DenseMap<const clang::FieldDecl *, unsigned> fieldInfo; | ||
|
|
||
| public: | ||
| CIRGenRecordLayout(cir::RecordType completeObjectType) | ||
| : completeObjectType(completeObjectType) {} | ||
|
|
||
| /// Return the "complete object" LLVM type associated with | ||
| /// this record. | ||
| cir::RecordType getCIRType() const { return completeObjectType; } | ||
|
|
||
| /// Return cir::RecordType element number that corresponds to the field FD. | ||
| unsigned getCIRFieldNo(const clang::FieldDecl *FD) const { | ||
| FD = FD->getCanonicalDecl(); | ||
| assert(fieldInfo.count(FD) && "Invalid field for record!"); | ||
| return fieldInfo.lookup(FD); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace clang::CIRGen | ||
|
|
||
| #endif |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,212 @@ | ||||||
| //===----------------------------------------------------------------------===// | ||||||
| // | ||||||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||||
| // See https://llvm.org/LICENSE.txt for license information. | ||||||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||||
| // | ||||||
| //===----------------------------------------------------------------------===// | ||||||
| // | ||||||
| // This contains code to compute the layout of a record. | ||||||
| // | ||||||
| //===----------------------------------------------------------------------===// | ||||||
|
|
||||||
| #include "CIRGenBuilder.h" | ||||||
| #include "CIRGenModule.h" | ||||||
| #include "CIRGenTypes.h" | ||||||
|
|
||||||
| #include "clang/AST/ASTContext.h" | ||||||
| #include "clang/AST/Decl.h" | ||||||
| #include "clang/AST/DeclCXX.h" | ||||||
| #include "clang/AST/RecordLayout.h" | ||||||
| #include "clang/CIR/Dialect/IR/CIRAttrs.h" | ||||||
| #include "llvm/Support/Casting.h" | ||||||
|
|
||||||
| #include <memory> | ||||||
|
|
||||||
| using namespace llvm; | ||||||
| using namespace clang; | ||||||
| using namespace clang::CIRGen; | ||||||
|
|
||||||
| namespace { | ||||||
| /// The CIRRecordLowering is responsible for lowering an ASTRecordLayout to an | ||||||
| /// mlir::Type. Some of the lowering is straightforward, some is not. | ||||||
| // TODO: Detail some of the complexities and weirdnesses? | ||||||
| // (See CGRecordLayoutBuilder.cpp) | ||||||
| struct CIRRecordLowering final { | ||||||
|
|
||||||
| // MemberInfo is a helper structure that contains information about a record | ||||||
| // member. In addition to the standard member types, there exists a sentinel | ||||||
| // member type that ensures correct rounding. | ||||||
| struct MemberInfo final { | ||||||
| CharUnits offset; | ||||||
| enum class InfoKind { Field } kind; | ||||||
| mlir::Type data; | ||||||
| union { | ||||||
| const FieldDecl *fieldDecl; | ||||||
| // CXXRecordDecl will be used here when supported. | ||||||
|
||||||
| // CXXRecordDecl will be used here when supported. | |
| // CXXRecordDecl will be used here when base-types are supported. |
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.
See this suggestion/change request here.
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.
These can't be bool type, they all have to be unsigned, else they get laid-out funny/inconsistently. However, you can/should use LLVM_PREFERRED_TYPE(bool) on them.
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.
Both the incubator and the classic codegen implementation declare them as bool. I did notice that I have to explicitly cast them as bool to access them, which seemed kind of odd. Is that a symptom of what you're talking about?
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.
Thats a symptom of them being bitfields. But yeah, bool-bitfields are problematic for some ABIs (only sequential bitfields of the same type can be 'joined', and at least 1 ABI will only do so for unsigned and int IIRC). So we use unsigned for them every time we are thinking about it during review. I'm guessing this is something in the classic-codegen that we never noticed.
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.
Should we also NYI on base types? Particularly with virtual base types this gets ugly.
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 cut a lot of code out from here. Anything other than unions will fail somewhere else before we get here, but I guess it wouldn't hurt to add more checks here now.
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.
maybe add an assert on missing feature here? this will lead to LLVM IR lowering differences so might be good to track.
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.
NIT
if (auto recordTy = mlir::dyn_cast<cir::RecordType>(ty))