[flang-commits] [flang] WIP: [flang] AliasAnalysis: Fix pointer component logic (PR #94242)
Joel E. Denny via flang-commits
flang-commits at lists.llvm.org
Mon Jun 3 14:40:15 PDT 2024
================
@@ -122,13 +122,32 @@ AliasResult AliasAnalysis::alias(Value lhs, Value rhs) {
}
if (lhsSrc.kind == rhsSrc.kind) {
+ // If the kinds and origins are the same, then lhs and rhs must alias unless
+ // either source is approximate. Approximate sources are for parts of the
+ // origin, but we don't have info here on which parts and whether they
+ // overlap, so we normally return MayAlias in that case.
+ //
+ // There is an exceptional case: The origins might compare unequal because
+ // only one has !isData(). If that source is approximate and the other is
+ // not, then the former is the source for the address *of* a pointer
+ // component in a composite, and the latter is for the address of that
+ // composite. As for the address of any composite vs. the address of one of
+ // its components, a store to one can affect a load from the other, so the
+ // result is MayAlias.
if (lhsSrc.origin == rhsSrc.origin) {
LLVM_DEBUG(llvm::dbgs()
<< " aliasing because same source kind and origin\n");
if (approximateSource)
return AliasResult::MayAlias;
return AliasResult::MustAlias;
}
+ if (lhsSrc.origin.u == rhsSrc.origin.u &&
+ ((lhsSrc.approximateSource && !lhsSrc.isData() && !rhsSrc.approximateSource) ||
+ (rhsSrc.approximateSource && !rhsSrc.isData() && !lhsSrc.approximateSource))) {
+ LLVM_DEBUG(llvm::dbgs()
+ << " aliasing between composite and pointer component\n");
+ return AliasResult::MayAlias;
+ }
----------------
jdenny-ornl wrote:
%11 below is the address of the data in `x%p` where `p` is a pointer:
```
func.func @_QMmPfoo() {
...
%2 = fir.alloca !fir.type<_QMmTt{p:!fir.box<!fir.ptr<i32>>}> {bindc_name = "x", uniq_name = "_QMmFfooEx"}
%3:2 = hlfir.declare %2 {uniq_name = "_QMmFfooEx"} : (!fir.ref<!fir.type<_QMmTt{p:!fir.box<!fir.ptr<i32>>}>>) -> (!fir.ref<!fir.type<_QMmTt{p:!fir.box<!fir.ptr<i32>>}>>, !fir.ref<!fir.type<_QMmTt{p:!fir.box<!fir.ptr<i32>>}>>)
...
%9 = hlfir.designate %3#0{"p"} {fortran_attrs = #fir.var_attrs<pointer>} : (!fir.ref<!fir.type<_QMmTt{p:!fir.box<!fir.ptr<i32>>}>>) -> !fir.ref<!fir.box<!fir.ptr<i32>>>
%10 = fir.load %9 : !fir.ref<!fir.box<!fir.ptr<i32>>>
%11 = fir.box_addr %10 : (!fir.box<!fir.ptr<i32>>) -> !fir.ptr<i32>
...
return
}
```
The LoadOp case in getSource needs to be extended to handle the hlfir.designate. My current draft of that patch does it in getOriginalDef.
I also have a patch to extend LoadOp for alloca.
https://github.com/llvm/llvm-project/pull/94242
More information about the flang-commits
mailing list