[MLIR] Fix assert expressions (#112474)

I noticed that several assertions in MLIR codebase have issues with
operator precedence

The issue with operator precedence in these assertions is due to the way
logical operators are evaluated. The `&&` operator has higher precedence
than the `||` operator, which means the assertion is currently
evaluating incorrectly, like this:
```
assert((resType.getNumDynamicDims() == dynOutDims.size()) ||
       (dynOutDims.empty() && "Either none or all output dynamic dims must be specified!"));
```

We should add parentheses around the entire expression involving
`dynOutDims.empty()` to ensure that the logical conditions are grouped
correctly. Here’s the corrected version:
```
assert(((resType.getNumDynamicDims() == dynOutDims.size()) || dynOutDims.empty()) &&
       "Either none or all output dynamic dims must be specified!");

```
This commit is contained in:
Alexander Pivovarov 2024-10-16 15:22:29 -07:00 committed by GitHub
parent 0a53f43c0c
commit a24c468782
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 10 additions and 10 deletions

View File

@ -892,8 +892,8 @@ FlatLinearValueConstraints::FlatLinearValueConstraints(IntegerSet set,
set.getNumDims() + set.getNumSymbols() + 1,
set.getNumDims(), set.getNumSymbols(),
/*numLocals=*/0) {
assert(operands.empty() ||
set.getNumInputs() == operands.size() && "operand count mismatch");
assert((operands.empty() || set.getNumInputs() == operands.size()) &&
"operand count mismatch");
// Set the values for the non-local variables.
for (unsigned i = 0, e = operands.size(); i < e; ++i)
setValue(i, operands[i]);

View File

@ -840,9 +840,9 @@ enum VectorMemoryAccessKind { ScalarBroadcast, Contiguous, Gather };
/// TODO: Statically shaped loops + vector masking
static uint64_t getTrailingNonUnitLoopDimIdx(LinalgOp linalgOp) {
SmallVector<int64_t> loopRanges = linalgOp.getStaticLoopRanges();
assert(linalgOp.hasDynamicShape() ||
llvm::count_if(loopRanges, [](int64_t dim) { return dim != 1; }) ==
1 &&
assert(
(linalgOp.hasDynamicShape() ||
llvm::count_if(loopRanges, [](int64_t dim) { return dim != 1; }) == 1) &&
"For statically shaped Linalg Ops, only one "
"non-unit loop dim is expected");

View File

@ -27,8 +27,8 @@ PadOp mlir::tensor::createPadHighOp(RankedTensorType resType, Value source,
OpBuilder &b,
SmallVector<Value> dynOutDims) {
assert((resType.getNumDynamicDims() == dynOutDims.size()) ||
dynOutDims.empty() &&
assert(((resType.getNumDynamicDims() == dynOutDims.size()) ||
dynOutDims.empty()) &&
"Either none or all output dynamic dims must be specified!");
// Init "low" and "high" padding values ("low" is kept as is, "high" is