Issue 242536
Summary [flang] TBAA: the store into a buffer allocated by assignment to an allocatable scalar is tagged as unrelated to the variable
Labels flang:fir-hlfir
Assignees
Reporter vzakhari
    When an assignment to an unallocated allocatable scalar allocates its buffer, Flang tags the store into the new buffer as `allocated data`. Later reads of the variable load the buffer's address from the descriptor and are tagged with the variable's own type, such as `dummy arg data/<name>` or `direct data/<name>`. These types are siblings in Flang's TBAA tree, so TBAA answers NoAlias for a store and a load of the same memory.

```fortran
module realloc
  integer, allocatable :: g
  integer, allocatable, target :: t
contains
  subroutine dummy_var(j, n, r)
    integer, allocatable, intent(inout) :: j
    integer, intent(in) :: n
    integer, intent(out) :: r
    if (allocated(j)) deallocate(j)
    j = n                        ! allocates a new buffer and stores n into it
    r = j ! reads that buffer through the descriptor of j
  end subroutine
 subroutine save_var(n, r)
    integer, intent(in) :: n
    integer, intent(out) :: r
    integer, allocatable, save :: j
    if (allocated(j)) deallocate(j)
    j = n
    r = j
  end subroutine
  subroutine module_var(n, r)
    integer, intent(in) :: n
    integer, intent(out) :: r
    if (allocated(g)) deallocate(g)
    g = n
    r = g
  end subroutine
  subroutine module_target(n, r)
    integer, intent(in) :: n
 integer, intent(out) :: r
    if (allocated(t)) deallocate(t)
    t = n
 r = t
  end subroutine
  subroutine local_var(n, r)
    integer, intent(in) :: n
    integer, intent(out) :: r
    integer, allocatable :: j
    j = n
    r = j
  end subroutine
end module
```

The IR before any LLVM pass comes from this command (the redirect hides a note for every skipped pass):

```
$ flang -O2 -S -emit-llvm -mllvm -opt-bisect-limit=0 realloc.f90 -o realloc.ll 2>/dev/null
```

In `dummy_var`, the assignment `j = n` has two paths. When `j` is allocated, it stores through `j`'s descriptor with the tag of `j` (block `%23`). When it allocates a new buffer, it stores with the tag `allocated data` (block `%24`). The read `r = j` loads the address from the descriptor again and uses the tag of `j` (block `%27`):

```llvm
23:                                               ; preds = %17
  store i32 %18, ptr %20, align 4, !tbaa !12
  br label %27

24: ; preds = %17
  %25 = call ptr @malloc(i64 4)
  store i32 %18, ptr %25, align 4, !tbaa !14
  %26 = insertvalue { ptr, i64, i32, i8, i8, i8, i8 } { ptr undef, i64 4, i32 20240719, i8 0, i8 9, i8 2, i8 0 }, ptr %25, 0
  store { ptr, i64, i32, i8, i8, i8, i8 } %26, ptr %4, align 8, !tbaa !4
  call void @llvm.memcpy.p0.p0.i32(ptr %0, ptr %4, i32 24, i1 false), !tbaa !4
  br label %27

27:                                               ; preds = %23, %24
  call void @llvm.memcpy.p0.p0.i32(ptr align 8 %5, ptr align 8 %0, i32 24, i1 false), !tbaa !4
  %28 = getelementptr { ptr, i64, i32, i8, i8, i8, i8 }, ptr %5, i32 0, i32 0
  %29 = load ptr, ptr %28, align 8, !tbaa !4
 %30 = load i32, ptr %29, align 4, !tbaa !12
  store i32 %30, ptr %2, align 4, !tbaa !16
  ret void
}
```

```llvm
!10 = !{!"dummy arg data", !11, i64 0}
!11 = !{!"any data access", !6, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"dummy arg data/_QMreallocFdummy_varEj", !10, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"allocated data", !11, i64 0}
```

`aa-eval` gives the same answer for all procedures except `local_var`:

