Skip to content

Commit 718c4ed

Browse files
authored
[flang] [NFCI] Using getSource instead of getOriginalDef (#128984)
As discussed in past MRs, this change removes the use of getOriginalDef to use getSource instead to gather data from an indirection.
1 parent 0ee8f69 commit 718c4ed

File tree

2 files changed

+47
-60
lines changed

2 files changed

+47
-60
lines changed

flang/lib/Optimizer/Analysis/AliasAnalysis.cpp

Lines changed: 22 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -51,40 +51,6 @@ static bool hasGlobalOpTargetAttr(mlir::Value v, fir::AddrOfOp op) {
5151
v, fir::GlobalOp::getTargetAttrName(globalOpName));
5252
}
5353

54-
static mlir::Value
55-
getOriginalDef(mlir::Value v,
56-
fir::AliasAnalysis::Source::Attributes &attributes,
57-
bool &isCapturedInInternalProcedure, bool &approximateSource) {
58-
mlir::Operation *defOp;
59-
bool breakFromLoop = false;
60-
while (!breakFromLoop && (defOp = v.getDefiningOp())) {
61-
mlir::Type ty = defOp->getResultTypes()[0];
62-
llvm::TypeSwitch<Operation *>(defOp)
63-
.Case<fir::ConvertOp>([&](fir::ConvertOp op) { v = op.getValue(); })
64-
.Case<fir::DeclareOp, hlfir::DeclareOp>([&](auto op) {
65-
v = op.getMemref();
66-
auto varIf = llvm::cast<fir::FortranVariableOpInterface>(defOp);
67-
attributes |= getAttrsFromVariable(varIf);
68-
isCapturedInInternalProcedure |=
69-
varIf.isCapturedInInternalProcedure();
70-
})
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-
})
83-
.Default([&](auto op) { breakFromLoop = true; });
84-
}
85-
return v;
86-
}
87-
8854
static bool isEvaluateInMemoryBlockArg(mlir::Value v) {
8955
if (auto evalInMem = llvm::dyn_cast_or_null<hlfir::EvaluateInMemoryOp>(
9056
v.getParentRegion()->getParentOp()))
@@ -621,38 +587,34 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v,
621587
if (mlir::isa<fir::PointerType>(boxTy.getEleTy()))
622588
attributes.set(Attribute::Pointer);
623589

624-
auto def = getOriginalDef(op.getMemref(), attributes,
625-
isCapturedInInternalProcedure,
626-
approximateSource);
627-
if (auto addrOfOp = def.template getDefiningOp<fir::AddrOfOp>()) {
628-
global = addrOfOp.getSymbol();
629-
630-
if (hasGlobalOpTargetAttr(def, addrOfOp))
631-
attributes.set(Attribute::Target);
590+
auto boxSrc = getSource(op.getMemref());
591+
attributes |= boxSrc.attributes;
592+
approximateSource |= boxSrc.approximateSource;
593+
isCapturedInInternalProcedure |=
594+
boxSrc.isCapturedInInternalProcedure;
632595

596+
global = llvm::dyn_cast<mlir::SymbolRefAttr>(boxSrc.origin.u);
597+
if (global) {
633598
type = SourceKind::Global;
634-
}
635-
// TODO: Add support to fir.allocmem
636-
else if (auto allocOp =
637-
def.template getDefiningOp<fir::AllocaOp>()) {
638-
v = def;
639-
defOp = v.getDefiningOp();
640-
type = SourceKind::Allocate;
641-
} else if (isDummyArgument(def)) {
642-
defOp = nullptr;
643-
v = def;
644599
} else {
645-
type = SourceKind::Indirect;
600+
auto def = llvm::cast<mlir::Value>(boxSrc.origin.u);
601+
// TODO: Add support to fir.allocmem
602+
if (auto allocOp = def.template getDefiningOp<fir::AllocaOp>()) {
603+
v = def;
604+
defOp = v.getDefiningOp();
605+
type = SourceKind::Allocate;
606+
} else if (isDummyArgument(def)) {
607+
defOp = nullptr;
608+
v = def;
609+
} else {
610+
type = SourceKind::Indirect;
611+
}
646612
}
647-
// TODO: This assignment is redundant but somehow works around an
648-
// apparent MSVC bug reporting "undeclared identifier" at the next
649-
// "breakFromLoop = true;". See
650-
// <https://github.com/llvm/llvm-project/pull/127845#issuecomment-2669829610>.
651613
breakFromLoop = true;
652-
} else {
653-
// No further tracking for addresses loaded from memory for now.
654-
type = SourceKind::Indirect;
614+
return;
655615
}
616+
// No further tracking for addresses loaded from memory for now.
617+
type = SourceKind::Indirect;
656618
breakFromLoop = true;
657619
})
658620
.Case<fir::AddrOfOp>([&](auto op) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: fir-opt %s -pass-pipeline='builtin.module(func.func(test-fir-alias-analysis))' -debug-only=fir-alias-analysis 2>&1 | FileCheck %s
2+
3+
// CHECK-LABEL: Testing : "_QFPtest"
4+
5+
// Checking that the source kind of a load of a load is SourceKind::Indirect
6+
// CHECK: {test.ptr = "load_load"}
7+
// CHECK-NEXT: SourceKind: Indirect
8+
9+
// Checking that the source kind of a load of an arg is SourceKind::Argument
10+
// CHECK: {test.ptr = "load_arg"}
11+
// CHECK-NEXT: SourceKind: Argument
12+
13+
func.func @_QFPtest(%arg0: !fir.ref<!fir.box<!fir.ptr<f32>>> ) attributes {test.ptr = "func"} {
14+
15+
%0 = fir.alloca !fir.llvm_ptr<!fir.box<!fir.ptr<f32>>>
16+
%1 = fir.convert %arg0 : (!fir.ref<!fir.box<!fir.ptr<f32>>>) -> !fir.llvm_ptr<!fir.box<!fir.ptr<f32>>>
17+
fir.store %1 to %0 : !fir.ref<!fir.llvm_ptr<!fir.box<!fir.ptr<f32>>>>
18+
%2 = fir.load %0 : !fir.ref<!fir.llvm_ptr<!fir.box<!fir.ptr<f32>>>>
19+
%3 = fir.convert %2 : (!fir.llvm_ptr<!fir.box<!fir.ptr<f32>>>) -> !fir.ref<!fir.box<!fir.ptr<f32>>>
20+
%15 = fir.load %3 : !fir.ref<!fir.box<!fir.ptr<f32>>>
21+
%16 = fir.box_addr %15 {test.ptr = "load_load"} : (!fir.box<!fir.ptr<f32>>) -> !fir.ptr<f32>
22+
%17 = fir.load %arg0 : !fir.ref<!fir.box<!fir.ptr<f32>>>
23+
%18 = fir.box_addr %17 {test.ptr = "load_arg"} : (!fir.box<!fir.ptr<f32>>) -> !fir.ptr<f32>
24+
return
25+
}

0 commit comments

Comments
 (0)