Skip to content

Commit 94b505d

Browse files
committed
[clang] TargetInfo hook for unaligned bitfields
1 parent d175e2f commit 94b505d

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
@@ -266,6 +266,9 @@ class TargetInfo : public TransferrableTargetInfo,
266266
LLVM_PREFERRED_TYPE(bool)
267267
unsigned AllowAMDGPUUnsafeFPAtomics : 1;
268268

269+
LLVM_PREFERRED_TYPE(bool)
270+
unsigned HasUnalignedAccess : 1;
271+
269272
unsigned ARMCDECoprocMask : 8;
270273

271274
unsigned MaxOpenCLWorkGroupSize;
@@ -858,6 +861,18 @@ class TargetInfo : public TransferrableTargetInfo,
858861
return PointerWidth;
859862
}
860863

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

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
@@ -186,6 +186,8 @@ AArch64TargetInfo::AArch64TargetInfo(const llvm::Triple &Triple,
186186
assert(UseBitFieldTypeAlignment && "bitfields affect type alignment");
187187
UseZeroLengthBitfieldAlignment = true;
188188

189+
HasUnalignedAccess = true;
190+
189191
// AArch64 targets default to using the ARM C++ ABI.
190192
TheCXXABI.set(TargetCXXABI::GenericAArch64);
191193

@@ -484,7 +486,7 @@ void AArch64TargetInfo::getTargetDefines(const LangOptions &Opts,
484486
if (HasPAuthLR)
485487
Builder.defineMacro("__ARM_FEATURE_PAUTH_LR", "1");
486488

487-
if (HasUnaligned)
489+
if (HasUnalignedAccess)
488490
Builder.defineMacro("__ARM_FEATURE_UNALIGNED", "1");
489491

490492
if ((FPU & NeonMode) && HasFullFP16)
@@ -908,7 +910,8 @@ bool AArch64TargetInfo::handleTargetFeatures(std::vector<std::string> &Features,
908910
HasSM4 = true;
909911
}
910912
if (Feature == "+strict-align")
911-
HasUnaligned = false;
913+
HasUnalignedAccess = false;
914+
912915
// All predecessor archs are added but select the latest one for ArchKind.
913916
if (Feature == "+v8a" && ArchInfo->Version < llvm::AArch64::ARMV8A.Version)
914917
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)