[flang-commits] [flang] [flang] add tbaa tags to global variables (PR #68727)

Renaud Kauffmann via flang-commits flang-commits at lists.llvm.org
Wed Oct 11 12:07:00 PDT 2023


================
@@ -406,7 +406,7 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v) {
         attributes.set(Attribute::Pointer);
     }
 
-  if (type == SourceKind::Global)
+  if (type == SourceKind::Global || type == SourceKind::Direct)
----------------
Renaud-K wrote:

Would it help, if I just said that %val does not come from a global variable. It comes from the heap because glbl is an allocatable. The box is global, but the data it wraps is not. 

In the example below glbl is a global
```module mod
real, dimension(1000) :: glbl
contains
subroutine func(arg)
  real, intent(in), dimension(:) :: arg
  call escape(glbl)
end subroutine
end module
```

In this case the SourceKind is global and we can infer that it cannot alias with anything else because of where it is physically located. But if you prefer to stick to Fortran rules, you can say that it is an F77 construct (since it does not have any target attribute) and therefore does not alias with anything. It would be non-conformant to pass it as an argument. 

```
fir.global @_QMmodEglbl : !fir.array<1000xf32> {
  %0 = fir.zero_bits !fir.array<1000xf32>
  fir.has_value %0 : !fir.array<1000xf32>
}
func.func @_QMmodPfunc(%arg0: !fir.box<!fir.array<?xf32>> {fir.bindc_name = "arg"}) {
  %c1000 = arith.constant 1000 : index
  %0 = fir.address_of(@_QMmodEglbl) : !fir.ref<!fir.array<1000xf32>>
  %1 = fir.shape %c1000 : (index) -> !fir.shape<1>
  %2 = fir.declare %0(%1) {uniq_name = "_QMmodEglbl"} : (!fir.ref<!fir.array<1000xf32>>, !fir.shape<1>) -> !fir.ref<!fir.array<1000xf32>>
  %3 = fir.declare %arg0 {fortran_attrs = #fir.var_attrs<intent_in>, uniq_name = "_QMmodFfuncEarg"} : (!fir.box<!fir.array<?xf32>>) -> !fir.box<!fir.array<?xf32>>
  fir.call @_QPescape(%2) fastmath<contract> : (!fir.ref<!fir.array<1000xf32>>) -> ()
  return
}
```

In the case of the Direct SourceKind, the outcome w.r.t. `arg` is the same but the rule is different. We have a direct memory access because, despite the box load and indirect fir.box_addr operations, we could establish that %val is indexing into glbl.  There is no target or pointer attribute on `arg` so we say that it cannot alias with it. 

A couple of observations here is:
1. Direct SourceKind by itself is not sufficient to determine aliasing, the attributes need to be looked at as well.
2. Unlike the global case, the rule for direct is not reflective. Aliasing is established w.r.t. another value.

Hopefully, this clarifies things. But if none of that helps I am now wondering if we cannot alter the rule to make it reflective. Instead of 
```
lhsSrc.kind == SourceKind::Direct && !rhsSrc.isTargetOrPointer()
```
could we have

```
lhsSrc.kind == SourceKind::Direct && !lhsSrc.isTargetOrPointer()
```

I will play with it today.

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


More information about the flang-commits mailing list