Skip to content

tools/mpy_ld.py: Optimise entry trampoline for x86/x64.#19309

Open
agatti wants to merge 1 commit into
micropython:masterfrom
agatti:natmod-x86-x86-optimised-trampoline
Open

tools/mpy_ld.py: Optimise entry trampoline for x86/x64.#19309
agatti wants to merge 1 commit into
micropython:masterfrom
agatti:natmod-x86-x86-optimised-trampoline

Conversation

@agatti
Copy link
Copy Markdown
Contributor

@agatti agatti commented Jun 4, 2026

Summary

This PR allows natmod entry trampolines to possibly be smaller depending on the offset between the mpy_init from the beginning of the text segment.

x64 and x86 shared the same, fixed-size entry point opcode that covered the whole 32-bits offset range. With changes introduced in 0fd0843 the entry point may change its length once all segments are laid out in the file, so optimised entry point code sequences can be emitted depending on the jump distance.

These changes generate optimised entry point jump opcodes for 8-, and 32- bit offsets. Depending on the size of the natmod and where the compiler decides to put the mpy_init symbol this can help shortening the output file by three bytes. Clang seems to prefer placing mpy_init at the beginning of the text segment on x86, whilst GCC sort of does it on x64. In both cases, a 2 bytes opcode is used instead of a 6 bytes one.

This is derived from the work being carried out in #19308, as this change won't really fit the parent PR's scope.

Testing

This should be tested by CI, as x86 and x64 natmod tests should be covered. In addition, the features0 natmod is small enough to trigger the generation of a 8-bit jump. Importing that module on either a x86 or x64 interpreter and running its factorial function works as expected.

Generative AI

I did not use generative AI tools when creating this PR.

This commit allows natmod entry trampolines to possibly be smaller
depending on the offset between the `mpy_init` from the beginning of the
text segment.

x64 and x86 shared the same, fixed-size entry point opcode that covered
the whole 32-bits offset range.  With changes introduced in
0fd0843 the entry point may change its
length once all segments are laid out in the file, so optimised entry
point code sequences can be emitted depending on the jump distance.

These changes generate optimised entry point jump opcodes for 8-, and
32- bit offsets.  Depending on the size of the natmod and where the
compiler decides to put the `mpy_init` symbol this can help shortening
the output file by three bytes.

Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
@agatti agatti force-pushed the natmod-x86-x86-optimised-trampoline branch from c5e1a3c to 723ffb1 Compare June 4, 2026 16:28
@agatti agatti added the tools Relates to tools/ directory in source, or other tooling label Jun 4, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.47%. Comparing base (75555f4) to head (723ffb1).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #19309   +/-   ##
=======================================
  Coverage   98.47%   98.47%           
=======================================
  Files         176      176           
  Lines       22845    22845           
=======================================
  Hits        22497    22497           
  Misses        348      348           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 4, 2026

Code size report:

Reference:  samd/mphalport: Run events at least once in mp_hal_delay_ms. [af38ee1]
Comparison: tools/mpy_ld.py: Optimise entry trampoline for x86/x64. [merge of 723ffb1]
  mpy-cross:    +0 +0.000% 
   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
      esp32:    +0 +0.000% ESP32_GENERIC
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:    +0 +0.000% VIRT_RV32

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tools Relates to tools/ directory in source, or other tooling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant