1//===- TestFunc.cpp - Pass to test helpers on function utilities ----------===//
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/BuiltinOps.h"
10#include "mlir/Interfaces/FunctionInterfaces.h"
11#include "mlir/Pass/Pass.h"
12
13using namespace mlir;
14
15namespace {
16/// This is a test pass for verifying FunctionOpInterface's insertArgument
17/// method.
18struct TestFuncInsertArg
19 : public PassWrapper<TestFuncInsertArg, OperationPass<ModuleOp>> {
20 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestFuncInsertArg)
21
22 StringRef getArgument() const final { return "test-func-insert-arg"; }
23 StringRef getDescription() const final { return "Test inserting func args."; }
24 void runOnOperation() override {
25 auto module = getOperation();
26
27 UnknownLoc unknownLoc = UnknownLoc::get(context: module.getContext());
28 for (auto func : module.getOps<FunctionOpInterface>()) {
29 auto inserts =
30 func->getDiscardableAttrOfType<ArrayAttr>(name: "test.insert_args");
31 if (!inserts || inserts.empty())
32 continue;
33 SmallVector<unsigned, 4> indicesToInsert;
34 SmallVector<Type, 4> typesToInsert;
35 SmallVector<DictionaryAttr, 4> attrsToInsert;
36 SmallVector<Location, 4> locsToInsert;
37 for (auto insert : inserts.getAsRange<ArrayAttr>()) {
38 indicesToInsert.push_back(
39 Elt: cast<IntegerAttr>(Val: insert[0]).getValue().getZExtValue());
40 typesToInsert.push_back(Elt: cast<TypeAttr>(Val: insert[1]).getValue());
41 attrsToInsert.push_back(Elt: insert.size() > 2
42 ? cast<DictionaryAttr>(Val: insert[2])
43 : DictionaryAttr::get(context: &getContext()));
44 locsToInsert.push_back(Elt: insert.size() > 3
45 ? Location(cast<LocationAttr>(Val: insert[3]))
46 : unknownLoc);
47 }
48 func->removeDiscardableAttr(name: "test.insert_args");
49 if (succeeded(Result: func.insertArguments(argIndices: indicesToInsert, argTypes: typesToInsert,
50 argAttrs: attrsToInsert, argLocs: locsToInsert)))
51 continue;
52
53 emitError(loc: func->getLoc()) << "failed to insert arguments";
54 return signalPassFailure();
55 }
56 }
57};
58
59/// This is a test pass for verifying FunctionOpInterface's insertResult method.
60struct TestFuncInsertResult
61 : public PassWrapper<TestFuncInsertResult, OperationPass<ModuleOp>> {
62 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestFuncInsertResult)
63
64 StringRef getArgument() const final { return "test-func-insert-result"; }
65 StringRef getDescription() const final {
66 return "Test inserting func results.";
67 }
68 void runOnOperation() override {
69 auto module = getOperation();
70
71 for (auto func : module.getOps<FunctionOpInterface>()) {
72 auto inserts =
73 func->getDiscardableAttrOfType<ArrayAttr>(name: "test.insert_results");
74 if (!inserts || inserts.empty())
75 continue;
76 SmallVector<unsigned, 4> indicesToInsert;
77 SmallVector<Type, 4> typesToInsert;
78 SmallVector<DictionaryAttr, 4> attrsToInsert;
79 for (auto insert : inserts.getAsRange<ArrayAttr>()) {
80 indicesToInsert.push_back(
81 Elt: cast<IntegerAttr>(Val: insert[0]).getValue().getZExtValue());
82 typesToInsert.push_back(Elt: cast<TypeAttr>(Val: insert[1]).getValue());
83 attrsToInsert.push_back(Elt: insert.size() > 2
84 ? cast<DictionaryAttr>(Val: insert[2])
85 : DictionaryAttr::get(context: &getContext()));
86 }
87 func->removeDiscardableAttr(name: "test.insert_results");
88 if (succeeded(Result: func.insertResults(resultIndices: indicesToInsert, resultTypes: typesToInsert,
89 resultAttrs: attrsToInsert)))
90 continue;
91
92 emitError(loc: func->getLoc()) << "failed to insert results";
93 return signalPassFailure();
94 }
95 }
96};
97
98/// This is a test pass for verifying FunctionOpInterface's eraseArgument
99/// method.
100struct TestFuncEraseArg
101 : public PassWrapper<TestFuncEraseArg, OperationPass<ModuleOp>> {
102 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestFuncEraseArg)
103
104 StringRef getArgument() const final { return "test-func-erase-arg"; }
105 StringRef getDescription() const final { return "Test erasing func args."; }
106 void runOnOperation() override {
107 auto module = getOperation();
108
109 for (auto func : module.getOps<FunctionOpInterface>()) {
110 BitVector indicesToErase(func.getNumArguments());
111 bool hasUsedArg = false;
112 for (auto argIndex : llvm::seq<int>(Begin: 0, End: func.getNumArguments())) {
113 if (!func.getArgAttr(index: argIndex, name: "test.erase_this_arg"))
114 continue;
115 indicesToErase.set(argIndex);
116 if (!func.isExternal() && !func.getArgument(idx: argIndex).use_empty()) {
117 emitError(loc: func->getLoc()) << "cannot erase argument " << argIndex
118 << " which still has uses";
119 hasUsedArg = true;
120 }
121 }
122 if (hasUsedArg)
123 return signalPassFailure();
124 if (succeeded(Result: func.eraseArguments(argIndices: indicesToErase)))
125 continue;
126 emitError(loc: func->getLoc()) << "failed to erase arguments";
127 return signalPassFailure();
128 }
129 }
130};
131
132/// This is a test pass for verifying FunctionOpInterface's eraseResult method.
133struct TestFuncEraseResult
134 : public PassWrapper<TestFuncEraseResult, OperationPass<ModuleOp>> {
135 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestFuncEraseResult)
136
137 StringRef getArgument() const final { return "test-func-erase-result"; }
138 StringRef getDescription() const final {
139 return "Test erasing func results.";
140 }
141 void runOnOperation() override {
142 auto module = getOperation();
143
144 for (auto func : module.getOps<FunctionOpInterface>()) {
145 BitVector indicesToErase(func.getNumResults());
146 for (auto resultIndex : llvm::seq<int>(Begin: 0, End: func.getNumResults()))
147 if (func.getResultAttr(index: resultIndex, name: "test.erase_this_result"))
148 indicesToErase.set(resultIndex);
149 if (succeeded(Result: func.eraseResults(resultIndices: indicesToErase)))
150 continue;
151 emitError(loc: func->getLoc()) << "failed to erase results";
152 return signalPassFailure();
153 }
154 }
155};
156
157/// This is a test pass for verifying FunctionOpInterface's setType method.
158struct TestFuncSetType
159 : public PassWrapper<TestFuncSetType, OperationPass<ModuleOp>> {
160 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestFuncSetType)
161
162 StringRef getArgument() const final { return "test-func-set-type"; }
163 StringRef getDescription() const final {
164 return "Test FunctionOpInterface::setType.";
165 }
166 void runOnOperation() override {
167 auto module = getOperation();
168 SymbolTable symbolTable(module);
169
170 for (auto func : module.getOps<FunctionOpInterface>()) {
171 auto sym = func->getDiscardableAttrOfType<FlatSymbolRefAttr>(
172 name: "test.set_type_from");
173 if (!sym)
174 continue;
175 func.setType(symbolTable.lookup<FunctionOpInterface>(name: sym.getValue())
176 .getFunctionType());
177 }
178 }
179};
180} // namespace
181
182namespace mlir {
183void registerTestFunc() {
184 PassRegistration<TestFuncInsertArg>();
185
186 PassRegistration<TestFuncInsertResult>();
187
188 PassRegistration<TestFuncEraseArg>();
189
190 PassRegistration<TestFuncEraseResult>();
191
192 PassRegistration<TestFuncSetType>();
193}
194} // namespace mlir
195