Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include output shape in shape mismatch error message #2264

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions stablehlo/dialect/Base.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ bool isCompatibleForHloTypeInference(TypeRange tp1, TypeRange tp2);
// undefined behavior.
bool isCompatibleForHloTypeInference(Value shape1, Type tp2);

// Returns true if the given shape, expressed as a slice of integers, is
// compatible with the given type for the purposes of HLO type inference.
bool isCompatibleForHloTypeInference(ArrayRef<int64_t> shape1, Type tp2);

// TODO(zhouxin) Move type inference related methods to TypeInference.cpp

std::pair<int64_t, int64_t> inferConcatenatedDimAndBound(int64_t leftSize,
Expand Down
16 changes: 12 additions & 4 deletions stablehlo/dialect/TypeInference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3792,10 +3792,18 @@ LogicalResult verifyDynamicReshapeOp(std::optional<Location> location,
}
}

if (!isCompatibleForHloTypeInference(outputShape, resultType))
return emitOptionalError(
location, "output_shape is incompatible with return type of operation ",
resultType);
if (SmallVector<int64_t> shape;
succeeded(matchInts(outputShape, shape)) &&
!isCompatibleForHloTypeInference(shape, resultType)) {
std::string str;
llvm::raw_string_ostream os(str);
os << "[";
llvm::interleaveComma(shape, os, [&](int64_t i) { os << i; });
os << "]";
return emitOptionalError(location, "output_shape ", os.str(),
" is incompatible with return type of operation ",
resultType);
}
return success();
}

Expand Down
2 changes: 1 addition & 1 deletion stablehlo/tests/ops_stablehlo.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -3165,7 +3165,7 @@ func.func @dynamic_reshape_incompatible_shapes(%arg0: tensor<?xf32>, %shape: ten
// -----

func.func @dynamic_reshape_output_shape_mismatching_size(%arg0: tensor<4xf32>) -> tensor<1x4xf32> {
// expected-error@+2 {{output_shape is incompatible with return type of operation 'tensor<1x4xf32>'}}
// expected-error@+2 {{output_shape [2, 2] is incompatible with return type of operation 'tensor<1x4xf32>'}}
%0 = stablehlo.constant dense<[2, 2]> : tensor<2xi64>
%1 = stablehlo.dynamic_reshape %arg0, %0 : (tensor<4xf32>, tensor<2xi64>) -> tensor<1x4xf32>
return %1 : tensor<1x4xf32>
Expand Down
Loading