Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 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
6 changes: 3 additions & 3 deletions flang/include/flang/Optimizer/Dialect/FIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ def AnyRefOfConstantSizeAggregateType : TypeConstraint<
// Memory SSA operations
//===----------------------------------------------------------------------===//

def fir_AllocaOp : fir_Op<"alloca", [AttrSizedOperandSegments,
MemoryEffects<[MemAlloc<AutomaticAllocationScopeResource>]>]> {
def fir_AllocaOp : fir_Op<"alloca",
[AttrSizedOperandSegments, DeclareOpInterfaceMethods<MemoryEffectsOpInterface>]> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just FYI: if you can remove code in fir::AllocaOp::getEffects as I suggest below, then you can declare the MemAlloc effect in the operation definition like this:

[MemAlloc<AutomaticAllocationScopeResource>]>:$res);

This way you won't need to change the cpp files.

let summary = "allocate storage for a temporary on the stack given a type";
let description = [{
This primitive operation is used to allocate an object on the stack. A
Expand Down Expand Up @@ -213,7 +213,7 @@ def fir_AllocaOp : fir_Op<"alloca", [AttrSizedOperandSegments,
}

def fir_AllocMemOp : fir_Op<"allocmem",
[MemoryEffects<[MemAlloc<DefaultResource>]>, AttrSizedOperandSegments]> {
[DeclareOpInterfaceMethods<MemoryEffectsOpInterface>, AttrSizedOperandSegments]> {
let summary = "allocate storage on the heap for an object of a given type";

let description = [{
Expand Down
52 changes: 37 additions & 15 deletions flang/lib/Optimizer/Analysis/AliasAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,29 @@ using namespace mlir;

#define DEBUG_TYPE "fir-alias-analysis"

static bool classifyAllocateFromEffects(mlir::Operation *op,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a header comment.

I would also try to simplify the interface of this function (I am not a fan of returning results through references, if this can be avoided :) ).

I recommend getting rid of arguments v, defOp and type. Instead of returning bool we can return either SourceKind::Unknown or SourceKind::Allocate. Then the callers can update their v and defOp values correspondingly if it returns SourceKind::Allocate. I think the interface will become more clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, thank you!

mlir::Value candidate, mlir::Value &v,
mlir::Operation *&defOp,
fir::AliasAnalysis::SourceKind &type) {
if (!op)
return false;
auto interface = llvm::dyn_cast<mlir::MemoryEffectOpInterface>(op);
if (!interface)
return false;
llvm::SmallVector<mlir::MemoryEffects::EffectInstance, 4> effects;
interface.getEffects(effects);
for (mlir::MemoryEffects::EffectInstance &e : effects) {
if (mlir::isa<mlir::MemoryEffects::Allocate>(e.getEffect()) &&
e.getValue() && e.getValue() == candidate) {
v = candidate;
defOp = op;
type = fir::AliasAnalysis::SourceKind::Allocate;
return true;
}
}
return false;
}

//===----------------------------------------------------------------------===//
// AliasAnalysis: alias
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -535,6 +558,9 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v,
mlir::Operation *instantiationPoint{nullptr};
while (defOp && !breakFromLoop) {
ty = defOp->getResultTypes()[0];
// Value-scoped allocation detection via effects.
if (classifyAllocateFromEffects(defOp, v, v, defOp, type))
break;
llvm::TypeSwitch<Operation *>(defOp)
.Case<hlfir::AsExprOp>([&](auto op) {
v = op.getVar();
Expand All @@ -554,11 +580,6 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v,
defOp = v.getDefiningOp();
}
})
.Case<fir::AllocaOp, fir::AllocMemOp>([&](auto op) {
// Unique memory allocation.
type = SourceKind::Allocate;
breakFromLoop = true;
})
.Case<fir::ConvertOp>([&](auto op) {
// Skip ConvertOp's and track further through the operand.
v = op->getOperand(0);
Expand Down Expand Up @@ -628,16 +649,17 @@ AliasAnalysis::Source AliasAnalysis::getSource(mlir::Value v,
type = SourceKind::Global;
} else {
auto def = llvm::cast<mlir::Value>(boxSrc.origin.u);
// TODO: Add support to fir.allocmem
if (auto allocOp = def.template getDefiningOp<fir::AllocaOp>()) {
v = def;
defOp = v.getDefiningOp();
type = SourceKind::Allocate;
} else if (isDummyArgument(def)) {
defOp = nullptr;
v = def;
} else {
type = SourceKind::Indirect;
bool classified = false;
if (auto defDefOp = def.getDefiningOp())
classified =
classifyAllocateFromEffects(defDefOp, def, v, defOp, type);
if (!classified) {
if (isDummyArgument(def)) {
defOp = nullptr;
v = def;
} else {
type = SourceKind::Indirect;
}
}
}
breakFromLoop = true;
Expand Down
28 changes: 28 additions & 0 deletions flang/lib/Optimizer/Dialect/FIROps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,21 @@ llvm::LogicalResult fir::AllocaOp::verify() {
return mlir::success();
}

void fir::AllocaOp::getEffects(
llvm::SmallVectorImpl<
mlir::SideEffects::EffectInstance<mlir::MemoryEffects::Effect>>
&effects) {
// Value-scoped Allocate for AA.
effects.emplace_back(
mlir::MemoryEffects::Allocate::get(),
mlir::cast<mlir::OpResult>(getOperation()->getResult(0)),
mlir::SideEffects::AutomaticAllocationScopeResource::get());
// Preserve previous behavior: op-scoped Allocate for passes relying on it.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we do not need this here and for AllocMemOp. Did you find any LIT tests that fail without this?

I believe the effect above implies the effect that you are adding below. If it is not the case I am interested to see an example that fails.

effects.emplace_back(
mlir::MemoryEffects::Allocate::get(),
mlir::SideEffects::AutomaticAllocationScopeResource::get());
}

bool fir::AllocaOp::ownsNestedAlloca(mlir::Operation *op) {
return op->hasTrait<mlir::OpTrait::IsIsolatedFromAbove>() ||
op->hasTrait<mlir::OpTrait::AutomaticAllocationScope>() ||
Expand Down Expand Up @@ -384,6 +399,19 @@ llvm::LogicalResult fir::AllocMemOp::verify() {
return mlir::success();
}

void fir::AllocMemOp::getEffects(
llvm::SmallVectorImpl<
mlir::SideEffects::EffectInstance<mlir::MemoryEffects::Effect>>
&effects) {
// Value-scoped Allocate for AA.
effects.emplace_back(mlir::MemoryEffects::Allocate::get(),
mlir::cast<mlir::OpResult>(getOperation()->getResult(0)),
mlir::SideEffects::DefaultResource::get());
// Preserve previous behavior: op-scoped Allocate for passes relying on it.
effects.emplace_back(mlir::MemoryEffects::Allocate::get(),
mlir::SideEffects::DefaultResource::get());
}

//===----------------------------------------------------------------------===//
// ArrayCoorOp
//===----------------------------------------------------------------------===//
Expand Down