cc: elide push/pop in kernel_outb/outw when port is a literal#447
Merged
Conversation
The general non-const-value path in ``builtin_kernel_outb`` /
``builtin_kernel_outw`` was emitting an unconditional save-around-eval
guard:
eval value → AX
push AX
eval port → DX ; might clobber AX in the general case
pop AX
out dx, al
But when ``port`` is an ``Int`` literal, the port lowering is a single
``mov dx, <imm>`` that doesn't touch AX — so the save is dead. Add an
``isinstance(port_arg, Int)`` branch that evaluates value into AX,
then loads the literal port into DX with a bare ``mov``, and skips
the push/pop entirely.
Almost every kernel ``kernel_outb`` / ``kernel_outw`` call site uses a
hex-literal port (``0x21``, ``0x3F6``, ``0x300``, ...), so this fires
kernel-wide.
Real-kernel impact: 1,980 bytes of kasm reduction across the kernel
C build (folds in ata, fdc, ne2k, ps2, sb16, opl3, console, vga,
serial, rtc).
Covered by two new unit tests:
- ``test_kernel_outb_constant_port_runtime_value_no_push_pop``
- ``test_kernel_outw_constant_port_runtime_value_no_push_pop``
The pre-existing ``test_kernel_outb_variable_value_uses_push_pop``
test uses a non-Int port (a function parameter) so it still
exercises the general save-around-eval path and stays green.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The general non-const-value path in
builtin_kernel_outb/builtin_kernel_outwwas emitting an unconditional save-around-eval guard:But when
portis anIntliteral, the port lowering is a singlemov dx, <imm>that doesn't touch AX — so the save is dead. Adding anisinstance(port_arg, Int)branch elides the push/pop in that case.Almost every kernel
kernel_outb/kernel_outwcall site uses a hex-literal port (0x21,0x3F6,0x300, ...), so this fires kernel-wide.Impact
Kernel kasm: -1,980 bytes (on top of the -217 from PR #446). Folds fire in ata, fdc, ne2k, ps2, sb16, opl3, console, vga, serial, rtc.
Test plan
test_kernel_outb_constant_port_runtime_value_no_push_pop,test_kernel_outw_constant_port_runtime_value_no_push_poptest_kernel_outb_variable_value_uses_push_pop(with a non-Int port) still exercises the general save-around-eval path🤖 Generated with Claude Code