[all-commits] [llvm/llvm-project] a41aaf: [mlir] Make `Regions`s `cloneInto` multithread-rea...

zero9178 via All-commits all-commits at lists.llvm.org
Thu Apr 21 04:43:50 PDT 2022

  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: a41aaf166fed03e18021885d0951f1dec63b25b9
  Author: Markus Böck <markus.boeck02 at gmail.com>
  Date:   2022-04-21 (Thu, 21 Apr 2022)

  Changed paths:
    M mlir/include/mlir/IR/Operation.h
    M mlir/include/mlir/IR/Region.h
    M mlir/lib/IR/Operation.cpp
    M mlir/lib/IR/Region.cpp

  Log Message:
  [mlir] Make `Regions`s `cloneInto` multithread-readable

Prior to this patch, `cloneInto` would do a simple walk over the blocks and contained operations and clone and map them as it encounters them. As finishing touch it then remaps any successor and operands it has remapped during that process.

This is generally fine, but sadly leads to a lot of uses of both operations and blocks from the source region, in the cloned operations in the target region. Those uses lead to writes in the use-def list of the operations, making `cloneInto` never thread safe.

This patch reimplements `cloneInto` in three steps to avoid ever creating any extra uses on elements in the source region:
* It first creates the mapping of all blocks and block operands
* It then clones all operations to create the mapping of all operation results, but does not yet clone any regions or set the operands
* After all operation results have been mapped, it now sets the operations operands and clones their regions.

That way it is now possible to call `cloneInto` from multiple threads if the Region or Operation is isolated-from-above. This allows creating copies of  functions or to use `mlir::inlineCall` with the same source region from multiple threads. In the general case, the method is thread-safe if through cloning, no new uses of `Value`s from outside the cloned Operation/Region are created. This can be ensured by mapping any outside operands via the `BlockAndValueMapping` to `Value`s owned by the caller thread.

While I was at it, I also reworked the `clone` method of `Operation` a little bit and added a proper options class to avoid having a `cloneWithoutRegionsAndOperands` method, and be more extensible in the future. `cloneWithoutRegions` is now also a simple wrapper that calls `clone` with the proper options set. That way all the operation cloning code is now contained solely within `clone`.

Differential Revision: https://reviews.llvm.org/D123917

More information about the All-commits mailing list