Skip to content

Commit a887561

Browse files
Aiur refactor (#289)
* Simplified types No more `ContextualType`. Instead, `TypedInner` carries an `escape` field * Infer term refactor into many smaller functions * And and Or U8 operations * fmt and clippy * bincode unmaintained
1 parent 09dd03b commit a887561

14 files changed

Lines changed: 457 additions & 276 deletions

File tree

Ix/Aiur/Bytecode.lean

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ inductive Op
2727
| u8ShiftRight : ValIdx → Op
2828
| u8Xor : ValIdx → ValIdx → Op
2929
| u8Add : ValIdx → ValIdx → Op
30+
| u8And : ValIdx → ValIdx → Op
31+
| u8Or : ValIdx → ValIdx → Op
3032
| debug : String → Option (Array ValIdx) → Op
3133
deriving Repr
3234

Ix/Aiur/Check.lean

Lines changed: 252 additions & 237 deletions
Large diffs are not rendered by default.

Ix/Aiur/Compile.lean

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def opLayout : Bytecode.Op → LayoutM Unit
120120
pushDegrees $ .replicate 8 1
121121
bumpAuxiliaries 8
122122
bumpLookups
123-
| .u8ShiftLeft _ | .u8ShiftRight _ | .u8Xor .. => do
123+
| .u8ShiftLeft _ | .u8ShiftRight _ | .u8Xor .. | .u8And .. | .u8Or .. => do
124124
pushDegree 1
125125
bumpAuxiliaries 1
126126
bumpLookups
@@ -246,7 +246,7 @@ partial def toIndex
246246
(term : TypedTerm) : StateM CompilerState (Array Bytecode.ValIdx) :=
247247
match term.inner with
248248
-- | .unsafeCast inner castTyp =>
249-
-- if typSize layoutMap castTyp != typSize layoutMap term.typ.unwrap then
249+
-- if typSize layoutMap castTyp != typSize layoutMap term.typ then
250250
-- panic! "Impossible cast"
251251
-- else
252252
-- toIndex layoutMap bindings (.mk term.typ inner)
@@ -335,32 +335,32 @@ partial def toIndex
335335
-- pushOp (Bytecode.Op.preimg layout.index out layout.inputSize) layout.inputSize
336336
-- | _ => panic! "should not happen after typechecking"
337337
| .proj arg i => do
338-
let typs := match arg.typ with
339-
| .evaluates (.tuple typs) => typs
338+
let typs := match (arg.typ, arg.escapes) with
339+
| (.tuple typs, false) => typs
340340
| _ => panic! "Should not happen after typechecking"
341341
let offset := (typs.extract 0 i).foldl (init := 0)
342342
fun acc typ => typSize layoutMap typ + acc
343343
let arg ← toIndex layoutMap bindings arg
344344
let length := typSize layoutMap typs[i]!
345345
pure $ arg.extract offset (offset + length)
346346
| .get arr i => do
347-
let eltTyp := match arr.typ with
348-
| .evaluates (.array typ _) => typ
347+
let eltTyp := match (arr.typ, arr.escapes) with
348+
| (.array typ _, false) => typ
349349
| _ => panic! "Should not happen after typechecking"
350350
let eltSize := typSize layoutMap eltTyp
351351
let offset := i * eltSize
352352
let arr ← toIndex layoutMap bindings arr
353353
pure $ arr.extract offset (offset + eltSize)
354354
| .slice arr i j => do
355-
let eltTyp := match arr.typ with
356-
| .evaluates (.array typ _) => typ
355+
let eltTyp := match (arr.typ, arr.escapes) with
356+
| (.array typ _, false) => typ
357357
| _ => panic! "Should not happen after typechecking"
358358
let eltSize := typSize layoutMap eltTyp
359359
let arr ← toIndex layoutMap bindings arr
360360
pure $ arr.extract (i * eltSize) (j * eltSize)
361361
| .set arr i val => do
362-
let eltTyp := match arr.typ with
363-
| .evaluates (.array typ _) => typ
362+
let eltTyp := match (arr.typ, arr.escapes) with
363+
| (.array typ _, false) => typ
364364
| _ => panic! "Should not happen after typechecking"
365365
let eltSize := typSize layoutMap eltTyp
366366
let arr ← toIndex layoutMap bindings arr
@@ -372,8 +372,8 @@ partial def toIndex
372372
let args ← toIndex layoutMap bindings arg
373373
pushOp (.store args)
374374
| .load ptr => do
375-
let size := match ptr.typ.unwrap with
376-
| .pointer typ => typSize layoutMap typ
375+
let size := match (ptr.typ, ptr.escapes) with
376+
| (.pointer typ, false) => typSize layoutMap typ
377377
| _ => unreachable!
378378
let ptr ← expectIdx ptr
379379
pushOp (.load size ptr) size
@@ -421,6 +421,14 @@ partial def toIndex
421421
let i ← expectIdx i
422422
let j ← expectIdx j
423423
pushOp (.u8Add i j) 2
424+
| .u8And i j => do
425+
let i ← expectIdx i
426+
let j ← expectIdx j
427+
pushOp (.u8And i j)
428+
| .u8Or i j => do
429+
let i ← expectIdx i
430+
let j ← expectIdx j
431+
pushOp (.u8Or i j)
424432
| .debug label term ret => do
425433
let term ← term.mapM (toIndex layoutMap bindings)
426434
modify fun stt => { stt with ops := stt.ops.push (.debug label term) }
@@ -454,7 +462,7 @@ partial def TypedTerm.compile
454462
modify fun stt => { stt with ops := stt.ops.push (.debug label term) }
455463
ret.compile returnTyp layoutMap bindings
456464
| .match term cases =>
457-
match term.typ.unwrapOr returnTyp with
465+
match term.typ with
458466
-- Also do this for tuple-like and array-like (one constructor only) datatypes
459467
| .tuple typs => match cases with
460468
| [(.tuple vars, branch)] => do

Ix/Aiur/Meta.lean

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ syntax "u8_shift_left" "(" trm ")" : trm
123123
syntax "u8_shift_right" "(" trm ")" : trm
124124
syntax "u8_xor" "(" trm ", " trm ")" : trm
125125
syntax "u8_add" "(" trm ", " trm ")" : trm
126+
syntax "u8_and" "(" trm ", " trm ")" : trm
127+
syntax "u8_or" "(" trm ", " trm ")" : trm
126128
syntax "dbg!" "(" str (", " trm)? ")" ";" (trm)? : trm
127129

128130
syntax trm "[" "@" noWs ident "]" : trm
@@ -219,6 +221,10 @@ partial def elabTrm : ElabStxCat `trm
219221
mkAppM ``Term.u8Xor #[← elabTrm i, ← elabTrm j]
220222
| `(trm| u8_add($i:trm, $j:trm)) => do
221223
mkAppM ``Term.u8Add #[← elabTrm i, ← elabTrm j]
224+
| `(trm| u8_and($i:trm, $j:trm)) => do
225+
mkAppM ``Term.u8And #[← elabTrm i, ← elabTrm j]
226+
| `(trm| u8_or($i:trm, $j:trm)) => do
227+
mkAppM ``Term.u8Or #[← elabTrm i, ← elabTrm j]
222228
| `(trm| dbg!($label:str $[, $t:trm]?); $[$ret:trm]?) => do
223229
let t ← match t with
224230
| none => mkAppOptM ``Option.none #[some (mkConst ``Term)]
@@ -360,6 +366,14 @@ where
360366
let i ← replaceToken old new i
361367
let j ← replaceToken old new j
362368
`(trm| u8_add($i, $j))
369+
| `(trm| u8_and($i:trm, $j:trm)) => do
370+
let i ← replaceToken old new i
371+
let j ← replaceToken old new j
372+
`(trm| u8_and($i, $j))
373+
| `(trm| u8_or($i:trm, $j:trm)) => do
374+
let i ← replaceToken old new i
375+
let j ← replaceToken old new j
376+
`(trm| u8_or($i, $j))
363377
| `(trm| dbg!($label:str $[, $t:trm]?); $[$ret:trm]?) => do
364378
let t' ← t.mapM $ replaceToken old new
365379
let ret' ← ret.mapM $ replaceToken old new

Ix/Aiur/Term.lean

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ inductive Term
9393
| u8ShiftRight : Term → Term
9494
| u8Xor : Term → Term → Term
9595
| u8Add : Term → Term → Term
96+
| u8And : Term → Term → Term
97+
| u8Or : Term → Term → Term
9698
| debug : String → Option Term → Term → Term
9799
deriving Repr, BEq, Hashable, Inhabited
98100

@@ -104,19 +106,6 @@ inductive Data
104106

105107
end
106108

107-
inductive ContextualType
108-
| evaluates : Typ → ContextualType
109-
| escapes : ContextualType
110-
deriving Repr, BEq, Inhabited
111-
112-
def ContextualType.unwrap : ContextualType → Typ
113-
| .escapes => panic! "term should not escape"
114-
| .evaluates typ => typ
115-
116-
def ContextualType.unwrapOr : ContextualType → Typ → Typ
117-
| .escapes => fun typ => typ
118-
| .evaluates typ => fun _ => typ
119-
120109
mutual
121110
inductive TypedTermInner
122111
| unit
@@ -149,12 +138,15 @@ inductive TypedTermInner
149138
| u8ShiftRight : TypedTerm → TypedTermInner
150139
| u8Xor : TypedTerm → TypedTerm → TypedTermInner
151140
| u8Add : TypedTerm → TypedTerm → TypedTermInner
141+
| u8And : TypedTerm → TypedTerm → TypedTermInner
142+
| u8Or : TypedTerm → TypedTerm → TypedTermInner
152143
| debug : String → Option TypedTerm → TypedTerm → TypedTermInner
153144
deriving Repr, Inhabited
154145

155146
structure TypedTerm where
156-
typ : ContextualType
147+
typ : Typ
157148
inner : TypedTermInner
149+
escapes : Bool
158150
deriving Repr, Inhabited
159151

160152
inductive TypedData

Tests/Aiur.lean

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,14 @@ def toplevel := ⟦
199199
(u8_add(i_xor_j, i), u8_add(i_xor_j, j))
200200
}
201201

202+
fn u8_add_and(i: G, j: G) -> G {
203+
u8_and(i, j)
204+
}
205+
206+
fn u8_add_or(i: G, j: G) -> G {
207+
u8_or(i, j)
208+
}
209+
202210
fn fold_matrix_sum(m: [[G; 2]; 2]) -> G {
203211
fold(0 .. 2, 0, |acc_outer, @i|
204212
fold(0 .. 2, acc_outer, |acc_inner, @j|
@@ -245,6 +253,8 @@ def aiurTestCases : List AiurTestCase := [
245253
⟨#[1, 2, 3, 4, 1, 2, 3, 4], .ofList [(#[0], ⟨0, 4⟩), (#[1], ⟨0, 8⟩)]⟩⟩,
246254
.noIO `shr_shr_shl_decompose #[87] #[0, 1, 0, 1, 0, 1, 0, 0],
247255
.noIO `u8_add_xor #[45, 131] #[219, 0, 49, 1],
256+
.noIO `u8_add_and #[45, 131] #[1],
257+
.noIO `u8_add_or #[45, 131] #[175],
248258
.noIO `fold_matrix_sum #[1, 2, 3, 4] #[10],
249259
]
250260

