Skip to content

Commit d380bf9

Browse files
authored
Merge pull request #73277 from Azoy/fix-conditional-invertible
[IRGen] Fix misalignment of conditional invertible requirement counts
2 parents 84d3618 + 852ef0b commit d380bf9

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

include/swift/ABI/GenericContext.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/ABI/MetadataRef.h"
2424
#include "swift/ABI/InvertibleProtocols.h"
2525
#include "swift/ABI/TrailingObjects.h"
26+
#include "swift/Basic/MathUtils.h"
2627
#include "swift/Demangling/Demangle.h"
2728

2829
namespace swift {
@@ -696,7 +697,7 @@ class TrailingGenericContextObjects<TargetSelf<Runtime>,
696697
if (!asSelf()->hasConditionalInvertedProtocols())
697698
return 0;
698699

699-
return countBitsUsed(getConditionalInvertedProtocols().rawBits());
700+
return popcount(getConditionalInvertedProtocols().rawBits());
700701
}
701702

702703
size_t numTrailingObjects(

include/swift/Basic/MathUtils.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919

2020
#include <cstddef>
2121

22+
#if SWIFT_COMPILER_IS_MSVC
23+
#include <intrin.h>
24+
#endif
25+
2226
namespace swift {
2327

2428
/// Round the given value up to the given alignment, as a power of two.
@@ -33,6 +37,15 @@ static inline size_t roundUpToAlignMask(size_t size, size_t alignMask) {
3337
return (size + alignMask) & ~alignMask;
3438
}
3539

40+
static inline unsigned popcount(unsigned value) {
41+
#if SWIFT_COMPILER_IS_MSVC
42+
return __popcnt(value);
43+
#else
44+
// Assume we have a compiler with this intrinsic.
45+
return __builtin_popcount(value);
46+
#endif
47+
}
48+
3649
} // namespace swift
3750

3851
#endif // #ifndef SWIFT_BASIC_MATH_UTILS_H

lib/IRGen/GenMeta.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,13 @@ namespace {
13341334
B.addPlaceholderWithSize(IGM.Int16Ty));
13351335
}
13361336

1337+
// The conditional invertible protocol set is alone as a 16 bit slot, so
1338+
// an even amount of conditional invertible protocols will cause an uneven
1339+
// alignment.
1340+
if ((numProtocols & 1) == 0) {
1341+
B.addInt16(0);
1342+
}
1343+
13371344
// Emit the generic requirements for the conditional conformance
13381345
// to each invertible protocol.
13391346
auto nominal = cast<NominalTypeDecl>(Type);

0 commit comments

Comments
 (0)