Skip to content

Conversation

@MikeWang000000
Copy link
Contributor

@MikeWang000000 MikeWang000000 commented Oct 2, 2025

Summary

This change checks __BIGGEST_ALIGNMENT__ and produces a compile-time error if the maximum alignment is less than 4 bytes. On m68k platforms, an additional message advises using -malign-int to enable 4-byte alignment.

This ensures correct operation of object tagging schemes that require at least 4-byte alignment.

Add a compile-time static assertion to ensure uPy objects are aligned to at least a 4-byte boundary.

Change the MICROPY_OBJ_BASE_ALIGNMENT macro to a numeric value, making it easier to define via CFLAGS in Makefiles.

Reference: https://wiki.debian.org/M68k/Alignment

On m68k, the default alignment has been traditionally 2 bytes instead of 4 bytes. This originates in the design of the original 68000 CPU which used an external 16-bit data bus such that data words with a length of 1 or 2 bytes could be handled by the data bus in one clock cycle instead of two. Starting with the 68020 CPU, the external data bus was extended to 32 bits and the previously used 2 bytes default alignment was no longer necessary to optimize performance of the data bus.

Related issue: #18183

Testing

Tested on Linux, m68k-linux-gnu-gcc (Debian 14.2.0-19) 14.2.0

Successful build, REPL launched without errors.

@github-actions
Copy link

github-actions bot commented Oct 2, 2025

Code size report:

  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
     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

@codecov
Copy link

codecov bot commented Oct 2, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.39%. Comparing base (35d07df) to head (08219b5).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #18194   +/-   ##
=======================================
  Coverage   98.39%   98.39%           
=======================================
  Files         171      171           
  Lines       22276    22276           
=======================================
  Hits        21918    21918           
  Misses        358      358           

☔ View full report in Codecov by Sentry.
📢 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.

@dpgeorge dpgeorge added the py-core Relates to py/ directory in source label Oct 3, 2025
@dpgeorge
Copy link
Member

dpgeorge commented Oct 3, 2025

Thanks for the contribution.

This check seems quite specific to m68k. Can it be done in a more general way?

@MikeWang000000
Copy link
Contributor Author

Yes, -malign-int is a specific GCC option for m68k targets only. I updated the code to use MICROPY_OBJ_BASE_ALIGNMENT macro in a more general way.


Updated changes:

Add a compile-time static assertion to ensure uPy objects are aligned to at least a 4-byte boundary.

Change the MICROPY_OBJ_BASE_ALIGNMENT macro to a numeric value, making it easier to define via CFLAGS in Makefiles.

Add a compile-time static assertion to ensure uPy objects are aligned
to at least a 4-byte boundary.

Change the MICROPY_OBJ_BASE_ALIGNMENT macro to a numeric value,
making it easier to define via CFLAGS in Makefiles.

Signed-off-by: Mike Wang <[email protected]>
@MikeWang000000
Copy link
Contributor Author

I didn't expect that all MSVC builds failed due to the new added static assert.
Interesting...

@dpgeorge
Copy link
Member

dpgeorge commented Oct 3, 2025

I didn't expect that all MSVC builds failed due to the new added static assert.

That might be fixed by #18145

@AJMansfield
Copy link
Contributor

AJMansfield commented Oct 3, 2025

I didn't expect that all MSVC builds failed due to the new added static assert. Interesting...

I'm not surprised --- there's actually like a dozen cases you need to cover if you want anything to do with alignment in C to be totally portable across all the compilers MicroPython targets.

I have a PR that adds proper portable macros for alignment, though, that I proposed a few days ago:

Changing MICROPY_OBJ_BASE_ALIGNMENT to just be a number rather than an attribute sounds like a good idea to me, though --- I'd suggest rebasing downstream of that and changing the declaration in obj.h to use the MP_ALIGNAS macro it defines.

@MikeWang000000 MikeWang000000 marked this pull request as draft October 3, 2025 19:48
@MikeWang000000
Copy link
Contributor Author

I'd suggest rebasing downstream of that and changing the declaration in obj.h to use the MP_ALIGNAS macro it defines.

Sure! I will update the code when PR #18145 and your PR #18148 get merged. Thanks for pointing this out!

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