← projectsllvm
open-source contribution / merged 8 sep 2026

Fixing an undersized memory calculation in LLVM's MLIR framework. A merged patch that brings struct sizes into agreement with LLVM's memory-layout rules.

c++ / llvm / mlir / filecheck

11 bytes for a 17-byte object

Two parts of a compiler need to agree on an object's size: the part reserving memory and the part placing values inside it. An issue reported by MattPD exposed a disagreement between MLIR and LLVM that could cause Flang, LLVM's Fortran compiler, to allocate too little memory.

The example was a packed struct containing an 80-bit float and a one-byte integer. In this target layout, the float needs 10 bytes to hold its value but occupies 16 bytes in memory. MLIR counted the first number; LLVM used the second.

before / 10-byte value + 1-byte integer = 11 bytes after / 16-byte allocation + 1-byte integer = 17 bytes

The integer belongs at byte offset 16. An 11-byte allocation cannot contain it.

what packing actually changes

A packed struct removes extra alignment gaps before its fields. Each field still occupies its full allocation size. The bug came from treating those two rules as the same thing.

The patch keeps field placement unchanged and rounds each field's occupied size to the target's required alignment before adding it to the total. The correction sits in MLIR's shared layout calculation, so nested structs and arrays receive the corrected size too.

testing the distinction

A useful regression test had to catch the wrong size without accidentally changing packing. One case places a one-byte field before the float, checking that no alignment gap is introduced. Another gives a type different required and preferred alignments, checking that the calculation uses the required one.

Seven layout queries cover those cases, the original example, unpacked structs, nesting and arrays. The pre-merge build-and-test jobs passed on Linux, Linux AArch64 and Windows.

accepted upstream

I submitted the implementation and regression tests for the existing diagnosis. OpenAI Codex assisted with the code investigation, implementation and validation; that assistance is disclosed in the pull request and commit.

Two reviewers approved the patch. It merged into LLVM's main branch on 8 September 2026 and closed the issue, changing the shared size calculation and adding coverage for the cases it had missed.