[zeek/spicy] d1d1c6: Make `switch` and `if` assignments into values

0 views
Skip to first unread message

Evan Typanski

unread,
Jul 24, 2026, 10:53:44 AM (2 days ago) Jul 24
to spicy-...@zeek.org
Branch: refs/heads/topic/etyp/move-last-uses
Home: https://github.com/zeek/spicy
Commit: d1d1c6fc5f8a21ddcdb858bbd9d275fdadf40202
https://github.com/zeek/spicy/commit/d1d1c6fc5f8a21ddcdb858bbd9d275fdadf40202
Author: Evan Typanski <evan.t...@corelight.com>
Date: 2026-07-24 (Fri, 24 Jul 2026)

Changed paths:
M hilti/toolchain/src/compiler/codegen/statements.cc

Log Message:
-----------
Make `switch` and `if` assignments into values

When making optimizer passes, the values from initializers in switch and
if statements look like values, not references. Using `&&` can make a
reference, which would make the optimizer do the wrong thing if it messed
with it.

This forces them to just use `auto` so that future optimizer passes don't
accidentally move from an aliased reference, for example.


Commit: b9119f073317b12c3bc78e255e8b84d0300b4f17
https://github.com/zeek/spicy/commit/b9119f073317b12c3bc78e255e8b84d0300b4f17
Author: Evan Typanski <evan.t...@corelight.com>
Date: 2026-07-24 (Fri, 24 Jul 2026)

Changed paths:
M hilti/runtime/CMakeLists.txt
M hilti/runtime/include/util.h
A hilti/runtime/src/tests/move.cc
M hilti/toolchain/src/compiler/codegen/expressions.cc
M tests/hilti/optimization/flatten-block.hlt
M tests/hilti/output/optimization/cfg-const-copy-prop.hlt

Log Message:
-----------
Make hilti moves a RT function

This avoids adding C++ semantics to the hilti codegen. Instead, it's just
a runtime helper that moves-or-not.

This also uncovered a "bug" in codegen: switch statements used auto&&,
which could bind to an lvalue, but everything in hilti sees it as an
rvalue. Since we don't really have a good reason to force this as a
forwarding reference, just use `auto` here. Then optimizers can rely
on the reality they see: these are not references.


Commit: c62bb264c31f23ad26fe9b8b769c5f441a031149
https://github.com/zeek/spicy/commit/c62bb264c31f23ad26fe9b8b769c5f441a031149
Author: Evan Typanski <evan.t...@corelight.com>
Date: 2026-07-24 (Fri, 24 Jul 2026)

Changed paths:
M hilti/toolchain/src/compiler/cfg.cc
M tests/Baseline/hilti.cfg.switch/output

Log Message:
-----------
Fix CFG for switch cases

The CFG was making many parallel branches for a switch:

switch (s)
/ | \
/ | \
/ | \
/ | \
/ | \
s == "a" s == "b" s == "c"
| | |
| | |
case A case B case C

But, that's not what's happening. In reality, the first case will compare
first, then the second, then the third: it's not a choice, it's a
sequential series of comparisons:

switch (s)
|
|
s == "a"
| \
| \
| case A
|
s == "b"
| \
| \
| case B
|
s == "c"
\
\
case C

This bug would make 's' appear dead after each case expression
(comparison with "a", "b", and "c") when in reality it's only dead after
the comparison with "c".

This just updated the switch CFG baselines, since including the test for
move last uses that caused it (basically a switch over multiple strings)
would require baselining C++, which is a bit brittle.


Commit: a4b278fd5abd45e2b9680dd728d99e2a6c1a9d64
https://github.com/zeek/spicy/commit/a4b278fd5abd45e2b9680dd728d99e2a6c1a9d64
Author: Evan Typanski <evan.t...@corelight.com>
Date: 2026-07-24 (Fri, 24 Jul 2026)

Changed paths:
M hilti/toolchain/CMakeLists.txt
M hilti/toolchain/include/compiler/detail/optimizer/pass-id.h
A hilti/toolchain/src/compiler/optimizer/passes/move-last-uses.cc
M tests/Baseline/hilti.cfg.dead-write-shadowed/output
M tests/Baseline/hilti.cfg.local-struct-value/output
M tests/Baseline/hilti.cfg.unreachable-return/output
M tests/Baseline/hilti.optimization.dataflow-unreachable/output
M tests/Baseline/hilti.optimization.unimplemented_hook/log
M tests/Baseline/hilti.optimization.unreachable-code/log
M tests/Baseline/hilti.optimization.unused-init/output
M tests/Baseline/hilti.optimization.unused_function/log
M tests/Baseline/hilti.types.struct.on-heap/output
M tests/Baseline/spicy.optimization.default-parser-functions/log
M tests/Baseline/spicy.optimization.default-parser-functions/opt.hlt
M tests/Baseline/spicy.optimization.feature_requirements/log
M tests/Baseline/spicy.optimization.feature_requirements/opt.hlt
M tests/Baseline/spicy.optimization.unused-functions/log
M tests/Baseline/spicy.optimization.unused-functions/opt.hlt
M tests/Baseline/spicy.optimization.unused-types/log
M tests/Baseline/spicy.optimization.unused-types/opt.hlt
M tests/Baseline/spicy.rt.debug-trace/.stderr
A tests/hilti/output/optimization/move-last-uses.hlt

Log Message:
-----------
Implement optimizer pass to `move` last uses

Closes #1960

Adds an optimization pass that adds moves when it's the last use. This is
relatively conservative; it doesn't try to understand expression ordering
and won't move in many cases. But, in user-code (not generated parser
code) this could give a bit of a speedup.

The analysis is just a liveness analysis. If the name isn't live after a
CFG node, we theoretically can move it.

Whether or not a node is "trivially copyable" is up to that node to
determine. We could use this in more places that we generate moves as
well.

Remove is trivially movable


Compare: https://github.com/zeek/spicy/compare/0441af31e9fe...a4b278fd5abd

To unsubscribe from these emails, change your notification settings at https://github.com/zeek/spicy/settings/notifications
Reply all
Reply to author
Forward
0 new messages