Skip to content

Commit 34c558d

Browse files
committed
[clang][LoongArch] Align global symbol by size
Fixes #101295
1 parent f395d82 commit 34c558d

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

clang/lib/Basic/Targets/LoongArch.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,23 @@ LoongArchTargetInfo::getGCCRegAliases() const {
118118
return llvm::ArrayRef(GCCRegAliases);
119119
}
120120

121+
unsigned LoongArchTargetInfo::getMinGlobalAlign(uint64_t TypeSize,
122+
bool HasNonWeakDef) const {
123+
unsigned Align = TargetInfo::getMinGlobalAlign(TypeSize, HasNonWeakDef);
124+
125+
if (HasFeatureLASX && TypeSize >= 512) { // TypeSize >= 64 bytes
126+
Align = std::max(Align, 256u); // align type at least 32 bytes
127+
} else if (HasFeatureLSX && TypeSize >= 256) { // TypeSize >= 32 bytes
128+
Align = std::max(Align, 128u); // align type at least 16 bytes
129+
} else if (TypeSize >= 64) { // TypeSize >= 8 bytes
130+
Align = std::max(Align, 64u); // align type at least 8 butes
131+
} else if (TypeSize >= 16) { // TypeSize >= 2 bytes
132+
Align = std::max(Align, 32u); // align type at least 4 bytes
133+
}
134+
135+
return Align;
136+
}
137+
121138
bool LoongArchTargetInfo::validateAsmConstraint(
122139
const char *&Name, TargetInfo::ConstraintInfo &Info) const {
123140
// See the GCC definitions here:

clang/lib/Basic/Targets/LoongArch.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ class LLVM_LIBRARY_VISIBILITY LoongArchTargetInfo : public TargetInfo {
8282

8383
ArrayRef<TargetInfo::GCCRegAlias> getGCCRegAliases() const override;
8484

85+
unsigned getMinGlobalAlign(uint64_t TypeSize,
86+
bool HasNonWeakDef) const override;
87+
8588
bool validateAsmConstraint(const char *&Name,
8689
TargetInfo::ConstraintInfo &Info) const override;
8790
std::string convertConstraint(const char *&Constraint) const override;

clang/test/CodeGen/LoongArch/align.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,28 @@
77
#include <stdint.h>
88

99
char *s1 = "1234";
10-
// LA32: @.str{{.*}} ={{.*}} constant [5 x i8] c"1234\00", align 1
11-
// LA64: @.str{{.*}} ={{.*}} constant [5 x i8] c"1234\00", align 1
10+
// LA32: @.str{{.*}} ={{.*}} constant [5 x i8] c"1234\00", align 4
11+
// LA64: @.str{{.*}} ={{.*}} constant [5 x i8] c"1234\00", align 4
1212

1313
char *s2 = "12345678abcd";
14-
// LA32: @.str{{.*}} ={{.*}} constant [13 x i8] c"12345678abcd\00", align 1
15-
// LA64: @.str{{.*}} ={{.*}} constant [13 x i8] c"12345678abcd\00", align 1
14+
// LA32: @.str{{.*}} ={{.*}} constant [13 x i8] c"12345678abcd\00", align 8
15+
// LA64: @.str{{.*}} ={{.*}} constant [13 x i8] c"12345678abcd\00", align 8
1616

1717
char *s3 = "123456789012345678901234567890ab";
18-
// LA32: @.str{{.*}} ={{.*}} constant [33 x i8] c"1234{{.*}}ab\00", align 1
19-
// LA64: @.str{{.*}} ={{.*}} constant [33 x i8] c"1234{{.*}}ab\00", align 1
18+
// LA32: @.str{{.*}} ={{.*}} constant [33 x i8] c"1234{{.*}}ab\00", align 16
19+
// LA64: @.str{{.*}} ={{.*}} constant [33 x i8] c"1234{{.*}}ab\00", align 16
2020

2121
char *s4 = "123456789012345678901234567890123456789012345678901234567890abcdef";
22-
// LA32: @.str{{.*}} ={{.*}} constant [67 x i8] c"1234{{.*}}cdef\00", align 1
23-
// LA64: @.str{{.*}} ={{.*}} constant [67 x i8] c"1234{{.*}}cdef\00", align 1
22+
// LA32: @.str{{.*}} ={{.*}} constant [67 x i8] c"1234{{.*}}cdef\00", align 32
23+
// LA64: @.str{{.*}} ={{.*}} constant [67 x i8] c"1234{{.*}}cdef\00", align 32
2424

2525
int8_t a;
2626
// LA32: @a ={{.*}} global i8 0, align 1
2727
// LA64: @a ={{.*}} global i8 0, align 1
2828

2929
int16_t b;
30-
// LA32: @b ={{.*}} global i16 0, align 2
31-
// LA64: @b ={{.*}} global i16 0, align 2
30+
// LA32: @b ={{.*}} global i16 0, align 4
31+
// LA64: @b ={{.*}} global i16 0, align 4
3232

3333
int32_t c;
3434
// LA32: @c ={{.*}} global i32 0, align 4

0 commit comments

Comments
 (0)