[PATCH] D142211: [LangRef] Require i8s to be naturally aligned

Jannik Silvanus via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 20 06:56:38 PST 2023


jsilvanus created this revision.
Herald added subscribers: jdoerfert, hiraditya.
Herald added a project: All.
jsilvanus requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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 <https://reviews.llvm.org/rGdf1a74ac3c6407d0658c46c859c4a07974af3293>) 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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D142211

Files:
  llvm/docs/LangRef.rst
  llvm/lib/IR/DataLayout.cpp
  llvm/test/Assembler/datalayout-invalid-i8-alignment.ll


Index: llvm/test/Assembler/datalayout-invalid-i8-alignment.ll
===================================================================
--- /dev/null
+++ llvm/test/Assembler/datalayout-invalid-i8-alignment.ll
@@ -0,0 +1,5 @@
+; RUN: not llvm-as %s 2>&1 | FileCheck %s
+
+; CHECK: error: Invalid ABI alignment, i8 must be naturally aligned
+
+target datalayout = "i8:16"
Index: llvm/lib/IR/DataLayout.cpp
===================================================================
--- llvm/lib/IR/DataLayout.cpp
+++ llvm/lib/IR/DataLayout.cpp
@@ -406,6 +406,9 @@
         return reportError("Invalid ABI alignment, must be a 16bit integer");
       if (ABIAlign != 0 && !isPowerOf2_64(ABIAlign))
         return reportError("Invalid ABI alignment, must be a power of 2");
+      if (Size == 8 && ABIAlign != 1)
+        return reportError(
+            "Invalid ABI alignment, i8 must be naturally aligned");
 
       // Preferred alignment.
       unsigned PrefAlign = ABIAlign;
Index: llvm/docs/LangRef.rst
===================================================================
--- llvm/docs/LangRef.rst
+++ llvm/docs/LangRef.rst
@@ -2772,6 +2772,8 @@
     This specifies the alignment for an integer type of a given bit
     ``<size>``. The value of ``<size>`` must be in the range [1,2^23).
     ``<pref>`` is optional and defaults to ``<abi>``.
+    For ``i8``, the ``<abi>`` value must equal 8,
+    that is, ``i8`` must be naturally aligned.
 ``v<size>:<abi>[:<pref>]``
     This specifies the alignment for a vector type of a given bit
     ``<size>``. The value of ``<size>`` must be in the range [1,2^23).
@@ -2839,7 +2841,7 @@
    same as the default address space.
 -  ``S0`` - natural stack alignment is unspecified
 -  ``i1:8:8`` - i1 is 8-bit (byte) aligned
--  ``i8:8:8`` - i8 is 8-bit (byte) aligned
+-  ``i8:8:8`` - i8 is 8-bit (byte) aligned as mandated
 -  ``i16:16:16`` - i16 is 16-bit aligned
 -  ``i32:32:32`` - i32 is 32-bit aligned
 -  ``i64:32:64`` - i64 has ABI alignment of 32-bits but preferred


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D142211.490822.patch
Type: text/x-patch
Size: 2006 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230120/fb068670/attachment-0001.bin>


More information about the llvm-commits mailing list