-
Notifications
You must be signed in to change notification settings - Fork 156
[CIR][CodeGen] Introduce CIR CXXSpecialMember attribute #1711
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
base: main
Are you sure you want to change the base?
Changes from all commits
8c7235c
904732f
d4c51f3
c3480fd
f10b62f
15a4a12
f6aaa4d
5de2d7a
0a8fd5b
ffe23ba
d6c0f3a
db31493
c40a840
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 |
---|---|---|
|
@@ -1351,6 +1351,62 @@ def GlobalDtorAttr : CIR_GlobalCtorDtor<"Dtor", "dtor", | |
"A function with this attribute excutes before module unloading" | ||
>; | ||
|
||
def CIR_CtorKind : CIR_I32EnumAttr<"CtorKind", "CXX Constructor Kind", [ | ||
I32EnumAttrCase<"Custom", 0, "custom">, | ||
I32EnumAttrCase<"Default", 1, "default">, | ||
I32EnumAttrCase<"Copy", 2, "copy">, | ||
]> { | ||
let genSpecializedAttr = 0; | ||
} | ||
|
||
def CIR_CXXCtorAttr | ||
: CIR_Attr<"CXXCtor", "cxx_ctor"> { | ||
let summary = "Marks a function as a CXX constructor"; | ||
let description = [{ | ||
Functions with this attribute are CXX constructors. | ||
xlauko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
The `custom` kind is used if the constructor is a custom constructor. | ||
The `default` kind is used if the constructor is a default constructor. | ||
The `copy` kind is used if the constructor is a copy constructor. | ||
}]; | ||
let parameters = (ins "mlir::Type":$type, | ||
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. Why do you need If we want to tag that this is member function from specific class it might make more sense to be more generic and have attribute Consequently these ctor/dtor attributes will be just enum attribute and unit attribute respectively. @bcardosolopes what do you think? 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.
The idea is to have the type around for grabbing any extra information we might need about the class (versus making assumptions on the type of the function
That's the idea moving forward, but starting off with ctor/dtor and go incrementally.
I like the idea of a |
||
EnumParameter<CIR_CtorKind>:$ctorKind); | ||
|
||
let assemblyFormat = [{ | ||
`<` $type `,` $ctorKind `>` | ||
}]; | ||
bcardosolopes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Printing and parsing also available in CIRDialect.cpp | ||
|
||
let builders = | ||
[AttrBuilderWithInferredContext<(ins "mlir::Type":$type, | ||
CArg<"CtorKind", "cir::CtorKind::Custom">:$ctorKind), [{ | ||
return $_get(type.getContext(), type, ctorKind); | ||
}]>]; | ||
} | ||
|
||
def CIR_CXXDtorAttr | ||
: CIR_Attr<"CXXDtor", "cxx_dtor"> { | ||
let summary = "Marks a function as a CXX destructor"; | ||
let description = [{ | ||
Functions with this attribute are CXX destructors | ||
}]; | ||
let parameters = (ins "mlir::Type":$type); | ||
|
||
let assemblyFormat = [{ | ||
`<` $type `>` | ||
}]; | ||
// Printing and parsing also available in CIRDialect.cpp | ||
|
||
let builders = | ||
[AttrBuilderWithInferredContext<(ins "mlir::Type":$type), [{ | ||
return $_get(type.getContext(), type); | ||
}]>]; | ||
} | ||
bcardosolopes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def CIR_CXXSpecialMemberAttr : AnyAttrOf<[ | ||
CIR_CXXCtorAttr, | ||
CIR_CXXDtorAttr | ||
]>; | ||
|
||
def BitfieldInfoAttr : CIR_Attr<"BitfieldInfo", "bitfield_info"> { | ||
let summary = "Represents a bit field info"; | ||
let description = [{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -172,6 +172,7 @@ REGISTER_ENUM_TYPE(GlobalLinkageKind); | |
REGISTER_ENUM_TYPE(VisibilityKind); | ||
REGISTER_ENUM_TYPE(CallingConv); | ||
REGISTER_ENUM_TYPE(SideEffect); | ||
REGISTER_ENUM_TYPE(CtorKind); | ||
} // namespace | ||
|
||
/// Parse an enum from the keyword, or default to the provided default value. | ||
|
@@ -2538,6 +2539,7 @@ ParseResult cir::FuncOp::parse(OpAsmParser &parser, OperationState &state) { | |
auto visibilityNameAttr = getGlobalVisibilityAttrName(state.name); | ||
auto dsoLocalNameAttr = getDsoLocalAttrName(state.name); | ||
auto annotationsNameAttr = getAnnotationsAttrName(state.name); | ||
auto cxxSpecialMemberAttr = getCxxSpecialMemberAttrName(state.name); | ||
if (::mlir::succeeded(parser.parseOptionalKeyword(builtinNameAttr.strref()))) | ||
state.addAttribute(builtinNameAttr, parser.getBuilder().getUnitAttr()); | ||
if (::mlir::succeeded( | ||
|
@@ -2618,6 +2620,37 @@ ParseResult cir::FuncOp::parse(OpAsmParser &parser, OperationState &state) { | |
state.addAttribute(annotationsNameAttr, annotations); | ||
} | ||
|
||
// Parse CXXSpecialMember attribute | ||
if (mlir::succeeded(parser.parseOptionalKeyword("cxx_ctor"))) { | ||
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. Still sounds simple enough that we shouldn't need these to be hand written, what specific problems are you hitting? 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. I can update this once the syntax of the attribute is approved. I am also happy with 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. Thanks! I'll defer this to @xlauko cause I'm going to be out until next week. One important part here is not to have a custom parser/printer because this is simple enough that it doesn't need one (see more examples in the LLVM dialect for inspiration). |
||
if (parser.parseLess().failed()) | ||
return failure(); | ||
mlir::Type type; | ||
if (parser.parseType(type).failed()) | ||
return failure(); | ||
if (parser.parseComma().failed()) | ||
return failure(); | ||
cir::CtorKind ctorKind; | ||
if (parseCIRKeyword<cir::CtorKind>(parser, ctorKind).failed()) | ||
return failure(); | ||
if (parser.parseGreater().failed()) | ||
return failure(); | ||
|
||
state.addAttribute(cxxSpecialMemberAttr, | ||
cir::CXXCtorAttr::get(type, ctorKind)); | ||
} | ||
|
||
if (mlir::succeeded(parser.parseOptionalKeyword("cxx_dtor"))) { | ||
if (parser.parseLess().failed()) | ||
return failure(); | ||
mlir::Type type; | ||
if (parser.parseType(type).failed()) | ||
return failure(); | ||
if (parser.parseGreater().failed()) | ||
return failure(); | ||
|
||
state.addAttribute(cxxSpecialMemberAttr, cir::CXXDtorAttr::get(type)); | ||
} | ||
|
||
// If additional attributes are present, parse them. | ||
if (parser.parseOptionalAttrDictWithKeyword(state.attributes)) | ||
return failure(); | ||
|
@@ -2798,6 +2831,19 @@ void cir::FuncOp::print(OpAsmPrinter &p) { | |
p.printAttribute(annotations); | ||
} | ||
|
||
if (getCxxSpecialMember()) { | ||
if (auto cxxCtor = dyn_cast<cir::CXXCtorAttr>(*getCxxSpecialMember())) { | ||
if (cxxCtor.getCtorKind() != cir::CtorKind::Custom) | ||
p << " cxx_ctor<" << cxxCtor.getType() << ", " << cxxCtor.getCtorKind() | ||
<< ">"; | ||
} else if (auto cxxDtor = | ||
dyn_cast<cir::CXXDtorAttr>(*getCxxSpecialMember())) { | ||
p << " cxx_dtor<" << cxxDtor.getType() << ">"; | ||
} else { | ||
assert(false && "expected a CXX special member"); | ||
} | ||
} | ||
|
||
function_interface_impl::printFunctionAttributes( | ||
p, *this, | ||
// These are all omitted since they are custom printed already. | ||
|
@@ -2808,7 +2854,7 @@ void cir::FuncOp::print(OpAsmPrinter &p) { | |
getCallingConvAttrName(), getNoProtoAttrName(), | ||
getSymVisibilityAttrName(), getArgAttrsAttrName(), getResAttrsAttrName(), | ||
getComdatAttrName(), getGlobalVisibilityAttrName(), | ||
getAnnotationsAttrName()}); | ||
getAnnotationsAttrName(), getCxxSpecialMemberAttrName()}); | ||
|
||
if (auto aliaseeName = getAliasee()) { | ||
p << " alias("; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,9 +14,9 @@ void f() { | |
!E(); | ||
} | ||
|
||
// CIR: cir.func private @_ZN1EC1Ev(!cir.ptr<!rec_E>) extra(#fn_attr) | ||
// CIR: cir.func private @_ZN1EC1Ev(!cir.ptr<!rec_E>) cxx_ctor<!rec_E, default> extra(#fn_attr) | ||
// CIR-NEXT: cir.func private @_ZN1EntEv(!cir.ptr<!rec_E>) -> !rec_E | ||
// CIR-NEXT: cir.func private @_ZN1ED1Ev(!cir.ptr<!rec_E>) extra(#fn_attr) | ||
// CIR-NEXT: cir.func private @_ZN1ED1Ev(!cir.ptr<!rec_E>) cxx_dtor<!rec_E> extra(#fn_attr) | ||
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. Sounds like we could also drop "cxx": |
||
// CIR-NEXT: cir.func dso_local @_Z1fv() extra(#fn_attr1) { | ||
// CIR-NEXT: cir.scope { | ||
// CIR-NEXT: %[[ONE:[0-9]+]] = cir.alloca !rec_E, !cir.ptr<!rec_E>, ["agg.tmp.ensured"] {alignment = 1 : i64} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// RUN: cir-opt %s -o %t.cir | ||
// RUN: FileCheck --input-file=%t.cir %s | ||
|
||
!s32i = !cir.int<s, 32> | ||
!rec_S = !cir.record<struct "S" {!s32i}> | ||
module { | ||
cir.func private @_ZN1SC1ERKS_(!cir.ptr<!rec_S>, !cir.ptr<!rec_S>) cxx_ctor<!rec_S, copy> | ||
cir.func private @_ZN1SC2Ei(!cir.ptr<!rec_S>, !cir.ptr<!rec_S>) | ||
cir.func private @_ZN1SC2Ev(!cir.ptr<!rec_S>) cxx_ctor<!rec_S, default> | ||
cir.func private @_ZN1SD2Ev(!cir.ptr<!rec_S>) cxx_dtor<!rec_S> | ||
} | ||
|
||
// CHECK: !s32i = !cir.int<s, 32> | ||
// CHECK: !rec_S = !cir.record<struct "S" {!s32i}> | ||
// CHECK: module { | ||
// CHECK: cir.func private @_ZN1SC1ERKS_(!cir.ptr<!rec_S>, !cir.ptr<!rec_S>) cxx_ctor<!rec_S, copy> | ||
// CHECK: cir.func private @_ZN1SC2Ei(!cir.ptr<!rec_S>, !cir.ptr<!rec_S>) | ||
// CHECK: cir.func private @_ZN1SC2Ev(!cir.ptr<!rec_S>) cxx_ctor<!rec_S, default> | ||
// CHECK: cir.func private @_ZN1SD2Ev(!cir.ptr<!rec_S>) cxx_dtor<!rec_S> | ||
// CHECK: } |
Uh oh!
There was an error while loading. Please reload this page.