Skip to content

[spec] specify host functions in spectec#2210

Merged
rossberg merged 6 commits into
WebAssembly:mainfrom
zilinc:zilinc/hostfunc
Jul 24, 2026
Merged

[spec] specify host functions in spectec#2210
rossberg merged 6 commits into
WebAssembly:mainfrom
zilinc:zilinc/hostfunc

Conversation

@zilinc

@zilinc zilinc commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

It breaks the prose backend.
The official spec still needs to splice the relevant additions from spectec.

@zilinc

zilinc commented Jul 15, 2026

Copy link
Copy Markdown
Contributor Author

@rossberg We talked about upstreaming this part, and here we go. It should be a good starting point, but there're still TODOs.

@rossberg rossberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks good so far.


def $lift_result(result) : instr* hint(show %)
def $lift_result(_VALS val*) = val*
def $lift_result(`(REF.EXN_ADDR a) THROW_REF) = (REF.EXN_ADDR a) THROW_REF

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the representation of exception results changes in #2189.

rule Step/call_ref-hostfunc-res:
s; f; val^n (REF.FUNC_ADDR a) (CALL_REF yy) ~> s'; f; $lift_result(result)
----
-- if $funcinst((s; f))[a] = fi

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it not work to use pattern matching here, like in the hand-written version?

@zilinc zilinc Jul 15, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the reason was that there's a MODULE field in the record as well, so it has to appear in the pattern. But otherwise I don't see any problems. I can do it if it's preferred.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right. In that case leave it as is.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering why I needed the parens around (s; f) in the $funcinst call. Maybe the reason is in the latex generation:

-\mbox{if}~ (s ; f){.}\mathsf{funcs}{}[a] = {\mathit{fi}} \\
+\mbox{if}~ s ; f{.}\mathsf{funcs}{}[a] = {\mathit{fi}} \\

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly. Or more precisely, the show hint for $funcinst removes the call parens..

@zilinc
zilinc force-pushed the zilinc/hostfunc branch from 2e45c1b to 58e434e Compare July 15, 2026 10:00
@rossberg

Copy link
Copy Markdown
Member

Quick reminder: please never, ever force-push to a PR branch, since that breaks half of GitHub's review work flow. ;)

@zilinc

zilinc commented Jul 21, 2026

Copy link
Copy Markdown
Contributor Author

@f52985 I'm stuck on the reduction rules. I have a function call on the rhs of a reduction, which is not supported by the AL pipeline. Would you please take a look? @rossberg Do we want to generate prose for them? Currently there's no prose for the manually written rules.

@rossberg

Copy link
Copy Markdown
Member

We don't necessarily need prose for these (since they are mostly interesting as part of a formalisation). But of course, the pipeline shouldn't break over them.

@f52985

f52985 commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator

@f52985 I'm stuck on the reduction rules. I have a function call on the rhs of a reduction, which is not supported by the AL pipeline. Would you please take a look?

https://github.com/f52985/spec/tree/hostfunc

In this branch, I implemented a fix that allows function calls—or, more generally, any expression whose type is a list of instructions—on the right-hand side of a reduction rule. It also seems to work correctly with the interpreter backend. Please check if this works, and let me know if something goes wrong.

@zilinc

zilinc commented Jul 23, 2026

Copy link
Copy Markdown
Contributor Author

@f52985 I'm stuck on the reduction rules. I have a function call on the rhs of a reduction, which is not supported by the AL pipeline. Would you please take a look?

https://github.com/f52985/spec/tree/hostfunc

In this branch, I implemented a fix that allows function calls—or, more generally, any expression whose type is a list of instructions—on the right-hand side of a reduction rule. It also seems to work correctly with the interpreter backend. Please check if this works, and let me know if something goes wrong.

Thanks heaps Dongjun. I'll give it a go and let you know.

@zilinc

zilinc commented Jul 24, 2026

Copy link
Copy Markdown
Contributor Author

@f52985 Thanks agian. The pipeline now works without errors. The prose merges the cases in a somewhat unexpected way, but since we are not using the prose for any of the rules here, it should be fine.

@zilinc

zilinc commented Jul 24, 2026

Copy link
Copy Markdown
Contributor Author

@rossberg This should be ready. The new rules look almost identical as before. I edited the doc a bit to reflect some meta-variable changes, and also tried to fix the manual prose for the normal function call case.

@rossberg rossberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, it wasn't necessary to change the prose for the normal function case, since that's gone anyway with #2189.

But while you're converting the host function section to SpecTec, would you mind including the inline math as well? For example, :math:`(s, \val^n)` becomes ${:(s, val^n)}.

@rossberg rossberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Two minor oversights, I believe.

Comment thread document/core/exec/instructions.rst Outdated
which means that it adheres to suitable pre- and post-conditions:
under a :ref:`valid store <valid-store>` :math:`S`, and given arguments :math:`\val^n` matching the ascribed parameter types :math:`t_1^n`,
executing the host function must yield a non-empty set of possible outcomes each of which is either divergence or consists of a valid store :math:`S'` that is an :ref:`extension <extend-store>` of :math:`S` and a result matching the ascribed return types :math:`t_2^m`.
under a :ref:`valid store <valid-store>` ${:s}, and given arguments ${:\val^n} matching the ascribed parameter types ${:t_1^n},

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
under a :ref:`valid store <valid-store>` ${:s}, and given arguments ${:\val^n} matching the ascribed parameter types ${:t_1^n},
under a :ref:`valid store <valid-store>` ${:s}, and given arguments ${:val^n} matching the ascribed parameter types ${:t_1^n},

Comment thread document/core/exec/instructions.rst Outdated
Here, :math:`\X{hf}(S; \val^n)` denotes the implementation-defined execution of host function :math:`\X{hf}` in current store :math:`S` with arguments :math:`\val^n`.
It yields a set of possible outcomes, where each element is either a pair of a modified store :math:`S'` and a :ref:`result <syntax-result>`
or the special value :math:`\bot` indicating divergence.
Here, ${:$hostcall(_HOSTFUNC hf, s, val^n)} denotes the implementation-defined execution of host function ${:_HOSTFUNC hf} in current store ${:s} with arguments ${:val^n}.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Here, ${:$hostcall(_HOSTFUNC hf, s, val^n)} denotes the implementation-defined execution of host function ${:_HOSTFUNC hf} in current store ${:s} with arguments ${:val^n}.
Here, ${:$hostcall(_HOSTFUNC hf, s, val^n)} denotes the implementation-defined execution of host function ${:hf} in current store ${:s} with arguments ${:val^n}.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for spotting them. The other one was an oversight; this one was intentional, but the intention was not quite correct.

@rossberg
rossberg merged commit dfa3f32 into WebAssembly:main Jul 24, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants