[flang-commits] [flang] [flang][AA] Relax TARGET handling in getCallModRef for local variables (PR #199869)

via flang-commits flang-commits at lists.llvm.org
Wed May 27 08:46:08 PDT 2026


================
@@ -806,6 +806,73 @@ static bool isSavedLocal(const fir::AliasAnalysis::Source &src) {
   return false;
 }
 
+bool AliasAnalysis::mayBeCapturedBefore(mlir::Operation *declareOp,
+                                        mlir::Operation *op) {
+  if (!declareOp || !op)
+    return true;
+  auto funcOp = op->getParentOfType<mlir::FunctionOpInterface>();
+  if (!funcOp)
+    return true;
+  mlir::Operation *callAnchor = op;
+  while (callAnchor->getParentOp() && callAnchor->getParentOp() != funcOp)
+    callAnchor = callAnchor->getParentOp();
+
+  llvm::SmallVector<mlir::Value, 8> worklist;
+  llvm::SmallPtrSet<mlir::Value, 8> seen;
+  for (mlir::Value res : declareOp->getResults())
+    if (seen.insert(res).second)
+      worklist.push_back(res);
+
+  while (!worklist.empty()) {
+    mlir::Value v = worklist.pop_back_val();
+    for (mlir::OpOperand &use : v.getUses()) {
+      mlir::Operation *userOp = use.getOwner();
+      if (userOp == op || userOp == callAnchor)
+        continue;
+      if (userOp->getBlock() == callAnchor->getBlock() &&
+          callAnchor->isBeforeInBlock(userOp))
----------------
jeanPerier wrote:

What if the code is inside a loop (construct of CFG)?
Then, the operation that captured the TARGET could be after the call if the code is excuted several times. The call itself could be the previous code point that captured.
I think this needs to either use some CFG related utilities or some info obtained via some kind of dense flow analysis.

https://github.com/llvm/llvm-project/pull/199869


More information about the flang-commits mailing list