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