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 "TestAttributes.h"
11 #include "TestInterfaces.h"
12 #include "TestTypes.h"
13 #include "mlir/Dialect/DLTI/DLTI.h"
14 #include "mlir/Dialect/MemRef/IR/MemRef.h"
15 #include "mlir/Dialect/StandardOps/IR/Ops.h"
16 #include "mlir/IR/BuiltinOps.h"
17 #include "mlir/IR/DialectImplementation.h"
18 #include "mlir/IR/PatternMatch.h"
19 #include "mlir/IR/TypeUtilities.h"
20 #include "mlir/Reducer/ReductionPatternInterface.h"
21 #include "mlir/Transforms/FoldUtils.h"
22 #include "mlir/Transforms/InliningUtils.h"
23 #include "llvm/ADT/StringSwitch.h"
24 
25 using namespace mlir;
26 using namespace mlir::test;
27 
28 void mlir::test::registerTestDialect(DialectRegistry &registry) {
29   registry.insert<TestDialect>();
30 }
31 
32 //===----------------------------------------------------------------------===//
33 // TestDialect Interfaces
34 //===----------------------------------------------------------------------===//
35 
36 namespace {
37 
38 /// Testing the correctness of some traits.
39 static_assert(
40     llvm::is_detected<OpTrait::has_implicit_terminator_t,
41                       SingleBlockImplicitTerminatorOp>::value,
42     "has_implicit_terminator_t does not match SingleBlockImplicitTerminatorOp");
43 static_assert(OpTrait::hasSingleBlockImplicitTerminator<
44                   SingleBlockImplicitTerminatorOp>::value,
45               "hasSingleBlockImplicitTerminator does not match "
46               "SingleBlockImplicitTerminatorOp");
47 
48 // Test support for interacting with the AsmPrinter.
49 struct TestOpAsmInterface : public OpAsmDialectInterface {
50   using OpAsmDialectInterface::OpAsmDialectInterface;
51 
52   LogicalResult getAlias(Attribute attr, raw_ostream &os) const final {
53     StringAttr strAttr = attr.dyn_cast<StringAttr>();
54     if (!strAttr)
55       return failure();
56 
57     // Check the contents of the string attribute to see what the test alias
58     // should be named.
59     Optional<StringRef> aliasName =
60         StringSwitch<Optional<StringRef>>(strAttr.getValue())
61             .Case("alias_test:dot_in_name", StringRef("test.alias"))
62             .Case("alias_test:trailing_digit", StringRef("test_alias0"))
63             .Case("alias_test:prefixed_digit", StringRef("0_test_alias"))
64             .Case("alias_test:sanitize_conflict_a",
65                   StringRef("test_alias_conflict0"))
66             .Case("alias_test:sanitize_conflict_b",
67                   StringRef("test_alias_conflict0_"))
68             .Default(llvm::None);
69     if (!aliasName)
70       return failure();
71 
72     os << *aliasName;
73     return success();
74   }
75 
76   void getAsmResultNames(Operation *op,
77                          OpAsmSetValueNameFn setNameFn) const final {
78     if (auto asmOp = dyn_cast<AsmDialectInterfaceOp>(op))
79       setNameFn(asmOp, "result");
80   }
81 
82   void getAsmBlockArgumentNames(Block *block,
83                                 OpAsmSetValueNameFn setNameFn) const final {
84     auto op = block->getParentOp();
85     auto arrayAttr = op->getAttrOfType<ArrayAttr>("arg_names");
86     if (!arrayAttr)
87       return;
88     auto args = block->getArguments();
89     auto e = std::min(arrayAttr.size(), args.size());
90     for (unsigned i = 0; i < e; ++i) {
91       if (auto strAttr = arrayAttr[i].dyn_cast<StringAttr>())
92         setNameFn(args[i], strAttr.getValue());
93     }
94   }
95 };
96 
97 struct TestDialectFoldInterface : public DialectFoldInterface {
98   using DialectFoldInterface::DialectFoldInterface;
99 
100   /// Registered hook to check if the given region, which is attached to an
101   /// operation that is *not* isolated from above, should be used when
102   /// materializing constants.
103   bool shouldMaterializeInto(Region *region) const final {
104     // If this is a one region operation, then insert into it.
105     return isa<OneRegionOp>(region->getParentOp());
106   }
107 };
108 
109 /// This class defines the interface for handling inlining with standard
110 /// operations.
111 struct TestInlinerInterface : public DialectInlinerInterface {
112   using DialectInlinerInterface::DialectInlinerInterface;
113 
114   //===--------------------------------------------------------------------===//
115   // Analysis Hooks
116   //===--------------------------------------------------------------------===//
117 
118   bool isLegalToInline(Operation *call, Operation *callable,
119                        bool wouldBeCloned) const final {
120     // Don't allow inlining calls that are marked `noinline`.
121     return !call->hasAttr("noinline");
122   }
123   bool isLegalToInline(Region *, Region *, bool,
124                        BlockAndValueMapping &) const final {
125     // Inlining into test dialect regions is legal.
126     return true;
127   }
128   bool isLegalToInline(Operation *, Region *, bool,
129                        BlockAndValueMapping &) const final {
130     return true;
131   }
132 
133   bool shouldAnalyzeRecursively(Operation *op) const final {
134     // Analyze recursively if this is not a functional region operation, it
135     // froms a separate functional scope.
136     return !isa<FunctionalRegionOp>(op);
137   }
138 
139   //===--------------------------------------------------------------------===//
140   // Transformation Hooks
141   //===--------------------------------------------------------------------===//
142 
143   /// Handle the given inlined terminator by replacing it with a new operation
144   /// as necessary.
145   void handleTerminator(Operation *op,
146                         ArrayRef<Value> valuesToRepl) const final {
147     // Only handle "test.return" here.
148     auto returnOp = dyn_cast<TestReturnOp>(op);
149     if (!returnOp)
150       return;
151 
152     // Replace the values directly with the return operands.
153     assert(returnOp.getNumOperands() == valuesToRepl.size());
154     for (const auto &it : llvm::enumerate(returnOp.getOperands()))
155       valuesToRepl[it.index()].replaceAllUsesWith(it.value());
156   }
157 
158   /// Attempt to materialize a conversion for a type mismatch between a call
159   /// from this dialect, and a callable region. This method should generate an
160   /// operation that takes 'input' as the only operand, and produces a single
161   /// result of 'resultType'. If a conversion can not be generated, nullptr
162   /// should be returned.
163   Operation *materializeCallConversion(OpBuilder &builder, Value input,
164                                        Type resultType,
165                                        Location conversionLoc) const final {
166     // Only allow conversion for i16/i32 types.
167     if (!(resultType.isSignlessInteger(16) ||
168           resultType.isSignlessInteger(32)) ||
169         !(input.getType().isSignlessInteger(16) ||
170           input.getType().isSignlessInteger(32)))
171       return nullptr;
172     return builder.create<TestCastOp>(conversionLoc, resultType, input);
173   }
174 };
175 
176 struct TestReductionPatternInterface : public DialectReductionPatternInterface {
177 public:
178   TestReductionPatternInterface(Dialect *dialect)
179       : DialectReductionPatternInterface(dialect) {}
180 
181   virtual void
182   populateReductionPatterns(RewritePatternSet &patterns) const final {
183     populateTestReductionPatterns(patterns);
184   }
185 };
186 
187 } // end anonymous namespace
188 
189 //===----------------------------------------------------------------------===//
190 // TestDialect
191 //===----------------------------------------------------------------------===//
192 
193 static void testSideEffectOpGetEffect(
194     Operation *op,
195     SmallVectorImpl<SideEffects::EffectInstance<TestEffects::Effect>> &effects);
196 
197 // This is the implementation of a dialect fallback for `TestEffectOpInterface`.
198 struct TestOpEffectInterfaceFallback
199     : public TestEffectOpInterface::FallbackModel<
200           TestOpEffectInterfaceFallback> {
201   static bool classof(Operation *op) {
202     bool isSupportedOp =
203         op->getName().getStringRef() == "test.unregistered_side_effect_op";
204     assert(isSupportedOp && "Unexpected dispatch");
205     return isSupportedOp;
206   }
207 
208   void
209   getEffects(Operation *op,
210              SmallVectorImpl<SideEffects::EffectInstance<TestEffects::Effect>>
211                  &effects) const {
212     testSideEffectOpGetEffect(op, effects);
213   }
214 };
215 
216 void TestDialect::initialize() {
217   registerAttributes();
218   registerTypes();
219   addOperations<
220 #define GET_OP_LIST
221 #include "TestOps.cpp.inc"
222       >();
223   addInterfaces<TestOpAsmInterface, TestDialectFoldInterface,
224                 TestInlinerInterface, TestReductionPatternInterface>();
225   allowUnknownOperations();
226 
227   // Instantiate our fallback op interface that we'll use on specific
228   // unregistered op.
229   fallbackEffectOpInterfaces = new TestOpEffectInterfaceFallback;
230 }
231 TestDialect::~TestDialect() {
232   delete static_cast<TestOpEffectInterfaceFallback *>(
233       fallbackEffectOpInterfaces);
234 }
235 
236 Operation *TestDialect::materializeConstant(OpBuilder &builder, Attribute value,
237                                             Type type, Location loc) {
238   return builder.create<TestOpConstant>(loc, type, value);
239 }
240 
241 void *TestDialect::getRegisteredInterfaceForOp(TypeID typeID,
242                                                OperationName opName) {
243   if (opName.getIdentifier() == "test.unregistered_side_effect_op" &&
244       typeID == TypeID::get<TestEffectOpInterface>())
245     return fallbackEffectOpInterfaces;
246   return nullptr;
247 }
248 
249 LogicalResult TestDialect::verifyOperationAttribute(Operation *op,
250                                                     NamedAttribute namedAttr) {
251   if (namedAttr.first == "test.invalid_attr")
252     return op->emitError() << "invalid to use 'test.invalid_attr'";
253   return success();
254 }
255 
256 LogicalResult TestDialect::verifyRegionArgAttribute(Operation *op,
257                                                     unsigned regionIndex,
258                                                     unsigned argIndex,
259                                                     NamedAttribute namedAttr) {
260   if (namedAttr.first == "test.invalid_attr")
261     return op->emitError() << "invalid to use 'test.invalid_attr'";
262   return success();
263 }
264 
265 LogicalResult
266 TestDialect::verifyRegionResultAttribute(Operation *op, unsigned regionIndex,
267                                          unsigned resultIndex,
268                                          NamedAttribute namedAttr) {
269   if (namedAttr.first == "test.invalid_attr")
270     return op->emitError() << "invalid to use 'test.invalid_attr'";
271   return success();
272 }
273 
274 Optional<Dialect::ParseOpHook>
275 TestDialect::getParseOperationHook(StringRef opName) const {
276   if (opName == "test.dialect_custom_printer") {
277     return ParseOpHook{[](OpAsmParser &parser, OperationState &state) {
278       return parser.parseKeyword("custom_format");
279     }};
280   }
281   return None;
282 }
283 
284 LogicalResult TestDialect::printOperation(Operation *op,
285                                           OpAsmPrinter &printer) const {
286   StringRef opName = op->getName().getStringRef();
287   if (opName == "test.dialect_custom_printer") {
288     printer.getStream() << opName << " custom_format";
289     return success();
290   }
291   return failure();
292 }
293 
294 //===----------------------------------------------------------------------===//
295 // TestBranchOp
296 //===----------------------------------------------------------------------===//
297 
298 Optional<MutableOperandRange>
299 TestBranchOp::getMutableSuccessorOperands(unsigned index) {
300   assert(index == 0 && "invalid successor index");
301   return targetOperandsMutable();
302 }
303 
304 //===----------------------------------------------------------------------===//
305 // TestDialectCanonicalizerOp
306 //===----------------------------------------------------------------------===//
307 
308 static LogicalResult
309 dialectCanonicalizationPattern(TestDialectCanonicalizerOp op,
310                                PatternRewriter &rewriter) {
311   rewriter.replaceOpWithNewOp<ConstantOp>(op, rewriter.getI32Type(),
312                                           rewriter.getI32IntegerAttr(42));
313   return success();
314 }
315 
316 void TestDialect::getCanonicalizationPatterns(
317     RewritePatternSet &results) const {
318   results.add(&dialectCanonicalizationPattern);
319 }
320 
321 //===----------------------------------------------------------------------===//
322 // TestFoldToCallOp
323 //===----------------------------------------------------------------------===//
324 
325 namespace {
326 struct FoldToCallOpPattern : public OpRewritePattern<FoldToCallOp> {
327   using OpRewritePattern<FoldToCallOp>::OpRewritePattern;
328 
329   LogicalResult matchAndRewrite(FoldToCallOp op,
330                                 PatternRewriter &rewriter) const override {
331     rewriter.replaceOpWithNewOp<CallOp>(op, TypeRange(), op.calleeAttr(),
332                                         ValueRange());
333     return success();
334   }
335 };
336 } // end anonymous namespace
337 
338 void FoldToCallOp::getCanonicalizationPatterns(RewritePatternSet &results,
339                                                MLIRContext *context) {
340   results.add<FoldToCallOpPattern>(context);
341 }
342 
343 //===----------------------------------------------------------------------===//
344 // Test Format* operations
345 //===----------------------------------------------------------------------===//
346 
347 //===----------------------------------------------------------------------===//
348 // Parsing
349 
350 static ParseResult parseCustomDirectiveOperands(
351     OpAsmParser &parser, OpAsmParser::OperandType &operand,
352     Optional<OpAsmParser::OperandType> &optOperand,
353     SmallVectorImpl<OpAsmParser::OperandType> &varOperands) {
354   if (parser.parseOperand(operand))
355     return failure();
356   if (succeeded(parser.parseOptionalComma())) {
357     optOperand.emplace();
358     if (parser.parseOperand(*optOperand))
359       return failure();
360   }
361   if (parser.parseArrow() || parser.parseLParen() ||
362       parser.parseOperandList(varOperands) || parser.parseRParen())
363     return failure();
364   return success();
365 }
366 static ParseResult
367 parseCustomDirectiveResults(OpAsmParser &parser, Type &operandType,
368                             Type &optOperandType,
369                             SmallVectorImpl<Type> &varOperandTypes) {
370   if (parser.parseColon())
371     return failure();
372 
373   if (parser.parseType(operandType))
374     return failure();
375   if (succeeded(parser.parseOptionalComma())) {
376     if (parser.parseType(optOperandType))
377       return failure();
378   }
379   if (parser.parseArrow() || parser.parseLParen() ||
380       parser.parseTypeList(varOperandTypes) || parser.parseRParen())
381     return failure();
382   return success();
383 }
384 static ParseResult
385 parseCustomDirectiveWithTypeRefs(OpAsmParser &parser, Type operandType,
386                                  Type optOperandType,
387                                  const SmallVectorImpl<Type> &varOperandTypes) {
388   if (parser.parseKeyword("type_refs_capture"))
389     return failure();
390 
391   Type operandType2, optOperandType2;
392   SmallVector<Type, 1> varOperandTypes2;
393   if (parseCustomDirectiveResults(parser, operandType2, optOperandType2,
394                                   varOperandTypes2))
395     return failure();
396 
397   if (operandType != operandType2 || optOperandType != optOperandType2 ||
398       varOperandTypes != varOperandTypes2)
399     return failure();
400 
401   return success();
402 }
403 static ParseResult parseCustomDirectiveOperandsAndTypes(
404     OpAsmParser &parser, OpAsmParser::OperandType &operand,
405     Optional<OpAsmParser::OperandType> &optOperand,
406     SmallVectorImpl<OpAsmParser::OperandType> &varOperands, Type &operandType,
407     Type &optOperandType, SmallVectorImpl<Type> &varOperandTypes) {
408   if (parseCustomDirectiveOperands(parser, operand, optOperand, varOperands) ||
409       parseCustomDirectiveResults(parser, operandType, optOperandType,
410                                   varOperandTypes))
411     return failure();
412   return success();
413 }
414 static ParseResult parseCustomDirectiveRegions(
415     OpAsmParser &parser, Region &region,
416     SmallVectorImpl<std::unique_ptr<Region>> &varRegions) {
417   if (parser.parseRegion(region))
418     return failure();
419   if (failed(parser.parseOptionalComma()))
420     return success();
421   std::unique_ptr<Region> varRegion = std::make_unique<Region>();
422   if (parser.parseRegion(*varRegion))
423     return failure();
424   varRegions.emplace_back(std::move(varRegion));
425   return success();
426 }
427 static ParseResult
428 parseCustomDirectiveSuccessors(OpAsmParser &parser, Block *&successor,
429                                SmallVectorImpl<Block *> &varSuccessors) {
430   if (parser.parseSuccessor(successor))
431     return failure();
432   if (failed(parser.parseOptionalComma()))
433     return success();
434   Block *varSuccessor;
435   if (parser.parseSuccessor(varSuccessor))
436     return failure();
437   varSuccessors.append(2, varSuccessor);
438   return success();
439 }
440 static ParseResult parseCustomDirectiveAttributes(OpAsmParser &parser,
441                                                   IntegerAttr &attr,
442                                                   IntegerAttr &optAttr) {
443   if (parser.parseAttribute(attr))
444     return failure();
445   if (succeeded(parser.parseOptionalComma())) {
446     if (parser.parseAttribute(optAttr))
447       return failure();
448   }
449   return success();
450 }
451 
452 static ParseResult parseCustomDirectiveAttrDict(OpAsmParser &parser,
453                                                 NamedAttrList &attrs) {
454   return parser.parseOptionalAttrDict(attrs);
455 }
456 static ParseResult parseCustomDirectiveOptionalOperandRef(
457     OpAsmParser &parser, Optional<OpAsmParser::OperandType> &optOperand) {
458   int64_t operandCount = 0;
459   if (parser.parseInteger(operandCount))
460     return failure();
461   bool expectedOptionalOperand = operandCount == 0;
462   return success(expectedOptionalOperand != optOperand.hasValue());
463 }
464 
465 //===----------------------------------------------------------------------===//
466 // Printing
467 
468 static void printCustomDirectiveOperands(OpAsmPrinter &printer, Operation *,
469                                          Value operand, Value optOperand,
470                                          OperandRange varOperands) {
471   printer << operand;
472   if (optOperand)
473     printer << ", " << optOperand;
474   printer << " -> (" << varOperands << ")";
475 }
476 static void printCustomDirectiveResults(OpAsmPrinter &printer, Operation *,
477                                         Type operandType, Type optOperandType,
478                                         TypeRange varOperandTypes) {
479   printer << " : " << operandType;
480   if (optOperandType)
481     printer << ", " << optOperandType;
482   printer << " -> (" << varOperandTypes << ")";
483 }
484 static void printCustomDirectiveWithTypeRefs(OpAsmPrinter &printer,
485                                              Operation *op, Type operandType,
486                                              Type optOperandType,
487                                              TypeRange varOperandTypes) {
488   printer << " type_refs_capture ";
489   printCustomDirectiveResults(printer, op, operandType, optOperandType,
490                               varOperandTypes);
491 }
492 static void printCustomDirectiveOperandsAndTypes(
493     OpAsmPrinter &printer, Operation *op, Value operand, Value optOperand,
494     OperandRange varOperands, Type operandType, Type optOperandType,
495     TypeRange varOperandTypes) {
496   printCustomDirectiveOperands(printer, op, operand, optOperand, varOperands);
497   printCustomDirectiveResults(printer, op, operandType, optOperandType,
498                               varOperandTypes);
499 }
500 static void printCustomDirectiveRegions(OpAsmPrinter &printer, Operation *,
501                                         Region &region,
502                                         MutableArrayRef<Region> varRegions) {
503   printer.printRegion(region);
504   if (!varRegions.empty()) {
505     printer << ", ";
506     for (Region &region : varRegions)
507       printer.printRegion(region);
508   }
509 }
510 static void printCustomDirectiveSuccessors(OpAsmPrinter &printer, Operation *,
511                                            Block *successor,
512                                            SuccessorRange varSuccessors) {
513   printer << successor;
514   if (!varSuccessors.empty())
515     printer << ", " << varSuccessors.front();
516 }
517 static void printCustomDirectiveAttributes(OpAsmPrinter &printer, Operation *,
518                                            Attribute attribute,
519                                            Attribute optAttribute) {
520   printer << attribute;
521   if (optAttribute)
522     printer << ", " << optAttribute;
523 }
524 
525 static void printCustomDirectiveAttrDict(OpAsmPrinter &printer, Operation *op,
526                                          DictionaryAttr attrs) {
527   printer.printOptionalAttrDict(attrs.getValue());
528 }
529 
530 static void printCustomDirectiveOptionalOperandRef(OpAsmPrinter &printer,
531                                                    Operation *op,
532                                                    Value optOperand) {
533   printer << (optOperand ? "1" : "0");
534 }
535 
536 //===----------------------------------------------------------------------===//
537 // Test IsolatedRegionOp - parse passthrough region arguments.
538 //===----------------------------------------------------------------------===//
539 
540 static ParseResult parseIsolatedRegionOp(OpAsmParser &parser,
541                                          OperationState &result) {
542   OpAsmParser::OperandType argInfo;
543   Type argType = parser.getBuilder().getIndexType();
544 
545   // Parse the input operand.
546   if (parser.parseOperand(argInfo) ||
547       parser.resolveOperand(argInfo, argType, result.operands))
548     return failure();
549 
550   // Parse the body region, and reuse the operand info as the argument info.
551   Region *body = result.addRegion();
552   return parser.parseRegion(*body, argInfo, argType,
553                             /*enableNameShadowing=*/true);
554 }
555 
556 static void print(OpAsmPrinter &p, IsolatedRegionOp op) {
557   p << "test.isolated_region ";
558   p.printOperand(op.getOperand());
559   p.shadowRegionArgs(op.region(), op.getOperand());
560   p.printRegion(op.region(), /*printEntryBlockArgs=*/false);
561 }
562 
563 //===----------------------------------------------------------------------===//
564 // Test SSACFGRegionOp
565 //===----------------------------------------------------------------------===//
566 
567 RegionKind SSACFGRegionOp::getRegionKind(unsigned index) {
568   return RegionKind::SSACFG;
569 }
570 
571 //===----------------------------------------------------------------------===//
572 // Test GraphRegionOp
573 //===----------------------------------------------------------------------===//
574 
575 static ParseResult parseGraphRegionOp(OpAsmParser &parser,
576                                       OperationState &result) {
577   // Parse the body region, and reuse the operand info as the argument info.
578   Region *body = result.addRegion();
579   return parser.parseRegion(*body, /*arguments=*/{}, /*argTypes=*/{});
580 }
581 
582 static void print(OpAsmPrinter &p, GraphRegionOp op) {
583   p << "test.graph_region ";
584   p.printRegion(op.region(), /*printEntryBlockArgs=*/false);
585 }
586 
587 RegionKind GraphRegionOp::getRegionKind(unsigned index) {
588   return RegionKind::Graph;
589 }
590 
591 //===----------------------------------------------------------------------===//
592 // Test AffineScopeOp
593 //===----------------------------------------------------------------------===//
594 
595 static ParseResult parseAffineScopeOp(OpAsmParser &parser,
596                                       OperationState &result) {
597   // Parse the body region, and reuse the operand info as the argument info.
598   Region *body = result.addRegion();
599   return parser.parseRegion(*body, /*arguments=*/{}, /*argTypes=*/{});
600 }
601 
602 static void print(OpAsmPrinter &p, AffineScopeOp op) {
603   p << "test.affine_scope ";
604   p.printRegion(op.region(), /*printEntryBlockArgs=*/false);
605 }
606 
607 //===----------------------------------------------------------------------===//
608 // Test parser.
609 //===----------------------------------------------------------------------===//
610 
611 static ParseResult parseParseIntegerLiteralOp(OpAsmParser &parser,
612                                               OperationState &result) {
613   if (parser.parseOptionalColon())
614     return success();
615   uint64_t numResults;
616   if (parser.parseInteger(numResults))
617     return failure();
618 
619   IndexType type = parser.getBuilder().getIndexType();
620   for (unsigned i = 0; i < numResults; ++i)
621     result.addTypes(type);
622   return success();
623 }
624 
625 static void print(OpAsmPrinter &p, ParseIntegerLiteralOp op) {
626   p << ParseIntegerLiteralOp::getOperationName();
627   if (unsigned numResults = op->getNumResults())
628     p << " : " << numResults;
629 }
630 
631 static ParseResult parseParseWrappedKeywordOp(OpAsmParser &parser,
632                                               OperationState &result) {
633   StringRef keyword;
634   if (parser.parseKeyword(&keyword))
635     return failure();
636   result.addAttribute("keyword", parser.getBuilder().getStringAttr(keyword));
637   return success();
638 }
639 
640 static void print(OpAsmPrinter &p, ParseWrappedKeywordOp op) {
641   p << ParseWrappedKeywordOp::getOperationName() << " " << op.keyword();
642 }
643 
644 //===----------------------------------------------------------------------===//
645 // Test WrapRegionOp - wrapping op exercising `parseGenericOperation()`.
646 
647 static ParseResult parseWrappingRegionOp(OpAsmParser &parser,
648                                          OperationState &result) {
649   if (parser.parseKeyword("wraps"))
650     return failure();
651 
652   // Parse the wrapped op in a region
653   Region &body = *result.addRegion();
654   body.push_back(new Block);
655   Block &block = body.back();
656   Operation *wrapped_op = parser.parseGenericOperation(&block, block.begin());
657   if (!wrapped_op)
658     return failure();
659 
660   // Create a return terminator in the inner region, pass as operand to the
661   // terminator the returned values from the wrapped operation.
662   SmallVector<Value, 8> return_operands(wrapped_op->getResults());
663   OpBuilder builder(parser.getBuilder().getContext());
664   builder.setInsertionPointToEnd(&block);
665   builder.create<TestReturnOp>(wrapped_op->getLoc(), return_operands);
666 
667   // Get the results type for the wrapping op from the terminator operands.
668   Operation &return_op = body.back().back();
669   result.types.append(return_op.operand_type_begin(),
670                       return_op.operand_type_end());
671 
672   // Use the location of the wrapped op for the "test.wrapping_region" op.
673   result.location = wrapped_op->getLoc();
674 
675   return success();
676 }
677 
678 static void print(OpAsmPrinter &p, WrappingRegionOp op) {
679   p << op.getOperationName() << " wraps ";
680   p.printGenericOp(&op.region().front().front());
681 }
682 
683 //===----------------------------------------------------------------------===//
684 // Test PolyForOp - parse list of region arguments.
685 //===----------------------------------------------------------------------===//
686 
687 static ParseResult parsePolyForOp(OpAsmParser &parser, OperationState &result) {
688   SmallVector<OpAsmParser::OperandType, 4> ivsInfo;
689   // Parse list of region arguments without a delimiter.
690   if (parser.parseRegionArgumentList(ivsInfo))
691     return failure();
692 
693   // Parse the body region.
694   Region *body = result.addRegion();
695   auto &builder = parser.getBuilder();
696   SmallVector<Type, 4> argTypes(ivsInfo.size(), builder.getIndexType());
697   return parser.parseRegion(*body, ivsInfo, argTypes);
698 }
699 
700 //===----------------------------------------------------------------------===//
701 // Test removing op with inner ops.
702 //===----------------------------------------------------------------------===//
703 
704 namespace {
705 struct TestRemoveOpWithInnerOps
706     : public OpRewritePattern<TestOpWithRegionPattern> {
707   using OpRewritePattern<TestOpWithRegionPattern>::OpRewritePattern;
708 
709   LogicalResult matchAndRewrite(TestOpWithRegionPattern op,
710                                 PatternRewriter &rewriter) const override {
711     rewriter.eraseOp(op);
712     return success();
713   }
714 };
715 } // end anonymous namespace
716 
717 void TestOpWithRegionPattern::getCanonicalizationPatterns(
718     RewritePatternSet &results, MLIRContext *context) {
719   results.add<TestRemoveOpWithInnerOps>(context);
720 }
721 
722 OpFoldResult TestOpWithRegionFold::fold(ArrayRef<Attribute> operands) {
723   return operand();
724 }
725 
726 OpFoldResult TestOpConstant::fold(ArrayRef<Attribute> operands) {
727   return getValue();
728 }
729 
730 LogicalResult TestOpWithVariadicResultsAndFolder::fold(
731     ArrayRef<Attribute> operands, SmallVectorImpl<OpFoldResult> &results) {
732   for (Value input : this->operands()) {
733     results.push_back(input);
734   }
735   return success();
736 }
737 
738 OpFoldResult TestOpInPlaceFold::fold(ArrayRef<Attribute> operands) {
739   assert(operands.size() == 1);
740   if (operands.front()) {
741     (*this)->setAttr("attr", operands.front());
742     return getResult();
743   }
744   return {};
745 }
746 
747 OpFoldResult TestPassthroughFold::fold(ArrayRef<Attribute> operands) {
748   return getOperand();
749 }
750 
751 LogicalResult OpWithInferTypeInterfaceOp::inferReturnTypes(
752     MLIRContext *, Optional<Location> location, ValueRange operands,
753     DictionaryAttr attributes, RegionRange regions,
754     SmallVectorImpl<Type> &inferredReturnTypes) {
755   if (operands[0].getType() != operands[1].getType()) {
756     return emitOptionalError(location, "operand type mismatch ",
757                              operands[0].getType(), " vs ",
758                              operands[1].getType());
759   }
760   inferredReturnTypes.assign({operands[0].getType()});
761   return success();
762 }
763 
764 LogicalResult OpWithShapedTypeInferTypeInterfaceOp::inferReturnTypeComponents(
765     MLIRContext *context, Optional<Location> location, ValueRange operands,
766     DictionaryAttr attributes, RegionRange regions,
767     SmallVectorImpl<ShapedTypeComponents> &inferredReturnShapes) {
768   // Create return type consisting of the last element of the first operand.
769   auto operandType = *operands.getTypes().begin();
770   auto sval = operandType.dyn_cast<ShapedType>();
771   if (!sval) {
772     return emitOptionalError(location, "only shaped type operands allowed");
773   }
774   int64_t dim =
775       sval.hasRank() ? sval.getShape().front() : ShapedType::kDynamicSize;
776   auto type = IntegerType::get(context, 17);
777   inferredReturnShapes.push_back(ShapedTypeComponents({dim}, type));
778   return success();
779 }
780 
781 LogicalResult OpWithShapedTypeInferTypeInterfaceOp::reifyReturnTypeShapes(
782     OpBuilder &builder, ValueRange operands,
783     llvm::SmallVectorImpl<Value> &shapes) {
784   shapes = SmallVector<Value, 1>{
785       builder.createOrFold<memref::DimOp>(getLoc(), operands.front(), 0)};
786   return success();
787 }
788 
789 LogicalResult
790 OpWithResultShapePerDimInterfaceOp ::reifyReturnTypeShapesPerResultDim(
791     OpBuilder &builder,
792     llvm::SmallVectorImpl<llvm::SmallVector<Value>> &shapes) {
793   SmallVector<Value> operand1Shape, operand2Shape;
794   Location loc = getLoc();
795   for (auto i :
796        llvm::seq<int>(0, operand1().getType().cast<ShapedType>().getRank())) {
797     operand1Shape.push_back(builder.create<memref::DimOp>(loc, operand1(), i));
798   }
799   for (auto i :
800        llvm::seq<int>(0, operand2().getType().cast<ShapedType>().getRank())) {
801     operand2Shape.push_back(builder.create<memref::DimOp>(loc, operand2(), i));
802   }
803   shapes.emplace_back(std::move(operand2Shape));
804   shapes.emplace_back(std::move(operand1Shape));
805   return success();
806 }
807 
808 //===----------------------------------------------------------------------===//
809 // Test SideEffect interfaces
810 //===----------------------------------------------------------------------===//
811 
812 namespace {
813 /// A test resource for side effects.
814 struct TestResource : public SideEffects::Resource::Base<TestResource> {
815   StringRef getName() final { return "<Test>"; }
816 };
817 } // end anonymous namespace
818 
819 static void testSideEffectOpGetEffect(
820     Operation *op,
821     SmallVectorImpl<SideEffects::EffectInstance<TestEffects::Effect>>
822         &effects) {
823   auto effectsAttr = op->getAttrOfType<AffineMapAttr>("effect_parameter");
824   if (!effectsAttr)
825     return;
826 
827   effects.emplace_back(TestEffects::Concrete::get(), effectsAttr);
828 }
829 
830 void SideEffectOp::getEffects(
831     SmallVectorImpl<MemoryEffects::EffectInstance> &effects) {
832   // Check for an effects attribute on the op instance.
833   ArrayAttr effectsAttr = (*this)->getAttrOfType<ArrayAttr>("effects");
834   if (!effectsAttr)
835     return;
836 
837   // If there is one, it is an array of dictionary attributes that hold
838   // information on the effects of this operation.
839   for (Attribute element : effectsAttr) {
840     DictionaryAttr effectElement = element.cast<DictionaryAttr>();
841 
842     // Get the specific memory effect.
843     MemoryEffects::Effect *effect =
844         StringSwitch<MemoryEffects::Effect *>(
845             effectElement.get("effect").cast<StringAttr>().getValue())
846             .Case("allocate", MemoryEffects::Allocate::get())
847             .Case("free", MemoryEffects::Free::get())
848             .Case("read", MemoryEffects::Read::get())
849             .Case("write", MemoryEffects::Write::get());
850 
851     // Check for a non-default resource to use.
852     SideEffects::Resource *resource = SideEffects::DefaultResource::get();
853     if (effectElement.get("test_resource"))
854       resource = TestResource::get();
855 
856     // Check for a result to affect.
857     if (effectElement.get("on_result"))
858       effects.emplace_back(effect, getResult(), resource);
859     else if (Attribute ref = effectElement.get("on_reference"))
860       effects.emplace_back(effect, ref.cast<SymbolRefAttr>(), resource);
861     else
862       effects.emplace_back(effect, resource);
863   }
864 }
865 
866 void SideEffectOp::getEffects(
867     SmallVectorImpl<TestEffects::EffectInstance> &effects) {
868   testSideEffectOpGetEffect(getOperation(), effects);
869 }
870 
871 //===----------------------------------------------------------------------===//
872 // StringAttrPrettyNameOp
873 //===----------------------------------------------------------------------===//
874 
875 // This op has fancy handling of its SSA result name.
876 static ParseResult parseStringAttrPrettyNameOp(OpAsmParser &parser,
877                                                OperationState &result) {
878   // Add the result types.
879   for (size_t i = 0, e = parser.getNumResults(); i != e; ++i)
880     result.addTypes(parser.getBuilder().getIntegerType(32));
881 
882   if (parser.parseOptionalAttrDictWithKeyword(result.attributes))
883     return failure();
884 
885   // If the attribute dictionary contains no 'names' attribute, infer it from
886   // the SSA name (if specified).
887   bool hadNames = llvm::any_of(result.attributes, [](NamedAttribute attr) {
888     return attr.first == "names";
889   });
890 
891   // If there was no name specified, check to see if there was a useful name
892   // specified in the asm file.
893   if (hadNames || parser.getNumResults() == 0)
894     return success();
895 
896   SmallVector<StringRef, 4> names;
897   auto *context = result.getContext();
898 
899   for (size_t i = 0, e = parser.getNumResults(); i != e; ++i) {
900     auto resultName = parser.getResultName(i);
901     StringRef nameStr;
902     if (!resultName.first.empty() && !isdigit(resultName.first[0]))
903       nameStr = resultName.first;
904 
905     names.push_back(nameStr);
906   }
907 
908   auto namesAttr = parser.getBuilder().getStrArrayAttr(names);
909   result.attributes.push_back({Identifier::get("names", context), namesAttr});
910   return success();
911 }
912 
913 static void print(OpAsmPrinter &p, StringAttrPrettyNameOp op) {
914   p << "test.string_attr_pretty_name";
915 
916   // Note that we only need to print the "name" attribute if the asmprinter
917   // result name disagrees with it.  This can happen in strange cases, e.g.
918   // when there are conflicts.
919   bool namesDisagree = op.names().size() != op.getNumResults();
920 
921   SmallString<32> resultNameStr;
922   for (size_t i = 0, e = op.getNumResults(); i != e && !namesDisagree; ++i) {
923     resultNameStr.clear();
924     llvm::raw_svector_ostream tmpStream(resultNameStr);
925     p.printOperand(op.getResult(i), tmpStream);
926 
927     auto expectedName = op.names()[i].dyn_cast<StringAttr>();
928     if (!expectedName ||
929         tmpStream.str().drop_front() != expectedName.getValue()) {
930       namesDisagree = true;
931     }
932   }
933 
934   if (namesDisagree)
935     p.printOptionalAttrDictWithKeyword(op->getAttrs());
936   else
937     p.printOptionalAttrDictWithKeyword(op->getAttrs(), {"names"});
938 }
939 
940 // We set the SSA name in the asm syntax to the contents of the name
941 // attribute.
942 void StringAttrPrettyNameOp::getAsmResultNames(
943     function_ref<void(Value, StringRef)> setNameFn) {
944 
945   auto value = names();
946   for (size_t i = 0, e = value.size(); i != e; ++i)
947     if (auto str = value[i].dyn_cast<StringAttr>())
948       if (!str.getValue().empty())
949         setNameFn(getResult(i), str.getValue());
950 }
951 
952 //===----------------------------------------------------------------------===//
953 // RegionIfOp
954 //===----------------------------------------------------------------------===//
955 
956 static void print(OpAsmPrinter &p, RegionIfOp op) {
957   p << RegionIfOp::getOperationName() << " ";
958   p.printOperands(op.getOperands());
959   p << ": " << op.getOperandTypes();
960   p.printArrowTypeList(op.getResultTypes());
961   p << " then";
962   p.printRegion(op.thenRegion(),
963                 /*printEntryBlockArgs=*/true,
964                 /*printBlockTerminators=*/true);
965   p << " else";
966   p.printRegion(op.elseRegion(),
967                 /*printEntryBlockArgs=*/true,
968                 /*printBlockTerminators=*/true);
969   p << " join";
970   p.printRegion(op.joinRegion(),
971                 /*printEntryBlockArgs=*/true,
972                 /*printBlockTerminators=*/true);
973 }
974 
975 static ParseResult parseRegionIfOp(OpAsmParser &parser,
976                                    OperationState &result) {
977   SmallVector<OpAsmParser::OperandType, 2> operandInfos;
978   SmallVector<Type, 2> operandTypes;
979 
980   result.regions.reserve(3);
981   Region *thenRegion = result.addRegion();
982   Region *elseRegion = result.addRegion();
983   Region *joinRegion = result.addRegion();
984 
985   // Parse operand, type and arrow type lists.
986   if (parser.parseOperandList(operandInfos) ||
987       parser.parseColonTypeList(operandTypes) ||
988       parser.parseArrowTypeList(result.types))
989     return failure();
990 
991   // Parse all attached regions.
992   if (parser.parseKeyword("then") || parser.parseRegion(*thenRegion, {}, {}) ||
993       parser.parseKeyword("else") || parser.parseRegion(*elseRegion, {}, {}) ||
994       parser.parseKeyword("join") || parser.parseRegion(*joinRegion, {}, {}))
995     return failure();
996 
997   return parser.resolveOperands(operandInfos, operandTypes,
998                                 parser.getCurrentLocation(), result.operands);
999 }
1000 
1001 OperandRange RegionIfOp::getSuccessorEntryOperands(unsigned index) {
1002   assert(index < 2 && "invalid region index");
1003   return getOperands();
1004 }
1005 
1006 void RegionIfOp::getSuccessorRegions(
1007     Optional<unsigned> index, ArrayRef<Attribute> operands,
1008     SmallVectorImpl<RegionSuccessor> &regions) {
1009   // We always branch to the join region.
1010   if (index.hasValue()) {
1011     if (index.getValue() < 2)
1012       regions.push_back(RegionSuccessor(&joinRegion(), getJoinArgs()));
1013     else
1014       regions.push_back(RegionSuccessor(getResults()));
1015     return;
1016   }
1017 
1018   // The then and else regions are the entry regions of this op.
1019   regions.push_back(RegionSuccessor(&thenRegion(), getThenArgs()));
1020   regions.push_back(RegionSuccessor(&elseRegion(), getElseArgs()));
1021 }
1022 
1023 //===----------------------------------------------------------------------===//
1024 // SingleNoTerminatorCustomAsmOp
1025 //===----------------------------------------------------------------------===//
1026 
1027 static ParseResult parseSingleNoTerminatorCustomAsmOp(OpAsmParser &parser,
1028                                                       OperationState &state) {
1029   Region *body = state.addRegion();
1030   if (parser.parseRegion(*body, /*arguments=*/{}, /*argTypes=*/{}))
1031     return failure();
1032   return success();
1033 }
1034 
1035 static void print(SingleNoTerminatorCustomAsmOp op, OpAsmPrinter &printer) {
1036   printer << op.getOperationName();
1037   printer.printRegion(
1038       op.getRegion(), /*printEntryBlockArgs=*/false,
1039       // This op has a single block without terminators. But explicitly mark
1040       // as not printing block terminators for testing.
1041       /*printBlockTerminators=*/false);
1042 }
1043 
1044 #include "TestOpEnums.cpp.inc"
1045 #include "TestOpInterfaces.cpp.inc"
1046 #include "TestOpStructs.cpp.inc"
1047 #include "TestTypeInterfaces.cpp.inc"
1048 
1049 #define GET_OP_CLASSES
1050 #include "TestOps.cpp.inc"
1051