Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -4255,6 +4255,16 @@ class CIR_CallOp<string mnemonic, list<Trait> extra_traits = []>
// For indirect call, the operand list is shifted by one.
setOperand(index + 1, value);
}

/// If this is a direct call, returns the callee as a `cir::FuncOp` in `symbolTable`.
/// Otherwise, returns `null`.
cir::FuncOp getDirectCallee(mlir::SymbolTable &symbolTable);

/// If this is a direct call, returns the callee as a `cir::FuncOp` in parent `module`.
/// Otherwise, returns `null`.
/// NOTE: This method walks the symbol table. If you are calling this method a lot,
/// consider using `cir::FuncOp::getDirectCallee(mlir::SymbolTable &)` instead.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you make such a change? All of the uses in this PR use the other form.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you know youre going to query this a lot, you'd run the mlir::SymbolTableAnalysis and use its result for repeated queries.

Copy link
Member

@bcardosolopes bcardosolopes Sep 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not a good use for CIRGen though, given the symbol table is constantly changing

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mlir::SymbolTableAnalysis claims to be good at handling insertions and removals. I looked through CIRGen though and couldn't find anywhere that this API could be used.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andykaylor does that answer your question? Or would you prefer me to remove the symbol table methods until we have a user?

cir::FuncOp getDirectCallee(mlir::ModuleOp module);
}];

let hasCustomAssemblyFormat = 1;
Expand Down
15 changes: 15 additions & 0 deletions clang/lib/CIR/Dialect/IR/CIRDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2950,6 +2950,21 @@ unsigned cir::CallOp::getNumArgOperands() {
return this->getOperation()->getNumOperands();
}

cir::FuncOp cir::CallOp::getDirectCallee(mlir::SymbolTable &symbolTable) {
if (!getCallee())
return {};
llvm::StringRef name = *getCallee();
return symbolTable.lookup<cir::FuncOp>(name);
}

cir::FuncOp cir::CallOp::getDirectCallee(mlir::ModuleOp module) {
if (!getCallee())
return {};
llvm::StringRef name = *getCallee();
mlir::Operation *global = mlir::SymbolTable::lookupSymbolIn(module, name);
return mlir::dyn_cast_if_present<FuncOp>(global);
}

static LogicalResult
verifyCallCommInSymbolUses(Operation *op, SymbolTableCollection &symbolTable) {
// Callee attribute only need on indirect calls.
Expand Down
33 changes: 10 additions & 23 deletions clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1443,17 +1443,8 @@ void LifetimeCheckPass::checkPointerDeref(mlir::Value addr, mlir::Location loc,
emitPsetRemark();
}

static FuncOp getCalleeFromSymbol(ModuleOp mod, llvm::StringRef name) {
auto global = mlir::SymbolTable::lookupSymbolIn(mod, name);
assert(global && "expected to find symbol for function");
return dyn_cast<FuncOp>(global);
}

static const ASTCXXMethodDeclInterface getMethod(ModuleOp mod, CallOp callOp) {
if (!callOp.getCallee())
return nullptr;
llvm::StringRef name = *callOp.getCallee();
auto method = getCalleeFromSymbol(mod, name);
cir::FuncOp method = callOp.getDirectCallee(mod);
if (!method || method.getBuiltin())
return nullptr;
return dyn_cast<ASTCXXMethodDeclInterface>(method.getAstAttr());
Expand Down Expand Up @@ -1756,12 +1747,10 @@ bool LifetimeCheckPass::isTaskType(mlir::Value taskVal) {
}

void LifetimeCheckPass::trackCallToCoroutine(CallOp callOp) {
if (auto fnName = callOp.getCallee()) {
auto calleeFuncOp = getCalleeFromSymbol(theModule, *fnName);
if (calleeFuncOp &&
(calleeFuncOp.getCoroutine() ||
(calleeFuncOp.isDeclaration() && callOp->getNumResults() > 0 &&
isTaskType(callOp->getResult(0))))) {
if (cir::FuncOp callee = callOp.getDirectCallee(theModule)) {
if (callee.getCoroutine() ||
(callee.isDeclaration() && callOp->getNumResults() > 0 &&
isTaskType(callOp->getResult(0)))) {
currScope->localTempTasks.insert(callOp->getResult(0));
}
return;
Expand Down Expand Up @@ -1792,13 +1781,11 @@ void LifetimeCheckPass::checkCall(CallOp callOp) {

// From this point on only owner and pointer class methods handling,
// starting from special methods.
if (auto fnName = callOp.getCallee()) {
auto calleeFuncOp = getCalleeFromSymbol(theModule, *fnName);
if (calleeFuncOp && calleeFuncOp.getCxxSpecialMember())
if (auto cxxCtor =
dyn_cast<cir::CXXCtorAttr>(*calleeFuncOp.getCxxSpecialMember()))
return checkCtor(callOp, cxxCtor);
}
cir::FuncOp callee = callOp.getDirectCallee(theModule);
if (callee && callee.getCxxSpecialMember())
if (auto cxxCtor =
dyn_cast<cir::CXXCtorAttr>(*callee.getCxxSpecialMember()))
return checkCtor(callOp, cxxCtor);
if (methodDecl.isMoveAssignmentOperator())
return checkMoveAssignment(callOp, methodDecl);
if (methodDecl.isCopyAssignmentOperator())
Expand Down
Loading