[LLD][COFF] Emit tail merge pdata for delay load thunks on ARM64EC (#116810)
[llvm-project.git] / mlir / unittests / Analysis / Presburger / UtilsTest.cpp
blobd91a0f2da9ceee1285d80135dc053649b2e5ef0c
1 //===- Utils.cpp - Tests for Utils file ----------===//
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 //===----------------------------------------------------------------------===//
9 #include "mlir/Analysis/Presburger/Utils.h"
11 #include <gmock/gmock.h>
12 #include <gtest/gtest.h>
14 using namespace mlir;
15 using namespace presburger;
17 static DivisionRepr
18 parseDivisionRepr(unsigned numVars, unsigned numDivs,
19 ArrayRef<ArrayRef<DynamicAPInt>> dividends,
20 ArrayRef<DynamicAPInt> divisors) {
21 DivisionRepr repr(numVars, numDivs);
22 for (unsigned i = 0, rows = dividends.size(); i < rows; ++i)
23 repr.setDiv(i, dividends[i], divisors[i]);
24 return repr;
27 static void checkEqual(DivisionRepr &a, DivisionRepr &b) {
28 EXPECT_EQ(a.getNumVars(), b.getNumVars());
29 EXPECT_EQ(a.getNumDivs(), b.getNumDivs());
30 for (unsigned i = 0, rows = a.getNumDivs(); i < rows; ++i) {
31 EXPECT_EQ(a.hasRepr(i), b.hasRepr(i));
32 if (!a.hasRepr(i))
33 continue;
34 EXPECT_TRUE(a.getDenom(i) == b.getDenom(i));
35 EXPECT_TRUE(a.getDividend(i).equals(b.getDividend(i)));
39 TEST(UtilsTest, ParseAndCompareDivisionReprTest) {
40 auto merge = [](unsigned i, unsigned j) -> bool { return true; };
41 DivisionRepr a = parseDivisionRepr(1, 1, {{DynamicAPInt(1), DynamicAPInt(2)}},
42 {DynamicAPInt(2)}),
43 b = parseDivisionRepr(1, 1, {{DynamicAPInt(1), DynamicAPInt(2)}},
44 {DynamicAPInt(2)}),
45 c = parseDivisionRepr(
46 2, 2,
47 {{DynamicAPInt(0), DynamicAPInt(1), DynamicAPInt(2)},
48 {DynamicAPInt(0), DynamicAPInt(1), DynamicAPInt(2)}},
49 {DynamicAPInt(2), DynamicAPInt(2)});
50 c.removeDuplicateDivs(merge);
51 checkEqual(a, b);
52 checkEqual(a, c);
55 TEST(UtilsTest, DivisionReprNormalizeTest) {
56 auto merge = [](unsigned i, unsigned j) -> bool { return true; };
57 DivisionRepr a = parseDivisionRepr(
58 2, 1, {{DynamicAPInt(1), DynamicAPInt(2), DynamicAPInt(-1)}},
59 {DynamicAPInt(2)}),
60 b = parseDivisionRepr(
61 2, 1,
62 {{DynamicAPInt(16), DynamicAPInt(32), DynamicAPInt(-16)}},
63 {DynamicAPInt(32)}),
64 c = parseDivisionRepr(1, 1,
65 {{DynamicAPInt(12), DynamicAPInt(-4)}},
66 {DynamicAPInt(8)}),
67 d = parseDivisionRepr(
68 2, 2,
69 {{DynamicAPInt(1), DynamicAPInt(2), DynamicAPInt(-1)},
70 {DynamicAPInt(4), DynamicAPInt(8), DynamicAPInt(-4)}},
71 {DynamicAPInt(2), DynamicAPInt(8)});
72 b.removeDuplicateDivs(merge);
73 c.removeDuplicateDivs(merge);
74 d.removeDuplicateDivs(merge);
75 checkEqual(a, b);
76 checkEqual(c, d);
79 TEST(UtilsTest, convolution) {
80 std::vector<Fraction> aVals({1, 2, 3, 4});
81 std::vector<Fraction> bVals({7, 3, 1, 6});
82 ArrayRef<Fraction> a(aVals);
83 ArrayRef<Fraction> b(bVals);
85 std::vector<Fraction> conv = multiplyPolynomials(a, b);
87 EXPECT_EQ(conv, std::vector<Fraction>({7, 17, 28, 45, 27, 22, 24}));
89 aVals = {3, 6, 0, 2, 5};
90 bVals = {2, 0, 6};
91 a = aVals;
92 b = bVals;
94 conv = multiplyPolynomials(a, b);
95 EXPECT_EQ(conv, std::vector<Fraction>({6, 12, 18, 40, 10, 12, 30}));