File: AttrTypeReplacerTest.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (231 lines) | stat: -rw-r--r-- 7,917 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//===- AttrTypeReplacerTest.cpp - Sub-element replacer unit tests ---------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "mlir/IR/AttrTypeSubElements.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinTypes.h"
#include "gtest/gtest.h"

using namespace mlir;

//===----------------------------------------------------------------------===//
// CyclicAttrTypeReplacer
//===----------------------------------------------------------------------===//

TEST(CyclicAttrTypeReplacerTest, testNoRecursion) {
  MLIRContext ctx;

  CyclicAttrTypeReplacer replacer;
  replacer.addReplacement([&](BoolAttr b) {
    return StringAttr::get(&ctx, b.getValue() ? "true" : "false");
  });

  EXPECT_EQ(replacer.replace(BoolAttr::get(&ctx, true)),
            StringAttr::get(&ctx, "true"));
  EXPECT_EQ(replacer.replace(BoolAttr::get(&ctx, false)),
            StringAttr::get(&ctx, "false"));
  EXPECT_EQ(replacer.replace(mlir::UnitAttr::get(&ctx)),
            mlir::UnitAttr::get(&ctx));
}

TEST(CyclicAttrTypeReplacerTest, testInPlaceRecursionPruneAnywhere) {
  MLIRContext ctx;
  Builder b(&ctx);

  CyclicAttrTypeReplacer replacer;
  // Replacer cycles through integer attrs 0 -> 1 -> 2 -> 0 -> ...
  replacer.addReplacement([&](IntegerAttr attr) {
    return replacer.replace(b.getI8IntegerAttr((attr.getInt() + 1) % 3));
  });
  // The first repeat of any integer attr is pruned into a unit attr.
  replacer.addCycleBreaker([&](IntegerAttr attr) { return b.getUnitAttr(); });

  // No recursion case.
  EXPECT_EQ(replacer.replace(mlir::UnitAttr::get(&ctx)),
            mlir::UnitAttr::get(&ctx));
  // Starting at 0.
  EXPECT_EQ(replacer.replace(b.getI8IntegerAttr(0)), mlir::UnitAttr::get(&ctx));
  // Starting at 2.
  EXPECT_EQ(replacer.replace(b.getI8IntegerAttr(2)), mlir::UnitAttr::get(&ctx));
}

//===----------------------------------------------------------------------===//
// CyclicAttrTypeReplacerTest: ChainRecursion
//===----------------------------------------------------------------------===//

class CyclicAttrTypeReplacerChainRecursionPruningTest : public ::testing::Test {
public:
  CyclicAttrTypeReplacerChainRecursionPruningTest() : b(&ctx) {
    // IntegerType<width = N>
    // ==> FunctionType<() => IntegerType< width = (N+1) % 3>>.
    // This will create a chain of infinite length without recursion pruning.
    replacer.addReplacement([&](mlir::IntegerType intType) {
      ++invokeCount;
      return b.getFunctionType(
          {}, {mlir::IntegerType::get(&ctx, (intType.getWidth() + 1) % 3)});
    });
  }

  void setBaseCase(std::optional<unsigned> pruneAt) {
    replacer.addCycleBreaker([&, pruneAt](mlir::IntegerType intType) {
      return (!pruneAt || intType.getWidth() == *pruneAt)
                 ? std::make_optional(b.getIndexType())
                 : std::nullopt;
    });
  }

  Type getFunctionTypeChain(unsigned N) {
    Type type = b.getIndexType();
    for (unsigned i = 0; i < N; i++)
      type = b.getFunctionType({}, type);
    return type;
  };

  MLIRContext ctx;
  Builder b;
  CyclicAttrTypeReplacer replacer;
  int invokeCount = 0;
};

TEST_F(CyclicAttrTypeReplacerChainRecursionPruningTest, testPruneAnywhere0) {
  setBaseCase(std::nullopt);

  // No recursion case.
  EXPECT_EQ(replacer.replace(b.getIndexType()), b.getIndexType());
  EXPECT_EQ(invokeCount, 0);

  // Starting at 0. Cycle length is 3.
  invokeCount = 0;
  EXPECT_EQ(replacer.replace(mlir::IntegerType::get(&ctx, 0)),
            getFunctionTypeChain(3));
  EXPECT_EQ(invokeCount, 3);

  // Starting at 1. Cycle length is 5 now because of a cached replacement at 0.
  invokeCount = 0;
  EXPECT_EQ(replacer.replace(mlir::IntegerType::get(&ctx, 1)),
            getFunctionTypeChain(5));
  EXPECT_EQ(invokeCount, 2);
}

