[flang-commits] [flang] [flang] Fix FIR AliasAnalysis for zero-offset view chains (PR #192710)

Razvan Lupusoru via flang-commits flang-commits at lists.llvm.org
Mon May 4 20:01:53 PDT 2026


================
@@ -365,10 +365,38 @@ static bool pathsDivergeAtComponent(const fir::AliasAnalysis::Source &lhsSrc,
   return false;
 }
 
+/// Walk backward from \p val through FortranObjectViewOpInterface ops
+/// that have zero offset (i.e. they access the same base address).
+/// Return true if \p other is found on this chain.
+static bool isOnZeroOffsetViewChain(mlir::Value val, mlir::Value other) {
+  while (auto *defOp = val.getDefiningOp()) {
+    auto viewOp = mlir::dyn_cast<fir::FortranObjectViewOpInterface>(defOp);
+    if (!viewOp)
+      break;
+    auto offset = viewOp.getViewOffset(mlir::cast<mlir::OpResult>(val));
+    if (!offset || *offset != 0)
+      break;
+    val = viewOp.getViewSource(mlir::cast<mlir::OpResult>(val));
+    if (val == other)
+      return true;
+  }
+  return false;
+}
+
 AliasResult AliasAnalysis::alias(mlir::Value lhs, mlir::Value rhs) {
   // A wrapper around alias(Source lhsSrc, Source rhsSrc, mlir::Value lhs,
   // mlir::Value rhs) This allows a user to provide Source that may be obtained
   // through other dialects
+
+  // If one value is directly derived from the other through a chain of
+  // zero-offset view operations (e.g. embox, declare, convert), they
+  // access the same underlying memory. This check avoids tracing through
+  // the full def-chain where upstream approximate sources (e.g. slices)
+  // would conservatively prevent MustAlias.
+  if (lhs == rhs || isOnZeroOffsetViewChain(lhs, rhs) ||
----------------
razvanlupusoru wrote:

The comment at the top of this function notes it is a wrapper - so this implementation is best moved to the 4 argument version of alias.

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


More information about the flang-commits mailing list