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 4 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
53 changes: 53 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,59 @@ def GlobalDtorAttr : CIR_GlobalCtorDtor<"Dtor", "dtor",
"A function with this attribute excutes before module unloading"
>;

class CIR_CXXSpecialMember<string name, string attrMnemonic, string sum,
string desc> : CIR_Attr<name, attrMnemonic> {
let summary = sum;
let description = desc;
let skipDefaultBuilders = 1;
}

def CXXCtorAttr
: CIR_CXXSpecialMember<
"CXXCtor", "cxx_ctor", "Marks a function as a CXX constructor",
"Functions with this attribute are CXX constructors"> {
let parameters = (ins "mlir::Type":$type,
"bool":$is_default_constructor,
"bool":$is_copy_constructor);
let assemblyFormat = [{
`<`
$type `,` $is_default_constructor `,` $is_copy_constructor
`>`
}];
let builders =
[AttrBuilder<(ins "mlir::Type":$type,
"bool":$is_default_constructor,
"bool":$is_copy_constructor), [{
return $_get($_ctxt, type, is_default_constructor,
is_copy_constructor);
}]>,
AttrBuilderWithInferredContext<(ins "mlir::Type":$type,
"bool":$is_default_constructor,
"bool":$is_copy_constructor), [{
return $_get(type.getContext(), type,
is_default_constructor, is_copy_constructor);
}]>];
}

def CXXDtorAttr
: CIR_CXXSpecialMember<
"CXXDtor", "cxx_dtor", "Marks a function as a CXX destructor",
"Functions with this attribute are CXX destructors"> {
let parameters = (ins "mlir::Type":$type);
let assemblyFormat = [{
`<`
$type
`>`
}];
let builders =
[AttrBuilder<(ins "mlir::Type":$type), [{
return $_get($_ctxt, type);
}]>,
AttrBuilderWithInferredContext<(ins "mlir::Type":$type), [{
return $_get(type.getContext(), type);
}]>];
}

