Skip to content

[6.0] [IRGen] Fix misalignment of conditional invertible requirement counts #74014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/swift/ABI/GenericContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "swift/ABI/MetadataRef.h"
#include "swift/ABI/InvertibleProtocols.h"
#include "swift/ABI/TrailingObjects.h"
#include "swift/Basic/MathUtils.h"
#include "swift/Demangling/Demangle.h"

namespace swift {
Expand Down Expand Up @@ -696,7 +697,7 @@ class TrailingGenericContextObjects<TargetSelf<Runtime>,
if (!asSelf()->hasConditionalInvertedProtocols())
return 0;

return countBitsUsed(getConditionalInvertedProtocols().rawBits());
return popcount(getConditionalInvertedProtocols().rawBits());
}

size_t numTrailingObjects(
Expand Down
21 changes: 21 additions & 0 deletions include/swift/Basic/MathUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@
#ifndef SWIFT_BASIC_MATH_UTILS_H
#define SWIFT_BASIC_MATH_UTILS_H

#include "Compiler.h"
#include <cstddef>
#include <cstdint>

#if SWIFT_COMPILER_IS_MSVC
#include <intrin.h>
#endif

namespace swift {

Expand All @@ -33,6 +39,21 @@ static inline size_t roundUpToAlignMask(size_t size, size_t alignMask) {
return (size + alignMask) & ~alignMask;
}

static inline unsigned popcount(unsigned value) {
#if SWIFT_COMPILER_IS_MSVC && (defined(_M_IX86) || defined(_M_X64))
// The __popcnt intrinsic is only available when targetting x86{_64} with MSVC.
return __popcnt(value);
#elif __has_builtin(__builtin_popcount)
return __builtin_popcount(value);
#else
// From llvm/ADT/bit.h which the runtime doesn't have access to (yet?)
uint32_t v = value;
v = v - ((v >> 1) & 0x55555555);
v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
return int(((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24);
#endif
}

} // namespace swift

#endif // #ifndef SWIFT_BASIC_MATH_UTILS_H
7 changes: 7 additions & 0 deletions lib/IRGen/GenMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,13 @@ namespace {
B.addPlaceholderWithSize(IGM.Int16Ty));
}

// The conditional invertible protocol set is alone as a 16 bit slot, so
// an even amount of conditional invertible protocols will cause an uneven
// alignment.
if ((numProtocols & 1) == 0) {
B.addInt16(0);
}

// Emit the generic requirements for the conditional conformance
// to each invertible protocol.
auto nominal = cast<NominalTypeDecl>(Type);
Expand Down