Skip to content

Commit b10ca7a

Browse files
committed
Micro-optimize alignment check.
1 parent f8b97f3 commit b10ca7a

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

mlir/lib/Bytecode/Reader/BytecodeReader.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,19 @@ class EncodingReader {
114114
if (!llvm::isPowerOf2_32(alignment))
115115
return emitError("expected alignment to be a power-of-two");
116116

117+
auto isUnaligned = [&](const uint8_t *ptr) {
118+
return ((uintptr_t)ptr & (alignment - 1)) != 0;
119+
};
120+
117121
// Ensure the data buffer was sufficiently aligned in the first place.
118-
if (LLVM_UNLIKELY(
119-
!llvm::isAddrAligned(llvm::Align(alignment), buffer.begin()))) {
122+
if (LLVM_UNLIKELY(isUnaligned(buffer.begin()))) {
120123
return emitError("expected bytecode buffer to be aligned to ", alignment,
121124
", but got pointer: '0x" +
122125
llvm::utohexstr((uintptr_t)buffer.begin()) + "'");
123126
}
124127

125128
// Shift the reader position to the next alignment boundary.
126-
while (uintptr_t(dataIt) & (uintptr_t(alignment) - 1)) {
129+
while (isUnaligned(dataIt)) {
127130
uint8_t padding;
128131
if (failed(parseByte(padding)))
129132
return failure();
@@ -135,7 +138,7 @@ class EncodingReader {
135138

136139
// Ensure the data iterator is now aligned. This case is unlikely because we
137140
// *just* went through the effort to align the data iterator.
138-
if (LLVM_UNLIKELY(!llvm::isAddrAligned(llvm::Align(alignment), dataIt))) {
141+
if (LLVM_UNLIKELY(isUnaligned(dataIt))) {
139142
return emitError("expected data iterator aligned to ", alignment,
140143
", but got pointer: '0x" +
141144
llvm::utohexstr((uintptr_t)dataIt) + "'");

0 commit comments

Comments
 (0)