Skip to content

Revert "[MLIR] Fuse locations of merged constants (#74670)" #75381

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

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 1 addition & 11 deletions mlir/include/mlir/Transforms/FoldUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ class Value;
class OperationFolder {
public:
OperationFolder(MLIRContext *ctx, OpBuilder::Listener *listener = nullptr)
: fusedLocationTag(StringAttr::get(ctx, "CSE")), interfaces(ctx),
rewriter(ctx, listener) {}
: interfaces(ctx), rewriter(ctx, listener) {}

/// Tries to perform folding on the given `op`, including unifying
/// deduplicated constants. If successful, replaces `op`'s uses with
Expand Down Expand Up @@ -96,15 +95,6 @@ class OperationFolder {
Dialect *dialect, Attribute value,
Type type, Location loc);

// Fuse `foldedLocation` into the Location of `retainedOp`. This will result
// in `retainedOp` having a FusedLoc with `fusedLocationTag` to help trace the
// source of the fusion. If `retainedOp` already had a FusedLoc with the same
// tag, `foldedLocation` will simply be appended to it.
void appendFoldedLocation(Operation *retainedOp, Location foldedLocation);

/// Tag for annotating fused locations as a result of merging constants.
StringAttr fusedLocationTag;

/// A mapping between an insertion region and the constants that have been
/// created within it.
DenseMap<Region *, ConstantMap> foldScopes;
Expand Down
79 changes: 1 addition & 78 deletions mlir/lib/Transforms/Utils/FoldUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ bool OperationFolder::insertKnownConstant(Operation *op, Attribute constValue) {
// If there is an existing constant, replace `op`.
if (folderConstOp) {
notifyRemoval(op);
appendFoldedLocation(folderConstOp, op->getLoc());
rewriter.replaceOp(op, folderConstOp->getResults());
return false;
}
Expand Down Expand Up @@ -295,10 +294,8 @@ OperationFolder::tryGetOrCreateConstant(ConstantMap &uniquedConstants,
// Check if an existing mapping already exists.
auto constKey = std::make_tuple(dialect, value, type);
Operation *&constOp = uniquedConstants[constKey];
if (constOp) {
appendFoldedLocation(constOp, loc);
if (constOp)
return constOp;
}

// If one doesn't exist, try to materialize one.
if (!(constOp = materializeConstant(dialect, rewriter, value, type, loc)))
Expand All @@ -319,7 +316,6 @@ OperationFolder::tryGetOrCreateConstant(ConstantMap &uniquedConstants,
// materialized operation in favor of the existing one.
if (auto *existingOp = uniquedConstants.lookup(newKey)) {
notifyRemoval(constOp);
appendFoldedLocation(existingOp, constOp->getLoc());
rewriter.eraseOp(constOp);
referencedDialects[existingOp].push_back(dialect);
return constOp = existingOp;
Expand All @@ -330,76 +326,3 @@ OperationFolder::tryGetOrCreateConstant(ConstantMap &uniquedConstants,
auto newIt = uniquedConstants.insert({newKey, constOp});
return newIt.first->second;
}

/// Helper that flattens nested fused locations to a single fused location.
/// Fused locations nested under non-fused locations are not flattened, and
/// calling this on non-fused locations is a no-op as a result.
///
/// Fused locations are only flattened into parent fused locations if the
/// child fused location has no metadata, or if the metadata of the parent and
/// child fused locations are the same---this to avoid breaking cases where
/// metadata matter.
static Location FlattenFusedLocationRecursively(const Location loc) {
auto fusedLoc = dyn_cast<FusedLoc>(loc);
if (!fusedLoc)
return loc;

SetVector<Location> flattenedLocs;
Attribute metadata = fusedLoc.getMetadata();
ArrayRef<Location> unflattenedLocs = fusedLoc.getLocations();
bool hasAnyNestedLocChanged = false;

for (const Location &unflattenedLoc : unflattenedLocs) {
Location flattenedLoc = FlattenFusedLocationRecursively(unflattenedLoc);

auto flattenedFusedLoc = dyn_cast<FusedLoc>(flattenedLoc);
if (flattenedFusedLoc && (!flattenedFusedLoc.getMetadata() ||
flattenedFusedLoc.getMetadata() == metadata)) {
hasAnyNestedLocChanged = true;
ArrayRef<Location> nestedLocations = flattenedFusedLoc.getLocations();
flattenedLocs.insert(nestedLocations.begin(), nestedLocations.end());
} else {
if (flattenedLoc != unflattenedLoc)
hasAnyNestedLocChanged = true;

flattenedLocs.insert(flattenedLoc);
}
}

if (!hasAnyNestedLocChanged &&
unflattenedLocs.size() == flattenedLocs.size()) {
return loc;
}

return FusedLoc::get(loc->getContext(), flattenedLocs.takeVector(),
fusedLoc.getMetadata());
}

void OperationFolder::appendFoldedLocation(Operation *retainedOp,
Location foldedLocation) {
// Append into existing fused location if it has the same tag.
if (auto existingFusedLoc =
dyn_cast<FusedLocWith<StringAttr>>(retainedOp->getLoc())) {
StringAttr existingMetadata = existingFusedLoc.getMetadata();
if (existingMetadata == fusedLocationTag) {
ArrayRef<Location> existingLocations = existingFusedLoc.getLocations();
SetVector<Location> locations(existingLocations.begin(),
existingLocations.end());
locations.insert(foldedLocation);
Location newFusedLoc = FusedLoc::get(
retainedOp->getContext(), locations.takeVector(), existingMetadata);
retainedOp->setLoc(FlattenFusedLocationRecursively(newFusedLoc));
return;
}
}

// Create a new fusedloc with retainedOp's loc and foldedLocation.
// If they're already equal, no need to fuse.
if (retainedOp->getLoc() == foldedLocation)
return;

Location newFusedLoc =
FusedLoc::get(retainedOp->getContext(),
{retainedOp->getLoc(), foldedLocation}, fusedLocationTag);
retainedOp->setLoc(FlattenFusedLocationRecursively(newFusedLoc));
}
41 changes: 0 additions & 41 deletions mlir/test/Transforms/canonicalize-debuginfo.mlir

This file was deleted.

34 changes: 0 additions & 34 deletions mlir/test/Transforms/constant-fold-debuginfo.mlir

This file was deleted.