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

First Optimizations #76

Merged
merged 149 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
149 commits
Select commit Hold shift + click to select a range
2a24203
add a restorative optimization for "all"
countvajhula Sep 16, 2022
7ba3ff1
simple cases of "deforestation" for values
countvajhula Sep 16, 2022
86dc145
basic optimization loop
countvajhula Sep 16, 2022
8696cf2
merge amps in sequence
countvajhula Sep 16, 2022
098e716
flatten nested compositions (associative law)
countvajhula Sep 16, 2022
f540e03
eliminate superfluous identity flows
countvajhula Sep 16, 2022
98559a5
incorporate various identities as optimizations
countvajhula Sep 16, 2022
2a4b4bd
add some comments
countvajhula Sep 16, 2022
d2a5bfd
merge `pass` filters in sequence by conjoining the predicates
countvajhula Sep 16, 2022
c6fbe1e
note a todo
countvajhula Sep 16, 2022
ea17feb
more comments
countvajhula Sep 16, 2022
71d7ec4
collapse deterministic conditionals
countvajhula Sep 16, 2022
d4a9651
note about optimizing "active" components of optimized expansions
countvajhula Sep 16, 2022
e452a8d
use core language words instead of symbols in optimizations
countvajhula Sep 28, 2022
1080bc9
don't optimize prisms where it expects a list input specifically
countvajhula Sep 29, 2022
83f28c9
rudimentary deforestation
countvajhula Sep 30, 2022
06c2d7a
remove invalid deforestation optimization (noted in CR)
countvajhula Oct 5, 2022
0c5b680
remove invalid optimization (CR)
countvajhula Oct 12, 2022
b29884a
add tests to check known counterexamples to seeming equivalences
countvajhula Oct 12, 2022
60be2a8
collapse singleton threading form
countvajhula Mar 1, 2023
ab173b2
Commit wip from today's meeting
countvajhula Aug 4, 2023
f948395
wip from today's meeting (ignition!)
countvajhula Aug 11, 2023
c698e26
implement filter-map using multiple values instead of allocated data
countvajhula Aug 11, 2023
b19783d
add version from ben with partially evaluated match, for completeness
countvajhula Aug 12, 2023
0351c39
add the hand-coded iteration (upper bound on performance)
countvajhula Aug 15, 2023
90ae5cc
add `range-map-sum` benchmark (used in St-Amour's writeup)
countvajhula Aug 18, 2023
191346c
minimally incorporate stream fusion into the compiler
countvajhula Aug 18, 2023
1b63193
restore restorative optimization that was accidentally dropped
countvajhula Aug 24, 2023
847fce1
test for the `filter-map` functional pipeline
countvajhula Aug 26, 2023
2240de1
use `racket -y` in running all tests to recompile if needed
countvajhula Aug 26, 2023
cb86eaa
fix stream fusion (from today's qi meeting)
countvajhula Aug 26, 2023
fdcd094
tidy - remove deforestation wip from benchmarking module
countvajhula Sep 1, 2023
ca94178
WIP from today's meeting
countvajhula Sep 2, 2023
91f2b46
use the much-anticipated syntax-spec-v1 from the package index :)
countvajhula Sep 2, 2023
8eb3dad
a couple more nonlocal testcases, using folds
countvajhula Sep 15, 2023
161bd26
add starter tests to validate compiler rewrite rules
countvajhula Sep 15, 2023
be0e6ed
benchmark for functional pipeline using foldr
countvajhula Sep 15, 2023
253e5fe
foldr fusable stream terminator
countvajhula Sep 16, 2023
7265eb9
WIP from today's meeting
countvajhula Sep 22, 2023
8fc8424
remove old unused deforestation code
countvajhula Sep 29, 2023
17c3bc5
Implement `foldl` as a stream
countvajhula Sep 30, 2023
7f67806
fix compiler tests by adding chirality
countvajhula Oct 1, 2023
9298580
consistent naming in rewrite rules
countvajhula Oct 5, 2023
f78af67
add a benchmark using `foldl`
countvajhula Oct 6, 2023
5a8785a
rename stream-related syntax classes for clarity
countvajhula Oct 7, 2023
6d2d258
Use macro-debugger-emit to see the process-bindings expansion step in…
dzoep Oct 7, 2023
15df450
Merge remote-tracking branch 'refs/remotes/origin-old/first-optimizat…
dzoep Oct 7, 2023
afd1f5f
Make fix procedure compatible with syntax-parse rules following the f…
dzoep Oct 7, 2023
60a81e7
Merge pull request #111 from dzoep/first-optimizations
countvajhula Oct 13, 2023
31f34bb
macros for emitting expansion events for the macro stepper
countvajhula Oct 14, 2023
4bb5622
Merge pull request #1 from drym-org/first-optimizations
dzoep Oct 20, 2023
577d415
Fix nonlocal tests (use right threading) for deforestation optimizati…
dzoep Oct 20, 2023
a12340d
Revert "Fix nonlocal tests (use right threading) for deforestation op…
dzoep Nov 5, 2023
f935c44
Fix the tests again.
dzoep Nov 5, 2023
c325624
Remove redundant inlining hint.
dzoep Nov 5, 2023
8f036e1
Preliminary fusion for no-argument range, supporting 1 or 2 values.
dzoep Nov 5, 2023
47c23cd
Support for multiple arguments to curry in producer syntax class.
dzoep Nov 5, 2023
f1925d7
Full support for range fusion.
dzoep Nov 5, 2023
3eb9e74
Finish stream fusion for producers, transformers, consumers and all t…
dzoep Nov 6, 2023
6e3b681
Merge pull request #112 from dzoep/first-optimizations
countvajhula Nov 10, 2023
9488686
Move the current deforestation (both syntax and implementation parts)…
dzoep Nov 11, 2023
bb59629
Fix missing requires (for ~literal matching), streamline procedures n…
dzoep Nov 11, 2023
6531cb5
Cleanup unused pattern variable, implement car deforestation.
dzoep Nov 11, 2023
9f0f940
Re-add begin-encourage-inline based on the benchmarks.
dzoep Nov 17, 2023
e5feb6f
Merge pull request #114 from dzoep/first-optimizations
countvajhula Nov 17, 2023
3378f0e
add `range-map` benchmark
countvajhula Nov 17, 2023
25ef195
fix compiler tests
countvajhula Nov 17, 2023
fc674ec
add a (failing) test for deforestation in nested positions
countvajhula Nov 17, 2023
e73be41
remove testing-related nesting in qi deforestation benchmark
countvajhula Nov 17, 2023
bb503de
validate that `range` deforestation doesn't harm performance
countvajhula Nov 17, 2023
4a7cd20
fix (most) compiler tests again
countvajhula Nov 17, 2023
5fc615b
remove outdated compiler rewrite rule
countvajhula Nov 18, 2023
693ebab
normalization rule to collapse `values` inside a threading form
countvajhula Nov 18, 2023
317173f
Add initial tests for the normalization pass
countvajhula Nov 18, 2023
429f93b
improve `range-map-sum` benchmark
countvajhula Nov 20, 2023
8fd4e2f
add a "long functional pipeline" benchmark
countvajhula Nov 20, 2023
e6db41c
remove unused code
countvajhula Nov 20, 2023
e3231ff
Replace `range-map` benchmark with `range-map-car`
countvajhula Nov 20, 2023
7b6f04e
failing unit test for range-map-car
countvajhula Nov 20, 2023
8b1c15e
Partial implementation of producer prepare contract.
dzoep Nov 20, 2023
2b347fa
Refactor normalize pass into its own module
countvajhula Nov 20, 2023
51faf39
note a todo for normalization, and remove an outdated comment
countvajhula Nov 20, 2023
708b29d
Fix car stream consumer, generate all possible contracts for range va…
dzoep Nov 21, 2023
44bf495
Merge pull request #115 from countvajhula/benchmarks-and-tests
countvajhula Nov 21, 2023
7e6370d
Merge branch 'drym-org:first-optimizations' into first-optimizations
dzoep Nov 22, 2023
cd0f855
Deforestation producers - currying prepare in the right order, contra…
dzoep Nov 22, 2023
7b24304
Preliminary implementation of consumer contracts with car as an example.
dzoep Nov 22, 2023
20bd38e
Unified range producer syntax class + currying contracted pipeline.
dzoep Nov 23, 2023
ac8ba9e
fusable-stream-producer: limit the number of arguments to range to 1 …
dzoep Nov 24, 2023
f938cb4
Merge pull request #117 from dzoep/first-optimizations
countvajhula Nov 24, 2023
fce8d11
move a simplification from the code generation step to normalization
countvajhula Nov 22, 2023
5988648
Simplify deforestation tests to high level assertions
countvajhula Nov 22, 2023
7a3ddbb
Handle a simplified host expression in the compiler
countvajhula Nov 26, 2023
8c64170
failing compiler tests for deforesting templates
countvajhula Nov 29, 2023
15b339d
Rewrite partial application to a use of a blanket template
countvajhula Nov 29, 2023
f07620f
A test to validate pre-supplying keyword arguments
countvajhula Nov 29, 2023
6efd559
remove unused import
countvajhula Nov 29, 2023
801f510
match blanket templates in transformers and consumers
countvajhula Nov 29, 2023
5609e15
Merge pull request #121 from countvajhula/reduce-chirality-to-templates
countvajhula Nov 29, 2023
b568e01
add a failing test to show an issue with bindings and currying
countvajhula Nov 29, 2023
47e3374
Add support for #%fine-template in deforested consumers.
dzoep Nov 25, 2023
a705fef
Add missing literal.
dzoep Nov 25, 2023
376372c
Matching _ as ~datum.
dzoep Nov 25, 2023
21050c1
deforestation range producer: fix currying with 3 arguments
dzoep Nov 26, 2023
914d5f6
deforestation error reporting: implement partial de-expander for flows
dzoep Nov 26, 2023
8898934
Currying for #%fine-template deforested producers.
dzoep Nov 27, 2023
d911906
Work on simplifying deforestation pass.
dzoep Nov 29, 2023
d359987
deforestation: simplify consumer syntax patterns
dzoep Nov 29, 2023
5043b2b
add a few compiler tests for deforestation
countvajhula Nov 29, 2023
7b57a6f
deforestation: fix blanket template with all arguments, expand transf…
dzoep Nov 29, 2023
861f6a8
reorganize compiler tests along producer, transformer, etc.
countvajhula Nov 29, 2023
5938228
combinatorial deforestation tests for producers using templates
countvajhula Nov 29, 2023
389bed9
Simplify compiler tests
countvajhula Nov 30, 2023
2f8a127
fix invalid test
countvajhula Nov 30, 2023
eed1581
Fix "anaphoric references" issue (resolves failing test)
countvajhula Nov 30, 2023
870d8c1
tests for keyword arguments in templates
countvajhula Nov 30, 2023
2d5a602
Merge pull request #122 from countvajhula/more-compiler-tests
dzoep Nov 30, 2023
23338b9
deforestation: fix blanket template currying
dzoep Nov 30, 2023
f4a5d41
deforestation: report too many arguments for blanket templates in syn…
dzoep Nov 30, 2023
b1fa395
deforestation: preliminary support for argument count limits in synta…
dzoep Nov 30, 2023
8e5024e
deforestation: update prettify-flow-syntax de-expander to reflect lat…
dzoep Nov 30, 2023
1119032
deforestation: unify range producer syntax patterns into one
dzoep Nov 30, 2023
7263863
deforestation: do not duplicate producer name for contracts and error…
dzoep Nov 30, 2023
9c2de48
deforestation: unified producer curry maker
dzoep Nov 30, 2023
e64a12b
deforestation: fix new producer curry semantics for implicit list->cs…
dzoep Nov 30, 2023
1f30957
deforestation: more patterns unification
dzoep Nov 30, 2023
df753ec
deforestation: improve invalid argument count error messages for stat…
dzoep Nov 30, 2023
7937725
Merge branch 'drym-org:first-optimizations' into first-optimizations
dzoep Nov 30, 2023
ba1a4ee
deforestation: fix error message
dzoep Nov 30, 2023
8bd4e21
deforestation: full blame information at runtime
dzoep Nov 30, 2023
a126d41
deforestation: add range producer semantic tests
dzoep Nov 30, 2023
89b2ae7
deforestation: fix support for #%fine-template in fusable-stream-tran…
dzoep Nov 30, 2023
679f44a
Merge pull request #123 from dzoep/first-optimizations
countvajhula Nov 30, 2023
26f4657
include the new producer tests in the deforestation test suite
countvajhula Nov 30, 2023
ecd896a
add a couple more tests for kwargs in a blanket template
countvajhula Nov 30, 2023
9f3e9dd
reorganize compiler tests into a dedicated folder (collection)
countvajhula Nov 30, 2023
0c683d5
avoid division by zero in comparing benchmark results
countvajhula Nov 30, 2023
77da7c0
Make higher benchmark threshold the same ratio as lower one
countvajhula Nov 30, 2023
d2fec40
Normalization tests and improvements
countvajhula Dec 1, 2023
3c6225a
remove old comment which I think has been addressed
countvajhula Dec 1, 2023
53b428e
Merge pull request #124 from countvajhula/reorganize-compiler-tests
countvajhula Dec 1, 2023
6959f2f
add a basic threading test
countvajhula Dec 6, 2023
e313717
remove outdated todo (again?)
countvajhula Dec 6, 2023
f9cbff1
fix "not currently expanding" issue
countvajhula Dec 6, 2023
989f294
move general and debugging-related functions into their own modules
countvajhula Dec 6, 2023
efd1095
simplify definitions and imports across phases
countvajhula Dec 6, 2023
806f267
fix literal `range` in tests
countvajhula Dec 6, 2023
4a2b8f6
declare macro-debugger dependency
countvajhula Dec 7, 2023
37eabcb
Merge pull request #127 from countvajhula/phase-shifting
countvajhula Dec 8, 2023
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
19 changes: 11 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -104,25 +104,28 @@ test:
raco test -exp $(PACKAGE-NAME)-{lib,test,doc,probe}

test-flow:
racket $(PACKAGE-NAME)-test/tests/flow.rkt
racket -y $(PACKAGE-NAME)-test/tests/flow.rkt

test-on:
racket $(PACKAGE-NAME)-test/tests/on.rkt
racket -y $(PACKAGE-NAME)-test/tests/on.rkt

test-threading:
racket $(PACKAGE-NAME)-test/tests/threading.rkt
racket -y $(PACKAGE-NAME)-test/tests/threading.rkt

test-switch:
racket $(PACKAGE-NAME)-test/tests/switch.rkt
racket -y $(PACKAGE-NAME)-test/tests/switch.rkt

test-definitions:
racket $(PACKAGE-NAME)-test/tests/definitions.rkt
racket -y $(PACKAGE-NAME)-test/tests/definitions.rkt

test-macro:
racket $(PACKAGE-NAME)-test/tests/macro.rkt
racket -y $(PACKAGE-NAME)-test/tests/macro.rkt

test-util:
racket $(PACKAGE-NAME)-test/tests/util.rkt
racket -y $(PACKAGE-NAME)-test/tests/util.rkt

test-compiler:
racket -y $(PACKAGE-NAME)-test/tests/compiler.rkt

test-probe:
raco test -exp $(PACKAGE-NAME)-probe
Expand Down Expand Up @@ -193,4 +196,4 @@ performance-report:
performance-regression-report:
@racket $(PACKAGE-NAME)-sdk/profile/report.rkt -r $(REF)

.PHONY: help install remove build build-docs build-all clean check-deps test test-flow test-on test-threading test-switch test-definitions test-macro test-util test-probe test-with-errortrace errortrace errortrace-flow errortrace-on errortrace-threading errortrace-switch errortrace-definitions errortrace-macro errortrace-util errortrace-probe docs cover coverage-check coverage-report cover-coveralls profile-local profile-loading profile-selected-forms profile-competitive profile-nonlocal profile performance-report performance-regression-report
.PHONY: help install remove build build-docs build-all clean check-deps test test-flow test-on test-threading test-switch test-definitions test-macro test-util test-compiler test-probe test-with-errortrace errortrace errortrace-flow errortrace-on errortrace-threading errortrace-switch errortrace-definitions errortrace-macro errortrace-util errortrace-probe docs cover coverage-check coverage-report cover-coveralls profile-local profile-loading profile-selected-forms profile-competitive profile-nonlocal profile performance-report performance-regression-report
2 changes: 1 addition & 1 deletion qi-lib/flow.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
(all-from-out "flow/extended/expander.rkt")
(all-from-out "flow/extended/forms.rkt"))