deny.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ ignore = [
7474
"RUSTSEC-2024-0384", # `instant` crate is unmaintained
7575
"RUSTSEC-2024-0370", # `proc-macro-error` crate is unmaintained
7676
"RUSTSEC-2023-0089", # `atomic-polyfill` crate is unmaintained
77+
"RUSTSEC-2025-0141", # `bincode` crate is unmaintained
78+
7779
#{ id = "RUSTSEC-0000-0000", reason = "you can specify a reason the advisory is ignored" },
7880
#"a-crate-that-is-yanked@0.1.1", # you can also ignore yanked crate versions if you wish
7981
#{ crate = "a-crate-that-is-yanked@0.1.1", reason = "you can specify why you are ignoring the yanked crate" },

src/aiur/bytecode.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ pub enum Op {
5353
U8ShiftRight(ValIdx),
5454
U8Xor(ValIdx, ValIdx),
5555
U8Add(ValIdx, ValIdx),
56+
U8And(ValIdx, ValIdx),
57+
U8Or(ValIdx, ValIdx),
5658
Debug(String, Option<Vec<ValIdx>>),
5759
}
5860

src/aiur/constraints.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use crate::aiur::{
1616
bytes1::{Bytes1, Bytes1Op},
1717
bytes2::{Bytes2, Bytes2Op},
1818
},
19-
memory_channel, u8_add_channel, u8_bit_decomposition_channel,
20-
u8_shift_left_channel, u8_shift_right_channel, u8_xor_channel,
19+
memory_channel, u8_add_channel, u8_and_channel, u8_bit_decomposition_channel,
20+
u8_or_channel, u8_shift_left_channel, u8_shift_right_channel, u8_xor_channel,
2121
};
2222

