brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.5 KiB · d4cef29 Raw
115 lines · cpp
1//===- LoopLikeInterface.cpp - Loop-like operations in MLIR ---------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "mlir/Interfaces/LoopLikeInterface.h"10 11#include "mlir/Interfaces/FunctionInterfaces.h"12 13using namespace mlir;14 15/// Include the definitions of the loop-like interfaces.16#include "mlir/Interfaces/LoopLikeInterface.cpp.inc"17 18bool LoopLikeOpInterface::blockIsInLoop(Block *block) {19  Operation *parent = block->getParentOp();20 21  // The block could be inside a loop-like operation22  if (isa<LoopLikeOpInterface>(parent) ||23      parent->getParentOfType<LoopLikeOpInterface>())24    return true;25 26  // This block might be nested inside another block, which is in a loop27  if (!isa<FunctionOpInterface>(parent))28    if (mlir::Block *parentBlock = parent->getBlock())29      if (blockIsInLoop(parentBlock))30        return true;31 32  // Or the block could be inside a control flow graph loop:33  // A block is in a control flow graph loop if it can reach itself in a graph34  // traversal35  DenseSet<Block *> visited;36  SmallVector<Block *> stack;37  stack.push_back(block);38  while (!stack.empty()) {39    Block *current = stack.pop_back_val();40    auto [it, inserted] = visited.insert(current);41    if (!inserted) {42      // loop detected43      if (current == block)44        return true;45      continue;46    }47 48    stack.reserve(stack.size() + current->getNumSuccessors());49    for (Block *successor : current->getSuccessors())50      stack.push_back(successor);51  }52  return false;53}54 55LogicalResult detail::verifyLoopLikeOpInterface(Operation *op) {56  // Note: These invariants are also verified by the RegionBranchOpInterface,57  // but the LoopLikeOpInterface provides better error messages.58  auto loopLikeOp = cast<LoopLikeOpInterface>(op);59 60  // Verify number of inits/iter_args/yielded values/loop results.61  if (loopLikeOp.getInits().size() != loopLikeOp.getRegionIterArgs().size())62    return op->emitOpError("different number of inits and region iter_args: ")63           << loopLikeOp.getInits().size()64           << " != " << loopLikeOp.getRegionIterArgs().size();65  if (!loopLikeOp.getYieldedValues().empty() &&66      loopLikeOp.getRegionIterArgs().size() !=67          loopLikeOp.getYieldedValues().size())68    return op->emitOpError(69               "different number of region iter_args and yielded values: ")70           << loopLikeOp.getRegionIterArgs().size()71           << " != " << loopLikeOp.getYieldedValues().size();72  if (loopLikeOp.getLoopResults() && loopLikeOp.getLoopResults()->size() !=73                                         loopLikeOp.getRegionIterArgs().size())74    return op->emitOpError(75               "different number of loop results and region iter_args: ")76           << loopLikeOp.getLoopResults()->size()77           << " != " << loopLikeOp.getRegionIterArgs().size();78 79  // Verify types of inits/iter_args/yielded values/loop results.80  int64_t i = 0;81  auto yieldedValues = loopLikeOp.getYieldedValues();82  for (const auto [index, init, regionIterArg] :83       llvm::enumerate(loopLikeOp.getInits(), loopLikeOp.getRegionIterArgs())) {84    if (init.getType() != regionIterArg.getType())85      return op->emitOpError(std::to_string(index))86             << "-th init and " << index87             << "-th region iter_arg have different type: " << init.getType()88             << " != " << regionIterArg.getType();89    if (!yieldedValues.empty()) {90      if (regionIterArg.getType() != yieldedValues[index].getType())91        return op->emitOpError(std::to_string(index))92               << "-th region iter_arg and " << index93               << "-th yielded value have different type: "94               << regionIterArg.getType()95               << " != " << yieldedValues[index].getType();96    }97    ++i;98  }99  i = 0;100  if (loopLikeOp.getLoopResults()) {101    for (const auto it : llvm::zip_equal(loopLikeOp.getRegionIterArgs(),102                                         *loopLikeOp.getLoopResults())) {103      if (std::get<0>(it).getType() != std::get<1>(it).getType())104        return op->emitOpError(std::to_string(i))105               << "-th region iter_arg and " << i106               << "-th loop result have different type: "107               << std::get<0>(it).getType()108               << " != " << std::get<1>(it).getType();109    }110    ++i;111  }112 113  return success();114}115