21#include <mlir/Bytecode/BytecodeImplementation.h>
22#include <mlir/IR/DialectImplementation.h>
23#include <mlir/Support/LLVM.h>
25#include <llvm/ADT/TypeSwitch.h>
33#define GET_ATTRDEF_CLASSES
46enum class LLZKAttrEncoding : uint8_t {
53 Attribute readAttribute(DialectBytecodeReader &reader)
const final {
55 if (failed(reader.readVarInt(encoding))) {
58 if (encoding > std::numeric_limits<uint8_t>::max()) {
59 reader.emitError() <<
"unknown LLZK attribute encoding: " << encoding;
63 switch (
static_cast<LLZKAttrEncoding
>(encoding)) {
64 case LLZKAttrEncoding::LoopBounds: {
65 FailureOr<APInt> lower =
readAPInt(reader);
66 FailureOr<APInt> upper =
readAPInt(reader);
67 FailureOr<APInt> step =
readAPInt(reader);
68 if (failed(lower) || failed(upper) || failed(step)) {
71 return LoopBoundsAttr::get(getContext(), *lower, *upper, *step);
75 reader.emitError() <<
"unknown LLZK attribute encoding: " << encoding;
79 LogicalResult writeAttribute(Attribute attr, DialectBytecodeWriter &writer)
const final {
80 if (
auto loopBounds = dyn_cast<LoopBoundsAttr>(attr)) {
81 writer.writeVarInt(
static_cast<uint64_t
>(LLZKAttrEncoding::LoopBounds));
91LogicalResult verifyLlzkMainAttr(Operation *op, Attribute attr) {
92 ModuleOp moduleOp = llvm::dyn_cast<ModuleOp>(op);
94 return op->emitError().append(
96 ModuleOp::getOperationName(),
'\''
101 if (succeeded(mainStructTypeOpt)) {
103 SymbolTableCollection symbolTables;
104 return st.getDefinition(symbolTables, op);
114 return verifyLlzkMainAttr(op, attr.getValue());
119auto LLZKDialect::initialize() ->
void {
124 #define GET_ATTRDEF_LIST
133 addInterfaces<LLZKDialectBytecodeInterfaceImpl>();
::llvm::LogicalResult verifyOperationAttribute(::mlir::Operation *op, ::mlir::NamedAttribute attribute) override
Provides a hook for verifying dialect attributes attached to the given op.
void writeAPInt(mlir::DialectBytecodeWriter &writer, const llvm::APInt &value)
Write an APInt with its bit width, so the bytecode reader can use MLIR's native APInt payload encodin...
mlir::FailureOr< llvm::APInt > readAPInt(mlir::DialectBytecodeReader &reader)
Read an APInt written by writeAPInt.
FailureOr< StructType > getTypeFromLlzkMainAttr(ModuleOp op, Attribute attr)
constexpr char MAIN_ATTR_NAME[]
Name of the attribute on the top-level ModuleOp that specifies the type of the main struct.
This implements the bytecode interface for the LLZK dialect.
LLZKDialectBytecodeInterface(mlir::Dialect *dia)