Skip to content

Concurrency: centralise the definition for std::bit_cast #78985

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 1 commit into from
Jan 29, 2025
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
45 changes: 45 additions & 0 deletions include/swift/Runtime/STLCompatibility.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//===---- STLCompatibility.h - Runtime C++ Compatibiltiy Stubs --*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_RUNTIME_STL_COMPATIBILITY_H
#define SWIFT_RUNTIME_STL_COMPATIBILITY_H

#if __cplusplus >= 202002l || defined(__cpp_lib_bit_cast)
#include <bit>
#else
#include <cstdint>
#include <cstring>
#include <memory>
#include <type_traits>

namespace std {
inline namespace __swift {
template <typename Destination, typename Source>
std::enable_if_t<sizeof(Destination) == sizeof(Source) &&
std::is_trivially_copyable_v<Source> &&
std::is_trivially_copyable_v<Destination>, Destination>
bit_cast(const Source &src) noexcept {
static_assert(std::is_trivially_constructible_v<Destination>,
"The destination type must be trivially constructible");
Destination dst;
if constexpr (std::is_pointer_v<Source> || std::is_pointer_v<Destination>)
std::memcpy(reinterpret_cast<uintptr_t *>(&dst),
reinterpret_cast<const uintptr_t *>(&src), sizeof(Destination));
else
std::memcpy(&dst, &src, sizeof(Destination));
return dst;
}
}
}
#endif

#endif
23 changes: 1 addition & 22 deletions stdlib/public/Concurrency/AsyncLet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "swift/ABI/TaskOptions.h"
#include "swift/Runtime/Heap.h"
#include "swift/Runtime/HeapObject.h"
#include "swift/Runtime/STLCompatibility.h"
#include "swift/Threading/Mutex.h"
#include "llvm/ADT/PointerIntPair.h"

Expand All @@ -35,28 +36,6 @@

#include <new>

#if __cplusplus < 202002l || !defined(__cpp_lib_bit_cast)
namespace std {
template <typename Destination, typename Source>
std::enable_if_t<sizeof(Destination) == sizeof(Source) &&
std::is_trivially_copyable_v<Source> &&
std::is_trivially_copyable_v<Destination>, Destination>
bit_cast(const Source &src) noexcept {
static_assert(std::is_trivially_constructible_v<Destination>,
"The destination type must be trivially constructible");
Destination dst;
if constexpr (std::is_pointer_v<Source> || std::is_pointer_v<Destination>)
std::memcpy(reinterpret_cast<uintptr_t *>(&dst),
reinterpret_cast<const uintptr_t *>(&src), sizeof(Destination));
else
std::memcpy(&dst, &src, sizeof(Destination));
return dst;
}
}
#else
#include <bit>
#endif

using namespace swift;

namespace {
Expand Down
23 changes: 1 addition & 22 deletions stdlib/public/Concurrency/DispatchGlobalExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "swift/Runtime/Concurrency.h"
#include "swift/Runtime/EnvironmentVariables.h"
#include "swift/Runtime/STLCompatibility.h"

#if SWIFT_CONCURRENCY_ENABLE_DISPATCH
#include "swift/Runtime/HeapObject.h"
Expand All @@ -54,28 +55,6 @@
#include "ExecutorImpl.h"
#include "TaskPrivate.h"

#if __cplusplus < 202002l || !defined(__cpp_lib_bit_cast)
namespace std {
template <typename Destination, typename Source>
std::enable_if_t<sizeof(Destination) == sizeof(Source) &&
std::is_trivially_copyable_v<Source> &&
std::is_trivially_copyable_v<Destination>, Destination>
bit_cast(const Source &src) noexcept {
static_assert(std::is_trivially_constructible_v<Destination>,
"The destination type must be trivially constructible");
Destination dst;
if constexpr (std::is_pointer_v<Source> || std::is_pointer_v<Destination>)
std::memcpy(reinterpret_cast<uintptr_t *>(&dst),
reinterpret_cast<const uintptr_t *>(&src), sizeof(Destination));
else
std::memcpy(&dst, &src, sizeof(Destination));
return dst;
}
}
#else
#include <bit>
#endif

using namespace swift;

// Ensure that Job's layout is compatible with what Dispatch expects.
Expand Down
23 changes: 1 addition & 22 deletions stdlib/public/Concurrency/TaskGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "swift/Runtime/Config.h"
#include "swift/Runtime/Heap.h"
#include "swift/Runtime/HeapObject.h"
#include "swift/Runtime/STLCompatibility.h"
#include "swift/Threading/Mutex.h"
#include <atomic>
#include <deque>
Expand Down Expand Up @@ -61,28 +62,6 @@
#include <dlfcn.h>
#endif

#if __cplusplus < 202002l || !defined(__cpp_lib_bit_cast)
namespace std {
template <typename Destination, typename Source>
std::enable_if_t<sizeof(Destination) == sizeof(Source) &&
std::is_trivially_copyable_v<Source> &&
std::is_trivially_copyable_v<Destination>, Destination>
bit_cast(const Source &src) noexcept {
static_assert(std::is_trivially_constructible_v<Destination>,
"The destination type must be trivially constructible");
Destination dst;
if constexpr (std::is_pointer_v<Source> || std::is_pointer_v<Destination>)
std::memcpy(reinterpret_cast<uintptr_t *>(&dst),
reinterpret_cast<const uintptr_t *>(&src), sizeof(Destination));
else
std::memcpy(&dst, &src, sizeof(Destination));
return dst;
}
}
#else
#include <bit>
#endif

using namespace swift;

#if 0
Expand Down