TEST_F(CyclicAttrTypeReplacerChainRecursionPruningTest, testPruneAnywhere1) {
  setBaseCase(std::nullopt);

  // Starting at 1. Cycle length is 3.
  EXPECT_EQ(replacer.replace(mlir::IntegerType::get(&ctx, 1)),
            getFunctionTypeChain(3));
  EXPECT_EQ(invokeCount, 3);
}

TEST_F(CyclicAttrTypeReplacerChainRecursionPruningTest, testPruneSpecific0) {
  setBaseCase(0);

  // Starting at 0. Cycle length is 3.
  EXPECT_EQ(replacer.replace(mlir::IntegerType::get(&ctx, 0)),
            getFunctionTypeChain(3));
  EXPECT_EQ(invokeCount, 3);
}

TEST_F(CyclicAttrTypeReplacerChainRecursionPruningTest, testPruneSpecific1) {
  setBaseCase(0);

  // Starting at 1. Cycle length is 5 (1 -> 2 -> 0 -> 1 -> 2 -> Prune).
  EXPECT_EQ(replacer.replace(mlir::IntegerType::get(&ctx, 1)),
            getFunctionTypeChain(5));
  EXPECT_EQ(invokeCount, 5);
}

//===----------------------------------------------------------------------===//
// CyclicAttrTypeReplacerTest: BranchingRecusion
//===----------------------------------------------------------------------===//

class CyclicAttrTypeReplacerBranchingRecusionPruningTest
    : public ::testing::Test {
public:
  CyclicAttrTypeReplacerBranchingRecusionPruningTest() : b(&ctx) {
    // IntegerType<width = N>
    // ==> FunctionType<
    //       IntegerType< width = (N+1) % 3> =>
    //         IntegerType< width = (N+1) % 3>>.
    // This will create a binary tree of infinite depth without pruning.
    replacer.addReplacement([&](mlir::IntegerType intType) {
      ++invokeCount;
      Type child = mlir::IntegerType::get(&ctx, (intType.getWidth() + 1) % 3);
      return b.getFunctionType({child}, {child});
    });
  }

  void setBaseCase(std::optional<unsigned> pruneAt) {
    replacer.addCycleBreaker([&, pruneAt](mlir::IntegerType intType) {
      return (!pruneAt || intType.getWidth() == *pruneAt)
                 ? std::make_optional(b.getIndexType())
                 : std::nullopt;
    });
  }

  Type getFunctionTypeTree(unsigned N) {
    Type type = b.getIndexType();
    for (unsigned i = 0; i < N; i++)
      type = b.getFunctionType(type, type);
    return type;
  };

  MLIRContext ctx;
  Builder b;
  CyclicAttrTypeReplacer replacer;
  int invokeCount = 0;
};

TEST_F(CyclicAttrTypeReplacerBranchingRecusionPruningTest, testPruneAnywhere0) {
  setBaseCase(std::nullopt);

  // No recursion case.
  EXPECT_EQ(replacer.replace(b.getIndexType()), b.getIndexType());
  EXPECT_EQ(invokeCount, 0);

  // Starting at 0. Cycle length is 3.
  invokeCount = 0;
  EXPECT_EQ(replacer.replace(mlir::IntegerType::get(&ctx, 0)),
            getFunctionTypeTree(3));
  // Since both branches are identical, this should incur linear invocations
  // of the replacement function instead of exponential.
  EXPECT_EQ(invokeCount, 3);

  // Starting at 1. Cycle length is 5 now because of a cached replacement at 0.
  invokeCount = 0;
  EXPECT_EQ(replacer.replace(mlir::IntegerType::get(&ctx, 1)),
            getFunctionTypeTree(5));
  EXPECT_EQ(invokeCount, 2);
}

TEST_F(CyclicAttrTypeReplacerBranchingRecusionPruningTest, testPruneAnywhere1) {
  setBaseCase(std::nullopt);

  // Starting at 1. Cycle length is 3.
  EXPECT_EQ(replacer.replace(mlir::IntegerType::get(&ctx, 1)),
            getFunctionTypeTree(3));
  EXPECT_EQ(invokeCount, 3);
}

TEST_F(CyclicAttrTypeReplacerBranchingRecusionPruningTest, testPruneSpecific0) {
  setBaseCase(0);

  // Starting at 0. Cycle length is 3.
  EXPECT_EQ(replacer.replace(mlir::IntegerType::get(&ctx, 0)),
            getFunctionTypeTree(3));
  EXPECT_EQ(invokeCount, 3);
}

TEST_F(CyclicAttrTypeReplacerBranchingRecusionPruningTest, testPruneSpecific1) {
  setBaseCase(0);

  // Starting at 1. Cycle length is 5 (1 -> 2 -> 0 -> 1 -> 2 -> Prune).
  EXPECT_EQ(replacer.replace(mlir::IntegerType::get(&ctx, 1)),
            getFunctionTypeTree(5));
  EXPECT_EQ(invokeCount, 5);
}