Skip to content

[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

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
Original file line number Diff line number Diff line change
Expand Up @@ -1260,8 +1260,7 @@ def CIR_CtorKind : CIR_I32EnumAttr<"CtorKind", "CXX Constructor Kind", [
let genSpecializedAttr = 0;
}

def CIR_CXXCtorAttr
: CIR_Attr<"CXXCtor", "cxx_ctor"> {
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.
Expand All @@ -1275,17 +1274,16 @@ def CIR_CXXCtorAttr
let assemblyFormat = [{
`<` $type `,` $ctorKind `>`
}];
// Printing and parsing also available in CIRDialect.cpp

let builders =
[AttrBuilderWithInferredContext<(ins "mlir::Type":$type,
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"> {
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
Expand All @@ -1295,12 +1293,12 @@ def CIR_CXXDtorAttr
let assemblyFormat = [{
`<` $type `>`
}];
// Printing and parsing also available in CIRDialect.cpp

let builders =
[AttrBuilderWithInferredContext<(ins "mlir::Type":$type), [{
let builders = [
AttrBuilderWithInferredContext<(ins "mlir::Type":$type), [{
return $_get(type.getContext(), type);
}]>];
}]>
];
}

def CIR_CXXSpecialMemberAttr : AnyAttrOf<[
Expand Down
42 changes: 13 additions & 29 deletions clang/lib/CIR/Dialect/IR/CIRDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ 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.
Expand Down Expand Up @@ -2612,34 +2611,17 @@ ParseResult cir::FuncOp::parse(OpAsmParser &parser, OperationState &state) {
}

// Parse CXXSpecialMember attribute
if (mlir::succeeded(parser.parseOptionalKeyword("cxx_ctor"))) {
if (parser.parseOptionalKeyword("special_member").succeeded()) {
cir::CXXCtorAttr ctorAttr;
cir::CXXDtorAttr dtorAttr;
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 (auto oa = parser.parseOptionalAttribute(ctorAttr); oa.has_value())
state.addAttribute(cxxSpecialMemberAttr, ctorAttr);
if (auto oa = parser.parseOptionalAttribute(dtorAttr); oa.has_value())
state.addAttribute(cxxSpecialMemberAttr, dtorAttr);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (auto oa = parser.parseOptionalAttribute(ctorAttr); oa.has_value())
state.addAttribute(cxxSpecialMemberAttr, ctorAttr);
if (auto oa = parser.parseOptionalAttribute(dtorAttr); oa.has_value())
state.addAttribute(cxxSpecialMemberAttr, dtorAttr);
if (auto oa = parser.parseOptionalAttribute(ctorAttr))
state.addAttribute(cxxSpecialMemberAttr, ctorAttr);
if (auto oa = parser.parseOptionalAttribute(dtorAttr))
state.addAttribute(cxxSpecialMemberAttr, dtorAttr);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parser.parseOptionalAttribute returns mlir::OptionalParseResult, so the oa.has_value() part is necessary to make the condition boolean.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are not using oa, so might as well do if (parser.parseOptionalAttribute(dtorAttr).has_value())?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are not using oa, so might as well do if (parser.parseOptionalAttribute(dtorAttr).has_value())?

right, updated!

if (parser.parseGreater().failed())
return failure();

state.addAttribute(cxxSpecialMemberAttr, cir::CXXDtorAttr::get(type));
}

// If additional attributes are present, parse them.
Expand Down Expand Up @@ -2824,12 +2806,14 @@ void cir::FuncOp::print(OpAsmPrinter &p) {

if (getCxxSpecialMember()) {
if (auto cxxCtor = dyn_cast<cir::CXXCtorAttr>(*getCxxSpecialMember())) {
if (cxxCtor.getCtorKind() != cir::CtorKind::Custom)
p << " cxx_ctor<" << cxxCtor.getType() << ", " << cxxCtor.getCtorKind()
<< ">";
p << " special_member<";
p.printAttribute(cxxCtor);
p << '>';
} else if (auto cxxDtor =
dyn_cast<cir::CXXDtorAttr>(*getCxxSpecialMember())) {
p << " cxx_dtor<" << cxxDtor.getType() << ">";
p << " special_member<";
p.printAttribute(cxxDtor);
p << '>';
} else {
assert(false && "expected a CXX special member");
}
Expand Down
2 changes: 1 addition & 1 deletion clang/test/CIR/CodeGen/ctor-alias.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ B::B() {
// CHECK: %1 = cir.load %0 : !cir.ptr<!cir.ptr<!rec_B>>, !cir.ptr<!rec_B>
// CHECK: cir.return
// CHECK: }
// CHECK: cir.func private dso_local @_ZN1BC1Ev(!cir.ptr<!rec_B>) cxx_ctor<!rec_B, default> alias(@_ZN1BC2Ev)
// CHECK: cir.func private dso_local @_ZN1BC1Ev(!cir.ptr<!rec_B>) special_member<#cir.cxx_ctor<!rec_B, default>> alias(@_ZN1BC2Ev)
4 changes: 2 additions & 2 deletions clang/test/CIR/CodeGen/static.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static Init __ioinit2(false);

// BEFORE: module {{.*}} {
// BEFORE-NEXT: cir.func private @_ZN4InitC1Eb(!cir.ptr<!rec_Init>, !cir.bool)
// BEFORE-NEXT: cir.func private @_ZN4InitD1Ev(!cir.ptr<!rec_Init>) cxx_dtor<!rec_Init>
// BEFORE-NEXT: cir.func private @_ZN4InitD1Ev(!cir.ptr<!rec_Init>) special_member<#cir.cxx_dtor<!rec_Init>>
// BEFORE-NEXT: cir.global "private" internal dso_local @_ZL8__ioinit = ctor : !rec_Init {
// BEFORE-NEXT: %0 = cir.get_global @_ZL8__ioinit : !cir.ptr<!rec_Init>
// BEFORE-NEXT: %1 = cir.const #true
Expand All @@ -42,7 +42,7 @@ static Init __ioinit2(false);
// AFTER-NEXT: cir.global "private" external @__dso_handle : i8
// AFTER-NEXT: cir.func private @__cxa_atexit(!cir.ptr<!cir.func<(!cir.ptr<!void>)>>, !cir.ptr<!void>, !cir.ptr<i8>)
// AFTER-NEXT: cir.func private @_ZN4InitC1Eb(!cir.ptr<!rec_Init>, !cir.bool)
// AFTER-NEXT: cir.func private @_ZN4InitD1Ev(!cir.ptr<!rec_Init>) cxx_dtor<!rec_Init>
// AFTER-NEXT: cir.func private @_ZN4InitD1Ev(!cir.ptr<!rec_Init>) special_member<#cir.cxx_dtor<!rec_Init>>
// AFTER-NEXT: cir.global "private" internal dso_local @_ZL8__ioinit = #cir.zero : !rec_Init {alignment = 1 : i64, ast = #cir.var.decl.ast}
// AFTER-NEXT: cir.func internal private @__cxx_global_var_init()
// AFTER-NEXT: %0 = cir.get_global @_ZL8__ioinit : !cir.ptr<!rec_Init>
Expand Down
4 changes: 2 additions & 2 deletions clang/test/CIR/CodeGen/temporaries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ void f() {
!E();
}

// CIR: cir.func private @_ZN1EC1Ev(!cir.ptr<!rec_E>) cxx_ctor<!rec_E, default> extra(#fn_attr)
// CIR: cir.func private @_ZN1EC1Ev(!cir.ptr<!rec_E>) special_member<#cir.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>) cxx_dtor<!rec_E> extra(#fn_attr)
// CIR-NEXT: cir.func private @_ZN1ED1Ev(!cir.ptr<!rec_E>) special_member<#cir.cxx_dtor<!rec_E>> extra(#fn_attr)
// 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}
Expand Down
2 changes: 1 addition & 1 deletion clang/test/CIR/CodeGen/tempref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
struct A { ~A(); };
A &&a = dynamic_cast<A&&>(A{});

// CHECK: cir.func private @_ZN1AD1Ev(!cir.ptr<!rec_A>) cxx_dtor<!rec_A> extra(#fn_attr)
// CHECK: cir.func private @_ZN1AD1Ev(!cir.ptr<!rec_A>) special_member<#cir.cxx_dtor<!rec_A>> extra(#fn_attr)
// CHECK-NEXT: cir.global external @a = #cir.ptr<null> : !cir.ptr<!rec_A> {alignment = 8 : i64, ast = #cir.var.decl.ast}
// CHECK-NEXT: cir.func internal private @__cxx_global_var_init() {
// CHECK-NEXT: cir.scope {
Expand Down
10 changes: 4 additions & 6 deletions clang/test/CIR/CodeGen/virtual-destructor-calls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ struct B : A {
// LLVM: call void @_ZN1AD2Ev

// Complete dtor: just an alias because there are no virtual bases.
// CIR: cir.func private dso_local @_ZN1BD1Ev(!cir.ptr<!rec_B>) cxx_dtor<!rec_B> alias(@_ZN1BD2Ev)
// FIXME: LLVM output should be: @_ZN1BD1Ev ={{.*}} unnamed_addr alias {{.*}} @_ZN1BD2Ev
// LLVM: declare dso_local void @_ZN1BD1Ev(ptr)
// CIR: cir.func private dso_local @_ZN1BD1Ev(!cir.ptr<!rec_B>) special_member<#cir.cxx_dtor<!rec_B>> alias(@_ZN1BD2Ev)

// Deleting dtor: defers to the complete dtor.
// LLVM: define{{.*}} void @_ZN1BD0Ev(ptr
Expand All @@ -48,11 +46,11 @@ struct B : A {

// (aliases from C)
// CIR: cir.func dso_local @_ZN1CD2Ev(%arg0: !cir.ptr<!rec_C>{{.*}})) {{.*}} {
// CIR: cir.func private dso_local @_ZN1CD1Ev(!cir.ptr<!rec_C>) cxx_dtor<!rec_C> alias(@_ZN1CD2Ev)
// CIR: cir.func private dso_local @_ZN1CD1Ev(!cir.ptr<!rec_C>) special_member<#cir.cxx_dtor<!rec_C>> alias(@_ZN1CD2Ev)

// CIR_O1-NOT: cir.func dso_local @_ZN1CD2Ev(%arg0: !cir.ptr<!rec_C>{{.*}})) {{.*}} {
// CIR_O1: cir.func private dso_local @_ZN1CD2Ev(!cir.ptr<!rec_C>) cxx_dtor<!rec_C> alias(@_ZN1BD2Ev)
// CIR_O1: cir.func private dso_local @_ZN1CD1Ev(!cir.ptr<!rec_C>) cxx_dtor<!rec_C> alias(@_ZN1CD2Ev)
// CIR_O1: cir.func private dso_local @_ZN1CD2Ev(!cir.ptr<!rec_C>) special_member<#cir.cxx_dtor<!rec_C>> alias(@_ZN1BD2Ev)
// CIR_O1: cir.func private dso_local @_ZN1CD1Ev(!cir.ptr<!rec_C>) special_member<#cir.cxx_dtor<!rec_C>> alias(@_ZN1CD2Ev)

// FIXME: LLVM output should be: @_ZN1CD2Ev ={{.*}} unnamed_addr alias {{.*}} @_ZN1BD2Ev
// LLVM: define dso_local void @_ZN1CD2Ev(ptr
Expand Down
12 changes: 6 additions & 6 deletions clang/test/CIR/IR/cxx-special-member.cir
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
!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 @_ZN1SC1ERKS_(!cir.ptr<!rec_S>, !cir.ptr<!rec_S>) special_member<#cir.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>
cir.func private @_ZN1SC2Ev(!cir.ptr<!rec_S>) special_member<#cir.cxx_ctor<!rec_S, default>>
cir.func private @_ZN1SD2Ev(!cir.ptr<!rec_S>) special_member<#cir.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 @_ZN1SC1ERKS_(!cir.ptr<!rec_S>, !cir.ptr<!rec_S>) special_member<#cir.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: cir.func private @_ZN1SC2Ev(!cir.ptr<!rec_S>) special_member<#cir.cxx_ctor<!rec_S, default>>
// CHECK: cir.func private @_ZN1SD2Ev(!cir.ptr<!rec_S>) special_member<#cir.cxx_dtor<!rec_S>>
// CHECK: }
Loading