[mlir] Fix `Region`s `takeBody` method if the region is not empty
authorMarkus Böck <markus.boeck02@gmail.com>
Thu, 21 Apr 2022 13:32:21 +0000 (15:32 +0200)
committerMarkus Böck <markus.boeck02@gmail.com>
Thu, 21 Apr 2022 13:32:59 +0000 (15:32 +0200)
commit850b2c6b3c73a48cce05c163c49fcea89491a5e1
treec03e3f8d83274aee7f12d59298524039e5de2905
parent95d77383f2ba8d3136856b52520d3f73f9bc89e7
[mlir] Fix `Region`s `takeBody` method if the region is not empty

The current implementation of takeBody first clears the Region, before then taking ownership of the blocks of the other regions. The issue here however, is that when clearing the region, it does not take into account references of operations to each other. In particular, blocks are deleted from front to back, and operations within a block are very likely to be deleted despite still having uses, causing an assertion to trigger [0].

This patch fixes that issue by simply calling dropAllReferences()before clearing the blocks.

[0] https://github.com/llvm/llvm-project/blob/9a8bb4bc635de9d56706262083c15eb1e0cf3e87/mlir/lib/IR/Operation.cpp#L154

Differential Revision: https://reviews.llvm.org/D123913
mlir/include/mlir/IR/Region.h
mlir/test/IR/test-take-body.mlir [new file with mode: 0644]
mlir/test/lib/IR/CMakeLists.txt
mlir/test/lib/IR/TestRegions.cpp [new file with mode: 0644]
mlir/tools/mlir-opt/mlir-opt.cpp