Skip to content

Commit 4dbf3f2

Browse files
committed
[LangRef] Require i8s to be naturally aligned
It is widely assumed that i8 is naturally aligned (i8:8), and that hence i8s can be used to access arbitrary bytes. As discussed in https://discourse.llvm.org/t/status-of-overaligned-i8, this patch makes this assumption explicit, by documenting it in the LangRef, and enforcing it when parsing a data layout string. Historically, there have been data layouts that violate this requirement, notably the old DXIL data layout that aligns i8 to 32 bits. A previous patch (df1a74a) enabled importing modules with invalid data layouts using override callbacks. Users who wish to continue importing modules with overaligned i8s (e.g. DXIL) thus need to provide a data layout override callback that fixes the data layout, at minimum by setting natural alignment for i8. Any further adjustments to the module (e.g. adding padding bytes if necessary) need to be done after module import. In the case of DXIL, this should not be necessary, because i8 usage in DXIL is very limited and its alignment actually does not matter, see https://github.com/microsoft/DirectXShaderCompiler/blob/main/docs/DXIL.rst#primitive-types Differential Revision: https://reviews.llvm.org/D142211
1 parent f341807 commit 4dbf3f2

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

llvm/docs/LangRef.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2772,6 +2772,8 @@ as follows:
27722772
This specifies the alignment for an integer type of a given bit
27732773
``<size>``. The value of ``<size>`` must be in the range [1,2^23).
27742774
``<pref>`` is optional and defaults to ``<abi>``.
2775+
For ``i8``, the ``<abi>`` value must equal 8,
2776+
that is, ``i8`` must be naturally aligned.
27752777
``v<size>:<abi>[:<pref>]``
27762778
This specifies the alignment for a vector type of a given bit
27772779
``<size>``. The value of ``<size>`` must be in the range [1,2^23).
@@ -2839,7 +2841,7 @@ specifications are given in this list:
28392841
same as the default address space.
28402842
- ``S0`` - natural stack alignment is unspecified
28412843
- ``i1:8:8`` - i1 is 8-bit (byte) aligned
2842-
- ``i8:8:8`` - i8 is 8-bit (byte) aligned
2844+
- ``i8:8:8`` - i8 is 8-bit (byte) aligned as mandated
28432845
- ``i16:16:16`` - i16 is 16-bit aligned
28442846
- ``i32:32:32`` - i32 is 32-bit aligned
28452847
- ``i64:32:64`` - i64 has ABI alignment of 32-bits but preferred

llvm/lib/IR/DataLayout.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,9 @@ Error DataLayout::parseSpecifier(StringRef Desc) {
406406
return reportError("Invalid ABI alignment, must be a 16bit integer");
407407
if (ABIAlign != 0 && !isPowerOf2_64(ABIAlign))
408408
return reportError("Invalid ABI alignment, must be a power of 2");
409+
if (AlignType == INTEGER_ALIGN && Size == 8 && ABIAlign != 1)
410+
return reportError(
411+
"Invalid ABI alignment, i8 must be naturally aligned");
409412

410413
// Preferred alignment.
411414
unsigned PrefAlign = ABIAlign;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
; RUN: not llvm-as %s 2>&1 | FileCheck %s
2+
3+
; CHECK: error: Invalid ABI alignment, i8 must be naturally aligned
4+
5+
target datalayout = "i8:16"

0 commit comments

Comments
 (0)