MLIR  20.0.0git
BuiltinDialectBytecode.cpp
Go to the documentation of this file.
1 //===- BuiltinDialectBytecode.cpp - Builtin 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 
10 #include "AttributeDetail.h"
13 #include "mlir/IR/BuiltinDialect.h"
14 #include "mlir/IR/BuiltinTypes.h"
15 #include "mlir/IR/Diagnostics.h"
17 #include "llvm/ADT/TypeSwitch.h"
18 
19 using namespace mlir;
20 
21 //===----------------------------------------------------------------------===//
22 // BuiltinDialectBytecodeInterface
23 //===----------------------------------------------------------------------===//
24 
25 namespace {
26 
27 //===----------------------------------------------------------------------===//
28 // Utility functions
29 
30 // TODO: Move these to separate file.
31 
32 // Returns the bitwidth if known, else return 0.
33 static unsigned getIntegerBitWidth(DialectBytecodeReader &reader, Type type) {
34  if (auto intType = dyn_cast<IntegerType>(type)) {
35  return intType.getWidth();
36  }
37  if (llvm::isa<IndexType>(type)) {
38  return IndexType::kInternalStorageBitWidth;
39  }
40  reader.emitError()
41  << "expected integer or index type for IntegerAttr, but got: " << type;
42  return 0;
43 }
44 
45 static LogicalResult readAPIntWithKnownWidth(DialectBytecodeReader &reader,
46  Type type, FailureOr<APInt> &val) {
47  unsigned bitWidth = getIntegerBitWidth(reader, type);
48  val = reader.readAPIntWithKnownWidth(bitWidth);
49  return val;
50 }
51 
52 static LogicalResult
53 readAPFloatWithKnownSemantics(DialectBytecodeReader &reader, Type type,
54  FailureOr<APFloat> &val) {
55  auto ftype = dyn_cast<FloatType>(type);
56  if (!ftype)
57  return failure();
58  val = reader.readAPFloatWithKnownSemantics(ftype.getFloatSemantics());
59  return success();
60 }
61 
62 LogicalResult
63 readPotentiallySplatString(DialectBytecodeReader &reader, ShapedType type,
64  bool isSplat,
65  SmallVectorImpl<StringRef> &rawStringData) {
66  rawStringData.resize(isSplat ? 1 : type.getNumElements());
67  for (StringRef &value : rawStringData)
68  if (failed(reader.readString(value)))
69  return failure();
70  return success();
71 }
72 
73 void writePotentiallySplatString(DialectBytecodeWriter &writer,
74  DenseStringElementsAttr attr) {
75  bool isSplat = attr.isSplat();
76  if (isSplat)
77  return writer.writeOwnedString(attr.getRawStringData().front());
78 
79  for (StringRef str : attr.getRawStringData())
80  writer.writeOwnedString(str);
81 }
82 
83 #include "mlir/IR/BuiltinDialectBytecode.cpp.inc"
84 
85 /// This class implements the bytecode interface for the builtin dialect.
86 struct BuiltinDialectBytecodeInterface : public BytecodeDialectInterface {
87  BuiltinDialectBytecodeInterface(Dialect *dialect)
88  : BytecodeDialectInterface(dialect) {}
89 
90  //===--------------------------------------------------------------------===//
91  // Attributes
92 
93  Attribute readAttribute(DialectBytecodeReader &reader) const override {
94  return ::readAttribute(getContext(), reader);
95  }
96 
97  LogicalResult writeAttribute(Attribute attr,
98  DialectBytecodeWriter &writer) const override {
99  return ::writeAttribute(attr, writer);
100  }
101 
102  //===--------------------------------------------------------------------===//
103  // Types
104 
105  Type readType(DialectBytecodeReader &reader) const override {
106  return ::readType(getContext(), reader);
107  }
108 
109  LogicalResult writeType(Type type,
110  DialectBytecodeWriter &writer) const override {
111  return ::writeType(type, writer);
112  }
113 };
114 } // namespace
115 
116 void builtin_dialect_detail::addBytecodeInterface(BuiltinDialect *dialect) {
117  dialect->addInterfaces<BuiltinDialectBytecodeInterface>();
118 }
static MLIRContext * getContext(OpFoldResult val)
Attributes are known-constant values of operations.
Definition: Attributes.h:25
This class defines a virtual interface for reading a bytecode stream, providing hooks into the byteco...
virtual FailureOr< APInt > readAPIntWithKnownWidth(unsigned bitWidth)=0
Read an APInt that is known to have been encoded with the given width.
virtual InFlightDiagnostic emitError(const Twine &msg={}) const =0
Emit an error to the reader.
virtual FailureOr< APFloat > readAPFloatWithKnownSemantics(const llvm::fltSemantics &semantics)=0
Read an APFloat that is known to have been encoded with the given semantics.
virtual LogicalResult readString(StringRef &result)=0
Read a string from the bytecode.
This class defines a virtual interface for writing to a bytecode stream, providing hooks into the byt...
virtual void writeOwnedString(StringRef str)=0
Write a string to the bytecode, which is owned by the caller and is guaranteed to not die before the ...
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
Definition: Dialect.h:38
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
void addBytecodeInterface(BuiltinDialect *dialect)
Add the interfaces necessary for encoding the builtin dialect components in bytecode.
Include the generated interface declarations.