Skip to content

Commit 575a648

Browse files
authored
[flang] Follow memory source through more operations (#66713)
Add support for fir.box_addr, fir.array_corr, fir.coordinate, fir.embox, fir.rebox and fir.load. 1) Through the use of boolean `followBoxAddr` determine whether the analysis should apply to the address of the box or the address wrapped by the box. 2) Some asserts have been removed to allow for more SourceKinds though the flow, in a particular SourceKind::Direct 3) getSource was a public method but the returned type (SourceKind) was not public making it impossible to be called publicly 4) About 12 tests have been added to check for real Fortran scenarios 5) More tests will be added with HLFIR 6) A few TODOs have been identified and will need to be addressed in follow-up patches. I felt that more changes would increase the complexity of the patch.
1 parent c133fe1 commit 575a648

File tree

5 files changed

+812
-37
lines changed

5 files changed

+812
-37
lines changed

flang/include/flang/Optimizer/Analysis/AliasAnalysis.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace fir {
2020
//===----------------------------------------------------------------------===//
2121
// AliasAnalysis
2222
//===----------------------------------------------------------------------===//
23-
class AliasAnalysis {
23+
struct AliasAnalysis {
2424
// Structures to describe the memory source of a value.
2525

2626
/// Kind of the memory source referenced by a value.
@@ -36,11 +36,15 @@ class AliasAnalysis {
3636
/// Represents memory allocated outside of a function
3737
/// and passed to the function via host association tuple.
3838
HostAssoc,
39+
/// Represents direct memory access whose source cannot be further
40+
/// determined
41+
Direct,
3942
/// Represents memory allocated by unknown means and
4043
/// with the memory address defined by a memory reading
4144
/// operation (e.g. fir::LoadOp).
4245
Indirect,
43-
/// Represents memory allocated by unknown means.
46+
/// Starting point to the analysis whereby nothing is known about
47+
/// the source
4448
Unknown);
4549

4650
/// Attributes of the memory source object.
@@ -81,7 +85,6 @@ class AliasAnalysis {
8185
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
8286
const AliasAnalysis::Source &op);
8387

84-
public:
8588
/// Given two values, return their aliasing behavior.
8689
mlir::AliasResult alias(mlir::Value lhs, mlir::Value rhs);
8790

flang/lib/Optimizer/Analysis/AliasAnalysis.cpp

+92-23
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ static bool isDummyArgument(mlir::Value v) {
3636
return blockArg.getOwner()->isEntryBlock();
3737
}
3838

39+
/// Temporary function to skip through all the no op operations
40+
/// TODO: Generalize support of fir.load
41+
static mlir::Value getOriginalDef(mlir::Value v) {
42+
mlir::Operation *defOp;
43+
bool breakFromLoop = false;
44+
while (!breakFromLoop && (defOp = v.getDefiningOp())) {
45+
llvm::TypeSwitch<Operation *>(defOp)
46+
.Case<fir::ConvertOp>([&](fir::ConvertOp op) { v = op.getValue(); })
47+
.Case<fir::DeclareOp, hlfir::DeclareOp>(
48+
[&](auto op) { v = op.getMemref(); })
49+
.Default([&](auto op) { breakFromLoop = true; });
50+
}
51+
return v;
52+
}
53+
3954
namespace fir {
4055

4156
void AliasAnalysis::Source::print(llvm::raw_ostream &os) const {
@@ -82,10 +97,26 @@ AliasResult AliasAnalysis::alias(Value lhs, Value rhs) {
8297

8398
// Indirect case currently not handled. Conservatively assume
8499
// it aliases with everything
85-
if (lhsSrc.kind == SourceKind::Indirect ||
86-
lhsSrc.kind == SourceKind::Unknown ||
87-
rhsSrc.kind == SourceKind::Indirect || rhsSrc.kind == SourceKind::Unknown)
100+
if (lhsSrc.kind > SourceKind::Direct || rhsSrc.kind > SourceKind::Direct) {
88101
return AliasResult::MayAlias;
102+
}
103+
104+
// SourceKind::Direct is set for the addresses wrapped in a global boxes.
105+
// ie: fir.global @_QMpointersEp : !fir.box<!fir.ptr<f32>>
106+
// Though nothing is known about them, they would only alias with targets or
107+
// pointers
108+
bool directSourceToNonTargetOrPointer = false;
109+
if (lhsSrc.u != rhsSrc.u) {
110+
if ((lhsSrc.kind == SourceKind::Direct && !rhsSrc.isTargetOrPointer()) ||
111+
(rhsSrc.kind == SourceKind::Direct && !lhsSrc.isTargetOrPointer()))
112+
directSourceToNonTargetOrPointer = true;
113+
}
114+
115+
if (lhsSrc.kind == SourceKind::Direct ||
116+
rhsSrc.kind == SourceKind::Direct) {
117+
if (!directSourceToNonTargetOrPointer)
118+
return AliasResult::MayAlias;
119+
}
89120

90121
if (lhsSrc.kind == rhsSrc.kind) {
91122
if (lhsSrc.u == rhsSrc.u) {
@@ -103,9 +134,6 @@ AliasResult AliasAnalysis::alias(Value lhs, Value rhs) {
103134
lhsSrc.kind == SourceKind::Global)
104135
return AliasResult::NoAlias;
105136

106-
assert(lhsSrc.kind == SourceKind::Argument &&
107-
"unexpected memory source kind");
108-
109137
// Dummy TARGET/POINTER arguments may alias.
110138
if (lhsSrc.isTargetOrPointer() && rhsSrc.isTargetOrPointer())
111139
return AliasResult::MayAlias;
@@ -122,7 +150,7 @@ AliasResult AliasAnalysis::alias(Value lhs, Value rhs) {
122150
return AliasResult::NoAlias;
123151
}
124152

125-
assert(lhsSrc.kind != rhsSrc.kind && "memory source kinds must be the same");
153+
assert(lhsSrc.kind != rhsSrc.kind && "memory source kinds must be different");
126154

127155
Source *src1, *src2;
128156
if (lhsSrc.kind < rhsSrc.kind) {
@@ -133,18 +161,6 @@ AliasResult AliasAnalysis::alias(Value lhs, Value rhs) {
133161
src2 = &lhsSrc;
134162
}
135163

136-
assert(src2->kind <= SourceKind::HostAssoc &&
137-
"unexpected memory source kind");
138-
if (src1->kind == SourceKind::Allocate)
139-
return AliasResult::NoAlias;
140-
141-
assert(((src1->kind == SourceKind::Global &&
142-
(src2->kind == SourceKind::Argument ||
143-
src2->kind == SourceKind::HostAssoc)) ||
144-
(src1->kind == SourceKind::Argument &&
145-
src2->kind == SourceKind::HostAssoc)) &&
146-
"unexpected memory source kinds");
147-
148164
if (src1->kind == SourceKind::Argument &&
149165
src2->kind == SourceKind::HostAssoc) {
150166
// Treat the host entity as TARGET for the purpose of disambiguating
@@ -229,6 +245,7 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v) {
229245
mlir::Type ty;
230246
bool breakFromLoop{false};
231247
bool approximateSource{false};
248+
bool followBoxAddr{false};
232249
mlir::SymbolRefAttr global;
233250
Source::Attributes attributes;
234251
while (defOp && !breakFromLoop) {
@@ -244,22 +261,74 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v) {
244261
v = op->getOperand(0);
245262
defOp = v.getDefiningOp();
246263
})
264+
.Case<fir::BoxAddrOp>([&](auto op) {
265+
v = op->getOperand(0);
266+
defOp = v.getDefiningOp();
267+
if (mlir::isa<fir::BaseBoxType>(v.getType()))
268+
followBoxAddr = true;
269+
})
270+
.Case<fir::ArrayCoorOp, fir::CoordinateOp>([&](auto op) {
271+
v = op->getOperand(0);
272+
defOp = v.getDefiningOp();
273+
if (mlir::isa<fir::BaseBoxType>(v.getType()))
274+
followBoxAddr = true;
275+
approximateSource = true;
276+
})
277+
.Case<fir::EmboxOp, fir::ReboxOp>([&](auto op) {
278+
if (followBoxAddr) {
279+
v = op->getOperand(0);
280+
defOp = v.getDefiningOp();
281+
} else
282+
breakFromLoop = true;
283+
})
247284
.Case<fir::LoadOp>([&](auto op) {
248-
// No further tracking for addresses loaded from memory (e.g. a box)
249-
// right now.
285+
if (followBoxAddr && mlir::isa<fir::BaseBoxType>(op.getType())) {
286+
// For now, support the load of an argument or fir.address_of
287+
// TODO: generalize to all operations (in particular fir.alloca and
288+
// fir.allocmem)
289+
auto def = getOriginalDef(op.getMemref());
290+
if (isDummyArgument(def) ||
291+
def.template getDefiningOp<fir::AddrOfOp>()) {
292+
v = def;
293+
defOp = v.getDefiningOp();
294+
return;
295+
}
296+
}
297+
// No further tracking for addresses loaded from memory for now.
250298
type = SourceKind::Indirect;
251299
breakFromLoop = true;
252300
})
253301
.Case<fir::AddrOfOp>([&](auto op) {
254302
// Address of a global scope object.
255-
type = SourceKind::Global;
256303
ty = v.getType();
304+
305+
// When the global is a
306+
// fir.global @_QMpointersEp : !fir.box<!fir.ptr<f32>>
307+
// or
308+
// fir.global @_QMpointersEp : !fir.box<!fir.heap<f32>>
309+
//
310+
// and when following through the wrapped address, capture
311+
// the fact that there is nothing known about it. Therefore setting
312+
// the source to Direct.
313+
//
314+
// When not following the wrapped address, then consider the address
315+
// of the box, which has nothing to do with the wrapped address and
316+
// lies in the global memory space.
317+
if (followBoxAddr &&
318+
mlir::isa<fir::BaseBoxType>(fir::unwrapRefType(ty)))
319+
type = SourceKind::Direct;
320+
else
321+
type = SourceKind::Global;
322+
257323
if (fir::valueHasFirAttribute(v,
258324
fir::GlobalOp::getTargetAttrNameStr()))
259325
attributes.set(Attribute::Target);
260326

327+
// TODO: Take followBoxAddr into account when setting the pointer
328+
// attribute
261329
if (Source::isPointerReference(ty))
262330
attributes.set(Attribute::Pointer);
331+
263332
global = llvm::cast<fir::AddrOfOp>(op).getSymbol();
264333
breakFromLoop = true;
265334
})
@@ -278,7 +347,7 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v) {
278347
breakFromLoop = true;
279348
return;
280349
}
281-
350+
// TODO: Look for the fortran attributes present on the operation
282351
// Track further through the operand
283352
v = op.getMemref();
284353
defOp = v.getDefiningOp();

flang/test/Analysis/AliasAnalysis/alias-analysis-2.fir

+19-11
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
// p1.addr and p2.addr could both be wrapped inside boxes
1212
// CHECK-DAG: p1.addr#0 <-> boxp1.addr#0: MayAlias
1313
// CHECK-DAG: p2.addr#0 <-> boxp1.addr#0: MayAlias
14-
// CHECK-DAG: p1.addr#0 <-> arg2.addr#0: MayAlias
15-
// CHECK-DAG: p2.addr#0 <-> arg2.addr#0: MayAlias
14+
15+
// TODO: To really see aliasing, we should be looking at a load of p1.addr
16+
// p1.addr is just a local address holding the address to the data
17+
// CHECK-DAG: p1.addr#0 <-> arg2.addr#0: NoAlias
18+
// CHECK-DAG: p2.addr#0 <-> arg2.addr#0: NoAlias
1619

1720
// p1.addr and p2.addr are the result of an allocation
1821
// They cannot physically alias with an argument
@@ -41,7 +44,7 @@
4144
// pointer arguments
4245
// CHECK-DAG: arg2.addr#0 <-> func.region0#0: MayAlias
4346
// CHECK-DAG: arg2.addr#0 <-> func.region0#1: MayAlias
44-
// CHECK-DAG: arg2.addr#0 <-> func.region0#2: MayAlias
47+
// CHECK-DAG: arg2.addr#0 <-> func.region0#2: MustAlias
4548
// CHECK-DAG: boxp1.addr#0 <-> arg2.addr#0: MayAlias
4649

4750
func.func @_QFPtest(%arg0: !fir.ref<f32> {fir.bindc_name = "v1", fir.target}, %arg1: !fir.ref<f32> {fir.bindc_name = "v2", fir.target}, %arg2: !fir.ref<!fir.box<!fir.ptr<f32>>> ) attributes {test.ptr = "func"} {
@@ -99,7 +102,7 @@ func.func @_QFPtest(%arg0: !fir.ref<f32> {fir.bindc_name = "v1", fir.target}, %a
99102
// CHECK-DAG: func.region0#0 <-> func.region0#1: MayAlias
100103
// CHECK-DAG: func.region0#0 <-> func.region0#2: MayAlias
101104

102-
func.func @_QFPtest2(%arg0: !fir.ref<f32> {fir.bindc_name = "v1", fir.target}, %arg1: !fir.ref<!fir.box<!fir.ptr<f32>>>, %arg2: !fir.ref<!fir.ptr<f32>> ) attributes {test.ptr = "func"} {
105+
func.func @_QFPtest2(%arg0: !fir.ref<f32> {fir.bindc_name = "v1", fir.target}, %arg1: !fir.ref<!fir.box<!fir.ptr<f32>>>, %arg2: !fir.ref<!fir.box<!fir.ptr<f32>>> ) attributes {test.ptr = "func"} {
103106
return
104107
}
105108

@@ -131,24 +134,29 @@ func.func @_QFPtest2(%arg0: !fir.ref<f32> {fir.bindc_name = "v1", fir.target}, %
131134
// CHECK-DAG: p#0 <-> func.region0#0: MayAlias
132135
// CHECK-DAG: p#0 <-> func.region0#1: NoAlias
133136

134-
// FIXME: p and p1 are pointers, they cannot alias with a wrapped address.
135-
// Only the addresses they wrap could alias with the address wrapped by the box
136-
// CHECK-DAG: p#0 <-> box.addr#0: MayAlias
137+
// p could be pointing to var2
138+
// var2, being a target, could also be passed as argument arg0
139+
140+
// This was the wrong question to ask. We are asking if the address of box _QMpointersEp can
141+
// alias with the wrapped scalar _QFEvar2. We meant box_addr of _QMpointersEp
142+
// CHECK-DAG: p#0 <-> box.addr#0: NoAlias
143+
144+
// TODO: Still need to handle more gracefully the difference between !fir.ref<!fir.box<>> and !fir.box<>
137145
// CHECK-DAG: box.addr#0 <-> func.region0#0: MayAlias
138146

139147
// var2, although it is a target, cannot alias with p
140148
// A modification of p would only make them point to a new target but not modify it
141149
// CHECK-DAG: var2#0 <-> p#0: NoAlias
142150
// It can alias with p1, if p1 is a pointer component
143151
// CHECK-DAG: var2#0 <-> func.region0#0: MayAlias
144-
// It can alias with a box.addr
145-
// CHECK-DAG: var2#0 <-> box.addr#0: MayAlias
152+
// It is the same as box.addr
153+
// CHECK-DAG: var2#0 <-> box.addr#0: MustAlias
146154

147155
// A global may not alias with a dummy
148156
// CHECK-DAG: var2#0 <-> func.region0#1: NoAlias
149157

150-
// FIXME: a pointer may only alias with a target but arg1 is a regular dummy
151-
// CHECK-DAG: box.addr#0 <-> func.region0#1: MayAlias
158+
// A pointer may only alias with a target but arg1 is a regular dummy
159+
// CHECK-DAG: box.addr#0 <-> func.region0#1: NoAlias
152160

153161
// Dummy argument do not alias
154162
// CHECK-DAG: func.region0#0 <-> func.region0#1: NoAlias

0 commit comments

Comments
 (0)