From 62fe67f9eb02cd5add1f70514fdcd1b324b14028 Mon Sep 17 00:00:00 2001 From: Jeff Niu Date: Sat, 30 Jul 2022 22:20:08 -0400 Subject: [mlir][DCA] Fix visiting call ops when run at function scopes When dead-code analysis is run at the scope of a function, call ops to other functions at the same level were being marked as unreachable, since the analysis optimistically assumes the call op to have no known predecessors and that all predecessors are known, but the callee would never get visited. This patch fixes the bug by checking if a referenced function is above the top-level op of the analysis, and is thus considered an external callable. Fixes #56830 Reviewed By: zero9178 Differential Revision: https://reviews.llvm.org/D130829 --- mlir/lib/Analysis/DataFlow/DeadCodeAnalysis.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'mlir/lib') diff --git a/mlir/lib/Analysis/DataFlow/DeadCodeAnalysis.cpp b/mlir/lib/Analysis/DataFlow/DeadCodeAnalysis.cpp index 327ef4f90b51..0f7a00c69eab 100644 --- a/mlir/lib/Analysis/DataFlow/DeadCodeAnalysis.cpp +++ b/mlir/lib/Analysis/DataFlow/DeadCodeAnalysis.cpp @@ -118,6 +118,7 @@ LogicalResult DeadCodeAnalysis::initialize(Operation *top) { } void DeadCodeAnalysis::initializeSymbolCallables(Operation *top) { + analysisScope = top; auto walkFn = [&](Operation *symTable, bool allUsesVisible) { Region &symbolTableRegion = symTable->getRegion(0); Block *symbolTableBlock = &symbolTableRegion.front(); @@ -278,14 +279,14 @@ LogicalResult DeadCodeAnalysis::visit(ProgramPoint point) { } void DeadCodeAnalysis::visitCallOperation(CallOpInterface call) { - Operation *callableOp = nullptr; - if (Value callableValue = call.getCallableForCallee().dyn_cast()) - callableOp = callableValue.getDefiningOp(); - else - callableOp = call.resolveCallable(&symbolTable); + Operation *callableOp = call.resolveCallable(&symbolTable); // A call to a externally-defined callable has unknown predecessors. - const auto isExternalCallable = [](Operation *op) { + const auto isExternalCallable = [this](Operation *op) { + // A callable outside the analysis scope is an external callable. + if (!analysisScope->isAncestor(op)) + return true; + // Otherwise, check if the callable region is defined. if (auto callable = dyn_cast(op)) return !callable.getCallableRegion(); return false; -- cgit v1.2.3