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

Higher order codegen #181

Merged
merged 235 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from 227 commits
Commits
Show all changes
235 commits
Select commit Hold shift + click to select a range
60070f4
add lit test
christopherhjung Sep 28, 2022
d9199a9
save
christopherhjung Sep 29, 2022
7d07813
start of eta problem solution
NeuralCoder3 Oct 5, 2022
0338d45
ideas for type transformation
NeuralCoder3 Oct 5, 2022
970c2c9
inner autodiff type fix
NeuralCoder3 Oct 6, 2022
4869d56
fixed eta problem
NeuralCoder3 Oct 6, 2022
58b9519
one step towards fixing the infinite loop
NeuralCoder3 Oct 6, 2022
d1568c4
investigation of endless loop
NeuralCoder3 Oct 6, 2022
a330129
start of real tests
NeuralCoder3 Oct 7, 2022
d4ebad4
64 bit width is working
NeuralCoder3 Oct 7, 2022
e903622
save
christopherhjung Oct 9, 2022
75b96b2
Merge remote-tracking branch 'origin/autodiff' into autodiff
christopherhjung Oct 9, 2022
91c15e7
save
christopherhjung Oct 10, 2022
4fd9d09
real ad test
NeuralCoder3 Oct 10, 2022
67192e3
reordered dialects
NeuralCoder3 Oct 10, 2022
f67665b
made filter explicit
NeuralCoder3 Oct 10, 2022
087340f
enzyme test overview
NeuralCoder3 Oct 11, 2022
ed023e7
extracted relevant code
NeuralCoder3 Oct 11, 2022
70850ad
simple brussel test
NeuralCoder3 Oct 12, 2022
ccaf515
initialization
NeuralCoder3 Oct 12, 2022
bf0a218
time meassurement
NeuralCoder3 Oct 12, 2022
372973f
added brusselator functions
NeuralCoder3 Oct 12, 2022
f6ae248
fixed dominance
NeuralCoder3 Oct 12, 2022
5c528c8
more debugging on problem
NeuralCoder3 Oct 12, 2022
abe63c5
more attempts
NeuralCoder3 Oct 12, 2022
4231c0b
tried filters
NeuralCoder3 Oct 13, 2022
affc4e8
Merge branch 'master' into autodiff
NeuralCoder3 Oct 13, 2022
2903c96
localized problem even more
NeuralCoder3 Oct 13, 2022
5e0071b
working state without lamspec
NeuralCoder3 Oct 13, 2022
4849db5
earliest failure point
NeuralCoder3 Oct 13, 2022
c4326c6
bug in thorin
NeuralCoder3 Oct 13, 2022
cdd9f65
stuck at error in line 170
NeuralCoder3 Oct 13, 2022
1deea17
Merge branch 'eta-problem' into autodiff
NeuralCoder3 Oct 13, 2022
55fd344
Merge branch 'ad_real' into autodiff
NeuralCoder3 Oct 13, 2022
661e329
fixed merge/vscode issue
NeuralCoder3 Oct 13, 2022
670270a
new tests with pow deriv
NeuralCoder3 Oct 13, 2022
9a73cc7
refactor
NeuralCoder3 Oct 13, 2022
571a012
adapted tests to filter printing
NeuralCoder3 Oct 14, 2022
bdd9876
disabled LamSpec
NeuralCoder3 Oct 14, 2022
503565b
Merge branch 'master' into autodiff
NeuralCoder3 Oct 14, 2022
cf1c313
moved issues to closure
NeuralCoder3 Oct 14, 2022
84af1a0
removed debug print
NeuralCoder3 Oct 14, 2022
2baff33
Comparison to other functional languages
NeuralCoder3 Oct 17, 2022
53fd9f2
implement higher order scalerize
christopherhjung Oct 17, 2022
c0fd971
grouped evaluation, added thorin timing
NeuralCoder3 Oct 18, 2022
819bf8e
added timing
NeuralCoder3 Oct 18, 2022
392607d
alloca timing
NeuralCoder3 Oct 18, 2022
c78306b
commented out alloca run
NeuralCoder3 Oct 18, 2022
7e2d588
complete alloca
NeuralCoder3 Oct 18, 2022
a99aa96
implement autodiff arr and ptr support
christopherhjung Oct 20, 2022
a35f56c
refactoring
christopherhjung Oct 20, 2022
8566c36
more refactoring
christopherhjung Oct 20, 2022
a0d328c
haskell fully cps
NeuralCoder3 Oct 21, 2022
56274a6
polymorphic
NeuralCoder3 Oct 21, 2022
eaf5c6e
large program tests
NeuralCoder3 Oct 21, 2022
b1772d4
map issue
NeuralCoder3 Oct 21, 2022
e760116
avoid OS dependent ignores
NeuralCoder3 Oct 21, 2022
2054750
removed editor dependent files
NeuralCoder3 Oct 21, 2022
536f8e7
removed temporary files
NeuralCoder3 Oct 21, 2022
ea68622
started to improve comment style
NeuralCoder3 Oct 21, 2022
639b8fd
Merge branch 'master' into autodiff
NeuralCoder3 Oct 21, 2022
e7c681e
fix missing builder
christopherhjung Oct 21, 2022
aff8b00
add DS_Store to gitignore
christopherhjung Oct 21, 2022
d4a4e34
remove DS_Store
christopherhjung Oct 21, 2022
c2b7874
repeat commit e760116
NeuralCoder3 Oct 22, 2022
1c0b82b
Merge branch 'autodiff' into autodiff_ptr (only technically, inspecti…
NeuralCoder3 Oct 24, 2022
ec371b7
Merge branch 'master' into autodiff
NeuralCoder3 Oct 25, 2022
be13f84
top_level flat arguments
NeuralCoder3 Oct 25, 2022
1f9c5f3
reorder memory
NeuralCoder3 Oct 25, 2022
e05a86e
fixed thorin printer errors
NeuralCoder3 Oct 25, 2022
010e724
fixed compilation preventing bugs
NeuralCoder3 Oct 25, 2022
be06774
update
NeuralCoder3 Oct 25, 2022
e1c1f28
Merge branch 'autodiff' into ad_ptr_merge
NeuralCoder3 Oct 25, 2022
e3d0f71
clang format
NeuralCoder3 Oct 25, 2022
15a153e
clang format
NeuralCoder3 Oct 25, 2022
eb6cc63
readded unicodes
NeuralCoder3 Oct 25, 2022
21b58f3
filter print
NeuralCoder3 Oct 25, 2022
4cff7dd
removed unused declaration
NeuralCoder3 Oct 25, 2022
88acd2b
removed superfluous code from tests
NeuralCoder3 Oct 25, 2022
ecdfe71
also for simple_mem
NeuralCoder3 Oct 25, 2022
0719560
corrections
NeuralCoder3 Oct 25, 2022
62c08ae
moved phases, passes
NeuralCoder3 Oct 25, 2022
e15e605
removed duplicate files
NeuralCoder3 Oct 25, 2022
2888aaa
synchronized optimization order
NeuralCoder3 Oct 25, 2022
aed00fc
removed builder
NeuralCoder3 Oct 25, 2022
e94ebf4
update
NeuralCoder3 Oct 25, 2022
d30e495
cleanup
NeuralCoder3 Oct 26, 2022
d50ebe6
removed unused imports
NeuralCoder3 Oct 26, 2022
32635c8
fixed parsing error
NeuralCoder3 Oct 26, 2022
b17aa52
Merge branch 'master' into autodiff
NeuralCoder3 Oct 26, 2022
72e9dd0
readable error message
NeuralCoder3 Oct 26, 2022
cba4f59
fixed real test
NeuralCoder3 Oct 26, 2022
e967893
fixed secondary closure problem with higher order functions (by elmin…
NeuralCoder3 Oct 26, 2022
35be7ad
added optional filter annotation to expected output
NeuralCoder3 Oct 26, 2022
2444b29
split pow test in thorin and llvm part
NeuralCoder3 Oct 26, 2022
725f7ad
split toplevel memory operations
NeuralCoder3 Oct 26, 2022
fc15f7d
rewrote argument preparation
NeuralCoder3 Oct 26, 2022
db758c9
more toplevel updates
NeuralCoder3 Oct 26, 2022
27654d3
thoughts about autodiff_zero
NeuralCoder3 Oct 26, 2022
92fe7a9
update
NeuralCoder3 Oct 26, 2022
b641029
refactor extract
NeuralCoder3 Oct 26, 2022
23e4f1a
alphabetical order
NeuralCoder3 Oct 26, 2022
1b8d949
refactor
NeuralCoder3 Oct 26, 2022
d97bb60
refactor
NeuralCoder3 Oct 26, 2022
4a5c15d
separated memory functions
NeuralCoder3 Oct 27, 2022
aeca3d0
refactor
NeuralCoder3 Oct 28, 2022
f77617c
matrix.transpose differentiation
NeuralCoder3 Oct 28, 2022
100b680
code metric measurements
NeuralCoder3 Oct 31, 2022
169c9ff
visualization
NeuralCoder3 Oct 31, 2022
fd313e5
added impala files
NeuralCoder3 Oct 31, 2022
4dc44ed
Merge branch 'master' into autodiff
NeuralCoder3 Nov 2, 2022
dc22856
Merge branch 'autodiff' of https://github.com/NeuralCoder3/thorin2 in…
NeuralCoder3 Nov 2, 2022
a3daddd
removed get pullback function
NeuralCoder3 Nov 3, 2022
f173c28
Merge branch 'autodiff' into ad_ptr_merge
NeuralCoder3 Nov 3, 2022
9d4c253
more variants to try get it to work
NeuralCoder3 Nov 3, 2022
d89f551
Merge branch 'master' into autodiff
NeuralCoder3 Nov 4, 2022
d9fa330
additional test file
NeuralCoder3 Nov 4, 2022
8551c4f
merged
NeuralCoder3 Nov 4, 2022
0858ecc
working simplified test
NeuralCoder3 Nov 4, 2022
4b9ae49
fixed version 1
NeuralCoder3 Nov 4, 2022
1d0a08f
correctly fixed program
NeuralCoder3 Nov 4, 2022
9f8dae1
fixed (most) tests
NeuralCoder3 Nov 4, 2022
b8cb071
fixed memory test (now working)
NeuralCoder3 Nov 4, 2022
20bbd1f
format
NeuralCoder3 Nov 4, 2022
a455c0b
more informative error message
NeuralCoder3 Nov 4, 2022
0b66fba
Merge branch 'autodiff' into ad_ptr_merge
NeuralCoder3 Nov 4, 2022
66ccd45
fixed some more tests
NeuralCoder3 Nov 4, 2022
7dadddf
partially reverted tuple, app for now
NeuralCoder3 Nov 7, 2022
23f7244
split memory test in two parts to debug code gen in the presence/abse…
NeuralCoder3 Nov 7, 2022
965a811
disabled reshape (fixed 5 additional test cases) for now
NeuralCoder3 Nov 7, 2022
dd569fc
fixed non-memory tests
NeuralCoder3 Nov 7, 2022
c215f17
fixed zero application for now
NeuralCoder3 Nov 8, 2022
5253c78
rebuild correct memory tuple
NeuralCoder3 Nov 8, 2022
1779f61
create pullback for rebuild tuples
NeuralCoder3 Nov 8, 2022
8d00f43
simpler imperative memory test
NeuralCoder3 Nov 8, 2022
4688fca
association of (mostly) correct pullbacks
NeuralCoder3 Nov 8, 2022
393f8f6
found cps2ds error
NeuralCoder3 Nov 8, 2022
9c24614
fixed parser errors
NeuralCoder3 Nov 8, 2022
f86922d
more tests on the cps2ds issue
NeuralCoder3 Nov 9, 2022
428908c
Merge branch 'master' into autodiff
NeuralCoder3 Nov 9, 2022
d7b63e9
Merge branch 'autodiff' of https://github.com/NeuralCoder3/thorin2 in…
NeuralCoder3 Nov 9, 2022
50532e0
Merge branch 'autodiff' into ad_ptr_merge
NeuralCoder3 Nov 10, 2022
939456b
adapted test case
NeuralCoder3 Nov 10, 2022
ea6d984
more debugging
NeuralCoder3 Nov 10, 2022
fdf001c
merge bug test case from ad_ptr_merge branch
NeuralCoder3 Nov 10, 2022
7ad6892
Merge branch 'master' into direct_old_var
NeuralCoder3 Nov 10, 2022
50b3aef
Merge remote-tracking branch 'origin/optimize_phase_extensions' into …
NeuralCoder3 Nov 10, 2022
11e54e3
skeleton for add_mem conversion
NeuralCoder3 Nov 10, 2022
d410eb0
plan
NeuralCoder3 Nov 10, 2022
702d848
add mem
NeuralCoder3 Nov 11, 2022
66e55fb
removed eval
NeuralCoder3 Nov 14, 2022
b41b437
Merge branch 'master' into ad_ptr_merge
NeuralCoder3 Nov 14, 2022
e75ee79
Merge branch 'ad_ptr_merge' into ho_codegen
NeuralCoder3 Nov 14, 2022
94d8918
removed ad
NeuralCoder3 Nov 14, 2022
be2005f
merge ad from master
NeuralCoder3 Nov 14, 2022
b8a0c95
fixed name preservation for tests, small improvements
NeuralCoder3 Nov 15, 2022
734823a
add mem related tests
NeuralCoder3 Nov 15, 2022
28a2095
fixed tests
NeuralCoder3 Nov 15, 2022
480f812
added commands to failing tests
NeuralCoder3 Nov 15, 2022
d8afb79
arg style test
NeuralCoder3 Nov 15, 2022
35c4924
made functions external
NeuralCoder3 Nov 15, 2022
3178a71
tests without extern
NeuralCoder3 Nov 16, 2022
3ca0edc
add reshape flat
NeuralCoder3 Nov 16, 2022
4c292b7
test with preprocessor
NeuralCoder3 Nov 16, 2022
6f08aca
fixed test case lea access
NeuralCoder3 Nov 17, 2022
be1d7e1
fixed oblivious lea reconstruction
NeuralCoder3 Nov 17, 2022
1c46504
fixed more test cases for reshape (divergence)
NeuralCoder3 Nov 17, 2022
226ec64
fixed llvm codegen for conditionals
NeuralCoder3 Nov 17, 2022
81da58b
investigated disabled tests for clos
NeuralCoder3 Nov 17, 2022
005dc1a
moved reshape call to clos
NeuralCoder3 Nov 17, 2022
420984e
more robust testing
NeuralCoder3 Nov 17, 2022
212a498
Merge remote-tracking branch 'upstream/feature/add-mem' into ho_codegen
NeuralCoder3 Nov 18, 2022
1517b90
moved add mem to closure
NeuralCoder3 Nov 18, 2022
d72d448
added complex no mem test case
NeuralCoder3 Nov 18, 2022
5a1603b
Merge remote-tracking branch 'upstream/feature/add-mem' into ho_codegen
NeuralCoder3 Nov 22, 2022
d4094d7
Merge remote-tracking branch 'upstream/feature/add-mem' into ho_codegen
NeuralCoder3 Nov 23, 2022
897388e
Merge remote-tracking branch 'upstream/feature/add-mem' into ho_codegen
NeuralCoder3 Nov 24, 2022
b0aead5
resolved circular dependency
NeuralCoder3 Nov 29, 2022
6fbb8f6
Merge remote-tracking branch 'origin/master' into ho_codegen
NeuralCoder3 Nov 29, 2022
8e540be
merge reshape from ad_ptr_merge
NeuralCoder3 Nov 29, 2022
fdc74c7
handle external functions
NeuralCoder3 Nov 29, 2022
a076278
fixed handling of main function
NeuralCoder3 Nov 29, 2022
2d11419
merge ad_ptr_merge compilation extension
NeuralCoder3 Nov 29, 2022
4e08350
Merge remote-tracking branch 'origin/master' into ho_codegen
NeuralCoder3 Nov 30, 2022
f6fa75a
Merge remote-tracking branch 'origin/master' into test/direct_old_var
NeuralCoder3 Dec 2, 2022
31c8751
fixed tests
NeuralCoder3 Dec 2, 2022
03f8a59
updated test
NeuralCoder3 Dec 2, 2022
794726d
more attempts to fix cps2ds
NeuralCoder3 Dec 5, 2022
87cd669
fixed lambda rewrite
NeuralCoder3 Dec 5, 2022
1792c0a
refactoring
NeuralCoder3 Dec 5, 2022
eeaa8ab
add expected result
NeuralCoder3 Dec 5, 2022
a5b9c7b
re-enable curr nom print
NeuralCoder3 Dec 5, 2022
f88f568
more complex case
NeuralCoder3 Dec 5, 2022
8b3fa6a
removed comment
NeuralCoder3 Dec 5, 2022
cca49e6
rewrite recursively
NeuralCoder3 Dec 5, 2022
eae0fab
rewrite callee first
NeuralCoder3 Dec 5, 2022
668cd96
Merge remote-tracking branch 'origin/default-compilation' into ho_cod…
NeuralCoder3 Dec 5, 2022
87b6356
Merge remote-tracking branch 'origin/direct_fix' into ho_codegen
NeuralCoder3 Dec 6, 2022
fb1ec79
Merge remote-tracking branch 'origin/default-compilation' into ho_cod…
NeuralCoder3 Dec 6, 2022
299da40
only reshape small arrays
NeuralCoder3 Dec 7, 2022
78a8a13
move mem to front (for closure)
NeuralCoder3 Dec 8, 2022
d6f4ae2
Merge branch 'ho_codegen' of https://github.com/NeuralCoder3/thorin2 …
NeuralCoder3 Dec 8, 2022
4733334
merge
NeuralCoder3 Dec 8, 2022
685d2f1
add mem tests
NeuralCoder3 Dec 9, 2022
fe95409
do not overwrite mem from non-apps
NeuralCoder3 Dec 9, 2022
0add729
some comments
NeuralCoder3 Dec 9, 2022
f528b4b
error reporting script
NeuralCoder3 Dec 9, 2022
1fe61ac
syntax highlight
NeuralCoder3 Dec 12, 2022
0e6f52c
correctly negated check
NeuralCoder3 Dec 13, 2022
a3c67d9
Merge branch 'ho_codegen' of https://github.com/NeuralCoder3/thorin2 …
NeuralCoder3 Dec 13, 2022
a5a7595
merge codegen fixes from matrix
NeuralCoder3 Dec 20, 2022
0a96f0f
Merge branch 'ho_codegen' of https://github.com/NeuralCoder3/thorin2 …
NeuralCoder3 Dec 20, 2022
255797e
generalized internal cleanup
NeuralCoder3 Dec 22, 2022
6c0024c
register phase
NeuralCoder3 Dec 23, 2022
8954d72
Merge remote-tracking branch 'origin/master' into ho_codegen
NeuralCoder3 Dec 23, 2022
50e46bc
move dependency axiom code to compile dialect
NeuralCoder3 Dec 23, 2022
29af823
polished test cases
NeuralCoder3 Feb 1, 2023
984f0f0
Merge remote-tracking branch 'origin/master' into ho_codegen
NeuralCoder3 Feb 1, 2023
46fee61
updated tests
NeuralCoder3 Feb 1, 2023
cb34c31
attempt at platform independent timing
NeuralCoder3 Feb 1, 2023
962aa01
Revert "attempt at platform independent timing"
NeuralCoder3 Feb 1, 2023
e9d4ace
disable timing for non-linux platform
NeuralCoder3 Feb 2, 2023
0639938
disabled silent mode
NeuralCoder3 Feb 2, 2023
997c1e6
Merge branch 'feature/strpool' into ho_codegen
leissa Mar 8, 2023
53926cb
fixed test case
leissa Mar 8, 2023
5ba5442
Merge remote-tracking branch 'origin/master' into ho_codegen
NeuralCoder3 Mar 10, 2023
1cdbc59
disable complex preprocessor test
NeuralCoder3 Mar 10, 2023
defa2d0
cleanup
NeuralCoder3 Mar 13, 2023
596d0b1
Merge branch 'anydsl_master' into ho_codegen
leissa Mar 13, 2023
34d60cb
removed sym performance hack
leissa Mar 13, 2023
95010b9
reverted nested extract change, broke six tests
NeuralCoder3 Mar 13, 2023
dcacca5
Merge branch 'ho_codegen' of https://github.com/NeuralCoder3/thorin2 …
NeuralCoder3 Mar 13, 2023
79ba273
refactored out the non-repeating appending of a suffix
NeuralCoder3 Mar 13, 2023
1bee16a
refactor
NeuralCoder3 Mar 13, 2023
2acef27
remove space in front of filter annotation
NeuralCoder3 Mar 13, 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
*.s
.DS_Store
.cache
build*
build*/
vgcore.*
7 changes: 6 additions & 1 deletion dialects/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,14 @@ add_thorin_dialect(clos
clos/pass/rw/clos2sjlj.h
clos/pass/rw/clos_conv_prep.cpp
clos/pass/rw/clos_conv_prep.h
clos/pass/rw/phase_wrapper.h
clos/phase/clos_conv.cpp
clos/phase/clos_conv.h
clos/phase/lower_typed_clos.cpp
clos/phase/lower_typed_clos.h
mem/passes/fp/copy_prop.cpp
mem/passes/rw/reshape.cpp
mem/phases/rw/add_mem.cpp
DEPENDS
mem
affine
Expand Down Expand Up @@ -137,6 +140,8 @@ add_thorin_dialect(mem
mem/passes/rw/alloc2malloc.h
mem/passes/rw/remem_elim.cpp
mem/passes/rw/remem_elim.h
mem/passes/rw/reshape.cpp
mem/passes/rw/reshape.h
mem/phases/rw/add_mem.cpp
mem/phases/rw/add_mem.h
DEPENDS
Expand All @@ -150,7 +155,6 @@ add_thorin_dialect(opt
SOURCES
opt/opt.cpp
opt/opt.h
opt/normalizers.cpp
DEPENDS
compile
mem
Expand All @@ -164,6 +168,7 @@ add_thorin_dialect(refly
refly/refly.cpp
refly/passes/remove_perm.h
refly/passes/remove_perm.cpp
refly/passes/debug_dump.h
refly/normalizers.cpp
DEPENDS
compile
Expand Down
10 changes: 5 additions & 5 deletions dialects/affine/passes/lower_for.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ const Def* LowerFor::rewrite(const Def* def) {

// reduce the body to remove the cn parameter
auto nom_body = body->as_nom<Lam>();
auto new_body = nom_body->stub(w, w.cn(w.sigma()))->set(body->dbg());
new_body->set(nom_body->reduce(w.tuple({iter, acc, yield_lam})));
auto new_body = nom_body->stub(w, w.cn(acc->type()))->set(body->dbg());
new_body->set(nom_body->reduce(w.tuple({iter, new_body->var(), yield_lam})));

// break
auto if_else_cn = w.cn(w.sigma());
auto if_else_cn = w.cn(acc->type());
auto if_else = w.nom_lam(if_else_cn);
if_else->app(false, brk, acc);
if_else->app(false, brk, if_else->var());

auto cmp = w.call(core::icmp::ul, Defs{iter, end});
for_lam->branch(false, cmp, new_body, if_else, w.tuple());
for_lam->branch(false, cmp, new_body, if_else, acc);
}

DefArray for_args{for_ax->num_args() - 2, [&](size_t i) { return for_ax->arg(i); }};
Expand Down
30 changes: 6 additions & 24 deletions dialects/clos/clos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
#include "dialects/clos/pass/rw/branch_clos_elim.h"
#include "dialects/clos/pass/rw/clos2sjlj.h"
#include "dialects/clos/pass/rw/clos_conv_prep.h"
#include "dialects/clos/phase/clos_conv.h"
#include "dialects/clos/phase/lower_typed_clos.h"
#include "dialects/clos/pass/rw/phase_wrapper.h"
#include "dialects/mem/mem.h"
#include "dialects/mem/passes/fp/copy_prop.h"
#include "dialects/mem/passes/rw/reshape.h"
#include "dialects/mem/phases/rw/add_mem.h"
#include "dialects/refly/passes/debug_dump.h"

namespace thorin::clos {

Expand Down Expand Up @@ -131,26 +133,6 @@ Ref ctype(World& w, Defs doms, Ref env_type) {
[&](auto i) { return clos_insert_env(i, env_type, [&](auto j) { return doms[j]; }); }));
}

/*
* Pass Wrappers
*/

class ClosConvWrapper : public RWPass<ClosConvWrapper, Lam> {
public:
ClosConvWrapper(PassMan& man)
: RWPass(man, "clos_conv") {}

void prepare() override { ClosConv(world()).run(); }
};

class LowerTypedClosWrapper : public RWPass<LowerTypedClosWrapper, Lam> {
public:
LowerTypedClosWrapper(PassMan& man)
: RWPass(man, "lower_typed_clos") {}

void prepare() override { LowerTypedClos(world()).run(); }
};

} // namespace thorin::clos

using namespace thorin;
Expand All @@ -159,11 +141,11 @@ extern "C" THORIN_EXPORT DialectInfo thorin_get_dialect_info() {
return {"clos",
[](Passes& passes) {
register_pass<clos::clos_conv_prep_pass, clos::ClosConvPrep>(passes, nullptr);
register_pass<clos::clos_conv_pass, clos::ClosConvWrapper>(passes);
register_pass<clos::clos_conv_pass, ClosConvWrapper>(passes);
register_pass<clos::branch_clos_pass, clos::BranchClosElim>(passes);
register_pass<clos::lower_typed_clos_prep_pass, clos::LowerTypedClosPrep>(passes);
register_pass<clos::clos2sjlj_pass, clos::Clos2SJLJ>(passes);
register_pass<clos::lower_typed_clos_pass, clos::LowerTypedClosWrapper>(passes);
register_pass<clos::lower_typed_clos_pass, LowerTypedClosWrapper>(passes);
// TODO:; remove after ho_codegen merge
passes[flags_t(Axiom::Base<clos::eta_red_bool_pass>)] = [&](World&, PipelineBuilder& builder, Ref app) {
auto bb = app->as<App>()->arg();
Expand Down
6 changes: 3 additions & 3 deletions dialects/clos/clos.thorin
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@
%compile.combined_phase
(%compile.phase_list
(%compile.single_pass_phase nullptr)
// optimization_phase
// (%compile.single_pass_phase (%mem.reshape_pass %mem.reshape_flat))
// (%compile.single_pass_phase %mem.add_mem_pass)
optimization_phase
(%compile.single_pass_phase (%mem.reshape_pass %mem.reshape_flat))
(%compile.single_pass_phase %mem.add_mem_pass)
(%compile.single_pass_phase %clos.clos_conv_prep_pass)
(%compile.single_pass_phase (%compile.eta_exp_pass nullptr))
(%compile.single_pass_phase %clos.clos_conv_pass)
Expand Down
9 changes: 4 additions & 5 deletions dialects/clos/pass/rw/clos_conv_prep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@ void ClosConvPrep::enter() {
}
}
}
if (auto body = curr_nom()->body()->isa<App>();
!wrapper_.contains(curr_nom()) && body && body->callee_type()->is_cn())
ignore_ = false;
else
ignore_ = true;

auto body = curr_nom()->body()->isa<App>();
// Skip if the nominal is already wrapped or the body is undefined/no continuation.
ignore_ = !(body && body->callee_type()->is_cn()) || wrapper_.contains(curr_nom());
}

const App* ClosConvPrep::rewrite_arg(const App* app) {
Expand Down
27 changes: 27 additions & 0 deletions dialects/clos/pass/rw/phase_wrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <thorin/config.h>
#include <thorin/pass/pass.h>

#include "thorin/dialects.h"

#include "dialects/clos/phase/clos_conv.h"
#include "dialects/clos/phase/lower_typed_clos.h"

using namespace thorin;

class ClosConvWrapper : public RWPass<ClosConvWrapper, Lam> {
public:
ClosConvWrapper(PassMan& man)
: RWPass(man, "clos_conv") {}

void prepare() override { clos::ClosConv(world()).run(); }
};

class LowerTypedClosWrapper : public RWPass<LowerTypedClosWrapper, Lam> {
public:
LowerTypedClosWrapper(PassMan& man)
: RWPass(man, "lower_typed_clos") {}

void prepare() override { clos::LowerTypedClos(world()).run(); }
};
40 changes: 40 additions & 0 deletions dialects/compile/compile.thorin
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,34 @@
.Pi PassList: *, %compile.Pass = PassList;
.Pi CombinedPhase: *, %compile.Phase = CombinedPhase;
///
/// (This is a forward declaration for opt.thorin.)
///
.ax %compile.Dialect: *;
///
/// ## Expressions
///
/// ### registered dialects
///
/// We expect the name in the tag before the `_` to be the name of the dialect (as given in `DialectInfo.plugin_name`).
/// (This is a forward declaration for opt.thorin.)
///
.ax %compile.core_dialect : %compile.Dialect;
.ax %compile.mem_dialect : %compile.Dialect;
.ax %compile.demo_dialect : %compile.Dialect;
.ax %compile.affine_dialect : %compile.Dialect;
.ax %compile.autodiff_dialect: %compile.Dialect;
.ax %compile.clos_dialect : %compile.Dialect;
.ax %compile.direct_dialect : %compile.Dialect;
.ax %compile.refly_dialect : %compile.Dialect;
///
/// ### %opt.is_loaded
///
/// Indicates whether a dialect is loaded.
/// The normalizer will statically evaluate this expression to a constant boolean.
/// TODO: find correct point (not at parsing but before compilation)
///
// .ax %opt.is_loaded: %opt.Dialect -> .Bool;
///
/// ### %compile.pipe
///
/// Given n phases, returns the representation of a pipeline.
Expand Down Expand Up @@ -119,4 +145,18 @@
.lam .extern _fallback_compile [] -> Pipeline = {
default_core_pipeline
};
///
/// ### Dependent Passes and Phases
///
.let empty_pass = %compile.nullptr_pass;
.let empty_phase = %compile.passes_to_phase 0 ();
.ax %compile.dialect_select: Π [T:*] -> %compile.Dialect -> T -> T -> T;
.let dialect_phase = %compile.dialect_select %compile.Phase;
.let dialect_pass = %compile.dialect_select %compile.Pass;
.lam dialect_cond_phase ![dialect: %compile.Dialect,phase: %compile.Phase] -> %compile.Phase = {
dialect_phase dialect phase empty_phase
};
.lam dialect_cond_pass ![dialect: %compile.Dialect,pass: %compile.Pass] -> %compile.Pass = {
dialect_pass dialect pass empty_pass
};

2 changes: 1 addition & 1 deletion dialects/compile/passes/internal_cleanup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace thorin::compile {

void InternalCleanup::enter() {
Lam* lam = curr_nom();
if (lam->sym()->starts_with("internal_")) {
if (lam->sym()->starts_with(prefix_)) {
lam->make_internal();
world().DLOG("internalized {}", lam);
}
Expand Down
8 changes: 6 additions & 2 deletions dialects/compile/passes/internal_cleanup.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ namespace thorin::compile {

class InternalCleanup : public RWPass<InternalCleanup, Lam> {
public:
InternalCleanup(PassMan& man)
: RWPass(man, "internal_cleanup") {}
InternalCleanup(PassMan& man, const char* prefix = "internal_")
: RWPass(man, "internal_cleanup")
, prefix_(prefix) {}

void enter() override;

private:
const char* prefix_;
};

} // namespace thorin::compile
90 changes: 68 additions & 22 deletions dialects/core/be/ll/ll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,9 @@ std::string Emitter::convert(const Def* type) {
}

std::string Emitter::convert_ret_pi(const Pi* pi) {
switch (pi->num_doms()) {
case 0: return "void";
case 1:
if (match<mem::M>(pi->dom())) return "void";
return convert(pi->dom());
case 2:
if (match<mem::M>(pi->dom(0))) return convert(pi->dom(1));
if (match<mem::M>(pi->dom(1))) return convert(pi->dom(0));
[[fallthrough]];
default: return convert(pi->dom());
}
auto dom = mem::strip_mem_ty(pi->dom());
if (dom == world().sigma()) { return "void"; }
return convert(dom);
}

/*
Expand Down Expand Up @@ -313,7 +305,27 @@ void Emitter::emit_epilogue(Lam* lam) {
}
}
} else if (auto ex = app->callee()->isa<Extract>(); ex && app->callee_type()->is_basicblock()) {
emit_unsafe(app->arg());
// A call to an extract like constructed for conditionals (else,then)#cond (args)
// TODO: we can not rely on the structure of the extract (it might be a nested extract)
NeuralCoder3 marked this conversation as resolved.
Show resolved Hide resolved
for (auto callee_def : ex->tuple()->projs()) {
// dissect the tuple of lambdas
auto callee = callee_def->isa_nom<Lam>();
assert(callee);
// each callees type should agree with the argument type (should be checked by type checking).
// Especially, the number of vars should be the number of arguments.
// TODO: does not hold for complex arguments that are not tuples.
assert(callee->num_vars() == app->num_args());
for (size_t i = 0, e = callee->num_vars(); i != e; ++i) {
// emits the arguments one by one (TODO: handle together like before)
if (auto arg = emit_unsafe(app->arg(i)); !arg.empty()) {
auto phi = callee->var(i);
assert(!match<mem::M>(phi->type()));
lam2bb_[callee].phis[phi].emplace_back(arg, id(lam, true));
locals_[phi] = id(phi);
}
}
}

auto c = emit(ex->index());
if (ex->tuple()->num_projs() == 2) {
auto [f, t] = ex->tuple()->projs<2>([this](auto def) { return emit(def); });
Expand Down Expand Up @@ -448,12 +460,14 @@ std::string Emitter::emit_bb(BB& bb, const Def* def) {

std::string prev = "undef";
auto t = convert(tuple->type());
for (size_t i = 0, n = tuple->num_projs(); i != n; ++i) {
auto e = tuple->proj(n, i);
if (auto v_elem = emit_unsafe(e); !v_elem.empty()) {
auto t_elem = convert(e->type());
auto namei = name + "." + std::to_string(i);
prev = bb.assign(namei, "insertvalue {} {}, {} {}, {}", t, prev, t_elem, v_elem, i);
for (size_t src = 0, dst = 0, n = tuple->num_projs(); src != n; ++src) {
auto e = tuple->proj(n, src);
if (auto elem = emit_unsafe(e); !elem.empty()) {
auto elem_t = convert(e->type());
// TODO: check dst vs src
auto namei = name + "." + std::to_string(dst);
prev = bb.assign(namei, "insertvalue {} {}, {} {}, {}", t, prev, elem_t, elem, dst);
dst++;
}
}
return prev;
Expand Down Expand Up @@ -736,10 +750,19 @@ std::string Emitter::emit_bb(BB& bb, const Def* def) {
declare("i8* @malloc(i64)");

emit_unsafe(malloc->arg(0));
auto v_size = emit(malloc->arg(1));
auto t_ptr = convert(force<mem::Ptr>(def->proj(1)->type()));
bb.assign(name + ".i8", "call i8* @malloc(i64 {})", v_size);
return bb.assign(name, "bitcast i8* {} to {}", name + ".i8", t_ptr);
auto size = emit(malloc->arg(1));
auto ptr_t = convert(force<mem::Ptr>(def->proj(1)->type()));
bb.assign(name + ".i8", "call i8* @malloc(i64 {})", size);
return bb.assign(name, "bitcast i8* {} to {}", name + ".i8", ptr_t);
} else if (auto free = match<mem::free>(def)) {
declare("void @free(i8*)");
emit_unsafe(free->arg(0));
auto ptr = emit(free->arg(1));
auto ptr_t = convert(force<mem::Ptr>(free->arg(1)->type()));

bb.assign(name + ".i8", "bitcast {} {} to i8*", ptr_t, ptr);
bb.tail("call void @free(i8* {})", name + ".i8");
return {};
} else if (auto mslot = match<mem::mslot>(def)) {
emit_unsafe(mslot->arg(0));
// TODO array with size
Expand Down Expand Up @@ -790,6 +813,26 @@ std::string Emitter::emit_bb(BB& bb, const Def* def) {
auto t = convert(arith->type());
auto mode = as_lit(arith->decurry()->arg());

// # if 0
NeuralCoder3 marked this conversation as resolved.
Show resolved Hide resolved
// // TODO this was von closure-conv branch which I need to double-check
// if (tuple->isa<Var>()) {
// // computing the index may crash, so we bail out
// assert(match<mem::M>(extract->type()) && "only mem-var should not be mapped");
// return {};
// }
// # endif

// auto ll_tup = emit_unsafe(tuple);

// // this exact location is important: after emitting the tuple -> ordering of mem ops
// // before emitting the index, as it might be a weird value for mem vars.
// if (match<mem::M>(extract->type())) return {};

// auto ll_idx = emit_unsafe(index);

// if (tuple->num_projs() == 2) {
// if (match<mem::M>(tuple->proj(2, 0_s)->type())) return ll_tup;
// if (match<mem::M>(tuple->proj(2, 1_s)->type())) return ll_tup;
switch (arith.id()) {
case math::arith::add: op = "fadd"; break;
case math::arith::sub: op = "fsub"; break;
Expand Down Expand Up @@ -944,6 +987,9 @@ std::string Emitter::emit_bb(BB& bb, const Def* def) {

return bb.assign(name, "{} {} {} to {}", op, t_src, v_src, t_dst);
}
auto& world = def->world();
world.DLOG("unhandled def: {} : {}", def, def->type());
def->dump();

def->dump(1);
err("unhandled def in LLVM backend: {}", def);
Expand Down
Loading