native 0.0.1
Vectors, masks and wide register packs for C++26
Loading...
Searching...
No Matches
kernel_policies.h
1// SPDX-FileCopyrightText: 2026 Edward Kmett <ekmett@gmail.com>
2// SPDX-License-Identifier: BSD-2-Clause OR Apache-2.0
3#pragma once
4#include "native/abi_refinement.h"
5
6// Literal scopes shared by definition emitters and checked against the
7// computed common refinement below.
8#define NATIVE_KERNEL_TARGET_0 ""
9#define NATIVE_KERNEL_TARGET_1 "avx2,fma"
10#define NATIVE_KERNEL_TARGET_2 "avx2,fma,avx512f,avx512dq"
11#define NATIVE_KERNEL_TARGET_3 "avx2,fma,avx512f,avx512dq,avx512bw"
12#define NATIVE_KERNEL_TARGET_4 "avx2,fma,avx512f,avx512dq,avx512vl"
13#define NATIVE_KERNEL_TARGET_5 "avx2,fma,avx512f,avx512dq,avx512bw,avx512vl"
14#define NATIVE_KERNEL_TARGET_7 "avx2,fma,avx512f,avx512dq,avx512bw,avx512bf16"
15#define NATIVE_KERNEL_TARGET_9 "avx2,fma,avx512f,avx512dq,avx512bw,avx512vl,avx512bf16"
16#define NATIVE_KERNEL_TARGET_11 "avx2,fma,avx512f,avx512dq,avx512bw,avx512fp16"
17#define NATIVE_KERNEL_TARGET_13 "avx2,fma,avx512f,avx512dq,avx512bw,avx512vl,avx512fp16"
18#define NATIVE_KERNEL_TARGET_15 "avx2,fma,avx512f,avx512dq,avx512bw,avx512bf16,avx512fp16"
19#define NATIVE_KERNEL_TARGET_17 "avx2,fma,avx512f,avx512dq,avx512bw,avx512vl,avx512bf16,avx512fp16"
20#define NATIVE_KERNEL_TARGET_WASM "simd128"
21#define NATIVE_KERNEL_TARGET_20 "neon"
22#define NATIVE_KERNEL_TARGET_21 "neon,bf16"
23#define NATIVE_KERNEL_TARGET_22 "neon,fullfp16"
24#define NATIVE_KERNEL_TARGET_23 "neon,fullfp16,bf16"
25
26// These are declaration-scope obligations, including the requirements of
27// force-inline callees. A feature that does not change an instruction can still
28// be required by its callee's target attribute. Keep those requirements until
29// that callee is split into narrower scopes.
30namespace native::detail {
31 inline constexpr isa<x86> kernel_base=feature_closure(avx2 & x86_feature::avx512f & x86_feature::avx512dq);
32 inline constexpr isa<x86> kernel_bw=feature_closure(kernel_base & x86_feature::avx512bw);
33 inline constexpr isa<x86> kernel_vl=feature_closure(kernel_base & x86_feature::avx512vl);
34 inline constexpr isa<x86> kernel_bw_bf16=feature_closure(kernel_bw & x86_feature::avx512bf16);
35 inline constexpr isa<x86> kernel_bw_fp16=feature_closure(kernel_bw & x86_feature::avx512fp16);
36 inline constexpr isa<x86> kernel_bw_half=feature_closure(kernel_bw_bf16 & x86_feature::avx512fp16);
37 inline constexpr isa<x86> kernel_full_half=feature_closure(avx512_bf16 & x86_feature::avx512fp16);
38 inline constexpr isa<arm> kernel_neon_half=feature_closure(neon_bf16 & arm_feature::neon_fp16);
39
40 template<architecture Family> struct kernel_profiles {
41 using raw=isa_list<isa<Family>{}>;
42 using storage=raw;
43 using memory=raw;
44 static constexpr std::array targets{NATIVE_KERNEL_TARGET_0};
45 static constexpr std::array indices{15};
46 };
47 template<> struct kernel_profiles<x86> {
48 using raw=isa_list<avx512,kernel_bw,kernel_vl,kernel_base,avx2,isa<x86>{}>;
49 using storage=isa_list<kernel_full_half,kernel_bw_half,
50 avx512_bf16,kernel_bw_bf16,avx512_fp16,kernel_bw_fp16,
51 avx512,kernel_bw,kernel_vl,kernel_base,avx2,isa<x86>{}>;
52 // Native-half memory operations carry the full AVX-512 declaration scope.
53 using memory=isa_list<kernel_full_half,avx512_bf16,avx512_fp16,
54 avx512,kernel_bw,kernel_vl,kernel_base,avx2,isa<x86>{}>;
55 static constexpr std::array targets{
56 NATIVE_KERNEL_TARGET_17,NATIVE_KERNEL_TARGET_9,NATIVE_KERNEL_TARGET_13,
57 NATIVE_KERNEL_TARGET_5,NATIVE_KERNEL_TARGET_15,NATIVE_KERNEL_TARGET_7,
58 NATIVE_KERNEL_TARGET_11,NATIVE_KERNEL_TARGET_3,NATIVE_KERNEL_TARGET_4,
59 NATIVE_KERNEL_TARGET_2,NATIVE_KERNEL_TARGET_1,NATIVE_KERNEL_TARGET_0};
60 static constexpr std::array indices{0,1,2,3,4,5,6,7,8,9,10,15};
61 };
62 template<> struct kernel_profiles<arm> {
63 using raw=isa_list<neon,isa<arm>{}>;
64 using storage=isa_list<kernel_neon_half,neon_bf16,neon_fp16,neon,isa<arm>{}>;
65 using memory=storage;
66 static constexpr std::array targets{NATIVE_KERNEL_TARGET_23,NATIVE_KERNEL_TARGET_21,
67 NATIVE_KERNEL_TARGET_22,NATIVE_KERNEL_TARGET_20,NATIVE_KERNEL_TARGET_0};
68 static constexpr std::array indices{11,12,13,14,15};
69 };
70 template<>
71 struct kernel_profiles<wasm> {
72 using raw = isa_list<feature_closure(wasm_feature::simd128), isa<wasm>{}>;
73 using storage = raw;
74 using memory = raw;
75 static constexpr std::array targets{NATIVE_KERNEL_TARGET_WASM, NATIVE_KERNEL_TARGET_0};
76 static constexpr std::array indices{16, 15};
77 };
78 template<architecture Family> struct kernel_policy_family {
79 using profiles=kernel_profiles<Family>;
80 using refinement=abi_refinement<typename profiles::raw,typename profiles::storage>;
81 using policies=typename refinement::policies;
82 using memory_refinement=abi_refinement<policies,typename profiles::memory>;
83 static_assert(std::same_as<policies,typename memory_refinement::policies>);
84 static_assert(profiles::targets.size()==refinement::cells.size);
85 static_assert(profiles::indices.size()==refinement::cells.size);
86 static_assert([] {
87 for(std::size_t i=0;i<profiles::targets.size();++i)
88 if(target_features<Family>(profiles::targets[i])!=refinement::cells.records[i].requirements)
89 return false;
90 return true;
91 }());
92 };
93 using host_kernel_profiles=kernel_profiles<target_arch>;
94 using raw_kernel_policies=host_kernel_profiles::raw;
95 using wide_storage_policies=host_kernel_profiles::storage;
96 using memory_kernel_policies=host_kernel_profiles::memory;
97 using wide_kernel_refinement=kernel_policy_family<target_arch>::refinement;
98 using wide_kernel_policies=kernel_policy_family<target_arch>::policies;
99 using wide_memory_refinement=kernel_policy_family<target_arch>::memory_refinement;
100}
constexpr architecture x86
Select x86 feature metadata independently of the compilation target.
Definition config.h:26
constexpr auto feature_closure(A input) noexcept
Definition isa.h:538
constexpr isa< Family > target_features(std::string_view text) noexcept
Definition isa.h:561