Skip to content

Commit 702664e

Browse files
[flang] Improve alias analysis to be precise for box and box.base_addr (#80335)
After PR#68727 the source for both the fir.box_addr and a box became the same. Thus the detection that only one of the sources was direct and the special logic around it was being skipped. As a result, the test included would show a "MayAlias" result instead of a "NoAlias" result.
1 parent 0881d0f commit 702664e

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

flang/lib/Optimizer/Analysis/AliasAnalysis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ AliasResult AliasAnalysis::alias(Value lhs, Value rhs) {
106106
// Though nothing is known about them, they would only alias with targets or
107107
// pointers
108108
bool directSourceToNonTargetOrPointer = false;
109-
if (lhsSrc.u != rhsSrc.u) {
109+
if (lhsSrc.u != rhsSrc.u || lhsSrc.kind != rhsSrc.kind) {
110110
if ((lhsSrc.kind == SourceKind::Direct && !rhsSrc.isTargetOrPointer()) ||
111111
(rhsSrc.kind == SourceKind::Direct && !lhsSrc.isTargetOrPointer()))
112112
directSourceToNonTargetOrPointer = true;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// RUN: fir-opt %s -pass-pipeline='builtin.module(func.func(test-fir-alias-analysis))' 2>&1 | FileCheck %s
2+
3+
// Exercise that a box reference does not alias with the address loaded from the box
4+
// module mm
5+
// real, allocatable :: arr(:)
6+
// contains
7+
// subroutine sub
8+
// arr(1) = ubound(arr, 1)
9+
// end subroutine
10+
// end module
11+
12+
// CHECK: box#0 <-> boxaddr#0: NoAlias
13+
14+
fir.global @_QMmmEarr : !fir.box<!fir.heap<!fir.array<?xf32>>> {
15+
%c0 = arith.constant 0 : index
16+
%0 = fir.zero_bits !fir.heap<!fir.array<?xf32>>
17+
%1 = fir.shape %c0 : (index) -> !fir.shape<1>
18+
%2 = fir.embox %0(%1) : (!fir.heap<!fir.array<?xf32>>, !fir.shape<1>) -> !fir.box<!fir.heap<!fir.array<?xf32>>>
19+
fir.has_value %2 : !fir.box<!fir.heap<!fir.array<?xf32>>>
20+
}
21+
func.func @_QMmmPsub() {
22+
%c1 = arith.constant 1 : index
23+
%c1_i64 = arith.constant 1 : i64
24+
%c0 = arith.constant 0 : index
25+
%0 = fir.address_of(@_QMmmEarr) : !fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>>
26+
%1 = fir.declare %0 {fortran_attrs = #fir.var_attrs<allocatable>, uniq_name = "_QMmmEarr", test.ptr = "box"} : (!fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>>) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>>
27+
%2 = fir.load %1 : !fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>>
28+
%3:3 = fir.box_dims %2, %c0 : (!fir.box<!fir.heap<!fir.array<?xf32>>>, index) -> (index, index, index)
29+
%4 = fir.convert %3#1 : (index) -> i64
30+
%5 = fir.convert %3#0 : (index) -> i64
31+
%6 = arith.addi %4, %5 : i64
32+
%7 = arith.subi %6, %c1_i64 : i64
33+
%8 = fir.convert %7 : (i64) -> i32
34+
%9 = fir.convert %8 : (i32) -> f32
35+
%10 = fir.box_addr %2 {test.ptr = "boxaddr"} : (!fir.box<!fir.heap<!fir.array<?xf32>>>) -> !fir.heap<!fir.array<?xf32>>
36+
%11 = fir.shape_shift %3#0, %3#1 : (index, index) -> !fir.shapeshift<1>
37+
%12 = fir.array_coor %10(%11) %c1 : (!fir.heap<!fir.array<?xf32>>, !fir.shapeshift<1>, index) -> !fir.ref<f32>
38+
fir.store %9 to %12 : !fir.ref<f32>
39+
return
40+
}

0 commit comments

Comments
 (0)