This is an archive of the discontinued LLVM Phabricator instance.

[mlir][transform] ApplyPatternsOp: Add check to prevent modifying the transform IR
ClosedPublic

Authored by springerm on Jun 16 2023, 2:17 AM.

Details

Summary

Add an extra check to make sure that transform IR is not getting modified by this op while it is being interpreted. This generally dangerous and we may want to enforce this for all transform ops that modify the payload in the future.

Users should generally try to apply patterns only to the piece of IR where it is needed (e.g., a matched function) and not the entire module (which may contain the transform IR).

This revision is in response to a crash in a downstream compiler that was caused by a dead transform.structured.match op that was removed by the GreedyPatternRewriteDriver's DCE while the enclosing sequence was being interpreted.

Diff Detail

Event Timeline

springerm created this revision.Jun 16 2023, 2:17 AM
Herald added a project: Restricted Project. · View Herald Transcript
springerm requested review of this revision.Jun 16 2023, 2:17 AM
ftynse accepted this revision.Jun 16 2023, 5:41 AM
This revision is now accepted and ready to land.Jun 16 2023, 5:41 AM

It would indeed make sense to check for transformations not applying to the transform IR. As a matter of fact, transform side effects were originally introduced to avoid DCE. We may start with a blanket check and see what breaks. Since we already have effects, we can allow transformations that don't have the "modify payload IR" effect to take handles pointing to transform IR. These typically would be just navigating the IR to, e.g., find the module adjacent to the transform one.

The tricky part is that the modifesPayload is "bubbled" up from the region of SequenceOps. E.g.:

transform.sequence %module_op {
  %0 = match "func.func" in %module_op
  apply_patterns to %0
}

The sequence "modifies the payload", even though only apply_patterns does. So the check as it is written in this revision will throw an error: transform.sequence modifies the payload (and %module_op contains the transform IR).

The side effect modeling for ops with regions is in a sad state, unfortunately. Normally, we should be able to differentiate between "proper" effects of the op and nested effects, and have no need to manually collect nested effects, but we cannot :(