def BitfieldInfoAttr : CIR_Attr<"BitfieldInfo", "bitfield_info"> {
let summary = "Represents a bit field info";
let description = [{
Expand Down
4 changes: 3 additions & 1 deletion clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -3653,7 +3653,9 @@ def FuncOp : CIR_Op<"func", [
OptionalAttr<GlobalCtorAttr>:$global_ctor,
OptionalAttr<GlobalDtorAttr>:$global_dtor,
OptionalAttr<ArrayAttr>:$annotations,
OptionalAttr<AnyASTFunctionDeclAttr>:$ast
OptionalAttr<AnyASTFunctionDeclAttr>:$ast,
OptionalAttr<CXXCtorAttr>:$cxx_ctor,
OptionalAttr<CXXDtorAttr>:$cxx_dtor
);

let regions = (region AnyRegion:$body);
Expand Down
26 changes: 19 additions & 7 deletions clang/lib/CIR/CodeGen/CIRGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -764,15 +764,26 @@ 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(
&getMLIRContext(),
convertType(getContext().getRecordType(dtor->getParent())));
fn.setCxxDtorAttr(cxxDtor);

emitDestructorBody(args);
else if (isa<CXXConstructorDecl>(fd))
} else if (auto ctor = dyn_cast<CXXConstructorDecl>(fd)) {
auto cxxCtor = cir::CXXCtorAttr::get(
&getMLIRContext(),
convertType(getContext().getRecordType(ctor->getParent())),
ctor->isDefaultConstructor(), ctor->isCopyConstructor());
fn.setCxxCtorAttr(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 +799,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
19 changes: 19 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2739,6 +2739,25 @@ cir::FuncOp CIRGenModule::createCIRFunction(mlir::Location loc, StringRef name,
f.setExtraAttrsAttr(
cir::ExtraFuncAttributesAttr::get(builder.getDictionaryAttr({})));

if (fd) {
CIRGenFunction cgf{*this, builder};

if (auto dtor = dyn_cast<CXXDestructorDecl>(fd)) {
auto cxxDtor = cir::CXXDtorAttr::get(
&getMLIRContext(),
convertType(cgf.getContext().getRecordType(dtor->getParent())));
f.setCxxDtorAttr(cxxDtor);
}

if (auto ctor = dyn_cast<CXXConstructorDecl>(fd)) {
auto cxxCtor = cir::CXXCtorAttr::get(
&getMLIRContext(),
convertType(cgf.getContext().getRecordType(ctor->getParent())),
ctor->isDefaultConstructor(), ctor->isCopyConstructor());
f.setCxxCtorAttr(cxxCtor);
}
}

if (!curCGF)
theModule.push_back(f);
}
Expand Down
68 changes: 60 additions & 8 deletions clang/lib/CIR/Dialect/IR/CIRDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2366,6 +2366,8 @@ ParseResult cir::FuncOp::parse(OpAsmParser &parser, OperationState &state) {
auto visibilityNameAttr = getGlobalVisibilityAttrName(state.name);
auto dsoLocalNameAttr = getDsoLocalAttrName(state.name);
auto annotationsNameAttr = getAnnotationsAttrName(state.name);
auto cxxCtorAttr = getCxxCtorAttrName(state.name);
auto cxxDtorAttr = getCxxDtorAttrName(state.name);
if (::mlir::succeeded(parser.parseOptionalKeyword(builtinNameAttr.strref())))
state.addAttribute(builtinNameAttr, parser.getBuilder().getUnitAttr());
if (::mlir::succeeded(
Expand Down Expand Up @@ -2446,6 +2448,41 @@ ParseResult cir::FuncOp::parse(OpAsmParser &parser, OperationState &state) {
state.addAttribute(annotationsNameAttr, annotations);
}

// Add CXXSpecialMember attributes.
if (mlir::succeeded(parser.parseOptionalKeyword("ctor"))) {
if (parser.parseLess().failed())
return failure();

mlir::Type type;
bool defaultCtor = false, copyCtor = false;
if (parser.parseType(type).failed())
return failure();
if (mlir::succeeded(parser.parseOptionalComma()))
if (mlir::succeeded(parser.parseOptionalKeyword("default_ctor")))
defaultCtor = true;
if (mlir::succeeded(parser.parseOptionalComma()))
if (mlir::succeeded(parser.parseOptionalKeyword("copy_ctor")))
copyCtor = true;
if (parser.parseGreater().failed())
return failure();

state.addAttribute(cxxCtorAttr,
CXXCtorAttr::get(type, defaultCtor, copyCtor));
}

if (mlir::succeeded(parser.parseOptionalKeyword("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(cxxDtorAttr, CXXDtorAttr::get(type));
}

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

if (auto cxxCtor = getCxxCtorAttr()) {
p << " ctor<" << cxxCtor.getType();
if (cxxCtor.getIsDefaultConstructor())
p << ", default_ctor";
if (cxxCtor.getIsCopyConstructor())
p << ", copy_ctor";
p << '>';
}

if (auto cxxDtor = getCxxDtorAttr()) {
p << " dtor<" << cxxDtor.getType() << ">";
}

function_interface_impl::printFunctionAttributes(
p, *this,
// These are all omitted since they are custom printed already.
{getAliaseeAttrName(), getBuiltinAttrName(), getCoroutineAttrName(),
getDsoLocalAttrName(), getExtraAttrsAttrName(),
getFunctionTypeAttrName(), getGlobalCtorAttrName(),
getGlobalDtorAttrName(), getLambdaAttrName(), getLinkageAttrName(),
getCallingConvAttrName(), getNoProtoAttrName(),
getSymVisibilityAttrName(), getArgAttrsAttrName(), getResAttrsAttrName(),
getComdatAttrName(), getGlobalVisibilityAttrName(),
getAnnotationsAttrName()});
{getAliaseeAttrName(), getBuiltinAttrName(),
getCoroutineAttrName(), getDsoLocalAttrName(),
getExtraAttrsAttrName(), getFunctionTypeAttrName(),
getGlobalCtorAttrName(), getGlobalDtorAttrName(),
getLambdaAttrName(), getLinkageAttrName(),
getCallingConvAttrName(), getNoProtoAttrName(),
getSymVisibilityAttrName(), getArgAttrsAttrName(),
getResAttrsAttrName(), getComdatAttrName(),
getGlobalVisibilityAttrName(), getAnnotationsAttrName(),
getCxxCtorAttrName(), getCxxDtorAttrName()});

if (auto aliaseeName = getAliasee()) {
p << " alias(";
Expand Down
16 changes: 9 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.getIsDefaultConstructor()) {
// 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.getIsCopyConstructor()) {
llvm_unreachable("NYI");
}

Expand Down Expand Up @@ -1788,8 +1787,11 @@ 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.getCxxCtorAttr())
return checkCtor(callOp, calleeFuncOp.getCxxCtorAttr());
}
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>) ctor<!rec_B, default_ctor> alias(@_ZN1BC2Ev)
8 changes: 4 additions & 4 deletions clang/test/CIR/CodeGen/static.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ static Init __ioinit(true);
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 @_ZN4InitC1Eb(!cir.ptr<!rec_Init>, !cir.bool) ctor<!rec_Init>
// BEFORE-NEXT: cir.func private @_ZN4InitD1Ev(!cir.ptr<!rec_Init>) 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 @@ -41,8 +41,8 @@ static Init __ioinit2(false);
// AFTER: module {{.*}} attributes {{.*}}cir.global_ctors = [#cir.global_ctor<"__cxx_global_var_init", 65536>, #cir.global_ctor<"__cxx_global_var_init.1", 65536>]
// 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 @_ZN4InitC1Eb(!cir.ptr<!rec_Init>, !cir.bool) ctor<!rec_Init>
// AFTER-NEXT: cir.func private @_ZN4InitD1Ev(!cir.ptr<!rec_Init>) 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>) ctor<!rec_E, default_ctor> 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>) 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>) 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 @@ -32,7 +32,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>) dtor<!rec_B> alias(@_ZN1BD2Ev)
// FIXME: LLVM output should be: @_ZN1BD1Ev ={{.*}} unnamed_addr alias {{.*}} @_ZN1BD2Ev
// LLVM: declare dso_local void @_ZN1BD1Ev(ptr)

Expand All @@ -43,11 +43,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>) 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>) dtor<!rec_C> alias(@_ZN1BD2Ev)
// CIR_O1: cir.func private dso_local @_ZN1CD1Ev(!cir.ptr<!rec_C>) dtor<!rec_C> alias(@_ZN1CD2Ev)

// FIXME: LLVM output should be: @_ZN1CD2Ev ={{.*}} unnamed_addr alias {{.*}} @_ZN1BD2Ev
// LLVM: define dso_local void @_ZN1CD2Ev(ptr
Expand Down
Loading