Skip to content

Commit 89ec5dd

Browse files
author
Tommy McMichen
committed
[CIR][NFC] Add cir::CallOp method to get called function
1 parent 4f3e788 commit 89ec5dd

File tree

3 files changed

+35
-23
lines changed

3 files changed

+35
-23
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4249,6 +4249,16 @@ class CIR_CallOp<string mnemonic, list<Trait> extra_traits = []>
42494249
// For indirect call, the operand list is shifted by one.
42504250
setOperand(index + 1, value);
42514251
}
4252+
4253+
/// If this is a direct call, returns the callee as a `cir::FuncOp` in `symbolTable`.
4254+
/// Otherwise, returns `null`.
4255+
cir::FuncOp getDirectCallee(mlir::SymbolTable &symbolTable);
4256+
4257+
/// If this is a direct call, returns the callee as a `cir::FuncOp` in parent `module`.
4258+
/// Otherwise, returns `null`.
4259+
/// NOTE: This method walks the symbol table. If you are calling this method a lot,
4260+
/// consider using `cir::FuncOp::getDirectCallee(mlir::SymbolTable &)` instead.
4261+
cir::FuncOp getDirectCallee(mlir::ModuleOp module);
42524262
}];
42534263

42544264
let hasCustomAssemblyFormat = 1;

clang/lib/CIR/Dialect/IR/CIRDialect.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2947,6 +2947,21 @@ unsigned cir::CallOp::getNumArgOperands() {
29472947
return this->getOperation()->getNumOperands();
29482948
}
29492949

2950+
cir::FuncOp cir::CallOp::getDirectCallee(mlir::SymbolTable &symbolTable) {
2951+
if (!getCallee())
2952+
return {};
2953+
llvm::StringRef name = *getCallee();
2954+
return symbolTable.lookup<cir::FuncOp>(name);
2955+
}
2956+
2957+
cir::FuncOp cir::CallOp::getDirectCallee(mlir::ModuleOp module) {
2958+
if (!getCallee())
2959+
return {};
2960+
llvm::StringRef name = *getCallee();
2961+
mlir::Operation *global = mlir::SymbolTable::lookupSymbolIn(module, name);
2962+
return mlir::dyn_cast_if_present<FuncOp>(global);
2963+
}
2964+
29502965
static LogicalResult
29512966
verifyCallCommInSymbolUses(Operation *op, SymbolTableCollection &symbolTable) {
29522967
// Callee attribute only need on indirect calls.

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

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,17 +1443,8 @@ void LifetimeCheckPass::checkPointerDeref(mlir::Value addr, mlir::Location loc,
14431443
emitPsetRemark();
14441444
}
14451445

1446-
static FuncOp getCalleeFromSymbol(ModuleOp mod, llvm::StringRef name) {
1447-
auto global = mlir::SymbolTable::lookupSymbolIn(mod, name);
1448-
assert(global && "expected to find symbol for function");
1449-
return dyn_cast<FuncOp>(global);
1450-
}
1451-
14521446
static const ASTCXXMethodDeclInterface getMethod(ModuleOp mod, CallOp callOp) {
1453-
if (!callOp.getCallee())
1454-
return nullptr;
1455-
llvm::StringRef name = *callOp.getCallee();
1456-
auto method = getCalleeFromSymbol(mod, name);
1447+
auto method = callOp.getDirectCallee(mod);
14571448
if (!method || method.getBuiltin())
14581449
return nullptr;
14591450
return dyn_cast<ASTCXXMethodDeclInterface>(method.getAstAttr());
@@ -1756,12 +1747,10 @@ bool LifetimeCheckPass::isTaskType(mlir::Value taskVal) {
17561747
}
17571748

17581749
void LifetimeCheckPass::trackCallToCoroutine(CallOp callOp) {
1759-
if (auto fnName = callOp.getCallee()) {
1760-
auto calleeFuncOp = getCalleeFromSymbol(theModule, *fnName);
1761-
if (calleeFuncOp &&
1762-
(calleeFuncOp.getCoroutine() ||
1763-
(calleeFuncOp.isDeclaration() && callOp->getNumResults() > 0 &&
1764-
isTaskType(callOp->getResult(0))))) {
1750+
if (auto calleeFuncOp = callOp.getDirectCallee(theModule)) {
1751+
if (calleeFuncOp.getCoroutine() ||
1752+
(calleeFuncOp.isDeclaration() && callOp->getNumResults() > 0 &&
1753+
isTaskType(callOp->getResult(0)))) {
17651754
currScope->localTempTasks.insert(callOp->getResult(0));
17661755
}
17671756
return;
@@ -1792,13 +1781,11 @@ void LifetimeCheckPass::checkCall(CallOp callOp) {
17921781

17931782
// From this point on only owner and pointer class methods handling,
17941783
// starting from special methods.
1795-
if (auto fnName = callOp.getCallee()) {
1796-
auto calleeFuncOp = getCalleeFromSymbol(theModule, *fnName);
1797-
if (calleeFuncOp && calleeFuncOp.getCxxSpecialMember())
1798-
if (auto cxxCtor =
1799-
dyn_cast<cir::CXXCtorAttr>(*calleeFuncOp.getCxxSpecialMember()))
1800-
return checkCtor(callOp, cxxCtor);
1801-
}
1784+
auto calleeFuncOp = callOp.getDirectCallee(theModule);
1785+
if (calleeFuncOp && calleeFuncOp.getCxxSpecialMember())
1786+
if (auto cxxCtor =
1787+
dyn_cast<cir::CXXCtorAttr>(*calleeFuncOp.getCxxSpecialMember()))
1788+
return checkCtor(callOp, cxxCtor);
18021789
if (methodDecl.isMoveAssignmentOperator())
18031790
return checkMoveAssignment(callOp, methodDecl);
18041791
if (methodDecl.isCopyAssignmentOperator())

0 commit comments

Comments
 (0)