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 18 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
54 changes: 54 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,60 @@ def GlobalDtorAttr : CIR_GlobalCtorDtor<"Dtor", "dtor"> {
}];
}

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.
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,
EnumParameter<CIR_CtorKind>:$ctorKind);

let assemblyFormat = [{
`<` $type `,` $ctorKind `>`
}];

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 `>`
}];

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

def CIR_CXXSpecialMemberAttr : AnyAttrOf<[
CIR_CXXCtorAttr,
CIR_CXXDtorAttr
]>;

def BitfieldInfoAttr : CIR_Attr<"BitfieldInfo", "bitfield_info"> {
let summary = "Represents a bit field info";
let description = [{
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -3717,6 +3717,7 @@ def FuncOp : CIR_Op<"func", [
CIR_OptionalPriorityAttr:$global_ctor_priority,
CIR_OptionalPriorityAttr:$global_dtor_priority,
OptionalAttr<ArrayAttr>:$annotations,
OptionalAttr<CIR_CXXSpecialMemberAttr>:$cxx_special_member,
OptionalAttr<AnyASTFunctionDeclAttr>:$ast
);

Expand Down
29 changes: 22 additions & 7 deletions clang/lib/CIR/CodeGen/CIRGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -764,15 +764,29 @@ cir::FuncOp CIRGenFunction::generateCode(clang::GlobalDecl gd, cir::FuncOp fn,
// Generate the body of the function.
// TODO: PGO.assignRegionCounters
assert(!cir::MissingFeatures::shouldInstrumentFunction());
if (isa<CXXDestructorDecl>(fd))
if (auto dtor = dyn_cast<CXXDestructorDecl>(fd)) {
auto cxxDtor = cir::CXXDtorAttr::get(
convertType(getContext().getRecordType(dtor->getParent())));
fn.setCxxSpecialMemberAttr(cxxDtor);

emitDestructorBody(args);
else if (isa<CXXConstructorDecl>(fd))
} else if (auto ctor = dyn_cast<CXXConstructorDecl>(fd)) {
cir::CtorKind ctorKind = cir::CtorKind::Custom;
if (ctor->isDefaultConstructor())
ctorKind = cir::CtorKind::Default;
if (ctor->isCopyConstructor())
ctorKind = cir::CtorKind::Copy;

auto cxxCtor = cir::CXXCtorAttr::get(
convertType(getContext().getRecordType(ctor->getParent())), ctorKind);
fn.setCxxSpecialMemberAttr(cxxCtor);

emitConstructorBody(args);
else if (getLangOpts().CUDA && !getLangOpts().CUDAIsDevice &&
fd->hasAttr<CUDAGlobalAttr>())
} else if (getLangOpts().CUDA && !getLangOpts().CUDAIsDevice &&
fd->hasAttr<CUDAGlobalAttr>()) {
CGM.getCUDARuntime().emitDeviceStub(*this, fn, args);
else if (isa<CXXMethodDecl>(fd) &&
cast<CXXMethodDecl>(fd)->isLambdaStaticInvoker()) {
} else if (isa<CXXMethodDecl>(fd) &&
cast<CXXMethodDecl>(fd)->isLambdaStaticInvoker()) {
// The lambda static invoker function is special, because it forwards or
// clones the body of the function call operator (but is actually
// static).
Expand All @@ -788,8 +802,9 @@ cir::FuncOp CIRGenFunction::generateCode(clang::GlobalDecl gd, cir::FuncOp fn,
fn.erase();
return nullptr;
}
} else
} else {
llvm_unreachable("no definition for emitted function");
}

assert(builder.getInsertionBlock() && "Should be valid");

Expand Down
21 changes: 21 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2735,6 +2735,27 @@ cir::FuncOp CIRGenModule::createCIRFunction(mlir::Location loc, StringRef name,
f.setExtraAttrsAttr(
cir::ExtraFuncAttributesAttr::get(builder.getDictionaryAttr({})));

if (fd) {
if (auto dtor = dyn_cast<CXXDestructorDecl>(fd)) {
auto cxxDtor = cir::CXXDtorAttr::get(
convertType(getASTContext().getRecordType(dtor->getParent())));
f.setCxxSpecialMemberAttr(cxxDtor);
}

if (auto ctor = dyn_cast<CXXConstructorDecl>(fd)) {
cir::CtorKind ctorKind = cir::CtorKind::Custom;
if (ctor->isDefaultConstructor())
ctorKind = cir::CtorKind::Default;
if (ctor->isCopyConstructor())
ctorKind = cir::CtorKind::Copy;

auto cxxCtor = cir::CXXCtorAttr::get(
convertType(getASTContext().getRecordType(ctor->getParent())),
ctorKind);
f.setCxxSpecialMemberAttr(cxxCtor);
}
}

if (!curCGF)
theModule.push_back(f);
}
Expand Down
24 changes: 23 additions & 1 deletion clang/lib/CIR/Dialect/IR/CIRDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2529,6 +2529,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(
Expand Down Expand Up @@ -2609,6 +2610,20 @@ ParseResult cir::FuncOp::parse(OpAsmParser &parser, OperationState &state) {
state.addAttribute(annotationsNameAttr, annotations);
}

// Parse CXXSpecialMember attribute
if (parser.parseOptionalKeyword("special_member").succeeded()) {
cir::CXXCtorAttr ctorAttr;
cir::CXXDtorAttr dtorAttr;
if (parser.parseLess().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();
}

// If additional attributes are present, parse them.
if (parser.parseOptionalAttrDictWithKeyword(state.attributes))
return failure();
Expand Down Expand Up @@ -2789,6 +2804,13 @@ void cir::FuncOp::print(OpAsmPrinter &p) {
p.printAttribute(annotations);
}

if (auto specialMemberAttr = getCxxSpecialMember()) {
assert((mlir::isa<cir::CXXCtorAttr, cir::CXXDtorAttr>(*specialMemberAttr)));
p << " special_member<";
p.printAttribute(*specialMemberAttr);
p << '>';
}

function_interface_impl::printFunctionAttributes(
p, *this,
// These are all omitted since they are custom printed already.
Expand All @@ -2799,7 +2821,7 @@ void cir::FuncOp::print(OpAsmPrinter &p) {
getLinkageAttrName(), getCallingConvAttrName(), getNoProtoAttrName(),
getSymVisibilityAttrName(), getArgAttrsAttrName(), getResAttrsAttrName(),
getComdatAttrName(), getGlobalVisibilityAttrName(),
getAnnotationsAttrName()});
getAnnotationsAttrName(), getCxxSpecialMemberAttrName()});

if (auto aliaseeName = getAliasee()) {
p << " alias(";
Expand Down
18 changes: 11 additions & 7 deletions clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ struct LifetimeCheckPass : public LifetimeCheckBase<LifetimeCheckPass> {
void checkLambdaCaptureStore(StoreOp storeOp);
void trackCallToCoroutine(CallOp callOp);

void checkCtor(CallOp callOp, ASTCXXConstructorDeclInterface ctor);
void checkCtor(CallOp callOp, cir::CXXCtorAttr ctor);
void checkMoveAssignment(CallOp callOp, ASTCXXMethodDeclInterface m);
void checkCopyAssignment(CallOp callOp, ASTCXXMethodDeclInterface m);
void checkNonConstUseOfOwner(mlir::Value ownerAddr, mlir::Location loc);
Expand Down Expand Up @@ -1549,8 +1549,7 @@ bool LifetimeCheckPass::isCtorInitPointerFromOwner(CallOp callOp) {
return false;
}

void LifetimeCheckPass::checkCtor(CallOp callOp,
ASTCXXConstructorDeclInterface ctor) {
void LifetimeCheckPass::checkCtor(CallOp callOp, cir::CXXCtorAttr ctor) {
// TODO: zero init
// 2.4.2 if the initialization is default initialization or zero
// initialization, example:
Expand All @@ -1559,7 +1558,7 @@ void LifetimeCheckPass::checkCtor(CallOp callOp,
// string_view p;
//
// both results in pset(p) == {null}
if (ctor.isDefaultConstructor()) {
if (ctor.getCtorKind() == cir::CtorKind::Default) {
// First argument passed is always the alloca for the 'this' ptr.

// Currently two possible actions:
Expand All @@ -1583,7 +1582,7 @@ void LifetimeCheckPass::checkCtor(CallOp callOp,
}

// User defined copy ctor calls ...
if (ctor.isCopyConstructor()) {
if (ctor.getCtorKind() == cir::CtorKind::Copy) {
llvm_unreachable("NYI");
}

Expand Down Expand Up @@ -1788,8 +1787,13 @@ void LifetimeCheckPass::checkCall(CallOp callOp) {

// From this point on only owner and pointer class methods handling,
// starting from special methods.
if (auto ctor = dyn_cast<ASTCXXConstructorDeclInterface>(methodDecl))
return checkCtor(callOp, ctor);
if (auto fnName = callOp.getCallee()) {
auto calleeFuncOp = getCalleeFromSymbol(theModule, *fnName);
if (calleeFuncOp && calleeFuncOp.getCxxSpecialMember())
if (auto cxxCtor =
dyn_cast<cir::CXXCtorAttr>(*calleeFuncOp.getCxxSpecialMember()))
return checkCtor(callOp, cxxCtor);
}
if (methodDecl.isMoveAssignmentOperator())
return checkMoveAssignment(callOp, methodDecl);
if (methodDecl.isCopyAssignmentOperator())
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>) 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>)
// 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>)
// 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>) 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>) 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>) 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
8 changes: 4 additions & 4 deletions clang/test/CIR/CodeGen/virtual-destructor-calls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +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>) alias(@_ZN1BD2Ev)
// 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 @@ -46,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>) 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>) alias(@_ZN1BD2Ev)
// CIR_O1: cir.func private dso_local @_ZN1CD1Ev(!cir.ptr<!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
20 changes: 20 additions & 0 deletions clang/test/CIR/IR/cxx-special-member.cir
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>) 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>) 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>) 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>) 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