8#include <native/config.h>
9#include <native/targets.h>
21#define NATIVE_TARGET_jam_avx2 "avx2,fma,bmi2"
22#define NATIVE_TARGET_jam_avx512 "avx2,fma,avx512f,avx512dq,avx512bw,avx512vl,bmi2"
23#define NATIVE_TARGET_jam_avx512_popcnt "avx2,fma,avx512f,avx512dq,avx512bw,avx512vl,avx512vpopcntdq,bmi2"
28hint_inline hint_const hint_target(
"avx2")
29__m256i shift_left(__m256i value, __m256i counts) noexcept {
30 return _mm256_sllv_epi32(value, counts);
32hint_inline hint_const hint_target(
"avx512f")
33__m512i shift_left(__m512i value, __m512i counts) noexcept {
34 return _mm512_sllv_epi32(value, counts);
36hint_inline hint_const hint_target(
"avx2")
37__m256i shift_right(__m256i value, __m256i counts) noexcept {
38 return _mm256_srlv_epi32(value, counts);
40hint_inline hint_const hint_target(
"avx512f")
41__m512i shift_right(__m512i value, __m512i counts) noexcept {
42 return _mm512_srlv_epi32(value, counts);
45hint_inline hint_target(
"avx2")
46void store_prefix(std::uint32_t * target, __m256i value,
unsigned count) noexcept {
47 auto const mask = _mm256_cmpgt_epi32(_mm256_set1_epi32(count),
48 _mm256_setr_epi32(0, 1, 2, 3, 4, 5, 6, 7));
49 _mm256_maskstore_epi32(
reinterpret_cast<int *
>(target), mask, value);
56import native.features;
58import native.x86.bmi2;
59import native.x86.memory;
60import native.x86.vpopcntdq;
63namespace jam::detail {
64using word = std::uint64_t;
65using bitmap = std::uint32_t;
67constexpr word pointer_cells(bitmap live)
noexcept {
69 x = (x | (x << 16)) & 0x0000ffff0000ffffULL;
70 x = (x | (x << 8)) & 0x00ff00ff00ff00ffULL;
71 x = (x | (x << 4)) & 0x0f0f0f0f0f0f0f0fULL;
72 x = (x | (x << 2)) & 0x3333333333333333ULL;
73 x = (x | (x << 1)) & 0x5555555555555555ULL;
77constexpr bitmap forward(forwarding_tables tables, bitmap value)
noexcept {
79 auto const * metadata = value >> 31 ? tables.young : tables.old;
80 if (!metadata) { assert(!(value >> 31));
return value; }
81 auto const index = (value & 0x7fffffffu) >> 5;
82 auto const & item = metadata[index];
83 auto const * alignment = value >> 31 ? tables.young_alignment : tables.old_alignment;
84 auto const bit = bitmap{1} << (value & 31);
85 if (!(item.live & bit))
return 0;
86 return item.destination + std::popcount(retained(item, alignment, index) & (bit - 1));
90inline constexpr auto pack_table = []
consteval {
91 std::array<std::array<std::uint8_t, 256>, 16> result{};
92 for (
unsigned live = 0; live != 16; ++live)
93 for (
unsigned value = 0; value != 256; ++value) {
94 unsigned packed = 0, position = 0;
95 for (
unsigned cell = 0; cell != 4; ++cell)
if (live & (1u << cell)) {
96 packed |= ((value >> (cell * 2)) & 3) << position;
99 result[live][value] =
static_cast<std::uint8_t
>(packed);
104constexpr word portable_pack(heap_block
const & item)
noexcept {
106 unsigned position = 0;
107 for (
unsigned i = 0; i != 8; ++i) {
108 auto const live = (item.live >> (4 * i)) & 15;
109 result |= word{pack_table[live][(item.pointers >> (8 * i)) & 255]} << position;
110 position += 2 * std::popcount(live);
115void baseline_move(word
const * source, word * target, heap_block
const * metadata,
116 std::size_t begin, std::size_t end, forwarding_tables tables,
117 std::uint8_t
const * alignment)
noexcept {
118 for (
auto i = begin; i != end; ++i) {
119 auto item = metadata[i];
120 item.pointers &= pointer_cells(item.live);
121 item.live = retained(item, alignment, i);
122 auto destination = item.destination & 0x7fffffffu;
123 if (item.live == ~bitmap{0} && !item.pointers) {
124 std::memcpy(target + destination, source + i * 32, 256);
127 for (
auto bits = item.live; bits; bits &= bits - 1) {
128 auto const bit =
static_cast<unsigned>(std::countr_zero(bits));
130 std::memcpy(&value, source + i * 32 + bit,
sizeof(value));
131 auto const fields = (item.pointers >> (bit * 2)) & 3;
132 if (fields & 1) value = (value & 0xffffffff00000000ULL) | forward(tables,
static_cast<bitmap
>(value));
133 if (fields & 2) value = (value & 0x00000000ffffffffULL) | (word{forward(tables,
static_cast<bitmap
>(value >> 32))} << 32);
134 std::memcpy(target + destination++, &value,
sizeof(value));
141hint_inline
void pack_starts(bitmap bits, bitmap kept, std::size_t destination,
142 bitmap * output, [[maybe_unused]] std::size_t source_index,
143 std::size_t & cleared,
bool in_place)
noexcept {
146 unsigned position = 0;
147 for (
auto mask = kept; mask; mask &= mask - 1, ++position)
148 packed |= bitmap{(bits >> std::countr_zero(mask)) & 1u} << position;
149 auto const index = destination / 32;
150 auto const shift = destination % 32;
152 assert(index <= source_index);
153 while (cleared <= index) output[cleared++] = 0;
155 output[index] |= packed << shift;
157 auto const upper = packed >> (32 - shift);
160 assert(index + 1 <= source_index);
161 while (cleared <= index + 1) output[cleared++] = 0;
163 output[index + 1] |= upper;
171#define JAM_PACK_MASKS(EXTRACT) \
172 bool const in_place = metadata == output; \
173 std::size_t cleared = 0, starts_cleared = 0; \
174 for (std::size_t i = 0; i != old_blocks; ++i) { \
175 auto item = metadata[i]; \
176 if constexpr (tracks_starts) \
177 pack_starts(starts[i] & item.live, retained(item, alignment, i), \
178 item.destination & 0x7fffffffu, destination_starts, i, starts_cleared, \
179 starts == destination_starts); \
180 item.pointers &= pointer_cells(item.live); \
181 if (!item.pointers) continue; \
182 item.live = retained(item, alignment, i); \
184 auto const first = static_cast<unsigned>(std::countr_zero(item.live)); \
185 auto const run = item.live >> first; \
186 if ((run & (run + bitmap{1})) == 0) packed = item.pointers >> (first * 2); \
187 else packed = (EXTRACT); \
188 if (!packed) continue; \
189 auto const destination = std::size_t{item.destination & 0x7fffffffu} * 2; \
190 auto const index = destination >> 6; \
191 auto const shift = destination & 63; \
193 assert(index <= i); \
194 while (cleared <= index) output[cleared++].pointers = 0; \
196 output[index].pointers |= packed << shift; \
198 auto const upper = packed >> (64 - shift); \
201 assert(index + 1 <= i); \
202 while (cleared <= index + 1) output[cleared++].pointers = 0; \
204 output[index + 1].pointers |= upper; \
208 if constexpr (tracks_starts) { \
209 while ((in_place && cleared < old_blocks) \
210 || (starts == destination_starts && starts_cleared < old_blocks)) { \
211 if (in_place && cleared < old_blocks) output[cleared++].pointers = 0; \
212 if (starts == destination_starts && starts_cleared < old_blocks) \
213 destination_starts[starts_cleared++] = 0; \
215 } else if (in_place) while (cleared < old_blocks) output[cleared++].pointers = 0;
217template<
bool tracks_starts>
218void baseline_pack(heap_block
const * metadata, heap_block * output, std::size_t old_blocks,
219 std::uint8_t
const * alignment, bitmap
const * starts =
nullptr,
220 bitmap * destination_starts =
nullptr) noexcept {
221 JAM_PACK_MASKS(portable_pack(item))
225#define JAM_TARGETS jam_avx512_popcnt, jam_avx512, jam_avx2
226#define JAM_LANES(ISA) ((ISA).has(native::x86_feature::avx512f) ? 16 : 8)
227#define JAM_EXTRACT(ISA) native::pext<ISA>(item.pointers, pointer_cells(item.live))
228#elif NATIVE_HOST_NEON
229#define JAM_TARGETS neon
230#define JAM_LANES(ISA) 4
231#define JAM_EXTRACT(ISA) portable_pack(item)
235static_assert(
sizeof(heap_block) == 16 && offsetof(heap_block, live) == 0
236 && offsetof(heap_block, destination) == 4);
240hint_inline hint_const hint_target(NATIVE_TARGET_jam_avx2)
241typename V::mask vector_mask(bitmap bits)
noexcept {
242 static_assert(V::lanes == 8);
243 constexpr std::array<bitmap, 8> positions{1, 2, 4, 8, 16, 32, 64, 128};
244 return (V(bits) & V::load(positions.data())) != V(0);
248hint_inline hint_target(NATIVE_TARGET_jam_avx2)
249bitmap pack_store(word * target, bitmap kept, V value)
noexcept {
250 auto const packed = native::compress(vector_mask<V>(kept), value);
251 store_prefix(
reinterpret_cast<bitmap *
>(target), packed.value.to_native(),
252 static_cast<unsigned>(packed.count));
253 return static_cast<bitmap
>(packed.count / 2);
256#define JAM_GATHER_BLOCK(ISA) \
257if constexpr ((ISA).has(native::x86_feature::avx512vpopcntdq) \
258 || !(ISA).has(native::x86_feature::avx512f)) { \
259 constexpr unsigned batch = lanes == 16 ? 4 : 2; \
260 using W = native::wide<V, batch>; \
261 using I = native::simd<std::int32_t, lanes, ISA>; \
262 auto const live_fields = pointer_cells(item.live); \
263 auto const fields = item.pointers & live_fields; \
264 for (unsigned chunk = 0; chunk != 64 / lanes; chunk += batch) { \
266 native::wide<M, batch> masks{M::from_bitset(0)}; \
267 _Pragma("clang loop unroll(full)") \
268 for (unsigned j = 0; j != batch; ++j) { \
269 values.registers[j] = V::load(reinterpret_cast<bitmap const *>(source + i * 32) \
270 + lanes * (chunk + j)); \
271 if constexpr (lanes == 16) \
272 masks.registers[j] = M::from_bitset(fields >> (lanes * (chunk + j))); \
274 masks.registers[j] = vector_mask<V>(fields >> (lanes * (chunk + j))); \
277 native::wide<I, batch> indices{I(0)}; \
278 W live{V(0)}, bases{V(0)}, ranks{V(0)}; \
279 auto & [...v] = values; \
280 auto & [...m] = masks; \
281 auto & [...ix] = indices; \
282 auto & [...l] = live; \
283 auto & [...b] = bases; \
284 auto & [...r] = ranks; \
285 ((m &= v != V(0)), ...); \
286 if (!tables.old) ((m &= (v & V(0x80000000u)) != V(0)), ...); \
287 ((ix = native::reinterpret_bits<std::int32_t>( \
288 ((v & V(0x7fffffffu)) >> native::imm<5>) << native::imm<2>)), ...); \
290 for (unsigned generation = 0; generation != 2; ++generation) { \
291 auto const * table = generation ? tables.young : tables.old; \
292 if (!table) continue; \
293 auto const * live_address = reinterpret_cast<bitmap const *>(table); \
294 auto const * base_address = reinterpret_cast<bitmap const *>( \
295 reinterpret_cast<char const *>(table) + offsetof(heap_block, destination)); \
296 native::wide<M, batch> selected{M::from_bitset(0)}; \
297 auto & [...g] = selected; \
298 ((g = m & ((v >> native::imm<31>) == V(generation))), ...); \
299 if constexpr (lanes == 16) { \
300 ((l = native::mask_vpgatherdd<4>(l, g, live_address, ix)), ...); \
301 ((b = native::mask_vpgatherdd<4>(b, g, base_address, ix)), ...); \
303 ((l = native::mask_vpgatherdd<4>(l, \
304 native::reinterpret_bits<std::int32_t>(native::mask_bits(g)), live_address, ix)), ...); \
305 ((b = native::mask_vpgatherdd<4>(b, \
306 native::reinterpret_bits<std::int32_t>(native::mask_bits(g)), base_address, ix)), ...); \
308 auto const * alignment_table = generation ? tables.young_alignment : tables.old_alignment; \
309 if (alignment_table) { \
310 auto const * address = reinterpret_cast<bitmap const *>(alignment_table); \
311 if constexpr (lanes == 16) \
312 ((r = native::mask_vpgatherdd<4>(r, g, address, \
313 native::reinterpret_bits<std::int32_t>((v & V(0x7fffffffu)) >> native::imm<8>))), ...); \
315 ((r = native::mask_vpgatherdd<4>(r, native::reinterpret_bits<std::int32_t>(native::mask_bits(g)), address, \
316 native::reinterpret_bits<std::int32_t>((v & V(0x7fffffffu)) >> native::imm<8>))), ...); \
319 ((ix = native::reinterpret_bits<std::int32_t>(l & V::from_native(shift_left(V(1).to_native(), (v & V(31)).to_native())))), ...); \
320 ((b = native::select(m & (ix == I(0)), V(0), b)), ...); \
321 ((m &= ix != I(0)), ...); \
322 if (tables.old_alignment || tables.young_alignment) { \
323 ((r = V::from_native(shift_right(r.to_native(), (((v >> native::imm<5>) & V(7)) << native::imm<2>).to_native())) & V(7)), ...); \
324 ((l = native::select((r & V(1)) != V(0), l | ((l >> native::imm<1>) & V(0x55555555u)) | ((l & V(0x55555555u)) << native::imm<1>), l)), ...); \
325 ((l = native::select((r & V(2)) != V(0), l | ((l >> native::imm<2>) & V(0x33333333u)) | ((l & V(0x33333333u)) << native::imm<2>), l)), ...); \
326 ((l = native::select((r & V(4)) != V(0), l | ((l >> native::imm<4>) & V(0x0f0f0f0fu)) | ((l & V(0x0f0f0f0fu)) << native::imm<4>), l)), ...); \
328 ((r = V::from_native(shift_left(V(1).to_native(), (v & V(31)).to_native()))), ...); \
329 ((r = l & (r - V(1))), ...); \
330 if constexpr ((ISA).has(native::x86_feature::avx512vpopcntdq)) \
331 ((r = native::vpopcntd(r)), ...); \
333 ((r = native::popcount(r)), ...); \
334 ((v = native::masked_add(m, b, b, r)), ...); \
336 _Pragma("clang loop unroll(full)") \
337 for (unsigned j = 0; j != batch; ++j) { \
338 auto const kept = live_fields >> (lanes * (chunk + j)); \
339 if constexpr (lanes == 16) { \
340 auto const written = native::compress_store(reinterpret_cast<bitmap *>(target + destination), \
341 lanes, M::from_bitset(kept), values.registers[j]); \
342 destination += static_cast<bitmap>(written / 2); \
343 } else if (kept & 255) { \
344 destination += pack_store(target + destination, static_cast<bitmap>(kept), values.registers[j]); \
352#define JAM_GATHER_BLOCK(ISA)
358#define JAM_COMPACTOR(name, ISA, ...) \
359 template<native::isa<> A> \
360 requires (native::target<A, __VA_ARGS__> == native::target<ISA, __VA_ARGS__>) \
361 void name##_move(word const * source, word * target, heap_block const * metadata, \
362 std::size_t begin, std::size_t end, forwarding_tables tables, \
363 std::uint8_t const * alignment) noexcept { \
364 constexpr unsigned lanes = JAM_LANES(ISA); \
365 using V = native::simd<bitmap, lanes, ISA>; \
366 using M = typename V::mask; \
367 for (auto i = begin; i != end; ++i) { \
368 auto item = metadata[i]; \
369 if (!item.live) continue; \
370 item.pointers &= pointer_cells(item.live); \
371 item.live = retained(item, alignment, i); \
372 auto destination = item.destination & 0x7fffffffu; \
373 if (item.live == ~bitmap{0} && !item.pointers) { \
374 std::memcpy(target + destination, source + i * 32, 256); \
377 JAM_GATHER_BLOCK(ISA) \
378 for (unsigned cell = 0; cell != 32; cell += lanes / 2) { \
379 auto const live = (item.live >> cell) & ((1u << (lanes / 2)) - 1); \
380 if (!live) continue; \
381 std::array<bitmap, lanes> values; \
382 std::memcpy(values.data(), source + i * 32 + cell, sizeof(values)); \
383 auto fields = (item.pointers >> (cell * 2)) & pointer_cells(live); \
384 for (; fields; fields &= fields - 1) { \
385 auto const lane = std::countr_zero(fields); \
386 values[lane] = forward(tables, values[lane]); \
388 auto const written = native::compress_store(reinterpret_cast<bitmap *>(target + destination), \
389 lanes, M::from_bitset(pointer_cells(live)), V::load(values.data())); \
390 destination += static_cast<bitmap>(written / 2); \
394 template<native::isa<> A, bool tracks_starts> \
395 requires (native::target<A, __VA_ARGS__> == native::target<ISA, __VA_ARGS__>) \
396 void name##_pack(heap_block const * metadata, heap_block * output, std::size_t old_blocks, \
397 std::uint8_t const * alignment, bitmap const * starts = nullptr, \
398 bitmap * destination_starts = nullptr) noexcept { \
399 JAM_PACK_MASKS(JAM_EXTRACT(ISA)) \
401NATIVE_TARGET_VARIANTS(vector, JAM_COMPACTOR, JAM_TARGETS)
402#undef JAM_GATHER_BLOCK
406constexpr compactor baseline{baseline_move, baseline_pack<false>,
407 baseline_pack<true>, native::scalar,
"baseline"};
408#define JAM_ENTRY(unused, tag) \
409 compactor{vector_move<NATIVE_TARGET_ISA(tag)>, vector_pack<NATIVE_TARGET_ISA(tag), false>, \
410 vector_pack<NATIVE_TARGET_ISA(tag), true>, \
411 NATIVE_TARGET_ISA(tag), #tag},
412constexpr compactor variants[]{NATIVE_DETAIL_TARGET_MAP(JAM_ENTRY, unused, JAM_TARGETS) baseline};
416namespace jam::detail {
418std::span<compactor const> compactor_variants() noexcept {
return variants; }
419compactor choose_compactor() noexcept {
420 auto selected = baseline;
421 native::with_isa(NATIVE_TARGET_LIST(JAM_TARGETS), native::observe_cpu(), [&]<native::isa<> A> {
422 for (
auto const & variant : variants)
if (variant.requirement == A) selected = variant;