Skip to content

Commit b3d928e

Browse files
Make sure expected __isa_enabled bit was set before disabling it in feature-dependent tests (#5874)
Co-authored-by: Stephan T. Lavavej <[email protected]>
1 parent ee05a89 commit b3d928e

File tree

3 files changed

+46
-39
lines changed

3 files changed

+46
-39
lines changed

tests/std/include/test_vector_algorithms_support.hpp

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
#pragma once
55

66
#include <algorithm>
7+
#include <cassert>
78
#include <cstddef>
89
#include <cstdint>
910
#include <cstdio>
11+
#include <cstdlib>
1012
#include <functional>
1113
#include <isa_availability.h>
1214
#include <random>
15+
#include <string>
1316
#include <vector>
1417

1518
inline void initialize_randomness(std::mt19937_64& gen) {
@@ -41,24 +44,53 @@ inline void initialize_randomness(std::mt19937_64& gen) {
4144
extern "C" long __isa_enabled;
4245

4346
inline void disable_instructions(ISA_AVAILABILITY isa) {
44-
__isa_enabled &= ~(1UL << static_cast<unsigned long>(isa));
47+
const unsigned long as_ulong = static_cast<unsigned long>(isa);
48+
49+
auto has_env_var_escape_hatch = [] {
50+
size_t return_value = 0;
51+
char buffer[2]{};
52+
const errno_t err = ::getenv_s(&return_value, buffer, std::size(buffer), "STL_TEST_DOWNLEVEL_MACHINE");
53+
return err == 0 && buffer == std::string{"1"};
54+
};
55+
56+
if (!has_env_var_escape_hatch()) {
57+
const bool has_feature = (__isa_enabled & (1UL << as_ulong)) != 0;
58+
if (!has_feature) {
59+
std::printf("The feature %lu is not available, the test does not have full coverage!\n"
60+
"You can set the environment variable STL_TEST_DOWNLEVEL_MACHINE to 1,\n"
61+
"if you intentionally test on a machine without all features available.\n",
62+
as_ulong);
63+
}
64+
assert(has_feature);
65+
}
66+
67+
__isa_enabled &= ~(1UL << as_ulong);
4568
}
4669
#endif // (defined(_M_IX86) || defined(_M_X64)) && !defined(_M_CEE_PURE)
4770

4871
constexpr std::size_t dataCount = 1024;
4972

5073
template <class TestFunc>
51-
void run_randomized_tests_with_different_isa_levels(TestFunc tests) {
52-
std::mt19937_64 gen;
53-
initialize_randomness(gen);
74+
void run_tests_with_different_isa_levels(TestFunc tests) {
75+
tests();
5476

55-
tests(gen);
77+
#if (defined(_M_IX86) || (defined(_M_X64) && !defined(_M_ARM64EC))) && !defined(_M_CEE_PURE)
78+
const auto original_isa = __isa_enabled;
5679

57-
#if (defined(_M_IX86) || defined(_M_X64)) && !defined(_M_CEE_PURE)
5880
disable_instructions(__ISA_AVAILABLE_AVX2);
59-
tests(gen);
81+
tests();
6082

6183
disable_instructions(__ISA_AVAILABLE_SSE42);
62-
tests(gen);
63-
#endif // (defined(_M_IX86) || defined(_M_X64)) && !defined(_M_CEE_PURE)
84+
tests();
85+
86+
__isa_enabled = original_isa;
87+
#endif // (defined(_M_IX86) || (defined(_M_X64) && !defined(_M_ARM64EC))) && !defined(_M_CEE_PURE)
88+
}
89+
90+
template <class TestFunc>
91+
void run_randomized_tests_with_different_isa_levels(TestFunc tests) {
92+
std::mt19937_64 gen;
93+
initialize_randomness(gen);
94+
95+
run_tests_with_different_isa_levels([&] { tests(gen); });
6496
}

tests/std/tests/GH_002431_byte_range_find_with_unreachable_sentinel/test.cpp

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
#include <algorithm>
55
#include <cassert>
66
#include <cstddef>
7-
#include <isa_availability.h>
87
#include <ranges>
98

109
#pragma warning(push) // TRANSITION, OS-23694920
1110
#pragma warning(disable : 4668) // 'MEOW' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
1211
#include <Windows.h>
1312
#pragma warning(pop)
1413

14+
#include "test_vector_algorithms_support.hpp"
15+
1516
using namespace std;
1617

1718
template <class T>
@@ -31,14 +32,6 @@ void test_impl(void* sv, void* ev) {
3132
}
3233
}
3334

34-
#if defined(_M_IX86) || defined(_M_X64)
35-
extern "C" long __isa_enabled;
36-
37-
void disable_instructions(ISA_AVAILABILITY isa) {
38-
__isa_enabled &= ~(1UL << static_cast<unsigned long>(isa));
39-
}
40-
#endif // defined(_M_IX86) || defined(_M_X64)
41-
4235
void test_all_element_sizes(void* p, size_t page) {
4336
test_impl<char>(p, reinterpret_cast<char*>(p) + page);
4437
test_impl<short>(p, reinterpret_cast<char*>(p) + page);
@@ -58,13 +51,7 @@ int main() {
5851
void* p2 = VirtualAlloc(p, page, MEM_COMMIT, PAGE_READWRITE);
5952
assert(p2 != nullptr);
6053

61-
test_all_element_sizes(p, page);
62-
#if defined(_M_IX86) || defined(_M_X64)
63-
disable_instructions(__ISA_AVAILABLE_AVX2);
64-
test_all_element_sizes(p, page);
65-
disable_instructions(__ISA_AVAILABLE_SSE42);
66-
test_all_element_sizes(p, page);
67-
#endif // defined(_M_IX86) || defined(_M_X64)
54+
run_tests_with_different_isa_levels([&] { test_all_element_sizes(p, page); });
6855

6956
VirtualFree(p, 0, MEM_RELEASE);
7057
}

tests/std/tests/GH_003617_vectorized_meow_element/test.cpp

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,13 @@
66
#ifdef _M_X64
77

88
#include <cstddef>
9-
#include <isa_availability.h>
109
#include <vector>
1110

1211
#include "test_min_max_element_support.hpp"
12+
#include "test_vector_algorithms_support.hpp"
1313

1414
using namespace std;
1515

16-
extern "C" long __isa_enabled;
17-
18-
void disable_instructions(ISA_AVAILABILITY isa) {
19-
__isa_enabled &= ~(1UL << static_cast<unsigned long>(isa));
20-
}
21-
2216
void test_gh_3617() {
2317
// Test GH-3617 "<algorithm>: Silent bad codegen for vectorized meow_element() above 4 GB".
2418
constexpr size_t n = 0x4000'0010;
@@ -31,13 +25,7 @@ void test_gh_3617() {
3125
}
3226

3327
int main() {
34-
test_gh_3617();
35-
36-
disable_instructions(__ISA_AVAILABLE_AVX2);
37-
test_gh_3617();
38-
39-
disable_instructions(__ISA_AVAILABLE_SSE42);
40-
test_gh_3617();
28+
run_tests_with_different_isa_levels([] { test_gh_3617(); });
4129
}
4230
#else // ^^^ x64 / other architectures vvv
4331
int main() {}

0 commit comments

Comments
 (0)