Repository navigation
Expand file tree
/
Copy pathUnspecifiedLanes.lean
More file actions
210 lines (185 loc) · 9.96 KB
/
Copy pathUnspecifiedLanes.lean
File metadata and controls
210 lines (185 loc) · 9.96 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
import CIL.SIMD.PackedLanes
import CIL.Safety.FrameSetupLemmas
import CIL.Safety.Execution
/-!
# Unspecified vector lanes
The soundness of `Vector128.CreateScalarUnsafe`, whose upper lane the runtime leaves
unspecified: the model keeps only the low lane (`v128Low64`), and every intrinsic that
accepts such a value gives the same answer for every possible upper lane. Last, the checked
interpreter's opaque 128-bit locals, which hold such a value whole rather than as bytes.
-/
namespace CIL
/-- `concrete` is a 128-bit vector the runtime may hold where the model holds `value`: the
same bits, or for `v128Low64` any vector with that low lane. -/
def Vector128Represents (value : Value) (concrete : Vector.V128) : Prop :=
match value with
| .v128 bits => concrete = bits
| .v128Low64 low => Vector.lane64 concrete 0 = low
| _ => False
theorem scalarUnsafe_represents_every_upper (low upper : W64) :
Vector128Represents (.v128Low64 low) (Vector.pack128 low upper) := by
simp [Vector128Represents]
/-- No upper lane is fixed by `CreateScalarUnsafe`, so no proof may depend on one. -/
theorem scalarUnsafe_no_fixed_upper (low upper : W64) :
¬ ∀ concrete, Vector128Represents (.v128Low64 low) concrete →
Vector.lane64 concrete 1 = upper := by
intro fixed
have zero := fixed (Vector.pack128 low 0) (scalarUnsafe_represents_every_upper low 0)
have one := fixed (Vector.pack128 low 1) (scalarUnsafe_represents_every_upper low 1)
simp only [Vector.lane128_1] at zero one
have impossible : (0 : W64) = 1 := zero.trans one.symm
exact (by decide : (0 : W64) ≠ 1) impossible
theorem scalarUnsafe_extract_all_concretizations (low : W64) (concrete : Vector.V128)
(represents : Vector128Represents (.v128Low64 low) concrete) :
evalIntrinsic (.vector (.extract64 128)) [.v128 concrete, .i32 0] = some (.i64 low) := by
change Vector.lane64 concrete 0 = low at represents
change some (Value.i64 (Vector.lane64 concrete 0)) = some (Value.i64 low)
exact congrArg (fun lane => some (Value.i64 lane)) represents
theorem scalarUnsafe_reinterpret_preserves (low : W64) :
evalIntrinsic (.vector (.reinterpret 128)) [.v128Low64 low] = some (.v128Low64 low) := rfl
theorem scalarUnsafe_shiftLeft_all_concretizations (value : Vector.V256) (low : W64)
(concrete : Vector.V128) (represents : Vector128Represents (.v128Low64 low) concrete) :
evalIntrinsic (.avx2 .shl64Count) [.v256 value, .v128 concrete] =
evalIntrinsic (.avx2 .shl64Count) [.v256 value, .v128Low64 low] := by
change Vector.lane64 concrete 0 = low at represents
change some (Value.v256 (Vector.shiftLeftCount64 value (Vector.lane64 concrete 0))) =
some (Value.v256 (Vector.shiftLeftCount64 value low))
exact congrArg
(fun count => some (Value.v256 (Vector.shiftLeftCount64 value count))) represents
theorem scalarUnsafe_shiftRight_all_concretizations (value : Vector.V256) (low : W64)
(concrete : Vector.V128) (represents : Vector128Represents (.v128Low64 low) concrete) :
evalIntrinsic (.avx2 .shr64Count) [.v256 value, .v128 concrete] =
evalIntrinsic (.avx2 .shr64Count) [.v256 value, .v128Low64 low] := by
change Vector.lane64 concrete 0 = low at represents
change some (Value.v256 (Vector.shiftRightCount64 value (Vector.lane64 concrete 0))) =
some (Value.v256 (Vector.shiftRightCount64 value low))
exact congrArg
(fun count => some (Value.v256 (Vector.shiftRightCount64 value count))) represents
/-- `concrete` is a value the runtime may hold where the model holds `value`. -/
def Concretizes (value concrete : Value) : Prop :=
match value with
| .v128Low64 _ => ∃ bits, concrete = .v128 bits ∧ Vector128Represents value bits
| _ => concrete = value
/-- `Concretizes`, pointwise over two argument lists of equal length. -/
def ConcretizesAll : List Value → List Value → Prop
| [], [] => True
| value :: values, concrete :: concretes =>
Concretizes value concrete ∧ ConcretizesAll values concretes
| _, _ => False
/-- Whatever the unspecified upper lanes hold, the ISA result agrees with the model result. -/
theorem evalUnspecifiedIntrinsic_concretizations {op : Intrinsic} {args concrete : List Value}
{result : Value}
(evaluated : evalUnspecifiedIntrinsic op args = some result)
(represents : ConcretizesAll args concrete) :
∃ value, evalIntrinsic op concrete = some value ∧ Concretizes result value := by
unfold evalUnspecifiedIntrinsic at evaluated
split at evaluated
· cases evaluated
rcases concrete with _ | ⟨first, _ | ⟨_, _⟩⟩ <;>
simp only [ConcretizesAll, and_false, and_true] at represents
obtain ⟨bits, rfl, lane⟩ := represents
exact ⟨.v128 bits, rfl, bits, rfl, lane⟩
· rename_i low index
rcases concrete with _ | ⟨first, _ | ⟨second, _ | ⟨_, _⟩⟩⟩ <;>
simp only [ConcretizesAll, and_false, and_true] at represents
obtain ⟨⟨bits, rfl, lane⟩, rfl⟩ := represents
change Vector.lane64 bits 0 = low at lane
split at evaluated
· rename_i zero
cases evaluated
have zero : index.toNat = 0 := Nat.eq_of_beq_eq_true zero
refine ⟨.i64 low, ?_, rfl⟩
show (if index.toNat < 2 then some (Value.i64 (Vector.lane64 bits index.toNat))
else none) = _
rw [zero, lane]
rfl
· cases evaluated
all_goals first
| cases evaluated
rcases concrete with _ | ⟨first, _ | ⟨second, _ | ⟨_, _⟩⟩⟩ <;>
simp only [ConcretizesAll, and_false, and_true] at represents
obtain ⟨rfl, bits, rfl, lane⟩ := represents
change Vector.lane64 bits 0 = _ at lane
exact ⟨_, rfl, by simp only [Concretizes, lane]⟩
| cases evaluated
/-- The checked and the plain evaluator agree wherever unspecified lanes are consumed. -/
theorem evalUnspecifiedIntrinsic_sound {op : Intrinsic} {args : List Value} {result : Value}
(evaluated : evalUnspecifiedIntrinsic op args = some result) :
evalIntrinsic op args = some result := by
unfold evalUnspecifiedIntrinsic at evaluated
split at evaluated
all_goals first | (cases evaluated; rfl) | (cases evaluated) | exact evaluated
end CIL
namespace CIL.Safety
/-- Every intrinsic the checked semantics evaluates on unspecified lanes agrees with every
runtime concretisation of them; no other operation can succeed on such operands. -/
theorem checkedIntrinsic_unspecified {op : CIL.Intrinsic} {operands concrete : List CIL.Value}
{result : CIL.Value}
(unspecified : operands.any CIL.Value.hasUnspecified = true)
(checked : checkedIntrinsic op operands = .ok result)
(represents : CIL.ConcretizesAll operands concrete) :
∃ value, CIL.evalIntrinsic op concrete = some value ∧ CIL.Concretizes result value := by
unfold checkedIntrinsic at checked
simp only [unspecified, ite_true] at checked
split at checked
· cases checked
exact CIL.evalUnspecifiedIntrinsic_concretizations (by assumption) represents
· cases checked
theorem opaque128_load (memory : Memory) (value : Opaque128Value) :
loadLocal memory (.opaque128 (some value)) = .ok (.scalar value.value) := rfl
theorem opaque128_store_load (memory : Memory) (before : Option Opaque128Value)
(value : Opaque128Value) :
storeLocal memory (.opaque128 before) (.scalar value.value) =
.ok (.opaque128 (some value), memory) ∧
loadLocal memory (.opaque128 (some value)) = .ok (.scalar value.value) := by
cases value <;> exact ⟨rfl, rfl⟩
theorem opaque128_store_memory_identity (memory result : Memory) (before : Option Opaque128Value)
(value : Value) (slot : LocalSlot)
(stored : storeLocal memory (.opaque128 before) value = .ok (slot, result)) :
result = memory := by
cases value with
| scalar value =>
cases value with
| v128 bits =>
change (Except.ok (.opaque128 (some (.known bits)), memory) :
ExecutionResult (LocalSlot × Memory)) = .ok (slot, result) at stored
exact (congrArg Prod.snd (Except.ok.inj stored)).symm
| v128Low64 low =>
change (Except.ok (.opaque128 (some (.low64 low)), memory) :
ExecutionResult (LocalSlot × Memory)) = .ok (slot, result) at stored
exact (congrArg Prod.snd (Except.ok.inj stored)).symm
| _ => cases stored
| reference _ => cases stored
| span _ _ => cases stored
| bufferSpan _ | readOnlyReference _ | array _ | methodPointer _ | methodTable _
| object _ => cases stored
theorem opaque128_init_zero (memory : Memory) :
makeOpaque128 (.v128 0) memory = .ok (.opaque128 (some (.known 0)), [], memory) := rfl
theorem opaque128_init_unknown (memory : Memory) :
makeOpaque128 .unmodeled memory = .ok (.opaque128 none, [], memory) ∧
loadLocal memory (.opaque128 none) =
.error (.unsupported "Uninitialized opaque vector local") := ⟨rfl, rfl⟩
/-- Storing into an opaque 128-bit local changes no allocation, cell, view or static binding. -/
theorem opaque128_store_preserves_world (memory result : Memory) (before : Option Opaque128Value)
(value : Value) (slot : LocalSlot)
(stored : storeLocal memory (.opaque128 before) value = .ok (slot, result)) :
result.allocations = memory.allocations ∧ result.cells = memory.cells ∧
result.views = memory.views ∧ result.staticBindings = memory.staticBindings := by
rw [opaque128_store_memory_identity _ _ _ _ _ stored]
exact ⟨rfl, rfl, rfl, rfl⟩
#print axioms opaque128_store_load
#print axioms opaque128_store_memory_identity
#print axioms opaque128_store_preserves_world
end CIL.Safety
#print axioms CIL.scalarUnsafe_represents_every_upper
#print axioms CIL.scalarUnsafe_no_fixed_upper
#print axioms CIL.scalarUnsafe_shiftLeft_all_concretizations
#print axioms CIL.scalarUnsafe_shiftRight_all_concretizations
#print axioms CIL.scalarUnsafe_extract_all_concretizations
#print axioms CIL.scalarUnsafe_reinterpret_preserves
#print axioms CIL.evalUnspecifiedIntrinsic_concretizations
#print axioms CIL.evalUnspecifiedIntrinsic_sound
#print axioms CIL.Safety.checkedIntrinsic_unspecified
#print axioms CIL.Safety.opaque128_load
#print axioms CIL.Safety.opaque128_init_zero
#print axioms CIL.Safety.opaque128_init_unknown