Repository: llvm/llvm-project
File: mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp (C++)

Code before:

  if (llvm::is_contained(shapeValues, kInferableDim))
    return op->emitOpError("shape input contains inferable dimension (-1) "
                           "which does not conform to the TOSA specification");

  return success();
}

LogicalResult checkErrorIfSlice(Operation *op) {
  auto sliceOp = dyn_cast<tosa::SliceOp>(op);
  if (!sliceOp)
    return success();

  constexpr int64_t kInferableDim = -1;
  SmallVector<int64_t> startValues;
  SmallVector<int64_t> sizeValues;
  const bool hasStartValues = tosa::getConstShapeValues(
      sliceOp.getStart().getDefiningOp(), startValues);
  const bool hasSizeValues =
      tosa::getConstShapeValues(sliceOp.getSize().getDefiningOp(), sizeValues);

  if (hasStartValues && llvm::is_contained(startValues, kInferableDim))
    return op->emitOpError("start input contains inferable dimension (-1) "
                           "which does not conform to the TOSA specification");
  if (hasSizeValues && llvm::is_contained(sizeValues, kInferableDim))
    return op->emitOpError("size input contains inferable dimension (-1) which "
                           "does not conform to the TOSA specification");

  return success();
}

static bool isOpIsolatedWithinRegion(Operation *op, Region *region) {
  return llvm::all_of(op->getOperands(), [&](auto operand) {
    Region *operandRegion = operand.getParentRegion();
    return operandRegion && region->isAncestor(operandRegion);
  });
}

static LogicalResult isRegionIsolatedFromAbove(Region &regionToCheck) {
  bool noLiveInValue = true;
  regionToCheck.walk([&noLiveInValue, &regionToCheck](Operation *op) {
    if (!isOpIsolatedWithinRegion(op, &regionToCheck)) {
      noLiveInValue = false;
      return WalkResult::interrupt();
    }
    return WalkResult::advance();
  });
  return noLiveInValue ? success() : failure();
}

LogicalResult checkIsolatedRegion(Operation *op, Region &regionToCheck,

Review comment (refactor):
This also seems to be a breaking change for us. 

Source IR:
```
func.func @slice_nofold(%arg0: tensor<?x4xf32>) -> tensor<?x2xf32> {
  %0 = tosa.const_shape {values = dense<[0, 0]> : tensor<2xindex>} : () -> !tosa.shape<2>
  %1 = tosa.const_shape {values = dense<[-1, 2]> : tensor<2xindex>} : () -> !tosa.shape<2>
  %3 = tosa.slice %arg0, %0, %1 : (tensor<?x4xf32>, !tosa.shape<2>, !tosa.shape<2>) -> tensor<?x2xf32>
  return %3 : tensor<?x2xf32>
}
```

when lowered to `tensor` produces:
```
mlir-opt --tosa-to-tensor
func.func @slice_nofold(%arg0: tensor<?x4xf32>) -> tensor<?x2xf32> {
    %c0 = arith.constant 0 : index
    %dim = tensor.dim %arg0, %c0 : tensor<?x4xf32>
    %c0_0 = arith.constant 0 : index
    %0 = arith.subi %dim, %c0_0 : index
    %extracted_slice = tensor.extract_slice %arg0[0, 0] [%0, 2] [1, 1] : tensor<?x4xf32> to tensor<?x2xf32>
    return %extracted_slice : tensor<?x2xf32>
}
```

Code after:
  if (llvm::is_contained(shapeValues, kInferableDimSize))
    return op->emitOpError("shape input contains inferable dimension (")
           << kInferableDimSize
           << ") "
              "which does not conform to the TOSA specification";

  return success();
}

LogicalResult checkErrorIfSlice(Operation *op) {
  auto sliceOp = dyn_cast<tosa::SliceOp>(op);
  if (!sliceOp)
    return success();

  SmallVector<int64_t> startValues;
  SmallVector<int64_t> sizeValues;
  const bool hasStartValues = tosa::getConstShapeValues(
      sliceOp.getStart().getDefiningOp(), startValues);
  const bool hasSizeValues =
      tosa::getConstShapeValues(sliceOp.getSize().getDefiningOp(), sizeValues);

  if (hasStartValues && llvm::is_contained(startValues, kInferableDimSize))
    return op->emitOpError("start input contains inferable dimension (")
           << kInferableDimSize
           << ") which does not conform to the TOSA specification";
  if (hasSizeValues && llvm::is_contained(sizeValues, kInferableDimSize))
    return op->emitOpError("size input contains inferable dimension (")
           << kInferableDimSize
           << ") which "
              "does not conform to the TOSA specification";

  return success();
}

static bool isOpIsolatedWithinRegion(Operation *op, Region *region) {
  return llvm::all_of(op->getOperands(), [&](auto operand) {
    Region *operandRegion = operand.getParentRegion();
    return operandRegion && region->isAncestor(operandRegion);
  });
}

static LogicalResult isRegionIsolatedFromAbove(Region &regionToCheck) {
  bool noLiveInValue = true;
  regionToCheck.walk([&noLiveInValue, &regionToCheck](Operation *op) {
    if (!isOpIsolatedWithinRegion(op, &regionToCheck)) {
      noLiveInValue = false;
      return WalkResult::interrupt();
    }
    return WalkResult::advance();
  });
  return noLiveInValue ? success() : failure();