1 //===- PDLInterp.cpp - PDL Interpreter Dialect ------------------*- C++ -*-===//
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/Dialect/PDLInterp/IR/PDLInterp.h"
10 #include "mlir/Dialect/PDL/IR/PDLTypes.h"
11 #include "mlir/IR/BuiltinTypes.h"
12 #include "mlir/IR/DialectImplementation.h"
13 
14 using namespace mlir;
15 using namespace mlir::pdl_interp;
16 
17 //===----------------------------------------------------------------------===//
18 // PDLInterp Dialect
19 //===----------------------------------------------------------------------===//
20 
21 void PDLInterpDialect::initialize() {
22   addOperations<
23 #define GET_OP_LIST
24 #include "mlir/Dialect/PDLInterp/IR/PDLInterpOps.cpp.inc"
25       >();
26 }
27 
28 //===----------------------------------------------------------------------===//
29 // pdl_interp::CreateOperationOp
30 //===----------------------------------------------------------------------===//
31 
32 static ParseResult parseCreateOperationOpAttributes(
33     OpAsmParser &p, SmallVectorImpl<OpAsmParser::OperandType> &attrOperands,
34     ArrayAttr &attrNamesAttr) {
35   Builder &builder = p.getBuilder();
36   SmallVector<Attribute, 4> attrNames;
37   if (succeeded(p.parseOptionalLBrace())) {
38     do {
39       StringAttr nameAttr;
40       OpAsmParser::OperandType operand;
41       if (p.parseAttribute(nameAttr) || p.parseEqual() ||
42           p.parseOperand(operand))
43         return failure();
44       attrNames.push_back(nameAttr);
45       attrOperands.push_back(operand);
46     } while (succeeded(p.parseOptionalComma()));
47     if (p.parseRBrace())
48       return failure();
49   }
50   attrNamesAttr = builder.getArrayAttr(attrNames);
51   return success();
52 }
53 
54 static void printCreateOperationOpAttributes(OpAsmPrinter &p,
55                                              CreateOperationOp op,
56                                              OperandRange attrArgs,
57                                              ArrayAttr attrNames) {
58   if (attrNames.empty())
59     return;
60   p << " {";
61   interleaveComma(llvm::seq<int>(0, attrNames.size()), p,
62                   [&](int i) { p << attrNames[i] << " = " << attrArgs[i]; });
63   p << '}';
64 }
65 
66 //===----------------------------------------------------------------------===//
67 // pdl_interp::GetValueTypeOp
68 //===----------------------------------------------------------------------===//
69 
70 /// Given the result type of a `GetValueTypeOp`, return the expected input type.
71 static Type getGetValueTypeOpValueType(Type type) {
72   Type valueTy = pdl::ValueType::get(type.getContext());
73   return type.isa<pdl::RangeType>() ? pdl::RangeType::get(valueTy) : valueTy;
74 }
75 
76 //===----------------------------------------------------------------------===//
77 // TableGen Auto-Generated Op and Interface Definitions
78 //===----------------------------------------------------------------------===//
79 
80 #define GET_OP_CLASSES
81 #include "mlir/Dialect/PDLInterp/IR/PDLInterpOps.cpp.inc"
82