//===- Dialect.cpp - Toy IR Dialect registration in MLIR ------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This file implements the dialect for the Toy IR: custom type parsing and // operation verification. // //===----------------------------------------------------------------------===// #include "toy/Dialect.h" #include "mlir/IR/Attributes.h" #include "mlir/IR/Builders.h" #include "mlir/IR/BuiltinTypes.h" #include "mlir/IR/OpImplementation.h" #include "mlir/IR/Operation.h" #include "mlir/IR/OperationSupport.h" #include "mlir/IR/Value.h" #include "mlir/Interfaces/FunctionImplementation.h" #include "mlir/Support/LLVM.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Casting.h" #include <algorithm> #include <string> usingnamespacemlir; usingnamespacemlir::toy; #include "toy/Dialect.cpp.inc" //===----------------------------------------------------------------------===// // ToyDialect //===----------------------------------------------------------------------===// /// Dialect initialization, the instance will be owned by the context. This is /// the point of registration of types and operations for the dialect. void ToyDialect::initialize() { … } //===----------------------------------------------------------------------===// // Toy Operations //===----------------------------------------------------------------------===// /// A generalized parser for binary operations. This parses the different forms /// of 'printBinaryOp' below. static mlir::ParseResult parseBinaryOp(mlir::OpAsmParser &parser, mlir::OperationState &result) { … } /// A generalized printer for binary operations. It prints in two different /// forms depending on if all of the types match. static void printBinaryOp(mlir::OpAsmPrinter &printer, mlir::Operation *op) { … } //===----------------------------------------------------------------------===// // ConstantOp //===----------------------------------------------------------------------===// /// Build a constant operation. /// The builder is passed as an argument, so is the state that this method is /// expected to fill in order to build the operation. void ConstantOp::build(mlir::OpBuilder &builder, mlir::OperationState &state, double value) { … } /// The 'OpAsmParser' class provides a collection of methods for parsing /// various punctuation, as well as attributes, operands, types, etc. Each of /// these methods returns a `ParseResult`. This class is a wrapper around /// `LogicalResult` that can be converted to a boolean `true` value on failure, /// or `false` on success. This allows for easily chaining together a set of /// parser rules. These rules are used to populate an `mlir::OperationState` /// similarly to the `build` methods described above. mlir::ParseResult ConstantOp::parse(mlir::OpAsmParser &parser, mlir::OperationState &result) { … } /// The 'OpAsmPrinter' class is a stream that allows for formatting /// strings, attributes, operands, types, etc. void ConstantOp::print(mlir::OpAsmPrinter &printer) { … } /// Verifier for the constant operation. This corresponds to the /// `let hasVerifier = 1` in the op definition. llvm::LogicalResult ConstantOp::verify() { … } //===----------------------------------------------------------------------===// // AddOp //===----------------------------------------------------------------------===// void AddOp::build(mlir::OpBuilder &builder, mlir::OperationState &state, mlir::Value lhs, mlir::Value rhs) { … } mlir::ParseResult AddOp::parse(mlir::OpAsmParser &parser, mlir::OperationState &result) { … } void AddOp::print(mlir::OpAsmPrinter &p) { … } //===----------------------------------------------------------------------===// // FuncOp //===----------------------------------------------------------------------===// void FuncOp::build(mlir::OpBuilder &builder, mlir::OperationState &state, llvm::StringRef name, mlir::FunctionType type, llvm::ArrayRef<mlir::NamedAttribute> attrs) { … } mlir::ParseResult FuncOp::parse(mlir::OpAsmParser &parser, mlir::OperationState &result) { … } void FuncOp::print(mlir::OpAsmPrinter &p) { … } //===----------------------------------------------------------------------===// // GenericCallOp //===----------------------------------------------------------------------===// void GenericCallOp::build(mlir::OpBuilder &builder, mlir::OperationState &state, StringRef callee, ArrayRef<mlir::Value> arguments) { … } //===----------------------------------------------------------------------===// // MulOp //===----------------------------------------------------------------------===// void MulOp::build(mlir::OpBuilder &builder, mlir::OperationState &state, mlir::Value lhs, mlir::Value rhs) { … } mlir::ParseResult MulOp::parse(mlir::OpAsmParser &parser, mlir::OperationState &result) { … } void MulOp::print(mlir::OpAsmPrinter &p) { … } //===----------------------------------------------------------------------===// // ReturnOp //===----------------------------------------------------------------------===// llvm::LogicalResult ReturnOp::verify() { … } //===----------------------------------------------------------------------===// // TransposeOp //===----------------------------------------------------------------------===// void TransposeOp::build(mlir::OpBuilder &builder, mlir::OperationState &state, mlir::Value value) { … } llvm::LogicalResult TransposeOp::verify() { … } //===----------------------------------------------------------------------===// // TableGen'd op method definitions //===----------------------------------------------------------------------===// #define GET_OP_CLASSES #include "toy/Ops.cpp.inc"