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

Refactor/blockexpr #274

Merged
merged 5 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
31 changes: 11 additions & 20 deletions include/thorin/ast/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,16 @@ class Ptrn : public Decl {
bool rebind_;
};

class ErrorPtrn : public Ptrn {
public:
ErrorPtrn(Loc loc)
: Ptrn(loc, false, Dbg()) {}

void bind(Scopes&, bool quiet = false) const override;
Ref emit_type(Emitter&) const override;
std::ostream& stream(Tab&, std::ostream&) const override;
};

/// `dbg: type`
class IdPtrn : public Ptrn {
public:
Expand Down Expand Up @@ -238,7 +248,7 @@ class GrpPtrn : public Ptrn {
/// `dbg::(ptrn_0, ..., ptrn_n-1)` or `dbg::[ptrn_0, ..., ptrn_n-1]`
class TuplePtrn : public Ptrn {
public:
TuplePtrn(Loc loc, bool rebind, Dbg dbg, Tok::Tag delim_l, Ptrs<Ptrn>&& ptrns)
TuplePtrn(Loc loc, Tok::Tag delim_l, Ptrs<Ptrn>&& ptrns, bool rebind, Dbg dbg)
: Ptrn(loc, rebind, dbg)
, delim_l_(delim_l)
, ptrns_(std::move(ptrns)) {}
Expand Down Expand Up @@ -355,25 +365,6 @@ class LitExpr : public Expr {
Ptr<Expr> type_;
};

/// `{ e }`
/// @deprecated will be removed; use `( e )` instead.
class BlockExpr : public Expr {
public:
BlockExpr(Loc loc, Ptr<Expr>&& expr)
: Expr(loc)
, expr_(std::move(expr)) {}

const Expr* expr() const { return expr_.get(); }

void bind(Scopes&) const override;
std::ostream& stream(Tab&, std::ostream&) const override;

private:
Ref emit_(Emitter&) const override;

Ptr<Expr> expr_;
};

/// `decls e` or `e .where decls` if @p where is `true`.
class DeclExpr : public Expr {
public:
Expand Down
3 changes: 1 addition & 2 deletions include/thorin/ast/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ class Parser : public fe::Parser<Tok, Tok::Tag, Look_Ahead, Parser> {
/// @name parse primary exprs
///@{
template<bool> Ptr<Expr> parse_arr_or_pack_expr();
Ptr<Expr> parse_block_expr();
Ptr<Expr> parse_decl_expr();
Ptr<Expr> parse_lit_expr();
Ptr<Expr> parse_extremum_expr();
Expand All @@ -100,7 +99,7 @@ class Parser : public fe::Parser<Tok, Tok::Tag, Look_Ahead, Parser> {

/// Depending on @p tag, this parses a `()`-style (Tok::Tag::D_paren_l) or `[]`-style (Tok::Tag::D_brckt_l) Ptrn.
Ptr<Ptrn> parse_ptrn(Tok::Tag tag, std::string_view ctxt, Prec = Prec::Bot, bool allow_annex = false);
Ptr<TuplePtrn> parse_tuple_ptrn(bool rebind, Dbg);
Ptr<TuplePtrn> parse_tuple_ptrn();
///@}

/// @name parse decls
Expand Down
31 changes: 12 additions & 19 deletions lit/affine/dynamic_for.thorin
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,28 @@

.ccon atoi [%mem.M, %mem.Ptr («⊤:.Nat; .I8», 0), .Cn [%mem.M, .I32]];

.con .extern main [mem : %mem.M, argc : .I32, argv : %mem.Ptr («⊤:.Nat; %mem.Ptr («⊤:.Nat; .I8», 0)», 0), return : .Cn [%mem.M, .I32]] = {
.con for_body [i : .I32, [acc_a : .I32, acc_b : .I32], continue : .Cn [.I32, .I32]] = {
.con .extern main [mem : %mem.M, argc : .I32, argv : %mem.Ptr («⊤:.Nat; %mem.Ptr («⊤:.Nat; .I8», 0)», 0), return : .Cn [%mem.M, .I32]] =
.con for_body [i : .I32, [acc_a : .I32, acc_b : .I32], continue : .Cn [.I32, .I32]] =
.let a : .I32 = %core.wrap.add 0 (i, acc_a);
.let b : .I32 = %core.wrap.sub 0 (i, acc_b);
continue (a, b)
};
continue (a, b);

.con atoi_cont_begin [mem : %mem.M, start : .I32] = {
.con atoi_cont_begin [mem : %mem.M, start : .I32] =
.let _19234: %mem.Ptr (%mem.Ptr («⊤:.Nat; .I8», 0), 0) = %mem.lea (⊤:.Nat, ‹⊤:.Nat; %mem.Ptr («⊤:.Nat; .I8», 0)›, 0) (argv, 2I32);
.let _19247: [%mem.M, %mem.Ptr («⊤:.Nat; .I8», 0)] = %mem.load (mem, _19234);

.con atoi_cont_end [mem : %mem.M, stop : .I32] = {
.con atoi_cont_end [mem : %mem.M, stop : .I32] =
.let _19318: %mem.Ptr (%mem.Ptr («⊤:.Nat; .I8», 0), 0) = %mem.lea (⊤:.Nat, ‹⊤:.Nat; %mem.Ptr («⊤:.Nat; .I8», 0)›, 0) (argv, 3I32);
.let _19331: [%mem.M, %mem.Ptr («⊤:.Nat; .I8», 0)] = %mem.load (mem, _19318);
.con atoi_cont_step [mem : %mem.M, step : .I32] = {
.con for_exit [acc : [.I32, .I32]] = {
return (mem, acc#.ff)
};

%affine.For (.i32, 2, (.I32, .I32)) (start, stop, step, (0I32, 5I32), for_body, for_exit)
};
atoi (_19331#.ff, _19331#.tt, atoi_cont_step)
};
atoi (_19247#.ff, _19247#.tt, atoi_cont_end)
};
.con atoi_cont_step [mem : %mem.M, step : .I32] =
.con for_exit [acc : [.I32, .I32]] = return (mem, acc#.ff);

%affine.For (.i32, 2, (.I32, .I32)) (start, stop, step, (0I32, 5I32), for_body, for_exit);
atoi (_19331#.ff, _19331#.tt, atoi_cont_step);
atoi (_19247#.ff, _19247#.tt, atoi_cont_end);

.let _19093: %mem.Ptr (%mem.Ptr («⊤:.Nat; .I8», 0), 0) = %mem.lea (⊤:.Nat, ‹⊤:.Nat; %mem.Ptr («⊤:.Nat; .I8», 0)›, 0) (argv, 1I32);
.let _19163: [%mem.M, %mem.Ptr («⊤:.Nat; .I8», 0)] = %mem.load (mem, _19093);
atoi (_19163#.ff, _19163#.tt, atoi_cont_begin)
};
atoi (_19163#.ff, _19163#.tt, atoi_cont_begin);

// CHECK-NOT: affine.for
19 changes: 8 additions & 11 deletions lit/affine/for_2acc.thorin
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@
.plugin core;
.plugin affine;

.con .extern main [mem : %mem.M, argc : .I32, argv : %mem.Ptr (%mem.Ptr (.I32, 0), 0), return : .Cn [%mem.M, .I32]] = {
.con for_exit [acc : [.I32, .I32]] = {
return (mem, acc#.ff)
};

.con for_body [i : .I32, acc : [.I32, .I32], continue : .Cn [[.I32, .I32]]] = {
.let a : .I32 = %core.wrap.add 0 (i, acc#.ff);
.let b : .I32 = %core.wrap.sub 0 (i, acc#.tt);
continue (a, b)
};
.con .extern main [mem : %mem.M, argc : .I32, argv : %mem.Ptr (%mem.Ptr (.I32, 0), 0), return : .Cn [%mem.M, .I32]] =
%affine.For (.i32, 2, (.I32, .I32)) (0I32, argc, 1I32, (0I32, 0I32), for_body, for_exit)
};
.where
.con for_body [i : .I32, acc : [.I32, .I32], continue : .Cn [[.I32, .I32]]] =
.let a : .I32 = %core.wrap.add 0 (i, acc#.ff);
.let b : .I32 = %core.wrap.sub 0 (i, acc#.tt);
continue (a, b);
.con for_exit [acc : [.I32, .I32]] = return (mem, acc#.ff);
.end;

// CHECK-NOT: affine.for
19 changes: 8 additions & 11 deletions lit/affine/for_2acc_2types.thorin
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@
.plugin core;
.plugin affine;

.con .extern main [mem : %mem.M, argc : .I32, argv : %mem.Ptr (%mem.Ptr (.I32, 0), 0), return : .Cn [%mem.M, .Idx 0]] = {
.con for_exit [acc : [.I32, .Idx 0]] = {
return (mem, acc#.tt)
};

.con for_body [i : .I32, acc : [.I32, .Idx 0], continue : .Cn [[.I32, .Idx 0]]] = {
.let a : .I32 = %core.wrap.add 0 (i, acc#.ff);
.let b : .Idx 0 = %core.wrap.add 0 (%core.conv.u 0 i, acc#.tt);
continue (a, b)
};
.con .extern main [mem : %mem.M, argc : .I32, argv : %mem.Ptr (%mem.Ptr (.I32, 0), 0), return : .Cn [%mem.M, .Idx 0]] =
%affine.For (.i32, 2, (.I32, .Idx 0)) (0I32, argc, 1I32, (0I32, 0:(.Idx 0)), for_body, for_exit)
};
.where
.con for_body [i : .I32, acc : [.I32, .Idx 0], continue : .Cn [[.I32, .Idx 0]]] =
.let a : .I32 = %core.wrap.add 0 (i, acc#.ff);
.let b : .Idx 0 = %core.wrap.add 0 (%core.conv.u 0 i, acc#.tt);
continue (a, b);
.con for_exit [acc : [.I32, .Idx 0]] = return (mem, acc#.tt);
.end;

// CHECK-NOT: affine.for
19 changes: 8 additions & 11 deletions lit/affine/for_2acc_real.thorin
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@
.plugin math;
.plugin affine;

.con .extern main [mem : %mem.M, argc : .I32, argv : %mem.Ptr (%mem.Ptr (.I32, 0), 0), return : .Cn [%mem.M, .Idx 0]] = {
.con for_exit [acc : [.I32, %math.F64]] = {
return (mem, %math.conv.f2u 0 acc#.tt)
};

.con for_body [i : .I32, [acc_a : .I32, acc_b : %math.F64], continue : .Cn [[.I32, %math.F64]]] = {
.let a : .I32 = %core.wrap.add 0 (i, acc_a);
.let b : %math.F64 = %math.conv.u2f %math.f64 (%core.wrap.add 0 (%core.conv.u 0 i, %math.conv.f2u 0 acc_b));
continue (a, b)
};
.con .extern main [mem : %mem.M, argc : .I32, argv : %mem.Ptr (%mem.Ptr (.I32, 0), 0), return : .Cn [%mem.M, .Idx 0]] =
%affine.For (.i32, 2, (.I32, %math.F64)) (0I32, argc, 1I32, (0I32, 0:%math.F64), for_body, for_exit)
};
.where
.con for_body [i : .I32, [acc_a : .I32, acc_b : %math.F64], continue : .Cn [[.I32, %math.F64]]] =
.let a = %core.wrap.add 0 (i, acc_a);
.let b = %math.conv.u2f %math.f64 (%core.wrap.add 0 (%core.conv.u 0 i, %math.conv.f2u 0 acc_b));
continue (a, b);
.con for_exit [acc : [.I32, %math.F64]] = return (mem, %math.conv.f2u 0 acc#.tt);
.end;

// CHECK-NOT: affine.for
// CHECK: %math.conv.f2u
38 changes: 20 additions & 18 deletions lit/affine/for_over_mem.thorin
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,25 @@
.plugin core;
.plugin affine;

.con .extern main [mem : %mem.M, argc : .I32, argv : %mem.Ptr (%mem.Ptr (.I32, 0), 0), return : .Cn [%mem.M, .I32]] = {
// .let arr_size = 16;
.let arr_size = ⊤:.Nat;
.let (alloc_mem, ptr) = %mem.alloc (<<%core.bitcast .Nat argc; .I32>>, 0) (mem);
.con for_exit acc :: [mem : %mem.M, .I32, .I32] = {
.let lea = %mem.lea (arr_size, <arr_size; .I32>, 0) (ptr, %core.conv.u arr_size (%core.wrap.sub 0 (argc, 4I32)));
.let (load_mem, val) = %mem.load (mem, lea);
return (load_mem, val)
};
.con for_body [i : .I32, [mem : %mem.M, acc_a : .I32, acc_b : .I32], continue : .Cn [%mem.M, .I32, .I32]] = {
.let a : .I32 = %core.wrap.add 0 (i, acc_a);
.let b : .I32 = %core.wrap.sub 0 (i, acc_b);
.let lea = %mem.lea (arr_size, <arr_size; .I32>, 0) (ptr, %core.conv.u arr_size i);
.let store_mem = %mem.store (mem, lea, a);
continue (store_mem, a, b)
};
%affine.For (.i32, 3, (%mem.M, .I32, .I32)) (0I32, argc, 1I32, (alloc_mem, 0I32, 0I32), for_body, for_exit)
};
.let arr_size = ⊤:.Nat;
// .let arr_size = 16;

.con .extern main [mem: %mem.M, argc: .I32, argv: %mem.Ptr (%mem.Ptr (.I32, 0), 0), return: .Cn [%mem.M, .I32]] =
.let (`mem, ptr) = %mem.alloc (<<%core.bitcast .Nat argc; .I32>>, 0) (mem);
%affine.For (.i32, 3, (%mem.M, .I32, .I32)) (0I32, argc, 1I32, (mem, 0I32, 0I32), for_body, for_exit)
.where
.con for_body [i: .I32, [mem: %mem.M, acc_a: .I32, acc_b: .I32], continue: .Cn [%mem.M, .I32, .I32]] =
.let a = %core.wrap.add 0 (i, acc_a);
.let b = %core.wrap.sub 0 (i, acc_b);
.let lea = %mem.lea (arr_size, <arr_size; .I32>, 0) (ptr, %core.conv.u arr_size i);
.let `mem = %mem.store (mem, lea, a);
continue (mem, a, b);

.con for_exit [mem: %mem.M, .I32, .I32]::acc =
.let lea = %mem.lea (arr_size, <arr_size; .I32>, 0) (ptr, %core.conv.u arr_size (%core.wrap.sub 0 (argc, 4I32)));
.let (`mem, val)::ld = %mem.load (mem, lea);
return ld;
.end;


// CHECK-NOT: affine.For
45 changes: 19 additions & 26 deletions lit/autodiff/autodiff_mult_in_call.thorin
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,28 @@
.plugin autodiff;


.con swap [[a:.I32, b:.I32], ret: .Cn [.I32,.I32]] = {
ret (b,a)
};
.con swap [[a:.I32, b:.I32], ret: .Cn [.I32,.I32]] = ret (b,a);

.con f [[a:.I32, b:.I32], ret: .Cn [.I32]] = {
.con f_cont [x:.I32,y:.I32] = {
ret x
};
swap((a,b),f_cont)
};

.con .extern main [mem : %mem.M, argc : .I32, argv : %mem.Ptr (%mem.Ptr (.I8, 0), 0), return : .Cn [%mem.M, .I32]] = {

.con ret_cont [r:.I32,pb:.Cn[.I32,.Cn[.I32,.I32]]] = {
.con pb_ret_cont [pr_a:.I32,pr_b:.I32] = {
.let sr = %core.wrap.mul 0 (10000I32, r);
.let sa = %core.wrap.mul 0 (100I32, pr_a);
.let sb = pr_b;
.let sp = %core.wrap.add 0 (sa, sb);
.let o = %core.wrap.add 0 (sr, sp);
return (mem, o)
};
pb(1I32,pb_ret_cont)
};
.con f [[a:.I32, b:.I32], ret: .Cn [.I32]] =
.con f_cont [x:.I32,y:.I32] = ret x;
swap((a,b),f_cont);

.con .extern main [mem: %mem.M, argc: .I32, argv: %mem.Ptr (%mem.Ptr (.I8, 0), 0), return: .Cn [%mem.M, .I32]] =
.let f_diff = %autodiff.ad f;

.let c = (42I32,43I32);
.let c = (42I32, 43I32);
f_diff (c,ret_cont)
};
.where
.con ret_cont [r:.I32,pb:.Cn[.I32,.Cn[.I32,.I32]]] =
pb(1I32,pb_ret_cont)
.where
.con pb_ret_cont [pr_a:.I32,pr_b:.I32] =
.let sr = %core.wrap.mul 0 (10000I32, r);
.let sa = %core.wrap.mul 0 (100I32, pr_a);
.let sb = pr_b;
.let sp = %core.wrap.add 0 (sa, sb);
.let o = %core.wrap.add 0 (sr, sp);
return (mem, o);
.end;
.end;

// CHECK-DAG: return{{.*}}430001
41 changes: 17 additions & 24 deletions lit/autodiff/autodiff_mult_in_call2.thorin
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,25 @@
.plugin autodiff;


.con snd [[a:.I32, b:.I32], ret: .Cn [.I32]] = {
ret (b)
};

.con f [[a:.I32, b:.I32], ret: .Cn [.I32]] = {
snd((a,b),ret)
};

.con .extern main [mem : %mem.M, argc : .I32, argv : %mem.Ptr (%mem.Ptr (.I8, 0), 0), return : .Cn [%mem.M, .I32]] = {

.con ret_cont [r:.I32,pb:.Cn[.I32,.Cn[.I32,.I32]]] = {
.con pb_ret_cont [pr_a:.I32,pr_b:.I32] = {
.let sr = %core.wrap.mul 0 (10000I32, r);
.let sa = %core.wrap.mul 0 (100I32, pr_a);
.let sb = pr_b;
.let sp = %core.wrap.add 0 (sa, sb);
.let o = %core.wrap.add 0 (sr, sp);
return (mem, o)
};
pb(1I32,pb_ret_cont)
};
.con snd [[a:.I32, b:.I32], ret: .Cn [.I32]] = ret (b);
.con f [[a:.I32, b:.I32], ret: .Cn [.I32]] = snd((a,b),ret);

.con .extern main [mem : %mem.M, argc : .I32, argv : %mem.Ptr (%mem.Ptr (.I8, 0), 0), return : .Cn [%mem.M, .I32]] =
.let f_diff = %autodiff.ad f;

.let c = (42I32,43I32);
.let c = (42I32,43I32);
f_diff (c,ret_cont)
};
.where
.con ret_cont [r:.I32,pb:.Cn[.I32,.Cn[.I32,.I32]]] =
pb(1I32,pb_ret_cont)
.where
.con pb_ret_cont [pr_a:.I32,pr_b:.I32] =
.let sr = %core.wrap.mul 0 (10000I32, r);
.let sa = %core.wrap.mul 0 (100I32, pr_a);
.let sb = pr_b;
.let sp = %core.wrap.add 0 (sa, sb);
.let o = %core.wrap.add 0 (sr, sp);
return (mem, o)
.end;
.end;

// CHECK-DAG: return{{.*}}430001
42 changes: 17 additions & 25 deletions lit/autodiff/autodiff_mult_in_call2_2.thorin
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,25 @@

.plugin autodiff;

.fun fst(a b:.I32): .I32 = return a;
.fun f(a b: .I32): .I32 = fst ((a, b), return);

.con fst [[a:.I32, b:.I32], ret: .Cn [.I32]] = {
ret (a)
};

.con f [[a:.I32, b:.I32], ret: .Cn [.I32]] = {
fst((a,b),ret)
};

.con .extern main [mem : %mem.M, argc : .I32, argv : %mem.Ptr (%mem.Ptr (.I8, 0), 0), return : .Cn [%mem.M, .I32]] = {

.con ret_cont [r:.I32,pb:.Cn[.I32,.Cn[.I32,.I32]]] = {
.con pb_ret_cont [pr_a:.I32,pr_b:.I32] = {
.let sr = %core.wrap.mul 0 (10000I32, r);
.let sa = %core.wrap.mul 0 (100I32, pr_a);
.let sb = pr_b;
.let sp = %core.wrap.add 0 (sa, sb);
.let o = %core.wrap.add 0 (sr, sp);
return (mem, o)
};
pb(1I32,pb_ret_cont)
};

.con .extern main [mem : %mem.M, argc : .I32, argv : %mem.Ptr (%mem.Ptr (.I8, 0), 0), return : .Cn [%mem.M, .I32]] =
.let f_diff = %autodiff.ad f;

.let c = (42I32,43I32);
.let c = (42I32, 43I32);
f_diff (c,ret_cont)
};
.where
.con ret_cont [r:.I32,pb:.Cn[.I32,.Cn[.I32,.I32]]] =
pb(1I32,pb_ret_cont)
.where
.con pb_ret_cont [pr_a:.I32,pr_b:.I32] =
.let sr = %core.wrap.mul 0 (10000I32, r);
.let sa = %core.wrap.mul 0 (100I32, pr_a);
.let sb = pr_b;
.let sp = %core.wrap.add 0 (sa, sb);
.let o = %core.wrap.add 0 (sr, sp);
return (mem, o);
.end;
.end;

// CHECK-DAG: return{{.*}}420100
Loading
Loading