Skip to content

Commit 514b3cb

Browse files
committed
refactor
1 parent b8a1b04 commit 514b3cb

File tree

10 files changed

+55
-50
lines changed

10 files changed

+55
-50
lines changed

clang/include/clang/CIR/Dialect/IR/CIRAttrs.td

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,16 +1288,19 @@ def CIR_CXXCtorAttr : CIR_Attr<"CXXCtor", "cxx_ctor"> {
12881288
The `copy` kind is used if the constructor is a copy constructor.
12891289
}];
12901290
let parameters = (ins "mlir::Type":$type,
1291-
EnumParameter<CIR_CtorKind>:$ctorKind);
1291+
EnumParameter<CIR_CtorKind>:$ctorKind,
1292+
OptionalParameter<"std::optional<const clang::CXXRecordDecl *>">:$recordDecl);
12921293

12931294
let assemblyFormat = [{
12941295
`<` $type `,` $ctorKind `>`
12951296
}];
12961297

12971298
let builders = [
12981299
AttrBuilderWithInferredContext<(ins "mlir::Type":$type,
1299-
CArg<"CtorKind", "cir::CtorKind::Custom">:$ctorKind), [{
1300-
return $_get(type.getContext(), type, ctorKind);
1300+
CArg<"CtorKind", "cir::CtorKind::Custom">:$ctorKind,
1301+
CArg<"std::optional<const clang::CXXRecordDecl *>",
1302+
"std::nullopt">:$recordDecl), [{
1303+
return $_get(type.getContext(), type, ctorKind, recordDecl);
13011304
}]>
13021305
];
13031306
}
@@ -1307,15 +1310,18 @@ def CIR_CXXDtorAttr : CIR_Attr<"CXXDtor", "cxx_dtor"> {
13071310
let description = [{
13081311
Functions with this attribute are CXX destructors
13091312
}];
1310-
let parameters = (ins "mlir::Type":$type);
1313+
let parameters = (ins "mlir::Type":$type,
1314+
OptionalParameter<"std::optional<const clang::CXXRecordDecl *>">:$recordDecl);
13111315

13121316
let assemblyFormat = [{
13131317
`<` $type `>`
13141318
}];
13151319

13161320
let builders = [
1317-
AttrBuilderWithInferredContext<(ins "mlir::Type":$type), [{
1318-
return $_get(type.getContext(), type);
1321+
AttrBuilderWithInferredContext<(ins "mlir::Type":$type,
1322+
CArg<"std::optional<const clang::CXXRecordDecl *>",
1323+
"std::nullopt">:$recordDecl), [{
1324+
return $_get(type.getContext(), type, recordDecl);
13191325
}]>
13201326
];
13211327
}

clang/include/clang/CIR/Dialect/IR/CIROps.td

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3991,8 +3991,7 @@ class CIR_CallOp<string mnemonic, list<Trait> extra_traits = []>
39913991
DefaultValuedAttr<CIR_CallingConv, "CallingConv::C">:$calling_conv,
39923992
DefaultValuedAttr<CIR_SideEffect, "SideEffect::All">:$side_effect,
39933993
CIR_ExtraFuncAttr:$extra_attrs,
3994-
OptionalAttr<ASTCallExprInterface>:$ast,
3995-
OptionalAttr<ASTRecordDeclInterface>:$ast_record
3994+
OptionalAttr<ASTCallExprInterface>:$ast
39963995
);
39973996
}
39983997

clang/include/clang/CIR/Dialect/IR/CIRStdOps.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ def CIR_IterBeginOp: CIR_StdOp<"begin",
6161
def CIR_IterEndOp: CIR_StdOp<"end",
6262
(ins CIR_AnyType:$container),
6363
(outs CIR_AnyType:$result)>;
64-
def CIR_StdVectorCtorOp: CIR_StdOp<"vector_ctor",
64+
def CIR_StdVectorCtorOp: CIR_StdOp<"vector_cxx_ctor",
6565
(ins CIR_AnyType:$first),
6666
(outs Optional<CIR_AnyType>:$result)>;
67-
def CIR_StdVectorDtorOp: CIR_StdOp<"vector_dtor",
67+
def CIR_StdVectorDtorOp: CIR_StdOp<"vector_cxx_dtor",
6868
(ins CIR_AnyType:$first),
6969
(outs Optional<CIR_AnyType>:$result)>;
7070

