[flang-commits] [flang] [flang][AddAliasTags] Generalise tbaa tags beyond functions (PR #92379)

Tom Eccles via flang-commits flang-commits at lists.llvm.org
Thu May 23 08:52:23 PDT 2024


tblah wrote:

> Hi Tom, it looks okay to me, but I do not really see what useful TBAA information we can generate for standalone isolated-from-above OpenMP operations. Can you please give some examples of how this may evolve and become useful?
> 
> It seems to me that we can only attach some useful TBAA tags after such operations are inlined into their users' contexts, but then this change does not really apply to this case.

Hi, thanks for taking a look.

Here is an example reduction declaration for a fixed size array:
```
omp.declare_reduction @add_reduction_byref_box_3xi32 : !fir.ref<!fir.box<!fir.array<3xi32>>> init {
  ^bb0(%arg0: !fir.ref<!fir.box<!fir.array<3xi32>>>):
    // Allocate and initialize a thread private copy of the reduction variable
    // [...]
    omp.yield(%1 : !fir.ref<!fir.box<!fir.array<3xi32>>>)
  } combiner {
  ^bb0(%arg0: !fir.ref<!fir.box<!fir.array<3xi32>>>, %arg1: !fir.ref<!fir.box<!fir.array<3xi32>>>):
    %0 = fir.load %arg0 : !fir.ref<!fir.box<!fir.array<3xi32>>>
    %1 = fir.load %arg1 : !fir.ref<!fir.box<!fir.array<3xi32>>>
    %c0 = arith.constant 0 : index
    %2:3 = fir.box_dims %0, %c0 : (!fir.box<!fir.array<3xi32>>, index) -> (index, index, index)
    %3 = fir.shape_shift %2#0, %2#1 : (index, index) -> !fir.shapeshift<1>
    %c1 = arith.constant 1 : index
    fir.do_loop %arg2 = %c1 to %2#1 step %c1 unordered {
      %4 = fir.array_coor %0(%3) %arg2 : (!fir.box<!fir.array<3xi32>>, !fir.shapeshift<1>, index) -> !fir.ref<i32>
      %5 = fir.array_coor %1(%3) %arg2 : (!fir.box<!fir.array<3xi32>>, !fir.shapeshift<1>, index) -> !fir.ref<i32>
      %6 = fir.load %4 : !fir.ref<i32>
      %7 = fir.load %5 : !fir.ref<i32>
      %8 = arith.addi %6, %7 : i32
      fir.store %8 to %4 : !fir.ref<i32>
    }
    omp.yield(%arg0 : !fir.ref<!fir.box<!fir.array<3xi32>>>)
  }  cleanup {
   // clean up allocations in the init region
   // [...]
  }
```

It would be great if that do-loop could be vectorized for large arrays. Alias analysis could help with this.

You are right that one approach would be to do all of the inlining in MLIR and add TBAA tags afterwards. This is not how OpenMP currently works. There was a desire to share as much OpenMP codegen with clang as possible so these are only inlined (and outlined) when the MLIR LLVM+OpenMP dialects are transformed into LLVM IR.

Given this design, I would prefer to adapt TBAA tags to understand openmp operations before they are inlined. In this case, the two block arguments do not alias (fir::AliasAnalysis doesn't yet know this and I have not added that to this PR).

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


More information about the flang-commits mailing list