From 86a03367bf62375cf0cc299a56f09bed5f0c4875 Mon Sep 17 00:00:00 2001 From: Slava Zakharin Date: Mon, 21 Apr 2025 08:59:41 -0700 Subject: [PATCH] [flang] Support fir.pack_array in FIR alias analysis. (#131946) `fir.pack_array` is just a pass-through op for the process of finding the source in FIR alias analysis (as defined in #127147). --- .../lib/Optimizer/Analysis/AliasAnalysis.cpp | 8 ++++ .../alias-analysis-pack-array.fir | 43 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 flang/test/Analysis/AliasAnalysis/alias-analysis-pack-array.fir diff --git a/flang/lib/Optimizer/Analysis/AliasAnalysis.cpp b/flang/lib/Optimizer/Analysis/AliasAnalysis.cpp index 2ff8ec05782c..bda98871a1c7 100644 --- a/flang/lib/Optimizer/Analysis/AliasAnalysis.cpp +++ b/flang/lib/Optimizer/Analysis/AliasAnalysis.cpp @@ -542,6 +542,14 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v, v = op->getOperand(0); defOp = v.getDefiningOp(); }) + .Case([&](auto op) { + // The packed array is not distinguishable from the original + // array, so skip PackArrayOp and track further through + // the array operand. + v = op.getArray(); + defOp = v.getDefiningOp(); + approximateSource = true; + }) .Case([&](auto op) { v = op->getOperand(0); defOp = v.getDefiningOp(); diff --git a/flang/test/Analysis/AliasAnalysis/alias-analysis-pack-array.fir b/flang/test/Analysis/AliasAnalysis/alias-analysis-pack-array.fir new file mode 100644 index 000000000000..06d090407a26 --- /dev/null +++ b/flang/test/Analysis/AliasAnalysis/alias-analysis-pack-array.fir @@ -0,0 +1,43 @@ +// Test alias analysis queries for dummy arguments wired +// through fir.pack_array. +// fir.pack_array is a pass-through operation for FIR alias analysis. +// RUN: fir-opt %s --test-fir-alias-analysis -split-input-file --mlir-disable-threading 2>&1 | FileCheck %s + +// The two pointers referencing two different maybe repacked +// versions of the original dummy arguments do not alias: +// CHECK-DAG: test1_y_repack(1)#0 <-> test1_x_repack(1)#0: NoAlias +// CHECK-DAG: test1_x_orig(1)#0 <-> test1_y_orig(1)#0: NoAlias + +// Repacked dummy does not alias with another original dummy: +// CHECK-DAG: test1_y_repack(1)#0 <-> test1_x_orig(1)#0: NoAlias +// CHECK-DAG: test1_x_repack(1)#0 <-> test1_y_orig(1)#0: NoAlias + +// Repacked dummy may alias with its original: +// CHECK-DAG: test1_x_repack(1)#0 <-> test1_x_orig(1)#0: MayAlias +// CHECK-DAG: test1_y_repack(1)#0 <-> test1_y_orig(1)#0: MayAlias + +// Ideally, these should report MustAlias, but MayAlias +// may work as well: +// CHECK-DAG: test1_y_repack(1)#0 <-> test1_y_repack2(1)#0: MayAlias +// CHECK-DAG: test1_x_repack(1)#0 <-> test1_x_repack2(1)#0: MayAlias + + +func.func @_QFtest1(%arg0: !fir.box> {fir.bindc_name = "x"}, %arg1: !fir.box> {fir.bindc_name = "y"}) { + %c1 = arith.constant 1 : index + %0 = fir.dummy_scope : !fir.dscope + %1 = fir.pack_array %arg0 heap whole : (!fir.box>) -> !fir.box> + %2:2 = hlfir.declare %1 dummy_scope %0 {uniq_name = "_QFtest1Ex"} : (!fir.box>, !fir.dscope) -> (!fir.box>, !fir.box>) + %3 = fir.pack_array %arg1 heap whole : (!fir.box>) -> !fir.box> + %4:2 = hlfir.declare %3 dummy_scope %0 {uniq_name = "_QFtest1Ey"} : (!fir.box>, !fir.dscope) -> (!fir.box>, !fir.box>) + %5 = fir.box_addr %4#0 {test.ptr = "test1_y_repack(1)"} : (!fir.box>) -> !fir.ref + %52 = fir.box_addr %4#0 {test.ptr = "test1_y_repack2(1)"} : (!fir.box>) -> !fir.ref + %6 = fir.load %5 : !fir.ref + %7 = fir.box_addr %2#0 {test.ptr = "test1_x_repack(1)"} : (!fir.box>) -> !fir.ref + %72 = fir.box_addr %2#0 {test.ptr = "test1_x_repack2(1)"} : (!fir.box>) -> !fir.ref + hlfir.assign %6 to %7 : f32, !fir.ref + fir.unpack_array %3 to %arg1 heap : !fir.box> + fir.unpack_array %1 to %arg0 heap : !fir.box> + %8 = fir.box_addr %arg0 {test.ptr = "test1_x_orig(1)"} : (!fir.box>) -> !fir.ref + %9 = fir.box_addr %arg1 {test.ptr = "test1_y_orig(1)"} : (!fir.box>) -> !fir.ref + return +}