Repository navigation
Expand file tree
/
Copy pathsimd_scanner.cpp
More file actions
344 lines (299 loc) · 15.6 KB
/
Copy pathsimd_scanner.cpp
File metadata and controls
344 lines (299 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// ─────────────────────────────────────────────────────────────────────────────
// zedda::simd_scanner — implementation
//
// COMPILE FLAGS (applied per-file in CMakeLists.txt, NOT globally):
// GCC/Clang : -mavx2 -mavx512f -mavx512bw
// MSVC : /arch:AVX2 (MSVC does not need a separate AVX-512 flag;
// intrinsics are available with /arch:AVX2 + headers)
//
// WHY PER-FILE FLAGS?
// If we applied -mavx2 globally, the compiler would emit AVX2 instructions
// in every .cpp (including auto-vectorized loops in profile_builder.cpp).
// That would crash on CPUs without AVX2. Restricting to this one file means
// only the functions below use AVX2 instructions — and they are only CALLED
// at runtime when has_avx2() returns true.
//
// WINDOWS NOTE:
// MSVC uses __cpuid / __cpuidex for CPU detection (no __builtin_cpu_supports).
// The intrinsic headers are <intrin.h> on MSVC, <immintrin.h> on GCC/Clang.
// ─────────────────────────────────────────────────────────────────────────────
#include "zedda/simd_scanner.hpp"
#include <cstdlib> // getenv
#include <mutex> // FIX C-M2: std::call_once + std::once_flag for cached scanner
// ── Intrinsic headers ─────────────────────────────────────────────────────────
#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
#if defined(_MSC_VER)
# include <intrin.h> // __cpuid, __cpuidex
# include <immintrin.h> // AVX2 / AVX-512 intrinsics
#elif defined(__GNUC__) || defined(__clang__)
# include <immintrin.h> // AVX2 / AVX-512
# include <cpuid.h> // __get_cpuid_count (fallback)
#endif
#endif
namespace zedda {
// ─────────────────────────────────────────────────────────────────────────────
// CPU Feature Detection
// GCC/Clang: __builtin_cpu_supports("avx2") — simplest, handles XSAVE check.
// MSVC: manual __cpuidex read of CPUID leaf 7, subleaf 0.
// ─────────────────────────────────────────────────────────────────────────────
bool has_avx2() noexcept {
#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
#if defined(_MSC_VER)
// FIX C-H7: CPUID leaf 7, subleaf 0 — EBX bit 5 = AVX2.
// Also verify OS has enabled XSAVE for YMM registers (XCR0 bits 1:2).
// Note: Checked before __clang__ so clang-cl on Windows uses MSVC intrinsics
// instead of emitting unresolved __cpu_model references.
int info[4] = {};
__cpuidex(info, 7, 0);
if ((info[1] & (1 << 5)) == 0) return false; // no AVX2 CPU support
// Check OS XSAVE support (CPUID leaf 1, ECX bit 26 = OSXSAVE).
__cpuid(info, 1);
if ((info[2] & (1 << 26)) == 0) return false;
// _xgetbv(_XCR_XFEATURE_ENABLED_MASK=0) — check YMM high bits are enabled.
unsigned long long xcr = _xgetbv(0);
return (xcr & 0x6) == 0x6; // bits 1:2 = YMM high 128 bits
#elif defined(__GNUC__) || defined(__clang__)
return __builtin_cpu_supports("avx2");
#else
return false; // unknown compiler — safe fallback
#endif
#else
return false;
#endif
}
bool has_avx512f() noexcept {
#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
#if defined(_MSC_VER)
// FIX C-H7: Add OS XSAVE check for ZMM registers (XCR0 bits 5,6,7).
// Without this, a CPU that supports AVX-512 but an OS that hasn't
// enabled it would crash with EXCEPTION_ILLEGAL_INSTRUCTION.
int info[4] = {};
__cpuidex(info, 7, 0);
bool f = (info[1] & (1 << 16)) != 0; // AVX-512F
bool bw = (info[1] & (1 << 30)) != 0; // AVX-512BW
if (!f || !bw) return false;
// Check OS XSAVE support (CPUID leaf 1, ECX bit 26 = OSXSAVE).
__cpuid(info, 1);
if ((info[2] & (1 << 26)) == 0) return false;
// _xgetbv(0) — check AVX-512 opmask (bit 5) and ZMM high (bits 6,7).
unsigned long long xcr = _xgetbv(0);
return (xcr & 0xE0) == 0xE0; // bits 5,6,7
#elif defined(__GNUC__) || defined(__clang__)
// AVX-512F: leaf 7.0 EBX bit 16; AVX-512BW: leaf 7.0 EBX bit 30
// We need both for byte-level 512-bit comparisons
return __builtin_cpu_supports("avx512f") && __builtin_cpu_supports("avx512bw");
#else
return false;
#endif
#else
return false;
#endif
}
// ─────────────────────────────────────────────────────────────────────────────
// SCALAR implementation — the correctness reference
//
// Simple byte-by-byte loop. Checks for delimiter, quote_char, '\n', '\r'.
// ALL other implementations must return EXACTLY the same index as this.
// ─────────────────────────────────────────────────────────────────────────────
size_t find_next_special_scalar(const char* data, size_t len, size_t pos,
char delim, char quote) noexcept {
while (pos < len) {
char c = data[pos];
if (c == delim || c == quote || c == '\n' || c == '\r') {
return pos;
}
++pos;
}
return len; // no match
}
// ─────────────────────────────────────────────────────────────────────────────
// AVX2 implementation — 32 bytes per loop iteration
//
// Technique (simdjson / simdcsv standard approach):
// 1. Load 32 bytes: _mm256_loadu_si256
// 2. Compare each byte against delim, quote, '\n', '\r' simultaneously
// 3. OR all comparison results → mask of any match
// 4. _mm256_movemask_epi8 → 32-bit integer (bit i = byte i had a match)
// 5. If mask != 0: first match is at pos + __builtin_ctz(mask)
// 6. If mask == 0: advance pos by 32, repeat
// Remainder (< 32 bytes): scalar fallback
//
// CRITICAL: _mm256_loadu_si256 is unaligned — safe on any 32-byte chunk
// even if the buffer start is not 32-byte aligned (which it usually isn't).
// ─────────────────────────────────────────────────────────────────────────────
#if defined(__AVX2__) || (defined(_MSC_VER) && defined(_M_AVX2)) \
|| ((defined(__GNUC__) || defined(__clang__)) && !defined(_MSC_VER) \
&& (defined(__x86_64__) || defined(__i386__)))
#if (defined(__GNUC__) || defined(__clang__)) && !defined(_MSC_VER) \
&& (defined(__x86_64__) || defined(__i386__))
__attribute__((target("avx2")))
#endif
size_t find_next_special_avx2(const char* data, size_t len, size_t pos,
char delim, char quote) noexcept {
// Broadcast the special chars to all 32 lanes of a 256-bit register
const __m256i v_delim = _mm256_set1_epi8(delim);
const __m256i v_quote = _mm256_set1_epi8(quote);
const __m256i v_newline= _mm256_set1_epi8('\n');
const __m256i v_cr = _mm256_set1_epi8('\r');
// Process 32 bytes per iteration
while (pos + 32 <= len) {
// Prefetch 4 cache lines ahead for next iterations (only if within bounds)
if (pos + 256 < len) {
#if defined(_MSC_VER)
_mm_prefetch(reinterpret_cast<const char*>(data + pos + 256), _MM_HINT_T0);
#elif defined(__GNUC__) || defined(__clang__)
// locality 3 = high temporal locality (fetch into all cache levels, matches _MM_HINT_T0)
__builtin_prefetch(data + pos + 256, 0, 3);
#endif
}
// Load 32 bytes — unaligned load is fine (slight perf penalty vs
// aligned, but alignment of arbitrary mmap'd file data is unknown)
__m256i chunk = _mm256_loadu_si256(
reinterpret_cast<const __m256i*>(data + pos)
);
// Compare against each special byte — result is 0xFF where match, 0x00 elsewhere
__m256i m_delim = _mm256_cmpeq_epi8(chunk, v_delim);
__m256i m_quote = _mm256_cmpeq_epi8(chunk, v_quote);
__m256i m_newline = _mm256_cmpeq_epi8(chunk, v_newline);
__m256i m_cr = _mm256_cmpeq_epi8(chunk, v_cr);
// OR all four: any byte that is a special char sets its bit
__m256i m_any = _mm256_or_si256(
_mm256_or_si256(m_delim, m_quote),
_mm256_or_si256(m_newline, m_cr)
);
// Collapse: one bit per byte — bit i is 1 if data[pos+i] was a match
int mask = _mm256_movemask_epi8(m_any);
if (mask != 0) {
// __builtin_ctz counts trailing zeros = index of lowest set bit
// = byte offset of first match within this 32-byte chunk
#if defined(_MSC_VER)
unsigned long idx;
_BitScanForward(&idx, static_cast<unsigned long>(mask));
return pos + idx;
#else
return pos + static_cast<size_t>(__builtin_ctz(static_cast<unsigned>(mask)));
#endif
}
pos += 32;
}
// Handle remainder (< 32 bytes) with scalar fallback
return find_next_special_scalar(data, len, pos, delim, quote);
}
#else
// Compiled on a system where AVX2 is not available to the compiler —
// provide a scalar stub. This should never be called at runtime because
// has_avx2() would return false, but it prevents linker errors.
size_t find_next_special_avx2(const char* data, size_t len, size_t pos,
char delim, char quote) noexcept {
return find_next_special_scalar(data, len, pos, delim, quote);
}
#endif // __AVX2__ || _M_AVX2
// ─────────────────────────────────────────────────────────────────────────────
// AVX-512 implementation — 64 bytes per loop iteration
//
// Technique:
// _mm512_loadu_si512 — load 64 bytes
// _mm512_cmpeq_epi8_mask — compare + produce 64-bit mask directly (no movemask!)
// OR the four 64-bit masks
// _tzcnt_u64 — first match position
//
// AVX-512 advantage vs AVX2:
// 1. Processes 2x bytes per instruction
// 2. _cmpeq_epi8_mask produces a native k-register bitmask —
// no need for _mm512_movemask_epi8 (which doesn't exist anyway)
// 3. On Skylake-X / Ice Lake: can execute 2 AVX-512 ops per cycle
// → theoretical 128 bytes/cycle throughput
//
// REALITY CHECK: 6.3M rows × 31 cols × ~15 bytes avg field =
// ~2.9 GB of data. At 50 GB/s memory bandwidth (DDR5), pure bandwidth
// limit is ~58ms. AVX-512 target of 2.5s has plenty of headroom;
// real bottleneck becomes memory latency and the Welford update loop.
// ─────────────────────────────────────────────────────────────────────────────
#if (defined(__AVX512F__) && defined(__AVX512BW__)) \
|| ((defined(__GNUC__) || defined(__clang__)) && !defined(_MSC_VER) \
&& (defined(__x86_64__) || defined(__i386__)))
#if (defined(__GNUC__) || defined(__clang__)) && !defined(_MSC_VER) \
&& (defined(__x86_64__) || defined(__i386__))
__attribute__((target("avx512f,avx512bw")))
#endif
size_t find_next_special_avx512(const char* data, size_t len, size_t pos,
char delim, char quote) noexcept {
const __m512i v_delim = _mm512_set1_epi8(delim);
const __m512i v_quote = _mm512_set1_epi8(quote);
const __m512i v_newline = _mm512_set1_epi8('\n');
const __m512i v_cr = _mm512_set1_epi8('\r');
while (pos + 64 <= len) {
// Prefetch 8 cache lines ahead (only if within bounds)
if (pos + 512 < len) {
#if defined(_MSC_VER)
_mm_prefetch(reinterpret_cast<const char*>(data + pos + 512), _MM_HINT_T0);
#elif defined(__GNUC__) || defined(__clang__)
// locality 3 = high temporal locality
__builtin_prefetch(data + pos + 512, 0, 3);
#endif
}
__m512i chunk = _mm512_loadu_si512(
reinterpret_cast<const __m512i*>(data + pos)
);
// Each _mm512_cmpeq_epi8_mask returns a 64-bit integer mask directly
__mmask64 m_delim = _mm512_cmpeq_epi8_mask(chunk, v_delim);
__mmask64 m_quote = _mm512_cmpeq_epi8_mask(chunk, v_quote);
__mmask64 m_newline = _mm512_cmpeq_epi8_mask(chunk, v_newline);
__mmask64 m_cr = _mm512_cmpeq_epi8_mask(chunk, v_cr);
__mmask64 mask = m_delim | m_quote | m_newline | m_cr;
if (mask != 0) {
#if defined(_MSC_VER)
unsigned long idx;
_BitScanForward64(&idx, static_cast<unsigned __int64>(mask));
return pos + idx;
#else
return pos + static_cast<size_t>(__builtin_ctzll(static_cast<unsigned long long>(mask)));
#endif
}
pos += 64;
}
// Handle remainder (< 64 bytes) cleanly without cross-target inlining issues.
while (pos < len) {
char c = data[pos];
if (c == delim || c == quote || c == '\n' || c == '\r') {
return pos;
}
++pos;
}
return len;
}
#else
size_t find_next_special_avx512(const char* data, size_t len, size_t pos,
char delim, char quote) noexcept {
// Stub: AVX-512 not available at compile time, fall through to AVX2/scalar
return find_next_special_avx2(data, len, pos, delim, quote);
}
#endif // __AVX512F__ && __AVX512BW__
// ─────────────────────────────────────────────────────────────────────────────
// Runtime dispatch — select_best_scanner() and get_active_scanner()
// ─────────────────────────────────────────────────────────────────────────────
ScanFn select_best_scanner() noexcept {
if (has_avx512f()) return find_next_special_avx512;
if (has_avx2()) return find_next_special_avx2;
return find_next_special_scalar;
}
ScanFn get_active_scanner() noexcept {
// FIX C-M2: Cache the result via std::call_once + once_flag to match
// the header contract ("Cached after first call, thread-safe via
// once_flag"). The previous un-cached version called std::getenv on
// every invocation, which is not thread-safe if another thread calls
// setenv, and adds ~100ns per call. ZEDDA_FORCE_SCALAR is read once
// at first call; if you need to toggle it, restart the process.
static std::once_flag flag;
static ScanFn cached = nullptr;
std::call_once(flag, []() {
const char* force = std::getenv("ZEDDA_FORCE_SCALAR");
if (force && force[0] == '1') {
cached = find_next_special_scalar;
} else {
cached = select_best_scanner();
}
});
return cached;
}
} // namespace zedda