| 1 | //===- LLVMDialectBytecode.cpp - LLVM Bytecode Implementation -------------===// |
| 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 | #include "LLVMDialectBytecode.h" |
| 10 | #include "mlir/Bytecode/BytecodeImplementation.h" |
| 11 | #include "mlir/Dialect/LLVMIR/LLVMAttrs.h" |
| 12 | #include "mlir/Dialect/LLVMIR/LLVMDialectDecl.h" |
| 13 | #include "mlir/Dialect/LLVMIR/LLVMTypes.h" |
| 14 | #include "mlir/IR/Diagnostics.h" |
| 15 | #include "llvm/ADT/APFloat.h" |
| 16 | #include "llvm/ADT/SmallVector.h" |
| 17 | #include <type_traits> |
| 18 | |
| 19 | using namespace mlir; |
| 20 | using namespace mlir::LLVM; |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | //===--------------------------------------------------------------------===// |
| 25 | // Optional ArrayRefs |
| 26 | // |
| 27 | // Note that both the writer and reader functions consider attributes to be |
| 28 | // optional. This is because the attribute may be present or empty. |
| 29 | //===--------------------------------------------------------------------===// |
| 30 | |
| 31 | template <class EntryTy> |
| 32 | static void writeOptionalArrayRef(DialectBytecodeWriter &writer, |
| 33 | ArrayRef<EntryTy> storage) { |
| 34 | if (storage.empty()) { |
| 35 | writer.writeOwnedBool(value: false); |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | writer.writeOwnedBool(value: true); |
| 40 | writer.writeList(storage, [&](EntryTy val) { |
| 41 | if constexpr (std::is_base_of_v<Attribute, EntryTy>) { |
| 42 | (void)writer.writeOptionalAttribute(attr: val); |
| 43 | } else if constexpr (std::is_integral_v<EntryTy>) { |
| 44 | (void)writer.writeVarInt(value: val); |
| 45 | } else { |
| 46 | static_assert(true, "EntryTy not supported" ); |
| 47 | } |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | template <class EntryTy> |
| 52 | static LogicalResult readOptionalArrayRef(DialectBytecodeReader &reader, |
| 53 | SmallVectorImpl<EntryTy> &storage) { |
| 54 | bool isPresent = false; |
| 55 | if (failed(Result: reader.readBool(result&: isPresent))) |
| 56 | return failure(); |
| 57 | // Nothing to do here, the array is empty. |
| 58 | if (!isPresent) |
| 59 | return success(); |
| 60 | |
| 61 | auto readEntry = [&]() -> FailureOr<EntryTy> { |
| 62 | EntryTy temp; |
| 63 | if constexpr (std::is_base_of_v<Attribute, EntryTy>) { |
| 64 | if (succeeded(reader.readOptionalAttribute(temp))) |
| 65 | return temp; |
| 66 | } else if constexpr (std::is_integral_v<EntryTy>) { |
| 67 | if (succeeded(reader.readVarInt(result&: temp))) |
| 68 | return temp; |
| 69 | } else { |
| 70 | static_assert(true, "EntryTy not supported" ); |
| 71 | } |
| 72 | return failure(); |
| 73 | }; |
| 74 | |
| 75 | return reader.readList(storage, readEntry); |
| 76 | } |
| 77 | |
| 78 | //===--------------------------------------------------------------------===// |
| 79 | // Optional integral types |
| 80 | //===--------------------------------------------------------------------===// |
| 81 | |
| 82 | template <class EntryTy> |
| 83 | static void writeOptionalInt(DialectBytecodeWriter &writer, |
| 84 | std::optional<EntryTy> storage) { |
| 85 | static_assert(std::is_integral_v<EntryTy>, |
| 86 | "EntryTy must be an integral type" ); |
| 87 | EntryTy val = storage.value_or(0); |
| 88 | writer.writeVarIntWithFlag(value: val, flag: storage.has_value()); |
| 89 | } |
| 90 | |
| 91 | template <class EntryTy> |
| 92 | static LogicalResult readOptionalInt(DialectBytecodeReader &reader, |
| 93 | std::optional<EntryTy> &storage) { |
| 94 | static_assert(std::is_integral_v<EntryTy>, |
| 95 | "EntryTy must be an integral type" ); |
| 96 | uint64_t result = 0; |
| 97 | bool flag = false; |
| 98 | if (failed(Result: reader.readVarIntWithFlag(result, flag))) |
| 99 | return failure(); |
| 100 | if (flag) |
| 101 | storage = static_cast<EntryTy>(result); |
| 102 | else |
| 103 | storage = std::nullopt; |
| 104 | return success(); |
| 105 | } |
| 106 | } // namespace |
| 107 | |
| 108 | //===--------------------------------------------------------------------===// |
| 109 | // Tablegen generated bytecode functions |
| 110 | //===--------------------------------------------------------------------===// |
| 111 | |
| 112 | #include "mlir/Dialect/LLVMIR/LLVMDialectBytecode.cpp.inc" |
| 113 | |
| 114 | namespace { |
| 115 | //===--------------------------------------------------------------------===// |
| 116 | // LLVMDialectBytecodeInterface |
| 117 | //===--------------------------------------------------------------------===// |
| 118 | |
| 119 | /// This class implements the bytecode interface for the LLVM dialect. |
| 120 | struct LLVMDialectBytecodeInterface : public BytecodeDialectInterface { |
| 121 | LLVMDialectBytecodeInterface(Dialect *dialect) |
| 122 | : BytecodeDialectInterface(dialect) {} |
| 123 | |
| 124 | // Attributes |
| 125 | Attribute readAttribute(DialectBytecodeReader &reader) const override { |
| 126 | return ::readAttribute(context: getContext(), reader); |
| 127 | } |
| 128 | |
| 129 | LogicalResult writeAttribute(Attribute attr, |
| 130 | DialectBytecodeWriter &writer) const override { |
| 131 | return ::writeAttribute(attribute: attr, writer); |
| 132 | } |
| 133 | |
| 134 | // Types |
| 135 | Type readType(DialectBytecodeReader &reader) const override { |
| 136 | return ::readType(context: getContext(), reader); |
| 137 | } |
| 138 | |
| 139 | LogicalResult writeType(Type type, |
| 140 | DialectBytecodeWriter &writer) const override { |
| 141 | return ::writeType(type, writer); |
| 142 | } |
| 143 | }; |
| 144 | } // namespace |
| 145 | |
| 146 | void LLVM::detail::addBytecodeInterface(LLVMDialect *dialect) { |
| 147 | dialect->addInterfaces<LLVMDialectBytecodeInterface>(); |
| 148 | } |
| 149 | |