Skip to content

Commit

Permalink
Attempt to reduce stack size again.
Browse files Browse the repository at this point in the history
  • Loading branch information
rsmmr committed Jul 9, 2024
1 parent 4323ace commit 05f7749
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 34 deletions.
18 changes: 6 additions & 12 deletions spicy/toolchain/src/compiler/codegen/parser-builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,13 @@ struct ProductionVisitor : public production::Visitor {
}

// Creates a new temporary variable of a give type and pushes that onto the
// destination stack. If the types takes parameters, we wrap the type into
// an unset optional because we wouldn't be able default instantiate it. In
// the case, we push a deref operation to the stack for accessing the
// value, so that usage remains transparent.
// destination stack. TODO: Update comment.
void pushTmpDestination(const std::string& prefix, QualifiedType* t) {
if ( ! t->type()->parameters().empty() ) {
auto dst = builder()->addTmp(prefix, builder()->typeOptional(t));
pushDestination(builder()->deref(dst), dst);
}
else {
auto dst = builder()->addTmp(prefix, t);
pushDestination(dst);
}
if ( t->type()->isA<type::Unit>() )
t = builder()->qualifiedType(builder()->typeValueReference(t), t->constness());

auto dst = builder()->addTmp(prefix, t);
pushDestination(dst);
}

