1//===- TestDialect.cpp - MLIR Dialect for Testing -------------------------===//
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 "TestDialect.h"
10#include "TestOps.h"
11#include "TestTypes.h"
12#include "mlir/Bytecode/BytecodeImplementation.h"
13#include "mlir/Dialect/Arith/IR/Arith.h"
14#include "mlir/Dialect/Func/IR/FuncOps.h"
15#include "mlir/Dialect/Tensor/IR/Tensor.h"
16#include "mlir/IR/AsmState.h"
17#include "mlir/IR/BuiltinAttributes.h"
18#include "mlir/IR/BuiltinOps.h"
19#include "mlir/IR/Diagnostics.h"
20#include "mlir/IR/ExtensibleDialect.h"
21#include "mlir/IR/MLIRContext.h"
22#include "mlir/IR/ODSSupport.h"
23#include "mlir/IR/OperationSupport.h"
24#include "mlir/IR/PatternMatch.h"
25#include "mlir/IR/TypeUtilities.h"
26#include "mlir/IR/Verifier.h"
27#include "mlir/Interfaces/CallInterfaces.h"
28#include "mlir/Interfaces/FunctionImplementation.h"
29#include "mlir/Interfaces/InferIntRangeInterface.h"
30#include "mlir/Support/LLVM.h"
31#include "mlir/Transforms/FoldUtils.h"
32#include "mlir/Transforms/InliningUtils.h"
33#include "llvm/ADT/STLFunctionalExtras.h"
34#include "llvm/ADT/SmallString.h"
35#include "llvm/ADT/StringExtras.h"
36#include "llvm/ADT/StringSwitch.h"
37#include "llvm/Support/Base64.h"
38#include "llvm/Support/Casting.h"
39
40#include "mlir/Dialect/DLTI/DLTI.h"
41#include "mlir/Interfaces/FoldInterfaces.h"
42#include "mlir/Reducer/ReductionPatternInterface.h"
43#include "mlir/Transforms/InliningUtils.h"
44#include <cstdint>
45#include <memory>
46#include <numeric>
47#include <optional>
48
49// Include this before the using namespace lines below to test that we don't
50// have namespace dependencies.
51#include "TestOpsDialect.cpp.inc"
52
53using namespace mlir;
54using namespace test;
55
56//===----------------------------------------------------------------------===//
57// PropertiesWithCustomPrint
58//===----------------------------------------------------------------------===//
59
60LogicalResult
61test::setPropertiesFromAttribute(PropertiesWithCustomPrint &prop,
62 Attribute attr,
63 function_ref<InFlightDiagnostic()> emitError) {
64 DictionaryAttr dict = dyn_cast<DictionaryAttr>(attr);
65 if (!dict) {
66 emitError() << "expected DictionaryAttr to set TestProperties";
67 return failure();
68 }
69 auto label = dict.getAs<mlir::StringAttr>("label");
70 if (!label) {
71 emitError() << "expected StringAttr for key `label`";
72 return failure();
73 }
74 auto valueAttr = dict.getAs<IntegerAttr>("value");
75 if (!valueAttr) {
76 emitError() << "expected IntegerAttr for key `value`";
77 return failure();
78 }
79
80 prop.label = std::make_shared<std::string>(label.getValue());
81 prop.value = valueAttr.getValue().getSExtValue();
82 return success();
83}
84
85DictionaryAttr
86test::getPropertiesAsAttribute(MLIRContext *ctx,
87 const PropertiesWithCustomPrint &prop) {
88 SmallVector<NamedAttribute> attrs;
89 Builder b{ctx};
90 attrs.push_back(Elt: b.getNamedAttr(name: "label", val: b.getStringAttr(bytes: *prop.label)));
91 attrs.push_back(Elt: b.getNamedAttr(name: "value", val: b.getI32IntegerAttr(value: prop.value)));
92 return b.getDictionaryAttr(value: attrs);
93}
94
95llvm::hash_code test::computeHash(const PropertiesWithCustomPrint &prop) {
96 return llvm::hash_combine(args: prop.value, args: StringRef(*prop.label));
97}
98
99void test::customPrintProperties(OpAsmPrinter &p,
100 const PropertiesWithCustomPrint &prop) {
101 p << " ";
102 p.printKeywordOrString(keyword: *prop.label);
103 p << " is " << prop.value;
104}
105
106ParseResult test::customParseProperties(OpAsmParser &parser,
107 PropertiesWithCustomPrint &prop) {
108 std::string label;
109 if (parser.parseKeywordOrString(result: &label) || parser.parseKeyword(keyword: "is") ||
110 parser.parseInteger(result&: prop.value))
111 return failure();
112 prop.label = std::make_shared<std::string>(args: std::move(label));
113 return success();
114}
115
116//===----------------------------------------------------------------------===//
117// MyPropStruct
118//===----------------------------------------------------------------------===//
119
120Attribute MyPropStruct::asAttribute(MLIRContext *ctx) const {
121 return StringAttr::get(context: ctx, bytes: content);
122}
123
124LogicalResult
125MyPropStruct::setFromAttr(MyPropStruct &prop, Attribute attr,
126 function_ref<InFlightDiagnostic()> emitError) {
127 StringAttr strAttr = dyn_cast<StringAttr>(Val&: attr);
128 if (!strAttr) {
129 emitError() << "Expect StringAttr but got " << attr;
130 return failure();
131 }
132 prop.content = strAttr.getValue();
133 return success();
134}
135
136llvm::hash_code MyPropStruct::hash() const {
137 return hash_value(S: StringRef(content));
138}
139
140LogicalResult test::readFromMlirBytecode(DialectBytecodeReader &reader,
141 MyPropStruct &prop) {
142 StringRef str;
143 if (failed(Result: reader.readString(result&: str)))
144 return failure();
145 prop.content = str.str();
146 return success();
147}
148
149void test::writeToMlirBytecode(DialectBytecodeWriter &writer,
150 MyPropStruct &prop) {
151 writer.writeOwnedString(str: prop.content);
152}
153
154//===----------------------------------------------------------------------===//
155// VersionedProperties
156//===----------------------------------------------------------------------===//
157
158LogicalResult
159test::setPropertiesFromAttribute(VersionedProperties &prop, Attribute attr,
160 function_ref<InFlightDiagnostic()> emitError) {
161 DictionaryAttr dict = dyn_cast<DictionaryAttr>(Val&: attr);
162 if (!dict) {
163 emitError() << "expected DictionaryAttr to set VersionedProperties";
164 return failure();
165 }
166 auto value1Attr = dict.getAs<IntegerAttr>(name: "value1");
167 if (!value1Attr) {
168 emitError() << "expected IntegerAttr for key `value1`";
169 return failure();
170 }
171 auto value2Attr = dict.getAs<IntegerAttr>(name: "value2");
172 if (!value2Attr) {
173 emitError() << "expected IntegerAttr for key `value2`";
174 return failure();
175 }
176
177 prop.value1 = value1Attr.getValue().getSExtValue();
178 prop.value2 = value2Attr.getValue().getSExtValue();
179 return success();
180}
181
182DictionaryAttr test::getPropertiesAsAttribute(MLIRContext *ctx,
183 const VersionedProperties &prop) {
184 SmallVector<NamedAttribute> attrs;
185 Builder b{ctx};
186 attrs.push_back(Elt: b.getNamedAttr(name: "value1", val: b.getI32IntegerAttr(value: prop.value1)));
187 attrs.push_back(Elt: b.getNamedAttr(name: "value2", val: b.getI32IntegerAttr(value: prop.value2)));
188 return b.getDictionaryAttr(value: attrs);
189}
190
191llvm::hash_code test::computeHash(const VersionedProperties &prop) {
192 return llvm::hash_combine(args: prop.value1, args: prop.value2);
193}
194
195void test::customPrintProperties(OpAsmPrinter &p,
196 const VersionedProperties &prop) {
197 p << " " << prop.value1 << " | " << prop.value2;
198}
199
200ParseResult test::customParseProperties(OpAsmParser &parser,
201 VersionedProperties &prop) {
202 if (parser.parseInteger(result&: prop.value1) || parser.parseVerticalBar() ||
203 parser.parseInteger(result&: prop.value2))
204 return failure();
205 return success();
206}
207
208//===----------------------------------------------------------------------===//
209// Bytecode Support
210//===----------------------------------------------------------------------===//
211
212LogicalResult test::readFromMlirBytecode(DialectBytecodeReader &reader,
213 MutableArrayRef<int64_t> prop) {
214 uint64_t size;
215 if (failed(Result: reader.readVarInt(result&: size)))
216 return failure();
217 if (size != prop.size())
218 return reader.emitError(msg: "array size mismach when reading properties: ")
219 << size << " vs expected " << prop.size();
220 for (auto &elt : prop) {
221 uint64_t value;
222 if (failed(Result: reader.readVarInt(result&: value)))
223 return failure();
224 elt = value;
225 }
226 return success();
227}
228
229void test::writeToMlirBytecode(DialectBytecodeWriter &writer,
230 ArrayRef<int64_t> prop) {
231 writer.writeVarInt(value: prop.size());
232 for (auto elt : prop)
233 writer.writeVarInt(value: elt);
234}
235
236//===----------------------------------------------------------------------===//
237// Dynamic operations
238//===----------------------------------------------------------------------===//
239
240static std::unique_ptr<DynamicOpDefinition>
241getDynamicGenericOp(TestDialect *dialect) {
242 return DynamicOpDefinition::get(
243 "dynamic_generic", dialect, [](Operation *op) { return success(); },
244 [](Operation *op) { return success(); });
245}
246
247static std::unique_ptr<DynamicOpDefinition>
248getDynamicTerminatorOp(TestDialect *dialect) {
249 auto def = DynamicOpDefinition::get(
250 "dynamic_terminator", dialect, [](Operation *op) { return success(); },
251 [](Operation *op) { return success(); });
252 def->addTrait(std::make_unique<DynamicOpTraits::IsTerminator>());
253 return def;
254}
255
256static std::unique_ptr<DynamicOpDefinition>
257getDynamicNoTerminatorOp(TestDialect *dialect) {
258 auto def = DynamicOpDefinition::get(
259 "dynamic_noterminator", dialect, [](Operation *op) { return success(); },
260 [](Operation *op) { return success(); });
261 def->addTrait(std::make_unique<DynamicOpTraits::NoTerminator>());
262 return def;
263}
264
265static std::unique_ptr<DynamicOpDefinition>
266getDynamicOneOperandTwoResultsOp(TestDialect *dialect) {
267 return DynamicOpDefinition::get(
268 "dynamic_one_operand_two_results", dialect,
269 [](Operation *op) {
270 if (op->getNumOperands() != 1) {
271 op->emitOpError()
272 << "expected 1 operand, but had " << op->getNumOperands();
273 return failure();
274 }
275 if (op->getNumResults() != 2) {
276 op->emitOpError()
277 << "expected 2 results, but had " << op->getNumResults();
278 return failure();
279 }
280 return success();
281 },
282 [](Operation *op) { return success(); });
283}
284
285static std::unique_ptr<DynamicOpDefinition>
286getDynamicCustomParserPrinterOp(TestDialect *dialect) {
287 auto verifier = [](Operation *op) {
288 if (op->getNumOperands() == 0 && op->getNumResults() == 0)
289 return success();
290 op->emitError() << "operation should have no operands and no results";
291 return failure();
292 };
293 auto regionVerifier = [](Operation *op) { return success(); };
294
295 auto parser = [](OpAsmParser &parser, OperationState &state) {
296 return parser.parseKeyword(keyword: "custom_keyword");
297 };
298
299 auto printer = [](Operation *op, OpAsmPrinter &printer, llvm::StringRef) {
300 printer << op->getName() << " custom_keyword";
301 };
302
303 return DynamicOpDefinition::get("dynamic_custom_parser_printer", dialect,
304 verifier, regionVerifier, parser, printer);
305}
306
307//===----------------------------------------------------------------------===//
308// TestDialect
309//===----------------------------------------------------------------------===//
310
311void test::registerTestDialect(DialectRegistry &registry) {
312 registry.insert<TestDialect>();
313}
314
315void test::testSideEffectOpGetEffect(
316 Operation *op,
317 SmallVectorImpl<SideEffects::EffectInstance<TestEffects::Effect>>
318 &effects) {
319 auto effectsAttr =
320 op->getDiscardableAttrOfType<AffineMapAttr>(name: "effect_parameter");
321 if (!effectsAttr)
322 return;
323
324 effects.emplace_back(Args: TestEffects::Concrete::get(), Args&: effectsAttr);
325}
326
327// This is the implementation of a dialect fallback for `TestEffectOpInterface`.
328struct TestOpEffectInterfaceFallback
329 : public TestEffectOpInterface::FallbackModel<
330 TestOpEffectInterfaceFallback> {
331 static bool classof(Operation *op) {
332 bool isSupportedOp =
333 op->getName().getStringRef() == "test.unregistered_side_effect_op";
334 assert(isSupportedOp && "Unexpected dispatch");
335 return isSupportedOp;
336 }
337
338 void
339 getEffects(Operation *op,
340 SmallVectorImpl<SideEffects::EffectInstance<TestEffects::Effect>>
341 &effects) const {
342 testSideEffectOpGetEffect(op, effects);
343 }
344};
345
346void TestDialect::initialize() {
347 registerAttributes();
348 registerTypes();
349 registerOpsSyntax();
350 addOperations<ManualCppOpWithFold>();
351 registerTestDialectOperations(this);
352 registerDynamicOp(getDynamicGenericOp(this));
353 registerDynamicOp(getDynamicTerminatorOp(this));
354 registerDynamicOp(getDynamicNoTerminatorOp(this));
355 registerDynamicOp(getDynamicOneOperandTwoResultsOp(this));
356 registerDynamicOp(getDynamicCustomParserPrinterOp(this));
357 registerInterfaces();
358 allowUnknownOperations();
359
360 // Instantiate our fallback op interface that we'll use on specific
361 // unregistered op.
362 fallbackEffectOpInterfaces = new TestOpEffectInterfaceFallback;
363}
364
365TestDialect::~TestDialect() {
366 delete static_cast<TestOpEffectInterfaceFallback *>(
367 fallbackEffectOpInterfaces);
368}
369
370Operation *TestDialect::materializeConstant(OpBuilder &builder, Attribute value,
371 Type type, Location loc) {
372 return TestOpConstant::create(builder, loc, type, value);
373}
374
375void *TestDialect::getRegisteredInterfaceForOp(TypeID typeID,
376 OperationName opName) {
377 if (opName.getIdentifier() == "test.unregistered_side_effect_op" &&
378 typeID == TypeID::get<TestEffectOpInterface>())
379 return fallbackEffectOpInterfaces;
380 return nullptr;
381}
382
383LogicalResult TestDialect::verifyOperationAttribute(Operation *op,
384 NamedAttribute namedAttr) {
385 if (namedAttr.getName() == "test.invalid_attr")
386 return op->emitError() << "invalid to use 'test.invalid_attr'";
387 return success();
388}
389
390LogicalResult TestDialect::verifyRegionArgAttribute(Operation *op,
391 unsigned regionIndex,
392 unsigned argIndex,
393 NamedAttribute namedAttr) {
394 if (namedAttr.getName() == "test.invalid_attr")
395 return op->emitError() << "invalid to use 'test.invalid_attr'";
396 return success();
397}
398
399LogicalResult
400TestDialect::verifyRegionResultAttribute(Operation *op, unsigned regionIndex,
401 unsigned resultIndex,
402 NamedAttribute namedAttr) {
403 if (namedAttr.getName() == "test.invalid_attr")
404 return op->emitError() << "invalid to use 'test.invalid_attr'";
405 return success();
406}
407
408std::optional<Dialect::ParseOpHook>
409TestDialect::getParseOperationHook(StringRef opName) const {
410 if (opName == "test.dialect_custom_printer") {
411 return ParseOpHook{[](OpAsmParser &parser, OperationState &state) {
412 return parser.parseKeyword("custom_format");
413 }};
414 }
415 if (opName == "test.dialect_custom_format_fallback") {
416 return ParseOpHook{[](OpAsmParser &parser, OperationState &state) {
417 return parser.parseKeyword("custom_format_fallback");
418 }};
419 }
420 if (opName == "test.dialect_custom_printer.with.dot") {
421 return ParseOpHook{[](OpAsmParser &parser, OperationState &state) {
422 return ParseResult::success();
423 }};
424 }
425 return std::nullopt;
426}
427
428llvm::unique_function<void(Operation *, OpAsmPrinter &)>
429TestDialect::getOperationPrinter(Operation *op) const {
430 StringRef opName = op->getName().getStringRef();
431 if (opName == "test.dialect_custom_printer") {
432 return [](Operation *op, OpAsmPrinter &printer) {
433 printer.getStream() << " custom_format";
434 };
435 }
436 if (opName == "test.dialect_custom_format_fallback") {
437 return [](Operation *op, OpAsmPrinter &printer) {
438 printer.getStream() << " custom_format_fallback";
439 };
440 }
441 return {};
442}
443
444static LogicalResult
445dialectCanonicalizationPattern(TestDialectCanonicalizerOp op,
446 PatternRewriter &rewriter) {
447 rewriter.replaceOpWithNewOp<arith::ConstantOp>(
448 op, rewriter.getI32IntegerAttr(value: 42));
449 return success();
450}
451
452void TestDialect::getCanonicalizationPatterns(
453 RewritePatternSet &results) const {
454 results.add(&dialectCanonicalizationPattern);
455}
456
457//===----------------------------------------------------------------------===//
458// TestCallWithSegmentsOp
459//===----------------------------------------------------------------------===//
460// The op `test.call_with_segments` models a call-like operation whose operands
461// are divided into 3 variadic segments: `prefix`, `args`, and `suffix`.
462// Only the middle segment represents the actual call arguments. The op uses
463// the AttrSizedOperandSegments trait, so we can derive segment boundaries from
464// the generated `operandSegmentSizes` attribute. We provide custom helpers to
465// expose the logical call arguments as both a read-only range and a mutable
466// range bound to the proper segment so that insertion/erasure updates the
467// attribute automatically.
468
469// Segment layout indices in the DenseI32ArrayAttr: [prefix, args, suffix].
470static constexpr unsigned kTestCallWithSegmentsArgsSegIndex = 1;
471
472Operation::operand_range CallWithSegmentsOp::getArgOperands() {
473 // Leverage generated getters for segment sizes: slice between prefix and
474 // suffix using current operand list.
475 return getOperation()->getOperands().slice(getPrefix().size(),
476 getArgs().size());
477}
478
479MutableOperandRange CallWithSegmentsOp::getArgOperandsMutable() {
480 Operation *op = getOperation();
481
482 // Obtain the canonical segment size attribute name for this op.
483 auto segName =
484 CallWithSegmentsOp::getOperandSegmentSizesAttrName(op->getName());
485 auto sizesAttr = dyn_cast_or_null<DenseI32ArrayAttr>(
486 op->getInherentAttr(segName).value_or(Attribute{}));
487 assert(sizesAttr && "missing operandSegmentSizes attribute on op");
488
489 // Compute the start and length of the args segment from the prefix size and
490 // args size stored in the attribute.
491 auto sizes = sizesAttr.asArrayRef();
492 unsigned start = static_cast<unsigned>(sizes[0]); // prefix size
493 unsigned len = static_cast<unsigned>(sizes[1]); // args size
494
495 NamedAttribute segNamed(segName, sizesAttr);
496 MutableOperandRange::OperandSegment binding{kTestCallWithSegmentsArgsSegIndex,
497 segNamed};
498
499 return MutableOperandRange(op, start, len, {binding});
500}
501