This is an archive of the discontinued LLVM Phabricator instance.

[mlir][cse] do not replace operands in previously simplified operations
ClosedPublic

Authored by youngar on Mar 28 2021, 10:37 PM.

Details

Summary

If an operation has been inserted as a key in to the known values
hashtable, then it can not be modified in a way which changes its hash.
This change avoids modifying the operands of any previously recorded
operation, which prevents their hash from changing.

In an SSACFG region, it is impossible to visit an operation before
visiting its operands, so this is not a problem. This situation can only
happen in regions without strict dominance, such as graph regions.

Diff Detail

Event Timeline

youngar created this revision.Mar 28 2021, 10:37 PM
youngar requested review of this revision.Mar 28 2021, 10:37 PM
rriddle added inline comments.Mar 29 2021, 11:15 AM
mlir/lib/Transforms/CSE.cpp
119–139

Can you check if this is a graph region before doing this? We should add additional checks when necessary.

youngar updated this revision to Diff 333968.Mar 29 2021, 1:05 PM

Check if the region does not have SSA dominance before using the slow-path for replacing uses.

youngar added inline comments.Mar 29 2021, 1:13 PM
mlir/lib/Transforms/CSE.cpp
119–139

(I meant to submit this comment much earlier)
This makes sense. Since I think unregistered operations have relaxed dominance, I think I should steal the check from Dominance.cpp:

/// Return true if the region with the given index inside the operation
/// has SSA dominance.
static bool hasSSADominance(Operation *op, unsigned index) {
  auto kindInterface = dyn_cast<RegionKindInterface>(op);
  return op->isRegistered() &&
         (!kindInterface || kindInterface.hasSSADominance(index));
}

Would it make sense to make this function public?

rriddle added inline comments.Mar 29 2021, 11:04 PM
mlir/lib/Transforms/CSE.cpp
119–139

We already check for this in simplifyRegion with domInfo.hasDominanceInfo. Can you pipe through that down to here? i.e. something like:

LogicalResult CSE::simplifyOperation(ScopedMapTy &knownValues, Operation *op, bool hasSSADominance) {
  ...
}

void CSE::simplifyBlock(ScopedMapTy &knownValues, DominanceInfo &domInfo,
                        Block *bb, bool hasSSADominance) {
  for (auto &inst : *bb) {
    if (succeeded(simplifyOperation(knownValues, &inst, hasSSADominance)))
      continue;
  }
}

void CSE::simplifyRegion(ScopedMapTy &knownValues, DominanceInfo &domInfo,
                         Region &region) {
  ...

  bool hasSSADominance = domInfo.hasDominanceInfo(&region);

  // If the region only contains one block, then simplify it directly.
  if (std::next(region.begin()) == region.end()) {
    ScopedMapTy::ScopeTy scope(knownValues);
    simplifyBlock(knownValues, domInfo, &region.front(), hasSSADominance);
    return;
  }

  // If the region does not have dominanceInfo, then skip it.
  // TODO: Regions without SSA dominance should define a different
  // traversal order which is appropriate and can be used here.
  if (!hasSSADominance)
    return;

  ...
}
youngar updated this revision to Diff 334306.Mar 30 2021, 5:18 PM

Use dominfo->hasDominanceInfo to determinine if the region has dominance. Funnel this information through function arguments.

rriddle accepted this revision.Mar 31 2021, 12:42 AM

Thanks for fixing this!

mlir/lib/Transforms/CSE.cpp
129

nit: Can you use llvm::zip(existing->getResults(), op->getResults()) here?

This revision is now accepted and ready to land.Mar 31 2021, 12:42 AM
youngar updated this revision to Diff 334377.Mar 31 2021, 1:37 AM

Use llvm::zip

This revision was landed with ongoing or failed builds.Mar 31 2021, 12:20 PM
This revision was automatically updated to reflect the committed changes.