// RAII helper to update the visitor's `_path` as we descend the parse tree.
Expand Down
4 changes: 2 additions & 2 deletions tests/Baseline/spicy.optimization.unused-functions/noopt.hlt
Original file line number Diff line number Diff line change
Expand Up @@ -1048,12 +1048,12 @@ method method tuple<const view<stream>, int<64>, const iterator<stream>, optiona
method method tuple<const view<stream>, int<64>, const iterator<stream>, optional<hilti::RecoverableFailure>> foo::D::__parse_foo__D_stage2(inout value_ref<stream> __data, iterator<stream> __begin, copy view<stream> __cur, copy bool __trim, copy int<64> __lah, copy iterator<stream> __lahe, copy optional<hilti::RecoverableFailure> __error) {
# "<...>/unused-functions.spicy:25:17-27:1"
local tuple<view<stream>, int<64>, const iterator<stream>, optional<hilti::RecoverableFailure>> __result;
local C __transient__anon;
local value_ref<C> __transient__anon;
# "<...>/unused-functions.spicy:24:10-24:16"

# Begin parsing production: Unit: foo__C_2 ->
__transient__anon = default<const C>();
(__cur, __lah, __lahe, __error) = __transient__anon.__parse_stage1(__data, __begin, __cur, __trim, __lah, __lahe, __error);
(__cur, __lah, __lahe, __error) = (*__transient__anon).__parse_stage1(__data, __begin, __cur, __trim, __lah, __lahe, __error);
# End parsing production: Unit: foo__C_2 ->

if ( ::__feat%foo@@D%uses_random_access )
Expand Down
4 changes: 2 additions & 2 deletions tests/Baseline/spicy.optimization.unused-functions/opt.hlt
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,12 @@ method method tuple<const view<stream>, int<64>, const iterator<stream>, optiona
method method tuple<const view<stream>, int<64>, const iterator<stream>, optional<hilti::RecoverableFailure>> foo::D::__parse_foo__D_stage2(inout value_ref<stream> __data, iterator<stream> __begin, copy view<stream> __cur, copy bool __trim, copy int<64> __lah, copy iterator<stream> __lahe, copy optional<hilti::RecoverableFailure> __error) {
# "<...>/unused-functions.spicy:25:17-27:1"
local tuple<view<stream>, int<64>, const iterator<stream>, optional<hilti::RecoverableFailure>> __result;
local C __transient__anon;
local value_ref<C> __transient__anon;
# "<...>/unused-functions.spicy:24:10-24:16"

# Begin parsing production: Unit: foo__C_2 ->
__transient__anon = default<const C>();
(__cur, __lah, __lahe, __error) = __transient__anon.__parse_stage1(__data, __begin, __cur, __trim, __lah, __lahe, __error);
(__cur, __lah, __lahe, __error) = (*__transient__anon).__parse_stage1(__data, __begin, __cur, __trim, __lah, __lahe, __error);
# End parsing production: Unit: foo__C_2 ->

(*self).__error = __error;
Expand Down
2 changes: 1 addition & 1 deletion tests/Baseline/spicy.optimization.unused-types/log
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@
[debug/optimizer] [unused-types.spicy:21:14-24:1] declaration::Function "method extern method view<stream> foo::Priv4::parse1(inout value_ref<stream> __data, optional<view<stream>> __cur = Null, optional<spicy_rt::UnitContext> __context) &needed-by-feature="is_filter" &needed-by-feature="supports_sinks" &static { # "<...>/unused-types.spicy:21:14-24:1" local value_ref<foo::Priv4> __unit = value_ref(default<const foo::Priv4>()); local view<stream> __ncur = __cur ? (*__cur) : cast<view<stream>>((*__data)); local int<64> __lahead = 0; local iterator<stream> __lahead_end; local optional<hilti::RecoverableFailure> __error = Null; # "<...>/unused-types.spicy:21:14-24:1" # Begin parsing production: Unit: foo__Priv4 -> Resolved_6 Resolved_7 (__ncur, __lahead, __lahead_end, __error) = (*__unit).__parse_stage1(__data, begin(__ncur), __ncur, True, __lahead, __lahead_end, __error); # End parsing production: Unit: foo__Priv4 -> Resolved_6 Resolved_7 if ( __error ) throw ; return __ncur; }" -> null (removing declaration for unused function)
[debug/optimizer] [unused-types.spicy:21:14-24:1] declaration::Function "method extern method view<stream> foo::Priv4::parse2(inout value_ref<foo::Priv4> __unit, inout value_ref<stream> __data, optional<view<stream>> __cur = Null, optional<spicy_rt::UnitContext> __context) &needed-by-feature="is_filter" &needed-by-feature="supports_sinks" &static { # "<...>/unused-types.spicy:21:14-24:1" local view<stream> __ncur = __cur ? (*__cur) : cast<view<stream>>((*__data)); local int<64> __lahead = 0; local iterator<stream> __lahead_end; local optional<hilti::RecoverableFailure> __error = Null; # "<...>/unused-types.spicy:21:14-24:1" # Begin parsing production: Unit: foo__Priv4 -> Resolved_6 Resolved_7 (__ncur, __lahead, __lahead_end, __error) = (*__unit).__parse_stage1(__data, begin(__ncur), __ncur, True, __lahead, __lahead_end, __error); # End parsing production: Unit: foo__Priv4 -> Resolved_6 Resolved_7 if ( __error ) throw ; return __ncur; }" -> null (removing declaration for unused function)
[debug/optimizer] [unused-types.spicy:21:14-24:1] declaration::Function "method extern method view<stream> foo::Priv4::parse3(inout value_ref<spicy_rt::ParsedUnit> __gunit, inout value_ref<stream> __data, optional<view<stream>> __cur = Null, optional<spicy_rt::UnitContext> __context) &needed-by-feature="is_filter" &needed-by-feature="supports_sinks" &static { # "<...>/unused-types.spicy:21:14-24:1" local value_ref<foo::Priv4> __unit = value_ref(default<const foo::Priv4>()); spicy_rt::initializeParsedUnit((*__gunit), __unit, typeinfo(foo::Priv4)); local view<stream> __ncur = __cur ? (*__cur) : cast<view<stream>>((*__data)); local int<64> __lahead = 0; local iterator<stream> __lahead_end; local optional<hilti::RecoverableFailure> __error = Null; # "<...>/unused-types.spicy:21:14-24:1" # Begin parsing production: Unit: foo__Priv4 -> Resolved_6 Resolved_7 (__ncur, __lahead, __lahead_end, __error) = (*__unit).__parse_stage1(__data, begin(__ncur), __ncur, True, __lahead, __lahead_end, __error); # End parsing production: Unit: foo__Priv4 -> Resolved_6 Resolved_7 if ( __error ) throw ; return __ncur; }" -> null (removing declaration for unused function)
[debug/optimizer] [unused-types.spicy:21:14-24:1] declaration::Function "method method tuple<const view<stream>, int<64>, const iterator<stream>, optional<hilti::RecoverableFailure>> foo::Priv4::__parse_foo__Priv4_stage2(inout value_ref<stream> __data, iterator<stream> __begin, copy view<stream> __cur, copy bool __trim, copy int<64> __lah, copy iterator<stream> __lahe, copy optional<hilti::RecoverableFailure> __error) { # "<...>/unused-types.spicy:21:14-24:1" local tuple<view<stream>, int<64>, const iterator<stream>, optional<hilti::RecoverableFailure>> __result; local Priv2 __transient__anon; # "<...>/unused-types.spicy:19:14-19:20" # Begin parsing production: Unit: foo__Priv2_2 -> __transient__anon = default<const foo::Priv2>(); (__cur, __lah, __lahe, __error) = __transient__anon.__parse_stage1(__data, __begin, __cur, __trim, __lah, __lahe, __error); # End parsing production: Unit: foo__Priv2_2 -> # "<...>/unused-types.spicy:20:14-20:20" # Begin parsing production: Unit: foo__Priv3_2 -> (*self).x = default<const foo::Priv3>(); (__cur, __lah, __lahe, __error) = (*self).x.__parse_stage1(__data, __begin, __cur, __trim, __lah, __lahe, __error); # End parsing production: Unit: foo__Priv3_2 -> (*self).__error = __error; default<void>(); __error = (*self).__error; (*self).__error = __error; default<void>(); __error = (*self).__error; hilti::debugDedent("spicy"); __result = (__cur, __lah, __lahe, __error); return __result; }" -> null (removing declaration for unused function)
[debug/optimizer] [unused-types.spicy:21:14-24:1] declaration::Function "method method tuple<const view<stream>, int<64>, const iterator<stream>, optional<hilti::RecoverableFailure>> foo::Priv4::__parse_foo__Priv4_stage2(inout value_ref<stream> __data, iterator<stream> __begin, copy view<stream> __cur, copy bool __trim, copy int<64> __lah, copy iterator<stream> __lahe, copy optional<hilti::RecoverableFailure> __error) { # "<...>/unused-types.spicy:21:14-24:1" local tuple<view<stream>, int<64>, const iterator<stream>, optional<hilti::RecoverableFailure>> __result; local value_ref<Priv2> __transient__anon; # "<...>/unused-types.spicy:19:14-19:20" # Begin parsing production: Unit: foo__Priv2_2 -> __transient__anon = default<const foo::Priv2>(); (__cur, __lah, __lahe, __error) = (*__transient__anon).__parse_stage1(__data, __begin, __cur, __trim, __lah, __lahe, __error); # End parsing production: Unit: foo__Priv2_2 -> # "<...>/unused-types.spicy:20:14-20:20" # Begin parsing production: Unit: foo__Priv3_2 -> (*self).x = default<const foo::Priv3>(); (__cur, __lah, __lahe, __error) = (*self).x.__parse_stage1(__data, __begin, __cur, __trim, __lah, __lahe, __error); # End parsing production: Unit: foo__Priv3_2 -> (*self).__error = __error; default<void>(); __error = (*self).__error; (*self).__error = __error; default<void>(); __error = (*self).__error; hilti::debugDedent("spicy"); __result = (__cur, __lah, __lahe, __error); return __result; }" -> null (removing declaration for unused function)
[debug/optimizer] [unused-types.spicy:21:14-24:1] declaration::Function "method method tuple<const view<stream>, int<64>, const iterator<stream>, optional<hilti::RecoverableFailure>> foo::Priv4::__parse_stage1(inout value_ref<stream> __data, iterator<stream> __begin, copy view<stream> __cur, copy bool __trim, copy int<64> __lah, copy iterator<stream> __lahe, copy optional<hilti::RecoverableFailure> __error) { # "<...>/unused-types.spicy:21:14-24:1" local tuple<view<stream>, int<64>, const iterator<stream>, optional<hilti::RecoverableFailure>> __result; try { hilti::debugIndent("spicy"); local iterator<stream> __begin = begin(__cur); (*self).__error = __error; default<void>(); __error = (*self).__error; local strong_ref<stream> filtered = Null; if ( ! filtered ) __result = (*self).__parse_foo__Priv4_stage2(__data, __begin, __cur, __trim, __lah, __lahe, __error); } catch ( hilti::SystemException __except ) { default<void>(); (*self).__error = __error; default<void>(); __error = (*self).__error; throw; } (*self).__error = __error; default<void>(); __error = (*self).__error; return __result; }" -> null (removing declaration for unused function)
[debug/optimizer] [unused-types.spicy:21:14-24:1] operator_::struct_::MemberCall "(*self).__on_0x25_done()" -> expression::Ctor "default<void>()" (replacing call to unimplemented method with default value)
[debug/optimizer] [unused-types.spicy:21:14-24:1] operator_::struct_::MemberCall "(*self).__on_0x25_error(hilti::exception_what(__except))" -> expression::Ctor "default<void>()" (replacing call to unimplemented method with default value)
Expand Down
8 changes: 4 additions & 4 deletions tests/Baseline/spicy.optimization.unused-types/noopt.hlt
Original file line number Diff line number Diff line change
Expand Up @@ -1447,12 +1447,12 @@ method method tuple<const view<stream>, int<64>, const iterator<stream>, optiona
method method tuple<const view<stream>, int<64>, const iterator<stream>, optional<hilti::RecoverableFailure>> foo::Priv4::__parse_foo__Priv4_stage2(inout value_ref<stream> __data, iterator<stream> __begin, copy view<stream> __cur, copy bool __trim, copy int<64> __lah, copy iterator<stream> __lahe, copy optional<hilti::RecoverableFailure> __error) {
# "<...>/unused-types.spicy:21:14-24:1"
local tuple<view<stream>, int<64>, const iterator<stream>, optional<hilti::RecoverableFailure>> __result;
local Priv2 __transient__anon;
local value_ref<Priv2> __transient__anon;
# "<...>/unused-types.spicy:19:14-19:20"

# Begin parsing production: Unit: foo__Priv2_2 ->
__transient__anon = default<const Priv2>();
(__cur, __lah, __lahe, __error) = __transient__anon.__parse_stage1(__data, __begin, __cur, __trim, __lah, __lahe, __error);
(__cur, __lah, __lahe, __error) = (*__transient__anon).__parse_stage1(__data, __begin, __cur, __trim, __lah, __lahe, __error);
# End parsing production: Unit: foo__Priv2_2 ->

if ( ::__feat%foo@@Priv4%uses_random_access )
Expand Down Expand Up @@ -2231,12 +2231,12 @@ method method tuple<const view<stream>, int<64>, const iterator<stream>, optiona
method method tuple<const view<stream>, int<64>, const iterator<stream>, optional<hilti::RecoverableFailure>> foo::Pub3::__parse_foo__Pub3_stage2(inout value_ref<stream> __data, iterator<stream> __begin, copy view<stream> __cur, copy bool __trim, copy int<64> __lah, copy iterator<stream> __lahe, copy optional<hilti::RecoverableFailure> __error) {
# "<...>/unused-types.spicy:29:20-32:1"
local tuple<view<stream>, int<64>, const iterator<stream>, optional<hilti::RecoverableFailure>> __result;
local Priv5 __transient__anon_2;
local value_ref<Priv5> __transient__anon_2;
# "<...>/unused-types.spicy:27:14-27:20"

# Begin parsing production: Unit: foo__Priv5_2 ->
__transient__anon_2 = default<const Priv5>();
(__cur, __lah, __lahe, __error) = __transient__anon_2.__parse_stage1(__data, __begin, __cur, __trim, __lah, __lahe, __error);
(__cur, __lah, __lahe, __error) = (*__transient__anon_2).__parse_stage1(__data, __begin, __cur, __trim, __lah, __lahe, __error);
# End parsing production: Unit: foo__Priv5_2 ->

if ( ::__feat%foo@@Pub3%uses_random_access )
Expand Down
4 changes: 2 additions & 2 deletions tests/Baseline/spicy.optimization.unused-types/opt.hlt
Original file line number Diff line number Diff line change
Expand Up @@ -358,12 +358,12 @@ method method tuple<const view<stream>, int<64>, const iterator<stream>, optiona
method method tuple<const view<stream>, int<64>, const iterator<stream>, optional<hilti::RecoverableFailure>> foo::Pub3::__parse_foo__Pub3_stage2(inout value_ref<stream> __data, iterator<stream> __begin, copy view<stream> __cur, copy bool __trim, copy int<64> __lah, copy iterator<stream> __lahe, copy optional<hilti::RecoverableFailure> __error) {
# "<...>/unused-types.spicy:29:20-32:1"
local tuple<view<stream>, int<64>, const iterator<stream>, optional<hilti::RecoverableFailure>> __result;
local Priv5 __transient__anon_2;
local value_ref<Priv5> __transient__anon_2;
# "<...>/unused-types.spicy:27:14-27:20"

# Begin parsing production: Unit: foo__Priv5_2 ->
__transient__anon_2 = default<const Priv5>();
(__cur, __lah, __lahe, __error) = __transient__anon_2.__parse_stage1(__data, __begin, __cur, __trim, __lah, __lahe, __error);
(__cur, __lah, __lahe, __error) = (*__transient__anon_2).__parse_stage1(__data, __begin, __cur, __trim, __lah, __lahe, __error);
# End parsing production: Unit: foo__Priv5_2 ->

# "<...>/unused-types.spicy:28:14-28:20"
Expand Down
22 changes: 11 additions & 11 deletions tests/Baseline/spicy.types.unit.hooks-across-imports/.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,31 @@
[debug/ast-stats] - type::bytes::Iterator: 175
[debug/ast-stats] - type::operand_list::Operand: 1224
[debug/ast-stats] - type::stream::Iterator: 133
[debug/ast-stats] garbage collected 10020 nodes in 14 rounds, 21400 left retained
[debug/ast-stats] garbage collected 2444 nodes in 16 rounds, 25276 left retained
[debug/ast-stats] garbage collected 2221 nodes in 8 rounds, 26623 left retained
[debug/ast-stats] garbage collected 4543 nodes in 11 rounds, 27799 left retained
[debug/ast-stats] garbage collected 420 nodes in 3 rounds, 27799 left retained
[debug/ast-stats] garbage collected 10028 nodes in 14 rounds, 21407 left retained
[debug/ast-stats] garbage collected 2444 nodes in 16 rounds, 25286 left retained
[debug/ast-stats] garbage collected 2224 nodes in 8 rounds, 26636 left retained
[debug/ast-stats] garbage collected 4543 nodes in 11 rounds, 27812 left retained
[debug/ast-stats] garbage collected 420 nodes in 3 rounds, 27812 left retained
[debug/ast-stats] # [HILTI] AST statistics:
[debug/ast-stats] - # AST rounds 5
[debug/ast-stats] - max tree depth: 27
[debug/ast-stats] - # context declarations: 262
[debug/ast-stats] - # context types: 55
[debug/ast-stats] - # context modules: 7
[debug/ast-stats] - # nodes reachable in AST: 19021
[debug/ast-stats] - # nodes live: 27799
[debug/ast-stats] - # nodes retained: 27799
[debug/ast-stats] - # nodes reachable in AST: 19034
[debug/ast-stats] - # nodes live: 27812
[debug/ast-stats] - # nodes retained: 27812
[debug/ast-stats] - # nodes live > 1%:
[debug/ast-stats] - Attribute: 294
[debug/ast-stats] - AttributeSet: 752
[debug/ast-stats] - QualifiedType: 9161
[debug/ast-stats] - QualifiedType: 9166
[debug/ast-stats] - declaration::Parameter: 388
[debug/ast-stats] - expression::Ctor: 804
[debug/ast-stats] - expression::Member: 342
[debug/ast-stats] - expression::Name: 1036
[debug/ast-stats] - type::Bool: 334
[debug/ast-stats] - type::Member: 565
[debug/ast-stats] - type::Name: 517
[debug/ast-stats] - type::Name: 520
[debug/ast-stats] - type::OperandList: 784
[debug/ast-stats] - type::Optional: 465
[debug/ast-stats] - type::String: 422
Expand All @@ -66,5 +66,5 @@
[debug/ast-stats] - type::stream::Iterator: 1023
[debug/ast-stats] - type::stream::View: 557
[debug/ast-stats] - type::tuple::Element: 675
[debug/ast-stats] garbage collected 32442 nodes in 18 rounds, 0 left retained
[debug/ast-stats] garbage collected 32455 nodes in 18 rounds, 0 left retained
[debug/ast-stats] garbage collected 0 nodes in 1 round, 0 left retained

0 comments on commit 05f7749

Please sign in to comment.