2323
type Expr = SymbolicExpression<G>;
@@ -415,6 +415,22 @@ impl Op {
415415
sel.clone(),
416416
state,
417417
),
418+
Op::U8And(i, j) => bytes2_constraints(
419+
*i,
420+
*j,
421+
&Bytes2Op::And,
422+
u8_and_channel(),
423+
sel.clone(),
424+
state,
425+
),
426+
Op::U8Or(i, j) => bytes2_constraints(
427+
*i,
428+
*j,
429+
&Bytes2Op::Or,
430+
u8_or_channel(),
431+
sel.clone(),
432+
state,
433+
),
418434
Op::IOSetInfo(..) | Op::IOWrite(_) | Op::Debug(..) => (),
419435
}
420436
}

src/aiur/execute.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,20 @@ impl Function {
271271
bytes2_execute(*i, *j, &Bytes2Op::Add, &mut map, record);
272272
}
273273
},
274+
ExecEntry::Op(Op::U8And(i, j)) => {
275+
if unconstrained {
276+
map.push(Bytes2::and(&map[*i], &map[*j]));
277+
} else {
278+
bytes2_execute(*i, *j, &Bytes2Op::And, &mut map, record);
279+
}
280+
},
281+
ExecEntry::Op(Op::U8Or(i, j)) => {
282+
if unconstrained {
283+
map.push(Bytes2::or(&map[*i], &map[*j]));
284+
} else {
285+
bytes2_execute(*i, *j, &Bytes2Op::Or, &mut map, record);
286+
}
287+
},
274288
ExecEntry::Op(Op::Debug(label, idxs)) => match idxs {
275289
None => println!("{label}"),
276290
Some(idxs) => {

0 commit comments

Comments
 (0)