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"
15
14
#include " mlir/IR/BuiltinOps.h"
16
15
#include " mlir/IR/Diagnostics.h"
17
16
#include " mlir/IR/OpImplementation.h"
20
19
#include " mlir/Support/LLVM.h"
21
20
#include " mlir/Support/LogicalResult.h"
22
21
#include " llvm/ADT/ArrayRef.h"
23
- #include " llvm/ADT/MapVector.h"
24
22
#include " llvm/ADT/ScopeExit.h"
25
- #include " llvm/ADT/SmallString.h"
26
23
#include " llvm/ADT/StringExtras.h"
27
24
#include " llvm/ADT/StringRef.h"
28
25
#include " llvm/Support/Endian.h"
29
26
#include " llvm/Support/MemoryBufferRef.h"
30
- #include " llvm/Support/SaveAndRestore.h"
31
27
#include " llvm/Support/SourceMgr.h"
28
+
32
29
#include < cstddef>
33
30
#include < list>
34
31
#include < memory>
@@ -93,25 +90,36 @@ namespace {
93
90
class EncodingReader {
94
91
public:
95
92
explicit EncodingReader (ArrayRef<uint8_t > contents, Location fileLoc)
96
- : dataIt (contents.data()), dataEnd(contents.end ()), fileLoc(fileLoc) {}
93
+ : buffer (contents), dataIt(buffer.begin ()), fileLoc(fileLoc) {}
97
94
explicit EncodingReader (StringRef contents, Location fileLoc)
98
95
: EncodingReader({reinterpret_cast <const uint8_t *>(contents.data ()),
99
96
contents.size ()},
100
97
fileLoc) {}
101
98
102
99
// / Returns true if the entire section has been read.
103
- bool empty () const { return dataIt == dataEnd ; }
100
+ bool empty () const { return dataIt == buffer. end () ; }
104
101
105
102
// / Returns the remaining size of the bytecode.
106
- size_t size () const { return dataEnd - dataIt; }
103
+ size_t size () const { return buffer. end () - dataIt; }
107
104
108
105
// / Align the current reader position to the specified alignment.
109
106
LogicalResult alignTo (unsigned alignment) {
110
107
if (!llvm::isPowerOf2_32 (alignment))
111
108
return emitError (" expected alignment to be a power-of-two" );
112
109
110
+ auto isUnaligned = [&](const uint8_t *ptr) {
111
+ return ((uintptr_t )ptr & (alignment - 1 )) != 0 ;
112
+ };
113
+
114
+ // Ensure the data buffer was sufficiently aligned in the first place.
115
+ if (LLVM_UNLIKELY (isUnaligned (buffer.begin ()))) {
116
+ return emitError (" expected bytecode buffer to be aligned to " , alignment,
117
+ " , but got pointer: '0x" +
118
+ llvm::utohexstr ((uintptr_t )buffer.begin ()) + " '" );
119
+ }
120
+
113
121
// Shift the reader position to the next alignment boundary.
114
- while (uintptr_t (dataIt) & ( uintptr_t (alignment) - 1 )) {
122
+ while (isUnaligned (dataIt)) {
115
123
uint8_t padding;
116
124
if (failed (parseByte (padding)))
117
125
return failure ();
@@ -123,7 +131,7 @@ class EncodingReader {
123
131
124
132
// Ensure the data iterator is now aligned. This case is unlikely because we
125
133
// *just* went through the effort to align the data iterator.
126
- if (LLVM_UNLIKELY (! llvm::isAddrAligned ( llvm::Align (alignment), dataIt))) {
134
+ if (LLVM_UNLIKELY (isUnaligned ( dataIt))) {
127
135
return emitError (" expected data iterator aligned to " , alignment,
128
136
" , but got pointer: '0x" +
129
137
llvm::utohexstr ((uintptr_t )dataIt) + " '" );
@@ -320,8 +328,11 @@ class EncodingReader {
320
328
return success ();
321
329
}
322
330
323
- // / The current data iterator, and an iterator to the end of the buffer.
324
- const uint8_t *dataIt, *dataEnd;
331
+ // / The bytecode buffer.
332
+ ArrayRef<uint8_t > buffer;
333
+
334
+ // / The current iterator within the 'buffer'.
335
+ const uint8_t *dataIt;
325
336
326
337
// / A location for the bytecode used to report errors.
327
338
Location fileLoc;
0 commit comments