jam 0.0.1
A compacting generational garbage collector for C++26
Loading...
Searching...
No Matches
simd.ccm
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Edward Kmett
2// SPDX-License-Identifier: BSD-2-Clause OR Apache-2.0
3
6module;
7#include <hint.h>
8#include <native/targets.h>
9#include <array>
10#include <cassert>
11#include <bit>
12#include <concepts>
13#include <cstddef>
14#include <cstdint>
15#include <cstring>
16#include <span>
17#include <type_traits>
18#include <utility>
19
20export module jam.simd;
21export import jam;
22export import native.simd;
23#if defined(__x86_64__) || defined(_M_X64)
24import native.x86.memory;
25#endif
26
27export namespace native {
28template<class T>
29struct simd_traits<jam::ptr<T>> { using storage_type = std::uint32_t; };
30}
31
32namespace jam::detail {
33// Typed storage and lane access stay available to baseline GC workers. Inherited
34// constructor bridges must not require the target ISA of the vector operations.
35template<class T, class Raw, class Self>
36struct alignas(Raw) ptr_storage {
37 using value_type = ptr<T>;
38 static constexpr std::size_t lanes = Raw::lanes;
39protected:
40 std::array<value_type, lanes> values_{};
41public:
42 [[nodiscard]] hint_inline constexpr value_type const & operator[](std::size_t i) const noexcept { return values_[i]; }
43 [[nodiscard]] hint_inline constexpr value_type & operator[](std::size_t i) noexcept { return values_[i]; }
45 constexpr ptr_storage() noexcept = default;
46 hint_inline constexpr ptr_storage(ptr_storage const & other) noexcept { assign(other); }
47 hint_inline constexpr ptr_storage & operator=(ptr_storage const & other) noexcept { assign(other); return *this; }
48 hint_inline constexpr void unsafe_assign(ptr_storage const & other) noexcept {
49 for (std::size_t i = 0; i != lanes; ++i) values_[i].unsafe_assign(other.values_[i]);
50 }
51 hint_inline constexpr void remember() noexcept {
52 if !consteval { if (auto * h = heap::current()) h->remember(std::span<value_type>{values_}); }
53 }
54 hint_inline constexpr void assign(ptr_storage const & other) noexcept { unsafe_assign(other); remember(); }
56 hint_inline constexpr ptr_storage(value_type const & value) noexcept {
57 for (auto & lane : values_) lane.unsafe_assign(value);
58 remember();
59 }
61 hint_inline constexpr explicit ptr_storage(std::array<value_type, lanes> const & values) noexcept
62 {
63 for (std::size_t i = 0; i != lanes; ++i) values_[i].unsafe_assign(values[i]);
64 remember();
65 }
67 hint_inline constexpr explicit ptr_storage(Raw const & raw) noexcept {
68 std::array<std::uint32_t, lanes> offsets;
69 std::memcpy(offsets.data(), &raw, sizeof(offsets));
70 for (std::size_t i = 0; i != lanes; ++i) values_[i].unsafe_assign(offsets[i]);
71 remember();
72 }
74 template<std::size_t Alignment = 1>
75 [[nodiscard]] static hint_inline constexpr Self load_memory(value_type const * p) noexcept {
76 std::array<value_type, lanes> values;
77 for (std::size_t i = 0; i != lanes; ++i) values[i].unsafe_assign(p[i]);
78 return Self{values};
79 }
81 template<std::size_t Alignment = 1>
82 hint_inline constexpr void store_memory(value_type * p) const noexcept {
83 for (std::size_t i = 0; i != lanes; ++i) p[i].unsafe_assign(values_[i]);
84 if !consteval { if (auto * h = heap::current()) h->remember(std::span<value_type>{p, lanes}); }
85 }
87 [[nodiscard]] static hint_inline constexpr Self load(value_type const * p) noexcept { return load_memory(p); }
89 hint_inline constexpr void store(value_type * p) const noexcept { store_memory(p); }
90};
91}
92#if defined(__x86_64__) || defined(_M_X64)
93#define JAM_PTR_TARGETS avx512, avx2, scalar
94#else
95#define JAM_PTR_TARGETS neon, scalar
96#endif
97
98#define JAM_PTR_SIMD(name, ISA, ...) \
99export namespace native { \
100 \
104template<class T, class Raw, class Self> \
105 requires (native::target<Raw::architecture, __VA_ARGS__> == native::target<ISA, __VA_ARGS__>) \
106struct simd_customization<jam::ptr<T>, Raw, Self> : jam::detail::ptr_storage<T, Raw, Self> { \
107 using base = jam::detail::ptr_storage<T, Raw, Self>; \
108 using base::base; \
109 using base::lanes; \
110 using raw_type = Raw; \
111 using mask_type = typename Raw::mask_type; \
112private: \
113 using base::values_; \
114public: \
115 \
116 [[nodiscard]] hint_inline constexpr Raw to_native() const noexcept { \
117 std::array<std::uint32_t, lanes> offsets; \
118 for (std::size_t i = 0; i != lanes; ++i) offsets[i] = values_[i].get(); \
119 return Raw::load(offsets.data()); \
120 } \
121 \
122 [[nodiscard]] friend hint_inline constexpr mask_type operator==(Self const & a, Self const & b) noexcept { \
123 if constexpr (requires(Raw x) { x == x; }) return a.to_native() == b.to_native(); \
124 else { \
125 std::uint64_t bits = 0; \
126 for (std::size_t i = 0; i != lanes; ++i) bits |= std::uint64_t(a[i] == b[i]) << i; \
127 return mask_type::from_bitset(bits); \
128 } \
129 } \
130 \
131 [[nodiscard]] friend hint_inline constexpr mask_type operator!=(Self const & a, Self const & b) noexcept { \
132 if constexpr (requires(Raw x) { x != x; }) return a.to_native() != b.to_native(); \
133 else { \
134 std::uint64_t bits = 0; \
135 for (std::size_t i = 0; i != lanes; ++i) bits |= std::uint64_t(a[i] != b[i]) << i; \
136 return mask_type::from_bitset(bits); \
137 } \
138 } \
139 \
140 [[nodiscard]] friend hint_inline constexpr mask_type operator!=(Self const & a, std::nullptr_t) noexcept { \
141 if constexpr (requires(Raw x) { x != x; }) return a.to_native() != Raw{std::uint32_t{0}}; \
142 else { \
143 std::uint64_t bits = 0; \
144 for (std::size_t i = 0; i != lanes; ++i) bits |= std::uint64_t(bool(a[i])) << i; \
145 return mask_type::from_bitset(bits); \
146 } \
147 } \
148 \
149 [[nodiscard]] friend hint_inline constexpr mask_type operator==(Self const & a, std::nullptr_t) noexcept { \
150 if constexpr (requires(Raw x) { x == x; }) return a.to_native() == Raw{std::uint32_t{0}}; \
151 else { \
152 std::uint64_t bits = 0; \
153 for (std::size_t i = 0; i != lanes; ++i) bits |= std::uint64_t(!a[i]) << i; \
154 return mask_type::from_bitset(bits); \
155 } \
156 } \
157}; \
158}
159NATIVE_TARGET_VARIANTS(pointer, JAM_PTR_SIMD, JAM_PTR_TARGETS)
160#undef JAM_PTR_SIMD
161
162export namespace jam {
164template<class T, std::size_t N, native::isa<> A>
165hint_inline void unsafe_assign(native::simd<ptr<T>, N, A> & target,
166 native::simd<ptr<T>, N, A> const & source) noexcept { target.unsafe_assign(source); }
168template<class T, std::size_t N, native::isa<> A>
169hint_inline void assign(native::simd<ptr<T>, N, A> & target,
170 native::simd<ptr<T>, N, A> const & source) noexcept { target.assign(source); }
172template<class T, std::size_t N, native::isa<> A, std::size_t K>
173hint_inline void unsafe_assign(native::wide<native::simd<ptr<T>, N, A>, K> & target,
174 native::wide<native::simd<ptr<T>, N, A>, K> const & source) noexcept {
175 for (std::size_t i = 0; i != K; ++i) target.registers[i].unsafe_assign(source.registers[i]);
178template<class T, std::size_t N, native::isa<> A, std::size_t K>
179hint_inline void assign(native::wide<native::simd<ptr<T>, N, A>, K> & target,
180 native::wide<native::simd<ptr<T>, N, A>, K> const & source) noexcept {
181 for (std::size_t i = 0; i != K; ++i) target.registers[i].assign(source.registers[i]);
182}
185template<class T, std::size_t N, native::isa<> A>
186struct tracer<native::simd<ptr<T>, N, A>> : detail::structural_tracer {
187 template<class Visitor>
188 static hint_inline constexpr void trace(Visitor & visit, native::simd<ptr<T>, N, A> const & value)
189 noexcept requires detail::visits<ptr<T>, Visitor> {
190 for (std::size_t i = 0; i != N; ++i) detail::trace_parts(visit, value[i]);
191 }
194template<class V, std::size_t K>
195struct tracer<native::wide<V, K>> : detail::structural_tracer {
196 template<class Visitor>
197 static hint_inline constexpr void trace(Visitor & visit, native::wide<V, K> const & value)
198 noexcept requires detail::visits<V, Visitor> {
199 for (auto const & part : value.registers) detail::trace_parts(visit, part);
200 }
201};
202}
203
204namespace jam::detail {
205template<class U>
206using gather_bits = std::conditional_t<sizeof(U) == 4, std::uint32_t, std::uint64_t>;
207template<class T, class U, std::size_t N, native::isa<> A>
208concept gather_shape = !std::is_const_v<U> && !std::is_volatile_v<U> &&
209 (std::same_as<U, std::uint32_t> || std::same_as<U, std::int32_t> ||
210 std::same_as<U, std::uint64_t> || std::same_as<U, std::int64_t> ||
211 std::same_as<U, float> || std::same_as<U, double> || is_ptr<U>) &&
212 requires { sizeof(native::simd<U, N, A>); sizeof(native::simd<gather_bits<U>, N, A>); };
213
214// For supported nonvirtual inheritance, these ABIs encode data-member pointers
215// as flat byte displacements. Decode that documented
216// ABI representation rather than applying a member pointer to a fabricated object
217// or extracting a vector mask just to find a sample object. Extended MS member
218// pointer representations are deliberately rejected instead of guessed.
219template<class T, class U>
220hint_inline hint_const std::ptrdiff_t member_displacement(U T::* member) noexcept {
221 assert(member != nullptr);
222#if defined(_WIN32)
223 static_assert(sizeof(member) == sizeof(std::int32_t), "gather requires the flat MS data-member-pointer ABI");
224 return __builtin_bit_cast(std::int32_t, member);
225#else
226 static_assert(sizeof(member) == sizeof(std::ptrdiff_t), "gather requires the Itanium data-member-pointer ABI");
227 return __builtin_bit_cast(std::ptrdiff_t, member);
228#endif
229}
230}
231
232#if defined(__x86_64__) || defined(_M_X64)
233// Each generation has 31-bit nonnegative cell indices. Gather from its own
234// base under a disjoint mask, preserving the other generation's result lanes.
235#define JAM_GATHER_X86 \
236 if constexpr (A.has(native::x86_feature::avx2) && (N == 4 || N == 8 || N == 16)) { \
237 using V = native::simd<std::uint32_t, N, A>; \
238 using I = native::simd<std::int32_t, N, A>; \
239 auto const displacement = detail::member_displacement(member); \
240 auto const encoded = nodes.to_native(); \
241 auto const indices = __builtin_bit_cast(I, encoded & V{std::uint32_t{0x7fffffffu}}); \
242 for (unsigned generation = 0; generation != 2; ++generation) { \
243 auto const selected = active & ((encoded >> native::imm<31>) == V{generation}); \
244 auto const & g = generation ? arena->young() : arena->old(); \
245 auto const address = reinterpret_cast<std::uintptr_t>(g.data()) + displacement; \
246 auto const * base = reinterpret_cast<B const *>(address); \
247 if constexpr (sizeof(B) == 4) { \
248 if constexpr (A.has(native::x86_feature::avx512f)) \
249 result = native::mask_vpgatherdd<8>(result, selected, base, indices); \
250 else result = native::mask_vpgatherdd<8>(result, \
251 I::from_native(selected.to_native()), base, indices); \
252 } else { \
253 if constexpr (A.has(native::x86_feature::avx512f)) \
254 result = native::mask_vpgatherdq<8>(result, selected, base, indices); \
255 else { \
256 using M64 = native::simd<std::int64_t, N, A>; \
257 using D = std::int32_t __attribute__((ext_vector_type(N))); \
258 using Q = std::int64_t __attribute__((ext_vector_type(N))); \
259 auto const mask = __builtin_bit_cast(M64, __builtin_convertvector(__builtin_bit_cast(D, selected), Q)); \
260 result = native::mask_vpgatherdq<8>(result, mask, base, indices); \
261 } \
262 } \
263 } \
264 } else
265#else
266#define JAM_GATHER_X86
267#endif
268
269#define JAM_GATHERS(name, ISA, ...) \
270export namespace jam { \
271template<class T, class U, class Owner, std::size_t N, native::isa<> A> \
272 requires detail::gather_shape<T, U, N, A> && std::convertible_to<U Owner::*, U T::*> && \
273 (native::target<A, __VA_ARGS__> == native::target<ISA, __VA_ARGS__>) \
274 \
275 \
276 \
277[[nodiscard]] hint_inline auto gather(native::simd<ptr<T>, N, A> const & nodes, \
278 U Owner::* selected, typename native::simd<ptr<T>, N, A>::mask_type active) noexcept { \
279 U T::* member = selected; \
280 using B = detail::gather_bits<U>; \
281 using R = native::simd<B, N, A>; \
282 using Out = native::simd<U, N, A>; \
283 auto * arena = heap::current(); \
284 assert(member != nullptr); \
285 R result{}; \
286 JAM_GATHER_X86 \
287 { \
288 auto const bits = active.to_bitset(); \
289 std::array<B, N> values{}; \
290 for (std::size_t i = 0; i != N; ++i) if ((bits >> i) & 1) { \
291 auto const * p = arena->address(nodes[i]); \
292 std::memcpy(&values[i], &(p->*member), sizeof(B)); \
293 } \
294 result = R::load(values.data()); \
295 } \
296 if constexpr (detail::is_ptr<U>) return Out{result}; \
297 else return __builtin_bit_cast(Out, result); \
298} \
299 \
300template<class T, class U, class Owner, std::size_t N, native::isa<> A> \
301 requires detail::gather_shape<T, U, N, A> && std::convertible_to<U Owner::*, U T::*> && \
302 (native::target<A, __VA_ARGS__> == native::target<ISA, __VA_ARGS__>) \
303 \
304[[nodiscard]] hint_inline auto gather(native::simd<ptr<T>, N, A> const & nodes, \
305 U Owner::* member) noexcept { \
306 return gather(nodes, member, nodes != nullptr); \
307} \
308 \
309template<class T, class U, class Owner, std::size_t N, native::isa<> A, std::size_t K> \
310 requires detail::gather_shape<T, U, N, A> && std::convertible_to<U Owner::*, U T::*> && \
311 (native::target<A, __VA_ARGS__> == native::target<ISA, __VA_ARGS__>) \
312 \
313[[nodiscard]] hint_inline auto gather(native::wide<native::simd<ptr<T>, N, A>, K> const & nodes, \
314 U Owner::* member, native::wide<typename native::simd<ptr<T>, N, A>::mask_type, K> const & active) noexcept { \
315 auto const & [...p] = nodes.registers; \
316 auto const & [...m] = active.registers; \
317 return native::wide<native::simd<U, N, A>, K>{std::array<native::simd<U, N, A>, K>{gather(p, member, m)...}}; \
318} \
319 \
320template<class T, class U, class Owner, std::size_t N, native::isa<> A, std::size_t K> \
321 requires detail::gather_shape<T, U, N, A> && std::convertible_to<U Owner::*, U T::*> && \
322 (native::target<A, __VA_ARGS__> == native::target<ISA, __VA_ARGS__>) \
323 \
324[[nodiscard]] hint_inline auto gather(native::wide<native::simd<ptr<T>, N, A>, K> const & nodes, \
325 U Owner::* member) noexcept { \
326 using M = typename native::simd<ptr<T>, N, A>::mask_type; \
327 auto const & [...p] = nodes.registers; \
328 native::wide<M, K> active{std::array<M, K>{(p != nullptr)...}}; \
329 return gather(nodes, member, active); \
330} \
331}
332
333#ifdef JAM_DOXYGEN
334export namespace jam {
342template<class T, class U, class Owner, std::size_t N, native::isa<> A>
343native::simd<U, N, A> gather(native::simd<ptr<T>, N, A> const & nodes,
344 U Owner::* member, typename native::simd<ptr<T>, N, A>::mask_type active) noexcept;
347template<class T, class U, class Owner, std::size_t N, native::isa<> A>
348native::simd<U, N, A> gather(native::simd<ptr<T>, N, A> const & nodes, U Owner::* member) noexcept;
351template<class T, class U, class Owner, std::size_t N, native::isa<> A, std::size_t K>
352native::wide<native::simd<U, N, A>, K> gather(native::wide<native::simd<ptr<T>, N, A>, K> const & nodes,
353 U Owner::* member, native::wide<typename native::simd<ptr<T>, N, A>::mask_type, K> const & active) noexcept;
356template<class T, class U, class Owner, std::size_t N, native::isa<> A, std::size_t K>
357native::wide<native::simd<U, N, A>, K> gather(native::wide<native::simd<ptr<T>, N, A>, K> const & nodes,
358 U Owner::* member) noexcept;
359}
360#else
361NATIVE_TARGET_VARIANTS(pointer_gather, JAM_GATHERS, JAM_PTR_TARGETS)
362#endif
363#undef JAM_GATHERS
364#undef JAM_GATHER_X86
365#undef JAM_PTR_TARGETS
A typed heap offset, with no root registration or ownership.
Definition heap.ccm:329
constexpr void unsafe_assign(std::array< ptr< T >, N > &target, std::array< ptr< T >, N > const &source) noexcept
Bulk-copy pointer bits without registration; the caller owns the barrier.
Definition jam.ccm:34
constexpr void assign(std::array< ptr< T >, N > &target, std::array< ptr< T >, N > const &source) noexcept
Copy an array and register young lanes with one old-range check.
Definition jam.ccm:39
native::simd< U, N, A > gather(native::simd< ptr< T >, N, A > const &nodes, U Owner::*member, typename native::simd< ptr< T >, N, A >::mask_type active) noexcept
Gather a member from selected lanes; inactive lanes return zero or null.
Per-value tracing customization; values without a hook or manifest are leaves.An allocation-only clai...
Definition heap.ccm:140