Skip to content

Commit 657c5bf

Browse files
authored
[flang][AliasAnalysis] don't crash on load from blockarg (#120760)
Values can have no defining operation when the value is a blockarg. I wrote this as a test for hlfir bufferization rather than alias analysis because I couldn't find a way to add the test.ptr attribute to a block argument.
1 parent 24fc8f0 commit 657c5bf

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

flang/lib/Optimizer/Analysis/AliasAnalysis.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,8 +599,9 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v,
599599
// If load is inside target and it points to mapped item,
600600
// continue tracking.
601601
Operation *loadMemrefOp = op.getMemref().getDefiningOp();
602-
bool isDeclareOp = llvm::isa<fir::DeclareOp>(loadMemrefOp) ||
603-
llvm::isa<hlfir::DeclareOp>(loadMemrefOp);
602+
bool isDeclareOp =
603+
llvm::isa_and_present<fir::DeclareOp>(loadMemrefOp) ||
604+
llvm::isa_and_present<hlfir::DeclareOp>(loadMemrefOp);
604605
if (isDeclareOp &&
605606
llvm::isa<omp::TargetOp>(loadMemrefOp->getParentOp())) {
606607
v = op.getMemref();
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// RUN: fir-opt %s --opt-bufferization | FileCheck %s
2+
3+
// Test that alias analysis doesn't crash determining if the arguments to
4+
// hlfir.assign alias.
5+
// CHECK: omp.private {type = firstprivate} @_QFFbEl_firstprivate_box_Uxi32
6+
7+
// TODO: we can't currently optimize this assign because alias analysis doesn't
8+
// know that the block arguments of the copy region cannot alias.
9+
10+
omp.private {type = firstprivate} @_QFFbEl_firstprivate_box_Uxi32 : !fir.ref<!fir.box<!fir.array<?xi32>>> alloc {
11+
^bb0(%arg0: !fir.ref<!fir.box<!fir.array<?xi32>>>):
12+
%0 = fir.load %arg0 : !fir.ref<!fir.box<!fir.array<?xi32>>>
13+
%c0 = arith.constant 0 : index
14+
%1:3 = fir.box_dims %0, %c0 : (!fir.box<!fir.array<?xi32>>, index) -> (index, index, index)
15+
%2 = fir.shape %1#1 : (index) -> !fir.shape<1>
16+
%3 = fir.allocmem !fir.array<?xi32>, %1#1 {bindc_name = ".tmp", uniq_name = ""}
17+
%true = arith.constant true
18+
%4:2 = hlfir.declare %3(%2) {uniq_name = ".tmp"} : (!fir.heap<!fir.array<?xi32>>, !fir.shape<1>) -> (!fir.box<!fir.array<?xi32>>, !fir.heap<!fir.array<?xi32>>)
19+
%c0_0 = arith.constant 0 : index
20+
%5:3 = fir.box_dims %0, %c0_0 : (!fir.box<!fir.array<?xi32>>, index) -> (index, index, index)
21+
%6 = fir.shape_shift %5#0, %5#1 : (index, index) -> !fir.shapeshift<1>
22+
%7 = fir.rebox %4#0(%6) : (!fir.box<!fir.array<?xi32>>, !fir.shapeshift<1>) -> !fir.box<!fir.array<?xi32>>
23+
%8 = fir.alloca !fir.box<!fir.array<?xi32>>
24+
fir.store %7 to %8 : !fir.ref<!fir.box<!fir.array<?xi32>>>
25+
omp.yield(%8 : !fir.ref<!fir.box<!fir.array<?xi32>>>)
26+
} copy {
27+
^bb0(%arg0: !fir.ref<!fir.box<!fir.array<?xi32>>>, %arg1 : !fir.ref<!fir.box<!fir.array<?xi32>>>):
28+
%0 = fir.load %arg0 {test.ptr = "load_from_block_arg"} : !fir.ref<!fir.box<!fir.array<?xi32>>>
29+
hlfir.assign %0 to %arg1 : !fir.box<!fir.array<?xi32>>, !fir.ref<!fir.box<!fir.array<?xi32>>>
30+
omp.yield(%arg1 : !fir.ref<!fir.box<!fir.array<?xi32>>>)
31+
} dealloc {
32+
^bb0(%arg0: !fir.ref<!fir.box<!fir.array<?xi32>>>):
33+
%0 = fir.load %arg0 : !fir.ref<!fir.box<!fir.array<?xi32>>>
34+
%1 = fir.box_addr %0 : (!fir.box<!fir.array<?xi32>>) -> !fir.ref<!fir.array<?xi32>>
35+
%2 = fir.convert %1 : (!fir.ref<!fir.array<?xi32>>) -> i64
36+
%c0_i64 = arith.constant 0 : i64
37+
%3 = arith.cmpi ne, %2, %c0_i64 : i64
38+
fir.if %3 {
39+
%4 = fir.convert %1 : (!fir.ref<!fir.array<?xi32>>) -> !fir.heap<!fir.array<?xi32>>
40+
fir.freemem %4 : !fir.heap<!fir.array<?xi32>>
41+
}
42+
omp.yield
43+
}

0 commit comments

Comments
 (0)