```
$ opt -passes='function(aa-eval)' -aa-pipeline=basic-aa,tbaa -evaluate-aa-metadata -print-all-alias-modref-info -disable-output realloc.ll
```

These are the lines of its output for the store into the new buffer and the later read. `opt` renumbers metadata, so the `!tbaa` attachments are left out:

```
_QMreallocPdummy_var:
 NoAlias:   %30 = load i32, ptr %29, align 4 <->   store i32 %18, ptr %25, align 4
_QMreallocPsave_var:
  NoAlias:   %29 = load i32, ptr %28, align 4 <->   store i32 %17, ptr %24, align 4
_QMreallocPmodule_var:
  NoAlias: %29 = load i32, ptr %28, align 4 <->   store i32 %17, ptr %24, align 4
_QMreallocPmodule_target:
  NoAlias:   %29 = load i32, ptr %28, align 4 <->   store i32 %17, ptr %24, align 4
_QMreallocPlocal_var:
  MayAlias: %22 = load i32, ptr %21, align 4 <->   store i32 %10, ptr %17, align 4
```

| procedure | variable | type of the read's tag | result |
|---|---|---|---|
| `dummy_var` | allocatable dummy argument | `dummy arg data/<name>` | NoAlias |
| `save_var` | allocatable with SAVE | `direct data/<name>` | NoAlias |
| `module_var` | allocatable module variable | `direct data/<name>` | NoAlias |
| `module_target` | allocatable module variable with TARGET | `target data/<name>` | NoAlias |
| `local_var` | local allocatable | `allocated data/<name>` | MayAlias |

In all five procedures, the store into the new buffer has the tag `allocated data`. Only the local variable is unaffected, because `allocated data/<name>` is a child of `allocated data`.

We saw this with `integer` and `real` scalars. Arrays, derived-type scalars and deferred-length character scalars are not affected in our tests. An array assignment stores into the new buffer through the updated descriptor, with the variable's tag. The other two are assigned by the runtime (`_FortranAAssign`).

Version: `flang version 24.0.0git (https://github.com/llvm/llvm-project 02ca8754da9a4c0083b4b8068481be6a9b72932b)`, on x86-64 Linux.

### Cause

`AddAliasTags` picks the tag from the source of the address. The new buffer comes from an allocation without a name, which gets the generic `allocated data` tag ([AddAliasTags.cpp L908-L915](https://github.com/llvm/llvm-project/blob/02ca8754da9a4c0083b4b8068481be6a9b72932b/flang/lib/Optimizer/Transforms/AddAliasTags.cpp#L908-L915)). The read's address comes from the variable's descriptor and gets the variable's tag: [L699-L717](https://github.com/llvm/llvm-project/blob/02ca8754da9a4c0083b4b8068481be6a9b72932b/flang/lib/Optimizer/Transforms/AddAliasTags.cpp#L699-L717) for dummy arguments and [L792-L811](https://github.com/llvm/llvm-project/blob/02ca8754da9a4c0083b4b8068481be6a9b72932b/flang/lib/Optimizer/Transforms/AddAliasTags.cpp#L792-L811) for module and SAVE variables.

### Impact

We have not seen wrong code from this. At `-O2`, all five procedures store `n` into `r` without reading the buffer. The optimizer forwards the new buffer's address through the descriptor, so the store and the read use the same pointer, and BasicAA answers MustAlias before TBAA is asked. Code in which the address is not forwarded could be affected. A similar inconsistency, in the tags of the local copies of VALUE dummy arguments, let dead store elimination remove stores that were needed (#215366).

The pattern also occurs in llvm-test-suite: `Fortran/gfortran/regression/save_5.f90` is compiled with `-fno-automatic`, so the allocatable scalar it assigns is SAVE.

Two possible fixes are to store into the new buffer through the updated descriptor, as array assignments do, so that the store gets the variable's tag, or to give accesses to unnamed allocations a tag that is not disjoint from the data of named variables.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to