[flang-commits] [flang] [flang] Respect record type changes in FIR alias analysis access paths (PR #231655)

Tom Eccles via flang-commits flang-commits at lists.llvm.org
Thu Oct 15 04:32:39 PDT 2026


================
@@ -573,15 +576,90 @@ static bool noAliasBasedOnType(mlir::Value lhs, mlir::Value rhs) {
   return false;
 }
 
-/// Return true if two access paths from the same origin variable diverge at
-/// a named component step, meaning they address disjoint subobjects of the
-/// root variable. For example, paths [Component("a")] and [Component("b")]
-/// diverge immediately, while [Component("a"), Component("x")] and
-/// [Component("a"), Component("y")] share a common prefix "a" and diverge
-/// at the second step.
+/// Return true if an object of type \p ty contains, or is, an object of type
+/// \p subTy (through array elements and components, not through
+/// descriptors).
+static bool typeContains(mlir::Type ty, mlir::Type subTy) {
+  if (ty == subTy)
+    return true;
+  if (auto seqTy = mlir::dyn_cast<fir::SequenceType>(ty))
+    return typeContains(seqTy.getElementType(), subTy);
+  if (auto recTy = mlir::dyn_cast<fir::RecordType>(ty))
+    return llvm::any_of(recTy.getTypeList(), [&](const auto &field) {
+      return typeContains(field.second, subTy);
+    });
+  return false;
+}
+
+/// Return the type of the parent component of \p recTy, if any. The parent
+/// component is the first component, named after the parent type.
+static fir::RecordType getParentType(fir::RecordType recTy) {
+  if (recTy.getNumFields() == 0)
+    return {};
+  const auto &[name, fieldTy] = recTy.getTypeList().front();
+  auto parentTy = mlir::dyn_cast<fir::RecordType>(fieldTy);
+  if (!parentTy ||
+      fir::NameUniquer::deconstruct(parentTy.getName()).second.name != name)
----------------
tblah wrote:

My AI reviewer came up with a case where the same name is used from different modules to confuse this check:
```
module ma
  type t
    sequence
    real :: a(4)
  end type
contains
  subroutine shift(x)
    type(t) :: x
    x%a(2:4) = x%a(1:3)
  end subroutine
end module
module mb
  type t
    sequence
    real :: a(4)
  end type
end module
program test
  use ma, only: ta => t, shift
  use mb, only: tb => t
  type outer
    type(ta) :: t
    type(tb) :: other
  end type
  type(outer) :: obj
  obj%other%a = [1.,2.,3.,4.]
  call shift(obj%other)
  print *, obj%other%a
  if (any(obj%other%a /= [1.,1.,2.,3.])) stop 1
end program
```

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


More information about the flang-commits mailing list