BEAM SSA Checks =============== While developing optimizations operating on the BEAM SSA it is often hard to check that various transforms have the intended effect. For example, unless a transform produces crashing code, it is hard to detect that the transform is broken. Likewise missing an optimization opportunity is also hard to detect. To simplify the creation of tests on BEAM SSA the compiler has an internal mode in which it parses and checks assertions on the structure and content of the produced BEAM SSA code. This is a short introduction to the syntax and semantics of the SSA checker functionality. Syntax ------ SSA checks are embedded in the source code as comments starting with with one of `%ssa%`, `%%ssa%` or `%%%ssa%`. This is a short introduction the syntax, for the full syntax please refer to the `ssa_check_when_clause` production in `erl_parse.yrl`. SSA checks can be placed inside any Erlang function, for example: t0() -> %ssa% () when post_ssa_opt -> %ssa% ret(#{}). #{}. will check that `t0/0` returns the literal `#{}`. If we want to check that a function returns its first formal parameter, we can write: t1(A, _B) -> %ssa% (X, _) when post_ssa_opt -> %ssa% ret(X). A. Note how we match the first formal parameter using `X`. The reason for having our own formal parameters for the SSA check, is that we don't want to introduce new identifiers at the Erlang level to support SSA-level checks. Consider if `t1/2` had been defined as `t1([A|As], B)` we would have had to introduce a new identifier for the aggregate value `[A|As]`. The full syntax for a SSA check clause is: ? () when -> '.' where `` can be one of `pass` (the check must succeed), `fail` and `xfail` (the check must fail). Omitting `` is parsed as an implicit `pass`. `` is a comma-separated list of variables. `` specifies when in the compiler pipeline to run the checks. For now the only supported value for `` is `post_ssa_opt` which runs the checks after the `ssa_opt` pass. `` is a comma-separated list of matches against the BEAM SSA code. For non-flow-control operations the syntax is: = ( ) ? where `` is the `#b_set.op` field from the internal SSA representation. BIFs are written as `bif:`. `` is a comma-separated list of variables or literals. For flow control operations and labels, the syntax is as follows: br(, , ) switch(, , [{