[clang] [CIR] Use target-specific maximum for vector alignment (PR #242040)
Adam Smith via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 12 15:46:53 PST 2026
================
@@ -1607,8 +1620,49 @@ uint64_t
cir::VectorType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
::mlir::DataLayoutEntryListRef params) const {
// This hook answers in bytes, not bits.
- return llvm::PowerOf2Ceil(llvm::divideCeil(
- dataLayout.getTypeSizeInBits(*this).getKnownMinValue(), uint64_t{8}));
+ uint64_t byteSize = llvm::divideCeil(
+ dataLayout.getTypeSizeInBits(*this).getKnownMinValue(), uint64_t{8});
+ uint64_t align = llvm::PowerOf2Ceil(byteSize);
+ uint64_t maxAlignBits = getVectorMaxAlignBits(params);
+ if (maxAlignBits)
+ align = std::min(align, maxAlignBits / 8);
----------------
adams381 wrote:
This changes record layout on AArch64. `CIRRecordLowering::determinePacked` and the padding logic use this hook as the alignment LLVM will give the lowered type, but the AArch64 LLVM data layout has no vector entries, so LLVM still aligns `<8 x float>` to 32 bytes. With this patch:
```c
typedef float v8f32 __attribute__((vector_size(32)));
typedef float v9f32 __attribute__((ext_vector_type(9)));
struct S { int i; v8f32 v; };
struct T { v9f32 a; int b; };
float readS(struct S *p) { return p->v[0]; }
int readT(struct T *p) { return p->b; }
```
The LLVM IR lowered from CIR has `%struct.S = type { i32, <8 x float> }` and `%struct.T = type { <9 x float>, [16 x i8], i32 }`. At -O1, `readS` loads from offset 32 and `readT` from offset 80. Classic codegen, and CIR before this patch, emit `<{ i32, [12 x i8], <8 x float> }>` and `<{ <9 x float>, i32, [12 x i8] }>` and load from 16 and 64, matching `offsetof`.
`T` is a second effect of the cap. `ASTContext` pads a non-power-of-two vector to its natural alignment before capping, so `sizeof(v9f32)` is 64, while the CIR alloc size of `!cir.vector<9 x !cir.float>` drops to `alignTo(36, 16) = 48`. LLVM still sizes `<9 x float>` at 64, so the 16 bytes of padding CIR inserts to reach offset 64 put `b` at 80.
https://github.com/llvm/llvm-project/pull/242040
More information about the cfe-commits
mailing list