This is an archive of the discontinued LLVM Phabricator instance.

Infrastructure for alias analysis in flang
ClosedPublic

Authored by Renaud-K on Oct 27 2022, 2:50 PM.

Details

Summary

These changes provide a development framework for the development of aliasing analysis in flang which will depend on the FIR dialect.
See https://github.com/llvm/llvm-project/blob/main/flang/docs/AliasingAnalysisFIR.md and https://discourse.llvm.org/t/alias-analysis-in-llvm-flang/62639/16

There is an emphasis on reusing/leveraging the mlir::AliasAnalysis and the MLIR testing passes for alias analysis.
There are no substantial changes in MLIR other than sharing code between test passes through a base class the passes can inherit from.
There might need to be more changes in MLIR in the future but I did not want these to be lost in this noise.

With these changes we will explore the possibility of stacking up several alias analysis with mlir::AliasAnalysis::addAnalysisImplementation

Changes include:

  1. Adding an Analysis/AliasAnalysis directory in flang which right now is doing a mock alias analysis
  2. Adding a lib directory under flang/test to allow for test passes to be built.
  3. Registering the test passes with fir-opt. Mimicking what is done with mlir-opt
  4. Moved the MLIR testing code into a common based class that can be reused among passes

Diff Detail

Event Timeline

Renaud-K created this revision.Oct 27 2022, 2:50 PM
Renaud-K requested review of this revision.Oct 27 2022, 2:50 PM
Renaud-K updated this revision to Diff 471355.Oct 27 2022, 6:43 PM

Needed to run clang-format on a couple of files.

The Flang changes look good to me (a small nit). The MLIR changes make some sense to me, but I will leave @riddle or someone from MLIR comment about sharing that MLIR testing infrastructure.

flang/lib/Lower/IntrinsicCall.cpp
5007

Did you intend for the parenthesis to wrap sizeof() / sizeof() instead of just the second sizeof ?

flang/test/lib/Analysis/AliasAnalysis/CMakeLists.txt
29

Does this work in flang out of tree builds ?

clementval added inline comments.Oct 31 2022, 3:07 AM
flang/test/lib/Analysis/AliasAnalysis/alias-analysis-1.fir
18–19

Is there a reason to split the input file here since you do not add any check lines after? Same for the split below.

Renaud-K added inline comments.Oct 31 2022, 9:29 AM
flang/lib/Lower/IntrinsicCall.cpp
5007

