[Mlir-commits] [mlir] [MLIR][Bytecode] Enforce alignment requirements (PR #157004)

Nikhil Kalra llvmlistbot at llvm.org
Fri Sep 5 09:31:17 PDT 2025


================
@@ -281,8 +291,22 @@ class EncodingReader {
 
     // Process the section alignment if present.
     if (hasAlignment) {
+      // Read the requested alignment from the bytecode parser.
       uint64_t alignment;
-      if (failed(parseVarInt(alignment)) || failed(alignTo(alignment)))
+      if (failed(parseVarInt(alignment)))
+        return failure();
+
+      // Check that the requested alignment is less than or equal to the
+      // alignment of the root buffer. If it is not, we cannot safely guarantee
+      // that the specified alignment is globally correct.
+      //
+      // E.g. if the buffer is 8k aligned and the section is 16k aligned,
+      // we could end up at an offset of 24k, which is not globally 16k aligned.
+      if (failed(alignmentValidator(alignment)))
+        return emitError("failed to align section ID: ", unsigned(sectionID));
+
+      // Align the buffer.
+      if (failed(alignTo(alignment)))
----------------
nikalra wrote:

I believe this should be tested by the newly added unit test: https://github.com/nikalra/llvm-project/blob/09ba822e15a2c615e0c8ad7bdb41bfd4ea18b293/mlir/unittests/Bytecode/BytecodeTest.cpp#L167

https://github.com/llvm/llvm-project/pull/157004


More information about the Mlir-commits mailing list