[Mlir-commits] [mlir] [mlir][linalg] Avoid asserts in IndexingMapOpInterface (PR #179072)

Samarth Narang llvmlistbot at llvm.org
Sat Jan 31 17:11:53 PST 2026


snarang181 wrote:

> @sweiglbosker, LMK how this look. We can probably emit a better diagnostic this way.
> 
> ```c++
> diff --git a/mlir/lib/Interfaces/IndexingMapOpInterface.cpp b/mlir/lib/Interfaces/IndexingMapOpInterface.cpp
> index 2ef36a21a1ac..115408da5af6 100644
> --- a/mlir/lib/Interfaces/IndexingMapOpInterface.cpp
> +++ b/mlir/lib/Interfaces/IndexingMapOpInterface.cpp
> @@ -35,12 +35,39 @@ LogicalResult mlir::IndexingMapOpInterface::verifyImpl() {
>        return this->emitOpError("unexpected symbols in indexing_map #")
>               << opOperand.getOperandNumber();
>  
> +    Type operandTy = opOperand.get().getType();
> +
> +    // Ranked container.
> +    auto rankedTensorTy = dyn_cast<RankedTensorType>(operandTy);
> +    auto memrefTy = dyn_cast<MemRefType>(operandTy);
> +
> +    // Unranked tensor is an "unknown rank".
> +    bool unrankedTensor = isa<UnrankedTensorType>(operandTy);
> +
> +    // Other shaped (but not supported) types are invalid.
> +    bool shapedButNotSupported = isa<ShapedType>(operandTy) &&
> +                                 !rankedTensorTy && !memrefTy &&
> +                                 !unrankedTensor && !isa<VectorType>(operandTy);
> +
>      // Result rank must match operand rank.
> -    if (indexingMap.getNumResults() != rank)
> +    if (indexingMap.getNumResults() != rank) {
> +      // If this operand does not have a meaningful rank,
> +      // emit a type-based diagnostic instead of a rank-based one.
> +      if (unrankedTensor || shapedButNotSupported) {
> +        return this->emitOpError("expected operand #")
> +               << opOperand.getOperandNumber()
> +               << " to be a ranked tensor or memref type to match the result "
> +                  "rank of "
> +                  "indexing_map ("
> +               << indexingMap.getNumResults() << "), but got " << operandTy;
> +      }
> +
> +      // Scalars/vectors (and true rank-0 cases).
>        return this->emitOpError("expected operand #")
>               << opOperand.getOperandNumber() << " rank (" << rank
>               << ") to match the result rank of indexing_map ("
>               << indexingMap.getNumResults() << ")";
> +    }
>  
>      llvm::append_range(allShapesSizes, shape);
>    }
> ```

and then, the diagnostic becomes
`unexpected error: 'linalg.generic' op expected operand #0 to be a ranked tensor or memref type to match the result rank of indexing_map (2), but got 'tensor<*xf32>'`

https://github.com/llvm/llvm-project/pull/179072


More information about the Mlir-commits mailing list