Just the second sizeof. This helps with a strange clang compilation error. I have the issue with clang 11 and up.

 expression does not compute the number of elements in this array; element type is 'const IntrinsicDummyArgument', not 'decltype(*rules.args)' (aka 'const IntrinsicDummyArgument &') [-Werror,-Wsizeof-array-div]
  assert(position < sizeof(rules.args) / sizeof(decltype(*rules.args)) &&
                           ~~~~~~~~~~  ^
/usr/include/assert.h:90:27: note: expanded from macro 'assert'
     (static_cast <bool> (expr)                                         \
                          ^~~~
/local/home/rkauffmann/clients/llvm-project-mirror/flang/lib/Lower/IntrinsicCall.cpp:5007:40: note: place parentheses around the 'sizeof(decltype(*rules.args))' expression to silence this warning
1 error generated.

place parentheses around the 'sizeof(decltype(*rules.args))' expression to silence this warning

flang/test/lib/Analysis/AliasAnalysis/alias-analysis-1.fir
18–19

There will be a message issued for the empty functions that will be interspersed with the other functions. -split-input-file helps with that. Though I was not a fan of it either. The output looks good on the terminal where stdout and stderr are properly interleaved but as soon as the terminal output is redirected through a pipe or a file the two are separated and it is hard to see which stdout corresponds to which stderr.

The pass itself is a generic op pass, it does not expect a function or check for empty functions which would have been my preference. Let me know if you know a better way.

PeteSteinfeld accepted this revision.Oct 31 2022, 9:57 AM
PeteSteinfeld added a subscriber: PeteSteinfeld.

Aside from my one nit comment, all builds and tests correctly and looks good.

flang/lib/Optimizer/Analysis/AliasAnalysis.cpp
32

"notable" rather than "notables"

This revision is now accepted and ready to land.Oct 31 2022, 9:57 AM
vzakhari added inline comments.Oct 31 2022, 10:41 AM
flang/test/lib/Analysis/AliasAnalysis/TestAliasAnalysis.cpp
73

Please add a new line.

mlir/test/lib/Analysis/TestAliasAnalysis.h
38

Please add new line.

Renaud-K added inline comments.Oct 31 2022, 1:07 PM
flang/test/lib/Analysis/AliasAnalysis/CMakeLists.txt
29

It works because there is no such thing as a flang only repo. Based on these instructions https://github.com/llvm/llvm-project/blob/main/flang/README.md, it is still expected to do a clone of the entire llvm-project for the stand-alone build. The MLIR sources are always going to be available.

Renaud-K updated this revision to Diff 472135.Oct 31 2022, 2:28 PM

Added lines at end of files.
Corrected typo

vzakhari added inline comments.Nov 2 2022, 12:40 PM
flang/test/lib/Analysis/AliasAnalysis/CMakeLists.txt
29

I guess a more elegant way to get the right path is to use get_target_property(INC_PATH MLIRTestAnalysis SOURCE_DIR) OR set ${CMAKE_CURRENT_SOURCE_DIR} as INTERFACE in target_include_directories inside mlir/test/lib/Analysis/CMakeLists.txt for MLIRTestAnalysis target and add a dependency on this target here.

Renaud-K added inline comments.Nov 2 2022, 3:09 PM
flang/test/lib/Analysis/AliasAnalysis/CMakeLists.txt
29

I am not sure it is better because you can no longer do

#include "mlir/test/lib/Analysis/TestAliasAnalysis.h"

but have to do

#include "TestAliasAnalysis.h"

instead which is misleading

Is there a variable pointing to the root ?

Renaud-K added inline comments.Nov 2 2022, 3:17 PM
flang/test/lib/Analysis/AliasAnalysis/CMakeLists.txt
29

I could use ${CMAKE_SOURCE_DIR}/..

vzakhari added inline comments.Nov 2 2022, 3:19 PM
flang/test/lib/Analysis/AliasAnalysis/CMakeLists.txt
29

I see what you mean.

I believe ${MLIR_MAIN_SRC_DIR} is pointing to mlir source directory in both in-tree (defined in mlir/CMakeLists.txt) and standalone build (defined in ${MLIR_INSTALL_PREFIX}/lib/cmake/mlir/MLIRConfig.cmake), so if you use it you may include test/lib/Analysis/TestAliasAnalysis.h

Renaud-K added inline comments.Nov 2 2022, 3:24 PM
flang/test/lib/Analysis/AliasAnalysis/CMakeLists.txt
29

All our MLIR includes start with "mlir/...."
So I like ${CMAKE_SOURCE_DIR}/.. better

Renaud-K added inline comments.Nov 2 2022, 3:38 PM
flang/test/lib/Analysis/AliasAnalysis/CMakeLists.txt
29

I could go with ${MLIR_MAIN_SRC_DIR}/.. but there seems to be a bug in our to CMakeLists.txt file:

set(MLIR_MAIN_SRC_DIR ${LLVM_MAIN_SRC_DIR}/../mlir/include ) # --src-root
set(MLIR_INCLUDE_DIR ${LLVM_MAIN_SRC_DIR}/../mlir/include ) # --includedir

We have set MLIR_MAIN_SRC_DIR and MLIR_INCLUDE_DIR to the same path

While in MLIR they are set as:

set(MLIR_MAIN_SRC_DIR     ${CMAKE_CURRENT_SOURCE_DIR}  )
set(MLIR_MAIN_INCLUDE_DIR ${MLIR_MAIN_SRC_DIR}/include )
Renaud-K updated this revision to Diff 473010.Nov 3 2022, 1:22 PM

Using MLIR_MAIN_SRC_DIR instead of CMAKE_SOURCE_DIR because

  1. It's definition is now fixed
  2. It works for both in and out-of-tree builds
Renaud-K updated this revision to Diff 473309.Nov 4 2022, 12:12 PM

Needed to fix test because of change https://reviews.llvm.org/D134900

This revision was automatically updated to reflect the committed changes.

The changes here LGTM!

rriddle added inline comments.Nov 7 2022, 6:32 PM
flang/lib/Optimizer/Analysis/AliasAnalysis.cpp
33

Can you add me to the data flow analysis review when you have that? I'd be interested in seeing what is necessary there/if we could promote stuff to just live in MLIR.

Renaud-K added inline comments.Nov 8 2022, 8:35 AM
flang/lib/Optimizer/Analysis/AliasAnalysis.cpp
33

Will do.

nikic added a subscriber: nikic.Mar 20 2023, 3:41 AM
nikic added inline comments.
flang/test/lib/Analysis/AliasAnalysis/CMakeLists.txt
29

Independently of how the include is performed, this is still a layering violation: Flang is using a private header (part of lib/ rather than include/) and linking against a private library (MLIRTestXYZ) here.

This header should be publicly exported if other (sub)projects want to make use of it.