|
11 | 11 | #include "mlir/Bytecode/BytecodeImplementation.h"
|
12 | 12 | #include "mlir/Bytecode/BytecodeOpInterface.h"
|
13 | 13 | #include "mlir/Bytecode/Encoding.h"
|
14 |
| -#include "mlir/IR/BuiltinDialect.h" |
| 14 | +#include "mlir/IR/AsmState.h" |
15 | 15 | #include "mlir/IR/BuiltinOps.h"
|
16 | 16 | #include "mlir/IR/Diagnostics.h"
|
| 17 | +#include "mlir/IR/Dialect.h" |
17 | 18 | #include "mlir/IR/OpImplementation.h"
|
18 | 19 | #include "mlir/IR/Verifier.h"
|
19 | 20 | #include "mlir/IR/Visitors.h"
|
20 | 21 | #include "mlir/Support/LLVM.h"
|
21 | 22 | #include "mlir/Support/LogicalResult.h"
|
22 | 23 | #include "llvm/ADT/ArrayRef.h"
|
23 |
| -#include "llvm/ADT/MapVector.h" |
24 | 24 | #include "llvm/ADT/ScopeExit.h"
|
25 |
| -#include "llvm/ADT/SmallString.h" |
26 | 25 | #include "llvm/ADT/StringExtras.h"
|
27 | 26 | #include "llvm/ADT/StringRef.h"
|
28 | 27 | #include "llvm/Support/Endian.h"
|
| 28 | +#include "llvm/Support/ErrorHandling.h" |
29 | 29 | #include "llvm/Support/MemoryBufferRef.h"
|
30 |
| -#include "llvm/Support/SaveAndRestore.h" |
31 | 30 | #include "llvm/Support/SourceMgr.h"
|
| 31 | + |
| 32 | +#include <cassert> |
32 | 33 | #include <cstddef>
|
| 34 | +#include <cstdint> |
| 35 | +#include <cstring> |
33 | 36 | #include <list>
|
34 | 37 | #include <memory>
|
35 | 38 | #include <numeric>
|
36 | 39 | #include <optional>
|
| 40 | +#include <string> |
37 | 41 |
|
38 | 42 | #define DEBUG_TYPE "mlir-bytecode-reader"
|
39 | 43 |
|
@@ -93,23 +97,31 @@ namespace {
|
93 | 97 | class EncodingReader {
|
94 | 98 | public:
|
95 | 99 | explicit EncodingReader(ArrayRef<uint8_t> contents, Location fileLoc)
|
96 |
| - : dataIt(contents.data()), dataEnd(contents.end()), fileLoc(fileLoc) {} |
| 100 | + : buffer(contents), dataIt(buffer.begin()), fileLoc(fileLoc) {} |
97 | 101 | explicit EncodingReader(StringRef contents, Location fileLoc)
|
98 | 102 | : EncodingReader({reinterpret_cast<const uint8_t *>(contents.data()),
|
99 | 103 | contents.size()},
|
100 | 104 | fileLoc) {}
|
101 | 105 |
|
102 | 106 | /// Returns true if the entire section has been read.
|
103 |
| - bool empty() const { return dataIt == dataEnd; } |
| 107 | + bool empty() const { return dataIt == buffer.end(); } |
104 | 108 |
|
105 | 109 | /// Returns the remaining size of the bytecode.
|
106 |
| - size_t size() const { return dataEnd - dataIt; } |
| 110 | + size_t size() const { return buffer.end() - dataIt; } |
107 | 111 |
|
108 | 112 | /// Align the current reader position to the specified alignment.
|
109 | 113 | LogicalResult alignTo(unsigned alignment) {
|
110 | 114 | if (!llvm::isPowerOf2_32(alignment))
|
111 | 115 | return emitError("expected alignment to be a power-of-two");
|
112 | 116 |
|
| 117 | + // Ensure the data buffer was sufficiently aligned in the first place. |
| 118 | + if (LLVM_UNLIKELY( |
| 119 | + !llvm::isAddrAligned(llvm::Align(alignment), buffer.begin()))) { |
| 120 | + return emitError("expected bytecode buffer to be aligned to ", alignment, |
| 121 | + ", but got pointer: '0x" + |
| 122 | + llvm::utohexstr((uintptr_t)buffer.begin()) + "'"); |
| 123 | + } |
| 124 | + |
113 | 125 | // Shift the reader position to the next alignment boundary.
|
114 | 126 | while (uintptr_t(dataIt) & (uintptr_t(alignment) - 1)) {
|
115 | 127 | uint8_t padding;
|
@@ -320,8 +332,11 @@ class EncodingReader {
|
320 | 332 | return success();
|
321 | 333 | }
|
322 | 334 |
|
323 |
| - /// The current data iterator, and an iterator to the end of the buffer. |
324 |
| - const uint8_t *dataIt, *dataEnd; |
| 335 | + /// The bytecode buffer. |
| 336 | + ArrayRef<uint8_t> buffer; |
| 337 | + |
| 338 | + /// The current iterator within the 'buffer'. |
| 339 | + const uint8_t *dataIt; |
325 | 340 |
|
326 | 341 | /// A location for the bytecode used to report errors.
|
327 | 342 | Location fileLoc;
|
|
0 commit comments