[clang] [clang][LoongArch] Align global symbol by size (PR #101309)

via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 31 02:18:45 PDT 2024


https://github.com/heiher created https://github.com/llvm/llvm-project/pull/101309

Fixes #101295

>From 34c558dda32076634d9575a32a0508e576c13d0b Mon Sep 17 00:00:00 2001
From: WANG Rui <wangrui at loongson.cn>
Date: Wed, 31 Jul 2024 17:11:56 +0800
Subject: [PATCH] [clang][LoongArch] Align global symbol by size

Fixes #101295
---
 clang/lib/Basic/Targets/LoongArch.cpp | 17 +++++++++++++++++
 clang/lib/Basic/Targets/LoongArch.h   |  3 +++
 clang/test/CodeGen/LoongArch/align.c  | 20 ++++++++++----------
 3 files changed, 30 insertions(+), 10 deletions(-)

diff --git a/clang/lib/Basic/Targets/LoongArch.cpp b/clang/lib/Basic/Targets/LoongArch.cpp
index cb3fd12c48ddb..63c2ed987442f 100644
--- a/clang/lib/Basic/Targets/LoongArch.cpp
+++ b/clang/lib/Basic/Targets/LoongArch.cpp
@@ -118,6 +118,23 @@ LoongArchTargetInfo::getGCCRegAliases() const {
   return llvm::ArrayRef(GCCRegAliases);
 }
 
+unsigned LoongArchTargetInfo::getMinGlobalAlign(uint64_t TypeSize,
+                                                bool HasNonWeakDef) const {
+  unsigned Align = TargetInfo::getMinGlobalAlign(TypeSize, HasNonWeakDef);
+
+  if (HasFeatureLASX && TypeSize >= 512) {       // TypeSize >= 64 bytes
+    Align = std::max(Align, 256u);               // align type at least 32 bytes
+  } else if (HasFeatureLSX && TypeSize >= 256) { // TypeSize >= 32 bytes
+    Align = std::max(Align, 128u);               // align type at least 16 bytes
+  } else if (TypeSize >= 64) {                   // TypeSize >= 8 bytes
+    Align = std::max(Align, 64u);                // align type at least 8 butes
+  } else if (TypeSize >= 16) {                   // TypeSize >= 2 bytes
+    Align = std::max(Align, 32u);                // align type at least 4 bytes
+  }
+
+  return Align;
+}
+
 bool LoongArchTargetInfo::validateAsmConstraint(
     const char *&Name, TargetInfo::ConstraintInfo &Info) const {
   // See the GCC definitions here:
diff --git a/clang/lib/Basic/Targets/LoongArch.h b/clang/lib/Basic/Targets/LoongArch.h
index c668ca7eca047..42aab7fe1aa49 100644
--- a/clang/lib/Basic/Targets/LoongArch.h
+++ b/clang/lib/Basic/Targets/LoongArch.h
@@ -82,6 +82,9 @@ class LLVM_LIBRARY_VISIBILITY LoongArchTargetInfo : public TargetInfo {
 
   ArrayRef<TargetInfo::GCCRegAlias> getGCCRegAliases() const override;
 
+  unsigned getMinGlobalAlign(uint64_t TypeSize,
+                             bool HasNonWeakDef) const override;
+
   bool validateAsmConstraint(const char *&Name,
                              TargetInfo::ConstraintInfo &Info) const override;
   std::string convertConstraint(const char *&Constraint) const override;
diff --git a/clang/test/CodeGen/LoongArch/align.c b/clang/test/CodeGen/LoongArch/align.c
index 1b171b74529d1..b353a98678a06 100644
--- a/clang/test/CodeGen/LoongArch/align.c
+++ b/clang/test/CodeGen/LoongArch/align.c
@@ -7,28 +7,28 @@
 #include <stdint.h>
 
 char *s1 = "1234";
-// LA32: @.str{{.*}} ={{.*}} constant [5 x i8] c"1234\00", align 1
-// LA64: @.str{{.*}} ={{.*}} constant [5 x i8] c"1234\00", align 1
+// LA32: @.str{{.*}} ={{.*}} constant [5 x i8] c"1234\00", align 4
+// LA64: @.str{{.*}} ={{.*}} constant [5 x i8] c"1234\00", align 4
 
 char *s2 = "12345678abcd";
-// LA32: @.str{{.*}} ={{.*}} constant [13 x i8] c"12345678abcd\00", align 1
-// LA64: @.str{{.*}} ={{.*}} constant [13 x i8] c"12345678abcd\00", align 1
+// LA32: @.str{{.*}} ={{.*}} constant [13 x i8] c"12345678abcd\00", align 8
+// LA64: @.str{{.*}} ={{.*}} constant [13 x i8] c"12345678abcd\00", align 8
 
 char *s3 = "123456789012345678901234567890ab";
-// LA32: @.str{{.*}} ={{.*}} constant [33 x i8] c"1234{{.*}}ab\00", align 1
-// LA64: @.str{{.*}} ={{.*}} constant [33 x i8] c"1234{{.*}}ab\00", align 1
+// LA32: @.str{{.*}} ={{.*}} constant [33 x i8] c"1234{{.*}}ab\00", align 16
+// LA64: @.str{{.*}} ={{.*}} constant [33 x i8] c"1234{{.*}}ab\00", align 16
 
 char *s4 = "123456789012345678901234567890123456789012345678901234567890abcdef";
-// LA32: @.str{{.*}} ={{.*}} constant [67 x i8] c"1234{{.*}}cdef\00", align 1
-// LA64: @.str{{.*}} ={{.*}} constant [67 x i8] c"1234{{.*}}cdef\00", align 1
+// LA32: @.str{{.*}} ={{.*}} constant [67 x i8] c"1234{{.*}}cdef\00", align 32
+// LA64: @.str{{.*}} ={{.*}} constant [67 x i8] c"1234{{.*}}cdef\00", align 32
 
 int8_t a;
 // LA32: @a ={{.*}} global i8 0, align 1
 // LA64: @a ={{.*}} global i8 0, align 1
 
 int16_t b;
-// LA32: @b ={{.*}} global i16 0, align 2
-// LA64: @b ={{.*}} global i16 0, align 2
+// LA32: @b ={{.*}} global i16 0, align 4
+// LA64: @b ={{.*}} global i16 0, align 4
 
 int32_t c;
 // LA32: @c ={{.*}} global i32 0, align 4



More information about the cfe-commits mailing list