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