Skip to content

Commit c0001b5

Browse files
committed
Also handle weak symbols
1 parent de67383 commit c0001b5

File tree

14 files changed

+37
-30
lines changed

14 files changed

+37
-30
lines changed

clang/include/clang/AST/ASTContext.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2406,11 +2406,11 @@ class ASTContext : public RefCountedBase<ASTContext> {
24062406

24072407
/// Return the alignment in bits that should be given to a
24082408
/// global variable with type \p T.
2409-
unsigned getAlignOfGlobalVar(QualType T, bool HasDef) const;
2409+
unsigned getAlignOfGlobalVar(QualType T, const VarDecl *VD) const;
24102410

24112411
/// Return the alignment in characters that should be given to a
24122412
/// global variable with type \p T.
2413-
CharUnits getAlignOfGlobalVarInChars(QualType T, bool HasDef) const;
2413+
CharUnits getAlignOfGlobalVarInChars(QualType T, const VarDecl *VD) const;
24142414

24152415
/// Return a conservative estimate of the alignment of the specified
24162416
/// decl \p D.

clang/include/clang/Basic/TargetInfo.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class DiagnosticsEngine;
5050
class LangOptions;
5151
class CodeGenOptions;
5252
class MacroBuilder;
53+
class VarDecl;
5354

5455
/// Contains information gathered from parsing the contents of TargetAttr.
5556
struct ParsedTargetAttr {
@@ -704,11 +705,9 @@ class TargetInfo : public TransferrableTargetInfo,
704705
}
705706

706707
/// getMinGlobalAlign - Return the minimum alignment of a global variable,
707-
/// unless its alignment is explicitly reduced via attributes. It may be
708-
/// that an external symbol needs to be considered unaligned (like
709-
/// artificial symbols created from a linker script). If \param HasDef is
710-
/// false, this symbol does not have a definition and is external.
711-
virtual unsigned getMinGlobalAlign(uint64_t Size, bool HasDef) const {
708+
/// unless its alignment is explicitly reduced via attributes. If \param VD
709+
/// is non-null, it may be used to examine the specific variable's attributes.
710+
virtual unsigned getMinGlobalAlign(uint64_t Size, const VarDecl *VD) const {
712711
return MinGlobalAlign;
713712
}
714713

clang/lib/AST/ASTContext.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,8 +1681,7 @@ CharUnits ASTContext::getDeclAlign(const Decl *D, bool ForAlignof) const {
16811681
if (VD->hasGlobalStorage() && !ForAlignof) {
16821682
uint64_t TypeSize =
16831683
!BaseT->isIncompleteType() ? getTypeSize(T.getTypePtr()) : 0;
1684-
Align = std::max(Align, getTargetInfo().getMinGlobalAlign(
1685-
TypeSize, VD->hasDefinition()));
1684+
Align = std::max(Align, getTargetInfo().getMinGlobalAlign(TypeSize, VD));
16861685
}
16871686

16881687
// Fields can be subject to extra alignment constraints, like if
@@ -2503,19 +2502,18 @@ unsigned ASTContext::getTargetDefaultAlignForAttributeAligned() const {
25032502
}
25042503

25052504
/// getAlignOfGlobalVar - Return the alignment in bits that should be given
2506-
/// to a global variable of the specified type (see comment for
2507-
/// getMinGlobalAlign about HasDef).
2508-
unsigned ASTContext::getAlignOfGlobalVar(QualType T, bool HasDef) const {
2505+
/// to a global variable of the specified type.
2506+
unsigned ASTContext::getAlignOfGlobalVar(QualType T, const VarDecl *VD) const {
25092507
uint64_t TypeSize = getTypeSize(T.getTypePtr());
25102508
return std::max(getPreferredTypeAlign(T),
2511-
getTargetInfo().getMinGlobalAlign(TypeSize, HasDef));
2509+
getTargetInfo().getMinGlobalAlign(TypeSize, VD));
25122510
}
25132511

25142512
/// getAlignOfGlobalVarInChars - Return the alignment in characters that
25152513
/// should be given to a global variable of the specified type.
25162514
CharUnits ASTContext::getAlignOfGlobalVarInChars(QualType T,
2517-
bool HasDef) const {
2518-
return toCharUnitsFromBits(getAlignOfGlobalVar(T, HasDef));
2515+
const VarDecl *VD) const {
2516+
return toCharUnitsFromBits(getAlignOfGlobalVar(T, VD));
25192517
}
25202518

25212519
CharUnits ASTContext::getOffsetOfBaseWithVBPtr(const CXXRecordDecl *RD) const {

clang/lib/Basic/Targets/AArch64.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,8 +1518,8 @@ MicrosoftARM64TargetInfo::getCallingConvKind(bool ClangABICompat4) const {
15181518
}
15191519

15201520
unsigned MicrosoftARM64TargetInfo::getMinGlobalAlign(uint64_t TypeSize,
1521-
bool HasDef) const {
1522-
unsigned Align = WindowsARM64TargetInfo::getMinGlobalAlign(TypeSize, HasDef);
1521+
const VarDecl *VD) const {
1522+
unsigned Align = WindowsARM64TargetInfo::getMinGlobalAlign(TypeSize, VD);
15231523

15241524
// MSVC does size based alignment for arm64 based on alignment section in
15251525
// below document, replicate that to keep alignment consistent with object

clang/lib/Basic/Targets/AArch64.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ class LLVM_LIBRARY_VISIBILITY MicrosoftARM64TargetInfo
236236
TargetInfo::CallingConvKind
237237
getCallingConvKind(bool ClangABICompat4) const override;
238238

239-
unsigned getMinGlobalAlign(uint64_t TypeSize, bool HasDef) const override;
239+
unsigned getMinGlobalAlign(uint64_t TypeSize,
240+
const VarDecl *VD) const override;
240241
};
241242

242243
// ARM64 MinGW target

clang/lib/Basic/Targets/CSKY.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@ bool CSKYTargetInfo::validateAsmConstraint(
308308
}
309309
}
310310

311-
unsigned CSKYTargetInfo::getMinGlobalAlign(uint64_t Size, bool HasDef) const {
311+
unsigned CSKYTargetInfo::getMinGlobalAlign(uint64_t Size,
312+
const VarDecl *VD) const {
312313
if (Size >= 32)
313314
return 32;
314315
return 0;

clang/lib/Basic/Targets/CSKY.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class LLVM_LIBRARY_VISIBILITY CSKYTargetInfo : public TargetInfo {
7171

7272
bool isValidCPUName(StringRef Name) const override;
7373

74-
unsigned getMinGlobalAlign(uint64_t, bool) const override;
74+
unsigned getMinGlobalAlign(uint64_t, const VarDecl *) const override;
7575

7676
ArrayRef<Builtin::Info> getTargetBuiltins() const override;
7777

clang/lib/Basic/Targets/NVPTX.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ NVPTXTargetInfo::NVPTXTargetInfo(const llvm::Triple &Triple,
116116
LongLongWidth = HostTarget->getLongLongWidth();
117117
LongLongAlign = HostTarget->getLongLongAlign();
118118
MinGlobalAlign = HostTarget->getMinGlobalAlign(/* TypeSize = */ 0,
119-
/* HasDef = */ true);
119+
/* VD = */ nullptr);
120120
NewAlign = HostTarget->getNewAlign();
121121
DefaultAlignForAttributeAligned =
122122
HostTarget->getDefaultAlignForAttributeAligned();

clang/lib/Basic/Targets/SPIR.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class LLVM_LIBRARY_VISIBILITY BaseSPIRTargetInfo : public TargetInfo {
125125
LongLongWidth = HostTarget->getLongLongWidth();
126126
LongLongAlign = HostTarget->getLongLongAlign();
127127
MinGlobalAlign = HostTarget->getMinGlobalAlign(/* TypeSize = */ 0,
128-
/* HasDef = */ true);
128+
/* VD = */ nullptr);
129129
NewAlign = HostTarget->getNewAlign();
130130
DefaultAlignForAttributeAligned =
131131
HostTarget->getDefaultAlignForAttributeAligned();

clang/lib/Basic/Targets/SystemZ.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "SystemZ.h"
14+
#include "clang/AST/Decl.h"
1415
#include "clang/Basic/Builtins.h"
1516
#include "clang/Basic/LangOptions.h"
1617
#include "clang/Basic/MacroBuilder.h"
@@ -139,10 +140,10 @@ bool SystemZTargetInfo::hasFeature(StringRef Feature) const {
139140
}
140141

141142
unsigned SystemZTargetInfo::getMinGlobalAlign(uint64_t Size,
142-
bool HasDef) const {
143-
// Don't enforce the minimum alignment on an external symbol if
143+
const VarDecl *VD) const {
144+
// Don't enforce the minimum alignment on an external or weak symbol if
144145
// -munaligned-symbols is passed.
145-
if (UnalignedSymbols && !HasDef)
146+
if (UnalignedSymbols && VD && (!VD->hasDefinition() || VD->isWeak()))
146147
return 0;
147148

148149
return MinGlobalAlign;

0 commit comments

Comments
 (0)