[PATCH] D84992: [clang codegen] Use IR "align" attribute for static array arguments.

Eli Friedman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 30 17:38:59 PDT 2020


efriedma created this revision.
efriedma added a reviewer: rjmccall.
Herald added a project: clang.
efriedma requested review of this revision.

Without the "align" attribute, marking the argument dereferenceable is basically useless.  See also D80166 <https://reviews.llvm.org/D80166>.

Fixes https://bugs.llvm.org/show_bug.cgi?id=46876 .


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D84992

Files:
  clang/lib/CodeGen/CGCall.cpp
  clang/test/CodeGen/vla.c


Index: clang/test/CodeGen/vla.c
===================================================================
--- clang/test/CodeGen/vla.c
+++ clang/test/CodeGen/vla.c
@@ -200,13 +200,13 @@
 // Make sure we emit dereferenceable or nonnull when the static keyword is
 // provided.
 void test8(int a[static 3]) { }
-// CHECK: define void @test8(i32* dereferenceable(12) %a)
+// CHECK: define void @test8(i32* align 4 dereferenceable(12) %a)
 
 void test9(int n, int a[static n]) { }
-// NULL-INVALID: define void @test9(i32 %n, i32* nonnull %a)
-// NULL-VALID: define void @test9(i32 %n, i32* %a)
+// NULL-INVALID: define void @test9(i32 %n, i32* nonnull align 4 %a)
+// NULL-VALID: define void @test9(i32 %n, i32* align 4 %a)
 
 // Make sure a zero-sized static array extent is still required to be nonnull.
 void test10(int a[static 0]) {}
-// NULL-INVALID: define void @test10(i32* nonnull %a)
-// NULL-VALID: define void @test10(i32* %a)
+// NULL-INVALID: define void @test10(i32* nonnull align 4 %a)
+// NULL-VALID: define void @test10(i32* align 4 %a)
Index: clang/lib/CodeGen/CGCall.cpp
===================================================================
--- clang/lib/CodeGen/CGCall.cpp
+++ clang/lib/CodeGen/CGCall.cpp
@@ -2499,6 +2499,9 @@
             // bytes).
             if (ArrTy->getSizeModifier() == ArrayType::Static) {
               QualType ETy = ArrTy->getElementType();
+              llvm::Align Alignment =
+                  CGM.getNaturalTypeAlignment(ETy).getAsAlign();
+              AI->addAttrs(llvm::AttrBuilder().addAlignmentAttr(Alignment));
               uint64_t ArrSize = ArrTy->getSize().getZExtValue();
               if (!ETy->isIncompleteType() && ETy->isConstantSizeType() &&
                   ArrSize) {
@@ -2518,10 +2521,15 @@
             // For C99 VLAs with the static keyword, we don't know the size so
             // we can't use the dereferenceable attribute, but in addrspace(0)
             // we know that it must be nonnull.
-            if (ArrTy->getSizeModifier() == VariableArrayType::Static &&
-                !getContext().getTargetAddressSpace(ArrTy->getElementType()) &&
-                !CGM.getCodeGenOpts().NullPointerIsValid)
-              AI->addAttr(llvm::Attribute::NonNull);
+            if (ArrTy->getSizeModifier() == VariableArrayType::Static) {
+              QualType ETy = ArrTy->getElementType();
+              llvm::Align Alignment =
+                  CGM.getNaturalTypeAlignment(ETy).getAsAlign();
+              AI->addAttrs(llvm::AttrBuilder().addAlignmentAttr(Alignment));
+              if (!getContext().getTargetAddressSpace(ETy) &&
+                  !CGM.getCodeGenOpts().NullPointerIsValid)
+                AI->addAttr(llvm::Attribute::NonNull);
+            }
           }
 
           // Set `align` attribute if any.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D84992.282086.patch
Type: text/x-patch
Size: 2806 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200731/7ee3ae0b/attachment.bin>


More information about the cfe-commits mailing list