Skip to content

[flang] Add FIR AliasAnalysis alias() wrapper to allow external getSource() method #115073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 6, 2024

Conversation

SusanTan
Copy link
Contributor

@SusanTan SusanTan commented Nov 5, 2024

Adding a wrapper around alias(mlir::Value lhs, mlir::Value rhs) to allow user to provide Source objects.

Copy link

github-actions bot commented Nov 5, 2024

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@SusanTan SusanTan marked this pull request as ready for review November 5, 2024 22:01
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Nov 5, 2024
@llvmbot
Copy link
Member

llvmbot commented Nov 5, 2024

@llvm/pr-subscribers-flang-fir-hlfir

Author: Susan Tan (SusanTan)

Changes
  1. Adding a wrapper around alias(mlir::Value lhs, mlir::Value rhs) to allow user to provide Source objects.
  2. extended getSource() to also handle fir::cg::declareOp

Full diff: https://github.com/llvm/llvm-project/pull/115073.diff

2 Files Affected:

  • (modified) flang/include/flang/Optimizer/Analysis/AliasAnalysis.h (+12-7)
  • (modified) flang/lib/Optimizer/Analysis/AliasAnalysis.cpp (+15-6)
diff --git a/flang/include/flang/Optimizer/Analysis/AliasAnalysis.h b/flang/include/flang/Optimizer/Analysis/AliasAnalysis.h
index 97c64dc3419524..d9953f580f401d 100644
--- a/flang/include/flang/Optimizer/Analysis/AliasAnalysis.h
+++ b/flang/include/flang/Optimizer/Analysis/AliasAnalysis.h
@@ -60,18 +60,18 @@ struct AliasAnalysis {
   //  module top
   //    real, pointer :: a(:)
   //  end module
-  //  
+  //
   //  subroutine test()
   //    use top
   //    a(1) = 1
   //  end subroutine
   //  -------------------------------------------------
-  // 
+  //
   //  flang -fc1 -emit-fir test.f90 -o test.fir
   //
   //  ------------------- test.fir --------------------
-  //  fir.global @_QMtopEa : !fir.box<!fir.ptr<!fir.array<?xf32>>> 
-  //  
+  //  fir.global @_QMtopEa : !fir.box<!fir.ptr<!fir.array<?xf32>>>
+  //
   //  func.func @_QPtest() {
   //    %c1 = arith.constant 1 : index
   //    %cst = arith.constant 1.000000e+00 : f32
@@ -100,12 +100,12 @@ struct AliasAnalysis {
   // Additionally, because it is relied on in HLFIR lowering, we allow querying
   // on a box SSA value, which is interpreted as querying on its data.
   //
-  // So in the above example, !fir.ref<f32> and !fir.box<!fir.ptr<!fir.array<?xf32>>> is data, 
+  // So in the above example, !fir.ref<f32> and !fir.box<!fir.ptr<!fir.array<?xf32>>> is data,
   // while !fir.ref<!fir.box<!fir.ptr<!fir.array<?xf32>>>> is not data.
 
   // This also applies to function arguments. In the example below, %arg0
   // is data, %arg1 is not data but a load of %arg1 is.
-  // 
+  //
   // func.func @_QFPtest2(%arg0: !fir.ref<f32>, %arg1: !fir.ref<!fir.box<!fir.ptr<f32>>> )  {
   //    %0 = fir.load %arg1 : !fir.ref<!fir.box<!fir.ptr<f32>>>
   //    ... }
@@ -183,6 +183,10 @@ struct AliasAnalysis {
   friend llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
                                        const AliasAnalysis::Source &op);
 
+  /// Given the values and their sources, return their aliasing behavior.
+  mlir::AliasResult alias(Source lhsSrc, Source rhsSrc, mlir::Value lhs,
+                          mlir::Value rhs);
+
   /// Given two values, return their aliasing behavior.
   mlir::AliasResult alias(mlir::Value lhs, mlir::Value rhs);
 
@@ -193,7 +197,8 @@ struct AliasAnalysis {
   /// If getInstantiationPoint is true, the search for the source
   /// will stop at [hl]fir.declare if it represents a dummy
   /// argument declaration (i.e. it has the dummy_scope operand).
-  Source getSource(mlir::Value, bool getInstantiationPoint = false);
+  fir::AliasAnalysis::Source getSource(mlir::Value,
+                                       bool getInstantiationPoint = false);
 
 private:
   /// Return true, if `ty` is a reference type to an object of derived type
diff --git a/flang/lib/Optimizer/Analysis/AliasAnalysis.cpp b/flang/lib/Optimizer/Analysis/AliasAnalysis.cpp
index 993d41633a0793..3b17f1a6ea15e1 100644
--- a/flang/lib/Optimizer/Analysis/AliasAnalysis.cpp
+++ b/flang/lib/Optimizer/Analysis/AliasAnalysis.cpp
@@ -12,6 +12,7 @@
 #include "flang/Optimizer/Dialect/FIRType.h"
 #include "flang/Optimizer/Dialect/FortranVariableInterface.h"
 #include "flang/Optimizer/HLFIR/HLFIROps.h"
+#include "flang/Optimizer/CodeGen/CGOps.h"
 #include "mlir/Analysis/AliasAnalysis.h"
 #include "mlir/Dialect/OpenMP/OpenMPDialect.h"
 #include "mlir/Dialect/OpenMP/OpenMPInterfaces.h"
@@ -38,7 +39,7 @@ static mlir::Value getOriginalDef(mlir::Value v) {
   while (!breakFromLoop && (defOp = v.getDefiningOp())) {
     llvm::TypeSwitch<Operation *>(defOp)
         .Case<fir::ConvertOp>([&](fir::ConvertOp op) { v = op.getValue(); })
-        .Case<fir::DeclareOp, hlfir::DeclareOp>(
+        .Case<fir::DeclareOp, hlfir::DeclareOp, fir::cg::XDeclareOp>(
             [&](auto op) { v = op.getMemref(); })
         .Default([&](auto op) { breakFromLoop = true; });
   }
@@ -130,18 +131,25 @@ bool AliasAnalysis::Source::mayBeActualArgWithPtr(
 }
 
 AliasResult AliasAnalysis::alias(mlir::Value lhs, mlir::Value rhs) {
+  // A wrapper around alias(Source lhsSrc, Source rhsSrc, mlir::Value lhs,
+  // mlir::Value rhs) This allows a user to provide Source that may be obtained
+  // through other dialects
+  auto lhsSrc = getSource(lhs);
+  auto rhsSrc = getSource(rhs);
+  return alias(lhsSrc, rhsSrc, lhs, rhs);
+}
+
+AliasResult AliasAnalysis::alias(Source lhsSrc, Source rhsSrc, mlir::Value lhs,
+                                 mlir::Value rhs) {
   // TODO: alias() has to be aware of the function scopes.
   // After MLIR inlining, the current implementation may
   // not recognize non-aliasing entities.
-  auto lhsSrc = getSource(lhs);
-  auto rhsSrc = getSource(rhs);
   bool approximateSource = lhsSrc.approximateSource || rhsSrc.approximateSource;
   LLVM_DEBUG(llvm::dbgs() << "\nAliasAnalysis::alias\n";
              llvm::dbgs() << "  lhs: " << lhs << "\n";
              llvm::dbgs() << "  lhsSrc: " << lhsSrc << "\n";
              llvm::dbgs() << "  rhs: " << rhs << "\n";
              llvm::dbgs() << "  rhsSrc: " << rhsSrc << "\n";);
-
   // Indirect case currently not handled. Conservatively assume
   // it aliases with everything
   if (lhsSrc.kind >= SourceKind::Indirect ||
@@ -463,7 +471,8 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v,
           // continue tracking.
           Operation *loadMemrefOp = op.getMemref().getDefiningOp();
           bool isDeclareOp = llvm::isa<fir::DeclareOp>(loadMemrefOp) ||
-                             llvm::isa<hlfir::DeclareOp>(loadMemrefOp);
+                             llvm::isa<hlfir::DeclareOp>(loadMemrefOp) ||
+                             llvm::isa<fir::cg::XDeclareOp>(loadMemrefOp);
           if (isDeclareOp &&
               llvm::isa<omp::TargetOp>(loadMemrefOp->getParentOp())) {
             v = op.getMemref();
@@ -492,7 +501,7 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v,
           global = llvm::cast<fir::AddrOfOp>(op).getSymbol();
           breakFromLoop = true;
         })
-        .Case<hlfir::DeclareOp, fir::DeclareOp>([&](auto op) {
+        .Case<hlfir::DeclareOp, fir::DeclareOp, fir::cg::XDeclareOp>([&](auto op) {
           if (omp::BlockArgOpenMPOpInterface argIface =
                   dyn_cast<omp::BlockArgOpenMPOpInterface>(op->getParentOp())) {
             Value ompValArg;

@SusanTan SusanTan changed the title [flang] Add FIR AliasAnalysis alias() wrapper and fir::cg::declareOp source tracing [flang] Add FIR AliasAnalysis alias() wrapper to allow external getSource() method Nov 5, 2024
@SusanTan SusanTan marked this pull request as draft November 5, 2024 22:23
@SusanTan SusanTan marked this pull request as ready for review November 5, 2024 22:24
@@ -12,6 +12,7 @@
#include "flang/Optimizer/Dialect/FIRType.h"
#include "flang/Optimizer/Dialect/FortranVariableInterface.h"
#include "flang/Optimizer/HLFIR/HLFIROps.h"
#include "flang/Optimizer/CodeGen/CGOps.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This include is not needed.

bool approximateSource = lhsSrc.approximateSource || rhsSrc.approximateSource;
LLVM_DEBUG(llvm::dbgs() << "\nAliasAnalysis::alias\n";
llvm::dbgs() << " lhs: " << lhs << "\n";
llvm::dbgs() << " lhsSrc: " << lhsSrc << "\n";
llvm::dbgs() << " rhs: " << rhs << "\n";
llvm::dbgs() << " rhsSrc: " << rhsSrc << "\n";);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra line should be OK to stay.

@razvanlupusoru
Copy link
Contributor

Looks great to me to make this improvement. By separating in this way, I can imagine how it would be useful for handling acc dialect operations (which FIR's AA cannot currently handle). Thank you.

I would appreciate @Renaud-K look at it and give his feedback.

Copy link
Contributor

@Renaud-K Renaud-K left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great. Strangely when I look at the changes from all commits, I am not seeing the new CG Op change. I am wondering if something did go wrong on a rebase or a push. You can always push the change again if it is the case.

@SusanTan
Copy link
Contributor Author

SusanTan commented Nov 5, 2024

This looks great. Strangely when I look at the changes from all commits, I am not seeing the new CG Op change. I am wondering if something did go wrong on a rebase or a push. You can always push the change again if it is the case.

I removed the CG changes because that was causing some test cases failing, and it's not necessary for downstream work.

Copy link
Contributor

@clementval clementval left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. note: If you use git-clang-format, it will only format the line you touch in your change so you don't have unrelated change in your PR.

@razvanlupusoru razvanlupusoru merged commit 57ab62a into llvm:main Nov 6, 2024
8 checks passed
Copy link

github-actions bot commented Nov 6, 2024

@SusanTan Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@SusanTan SusanTan deleted the susant/firaa branch February 14, 2025 14:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants