Where to move utility function?

The function below in already duplicated in the upstream code base and we have a use for it. Where it will be wise to move such utility functions so that any conversion/analysis can make use of them and remove the duplicates.

It is present in the following files:

  • mlir/lib/Dialect/Linalg/IR/LinalgInterfaces.cpp
  • mlir/lib/Dialect/Linalg/Transforms/Vectorization.cpp
/// Return the unique instance of OpType in `block` if it is indeed unique.
/// Return null if none or more than 1 instances exist.
template <typename OpType>
static OpType getSingleOpOfType(Block &block) {
  OpType res = nullptr;
  block.walk([&](OpType op) {
    if (res) {
      res = nullptr;
      return WalkResult::interrupt();
    res = op;
    return WalkResult::advance();
  return res;

Looks generic enough. A new file include/mlir/IR/BlockUtilities.h? I’m hesitant to put it directly into Block.h.

Patch up for review https://reviews.llvm.org/D110145

thanks for the awesome information.