Skip to content

Commit 7df79ab

Browse files
committed
[clang] TargetInfo hook for unaligned bitfields (#65742)
Promote ARM & AArch64's HasUnaligned to TargetInfo and set for all targets.
1 parent a8ca4ab commit 7df79ab

File tree

14 files changed

+34
-8
lines changed

14 files changed

+34
-8
lines changed

clang/include/clang/Basic/TargetInfo.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ class TargetInfo : public TransferrableTargetInfo,
267267
LLVM_PREFERRED_TYPE(bool)
268268
unsigned AllowAMDGPUUnsafeFPAtomics : 1;
269269

270+
LLVM_PREFERRED_TYPE(bool)
271+
unsigned HasUnalignedAccess : 1;
272+
270273
unsigned ARMCDECoprocMask : 8;
271274

272275
unsigned MaxOpenCLWorkGroupSize;
@@ -859,6 +862,18 @@ class TargetInfo : public TransferrableTargetInfo,
859862
return PointerWidth;
860863
}
861864

865+
/// Return true iff unaligned accesses are a single instruction (rather than
866+
/// a synthesized sequence).
867+
bool hasUnalignedAccess() const { return HasUnalignedAccess; }
868+
869+
/// Return true iff unaligned accesses are cheap. This affects placement and
870+
/// size of bitfield loads/stores. (Not the ABI-mandated placement of
871+
/// the bitfields themselves.)
872+
bool hasCheapUnalignedBitFieldAccess() const {
873+
// Simply forward to the unaligned access getter.
874+
return hasUnalignedAccess();
875+
}
876+
862877
/// \brief Returns the default value of the __USER_LABEL_PREFIX__ macro,
863878
/// which is the prefix given to user symbols by default.
864879
///

