Skip to content

[flang] AliasAnalysis: Handle fir.load on fir.alloca #117785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions flang/lib/Optimizer/Analysis/AliasAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,29 @@ using namespace mlir;
// AliasAnalysis: alias
//===----------------------------------------------------------------------===//

namespace fir {
static AliasAnalysis::Source::Attributes
getAttrsFromVariable(fir::FortranVariableOpInterface var);
}

/// Temporary function to skip through all the no op operations
/// TODO: Generalize support of fir.load
static mlir::Value getOriginalDef(mlir::Value v) {
static mlir::Value
getOriginalDef(mlir::Value v,
fir::AliasAnalysis::Source::Attributes &attributes,
bool &isCapturedInInternalProcedure) {
mlir::Operation *defOp;
bool breakFromLoop = false;
while (!breakFromLoop && (defOp = v.getDefiningOp())) {
llvm::TypeSwitch<Operation *>(defOp)
.Case<fir::ConvertOp>([&](fir::ConvertOp op) { v = op.getValue(); })
.Case<fir::DeclareOp, hlfir::DeclareOp>(
[&](auto op) { v = op.getMemref(); })
.Case<fir::DeclareOp, hlfir::DeclareOp>([&](auto op) {
v = op.getMemref();
auto varIf = llvm::cast<fir::FortranVariableOpInterface>(defOp);
attributes |= fir::getAttrsFromVariable(varIf);
isCapturedInInternalProcedure |=
varIf.isCapturedInInternalProcedure();
})
.Default([&](auto op) { breakFromLoop = true; });
}
return v;
Expand Down Expand Up @@ -530,6 +543,17 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v,
bool isBoxRef{fir::isa_ref_type(v.getType()) &&
mlir::isa<fir::BaseBoxType>(fir::unwrapRefType(v.getType()))};
bool followingData = !isBoxRef;
// "fir.alloca !fir.ptr<...>" returns the address *of* a pointer and is thus
// non-data, and yet there's no box. Don't treat it like data, or it will
// appear to alias like the address *in* a pointer. TODO: That case occurs in
// our test suite (alias-analysis-2.fir), but does flang currently generate
// such code? Perhaps we should update docs
// (AliasAnalysis::Source::SourceOrigin::isData) and debug output
// (AliasAnalysis::Source::print) for isData as the current wording implies
// !isData requires a box.
if (mlir::isa_and_nonnull<fir::AllocaOp, fir::AllocMemOp>(defOp) &&
isPointerReference(v.getType()))
followingData = false;
mlir::SymbolRefAttr global;
Source::Attributes attributes;
mlir::Operation *instantiationPoint{nullptr};
Expand All @@ -543,6 +567,12 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v,
.Case<fir::AllocaOp, fir::AllocMemOp>([&](auto op) {
// Unique memory allocation.
type = SourceKind::Allocate;
// If there's no DeclareOp, then we need to get the pointer attribute
// from the type. TODO: That case occurs in our test suite
// (alias-analysis-2.fir), but does flang currently generate such
// code?
if (isPointerReference(ty))
attributes.set(Attribute::Pointer);
breakFromLoop = true;
})
.Case<fir::ConvertOp>([&](auto op) {
Expand Down Expand Up @@ -575,10 +605,12 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v,
.Case<fir::LoadOp>([&](auto op) {
// If the load is from a leaf source, return the leaf. Do not track
// through indirections otherwise.
// TODO: Add support to fir.alloca and fir.allocmem
auto def = getOriginalDef(op.getMemref());
// TODO: Add support to fir.allocmem.
auto def = getOriginalDef(op.getMemref(), attributes,
isCapturedInInternalProcedure);
if (isDummyArgument(def) ||
def.template getDefiningOp<fir::AddrOfOp>()) {
def.template getDefiningOp<fir::AddrOfOp>() ||
def.template getDefiningOp<fir::AllocaOp>()) {
v = def;
defOp = v.getDefiningOp();
return;
Expand Down
13 changes: 8 additions & 5 deletions flang/test/Analysis/AliasAnalysis/alias-analysis-2.fir
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
// They cannot physically alias
// CHECK-DAG: p1.addr#0 <-> p2.addr#0: NoAlias

// p1.addr and p2.addr could both be wrapped inside boxes
// CHECK-DAG: p1.addr#0 <-> boxp1.addr#0: MayAlias
// CHECK-DAG: p2.addr#0 <-> boxp1.addr#0: MayAlias
// p1.addr is the address returned by an alloca. It does not have a target
// attribute, and it is not the address retrieved from a pointer. It cannot
// alias anything. Likewise for p2.addr.
// CHECK-DAG: p1.addr#0 <-> boxp1.addr#0: NoAlias
// CHECK-DAG: p2.addr#0 <-> boxp1.addr#0: NoAlias

// TODO: To really see aliasing, we should be looking at a load of p1.addr
// p1.addr is just a local address holding the address to the data
Expand All @@ -27,10 +29,11 @@
// CHECK-DAG: p2.addr#0 <-> func.region0#2: NoAlias

// All arguments are either pointers or targets
// A pointer in a box may alias with both
// The address *in* a local pointer may alias the address of a target
// argument, but it does not alias the address *of* a pointer argument.
// CHECK-DAG: boxp1.addr#0 <-> func.region0#0: MayAlias
// CHECK-DAG: boxp1.addr#0 <-> func.region0#1: MayAlias
// CHECK-DAG: boxp1.addr#0 <-> func.region0#2: MayAlias
// CHECK-DAG: boxp1.addr#0 <-> func.region0#2: NoAlias

// A target dummy may alias with another target
// CHECK-DAG: func.region0#0 <-> func.region0#1: MayAlias
Expand Down
Loading
Loading