-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TIR][Arith] Additional Simplifications Inside Conditionals (#11524)
* [TIR][Arith] Use equality constraints in analyzer Previously, constraints with inequalities were recognized and used for simplifications by `ConstIntBoundAnalyzer` and `ModularSetAnalyzer`, but constraints with equalities were not. This adds equality-based constraints. (e.g. Inside the then-case of `if i==5`, the value of `i` is known to be 5.) * [TIR][Arith] RewriteSimplifier, apply literal constraints Previously, constraints were only checked within a `tir.likely` annotation. After this change, constraints are used for simplification of all boolean expressions. (e.g. Within a conditional `if i==n`, the expression `(i==n) and (j==m)` can be simplified to `j==m`.) * [TIR][Arith] Do not apply literal constraints to BufferLoad If a literal constraint relies on the contents of a buffer, the constraint may not be assumed to hold. This prevents the incorrect rewriting of `A[i]==n` to true within a `if A[i]==n` conditional, as the value of `A[i]` may have changed. * [TIR][Arith] Use each independent constraints in RewriteSimplifier Inside a constraint `if i==n and j==m`, both `i==n` and `j==m` may be replaced with true, even in separate expressions. This commit uses a new internal utility function `tvm::arith::ExtractConstraints`, which breaks up a boolean expression into a list of true statements. This may be used to reduce duplication elsewhere, such as `const_int_bound.cc` and `iter_affine_map.cc`. * [TIR][Arith] Check for negation of literal constraints When inside a conditional of `i!=n`, in addition to the previous replacement of `i!=n` with true, we can also replace `i==n` with false. * [TIR][Arith] Added unittests for new simplifications * Fix lint error * Fixed handling of negation of non-boolean types * Removed extra asterisk
- Loading branch information
1 parent
03eefe0
commit c78539c
Showing
7 changed files
with
398 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/*! | ||
* \file tvm/arith/constraint_extract.cc | ||
*/ | ||
|
||
#include "constraint_extract.h" | ||
|
||
#include <tvm/arith/analyzer.h> | ||
#include <tvm/tir/expr.h> | ||
|
||
#include "pattern_match.h" | ||
|
||
namespace tvm { | ||
namespace arith { | ||
|
||
void CollectConstraints(const PrimExpr& expr, Analyzer* analyzer, std::vector<PrimExpr>* collect) { | ||
collect->push_back(expr); | ||
|
||
PVar<PrimExpr> x, y; | ||
if ((x && y).Match(expr)) { | ||
CollectConstraints(x.Eval(), analyzer, collect); | ||
CollectConstraints(y.Eval(), analyzer, collect); | ||
} else if ((!(x || y)).Match(expr)) { | ||
CollectConstraints(analyzer->rewrite_simplify(tir::Not(x.Eval())), analyzer, collect); | ||
CollectConstraints(analyzer->rewrite_simplify(tir::Not(y.Eval())), analyzer, collect); | ||
} | ||
} | ||
|
||
std::vector<PrimExpr> ExtractConstraints(const PrimExpr& expr) { | ||
std::vector<PrimExpr> out; | ||
Analyzer analyzer; | ||
CollectConstraints(expr, &analyzer, &out); | ||
return out; | ||
} | ||
|
||
} // namespace arith | ||
} // namespace tvm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/*! | ||
* \file contraint_extract.h | ||
* | ||
* \brief Centralized location for extraction of constraints from a boolean expression. | ||
*/ | ||
|
||
#ifndef TVM_ARITH_CONSTRAINT_EXTRACT_H_ | ||
#define TVM_ARITH_CONSTRAINT_EXTRACT_H_ | ||
|
||
#include <tvm/tir/expr.h> | ||
|
||
#include <vector> | ||
|
||
namespace tvm { | ||
namespace arith { | ||
|
||
/* \brief Returns constraints that are true if the expression is true. | ||
* | ||
* Utility to break up a boolean expression into independent | ||
* constraints. | ||
* | ||
* Example: `i==5 && j==3` => `[i==5 && j==3, i==5, j==3]` | ||
* Example: `i==5 || j==3` => `[i==5 || j==3]` | ||
* Example: `!(i>5 || j==3)` => `[!(i==5 || j==3), i<=5, j!=3]` | ||
* | ||
* Intended for use in bounds analysis or simplification within a | ||
* conditional, or identifying independent conditionals that may be | ||
* hoisted. | ||
* | ||
* \param expr The expression to be analyzers | ||
* | ||
* \returns A vector of independent constraints | ||
*/ | ||
std::vector<PrimExpr> ExtractConstraints(const PrimExpr& expr); | ||
|
||
} // namespace arith | ||
} // namespace tvm | ||
|
||
#endif // TVM_ARITH_CONSTRAINT_EXTRACT_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.