Skip to content

Commit 8688931

Browse files
author
Tommy McMichen
committed
[CIR][NFC] Add cir::CallOp method to get called function
1 parent c543481 commit 8688931

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
@@ -4255,6 +4255,16 @@ class CIR_CallOp<string mnemonic, list<Trait> extra_traits = []>
42554255
// For indirect call, the operand list is shifted by one.
42564256
setOperand(index + 1, value);
42574257
}
4258+
4259+
/// If this is a direct call, returns the callee as a `cir::FuncOp` in `symbolTable`.
4260+
/// Otherwise, returns `null`.
4261+
cir::FuncOp getDirectCallee(mlir::SymbolTable &symbolTable);
4262+
4263+
/// If this is a direct call, returns the callee as a `cir::FuncOp` in parent `module`.
4264+
/// Otherwise, returns `null`.
4265+
/// NOTE: This method walks the symbol table. If you are calling this method a lot,
4266+
/// consider using `cir::FuncOp::getDirectCallee(mlir::SymbolTable &)` instead.
4267+
cir::FuncOp getDirectCallee(mlir::ModuleOp module);
42584268
}];
42594269

42604270
let hasCustomAssemblyFormat = 1;

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

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

2953+
cir::FuncOp cir::CallOp::getDirectCallee(mlir::SymbolTable &symbolTable) {
2954+
if (!getCallee())
2955+
return {};
2956+
llvm::StringRef name = *getCallee();
2957+
return symbolTable.lookup<cir::FuncOp>(name);
2958+
}
2959+
2960+
cir::FuncOp cir::CallOp::getDirectCallee(mlir::ModuleOp module) {
2961+
if (!getCallee())
2962+
return {};
2963+
llvm::StringRef name = *getCallee();
2964+
mlir::Operation *global = mlir::SymbolTable::lookupSymbolIn(module, name);
2965+
return mlir::dyn_cast_if_present<FuncOp>(global);
2966+
}
2967+
29532968
static LogicalResult
29542969
verifyCallCommInSymbolUses(Operation *op, SymbolTableCollection &symbolTable) {
29552970
// 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)