| 1 | //===- TestOps.h - MLIR Test Dialect Operations ---------------------------===// |
| 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 | #ifndef MLIR_TESTOPS_H |
| 10 | #define MLIR_TESTOPS_H |
| 11 | |
| 12 | #include "TestAttributes.h" |
| 13 | #include "TestInterfaces.h" |
| 14 | #include "TestTypes.h" |
| 15 | #include "mlir/Bytecode/BytecodeImplementation.h" |
| 16 | #include "mlir/Dialect/DLTI/DLTI.h" |
| 17 | #include "mlir/Dialect/DLTI/Traits.h" |
| 18 | #include "mlir/Dialect/Func/IR/FuncOps.h" |
| 19 | #include "mlir/Dialect/LLVMIR/NVVMRequiresSMTraits.h" |
| 20 | #include "mlir/Dialect/Linalg/IR/Linalg.h" |
| 21 | #include "mlir/Dialect/Linalg/IR/LinalgInterfaces.h" |
| 22 | #include "mlir/Dialect/Traits.h" |
| 23 | #include "mlir/IR/AsmState.h" |
| 24 | #include "mlir/IR/BuiltinOps.h" |
| 25 | #include "mlir/IR/BuiltinTypes.h" |
| 26 | #include "mlir/IR/Dialect.h" |
| 27 | #include "mlir/IR/DialectResourceBlobManager.h" |
| 28 | #include "mlir/IR/ExtensibleDialect.h" |
| 29 | #include "mlir/IR/OpDefinition.h" |
| 30 | #include "mlir/IR/OpImplementation.h" |
| 31 | #include "mlir/IR/RegionKindInterface.h" |
| 32 | #include "mlir/IR/SymbolTable.h" |
| 33 | #include "mlir/Interfaces/CallInterfaces.h" |
| 34 | #include "mlir/Interfaces/ControlFlowInterfaces.h" |
| 35 | #include "mlir/Interfaces/CopyOpInterface.h" |
| 36 | #include "mlir/Interfaces/DerivedAttributeOpInterface.h" |
| 37 | #include "mlir/Interfaces/InferIntRangeInterface.h" |
| 38 | #include "mlir/Interfaces/InferTypeOpInterface.h" |
| 39 | #include "mlir/Interfaces/LoopLikeInterface.h" |
| 40 | #include "mlir/Interfaces/MemorySlotInterfaces.h" |
| 41 | #include "mlir/Interfaces/SideEffectInterfaces.h" |
| 42 | #include "mlir/Interfaces/ValueBoundsOpInterface.h" |
| 43 | #include "mlir/Interfaces/ViewLikeInterface.h" |
| 44 | #include "llvm/ADT/SetVector.h" |
| 45 | |
| 46 | namespace test { |
| 47 | class TestDialect; |
| 48 | |
| 49 | //===----------------------------------------------------------------------===// |
| 50 | // TestResource |
| 51 | //===----------------------------------------------------------------------===// |
| 52 | |
| 53 | /// A test resource for side effects. |
| 54 | struct TestResource : public mlir::SideEffects::Resource::Base<TestResource> { |
| 55 | llvm::StringRef getName() final { return "<Test>" ; } |
| 56 | }; |
| 57 | |
| 58 | //===----------------------------------------------------------------------===// |
| 59 | // PropertiesWithCustomPrint |
| 60 | //===----------------------------------------------------------------------===// |
| 61 | |
| 62 | struct PropertiesWithCustomPrint { |
| 63 | /// A shared_ptr to a const object is safe: it is equivalent to a value-based |
| 64 | /// member. Here the label will be deallocated when the last operation |
| 65 | /// refering to it is destroyed. However there is no pool-allocation: this is |
| 66 | /// offloaded to the client. |
| 67 | std::shared_ptr<const std::string> label; |
| 68 | int value; |
| 69 | bool operator==(const PropertiesWithCustomPrint &rhs) const { |
| 70 | return value == rhs.value && *label == *rhs.label; |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | llvm::LogicalResult setPropertiesFromAttribute( |
| 75 | PropertiesWithCustomPrint &prop, mlir::Attribute attr, |
| 76 | llvm::function_ref<mlir::InFlightDiagnostic()> emitError); |
| 77 | mlir::DictionaryAttr |
| 78 | getPropertiesAsAttribute(mlir::MLIRContext *ctx, |
| 79 | const PropertiesWithCustomPrint &prop); |
| 80 | llvm::hash_code computeHash(const PropertiesWithCustomPrint &prop); |
| 81 | void customPrintProperties(mlir::OpAsmPrinter &p, |
| 82 | const PropertiesWithCustomPrint &prop); |
| 83 | mlir::ParseResult customParseProperties(mlir::OpAsmParser &parser, |
| 84 | PropertiesWithCustomPrint &prop); |
| 85 | |
| 86 | //===----------------------------------------------------------------------===// |
| 87 | // MyPropStruct |
| 88 | //===----------------------------------------------------------------------===// |
| 89 | namespace test_properties { |
| 90 | class MyPropStruct { |
| 91 | public: |
| 92 | std::string content; |
| 93 | // These three methods are invoked through the `MyStructProperty` wrapper |
| 94 | // defined in TestOps.td |
| 95 | mlir::Attribute asAttribute(mlir::MLIRContext *ctx) const; |
| 96 | static llvm::LogicalResult |
| 97 | setFromAttr(MyPropStruct &prop, mlir::Attribute attr, |
| 98 | llvm::function_ref<mlir::InFlightDiagnostic()> emitError); |
| 99 | llvm::hash_code hash() const; |
| 100 | bool operator==(const MyPropStruct &rhs) const { |
| 101 | return content == rhs.content; |
| 102 | } |
| 103 | }; |
| 104 | inline llvm::hash_code hash_value(const MyPropStruct &S) { return S.hash(); } |
| 105 | } // namespace test_properties |
| 106 | using test_properties::MyPropStruct; |
| 107 | |
| 108 | llvm::LogicalResult readFromMlirBytecode(mlir::DialectBytecodeReader &reader, |
| 109 | MyPropStruct &prop); |
| 110 | void writeToMlirBytecode(mlir::DialectBytecodeWriter &writer, |
| 111 | MyPropStruct &prop); |
| 112 | |
| 113 | //===----------------------------------------------------------------------===// |
| 114 | // VersionedProperties |
| 115 | //===----------------------------------------------------------------------===// |
| 116 | |
| 117 | struct VersionedProperties { |
| 118 | // For the sake of testing, assume that this object was associated to version |
| 119 | // 1.2 of the test dialect when having only one int value. In the current |
| 120 | // version 2.0, the property has two values. We also assume that the class is |
| 121 | // upgrade-able if value2 = 0. |
| 122 | int value1; |
| 123 | int value2; |
| 124 | bool operator==(const VersionedProperties &rhs) const { |
| 125 | return value1 == rhs.value1 && value2 == rhs.value2; |
| 126 | } |
| 127 | }; |
| 128 | |
| 129 | llvm::LogicalResult setPropertiesFromAttribute( |
| 130 | VersionedProperties &prop, mlir::Attribute attr, |
| 131 | llvm::function_ref<mlir::InFlightDiagnostic()> emitError); |
| 132 | mlir::DictionaryAttr getPropertiesAsAttribute(mlir::MLIRContext *ctx, |
| 133 | const VersionedProperties &prop); |
| 134 | llvm::hash_code computeHash(const VersionedProperties &prop); |
| 135 | void customPrintProperties(mlir::OpAsmPrinter &p, |
| 136 | const VersionedProperties &prop); |
| 137 | mlir::ParseResult customParseProperties(mlir::OpAsmParser &parser, |
| 138 | VersionedProperties &prop); |
| 139 | |
| 140 | //===----------------------------------------------------------------------===// |
| 141 | // Bytecode Support |
| 142 | //===----------------------------------------------------------------------===// |
| 143 | |
| 144 | llvm::LogicalResult readFromMlirBytecode(mlir::DialectBytecodeReader &reader, |
| 145 | llvm::MutableArrayRef<int64_t> prop); |
| 146 | void writeToMlirBytecode(mlir::DialectBytecodeWriter &writer, |
| 147 | llvm::ArrayRef<int64_t> prop); |
| 148 | |
| 149 | } // namespace test |
| 150 | |
| 151 | #define GET_OP_CLASSES |
| 152 | #include "TestOps.h.inc" |
| 153 | |
| 154 | #endif // MLIR_TESTOPS_H |
| 155 | |