diff --git a/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp b/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp --- a/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp +++ b/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp @@ -1317,7 +1317,7 @@ DenseElementsAttr attr; if (!matchPattern(op.source(), m_Constant(&attr))) return {}; - // TODO: support the splat case + // TODO: Support the splat case. if (!attr || attr.isSplat()) return {}; @@ -1326,6 +1326,18 @@ if (!op.source().getDefiningOp()->hasOneUse()) return {}; + // Dynamic result shape is not supported. + auto resultType = op.result().getType().dyn_cast(); + if (!resultType || !resultType.hasStaticShape()) + return {}; + + // Control the size. Sice the way to get a new constant collects each element, + // it can have a bad impact on the compile time when the data size is big. + // TODO: create an option if a customization is needed. + constexpr int64_t kConstantFoldingMaxNumElements = 1024; + if (resultType.getNumElements() > kConstantFoldingMaxNumElements) + return {}; + if (auto intElems = attr.dyn_cast()) { if (auto folded = foldExtractSliceAfterConstant