[MLIR] Add native Bytecode support for properties

This is adding a new interface (`BytecodeOpInterface`) to allow operations to
opt-in skipping conversion to attribute and serializing properties to native
bytecode.

The scheme relies on a new section where properties are stored in sequence

  { size, serialize_properties }, ...

The operations are storing the index of a properties, a table of offset is
built when loading the properties section the first time.

Back-deployment to version prior to 4 are relying on getAttrDictionnary() which
we intend to deprecate and remove: that is putting a de-factor end-of-support
horizon for supporting deployments to version older than 4.

Differential Revision: https://reviews.llvm.org/D151065
This commit is contained in:
Mehdi Amini
2023-05-01 08:43:50 -07:00
parent a6d09d4b1a
commit 837d1ce0dc
62 changed files with 766 additions and 46 deletions

View File

@@ -9,6 +9,7 @@
#ifndef STANDALONE_STANDALONEDIALECT_H
#define STANDALONE_STANDALONEDIALECT_H
#include "mlir/Bytecode/BytecodeOpInterface.h"
#include "mlir/IR/Dialect.h"
#include "Standalone/StandaloneOpsDialect.h.inc"

View File

@@ -74,6 +74,10 @@ public:
/// Read a reference to the given attribute.
virtual LogicalResult readAttribute(Attribute &result) = 0;
/// Read an optional reference to the given attribute. Returns success even if
/// the Attribute isn't present.
virtual LogicalResult readOptionalAttribute(Attribute &attr) = 0;
template <typename T>
LogicalResult readAttributes(SmallVectorImpl<T> &attrs) {
return readList(attrs, [this](T &attr) { return readAttribute(attr); });
@@ -88,6 +92,18 @@ public:
return emitError() << "expected " << llvm::getTypeName<T>()
<< ", but got: " << baseResult;
}
template <typename T>
LogicalResult readOptionalAttribute(T &result) {
Attribute baseResult;
if (failed(readOptionalAttribute(baseResult)))
return failure();
if (!baseResult)
return success();
if ((result = dyn_cast<T>(baseResult)))
return success();
return emitError() << "expected " << llvm::getTypeName<T>()
<< ", but got: " << baseResult;
}
/// Read a reference to the given type.
virtual LogicalResult readType(Type &result) = 0;
@@ -179,6 +195,7 @@ public:
/// Write a reference to the given attribute.
virtual void writeAttribute(Attribute attr) = 0;
virtual void writeOptionalAttribute(Attribute attr) = 0;
template <typename T>
void writeAttributes(ArrayRef<T> attrs) {
writeList(attrs, [this](T attr) { writeAttribute(attr); });

View File

@@ -0,0 +1,27 @@
//===- CallInterfaces.h - Call Interfaces for MLIR --------------*- C++ -*-===//
//
// Part of the LLVM Project, 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
//
//===----------------------------------------------------------------------===//
//
// This file contains the definitions of the BytecodeOpInterface defined in
// `BytecodeOpInterface.td`.
//
//===----------------------------------------------------------------------===//
#ifndef MLIR_BYTECODE_BYTECODEOPINTERFACE_H
#define MLIR_BYTECODE_BYTECODEOPINTERFACE_H
#include "mlir/Bytecode/BytecodeImplementation.h"
#include "mlir/Bytecode/BytecodeOpInterface.h"
#include "mlir/Bytecode/BytecodeReader.h"
#include "mlir/Bytecode/BytecodeWriter.h"
#include "mlir/IR/OpDefinition.h"
#include "mlir/Support/LogicalResult.h"
/// Include the generated interface declarations.
#include "mlir/Bytecode/BytecodeOpInterface.h.inc"
#endif // MLIR_BYTECODE_BYTECODEOPINTERFACE_H

View File

@@ -0,0 +1,43 @@
//===- BytecodeOpInterface.td - Bytecode OpInterface -------*- tablegen -*-===//
//
// Part of the LLVM Project, 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
//
//===----------------------------------------------------------------------===//
//
// This file contains an interface for operation interactions with the bytecode
// serialization/deserialization, in particular for properties.
//
//===----------------------------------------------------------------------===//
#ifndef MLIR_BYTECODE_BYTECODEOPINTERFACES
#define MLIR_BYTECODE_BYTECODEOPINTERFACES
include "mlir/IR/OpBase.td"
// `BytecodeOpInterface`
def BytecodeOpInterface : OpInterface<"BytecodeOpInterface"> {
let description = [{
This interface allows operation to control the serialization of their
properties.
}];
let cppNamespace = "::mlir";
let methods = [
StaticInterfaceMethod<[{
Read the properties for this operation from the bytecode and populate the state.
}],
"LogicalResult", "readProperties", (ins
"::mlir::DialectBytecodeReader &":$reader,
"::mlir::OperationState &":$state)
>,
InterfaceMethod<[{
Write the properties for this operation to the bytecode.
}],
"void", "writeProperties", (ins "::mlir::DialectBytecodeWriter &":$writer)
>,
];
}
#endif // MLIR_BYTECODE_BYTECODEOPINTERFACES

View File

@@ -46,6 +46,9 @@ public:
/// is returned by bytecode writer entry point.
void setDesiredBytecodeVersion(int64_t bytecodeVersion);
/// Get the set desired bytecode version to emit.
int64_t getDesiredBytecodeVersion() const;
//===--------------------------------------------------------------------===//
// Resources
//===--------------------------------------------------------------------===//

View File

@@ -0,0 +1 @@
add_mlir_interface(BytecodeOpInterface)

View File

@@ -69,8 +69,11 @@ enum ID : uint8_t {
/// This section contains the versions of each dialect.
kDialectVersions = 7,
/// This section contains the properties for the operations.
kProperties = 8,
/// The total number of section types.
kNumSections = 8,
kNumSections = 9,
};
} // namespace Section
@@ -90,6 +93,7 @@ enum : uint8_t {
kHasSuccessors = 0b00001000,
kHasInlineRegions = 0b00010000,
kHasUseListOrders = 0b00100000,
kHasProperties = 0b01000000,
// clang-format on
};
} // namespace OpEncodingMask

