Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Antiunification (originally Lakeroad PR https://github.com/uwsampl/lakeroad/pull/386) #3

Merged
merged 95 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
95 commits
Select commit Hold shift + click to select a range
489ca26
Add lakeroad-egglog
gussmith23 Aug 31, 2023
710d116
Add test infra and test
gussmith23 Aug 31, 2023
4362402
Add widths
gussmith23 Sep 3, 2023
7513f0b
Change arg order
gussmith23 Sep 3, 2023
3ed9a4a
Add permuter example
gussmith23 Sep 4, 2023
28d1cb3
Add simple typing
gussmith23 Sep 4, 2023
11a0b0f
Update permuter
gussmith23 Sep 4, 2023
de62469
Add typing test
gussmith23 Sep 4, 2023
670d5ef
Add ZeroExtend
gussmith23 Sep 4, 2023
247df0e
Fix bug
gussmith23 Sep 4, 2023
e0f6586
Add eq judgement
gussmith23 Sep 4, 2023
b2d28d7
Make mux an Op3
gussmith23 Sep 4, 2023
56187a0
Add more typing judgements
gussmith23 Sep 4, 2023
98947c3
Update permuter test
gussmith23 Sep 4, 2023
6b7883e
Comment
gussmith23 Sep 4, 2023
e47784d
Add commented code for producing SVGs
gussmith23 Sep 4, 2023
b78514e
Merge remote-tracking branch 'uwsampl/main' into gussmith23/egglog-ex…
gussmith23 Sep 24, 2023
65e08d6
Change comment
gussmith23 Sep 24, 2023
62d2f10
Make Ops expensive to extract
gussmith23 Sep 24, 2023
ad7e39a
Add LUTs; add a bunch of rewrites
gussmith23 Sep 24, 2023
773d08f
Upgrade tests
gussmith23 Sep 24, 2023
e94688c
Do more rewrites/extract in test
gussmith23 Sep 24, 2023
11a955e
Move code
gussmith23 Sep 25, 2023
da94835
Merge remote-tracking branch 'uwsampl/main' into gussmith23/egglog-ex…
gussmith23 Oct 2, 2023
4555e2a
Put everything in one ruleset
gussmith23 Oct 2, 2023
8ddca3e
Simplify nested extracts
gussmith23 Oct 2, 2023
8c96c9a
Simplify concats of nested extracts
gussmith23 Oct 2, 2023
51ea227
Add more simplification rewrites
gussmith23 Oct 2, 2023
3fa9f80
Add check for regression
gussmith23 Oct 2, 2023
0327bd7
Split back into rulesets
gussmith23 Oct 2, 2023
b992418
Begin an egglog->verilator compiler
gussmith23 Oct 2, 2023
1cd4a1c
More work on Verilog compiler
gussmith23 Oct 3, 2023
856ddff
More work
gussmith23 Oct 4, 2023
fcb5868
Add Yosys submodule w/ Lakeroad Egglog backend
gussmith23 Oct 7, 2023
a1a21e0
Add agilex_alm skeleton and diagram
gussmith23 Oct 7, 2023
c54d9ea
Finish initial impl of ALM model
gussmith23 Oct 7, 2023
3ea60c1
Whoops, get rid of copilot bug
gussmith23 Oct 7, 2023
2572751
Rewrite to explicitly use shift and cast; avoids
gussmith23 Oct 7, 2023
f7c1bfd
Update Yosys
gussmith23 Oct 7, 2023
d5a1985
fmt
gussmith23 Oct 7, 2023
faac77f
Add Shr and Xor
gussmith23 Oct 8, 2023
a635af7
Add agilex ALM test (doesn't do anything for now)
gussmith23 Oct 8, 2023
b0837a9
begin structuring rewrite generation
gussmith23 Oct 12, 2023
5e3d8c4
More work
gussmith23 Oct 13, 2023
2d23b63
more work
gussmith23 Oct 23, 2023
f72cfb3
Add more typing rules (wire, xor, shr) + test (#379)
thiskappaisgrey Nov 1, 2023
1f3c8d9
Cleanup
gussmith23 Nov 5, 2023
b9df9e4
Add new lakeroad-egglog tests to main test script
gussmith23 Nov 5, 2023
7d6f9b2
Merge remote-tracking branch 'uwsampl/main' into gussmith23/egglog-ex…
gussmith23 Nov 5, 2023
900bc14
Fork new version of Lakeroad egglog definition for AU experiments
gussmith23 Nov 5, 2023
e552aaf
WIP
gussmith23 Nov 6, 2023
22303f5
Merge remote-tracking branch 'uwsampl/main' into gussmith23/lr-egglog…
gussmith23 Nov 6, 2023
f9b6026
Merge remote-tracking branch 'uwsampl/main' into gussmith23/lr-egglog…
gussmith23 Nov 10, 2023
bb94288
Add 'debruijnify', which converts a list of expressions into potentia…
gussmith23 Nov 12, 2023
1249b65
Adds initial module-enumeration rewrites
gussmith23 Nov 13, 2023
53c084f
Add anotehr rule
gussmith23 Nov 14, 2023
a2073e3
Add another rewrite
gussmith23 Nov 14, 2023
70cd8ae
More rewrites
gussmith23 Nov 14, 2023
4e94a6b
Add commented out svg code
gussmith23 Nov 14, 2023
afb73a7
Move lakeroad import stuff into its own function
gussmith23 Nov 20, 2023
63ed779
Update yosys
gussmith23 Nov 20, 2023
18f5fd0
Add new permuter test
gussmith23 Nov 20, 2023
21fcb17
Run enumeration rewrites
gussmith23 Nov 20, 2023
1d7aa1c
Add function to generate rewrites
gussmith23 Nov 21, 2023
498d410
Generate rewrites programmatically
gussmith23 Nov 21, 2023
57c8ac1
run til saturation
gussmith23 Nov 21, 2023
7ed6d37
Add typing rules
gussmith23 Nov 22, 2023
6ff4214
Just run to saturation without checking
gussmith23 Nov 23, 2023
e8f3004
Add module enumeration rewrite for Var
gussmith23 Nov 23, 2023
3e1e0ce
Rewrite to split up concats
gussmith23 Nov 23, 2023
f605de8
run expansion rewrites
gussmith23 Nov 23, 2023
e15c5b5
Add Var_ to prevent loops in `apply`s
gussmith23 Nov 27, 2023
7575865
Add Cargo.lock
gussmith23 Nov 30, 2023
317cba6
Move new LR syntax to main lakeroad.egg file
gussmith23 Dec 2, 2023
bf2879a
Add revision for egglog
gussmith23 Dec 2, 2023
06be4c2
Add test for finding loop
gussmith23 Dec 3, 2023
c74e9cf
Remove lockfile to see if it fixes CI
gussmith23 Dec 6, 2023
25aa9f5
Merge branch 'main' into gussmith23/lr-egglog-antiunify
gussmith23 Dec 19, 2023
0a048ca
Add old churchroad content
gussmith23 Feb 7, 2024
172687a
Lots of deleting and moving
gussmith23 Feb 7, 2024
20fe828
Update workflow
gussmith23 Feb 7, 2024
890c3fa
Cut down Dockerfile
gussmith23 Feb 8, 2024
2a9f648
Fix command
gussmith23 Feb 8, 2024
1b0b956
Run workflow on GitHub runners
gussmith23 Feb 10, 2024
9b02bfa
Move lakeroad-egglog files to top level
gussmith23 Feb 10, 2024
2bb1361
Fix test script; dump README
gussmith23 Feb 10, 2024
552888d
Merge branch 'gussmith23/2024-02-07-convert-lakeroad-to-churchroad' i…
gussmith23 Feb 10, 2024
2f7bed4
Merge remote-tracking branch 'origin/main' into gussmith23/lr-egglog-…
gussmith23 Feb 19, 2024
807f75c
undo
gussmith23 Feb 19, 2024
07ab987
Remove Yosys submodule after #4
gussmith23 Feb 19, 2024
d1f04b3
Fixes after merge
gussmith23 Feb 19, 2024
cf2c4c1
Rename symbol
gussmith23 Feb 19, 2024
f167411
remove lexpr
gussmith23 Feb 19, 2024
d4cc221
Comment changes, renamings
gussmith23 Feb 19, 2024
0c34ddb
`cargo update`
gussmith23 Feb 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,299 changes: 1,299 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
egglog = { git = "https://github.com/egraphs-good/egglog" }
log = "0.4.20"
egglog = { git = "https://github.com/egraphs-good/egglog", rev = "73a76dc9228ce24291b3e41bbbca14eddd10cd5e" }
log = "0.4.20"
306 changes: 162 additions & 144 deletions egglog_src/lakeroad.egg
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
;;; Lakeroad language definition, for importing before running other tests.
;;; Churchroad language definition with antiunification experiments.

(sort IVec (Vec i64))

;;; Forward-declare Exprs and declare ExprVec.
(sort Expr)
(sort ExprVec (Vec Expr))

; Ops
(datatype Op
Expand All @@ -8,57 +14,114 @@
(Shr)
; Returns a bitvector of width 1.
(Eq)
; Bitwise not.
(Not)
(LogicNot)
; (Mux select-expr expr expr)
(Mux))
(Mux)

; (Op1 (Extract high low) expr)
; Extraction from a bitvector.
(Extract i64 i64)

; Sketch Ops
(datatype SketchOp
(LUT4))
; (Op2 (Concat) top-expr bottom-expr)
; Concatenation of two bitvectors.
(Concat)

; (Op2 (Reg init-value) clock-expr data-expr)
(Reg i64)

; Our language. Note that we do not explicitly use IDs as in the Lakeroad paper
; formalization. The IDs are still there, though: they're the
; eclass IDs!
(datatype Expr
; (BV value bitwidth)
; (Op0 (BV value bitwidth))
(BV i64 i64)
; (Var name bitwidth)
(Var String i64)
; (OpN op input-expr...)
(Op1 Op Expr :cost 100000000)
(Op2 Op Expr Expr :cost 100000000)
(Op3 Op Expr Expr Expr :cost 100000000)
; (Reg default-value clock-expr d-expr)
(Reg i64 Expr Expr)
; (Wire name bitwidth)
; Wire is a placeholder for another expression. See below how
; wires are the key to creating cyclic graphs.
(Wire String i64)
; (Extract high low expr)
; Extracts expr[high:low] using inclusive ranges as in Rosette/Verilog.
(Extract i64 i64 Expr)
; (Concat expr expr)
(Concat Expr Expr)
; (ZeroExtend expr bitwidth)
(ZeroExtend Expr i64)

; (Sketch sketch-id input-expr...)
; A sketch node. This is a proposal for a potential expression, that will
; need to be solved for/made concrete later on.
(Sketch1 SketchOp Expr)
(Sketch2 SketchOp Expr Expr)
(Sketch3 SketchOp Expr Expr Expr)
(Sketch4 SketchOp Expr Expr Expr Expr)

; (Op1 (ZeroExtend bitwidth) expr)
(ZeroExtend i64)
)

(datatype Graph
;;; Hole with a bitwidth.
;(Hole i64)
;;; TODO need to implement bitwidth. for now too lazy to implement the typechecking.
(Hole)

;;;
(Op0_ Op)
(Op1_ Op Graph)
(Op2_ Op Graph Graph)
(Op3_ Op Graph Graph Graph)
)

;;; Types for Lakeroad expressions.
;;; Module declaration
(sort Module)
;;; IVec is an vector of integers holding De Bruijn indices.
(function MakeModule (Graph IVec) Module)

; Our language. Note that we do not explicitly use IDs as in the Lakeroad paper
; (FPGA Technology Mapping via Program Synthesis, Smith et. al., ASPLOS 2024)
; formalization. The IDs are still there, though: they're the eclass IDs!

; (Var name bitwidth)
(function Var (String i64) Expr)
;;; I'm unsure whether we need this. This is the variant of Var we use as
;;; arguments to `apply` nodes. This prevents the loop induced by putting
;;; `(Var a 8) = (apply (MakeModule (Hole) [0]) [(Var a 8)])`
;;; into the graph. If we put this in the graph, you could infinitely extract
;;; `(apply (MakeModule ...) (apply (MakeModule ...) (apply (MakeModule ...) ...)))`
;;; (which captures the fact that you can apply the identity function as many
;;; times as you want).
(function Var_ (String i64) Expr)

;;; "Direct" representation of programs:
;;; Expressing programs via direct application of Ops to leaf nodes (Vars
;;; and Consts).
; (OpN op input-expr...)
(function Op0 (Op) Expr)
(function Op1 (Op Expr) Expr)
(function Op2 (Op Expr Expr) Expr)
(function Op3 (Op Expr Expr Expr) Expr)


; (Wire name bitwidth)
(function Wire (String i64) Expr)

(function apply (Module ExprVec) Expr)

;;; Types for Churchroad expressions.
(datatype Type
;;; Bitvector type.
(Bitvector i64))
(Bitvector i64)
;;; Module type: when `apply`ed, gives back the indicated type. This could be
;;; a lot more rigorous. Currently will not allow for checking correct input
;;; types.
(ModuleType Type))

;;; Indicates that a Lakeroad expression has a given type.
;;; Indicates that a Churchroad expression has a given type.
(relation HasType (Expr Type))

;;; Indicates that all input and output bitwidths must match for this type of
;;; op.
(relation AllBitwidthsMatch (Op))
(AllBitwidthsMatch (And))
(AllBitwidthsMatch (Or))
(AllBitwidthsMatch (Xor))
(AllBitwidthsMatch (Shr))
; Have to write this one as a rule, unfortunately.
(ruleset core)
(rule ((Reg n)) ((AllBitwidthsMatch (Reg n))) :ruleset core)

;;; Indicates that, for the op, the input bitwidths must match, and the output
;;; bitwidth is the indicated constant.
(relation InputBitwidthsMatchOutputBitwidthConst (Op i64))
(InputBitwidthsMatchOutputBitwidthConst (Eq) 1)

;;; Bitwise: Indicates that an op `(op a b ...)` can be written
;;; `(concat (op a[0] b[0] ...) (op a[1] b[1] ...) ...)`.
(relation Bitwise (Op))
(Bitwise (And))
(Bitwise (Or))
(Bitwise (Xor))


;;; Typing judgements.
(ruleset typing)
(rule
Expand All @@ -70,45 +133,50 @@
((HasType (Var name bw) (Bitvector bw)))
:ruleset typing)
(rule
((BV name bw))
((HasType (BV name bw) (Bitvector bw)))
((Op0 (BV val bw)))
((HasType (Op0 (BV val bw)) (Bitvector bw)))
:ruleset typing)
(rule
((Reg default clk-expr d-expr)
; Currently require clock to be one bit.
(HasType clk-expr (Bitvector 1))
(HasType d-expr (Bitvector bw)))
((HasType (Reg default clk-expr d-expr) (Bitvector bw)))
((Op1 op i0)
(HasType i0 (Bitvector bw))
(AllBitwidthsMatch op))
((HasType (Op1 op i0) (Bitvector bw)))
:ruleset typing)
(rule
((Op2 (And) a-expr b-expr)
(HasType a-expr (Bitvector bw))
(HasType b-expr (Bitvector bw)))
((HasType (Op2 (And) a-expr b-expr) (Bitvector bw)))
((Op2 op i0 i1)
(HasType i0 (Bitvector bw))
(HasType i1 (Bitvector bw))
(AllBitwidthsMatch op))
((HasType (Op2 op i0 i1) (Bitvector bw)))
:ruleset typing)
(rule
((Op2 (Or) a-expr b-expr)
(HasType a-expr (Bitvector bw))
(HasType b-expr (Bitvector bw)))
((HasType (Op2 (Or) a-expr b-expr) (Bitvector bw)))
((Op3 op i0 i1 i2)
(HasType i0 (Bitvector bw))
(HasType i1 (Bitvector bw))
(HasType i2 (Bitvector bw))
(AllBitwidthsMatch op))
((HasType (Op3 op i0 i1 i2) (Bitvector bw)))
:ruleset typing)
(rule
((Op2 (Xor) a-expr b-expr)
(HasType a-expr (Bitvector bw))
(HasType b-expr (Bitvector bw)))
((HasType (Op2 (Xor) a-expr b-expr) (Bitvector bw)))
((Op1 op i0)
(HasType i0 (Bitvector bw))
(InputBitwidthsMatchOutputBitwidthConst op out-bw))
((HasType (Op1 op i0) (Bitvector out-bw)))
:ruleset typing)
(rule
((Op2 (Shr) a-expr b-expr)
(HasType a-expr (Bitvector bw))
(HasType b-expr (Bitvector bw)))
((HasType (Op2 (Shr) a-expr b-expr) (Bitvector bw)))
((Op2 op i0 i1)
(HasType i0 (Bitvector bw))
(HasType i1 (Bitvector bw))
(InputBitwidthsMatchOutputBitwidthConst op out-bw))
((HasType (Op2 op i0 i1) (Bitvector out-bw)))
:ruleset typing)
(rule
((Op2 (Eq) a-expr b-expr)
(HasType a-expr (Bitvector bw))
(HasType b-expr (Bitvector bw)))
((HasType (Op2 (Eq) a-expr b-expr) (Bitvector 1)))
((Op3 op i0 i1 i2)
(HasType i0 (Bitvector bw))
(HasType i1 (Bitvector bw))
(HasType i2 (Bitvector bw))
(InputBitwidthsMatchOutputBitwidthConst op out-bw))
((HasType (Op3 op i0 i1 i2) (Bitvector out-bw)))
:ruleset typing)
(rule
((Op3 (Mux) sel-expr a-expr b-expr)
Expand All @@ -118,89 +186,39 @@
((HasType (Op3 (Mux) sel-expr a-expr b-expr) (Bitvector bw)))
:ruleset typing)
(rule
((Concat a-expr b-expr)
((Op2 (Concat) a-expr b-expr)
(HasType a-expr (Bitvector m))
(HasType b-expr (Bitvector n)))
((HasType (Concat a-expr b-expr) (Bitvector (+ m n))))
((HasType (Op2 (Concat) a-expr b-expr) (Bitvector (+ m n))))
:ruleset typing)
(rule
((Extract high low expr)
(HasType expr (Bitvector n)))
((HasType (Extract high low expr) (Bitvector (+ 1 (- high low)))))
((Op1 (Extract high low) expr)
(HasType expr (Bitvector n))
(>= 0 low)
(< high n))
((HasType (Op1 (Extract high low) expr) (Bitvector (+ 1 (- high low)))))
:ruleset typing)
(rule
((ZeroExtend expr bitwidth))
((HasType (ZeroExtend expr bitwidth) (Bitvector bitwidth)))
((Op1 (ZeroExtend bitwidth) expr))
((HasType (Op1 (ZeroExtend bitwidth) expr) (Bitvector bitwidth)))
:ruleset typing)


;;; LUT sketch proposal rewrites.
(ruleset sketch-proposal)
(rule
((Op1 op e) (HasType (Op1 op e) (Bitvector 1)) (HasType e (Bitvector n)) (<= n 4))
((union (Op1 op e) (Sketch1 (LUT4) e)))
:ruleset sketch-proposal)
(rule
((Op2 op e1 e2) (HasType (Op2 op e1 e2) (Bitvector 1)) (HasType e1 (Bitvector m)) (HasType e2 (Bitvector n)) (<= (+ m n) 4))
((union (Op2 op e1 e2) (Sketch1 (LUT4) (Concat e1 e2))))
:ruleset sketch-proposal)
(rule
((Op3 op e1 e2 e3) (HasType (Op3 op e1 e2 e3) (Bitvector 1)) (HasType e1 (Bitvector m)) (HasType e2 (Bitvector n)) (HasType e3 (Bitvector o)) (<= (+ (+ m n) o) 4))
((union (Op3 op e1 e2 e3) (Sketch1 (LUT4) (Concat (Concat e1 e2) e3))))
:ruleset sketch-proposal)


;;; Misc rewrites.
(ruleset misc)
;;; Split up Mux op.
;;; Given a multibit mux expr, split it up into one-bit mux exprs.
(rule
((Op3 (Mux) sel-expr a-expr b-expr)
(HasType sel-expr (Bitvector 1))
(HasType a-expr (Bitvector n))
(HasType b-expr (Bitvector n))
(> n 1))
((union
(Op3 (Mux) sel-expr a-expr b-expr)
(Concat
(Op3 (Mux) sel-expr (Extract (- n 1) (- n 1) a-expr) (Extract (- n 1) (- n 1) b-expr))
(Op3 (Mux) sel-expr (Extract (- n 2) 0 a-expr) (Extract (- n 2) 0 b-expr)))))
:ruleset misc)

; Simplify nested extracts.
(rewrite
(Extract hi1 lo1 (Extract hi0 lo0 expr))
(Extract (+ (+ lo0 lo1) (- hi1 lo1)) (+ lo0 lo1) expr)
:ruleset misc)

; Simplify Concats of contiguous Extracts.
(rule
((Concat (Extract hi1 lo1 expr) (Extract hi0 lo0 expr))
(= lo1 (+ hi0 1)))
((union
(Concat (Extract hi1 lo1 expr) (Extract hi0 lo0 expr))
(Extract hi1 lo0 expr)))
:ruleset misc)

; Simplify Extracts of Concats.
(rule
((Extract hi lo (Concat e1 e0))
(HasType e0 (Bitvector bw0))
(HasType e1 (Bitvector bw1))
;;; Check that the extract is only extracting from e1.
(< hi (+ bw0 bw1))
(>= lo bw0))
((union
(Extract hi lo (Concat e1 e0))
(Extract (- hi bw0) (- lo bw0) e1)))
:ruleset misc)
((Op1 (Reg init) expr)
(HasType expr (Bitvector n)))
((HasType (Op1 (Reg init) expr) (Bitvector n)))
:ruleset typing)

;;; Rewrites that are likely to expand the egraph.
(ruleset expansion)
(rule
((Extract hi lo (Concat e1 e0))
(HasType e0 (Bitvector bw0))
(HasType e1 (Bitvector bw1))
;;; Check that the extract is only extracting from e0.
(< hi bw0))
((union
(Extract hi lo (Concat e1 e0))
(Extract hi lo e0)))
:ruleset misc)
((Op2 op e1 e2)
(Bitwise op)
(HasType (Op2 op e1 e2) (Bitvector n))
(> n 1))
((union
(Op2 op e1 e2)
(Op2 (Concat)
(Op2 op
(Op1 (Extract (- n 1) 1) e1) (Op1 (Extract (- n 1) 1) e2))
(Op2 op
(Op1 (Extract 0 0) e1) (Op1 (Extract 0 0) e2))))))
2 changes: 1 addition & 1 deletion run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ set -e

cargo test

./yosys-plugin/run_tests.sh
./yosys-plugin/run_tests.sh
Loading