Skip to content

Commit 5dbb215

Browse files
committed
initial commit
1 parent 631455b commit 5dbb215

File tree

7 files changed

+77
-20
lines changed

7 files changed

+77
-20
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3991,7 +3991,8 @@ 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
3994+
OptionalAttr<ASTCallExprInterface>:$ast,
3995+
OptionalAttr<ASTRecordDeclInterface>:$ast_record
39953996
);
39963997
}
39973998

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,11 @@ 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",
65+
(ins CIR_AnyType:$first),
66+
(outs Optional<CIR_AnyType>:$result)>;
67+
def CIR_StdVectorDtorOp: CIR_StdOp<"vector_dtor",
68+
(ins CIR_AnyType:$first),
69+
(outs Optional<CIR_AnyType>:$result)>;
6470

6571
#endif

clang/lib/CIR/CodeGen/CIRGenCall.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,8 @@ 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) {
427+
std::optional<const clang::CallExpr *> E,
428+
std::optional<const clang::RecordDecl *> RD) {
428429
auto builder = CGM.getBuilder();
429430
// FIXME: We no longer need the types from CallArgs; lift up and simplify
430431

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

657+
if (RD)
658+
callLikeOp->setAttr("ast_record",
659+
cir::ASTRecordDeclAttr::get(&getMLIRContext(), *RD));
660+
656661
if (callOrTryCall)
657662
*callOrTryCall = callLikeOp;
658663
return callLikeOp;

clang/lib/CIR/CodeGen/CIRGenClass.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2012,7 +2012,8 @@ 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));
2015+
emitCall(Info, Callee, ReturnValueSlot(), Args, &C, false, getLoc(Loc),
2016+
nullptr, ClassDecl);
20162017

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

clang/lib/CIR/CodeGen/CIRGenExprCXX.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1575,7 +1575,8 @@ 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()));
1578+
: getLoc(Dtor.getDecl()->getSourceRange()),
1579+
CE, DtorDecl->getParent());
15791580
}
15801581

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

clang/lib/CIR/CodeGen/CIRGenFunction.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1865,7 +1865,8 @@ 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);
1868+
std::optional<const clang::CallExpr *> E = std::nullopt,
1869+
std::optional<const clang::RecordDecl *> RD = std::nullopt);
18691870
RValue emitCall(const CIRGenFunctionInfo &CallInfo,
18701871
const CIRGenCallee &Callee, ReturnValueSlot ReturnValue,
18711872
const CallArgList &Args,

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

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,66 @@ template <typename TargetOp> class StdRecognizer {
4242
template <size_t... Indices>
4343
static TargetOp buildCall(CIRBaseBuilderTy &builder, CallOp call,
4444
std::index_sequence<Indices...>) {
45-
return builder.create<TargetOp>(call.getLoc(), call.getResult().getType(),
46-
call.getCalleeAttr(),
47-
call.getOperand(Indices)...);
45+
return builder.create<TargetOp>(
46+
call.getLoc(),
47+
(call.getResult() ? call.getResult().getType() : mlir::TypeRange{}),
48+
call.getCalleeAttr(), call.getOperand(Indices)...);
4849
}
4950

5051
public:
51-
static bool raise(CallOp call, mlir::MLIRContext &context, bool remark) {
52+
static FuncOp getCalleeFromSymbol(mlir::ModuleOp theModule,
53+
llvm::StringRef name) {
54+
auto global = mlir::SymbolTable::lookupSymbolIn(theModule, name);
55+
assert(global && "expected to find symbol for function");
56+
return dyn_cast<FuncOp>(global);
57+
}
58+
59+
static bool isStdVector(const clang::CXXRecordDecl *RD) {
60+
if (!RD || !RD->getDeclContext()->isStdNamespace())
61+
return false;
62+
63+
if (RD->getDeclName().isIdentifier()) {
64+
StringRef Name = RD->getName();
65+
return Name == "vector";
66+
}
67+
68+
return false;
69+
}
70+
71+
static bool raise(mlir::ModuleOp theModule, CallOp call,
72+
mlir::MLIRContext &context, bool remark) {
5273
constexpr int numArgs = TargetOp::getNumArgs();
5374
if (call.getNumOperands() != numArgs)
5475
return false;
5576

56-
auto callExprAttr = call.getAstAttr();
57-
llvm::StringRef stdFuncName = TargetOp::getFunctionName();
58-
if (!callExprAttr || !callExprAttr.isStdFunctionCall(stdFuncName))
59-
return false;
77+
llvm::StringRef name = *call.getCallee();
78+
auto calleeFunc = getCalleeFromSymbol(theModule, name);
6079

61-
if (!checkArguments(call.getArgOperands()))
62-
return false;
80+
llvm::StringRef stdFuncName = TargetOp::getFunctionName();
6381

64-
if (remark)
65-
mlir::emitRemark(call.getLoc())
66-
<< "found call to std::" << stdFuncName << "()";
82+
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())))
92+
return false;
93+
} else {
94+
auto callExprAttr = call.getAstAttr();
95+
if (!callExprAttr || !callExprAttr.isStdFunctionCall(stdFuncName))
96+
return false;
97+
98+
if (!checkArguments(call.getArgOperands()))
99+
return false;
100+
101+
if (remark)
102+
mlir::emitRemark(call.getLoc())
103+
<< "found call to std::" << stdFuncName << "()";
104+
}
67105

68106
CIRBaseBuilderTy builder(context);
69107
builder.setInsertionPointAfter(call.getOperation());
@@ -194,12 +232,16 @@ void IdiomRecognizerPass::recognizeCall(CallOp call) {
194232

195233
bool remark = opts.emitRemarkFoundCalls();
196234

197-
using StdFunctionsRecognizer = std::tuple<StdRecognizer<StdFindOp>>;
235+
using StdFunctionsRecognizer =
236+
std::tuple<StdRecognizer<StdFindOp>, StdRecognizer<StdVectorCtorOp>,
237+
StdRecognizer<StdVectorDtorOp>>;
198238

199239
// MSVC requires explicitly capturing these variables.
200240
std::apply(
201241
[&, call, remark, this](auto... recognizers) {
202-
(decltype(recognizers)::raise(call, this->getContext(), remark) || ...);
242+
(decltype(recognizers)::raise(theModule, call, this->getContext(),
243+
remark) ||
244+
...);
203245
},
204246
StdFunctionsRecognizer());
205247
}

0 commit comments

Comments
 (0)