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 all 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
31 changes: 20 additions & 11 deletions flang/lib/Optimizer/Analysis/AliasAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,22 @@ static bool hasGlobalOpTargetAttr(mlir::Value v, fir::AddrOfOp op) {
v, fir::GlobalOp::getTargetAttrName(globalOpName));
}

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 |= getAttrsFromVariable(varIf);
isCapturedInInternalProcedure |=
varIf.isCapturedInInternalProcedure();
})
.Default([&](auto op) { breakFromLoop = true; });
}
return v;
Expand Down Expand Up @@ -600,7 +608,8 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v,
if (mlir::isa<fir::PointerType>(boxTy.getEleTy()))
attributes.set(Attribute::Pointer);

auto def = getOriginalDef(op.getMemref());
auto def = getOriginalDef(op.getMemref(), attributes,
isCapturedInInternalProcedure);
if (auto addrOfOp = def.template getDefiningOp<fir::AddrOfOp>()) {
global = addrOfOp.getSymbol();

Expand All @@ -609,13 +618,13 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v,

type = SourceKind::Global;
}

// TODO: Add support to fir.alloca and fir.allocmem
// if (auto allocOp = def.template getDefiningOp<fir::AllocaOp>()) {
// ...
// }

if (isDummyArgument(def)) {
// TODO: Add support to fir.allocmem
else if (auto allocOp =
def.template getDefiningOp<fir::AllocaOp>()) {
v = def;
defOp = v.getDefiningOp();
type = SourceKind::Allocate;
} else if (isDummyArgument(def)) {
defOp = nullptr;
v = def;
}
Expand Down
15 changes: 9 additions & 6 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 All @@ -54,7 +57,7 @@

// The addresses stored in two different pointers can alias, even if one has no
// box. In this program, they happen to be the same address.
// T4:
// T4 from <https://github.com/llvm/llvm-project/pull/117785#discussion_r1924348480>.
// CHECK-DAG: p1.tgt#0 <-> boxp1.addr#0: MayAlias

func.func @_QFPtest(%arg0: !fir.ref<f32> {fir.bindc_name = "v1", fir.target}, %arg1: !fir.ref<f32> {fir.bindc_name = "v2", fir.target}, %arg2: !fir.ref<!fir.box<!fir.ptr<f32>>> ) attributes {test.ptr = "func"} {
Expand Down
Loading