clang/lib/Basic/TargetInfo.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ TargetInfo::TargetInfo(const llvm::Triple &T) : Triple(T) {
157157
HasAArch64SVETypes = false;
158158
HasRISCVVTypes = false;
159159
AllowAMDGPUUnsafeFPAtomics = false;
160+
HasUnalignedAccess = false;
160161
ARMCDECoprocMask = 0;
161162

162163
// Default to no types using fpret.

clang/lib/Basic/Targets/AArch64.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ AArch64TargetInfo::AArch64TargetInfo(const llvm::Triple &Triple,
188188
assert(UseBitFieldTypeAlignment && "bitfields affect type alignment");
189189
UseZeroLengthBitfieldAlignment = true;
190190

191+
HasUnalignedAccess = true;
192+
191193
// AArch64 targets default to using the ARM C++ ABI.
192194
TheCXXABI.set(TargetCXXABI::GenericAArch64);
193195

@@ -496,7 +498,7 @@ void AArch64TargetInfo::getTargetDefines(const LangOptions &Opts,
496498
if (HasPAuthLR)
497499
Builder.defineMacro("__ARM_FEATURE_PAUTH_LR", "1");
498500

499-
if (HasUnaligned)
501+
if (HasUnalignedAccess)
500502
Builder.defineMacro("__ARM_FEATURE_UNALIGNED", "1");
501503

502504
if ((FPU & NeonMode) && HasFullFP16)
@@ -921,7 +923,8 @@ bool AArch64TargetInfo::handleTargetFeatures(std::vector<std::string> &Features,
921923
HasSM4 = true;
922924
}
923925
if (Feature == "+strict-align")
924-
HasUnaligned = false;
926+
HasUnalignedAccess = false;
927+
925928
// All predecessor archs are added but select the latest one for ArchKind.
926929
if (Feature == "+v8a" && ArchInfo->Version < llvm::AArch64::ARMV8A.Version)
927930
ArchInfo = &llvm::AArch64::ARMV8A;

clang/lib/Basic/Targets/AArch64.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ class LLVM_LIBRARY_VISIBILITY AArch64TargetInfo : public TargetInfo {
3838
bool HasSHA2 = false;
3939
bool HasSHA3 = false;
4040
bool HasSM4 = false;
41-
bool HasUnaligned = true;
4241
bool HasFullFP16 = false;
4342
bool HasDotProd = false;
4443
bool HasFP16FML = false;

clang/lib/Basic/Targets/ARM.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ bool ARMTargetInfo::handleTargetFeatures(std::vector<std::string> &Features,
509509
SHA2 = 0;
510510
AES = 0;
511511
DSP = 0;
512-
Unaligned = 1;
512+
HasUnalignedAccess = true;
513513
SoftFloat = false;
514514
// Note that SoftFloatABI is initialized in our constructor.
515515
HWDiv = 0;
@@ -576,7 +576,7 @@ bool ARMTargetInfo::handleTargetFeatures(std::vector<std::string> &Features,
576576
return false;
577577
}
578578
} else if (Feature == "+strict-align") {
579-
Unaligned = 0;
579+
HasUnalignedAccess = false;
580580
} else if (Feature == "+fp16") {
581581
HW_FP |= HW_FP_HP;
582582
} else if (Feature == "+fullfp16") {
@@ -785,7 +785,7 @@ void ARMTargetInfo::getTargetDefines(const LangOptions &Opts,
785785
Builder.defineMacro("__ARM_ARCH_PROFILE", "'" + CPUProfile + "'");
786786

787787
// ACLE 6.4.3 Unaligned access supported in hardware
788-
if (Unaligned)
788+
if (HasUnalignedAccess)
789789
Builder.defineMacro("__ARM_FEATURE_UNALIGNED", "1");
790790

791791
// ACLE 6.4.4 LDREX/STREX

clang/lib/Basic/Targets/ARM.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@ class LLVM_LIBRARY_VISIBILITY ARMTargetInfo : public TargetInfo {
8888
LLVM_PREFERRED_TYPE(bool)
8989
unsigned DSP : 1;
9090
LLVM_PREFERRED_TYPE(bool)
91-
unsigned Unaligned : 1;
92-
LLVM_PREFERRED_TYPE(bool)
9391
unsigned DotProd : 1;
9492
LLVM_PREFERRED_TYPE(bool)
9593
unsigned HasMatMul : 1;

clang/lib/Basic/Targets/LoongArch.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,8 @@ bool LoongArchTargetInfo::handleTargetFeatures(
285285
HasFeatureLSX = true;
286286
else if (Feature == "+lasx")
287287
HasFeatureLASX = true;
288+
else if (Feature == "-ual")
289+
HasUnalignedAccess = false;
288290
}
289291
return true;
290292
}

clang/lib/Basic/Targets/LoongArch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ class LLVM_LIBRARY_VISIBILITY LoongArch64TargetInfo
132132
: LoongArchTargetInfo(Triple, Opts) {
133133
LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
134134
IntMaxType = Int64Type = SignedLong;
135+
HasUnalignedAccess = true;
135136
resetDataLayout("e-m:e-p:64:64-i64:64-i128:128-n64-S128");
136137
// TODO: select appropriate ABI.
137138
setABI("lp64d");

clang/lib/Basic/Targets/Mips.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,8 @@ class LLVM_LIBRARY_VISIBILITY MipsTargetInfo : public TargetInfo {
328328
IsMips16 = true;
329329
else if (Feature == "+micromips")
330330
IsMicromips = true;
331+
else if (Feature == "+mips32r6" || Feature == "+mips64r6")
332+
HasUnalignedAccess = true;
331333
else if (Feature == "+dsp")
332334
DspRev = std::max(DspRev, DSP1);
333335
else if (Feature == "+dspr2")

clang/lib/Basic/Targets/PPC.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class LLVM_LIBRARY_VISIBILITY PPCTargetInfo : public TargetInfo {
9292
LongDoubleFormat = &llvm::APFloat::PPCDoubleDouble();
9393
HasStrictFP = true;
9494
HasIbm128 = true;
95+
HasUnalignedAccess = true;
9596
}
9697

9798
// Set the language option for altivec based on our value.

clang/lib/Basic/Targets/SystemZ.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class LLVM_LIBRARY_VISIBILITY SystemZTargetInfo : public TargetInfo {
4747
LongDoubleFormat = &llvm::APFloat::IEEEquad();
4848
DefaultAlignForAttributeAligned = 64;
4949
MinGlobalAlign = 16;
50+
HasUnalignedAccess = true;
5051
if (Triple.isOSzOS()) {
5152
TLSSupported = false;
5253
// All vector types are default aligned on an 8-byte boundary, even if the

clang/lib/Basic/Targets/VE.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class LLVM_LIBRARY_VISIBILITY VETargetInfo : public TargetInfo {
4040
Int64Type = SignedLong;
4141
RegParmMax = 8;
4242
MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
43+
HasUnalignedAccess = true;
4344

4445
WCharType = UnsignedInt;
4546
WIntType = UnsignedInt;

clang/lib/Basic/Targets/WebAssembly.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class LLVM_LIBRARY_VISIBILITY WebAssemblyTargetInfo : public TargetInfo {
8484
SizeType = UnsignedLong;
8585
PtrDiffType = SignedLong;
8686
IntPtrType = SignedLong;
87+
HasUnalignedAccess = true;
8788
}
8889

8990
StringRef getABI() const override;

clang/lib/Basic/Targets/X86.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ class LLVM_LIBRARY_VISIBILITY X86TargetInfo : public TargetInfo {
188188
LongDoubleFormat = &llvm::APFloat::x87DoubleExtended();
189189
AddrSpaceMap = &X86AddrSpaceMap;
190190
HasStrictFP = true;
191+
HasUnalignedAccess = true;
191192

192193
bool IsWinCOFF =
193194
getTriple().isOSWindows() && getTriple().isOSBinFormatCOFF();

0 commit comments

Comments
 (0)