This is an archive of the discontinued LLVM Phabricator instance.

[MLIR] Introduce copy removal pass
Needs RevisionPublic

Authored by arnab-oss on Jan 19 2022, 6:18 AM.

Details

Summary

Introduce a pass to remove redundant copy statements from the IR.

func() {
   %source = alloc()
   write_to(%source)
   %destination = alloc()
   copy(%source, %destination)
   dealloc(%source)
   return %destination
 }

Output:

func(){
   %source = alloc()
   write_to(%source)
   return %source
 }

In the above case we can remove the copy op, and replace all uses of
destination with source, and other redundant allocations and
deallocations subject to the following constraints.

Constraints:

  1. There should not exist any users of destination before the copy op.
  2. There should not be any write operations on source and destination after copy op.

    Also, we can Remove copy op when there are no uses of destination after the copy op. In this case, we only remove the copy op, and we CANNOT replace all uses of source with destination. For example,
%source = alloc()
%destination = alloc()
write_to(%destination)
copy(%source, %destination)
dealloc(%source)
dealloc(%destination)

Output:

%source = alloc()
%destination = alloc()
write_to(%destination)
dealloc(%source)
dealloc(%destination)

Diff Detail

Event Timeline

arnab-oss created this revision.Jan 19 2022, 6:18 AM
arnab-oss requested review of this revision.Jan 19 2022, 6:18 AM
arnab-oss edited the summary of this revision. (Show Details)Jan 19 2022, 6:20 AM
bondhugula added inline comments.Jan 19 2022, 7:30 AM
mlir/include/mlir/Transforms/Passes.h
77

Copy -> copy. Specify what kind of copy operations?

mlir/include/mlir/Transforms/Passes.td
326
rriddle added inline comments.Jan 19 2022, 10:30 AM
mlir/include/mlir/Transforms/Passes.td
326

I don't see anything immediate that would require limiting this to FuncOp anyways, why not just Pass<"copy-removal">? mlir/Transforms/ should really only contain generic passes anyways.

bondhugula retitled this revision from [MLIR] Introduce Copy Removal Pass. to [MLIR] Introduce copy removal pass.Feb 12 2022, 8:38 PM
bondhugula requested changes to this revision.Feb 12 2022, 8:46 PM
bondhugula added inline comments.
mlir/test/Transforms/copy-removal.mlir
19–29

The removal is going to be incorrect in the presence of memref dialect ops that create aliases (eg. subview, expand_shape, collapse_shape). We do need at least a static utility doNoAliasProvably(Value memrefA, Value memrefB) to guard against these.

This revision now requires changes to proceed.Feb 12 2022, 8:46 PM