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

source code of mlir/test/lib/Dialect/Test/TestOps.h