Skip to content

[mlir][tensor] Check the EmptyOp's dynamicSize to be non-negative #65577

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

Merged
merged 2 commits into from
Sep 11, 2023

Conversation

sott0n
Copy link
Contributor

@sott0n sott0n commented Sep 7, 2023

This patch addresses a crash that occurs when negative dynamic sizes are provided in tensor.emptyOp by adding a check to ensure that dynamic sizes are non-negative.

Fixes #64064

@sott0n sott0n requested a review from a team as a code owner September 7, 2023 07:48
@github-actions github-actions bot added mlir:core MLIR Core Infrastructure mlir mlir:tensor labels Sep 7, 2023
@@ -621,6 +621,18 @@ LogicalResult EmptyOp::verify() {
return emitOpError("incorrect number of dynamic sizes, has ")
<< getDynamicSizes().size() << ", expected "
<< getType().getNumDynamicDims();

if (getDynamicSizes().size() > 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check does not seem needed?

if (getDynamicSizes().size() > 0) {
if (llvm::any_of(getDynamicSizes(), [](Value operand) {
APInt constSizeArg;
if (!matchPattern(operand, m_ConstantInt(&constSizeArg))) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not belong to the verifier, because: https://mlir.llvm.org/getting_started/DeveloperGuide/#ir-verifier

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for teaching me about IR's verifier.

This is why the guidelines to write verifier is to stick to information local to an operation (think “what I see when I print this operation alone”). Looking through operands or results is extremely unusual and should be avoided.

I understand it, so i will remove this checker from verifier.

@@ -691,6 +703,9 @@ struct ReplaceEmptyTensorStaticShapeDims : OpRewritePattern<EmptyOp> {
Value dynamicSize = op.getDynamicSizes()[ctr++];
std::optional<int64_t> cst = getConstantIntValue(dynamicSize);
if (cst.has_value()) {
// dynamic size must be non-negative.
if (cst.value() < 0)
return failure();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be enough without the verifier right?

@sott0n
Copy link
Contributor Author

sott0n commented Sep 11, 2023

@joker-eph Could you merge it?

@joker-eph joker-eph merged commit ca8cf90 into llvm:main Sep 11, 2023
@sott0n sott0n deleted the fix-tensor-empty-negative-size branch September 12, 2023 01:32
ZijunZhaoCCK pushed a commit to ZijunZhaoCCK/llvm-project that referenced this pull request Sep 19, 2023
…vm#65577)

This patch addresses a crash that occurs when negative dynamic sizes are
provided in tensor.emptyOp by adding a check to ensure that dynamic
sizes are non-negative.

Fixes llvm#64064
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mlir:core MLIR Core Infrastructure mlir:tensor mlir
Projects
None yet
2 participants