156 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef MLIR_TESTOPS_H10#define MLIR_TESTOPS_H11 12#include "TestAttributes.h"13#include "TestInterfaces.h"14#include "TestTypes.h"15#include "mlir/Bytecode/BytecodeImplementation.h"16#include "mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h"17#include "mlir/Dialect/DLTI/DLTI.h"18#include "mlir/Dialect/DLTI/Traits.h"19#include "mlir/Dialect/Func/IR/FuncOps.h"20#include "mlir/Dialect/LLVMIR/NVVMRequiresSMTraits.h"21#include "mlir/Dialect/Linalg/IR/Linalg.h"22#include "mlir/Dialect/Linalg/IR/LinalgInterfaces.h"23#include "mlir/Dialect/Traits.h"24#include "mlir/IR/AsmState.h"25#include "mlir/IR/BuiltinOps.h"26#include "mlir/IR/BuiltinTypes.h"27#include "mlir/IR/Dialect.h"28#include "mlir/IR/DialectResourceBlobManager.h"29#include "mlir/IR/ExtensibleDialect.h"30#include "mlir/IR/OpDefinition.h"31#include "mlir/IR/OpImplementation.h"32#include "mlir/IR/RegionKindInterface.h"33#include "mlir/IR/SymbolTable.h"34#include "mlir/Interfaces/CallInterfaces.h"35#include "mlir/Interfaces/ControlFlowInterfaces.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#include "llvm/ADT/SmallVector.h"46 47namespace test {48class TestDialect;49 50//===----------------------------------------------------------------------===//51// TestResource52//===----------------------------------------------------------------------===//53 54/// A test resource for side effects.55struct TestResource : public mlir::SideEffects::Resource::Base<TestResource> {56 llvm::StringRef getName() final { return "<Test>"; }57};58 59//===----------------------------------------------------------------------===//60// PropertiesWithCustomPrint61//===----------------------------------------------------------------------===//62 63struct PropertiesWithCustomPrint {64 /// A shared_ptr to a const object is safe: it is equivalent to a value-based65 /// member. Here the label will be deallocated when the last operation66 /// refering to it is destroyed. However there is no pool-allocation: this is67 /// offloaded to the client.68 std::shared_ptr<const std::string> label;69 int value;70 bool operator==(const PropertiesWithCustomPrint &rhs) const {71 return value == rhs.value && *label == *rhs.label;72 }73};74 75llvm::LogicalResult setPropertiesFromAttribute(76 PropertiesWithCustomPrint &prop, mlir::Attribute attr,77 llvm::function_ref<mlir::InFlightDiagnostic()> emitError);78mlir::DictionaryAttr79getPropertiesAsAttribute(mlir::MLIRContext *ctx,80 const PropertiesWithCustomPrint &prop);81llvm::hash_code computeHash(const PropertiesWithCustomPrint &prop);82void customPrintProperties(mlir::OpAsmPrinter &p,83 const PropertiesWithCustomPrint &prop);84mlir::ParseResult customParseProperties(mlir::OpAsmParser &parser,85 PropertiesWithCustomPrint &prop);86 87//===----------------------------------------------------------------------===//88// MyPropStruct89//===----------------------------------------------------------------------===//90namespace test_properties {91class MyPropStruct {92public:93 std::string content;94 // These three methods are invoked through the `MyStructProperty` wrapper95 // defined in TestOps.td96 mlir::Attribute asAttribute(mlir::MLIRContext *ctx) const;97 static llvm::LogicalResult98 setFromAttr(MyPropStruct &prop, mlir::Attribute attr,99 llvm::function_ref<mlir::InFlightDiagnostic()> emitError);100 llvm::hash_code hash() const;101 bool operator==(const MyPropStruct &rhs) const {102 return content == rhs.content;103 }104};105inline llvm::hash_code hash_value(const MyPropStruct &S) { return S.hash(); }106} // namespace test_properties107using test_properties::MyPropStruct;108 109llvm::LogicalResult readFromMlirBytecode(mlir::DialectBytecodeReader &reader,110 MyPropStruct &prop);111void writeToMlirBytecode(mlir::DialectBytecodeWriter &writer,112 MyPropStruct &prop);113 114//===----------------------------------------------------------------------===//115// VersionedProperties116//===----------------------------------------------------------------------===//117 118struct VersionedProperties {119 // For the sake of testing, assume that this object was associated to version120 // 1.2 of the test dialect when having only one int value. In the current121 // version 2.0, the property has two values. We also assume that the class is122 // upgrade-able if value2 = 0.123 int value1;124 int value2;125 bool operator==(const VersionedProperties &rhs) const {126 return value1 == rhs.value1 && value2 == rhs.value2;127 }128};129 130llvm::LogicalResult setPropertiesFromAttribute(131 VersionedProperties &prop, mlir::Attribute attr,132 llvm::function_ref<mlir::InFlightDiagnostic()> emitError);133mlir::DictionaryAttr getPropertiesAsAttribute(mlir::MLIRContext *ctx,134 const VersionedProperties &prop);135llvm::hash_code computeHash(const VersionedProperties &prop);136void customPrintProperties(mlir::OpAsmPrinter &p,137 const VersionedProperties &prop);138mlir::ParseResult customParseProperties(mlir::OpAsmParser &parser,139 VersionedProperties &prop);140 141//===----------------------------------------------------------------------===//142// Bytecode Support143//===----------------------------------------------------------------------===//144 145llvm::LogicalResult readFromMlirBytecode(mlir::DialectBytecodeReader &reader,146 llvm::MutableArrayRef<int64_t> prop);147void writeToMlirBytecode(mlir::DialectBytecodeWriter &writer,148 llvm::ArrayRef<int64_t> prop);149 150} // namespace test151 152#define GET_OP_CLASSES153#include "TestOps.h.inc"154 155#endif // MLIR_TESTOPS_H156