View File

@@ -1,3 +1,4 @@
add_subdirectory(Bytecode)
add_subdirectory(Conversion)
add_subdirectory(Dialect)
add_subdirectory(IR)

View File

@@ -14,6 +14,7 @@
#ifndef MLIR_DIALECT_AMDGPU_IR_AMDGPUDIALECT_H_
#define MLIR_DIALECT_AMDGPU_IR_AMDGPUDIALECT_H_
#include "mlir/Bytecode/BytecodeOpInterface.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/Dialect.h"
#include "mlir/IR/OpDefinition.h"

View File

@@ -13,6 +13,7 @@
#ifndef MLIR_DIALECT_AMX_AMXDIALECT_H_
#define MLIR_DIALECT_AMX_AMXDIALECT_H_
#include "mlir/Bytecode/BytecodeOpInterface.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/Dialect.h"
#include "mlir/IR/OpDefinition.h"

View File

@@ -9,6 +9,7 @@
#ifndef MLIR_DIALECT_AFFINE_TRANSFORMOPS_AFFINETRANSFORMOPS_H
#define MLIR_DIALECT_AFFINE_TRANSFORMOPS_AFFINETRANSFORMOPS_H
#include "mlir/Bytecode/BytecodeOpInterface.h"
#include "mlir/Dialect/Transform/IR/TransformInterfaces.h"
#include "mlir/Dialect/Transform/IR/TransformTypes.h"
#include "mlir/IR/OpImplementation.h"

View File

@@ -9,6 +9,7 @@
#ifndef MLIR_DIALECT_ARITH_IR_ARITH_H_
#define MLIR_DIALECT_ARITH_IR_ARITH_H_
#include "mlir/Bytecode/BytecodeOpInterface.h"
#include "mlir/IR/Dialect.h"
#include "mlir/IR/OpDefinition.h"
#include "mlir/IR/OpImplementation.h"

View File

@@ -13,6 +13,7 @@
#ifndef MLIR_DIALECT_ARMNEON_ARMNEONDIALECT_H_
#define MLIR_DIALECT_ARMNEON_ARMNEONDIALECT_H_
#include "mlir/Bytecode/BytecodeOpInterface.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/Dialect.h"
#include "mlir/IR/OpDefinition.h"

View File

@@ -13,6 +13,7 @@
#ifndef MLIR_DIALECT_ARMSVE_ARMSVEDIALECT_H
#define MLIR_DIALECT_ARMSVE_ARMSVEDIALECT_H
#include "mlir/Bytecode/BytecodeOpInterface.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/Dialect.h"
#include "mlir/IR/OpDefinition.h"

View File

@@ -14,6 +14,7 @@
#ifndef MLIR_DIALECT_ASYNC_IR_ASYNC_H
#define MLIR_DIALECT_ASYNC_IR_ASYNC_H
#include "mlir/Bytecode/BytecodeOpInterface.h"
#include "mlir/Dialect/Async/IR/AsyncTypes.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinTypes.h"

View File

@@ -9,6 +9,7 @@
#ifndef MLIR_DIALECT_BUFFERIZATION_IR_BUFFERIZATION_H_
#define MLIR_DIALECT_BUFFERIZATION_IR_BUFFERIZATION_H_
#include "mlir/Bytecode/BytecodeOpInterface.h"
#include "mlir/Dialect/Bufferization/IR/AllocationOpInterface.h"
#include "mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h"
#include "mlir/Interfaces/CopyOpInterface.h"

View File

@@ -9,6 +9,7 @@
#ifndef MLIR_DIALECT_BUFFERIZATION_TRANSFORMOPS_BUFFERIZATIONTRANSFORMOPS_H
#define MLIR_DIALECT_BUFFERIZATION_TRANSFORMOPS_BUFFERIZATIONTRANSFORMOPS_H
#include "mlir/Bytecode/BytecodeOpInterface.h"
#include "mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h"
#include "mlir/Dialect/Transform/IR/TransformInterfaces.h"
#include "mlir/Dialect/Transform/IR/TransformTypes.h"

View File

@@ -9,6 +9,7 @@
#ifndef MLIR_DIALECT_COMPLEX_IR_COMPLEX_H_
#define MLIR_DIALECT_COMPLEX_IR_COMPLEX_H_
#include "mlir/Bytecode/BytecodeOpInterface.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/OpImplementation.h"
#include "mlir/Interfaces/InferTypeOpInterface.h"

View File

@@ -13,6 +13,7 @@
#ifndef MLIR_DIALECT_CONTROLFLOW_IR_CONTROLFLOW_H
#define MLIR_DIALECT_CONTROLFLOW_IR_CONTROLFLOW_H
#include "mlir/Bytecode/BytecodeOpInterface.h"
#include "mlir/IR/Dialect.h"
#include "mlir/Dialect/ControlFlow/IR/ControlFlowOpsDialect.h.inc"

View File

@@ -13,6 +13,7 @@
#ifndef MLIR_DIALECT_CONTROLFLOW_IR_CONTROLFLOWOPS_H
#define MLIR_DIALECT_CONTROLFLOW_IR_CONTROLFLOWOPS_H
#include "mlir/Bytecode/BytecodeOpInterface.h"
#include "mlir/Dialect/ControlFlow/IR/ControlFlow.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinTypes.h"

Some files were not shown because too many files have changed in this diff Show More