MicroPython ESP32 mpy-cross O flag

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
BobVeringa
Posts: 2
Joined: Wed Jul 14, 2021 10:13 pm

MicroPython ESP32 mpy-cross O flag

Post by BobVeringa » Wed Jul 14, 2021 10:55 pm

Hey everyone,
I am working on a project that is using MicroPython. The project uses frozen bytecode to create binaries that can be deployed to devices. Recently, I have been looking at using bytecode optimization that should be supported by mpy-cross https://docs.micropython.org/en/latest/ ... .opt_level. However, I have been unable to get this to work.

I have found this issue https://github.com/micropython/micropython/issues/5392 which provided some helpful information but did not help in finding a way to enable it.

I am using MicroPython on an ESP32, so when looking through the esp32 port I found the mpy-cross flags in the CMakeLists.txt
https://github.com/micropython/micropyt ... s.txt#L167

I changed

Code: Select all

# Define mpy-cross flags, for use with frozen code.
set(MICROPY_CROSS_FLAGS -march=xtensawin)
to

Code: Select all

# Define mpy-cross flags, for use with frozen code.
set(MICROPY_CROSS_FLAGS "-march=xtensawin -O3")
But that did not seem to do anything. Because when I call in my main.py (which is frozen).

Code: Select all

import micropython
print(micropython.opt_level())
It prints "0"

I validated that changing the flags actually does something by changing the "-O3" to "-h" and during the build process a help message is displayed.

My question is, where do I need to make the modification to be able to set the opt_level?

It feels like I am missing something obvious, but I have looked around for a while and haven't found anything. Any help or tips in what direction to look are much appreciated.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: MicroPython ESP32 mpy-cross O flag

Post by jimmo » Mon Jul 19, 2021 1:33 pm

BobVeringa wrote:
Wed Jul 14, 2021 10:55 pm
My question is, where do I need to make the modification to be able to set the opt_level?
The optimisation level printed by "micropython.opt_level()" is the current runtime optimisation level (i.e. what will applied when compiling new code).

In other words, even though the bytecode that is being executed in the statement "micropython.opt_level()" has been compiled with optimisations enabled, the value that it's querying is completely independent.

The optimisation level set for mpy-cross will be used when compiling that code to bytecode, but there's no way to query that at runtime. I guess one thing you could do is verify that the optimisations are being applied (e.g. assert statements are removed).

FWIW, the MicroPython compiler optimisations are mostly going to improve the size of your compiled output. If you want performance improvements then you're better off using @native (and @viper).

BobVeringa
Posts: 2
Joined: Wed Jul 14, 2021 10:13 pm

Re: MicroPython ESP32 mpy-cross O flag

Post by BobVeringa » Mon Jul 26, 2021 9:24 pm

Thanks, that does clear up some things.

For my use-case, the improvement of compiled output is also interesting. To increase performance, I mainly use the @native decorator combined with C modules. My interest in the optimization mainly stems from wanting to better understand the build process and what I can do to improve the builds.

A smaller build size really helps with OTA, as it means I need to send fewer bytes over the air.

Validating it with assert statements is indeed a good idea. I was trying other things, but those didn't really seem to work. Thanks for the tip, I'll give it a shot.

Post Reply