-
Notifications
You must be signed in to change notification settings - Fork 15.5k
[CIR] Add CIR vtable attribute #154415
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
[CIR] Add CIR vtable attribute #154415
Changes from 1 commit
f5e0612
d37efd2
98c808f
32180c9
1736a32
6f96418
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 | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -496,6 +496,46 @@ def CIR_GlobalViewAttr : CIR_Attr<"GlobalView", "global_view", [ | |||||||||||||
| }]; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| //===----------------------------------------------------------------------===// | ||||||||||||||
| // VTableAttr | ||||||||||||||
| //===----------------------------------------------------------------------===// | ||||||||||||||
|
|
||||||||||||||
| def VTableAttr : CIR_Attr<"VTable", "vtable", [TypedAttrInterface]> { | ||||||||||||||
| let summary = "Represents a C++ vtable"; | ||||||||||||||
| let description = [{ | ||||||||||||||
| Wraps a #cir.const_record containing vtable data. | ||||||||||||||
|
||||||||||||||
|
|
||||||||||||||
| Example: | ||||||||||||||
| ``` | ||||||||||||||
| cir.global linkonce_odr @_ZTV1B = #cir.vtable<< | ||||||||||||||
|
||||||||||||||
| {#cir.const_array<[#cir.null : !cir.ptr<i8>, | ||||||||||||||
| #cir.global_view<@_ZTI1B> : !cir.ptr<i8>, | ||||||||||||||
| #cir.global_view<@_ZN1BD1Ev> : !cir.ptr<i8>, | ||||||||||||||
| #cir.global_view<@_ZN1BD0Ev> : !cir.ptr<i8>, | ||||||||||||||
| #cir.global_view<@_ZNK1A5quackEv> : !cir.ptr<i8>]> | ||||||||||||||
| : !cir.array<!cir.ptr<i8> x 5>}>> | ||||||||||||||
| : !cir.record<"", !cir.array<!cir.ptr<i8> x 5>> | ||||||||||||||
| ``` | ||||||||||||||
| }]; | ||||||||||||||
|
|
||||||||||||||
| // `vtable_data` is a const record with one element, containing an array of | ||||||||||||||
|
||||||||||||||
| // vtable information. | ||||||||||||||
| let parameters = (ins AttributeSelfTypeParameter<"">:$type, | ||||||||||||||
| "mlir::ArrayAttr":$vtable_data); | ||||||||||||||
|
||||||||||||||
| let parameters = (ins AttributeSelfTypeParameter<"">:$type, | |
| "mlir::ArrayAttr":$vtable_data); | |
| let parameters = (ins | |
| AttributeSelfTypeParameter<"">:$type, | |
| "mlir::ArrayAttr":$vtable_data | |
| ); |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -424,6 +424,44 @@ cir::ConstVectorAttr::verify(function_ref<InFlightDiagnostic()> emitError, | |||||
| return elementTypeCheck; | ||||||
| } | ||||||
|
|
||||||
| //===----------------------------------------------------------------------===// | ||||||
| // CIR VTableAttr | ||||||
| //===----------------------------------------------------------------------===// | ||||||
|
|
||||||
| LogicalResult cir::VTableAttr::verify( | ||||||
| llvm::function_ref<mlir::InFlightDiagnostic()> emitError, mlir::Type type, | ||||||
| mlir::ArrayAttr vtableData) { | ||||||
| auto sTy = mlir::dyn_cast_if_present<cir::RecordType>(type); | ||||||
| if (!sTy) | ||||||
| return emitError() << "expected !cir.record type result"; | ||||||
| if (sTy.getMembers().empty() || vtableData.empty()) | ||||||
| return emitError() << "expected record type with one or more subtype"; | ||||||
|
|
||||||
| for (size_t i = 0; i < sTy.getMembers().size(); ++i) { | ||||||
|
||||||
| auto constArrayAttr = mlir::dyn_cast<cir::ConstArrayAttr>(vtableData[i]); | ||||||
| if (!constArrayAttr) | ||||||
| return emitError() << "expected constant array subtype"; | ||||||
|
|
||||||
| if (cir::ConstRecordAttr::verify(emitError, type, vtableData).failed()) | ||||||
| return failure(); | ||||||
|
|
||||||
| LogicalResult eltTypeCheck = success(); | ||||||
| auto arrayElts = mlir::cast<ArrayAttr>(constArrayAttr.getElts()); | ||||||
| arrayElts.walkImmediateSubElements( | ||||||
| [&](Attribute attr) { | ||||||
|
||||||
| [&](Attribute attr) { | |
| [&](mlir::Attribute attr) { |
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.
| [&](Type type) {}); | |
| [&](mlir::Type type) {}); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // RUN: cir-opt %s | FileCheck %s | ||
|
|
||
| !rec_S = !cir.record<struct "S" {!cir.vptr}> | ||
| !u8i = !cir.int<u, 8> | ||
| !rec_anon_struct = !cir.record<struct {!cir.array<!cir.ptr<!u8i> x 4>}> | ||
| module { | ||
| cir.global external @_ZTV1S = #cir.vtable<{#cir.const_array<[#cir.ptr<null> : !cir.ptr<!u8i>, #cir.ptr<null> : !cir.ptr<!u8i>, #cir.global_view<@_ZN1S3keyEv> : !cir.ptr<!u8i>, #cir.global_view<@_ZN1S6nonKeyEv> : !cir.ptr<!u8i>]> : !cir.array<!cir.ptr<!u8i> x 4>}> : !rec_anon_struct {alignment = 8 : i64} | ||
| // CHECK: cir.global external @_ZTV1S = #cir.vtable<{#cir.const_array<[#cir.ptr<null> : !cir.ptr<!u8i>, #cir.ptr<null> : !cir.ptr<!u8i>, #cir.global_view<@_ZN1S3keyEv> : !cir.ptr<!u8i>, #cir.global_view<@_ZN1S6nonKeyEv> : !cir.ptr<!u8i>]> : !cir.array<!cir.ptr<!u8i> x 4>}> : !rec_anon_struct {alignment = 8 : i64} | ||
|
|
||
| cir.func private dso_local @_ZN1S3keyEv(%arg0: !cir.ptr<!rec_S>) | ||
| cir.func private dso_local @_ZN1S6nonKeyEv(%arg0: !cir.ptr<!rec_S>) | ||
| } |
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.