llvm-project
100 строк · 3.6 Кб
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 <cstdint>10#include <limits>11
12#include "mlir/IR/AffineExpr.h"13#include "mlir/IR/Builders.h"14#include "gtest/gtest.h"15
16using namespace mlir;17
18// Test creating AffineExprs using the overloaded binary operators.
19TEST(AffineExprTest, constructFromBinaryOperators) {20MLIRContext ctx;21OpBuilder b(&ctx);22
23auto d0 = b.getAffineDimExpr(0);24auto d1 = b.getAffineDimExpr(1);25
26auto sum = d0 + d1;27auto difference = d0 - d1;28auto product = d0 * d1;29auto remainder = d0 % d1;30
31ASSERT_EQ(sum.getKind(), AffineExprKind::Add);32ASSERT_EQ(difference.getKind(), AffineExprKind::Add);33ASSERT_EQ(product.getKind(), AffineExprKind::Mul);34ASSERT_EQ(remainder.getKind(), AffineExprKind::Mod);35}
36
37TEST(AffineExprTest, constantFolding) {38MLIRContext ctx;39OpBuilder b(&ctx);40auto cn1 = b.getAffineConstantExpr(-1);41auto c0 = b.getAffineConstantExpr(0);42auto c1 = b.getAffineConstantExpr(1);43auto c2 = b.getAffineConstantExpr(2);44auto c3 = b.getAffineConstantExpr(3);45auto c6 = b.getAffineConstantExpr(6);46auto cmax = b.getAffineConstantExpr(std::numeric_limits<int64_t>::max());47auto cmin = b.getAffineConstantExpr(std::numeric_limits<int64_t>::min());48
49ASSERT_EQ(getAffineBinaryOpExpr(AffineExprKind::Add, c1, c2), c3);50ASSERT_EQ(getAffineBinaryOpExpr(AffineExprKind::Mul, c2, c3), c6);51ASSERT_EQ(getAffineBinaryOpExpr(AffineExprKind::FloorDiv, c3, c2), c1);52ASSERT_EQ(getAffineBinaryOpExpr(AffineExprKind::CeilDiv, c3, c2), c2);53
54// Test division by zero:55auto c3ceildivc0 = getAffineBinaryOpExpr(AffineExprKind::CeilDiv, c3, c0);56ASSERT_EQ(c3ceildivc0.getKind(), AffineExprKind::CeilDiv);57
58auto c3floordivc0 = getAffineBinaryOpExpr(AffineExprKind::FloorDiv, c3, c0);59ASSERT_EQ(c3floordivc0.getKind(), AffineExprKind::FloorDiv);60
61auto c3modc0 = getAffineBinaryOpExpr(AffineExprKind::Mod, c3, c0);62ASSERT_EQ(c3modc0.getKind(), AffineExprKind::Mod);63
64// Test overflow:65auto cmaxplusc1 = getAffineBinaryOpExpr(AffineExprKind::Add, cmax, c1);66ASSERT_EQ(cmaxplusc1.getKind(), AffineExprKind::Add);67
68auto cmaxtimesc2 = getAffineBinaryOpExpr(AffineExprKind::Mul, cmax, c2);69ASSERT_EQ(cmaxtimesc2.getKind(), AffineExprKind::Mul);70
71auto cminceildivcn1 =72getAffineBinaryOpExpr(AffineExprKind::CeilDiv, cmin, cn1);73ASSERT_EQ(cminceildivcn1.getKind(), AffineExprKind::CeilDiv);74
75auto cminfloordivcn1 =76getAffineBinaryOpExpr(AffineExprKind::FloorDiv, cmin, cn1);77ASSERT_EQ(cminfloordivcn1.getKind(), AffineExprKind::FloorDiv);78}
79
80TEST(AffineExprTest, divisionSimplification) {81MLIRContext ctx;82OpBuilder b(&ctx);83auto cn6 = b.getAffineConstantExpr(-6);84auto c6 = b.getAffineConstantExpr(6);85auto d0 = b.getAffineDimExpr(0);86auto d1 = b.getAffineDimExpr(1);87
88ASSERT_EQ(c6.floorDiv(-1), cn6);89ASSERT_EQ((d0 * 6).floorDiv(2), d0 * 3);90ASSERT_EQ((d0 * 6).floorDiv(4).getKind(), AffineExprKind::FloorDiv);91ASSERT_EQ((d0 * 6).floorDiv(-2), d0 * -3);92ASSERT_EQ((d0 * 6 + d1).floorDiv(2), d0 * 3 + d1.floorDiv(2));93ASSERT_EQ((d0 * 6 + d1).floorDiv(-2), d0 * -3 + d1.floorDiv(-2));94ASSERT_EQ((d0 * 6 + d1).floorDiv(4).getKind(), AffineExprKind::FloorDiv);95
96ASSERT_EQ(c6.ceilDiv(-1), cn6);97ASSERT_EQ((d0 * 6).ceilDiv(2), d0 * 3);98ASSERT_EQ((d0 * 6).ceilDiv(4).getKind(), AffineExprKind::CeilDiv);99ASSERT_EQ((d0 * 6).ceilDiv(-2), d0 * -3);100}
101