Issue 240334
Summary [MLIR][VectorToLLVM] Default access alignment uses preferred rather than ABI element alignment
Labels mlir
Assignees
Reporter jonascloud
    ## Summary

When no explicit alignment is specified, `getMemRefAlignment()` uses the **preferred alignment of the converted memref element type** to annotate lowered LLVM memory operations.

Preferred alignment can exceed ABI alignment. For example, `i16:16:32` specifies a two-byte ABI alignment and a four-byte preferred alignment. An in-bounds access at an odd i16 index can therefore receive an invalid `align 4` guarantee, even when the buffer base is strongly aligned.

LLVM defines overstated load alignment as undefined behavior. No native runtime miscalculation has been demonstrated here; the observed defect is the invalid LLVM IR guarantee.

## Reproducer

Reproduced with IREE 3.13.0:

- IREE commit: `89ece87b89852100c0a4ac3d515e14842d090967`
- LLVM submodule: `32d702efa29d0fdd02cff7b79dea8fdbbcf059d4`

The reproducer contains no IREE-specific operations, model weights, or BF16 conversion:

```mlir
module {
  func.func private @odd_element(%weights: memref<2xi16>)
      -> vector<1xi16> {
    %aligned = memref.assume_alignment %weights, 64 : memref<2xi16>
    %_one_ = arith.constant 1 : index
    %value = vector.load %aligned[%one]
        : memref<2xi16>, vector<1xi16>
    return %value : vector<1xi16>
 }
}
```

Run using IREE's combined conversion pass:

```sh
iree-opt repro.mlir \
 --iree-convert-to-llvm='target-data-layout=e-i16:16:32'
```

Relevant output:

```mlir
%2 = llvm.getelementptr %arg1[1]
    : (!llvm.ptr) -> !llvm.ptr, i16
%3 = llvm.load %2 <alignment = 4>
    : !llvm.ptr -> vector<1xi16>
```

### Expected behavior

The accessed address is `B + 2`, where `B` is 64-byte aligned. It is therefore two-byte aligned, but **not** four-byte aligned. The emitted load should specify `alignment = 2`.

### Control

Changing only the preferred alignment:

```sh
iree-opt repro.mlir \
 --iree-convert-to-llvm='target-data-layout=e-i16:16:16'
```

produces the same GEP with `alignment = 2`.

These commands were tested with `iree-opt`; a standalone upstream `mlir-opt` invocation has not been verified.

## Suspected cause

In [`ConvertVectorToLLVM.cpp`](https://github.com/llvm/llvm-project/blob/88c3d5a163cf6d4caf86302a6c42ccf415c47841/mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp#L86-L120), `getMemRefAlignment()` queries:

```cpp
.getPreferredAlignment(elementTy, typeConverter.getDataLayout())
```

This path is selected with the default `useVectorAlignment=false` when the operation has no explicit alignment. It does not account for the access index.

The same fallback is present in the linked LLVM main snapshot, `88c3d5a163cf`, inspected on 2026-11-09. That is a source inspection, not execution of a newer compiler binary.

## Suggested correction

For the default scalar-element path, use the **converted memref element's ABI alignment**, rather than its preferred alignment.

- Preserve explicit operation alignment, including weaker guarantees.
- Keep the opt-in vector-alignment policy separate.
- Do not simply omit LLVM alignment: the loaded vector's ABI alignment can itself exceed the guaranteed element alignment.

Regression coverage should use a layout where ABI and preferred alignment differ, exercise the shared load/store and masked/gather/scatter paths, and retain explicit-alignment precedence checks. No compiler patch has been built or tested yet.

## Related history

- [D80734](https://reviews.llvm.org/D80734) corrected excessive vector alignment by switching to element alignment, but retained the preferred-alignment query.
- [D100444](https://reviews.llvm.org/D100444#3162527) exposed a separate IREE miscompile from propagating base alignment without considering indices. Its revert left this fallback intact.

The original design describes a natural scalar-alignment baseline, but does not clearly distinguish ABI from preferred alignment. Clarifying that default contract alongside the correction would be helpful.

---
AI assistance was used for investigation and drafting.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to