Skip to content

Commit 5236e3d

Browse files
[Flang][Alias analysis] Fix alias analysis for omp private allocatable item (#120243)
Flang alias analysis crashes for omp private allocatable item. The issue is described here : #116954 . We know that private value can't alias with anything else unless it is POINTER or TARGET. That's why we can simplify alias analysis logic.
1 parent 035e64c commit 5236e3d

File tree

2 files changed

+64
-22
lines changed

2 files changed

+64
-22
lines changed

flang/lib/Optimizer/Analysis/AliasAnalysis.cpp

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -505,30 +505,17 @@ getAttrsFromVariable(fir::FortranVariableOpInterface var) {
505505
}
506506

507507
template <typename OMPTypeOp, typename DeclTypeOp>
508-
static Value getPrivateArg(omp::BlockArgOpenMPOpInterface &argIface,
509-
OMPTypeOp &op, DeclTypeOp &declOp) {
510-
Value privateArg;
508+
static bool isPrivateArg(omp::BlockArgOpenMPOpInterface &argIface,
509+
OMPTypeOp &op, DeclTypeOp &declOp) {
511510
if (!op.getPrivateSyms().has_value())
512-
return privateArg;
511+
return false;
513512
for (auto [opSym, blockArg] :
514513
llvm::zip_equal(*op.getPrivateSyms(), argIface.getPrivateBlockArgs())) {
515514
if (blockArg == declOp.getMemref()) {
516-
omp::PrivateClauseOp privateOp =
517-
SymbolTable::lookupNearestSymbolFrom<omp::PrivateClauseOp>(
518-
op, cast<SymbolRefAttr>(opSym));
519-
privateOp.walk([&](omp::YieldOp yieldOp) {
520-
// TODO Extend alias analysis if omp.yield points to
521-
// block argument value
522-
if (!yieldOp.getResults()[0].getDefiningOp())
523-
return;
524-
llvm::TypeSwitch<Operation *>(yieldOp.getResults()[0].getDefiningOp())
525-
.template Case<fir::DeclareOp, hlfir::DeclareOp>(
526-
[&](auto declOp) { privateArg = declOp.getMemref(); });
527-
});
528-
return privateArg;
515+
return true;
529516
}
530517
}
531-
return privateArg;
518+
return false;
532519
}
533520

534521
AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v,
@@ -631,6 +618,7 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v,
631618
breakFromLoop = true;
632619
})
633620
.Case<hlfir::DeclareOp, fir::DeclareOp>([&](auto op) {
621+
bool isPrivateItem = false;
634622
if (omp::BlockArgOpenMPOpInterface argIface =
635623
dyn_cast<omp::BlockArgOpenMPOpInterface>(op->getParentOp())) {
636624
Value ompValArg;
@@ -644,19 +632,18 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v,
644632
omp::MapInfoOp mapInfo =
645633
llvm::cast<omp::MapInfoOp>(opArg.getDefiningOp());
646634
ompValArg = mapInfo.getVarPtr();
647-
break;
635+
return;
648636
}
649637
}
650638
// If given operation does not reflect mapping item,
651639
// check private clause
652-
if (!ompValArg)
653-
ompValArg = getPrivateArg(argIface, targetOp, op);
640+
isPrivateItem = isPrivateArg(argIface, targetOp, op);
654641
})
655642
.template Case<omp::DistributeOp, omp::ParallelOp,
656643
omp::SectionsOp, omp::SimdOp, omp::SingleOp,
657644
omp::TaskloopOp, omp::TaskOp, omp::WsloopOp>(
658645
[&](auto privateOp) {
659-
ompValArg = getPrivateArg(argIface, privateOp, op);
646+
isPrivateItem = isPrivateArg(argIface, privateOp, op);
660647
});
661648
if (ompValArg) {
662649
v = ompValArg;
@@ -706,6 +693,11 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v,
706693
} else {
707694
instantiationPoint = op;
708695
}
696+
if (isPrivateItem) {
697+
type = SourceKind::Allocate;
698+
breakFromLoop = true;
699+
return;
700+
}
709701
// TODO: Look for the fortran attributes present on the operation
710702
// Track further through the operand
711703
v = op.getMemref();
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Use --mlir-disable-threading so that the AA queries are serialized
2+
// as well as its diagnostic output.
3+
// RUN: fir-opt %s -pass-pipeline='builtin.module(func.func(test-fir-alias-analysis))' -split-input-file --mlir-disable-threading 2>&1 | FileCheck %s
4+
5+
// Fortran code before simplification:
6+
// SUBROUTINE mysub(ns,ne)
7+
// INTEGER :: n
8+
// REAL(KIND=8), DIMENSION(:), allocatable :: ar1
9+
// real(kind=8), dimension(20) :: ar2
10+
// REAL(KIND=8), DIMENSION(20) :: d
11+
//
12+
//!$OMP parallel PRIVATE(ar1)
13+
// d(1:1) = (/(DOT_PRODUCT(ar1(1:n), ar2(1:n)),n=1, 1)/)
14+
//!$OMP END parallel
15+
// END SUBROUTINE
16+
17+
// CHECK-LABEL: Testing : "testPrivateAllocatable"
18+
// CHECK: ar2#0 <-> ar1#0: NoAlias
19+
// CHECK: ar2#1 <-> ar1#0: NoAlias
20+
// CHECK: ar2#0 <-> ar1#1: NoAlias
21+
// CHECK: ar2#1 <-> ar1#1: NoAlias
22+
23+
omp.private {type = private} @_QFmysubEar1_private_ref_box_heap_Uxf64 : !fir.ref<!fir.box<!fir.heap<!fir.array<?xf64>>>> alloc {
24+
^bb0(%arg0: !fir.ref<!fir.box<!fir.heap<!fir.array<?xf64>>>>):
25+
%0 = fir.alloca !fir.box<!fir.heap<!fir.array<?xf64>>> {bindc_name = "ar1", pinned, uniq_name = "_QFmysubEar1"}
26+
%5:2 = hlfir.declare %0 {fortran_attrs = #fir.var_attrs<allocatable>, uniq_name = "_QFmysubEar1"} : (!fir.ref<!fir.box<!fir.heap<!fir.array<?xf64>>>>) -> (!fir.ref<!fir.box<!fir.heap<!fir.array<?xf64>>>>, !fir.ref<!fir.box<!fir.heap<!fir.array<?xf64>>>>)
27+
omp.yield(%5#0 : !fir.ref<!fir.box<!fir.heap<!fir.array<?xf64>>>>)
28+
} dealloc {
29+
^bb0(%arg0: !fir.ref<!fir.box<!fir.heap<!fir.array<?xf64>>>>):
30+
omp.yield
31+
}
32+
func.func @testPrivateAllocatable(%arg0: !fir.ref<i32> {fir.bindc_name = "ns"}, %arg1: !fir.ref<i32> {fir.bindc_name = "ne"}) {
33+
%0 = fir.dummy_scope : !fir.dscope
34+
%1 = fir.alloca !fir.box<!fir.heap<!fir.array<?xf64>>> {bindc_name = "ar1", uniq_name = "_QFmysubEar1"}
35+
%2 = fir.zero_bits !fir.heap<!fir.array<?xf64>>
36+
%c0 = arith.constant 0 : index
37+
%3 = fir.shape %c0 : (index) -> !fir.shape<1>
38+
%4 = fir.embox %2(%3) : (!fir.heap<!fir.array<?xf64>>, !fir.shape<1>) -> !fir.box<!fir.heap<!fir.array<?xf64>>>
39+
fir.store %4 to %1 : !fir.ref<!fir.box<!fir.heap<!fir.array<?xf64>>>>
40+
%5:2 = hlfir.declare %1 {fortran_attrs = #fir.var_attrs<allocatable>, uniq_name = "_QFmysubEar1"} : (!fir.ref<!fir.box<!fir.heap<!fir.array<?xf64>>>>) -> (!fir.ref<!fir.box<!fir.heap<!fir.array<?xf64>>>>, !fir.ref<!fir.box<!fir.heap<!fir.array<?xf64>>>>)
41+
%c20 = arith.constant 20 : index
42+
%6 = fir.alloca !fir.array<20xf64> {bindc_name = "ar2", uniq_name = "_QFmysubEar2"}
43+
%7 = fir.shape %c20 : (index) -> !fir.shape<1>
44+
%8:2 = hlfir.declare %6(%7) {uniq_name = "_QFmysubEar2", test.ptr="ar2" } : (!fir.ref<!fir.array<20xf64>>, !fir.shape<1>) -> (!fir.ref<!fir.array<20xf64>>, !fir.ref<!fir.array<20xf64>>)
45+
omp.parallel private(@_QFmysubEar1_private_ref_box_heap_Uxf64 %5#0 -> %arg2 : !fir.ref<!fir.box<!fir.heap<!fir.array<?xf64>>>>) {
46+
%20:2 = hlfir.declare %arg2 {fortran_attrs = #fir.var_attrs<allocatable>, uniq_name = "_QFmysubEar1", test.ptr = "ar1"} : (!fir.ref<!fir.box<!fir.heap<!fir.array<?xf64>>>>) -> (!fir.ref<!fir.box<!fir.heap<!fir.array<?xf64>>>>, !fir.ref<!fir.box<!fir.heap<!fir.array<?xf64>>>>)
47+
omp.terminator
48+
}
49+
return
50+
}

0 commit comments

Comments
 (0)