[flang-commits] [flang] [flang] Add FIR AliasAnalysis alias() wrapper and fir::cg::declareOp source tracing (PR #115073)

Susan Tan via flang-commits flang-commits at lists.llvm.org
Tue Nov 5 13:49:05 PST 2024


https://github.com/SusanTan updated https://github.com/llvm/llvm-project/pull/115073

>From bd6c52e05998106b0ee56327f16819fd54499dea Mon Sep 17 00:00:00 2001
From: Susan Tan <zujunt at genoa1.pgi.net>
Date: Thu, 31 Oct 2024 15:41:47 -0700
Subject: [PATCH 1/2] add XDeclareOp from CG

---
 flang/lib/Optimizer/Analysis/AliasAnalysis.cpp | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/flang/lib/Optimizer/Analysis/AliasAnalysis.cpp b/flang/lib/Optimizer/Analysis/AliasAnalysis.cpp
index 8b7918744017cc..6502f8aba399bb 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; });
   }
@@ -440,7 +441,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();
@@ -469,7 +471,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 declare operation is inside omp target region,
           // continue alias analysis outside the target region
           if (auto targetOp =

>From 1f86ad5b02bd17e62ede3ecf397f022be0c8011b Mon Sep 17 00:00:00 2001
From: Susan Tan <zujunt at genoa1.pgi.net>
Date: Tue, 5 Nov 2024 13:27:19 -0800
Subject: [PATCH 2/2] adding a wrapper around alias to allow separate
 implementation of getSource()

---
 .../flang/Optimizer/Analysis/AliasAnalysis.h  | 19 ++++++++++++-------
 .../lib/Optimizer/Analysis/AliasAnalysis.cpp  | 13 ++++++++++---
 2 files changed, 22 insertions(+), 10 deletions(-)

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 8b7918744017cc..40edac7e28fedf 100644
--- a/flang/lib/Optimizer/Analysis/AliasAnalysis.cpp
+++ b/flang/lib/Optimizer/Analysis/AliasAnalysis.cpp
@@ -130,18 +130,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 ||



More information about the flang-commits mailing list