(require syntax-spec
(require syntax-spec-v1
(for-syntax racket/base
syntax/parse
(only-in "private/util.rkt"
Expand Down
1 change: 0 additions & 1 deletion qi-lib/flow/aux-syntax.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

(define-syntax-class literal
(pattern
;; TODO: would be ideal to also match literal vectors, boxes and prefabs
(~or* expr:boolean
expr:char
expr:string
Expand Down
109 changes: 59 additions & 50 deletions qi-lib/flow/core/compiler.rkt
Original file line number Diff line number Diff line change
@@ -1,27 +1,52 @@
#lang racket/base

(provide (for-syntax compile-flow))
(provide (for-syntax compile-flow
normalize-pass))

(require (for-syntax racket/base
syntax/parse
racket/match
(only-in racket/list make-list)
"syntax.rkt"
"../aux-syntax.rkt")
"../aux-syntax.rkt"
"util.rkt"
"debug.rkt"
"normalize.rkt"
"deforest.rkt")
"impl.rkt"
(only-in racket/list make-list)
racket/function
racket/undefined
(prefix-in fancy: fancy-app))
(prefix-in fancy: fancy-app)
racket/list)

(begin-for-syntax

;; note: this does not return compiled code but instead,
;; syntax whose expansion compiles the code
(define (compile-flow stx)
(process-bindings (optimize-flow stx)))

(define (deforest-pass stx)
(find-and-map/qi (fix deforest-rewrite)
stx))

(define-qi-expansion-step (~deforest-pass stx)
;; Note: deforestation happens only for threading,
;; and the normalize pass strips the threading form
;; if it contains only one expression, so this would not be hit.
(deforest-pass stx))

(define (normalize-pass stx)
(find-and-map/qi (fix normalize-rewrite)
stx))

(define-qi-expansion-step (~normalize-pass stx)
(normalize-pass stx))

(define (optimize-flow stx)
stx))
(~deforest-pass
(~normalize-pass stx))))

;; Transformation rules for the `as` binding form:
;;
Expand Down Expand Up @@ -53,26 +78,6 @@

(begin-for-syntax

(define (find-and-map f stx)
;; f : syntax? -> (or/c syntax? #f)
(match stx
[(? syntax?) (let ([stx^ (f stx)])
(or stx^ (datum->syntax stx
(find-and-map f (syntax-e stx))
stx
stx)))]
[(cons a d) (cons (find-and-map f a)
(find-and-map f d))]
[_ stx]))

(define (find-and-map/qi f stx)
;; #%host-expression is a Racket macro defined by syntax-spec
;; that resumes expansion of its sub-expression with an
;; expander environment containing the original surface bindings
(find-and-map (syntax-parser [((~datum #%host-expression) e:expr) this-syntax]
[_ (f this-syntax)])
stx))

;; (as name) → (~> (esc (λ (x) (set! name x))) ⏚)
;; TODO: use a box instead of set!
(define (rewrite-all-bindings stx)
Expand All @@ -98,7 +103,7 @@
(with-syntax ([(v ...) ids])
#`(let ([v undefined] ...) #,stx)))

(define (process-bindings stx)
(define-qi-expansion-step (process-bindings stx)
;; TODO: use syntax-parse and match ~> specifically.
;; Since macros are expanded "outside in," presumably
;; it will naturally wrap the outermost ~>
Expand Down Expand Up @@ -206,23 +211,15 @@
[((~datum #%fine-template) (prarg-pre ... (~datum _) prarg-post ...))
#'(fancy:#%app prarg-pre ... _ prarg-post ...)]

;; Pre-supplied arguments without a template
[((~datum #%partial-application) (natex prarg ...+))
;; we use currying instead of templates when a template hasn't
;; explicitly been indicated since in such cases, we cannot
;; always infer the appropriate arity for a template (e.g. it
;; may change under composition within the form), while a
;; curried function will accept any number of arguments
#:do [(define chirality (syntax-property this-syntax 'chirality))]
(if (and chirality (eq? chirality 'right))
#'(lambda args
(apply natex prarg ... args))
;; TODO: keyword arguments don't work for the left-chiral case
;; since we can't just blanket place the pre-supplied args
;; and need to handle the keyword arguments differently
;; from the positional arguments.
#'(lambda args
((kw-helper natex args) prarg ...)))]))
;; if in the course of optimization we ever end up with a fully
;; simplified host expression, the compiler would a priori reject it as
;; not being a core Qi expression. So we add this extra rule here
;; to simply pass this expression through.
;; TODO: should `#%host-expression` be formally declared as being part
;; of the core language by including it in the syntax-spec grammar
;; in extended/expander.rkt?
[((~datum #%host-expression) hex)
this-syntax]))

;; The form-specific parsers, which are delegated to from
;; the qi0->racket macro:
Expand Down Expand Up @@ -493,16 +490,28 @@ the DSL.
(define (blanket-template-form-parser stx)
(syntax-parse stx
;; "prarg" = "pre-supplied argument"
;; Note: use of currying here doesn't play well with bindings
;; because curry / curryr immediately evaluate their arguments
;; and resolve any references to bindings at compile time.
;; That's why we use a lambda which delays evaluation until runtime
;; when the reference is actually resolvable. See "anaphoric references"
;; in the compiler meeting notes,
;; "The Artist Formerly Known as Bindingspec"
[((~datum #%blanket-template)
(natex prarg-pre ...+ (~datum __) prarg-post ...+))
#'(curry (curryr natex
prarg-post ...)
prarg-pre ...)]
;; "(curry (curryr ...) ...)"
#'(lambda largs
(apply
(lambda rargs
((kw-helper natex rargs) prarg-post ...))
prarg-pre ...
largs))]
[((~datum #%blanket-template) (natex prarg-pre ...+ (~datum __)))
#'(curry natex prarg-pre ...)]
;; "curry"
#'(lambda args
(apply natex prarg-pre ... args))]
[((~datum #%blanket-template)
(natex (~datum __) prarg-post ...+))
#'(curryr natex prarg-post ...)]
;; TODO: this should be a compiler optimization
[((~datum #%blanket-template) (natex (~datum __)))
#'natex])))
;; "curryr"
#'(lambda args
((kw-helper natex args) prarg-post ...))])))
22 changes: 22 additions & 0 deletions qi-lib/flow/core/debug.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#lang racket/base

(provide qi-expansion-step
define-qi-expansion-step)

(require macro-debugger/emit)

;; These macros emit expansion "events" that allow the macro
;; stepper to report stages in the expansion of an expression,
;; giving us visibility into this process for debugging purposes.
;; Note that this currently does not distinguish substeps
;; of a parent expansion step.
(define-syntax-rule (qi-expansion-step name stx0 stx1)
(let ()
(emit-local-step stx0 stx1 #:id #'name)
stx1))

(define-syntax-rule (define-qi-expansion-step (name stx0)
body ...)
(define (name stx0)
(let ([stx1 (let () body ...)])
(qi-expansion-step name stx0 stx1))))
Loading
Loading