Skip to content
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

[flang][NFC] Moving alias analysis utilities utilities together. Adding new utility. #125925

Merged
merged 2 commits into from
Feb 6, 2025
Merged
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
84 changes: 43 additions & 41 deletions flang/lib/Optimizer/Analysis/AliasAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,27 @@ using namespace mlir;
// AliasAnalysis: alias
//===----------------------------------------------------------------------===//

/// Temporary function to skip through all the no op operations
/// TODO: Generalize support of fir.load
static mlir::Value getOriginalDef(mlir::Value v) {
static fir::AliasAnalysis::Source::Attributes
getAttrsFromVariable(fir::FortranVariableOpInterface var) {
fir::AliasAnalysis::Source::Attributes attrs;
if (var.isTarget())
attrs.set(fir::AliasAnalysis::Attribute::Target);
if (var.isPointer())
attrs.set(fir::AliasAnalysis::Attribute::Pointer);
if (var.isIntentIn())
attrs.set(fir::AliasAnalysis::Attribute::IntentIn);

return attrs;
}

static bool hasGlobalOpTargetAttr(mlir::Value v, fir::AddrOfOp op) {
auto globalOpName =
mlir::OperationName(fir::GlobalOp::getOperationName(), op->getContext());
return fir::valueHasFirAttribute(
v, fir::GlobalOp::getTargetAttrName(globalOpName));
}

mlir::Value getOriginalDef(mlir::Value v) {
mlir::Operation *defOp;
bool breakFromLoop = false;
while (!breakFromLoop && (defOp = v.getDefiningOp())) {
Expand All @@ -46,6 +64,27 @@ static mlir::Value getOriginalDef(mlir::Value v) {
return v;
}

static bool isEvaluateInMemoryBlockArg(mlir::Value v) {
if (auto evalInMem = llvm::dyn_cast_or_null<hlfir::EvaluateInMemoryOp>(
v.getParentRegion()->getParentOp()))
return evalInMem.getMemory() == v;
return false;
}

template <typename OMPTypeOp, typename DeclTypeOp>
static bool isPrivateArg(omp::BlockArgOpenMPOpInterface &argIface,
OMPTypeOp &op, DeclTypeOp &declOp) {
if (!op.getPrivateSyms().has_value())
return false;
for (auto [opSym, blockArg] :
llvm::zip_equal(*op.getPrivateSyms(), argIface.getPrivateBlockArgs())) {
if (blockArg == declOp.getMemref()) {
return true;
}
}
return false;
}

namespace fir {

void AliasAnalysis::Source::print(llvm::raw_ostream &os) const {
Expand Down Expand Up @@ -91,13 +130,6 @@ bool AliasAnalysis::Source::isDummyArgument() const {
return false;
}

static bool isEvaluateInMemoryBlockArg(mlir::Value v) {
if (auto evalInMem = llvm::dyn_cast_or_null<hlfir::EvaluateInMemoryOp>(
v.getParentRegion()->getParentOp()))
return evalInMem.getMemory() == v;
return false;
}

bool AliasAnalysis::Source::isData() const { return origin.isData; }
bool AliasAnalysis::Source::isBoxData() const {
return mlir::isa<fir::BaseBoxType>(fir::unwrapRefType(valueType)) &&
Expand Down Expand Up @@ -491,33 +523,6 @@ ModRefResult AliasAnalysis::getModRef(mlir::Region &region,
return result;
}

AliasAnalysis::Source::Attributes
getAttrsFromVariable(fir::FortranVariableOpInterface var) {
AliasAnalysis::Source::Attributes attrs;
if (var.isTarget())
attrs.set(AliasAnalysis::Attribute::Target);
if (var.isPointer())
attrs.set(AliasAnalysis::Attribute::Pointer);
if (var.isIntentIn())
attrs.set(AliasAnalysis::Attribute::IntentIn);

return attrs;
}

template <typename OMPTypeOp, typename DeclTypeOp>
static bool isPrivateArg(omp::BlockArgOpenMPOpInterface &argIface,
OMPTypeOp &op, DeclTypeOp &declOp) {
if (!op.getPrivateSyms().has_value())
return false;
for (auto [opSym, blockArg] :
llvm::zip_equal(*op.getPrivateSyms(), argIface.getPrivateBlockArgs())) {
if (blockArg == declOp.getMemref()) {
return true;
}
}
return false;
}

AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v,
bool getLastInstantiationPoint) {
auto *defOp = v.getDefiningOp();
Expand Down Expand Up @@ -604,10 +609,7 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v,
ty = v.getType();
type = SourceKind::Global;

auto globalOpName = mlir::OperationName(
fir::GlobalOp::getOperationName(), defOp->getContext());
if (fir::valueHasFirAttribute(
v, fir::GlobalOp::getTargetAttrName(globalOpName)))
if (hasGlobalOpTargetAttr(v, op))
attributes.set(Attribute::Target);

// TODO: Take followBoxData into account when setting the pointer
Expand Down