[clang] 1d0972f - [Sema] Introduce MaximumAlignment value, to be used instead of magical constants

Roman Lebedev via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 24 06:50:17 PST 2020


Author: Roman Lebedev
Date: 2020-01-24T17:49:17+03:00
New Revision: 1d0972ff5eab544579c400ee674d5c9c60391ee1

URL: https://github.com/llvm/llvm-project/commit/1d0972ff5eab544579c400ee674d5c9c60391ee1
DIFF: https://github.com/llvm/llvm-project/commit/1d0972ff5eab544579c400ee674d5c9c60391ee1.diff

LOG: [Sema] Introduce MaximumAlignment value, to be used instead of magical constants

There is llvm::Value::MaximumAlignment, which is numerically
equivalent to these constants, but we can't use it directly
because we can't include llvm IR headers in clang Sema.
So instead, copy-paste the constant, and fixup the places to use it.

This was initially reviewed in https://reviews.llvm.org/D72998

Added: 
    

Modified: 
    clang/include/clang/Sema/Sema.h
    clang/lib/Sema/SemaChecking.cpp
    clang/lib/Sema/SemaDeclAttr.cpp
    clang/test/CXX/drs/dr6xx.cpp
    clang/test/Sema/attr-aligned.c

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index fae1ade80ca9..92d964d6603d 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -372,6 +372,15 @@ class Sema final {
                                       QualType ResultTy,
                                       ArrayRef<QualType> Args);
 
+  /// The maximum alignment, same as in llvm::Value. We duplicate them here
+  /// because that allows us not to duplicate the constants in clang code,
+  /// which we must to since we can't directly use the llvm constants.
+  ///
+  /// This is the greatest alignment value supported by load, store, and alloca
+  /// instructions, and global values.
+  static const unsigned MaxAlignmentExponent = 29;
+  static const unsigned MaximumAlignment = 1u << MaxAlignmentExponent;
+
 public:
   typedef OpaquePtr<DeclGroupRef> DeclGroupPtrTy;
   typedef OpaquePtr<TemplateName> TemplateTy;

diff  --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 4485539f3f1c..186f2b5b990c 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -3665,11 +3665,9 @@ void Sema::checkCall(NamedDecl *FDecl, const FunctionProtoType *Proto,
           return;
         }
 
-        // Alignment calculations can wrap around if it's greater than 2**29.
-        unsigned MaximumAlignment = 536870912;
-        if (I > MaximumAlignment)
+        if (I > Sema::MaximumAlignment)
           Diag(Arg->getExprLoc(), diag::warn_assume_aligned_too_great)
-              << Arg->getSourceRange() << MaximumAlignment;
+              << Arg->getSourceRange() << Sema::MaximumAlignment;
       }
     }
   }
@@ -5394,11 +5392,9 @@ bool Sema::SemaBuiltinAssumeAligned(CallExpr *TheCall) {
       return Diag(TheCall->getBeginLoc(), diag::err_alignment_not_power_of_two)
              << Arg->getSourceRange();
 
-    // Alignment calculations can wrap around if it's greater than 2**29.
-    unsigned MaximumAlignment = 536870912;
-    if (Result > MaximumAlignment)
+    if (Result > Sema::MaximumAlignment)
       Diag(TheCall->getBeginLoc(), diag::warn_assume_aligned_too_great)
-          << Arg->getSourceRange() << MaximumAlignment;
+          << Arg->getSourceRange() << Sema::MaximumAlignment;
   }
 
   if (NumArgs > 2) {

diff  --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 8ad981f89b6e..e097ec0f1c59 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -1626,11 +1626,9 @@ void Sema::AddAssumeAlignedAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E,
       return;
     }
 
-    // Alignment calculations can wrap around if it's greater than 2**29.
-    unsigned MaximumAlignment = 536870912;
-    if (I > MaximumAlignment)
+    if (I > Sema::MaximumAlignment)
       Diag(CI.getLoc(), diag::warn_assume_aligned_too_great)
-          << CI.getRange() << MaximumAlignment;
+          << CI.getRange() << Sema::MaximumAlignment;
   }
 
   if (OE) {
@@ -3816,13 +3814,12 @@ void Sema::AddAlignedAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E,
     }
   }
 
-  // Alignment calculations can wrap around if it's greater than 2**28.
-  unsigned MaxValidAlignment =
-      Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192
-                                                              : 268435456;
-  if (AlignVal > MaxValidAlignment) {
+  unsigned MaximumAlignment = Sema::MaximumAlignment;
+  if (Context.getTargetInfo().getTriple().isOSBinFormatCOFF())
+    MaximumAlignment = std::min(MaximumAlignment, 8192u);
+  if (AlignVal > MaximumAlignment) {
     Diag(AttrLoc, diag::err_attribute_aligned_too_great)
-        << MaxValidAlignment << E->getSourceRange();
+        << MaximumAlignment << E->getSourceRange();
     return;
   }
 

diff  --git a/clang/test/CXX/drs/dr6xx.cpp b/clang/test/CXX/drs/dr6xx.cpp
index 6ff162545826..7a0adb5fe406 100644
--- a/clang/test/CXX/drs/dr6xx.cpp
+++ b/clang/test/CXX/drs/dr6xx.cpp
@@ -551,9 +551,9 @@ namespace dr648 { // dr648: yes
 
 #if __cplusplus >= 201103L
 namespace dr649 { // dr649: yes
-  alignas(0x20000000) int n; // expected-error {{requested alignment}}
-  struct alignas(0x20000000) X {}; // expected-error {{requested alignment}}
-  struct Y { int n alignas(0x20000000); }; // expected-error {{requested alignment}}
+  alignas(0x40000000) int n; // expected-error {{requested alignment}}1
+  struct alignas(0x40000000) X {}; // expected-error {{requested alignment}}
+  struct Y { int n alignas(0x40000000); }; // expected-error {{requested alignment}}
   struct alignas(256) Z {};
   // This part is superseded by dr2130 and eventually by aligned allocation support.
   auto *p = new Z;

diff  --git a/clang/test/Sema/attr-aligned.c b/clang/test/Sema/attr-aligned.c
index b8d2fc6863d2..1d65ef41b810 100644
--- a/clang/test/Sema/attr-aligned.c
+++ b/clang/test/Sema/attr-aligned.c
@@ -1,14 +1,15 @@
 // RUN: %clang_cc1 -triple i386-apple-darwin9 -fsyntax-only -verify %s
 
 int x __attribute__((aligned(3))); // expected-error {{requested alignment is not a power of 2}}
-int y __attribute__((aligned(1 << 29))); // expected-error {{requested alignment must be 268435456 bytes or smaller}}
+int y __attribute__((aligned(1 << 30))); // expected-error {{requested alignment must be 536870912 bytes or smaller}}
 
 // PR26444
+int y __attribute__((aligned(1 << 29)));
 int y __attribute__((aligned(1 << 28)));
 
 // PR3254
 short g0[3] __attribute__((aligned));
-short g0_chk[__alignof__(g0) == 16 ? 1 : -1]; 
+short g0_chk[__alignof__(g0) == 16 ? 1 : -1];
 
 // <rdar://problem/6840045>
 typedef char ueber_aligned_char __attribute__((aligned(8)));


        


More information about the cfe-commits mailing list