Skip to content

Commit a4b2363

Browse files
authored
[mlir] Don't assert when simplifying certain AffineExprs (#78855)
Currently, `simplifyMul()` asserts that either `lhs` or `rhs` is symbolic or constant. This method is called by the overloaded `*` operator for `AffineExpr`s which leads to a crash when building a multiplication expression where neither operand is symbolic or constant. This patch returns a `nullptr` from `simplifyMul()` to signal that the expression could not be simplified instead. Fix #75770
1 parent 5e379b6 commit a4b2363

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

mlir/lib/IR/AffineExpr.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,8 @@ static AffineExpr simplifyMul(AffineExpr lhs, AffineExpr rhs) {
774774
return getAffineConstantExpr(lhsConst.getValue() * rhsConst.getValue(),
775775
lhs.getContext());
776776

777-
assert(lhs.isSymbolicOrConstant() || rhs.isSymbolicOrConstant());
777+
if (!lhs.isSymbolicOrConstant() && !rhs.isSymbolicOrConstant())
778+
return nullptr;
778779

779780
// Canonicalize the mul expression so that the constant/symbolic term is the
780781
// RHS. If both the lhs and rhs are symbolic, swap them if the lhs is a

mlir/unittests/IR/AffineExprTest.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===- AffineExprTest.cpp - unit tests for affine expression API ----------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "mlir/IR/AffineExpr.h"
10+
#include "mlir/IR/Builders.h"
11+
#include "gtest/gtest.h"
12+
13+
using namespace mlir;
14+
15+
// Test creating AffineExprs using the overloaded binary operators.
16+
TEST(AffineExprTest, constructFromBinaryOperators) {
17+
MLIRContext ctx;
18+
OpBuilder b(&ctx);
19+
20+
auto d0 = b.getAffineDimExpr(0);
21+
auto d1 = b.getAffineDimExpr(1);
22+
23+
auto sum = d0 + d1;
24+
auto difference = d0 - d1;
25+
auto product = d0 * d1;
26+
auto remainder = d0 % d1;
27+
28+
ASSERT_EQ(sum.getKind(), AffineExprKind::Add);
29+
ASSERT_EQ(difference.getKind(), AffineExprKind::Add);
30+
ASSERT_EQ(product.getKind(), AffineExprKind::Mul);
31+
ASSERT_EQ(remainder.getKind(), AffineExprKind::Mod);
32+
}

mlir/unittests/IR/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
add_mlir_unittest(MLIRIRTests
22
AdaptorTest.cpp
3+
AffineExprTest.cpp
34
AffineMapTest.cpp
45
AttributeTest.cpp
56
DialectTest.cpp

0 commit comments

Comments
 (0)