Thanks to visit codestin.com
Credit goes to github.com

Skip to content

py/emitinlinerv32: Reduce footprint and fix build on ESP32. #16524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 28, 2025

Conversation

agatti
Copy link
Contributor

@agatti agatti commented Jan 4, 2025

Summary

This PR fixes a warning occurring on the compiler version shipped with ESP-IDF v5.2.0 and later, and incorporates the suggestions made in #15714 to reduce the overall footprint.

The size reduction for QEMU/VIRT_RV32 is 975 bytes, on ESP32 should probably be close to that.

This PR can wait for v1.25.0 to get out first, there's no improvement in functionality or any critical bugfixes compared to #15714 (ESP32 has this feature turned off by default anyway).

Testing

The inlineasm tests passed successfully on both QEMU and an ESP32C3 using ESP-IDF v5.2.0.

Copy link

codecov bot commented Jan 4, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.53%. Comparing base (14ba32b) to head (8633abc).
Report is 2 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #16524   +/-   ##
=======================================
  Coverage   98.53%   98.53%           
=======================================
  Files         169      169           
  Lines       21852    21852           
=======================================
  Hits        21532    21532           
  Misses        320      320           

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

Copy link

github-actions bot commented Jan 4, 2025

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:  -975 -0.215% VIRT_RV32

@projectgus projectgus self-requested a review January 6, 2025 23:50
@dpgeorge dpgeorge added the py-core Relates to py/ directory in source label Jan 23, 2025
Copy link
Contributor

@projectgus projectgus left a comment

Choose a reason for hiding this comment

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

This change looks good to me, but tbh I'm not expert enough on either the native emitters or RISC-V instruction encodings to understand it deeply. 😁

I'm going to mark this against the 1.26 release so it doesn't get forgotten about, though.

{ MP_QSTR_sw, asm_rv32_opcode_sw, CALL_RIR, R, 0, I, 0, R, 0, 0xFFFFFFFF, 0x00000FFF, 0xFFFFFFFF },
{ MP_QSTR_xor, asm_rv32_opcode_xor, CALL_RRR, R, 0, R, 0, R, 0, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF },
{ MP_QSTR_xori, asm_rv32_opcode_xori, CALL_RRI, R, 0, R, 0, I, 0, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000FFF },
{ MP_QSTR_add, 0x00, 0x00, 0x00, 3, CALL_RRR, R, 0, R, 0, R, 0, asm_rv32_opcode_add },
Copy link
Member

Choose a reason for hiding this comment

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

These mask indices are quite cryptic, it's not obvious they index OPCODE_MASKS.

I suggest making an enum for them, eg:

enum {
    MASK_FFFFFFFF,
    MASK_00000FFF,
    MASK_FFFFF000,
    ...
};

then define OPCODE_MASKS to ensure they indices match:

static const uint32_t OPCODE_MASKS[] = {
    [MASK_FFFFFFFF] = 0xFFFFFFFF,
    [MASK_00000FFF] = 0x00000FFF,
    [MASK_FFFFF000] = 0xFFFFF000,
    ...
};

and then use these indices in this table here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, I've modified the code as you suggested. Too bad now the table doesn't look as neat, since NONE was the same length of a raw mask index and I couldn't come up with an 8 characters long equivalent :)

Copy link
Member

Choose a reason for hiding this comment

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

You could use MASK_NOT_USED, or just MASK_________.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I used MASK_NOT_USED. Wonder why I didn't think of that myself... :)

This commit fixes a compilation warning (turned error) about a
potentially uninitialised variable being used.  The warning can be
ignored as the variable in question is always written to, but the code
has been changed to silence that warning.

Signed-off-by: Alessandro Gatti <[email protected]>
@agatti agatti force-pushed the inlineasm-rv32-size branch from ca2929c to 80f85d8 Compare February 27, 2025 11:31
This commit introduces a few changes aimed at reducing the amount of
space taken by the inline assembler once compiled:

* The register string table uses 2 bytes for each qstr rather than the
  usual 4
* The opcode table uses 2 bytes for each qstr rather than the usual 4
* Opcode masks are not embedded in each opcode entry but looked up via
  an additional smaller table, reducing the number of bytes taken by
  an opcode's masks from 12 to 2 (with a fixed overhead of 24 bytes for
  the the masks themselves stored elsewhere)
* Some error messages had a trailing period, now removed
* Error messages have been parameterised when possible, and the overall
  text length is smaller.

Signed-off-by: Alessandro Gatti <[email protected]>
@agatti agatti force-pushed the inlineasm-rv32-size branch from 80f85d8 to 8633abc Compare February 27, 2025 17:43
@dpgeorge dpgeorge removed this from the release-1.26.0 milestone Feb 28, 2025
@dpgeorge dpgeorge merged commit 8633abc into micropython:master Feb 28, 2025
63 checks passed
@agatti agatti deleted the inlineasm-rv32-size branch March 5, 2025 01:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
py-core Relates to py/ directory in source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants