Skip to content

Commit e7bf54d

Browse files
authored
[flang] AliasAnalysis: Handle fir.load on hlfir.designate (#127107)
For example, determine that the address in `obj%p` below cannot alias the address of `v`: ``` module m type :: ty real, pointer :: p end type ty end module m subroutine test() use m real, target :: t real :: v type(ty) :: obj obj%p => t v = obj%p end subroutine test ```
1 parent 210036a commit e7bf54d

File tree

3 files changed

+531
-6
lines changed

3 files changed

+531
-6
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,14 @@ struct AliasAnalysis {
211211
fir::AliasAnalysis::Source getSource(mlir::Value,
212212
bool getLastInstantiationPoint = false);
213213

214+
/// Return true, if `ty` is a reference type to a boxed
215+
/// POINTER object or a raw fir::PointerType.
216+
static bool isPointerReference(mlir::Type ty);
217+
214218
private:
215219
/// Return true, if `ty` is a reference type to an object of derived type
216220
/// that contains a component with POINTER attribute.
217221
static bool isRecordWithPointerComponent(mlir::Type ty);
218-
219-
/// Return true, if `ty` is a reference type to a boxed
220-
/// POINTER object or a raw fir::PointerType.
221-
static bool isPointerReference(mlir::Type ty);
222222
};
223223

224224
inline bool operator==(const AliasAnalysis::Source::SourceOrigin &lhs,

flang/lib/Optimizer/Analysis/AliasAnalysis.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ static bool hasGlobalOpTargetAttr(mlir::Value v, fir::AddrOfOp op) {
5454
static mlir::Value
5555
getOriginalDef(mlir::Value v,
5656
fir::AliasAnalysis::Source::Attributes &attributes,
57-
bool &isCapturedInInternalProcedure) {
57+
bool &isCapturedInInternalProcedure, bool &approximateSource) {
5858
mlir::Operation *defOp;
5959
bool breakFromLoop = false;
6060
while (!breakFromLoop && (defOp = v.getDefiningOp())) {
61+
mlir::Type ty = defOp->getResultTypes()[0];
6162
llvm::TypeSwitch<Operation *>(defOp)
6263
.Case<fir::ConvertOp>([&](fir::ConvertOp op) { v = op.getValue(); })
6364
.Case<fir::DeclareOp, hlfir::DeclareOp>([&](auto op) {
@@ -67,6 +68,18 @@ getOriginalDef(mlir::Value v,
6768
isCapturedInInternalProcedure |=
6869
varIf.isCapturedInInternalProcedure();
6970
})
71+
.Case<fir::CoordinateOp>([&](auto op) {
72+
if (fir::AliasAnalysis::isPointerReference(ty))
73+
attributes.set(fir::AliasAnalysis::Attribute::Pointer);
74+
v = op->getOperand(0);
75+
approximateSource = true;
76+
})
77+
.Case<hlfir::DesignateOp>([&](hlfir::DesignateOp op) {
78+
auto varIf = llvm::cast<fir::FortranVariableOpInterface>(defOp);
79+
attributes |= getAttrsFromVariable(varIf);
80+
v = op.getMemref();
81+
approximateSource = true;
82+
})
7083
.Default([&](auto op) { breakFromLoop = true; });
7184
}
7285
return v;
@@ -609,7 +622,8 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v,
609622
attributes.set(Attribute::Pointer);
610623

611624
auto def = getOriginalDef(op.getMemref(), attributes,
612-
isCapturedInInternalProcedure);
625+
isCapturedInInternalProcedure,
626+
approximateSource);
613627
if (auto addrOfOp = def.template getDefiningOp<fir::AddrOfOp>()) {
614628
global = addrOfOp.getSymbol();
615629

0 commit comments

Comments
 (0)