| // Copyright 2021 The IREE Authors | |
| // | |
| // Licensed 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 | |
| #include "iree-dialects/Dialect/Input/InputOps.h" | |
| #include "iree-dialects/Dialect/Input/InputDialect.h" | |
| #include "mlir/Dialect/Arithmetic/IR/Arithmetic.h" | |
| #include "mlir/IR/Builders.h" | |
| #include "mlir/IR/BuiltinTypes.h" | |
| #include "mlir/IR/OpImplementation.h" | |
| #include "mlir/IR/TypeUtilities.h" | |
| using namespace mlir; | |
| using namespace mlir::iree_compiler::IREE::Input; | |
| //===----------------------------------------------------------------------===// | |
| // custom<SymbolVisibility>($sym_visibility) | |
| //===----------------------------------------------------------------------===// | |
| // some.op custom<SymbolVisibility>($sym_visibility) $sym_name | |
| // -> | |
| // some.op @foo | |
| // some.op private @foo | |
| static ParseResult parseSymbolVisibility(OpAsmParser &parser, | |
| StringAttr &symVisibilityAttr) { | |
| StringRef symVisibility; | |
| parser.parseOptionalKeyword(&symVisibility, {"public", "private", "nested"}); | |
| if (!symVisibility.empty()) { | |
| symVisibilityAttr = parser.getBuilder().getStringAttr(symVisibility); | |
| } | |
| return success(); | |
| } | |
| static void printSymbolVisibility(OpAsmPrinter &p, Operation *op, | |
| StringAttr symVisibilityAttr) { | |
| if (!symVisibilityAttr) { | |
| p << "public"; | |
| } else { | |
| p << symVisibilityAttr.getValue(); | |
| } | |
| } | |
| //===----------------------------------------------------------------------===// | |
| // custom<TypeOrAttr>($type, $attr) | |
| //===----------------------------------------------------------------------===// | |
| // some.op custom<TypeOrAttr>($type, $attr) | |
| // -> | |
| // some.op : i32 | |
| // some.op = 42 : i32 | |
| // some.op : i32 = 42 : index | |
| static ParseResult parseTypeOrAttr(OpAsmParser &parser, TypeAttr &typeAttr, | |
| Attribute &attr) { | |
| if (succeeded(parser.parseOptionalEqual())) { | |
| if (failed(parser.parseAttribute(attr))) { | |
| return parser.emitError(parser.getCurrentLocation()) | |
| << "expected attribute"; | |
| } | |
| typeAttr = TypeAttr::get(attr.getType()); | |
| return success(); | |
| } | |
| Type type; | |
| if (failed(parser.parseColonType(type))) { | |
| return parser.emitError(parser.getCurrentLocation()) << "expected type"; | |
| } | |
| typeAttr = TypeAttr::get(type); | |
| if (succeeded(parser.parseOptionalEqual())) { | |
| if (failed(parser.parseAttribute(attr))) { | |
| return parser.emitError(parser.getCurrentLocation()) | |
| << "expected attribute"; | |
| } | |
| } | |
| return success(); | |
| } | |
| static void printTypeOrAttr(OpAsmPrinter &p, Operation *op, TypeAttr type, | |
| Attribute attr) { | |
| if (!attr || attr.getType() != type.getValue()) { | |
| p << " : "; | |
| p.printAttribute(type); | |
| } | |
| if (attr) { | |
| p << " = "; | |
| p.printAttribute(attr); | |
| } | |
| } | |
| //===----------------------------------------------------------------------===// | |
| // GlobalOp | |
| //===----------------------------------------------------------------------===// | |
| void GlobalOp::build(OpBuilder &builder, OperationState &result, StringRef name, | |
| bool isMutable, Type type, | |
| Optional<Attribute> initialValue, | |
| ArrayRef<NamedAttribute> attrs) { | |
| result.addAttribute(SymbolTable::getSymbolAttrName(), | |
| builder.getStringAttr(name)); | |
| if (isMutable) { | |
| result.addAttribute("is_mutable", builder.getUnitAttr()); | |
| } | |
| if (initialValue.hasValue()) { | |
| result.addAttribute("initial_value", initialValue.getValue()); | |
| } | |
| result.addAttribute("type", TypeAttr::get(type)); | |
| result.attributes.append(attrs.begin(), attrs.end()); | |
| } | |
| void GlobalOp::build(OpBuilder &builder, OperationState &result, StringRef name, | |
| bool isMutable, Type type, | |
| ArrayRef<NamedAttribute> attrs) { | |
| build(builder, result, name, isMutable, type, llvm::None, attrs); | |
| } | |
| #define GET_OP_CLASSES | |
| #include "iree-dialects/Dialect/Input/InputOps.cpp.inc" |