Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7aad383

Browse files
authored
[libc] Some MSVC compatibility changes for src/string/memory_utils. (#158393)
1 parent ffcaeca commit 7aad383

File tree

9 files changed

+43
-5
lines changed

9 files changed

+43
-5
lines changed

libc/src/__support/endian_internal.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,17 @@ template <> LIBC_INLINE uint16_t byte_swap<uint16_t>(uint16_t value) {
3535
#if __has_builtin(__builtin_bswap16)
3636
return __builtin_bswap16(value);
3737
#else
38-
return (v << 8) | (v >> 8);
38+
return (value << 8) | (value >> 8);
3939
#endif // __builtin_bswap16
4040
}
4141

4242
template <> LIBC_INLINE uint32_t byte_swap<uint32_t>(uint32_t value) {
4343
#if __has_builtin(__builtin_bswap32)
4444
return __builtin_bswap32(value);
4545
#else
46-
return byte_swap<uint16_t>(static_cast<uint16>(v >> 16)) ||
47-
(static_cast<uint32_t>(byte_swap<uint16_t>(static_cast<uint16_t>(v)))
46+
return byte_swap<uint16_t>(static_cast<uint16_t>(value >> 16)) ||
47+
(static_cast<uint32_t>(
48+
byte_swap<uint16_t>(static_cast<uint16_t>(value)))
4849
<< 16);
4950
#endif // __builtin_bswap64
5051
}
@@ -53,8 +54,9 @@ template <> LIBC_INLINE uint64_t byte_swap<uint64_t>(uint64_t value) {
5354
#if __has_builtin(__builtin_bswap64)
5455
return __builtin_bswap64(value);
5556
#else
56-
return byte_swap<uint32_t>(static_cast<uint32>(v >> 32)) ||
57-
(static_cast<uint64_t>(byte_swap<uint32_t>(static_cast<uint32_t>(v)))
57+
return byte_swap<uint32_t>(static_cast<uint32_t>(value >> 32)) ||
58+
(static_cast<uint64_t>(
59+
byte_swap<uint32_t>(static_cast<uint32_t>(value)))
5860
<< 32);
5961
#endif // __builtin_bswap64
6062
}

libc/src/__support/macros/config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
#define __builtin_expect(value, expectation) (value)
4747
#define __builtin_unreachable() __assume(0)
4848

49+
#define __builtin_prefetch(X, Y, Z)
50+
4951
#endif // LIBC_COMPILER_IS_MSVC
5052

5153
#ifdef __clang__

libc/src/string/memory_utils/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ add_header_library(
4242
libc.src.__support.macros.config
4343
libc.src.__support.macros.optimization
4444
libc.src.__support.macros.properties.architectures
45+
libc.src.__support.macros.properties.compiler
4546
)
4647

4748
add_header_library(

libc/src/string/memory_utils/op_generic.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "src/__support/macros/attributes.h" // LIBC_INLINE
3232
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL
3333
#include "src/__support/macros/optimization.h"
34+
#include "src/__support/macros/properties/compiler.h"
3435
#include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_INT64
3536
#include "src/string/memory_utils/op_builtin.h"
3637
#include "src/string/memory_utils/utils.h"
@@ -39,12 +40,22 @@ static_assert((UINTPTR_MAX == 4294967295U) ||
3940
(UINTPTR_MAX == 18446744073709551615UL),
4041
"We currently only support 32- or 64-bit platforms");
4142

43+
#ifdef LIBC_COMPILER_IS_MSVC
44+
45+
namespace LIBC_NAMESPACE_DECL {
46+
using generic_v128 = __m128i;
47+
using generic_v256 = __m256i;
48+
using generic_v512 = __m512i;
49+
} // namespace LIBC_NAMESPACE_DECL
50+
51+
#else
4252
namespace LIBC_NAMESPACE_DECL {
4353
// Compiler types using the vector attributes.
4454
using generic_v128 = uint8_t __attribute__((__vector_size__(16)));
4555
using generic_v256 = uint8_t __attribute__((__vector_size__(32)));
4656
using generic_v512 = uint8_t __attribute__((__vector_size__(64)));
4757
} // namespace LIBC_NAMESPACE_DECL
58+
#endif // LIBC_COMPILER_IS_MSVC
4859

4960
namespace LIBC_NAMESPACE_DECL {
5061
namespace generic {

libc/src/string/memory_utils/op_x86.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "src/__support/macros/attributes.h" // LIBC_INLINE
1616
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL
1717
#include "src/__support/macros/properties/architectures.h"
18+
#include "src/__support/macros/properties/compiler.h"
1819

1920
#if defined(LIBC_TARGET_ARCH_IS_X86)
2021

@@ -57,7 +58,12 @@ LIBC_INLINE_VAR constexpr bool K_AVX512_BW = LLVM_LIBC_IS_DEFINED(__AVX512BW__);
5758
// Memcpy repmovsb implementation
5859
struct Memcpy {
5960
LIBC_INLINE static void repmovsb(void *dst, const void *src, size_t count) {
61+
#ifdef LIBC_COMPILER_IS_MSVC
62+
__movsb(static_cast<unsigned char *>(dst),
63+
static_cast<const unsigned char *>(src), count);
64+
#else
6065
asm volatile("rep movsb" : "+D"(dst), "+S"(src), "+c"(count) : : "memory");
66+
#endif // LIBC_COMPILER_IS_MSVC
6167
}
6268
};
6369

@@ -138,8 +144,10 @@ LIBC_INLINE MemcmpReturnType cmp_neq<uint64_t>(CPtr p1, CPtr p2,
138144
// When we use these SIMD types in template specialization GCC complains:
139145
// "ignoring attributes on template argument ‘__m128i’ [-Wignored-attributes]"
140146
// Therefore, we disable this warning in this file.
147+
#ifndef LIBC_COMPILER_IS_MSVC
141148
#pragma GCC diagnostic push
142149
#pragma GCC diagnostic ignored "-Wignored-attributes"
150+
#endif // !LIBC_COMPILER_IS_MSVC
143151

144152
///////////////////////////////////////////////////////////////////////////////
145153
// Specializations for __m128i
@@ -366,7 +374,9 @@ LIBC_INLINE MemcmpReturnType cmp_neq<__m512i>(CPtr p1, CPtr p2, size_t offset) {
366374
}
367375
#endif // __AVX512BW__
368376

377+
#ifndef LIBC_COMPILER_IS_MSVC
369378
#pragma GCC diagnostic pop
379+
#endif // !LIBC_COMPILER_IS_MSVC
370380

371381
} // namespace generic
372382
} // namespace LIBC_NAMESPACE_DECL

libc/src/string/memory_utils/utils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "src/__support/macros/attributes.h" // LIBC_INLINE
1818
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL
1919
#include "src/__support/macros/properties/architectures.h"
20+
#include "src/__support/macros/properties/compiler.h"
2021

2122
#include <stddef.h> // size_t
2223

@@ -90,13 +91,17 @@ LIBC_INLINE void memcpy_inline(void *__restrict dst,
9091
// different value of the Size parameter. This doesn't play well with GCC's
9192
// Value Range Analysis that wrongly detects out of bounds accesses. We
9293
// disable these warnings for the purpose of this function.
94+
#ifndef LIBC_COMPILER_IS_MSVC
9395
#pragma GCC diagnostic push
9496
#pragma GCC diagnostic ignored "-Warray-bounds"
9597
#pragma GCC diagnostic ignored "-Wstringop-overread"
9698
#pragma GCC diagnostic ignored "-Wstringop-overflow"
99+
#endif // !LIBC_COMPILER_IS_MSVC
97100
for (size_t i = 0; i < Size; ++i)
98101
static_cast<char *>(dst)[i] = static_cast<const char *>(src)[i];
102+
#ifndef LIBC_COMPILER_IS_MSVC
99103
#pragma GCC diagnostic pop
104+
#endif // !LIBC_COMPILER_IS_MSVC
100105
#endif
101106
}
102107

libc/test/UnitTest/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ add_unittest_framework_library(
7676
libc.src.__support.CPP.string_view
7777
libc.src.__support.CPP.type_traits
7878
libc.src.__support.fixed_point.fx_rep
79+
libc.src.__support.macros.properties.compiler
7980
libc.src.__support.macros.properties.types
8081
libc.src.__support.OSUtil.osutil
8182
libc.src.__support.uint128

libc/test/UnitTest/LibcTest.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "src/__support/CPP/string_view.h"
3131
#include "src/__support/CPP/type_traits.h"
3232
#include "src/__support/c_string.h"
33+
#include "src/__support/macros/properties/compiler.h"
3334
#include "test/UnitTest/ExecuteFunction.h"
3435
#include "test/UnitTest/TestLogger.h"
3536

@@ -260,7 +261,11 @@ constexpr char const *GetPrettyFunctionParamType(char const *str) {
260261
// This function recovers ParamType at compile time by using __PRETTY_FUNCTION__
261262
// It can be customized by using the REGISTER_TYPE_NAME macro below.
262263
template <typename ParamType> static constexpr const char *GetTypeName() {
264+
#ifdef LIBC_COMPILER_IS_MSVC
265+
return GetPrettyFunctionParamType(__FUNCSIG__);
266+
#else
263267
return GetPrettyFunctionParamType(__PRETTY_FUNCTION__);
268+
#endif // LIBC_COMPILER_IS_MSVC
264269
}
265270

266271
template <typename T>

utils/bazel/llvm-project-overlay/libc/test/UnitTest/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ libc_test_library(
6262
"//libc:__support_libc_errno",
6363
"//libc:__support_macros_config",
6464
"//libc:__support_macros_properties_architectures",
65+
"//libc:__support_macros_properties_compiler",
6566
"//libc:__support_macros_properties_types",
6667
"//libc:__support_stringutil",
6768
"//libc:__support_uint128",

0 commit comments

Comments
 (0)