clang/lib/CIR/CodeGen/CIRGenCall.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,7 @@ RValue CIRGenFunction::emitCall(const CIRGenFunctionInfo &CallInfo,
424424
const CallArgList &CallArgs,
425425
cir::CIRCallOpInterface *callOrTryCall,
426426
bool IsMustTail, mlir::Location loc,
427-
std::optional<const clang::CallExpr *> E,
428-
std::optional<const clang::RecordDecl *> RD) {
427+
std::optional<const clang::CallExpr *> E) {
429428
auto builder = CGM.getBuilder();
430429
// FIXME: We no longer need the types from CallArgs; lift up and simplify
431430

@@ -654,10 +653,6 @@ RValue CIRGenFunction::emitCall(const CIRGenFunctionInfo &CallInfo,
654653
callLikeOp->setAttr("ast",
655654
cir::ASTCallExprAttr::get(&getMLIRContext(), *E));
656655

657-
if (RD)
658-
callLikeOp->setAttr("ast_record",
659-
cir::ASTRecordDeclAttr::get(&getMLIRContext(), *RD));
660-
661656
if (callOrTryCall)
662657
*callOrTryCall = callLikeOp;
663658
return callLikeOp;

clang/lib/CIR/CodeGen/CIRGenClass.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,8 +2012,7 @@ void CIRGenFunction::emitCXXConstructorCall(
20122012
Args, D, Type, ExtraArgs.Prefix, ExtraArgs.Suffix, PassPrototypeArgs);
20132013
CIRGenCallee Callee = CIRGenCallee::forDirect(CalleePtr, GlobalDecl(D, Type));
20142014
cir::CIRCallOpInterface C;
2015-
emitCall(Info, Callee, ReturnValueSlot(), Args, &C, false, getLoc(Loc),
2016-
nullptr, ClassDecl);
2015+
emitCall(Info, Callee, ReturnValueSlot(), Args, &C, false, getLoc(Loc));
20172016

20182017
assert(CGM.getCodeGenOpts().OptimizationLevel == 0 ||
20192018
ClassDecl->isDynamicClass() || Type == Ctor_Base ||

clang/lib/CIR/CodeGen/CIRGenExprCXX.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,8 +1575,7 @@ RValue CIRGenFunction::emitCXXDestructorCall(GlobalDecl Dtor,
15751575
return emitCall(CGM.getTypes().arrangeCXXStructorDeclaration(Dtor), Callee,
15761576
ReturnValueSlot(), Args, nullptr, CE && CE == MustTailCall,
15771577
CE ? getLoc(CE->getExprLoc())
1578-
: getLoc(Dtor.getDecl()->getSourceRange()),
1579-
CE, DtorDecl->getParent());
1578+
: getLoc(Dtor.getDecl()->getSourceRange()));
15801579
}
15811580

15821581
/// Emit a call to an operator new or operator delete function, as implicitly

clang/lib/CIR/CodeGen/CIRGenFunction.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,8 @@ cir::FuncOp CIRGenFunction::generateCode(clang::GlobalDecl gd, cir::FuncOp fn,
766766
assert(!cir::MissingFeatures::shouldInstrumentFunction());
767767
if (auto dtor = dyn_cast<CXXDestructorDecl>(fd)) {
768768
auto cxxDtor = cir::CXXDtorAttr::get(
769-
convertType(getContext().getRecordType(dtor->getParent())));
769+
convertType(getContext().getRecordType(dtor->getParent())),
770+
dtor->getParent());
770771
fn.setCxxSpecialMemberAttr(cxxDtor);
771772

772773
emitDestructorBody(args);
@@ -778,7 +779,8 @@ cir::FuncOp CIRGenFunction::generateCode(clang::GlobalDecl gd, cir::FuncOp fn,
778779
ctorKind = cir::CtorKind::Copy;
779780

780781
auto cxxCtor = cir::CXXCtorAttr::get(
781-
convertType(getContext().getRecordType(ctor->getParent())), ctorKind);
782+
convertType(getContext().getRecordType(ctor->getParent())), ctorKind,
783+
ctor->getParent());
782784
fn.setCxxSpecialMemberAttr(cxxCtor);
783785

784786
emitConstructorBody(args);

clang/lib/CIR/CodeGen/CIRGenFunction.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,8 +1865,7 @@ class CIRGenFunction : public CIRGenTypeCache {
18651865
const CallArgList &Args,
18661866
cir::CIRCallOpInterface *callOrTryCall, bool IsMustTail,
18671867
mlir::Location loc,
1868-
std::optional<const clang::CallExpr *> E = std::nullopt,
1869-
std::optional<const clang::RecordDecl *> RD = std::nullopt);
1868+
std::optional<const clang::CallExpr *> E = std::nullopt);
18701869
RValue emitCall(const CIRGenFunctionInfo &CallInfo,
18711870
const CIRGenCallee &Callee, ReturnValueSlot ReturnValue,
18721871
const CallArgList &Args,

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2738,7 +2738,8 @@ cir::FuncOp CIRGenModule::createCIRFunction(mlir::Location loc, StringRef name,
27382738
if (fd) {
27392739
if (auto dtor = dyn_cast<CXXDestructorDecl>(fd)) {
27402740
auto cxxDtor = cir::CXXDtorAttr::get(
2741-
convertType(getASTContext().getRecordType(dtor->getParent())));
2741+
convertType(getASTContext().getRecordType(dtor->getParent())),
2742+
dtor->getParent());
27422743
f.setCxxSpecialMemberAttr(cxxDtor);
27432744
}
27442745

@@ -2751,7 +2752,7 @@ cir::FuncOp CIRGenModule::createCIRFunction(mlir::Location loc, StringRef name,
27512752

27522753
auto cxxCtor = cir::CXXCtorAttr::get(
27532754
convertType(getASTContext().getRecordType(ctor->getParent())),
2754-
ctorKind);
2755+
ctorKind, ctor->getParent());
27552756
f.setCxxSpecialMemberAttr(cxxCtor);
27562757
}
27572758
}

clang/lib/CIR/Dialect/Transforms/IdiomRecognizer.cpp

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "llvm/ADT/StringMap.h"
2222
#include "llvm/ADT/StringRef.h"
2323
#include "llvm/ADT/Twine.h"
24+
#include "llvm/ADT/TypeSwitch.h"
2425
#include "llvm/Support/ErrorHandling.h"
2526
#include "llvm/Support/Path.h"
2627

@@ -56,16 +57,29 @@ template <typename TargetOp> class StdRecognizer {
5657
return dyn_cast<FuncOp>(global);
5758
}
5859

59-
static bool isStdVector(const clang::CXXRecordDecl *RD) {
60-
if (!RD || !RD->getDeclContext()->isStdNamespace())
61-
return false;
60+
static std::optional<StringRef>
61+
getRecordName(const clang::CXXRecordDecl *rd) {
62+
if (!rd || !rd->getDeclContext()->isStdNamespace())
63+
return std::nullopt;
6264

63-
if (RD->getDeclName().isIdentifier()) {
64-
StringRef Name = RD->getName();
65-
return Name == "vector";
66-
}
65+
if (rd->getDeclName().isIdentifier())
66+
return rd->getName();
6767

68-
return false;
68+
return std::nullopt;
69+
}
70+
71+
static std::optional<std::string>
72+
resolveSpecialMember(mlir::Attribute specialMember) {
73+
return TypeSwitch<Attribute, std::optional<std::string>>(specialMember)
74+
.Case<CXXCtorAttr, CXXDtorAttr>(
75+
[](auto attr) -> std::optional<std::string> {
76+
if (!attr.getRecordDecl())
77+
return std::nullopt;
78+
if (auto recordName = getRecordName(*attr.getRecordDecl()))
79+
return recordName->str() + "_" + attr.getMnemonic().str();
80+
return std::nullopt;
81+
})
82+
.Default([](Attribute) { return std::nullopt; });
6983
}
7084

7185
static bool raise(mlir::ModuleOp theModule, CallOp call,
@@ -74,21 +88,12 @@ template <typename TargetOp> class StdRecognizer {
7488
if (call.getNumOperands() != numArgs)
7589
return false;
7690

77-
llvm::StringRef name = *call.getCallee();
78-
auto calleeFunc = getCalleeFromSymbol(theModule, name);
79-
8091
llvm::StringRef stdFuncName = TargetOp::getFunctionName();
92+
auto calleeFunc = getCalleeFromSymbol(theModule, *call.getCallee());
8193

8294
if (auto specialMember = calleeFunc.getCxxSpecialMemberAttr()) {
83-
auto matches =
84-
(stdFuncName == "vector_ctor" && isa<CXXCtorAttr>(specialMember)) ||
85-
(stdFuncName == "vector_dtor" && isa<CXXDtorAttr>(specialMember));
86-
if (!matches)
87-
return false;
88-
89-
auto recordDeclAttr = call.getAstRecordAttr();
90-
if (!recordDeclAttr ||
91-
!isStdVector(cast<clang::CXXRecordDecl>(recordDeclAttr.getRawDecl())))
95+
auto resolved = resolveSpecialMember(specialMember);
96+
if (!resolved || *resolved != stdFuncName.str())
9297
return false;
9398
} else {
9499
auto callExprAttr = call.getAstAttr();
@@ -97,12 +102,12 @@ template <typename TargetOp> class StdRecognizer {
97102

98103
if (!checkArguments(call.getArgOperands()))
99104
return false;
100-
101-
if (remark)
102-
mlir::emitRemark(call.getLoc())
103-
<< "found call to std::" << stdFuncName << "()";
104105
}
105106

107+
if (remark)
108+
mlir::emitRemark(call.getLoc())
109+
<< "found call to std::" << stdFuncName << "()";
110+
106111
CIRBaseBuilderTy builder(context);
107112
builder.setInsertionPointAfter(call.getOperation());
108113
TargetOp op = buildCall(builder, call, std::make_index_sequence<numArgs>());

0 commit comments

Comments
 (0)