Repository navigation
Expand file tree
/
Copy pathFeatures.lean
More file actions
169 lines (152 loc) · 7.48 KB
/
Copy pathFeatures.lean
File metadata and controls
169 lines (152 loc) · 7.48 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
import Std
import CIL.Safety.StepAttribute
/-!
# Feature profiles
The hardware capabilities a method can observe through `IsSupported` and
`IsHardwareAccelerated`. A `FeatureProfile` fixes them for a whole execution; `step` reads
it for feature tests and intrinsic availability.
Supported deployment policy: .NET 10 CoreCLR on 64-bit little-endian hosts.
IsSupported includes OS support and runtime disabling, not just CPU capability.
The runtime enables SSE4.2, AVX2/BMI and AVX-512 capabilities in whole groups;
AVX-512 VBMI is its own group, which requires AVX-512 and lies outside x86-64-v4.
AES is its own group on each architecture, requiring only X86Base or ArmBase; no x86-64 level
includes it.
ARM64 enables ArmBase and AdvSimd together. Vector128 acceleration follows the
baseline SIMD group; Vector256 acceleration requires AVX2 but may be disabled
independently by the preferred vector width.
Pinned runtime sources, measured x64/ARM64 versions, and migration guidance:
docs/RUNTIME-PROFILES.md
https://github.com/dotnet/runtime/blob/v10.0.13/src/coreclr/vm/codeman.cpp
https://github.com/dotnet/runtime/blob/v10.0.13/src/coreclr/tools/Common/JitInterface/ThunkGenerator/InstructionSetDesc.txt
`FeatureProfile.valid_iff_prerequisites` spells out every operation prerequisite `Valid` implies.
-/
namespace CIL
/-- A capability flag a method can test; each names one `FeatureProfile` field. -/
inductive Feature where
| advSimd | sse2 | ssse3 | sse42 | avx | avx2 | avx512F | avx512FVL | bmi1
| sse41 | avx512DQ | avx512DQVL | bmi2 | armBase64 | vector256Accelerated
| x86Base64 | vector128Accelerated | avx512Vbmi | aes | armAes
deriving DecidableEq, Repr
/-- The target instruction set; `scalar` is a profile with no ISA-specific capability. -/
inductive Architecture where
| scalar | arm64 | x64
deriving DecidableEq, Repr
/-- Runtime-visible flags are fixed throughout an execution. `Valid` constrains
runtime disabling to the supported .NET 10 CoreCLR capability groups. -/
structure FeatureProfile where
architecture : Architecture := .scalar
nativeWidth : Nat := 64
littleEndian : Bool := true
advSimd : Bool := false
sse2 : Bool := false
ssse3 : Bool := false
sse42 : Bool := false
avx : Bool := false
avx2 : Bool := false
avx512F : Bool := false
avx512FVL : Bool := false
bmi1 : Bool := false
sse41 : Bool := false
avx512DQ : Bool := false
avx512DQVL : Bool := false
bmi2 : Bool := false
armBase64 : Bool := false
vector256Accelerated : Bool := false
x86Base64 : Bool := false
vector128Accelerated : Bool := false
avx512Vbmi : Bool := false
aes : Bool := false
armAes : Bool := false
deriving DecidableEq, Repr
/-- The profile with every capability off, so only portable code paths run. -/
def FeatureProfile.scalar : FeatureProfile := {}
/-- The result of testing a feature under this profile. -/
def FeatureProfile.evaluate (p : FeatureProfile) : Feature → Bool
| .advSimd => p.advSimd
| .sse2 => p.sse2
| .ssse3 => p.ssse3
| .sse42 => p.sse42
| .avx => p.avx
| .avx2 => p.avx2
| .avx512F => p.avx512F
| .avx512FVL => p.avx512FVL
| .bmi1 => p.bmi1
| .sse41 => p.sse41
| .avx512DQ => p.avx512DQ
| .avx512DQVL => p.avx512DQVL
| .bmi2 => p.bmi2
| .armBase64 => p.armBase64
| .vector256Accelerated => p.vector256Accelerated
| .x86Base64 => p.x86Base64
| .vector128Accelerated => p.vector128Accelerated
| .avx512Vbmi => p.avx512Vbmi
| .aes => p.aes
| .armAes => p.armAes
-- One equation per `Feature`: a known feature test reduces to the profile field a caller's
-- fact names, and a symbolic `evaluate id` stays folded so a fact about it still applies.
open Lean Meta Elab Command in
run_cmd do
let some equations ← liftTermElabM <| getEqnsFor? ``CIL.FeatureProfile.evaluate
| throwError "FeatureProfile.evaluate has no equations"
for name in equations do
elabCommand (← `(attribute [cil_step, cil_feature] $(mkIdent name)))
/-- Declared capability domain: 64-bit little-endian execution, architecture
separation, and prerequisites used by the reachable SSE/AVX operations. This is
an explicit domain, rather than a claim to enumerate every possible CLR host.
`valid_iff_prerequisites` restates it with every prerequisite it implies spelled out. -/
def FeatureProfile.Valid (p : FeatureProfile) : Prop :=
p.nativeWidth = 64 ∧ p.littleEndian = true ∧
(p.x86Base64 = true → p.architecture = .x64) ∧
(p.armBase64 = true → p.architecture = .arm64) ∧
-- The x86 prerequisite chain; each runtime group below inherits it.
(p.avx512F = true → p.avx2 = true) ∧ (p.avx2 = true → p.avx = true) ∧
(p.avx = true → p.sse42 = true) ∧ (p.sse42 = true → p.sse2 = true) ∧
-- VBMI (.NET's AVX512v2 group) requires AVX-512, but x64-v4 machines differ on it.
(p.avx512Vbmi = true → p.avx512F = true) ∧
-- AES is a separate group on each architecture, but some hosts and switches disable it.
(p.aes = true → p.x86Base64 = true) ∧ (p.armAes = true → p.armBase64 = true) ∧
-- .NET 10 CoreCLR runtime groups; see docs/RUNTIME-PROFILES.md.
p.sse2 = p.x86Base64 ∧ p.ssse3 = p.sse41 ∧ p.sse41 = p.sse42 ∧
p.avx2 = p.bmi1 ∧ p.avx2 = p.bmi2 ∧
p.avx512F = p.avx512FVL ∧ p.avx512F = p.avx512DQ ∧ p.avx512F = p.avx512DQVL ∧
p.advSimd = p.armBase64 ∧ p.vector128Accelerated = (p.sse2 || p.advSimd) ∧
(p.vector256Accelerated = true → p.avx2 = true)
instance (p : FeatureProfile) : Decidable p.Valid := inferInstanceAs (Decidable (_ ∧ _))
@[simp] theorem FeatureProfile.scalar_evaluate (f : Feature) : scalar.evaluate f = false := by
cases f <;> rfl
theorem FeatureProfile.scalar_valid : scalar.Valid := by decide
/-- `Valid` is equivalent to its expanded form, which also lists every prerequisite that the
runtime groups and the x86 chain already imply; dropping those weakened nothing. -/
theorem FeatureProfile.valid_iff_prerequisites (p : FeatureProfile) : p.Valid ↔
p.nativeWidth = 64 ∧ p.littleEndian = true ∧
(p.advSimd = true → p.architecture = .arm64) ∧
((p.sse2 || p.ssse3 || p.sse42 || p.avx || p.avx2 || p.avx512F || p.avx512FVL || p.bmi1 ||
p.sse41 || p.avx512DQ || p.avx512DQVL || p.bmi2 || p.avx512Vbmi || p.aes) = true →
p.x86Base64 = true) ∧
(p.x86Base64 = true → p.architecture = .x64) ∧
(p.ssse3 = true → p.sse2 = true) ∧
(p.sse42 = true → p.sse2 = true ∧ p.ssse3 = true) ∧
(p.avx2 = true → p.avx = true) ∧
(p.avx512FVL = true → p.avx512F = true) ∧
(p.avx = true → p.sse42 = true) ∧
(p.avx512F = true → p.avx2 = true) ∧
(p.sse41 = true → p.ssse3 = true) ∧
(p.sse42 = true → p.sse41 = true) ∧
(p.avx512DQ = true → p.avx512F = true) ∧
(p.avx512DQVL = true → p.avx512DQ = true ∧ p.avx512FVL = true) ∧
(p.avx512Vbmi = true → p.avx512F = true ∧ p.avx512DQVL = true) ∧
(p.armBase64 = true → p.architecture = .arm64) ∧
(p.advSimd = true → p.armBase64 = true) ∧
(p.armAes = true → p.armBase64 = true) ∧
-- .NET 10 CoreCLR runtime groups; see docs/RUNTIME-PROFILES.md.
p.sse2 = p.x86Base64 ∧ p.ssse3 = p.sse41 ∧ p.sse41 = p.sse42 ∧
p.avx2 = p.bmi1 ∧ p.avx2 = p.bmi2 ∧
p.avx512F = p.avx512FVL ∧ p.avx512F = p.avx512DQ ∧ p.avx512F = p.avx512DQVL ∧
p.advSimd = p.armBase64 ∧ p.vector128Accelerated = (p.sse2 || p.advSimd) ∧
(p.vector256Accelerated = true → p.avx2 = true) := by
unfold FeatureProfile.Valid
cases p
simp only
constructor <;> intro h <;> grind
#print axioms FeatureProfile.valid_iff_prerequisites
end CIL