Skip to content

Optimize for size #32296

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

Closed
engstad opened this issue Mar 16, 2016 · 8 comments
Closed

Optimize for size #32296

engstad opened this issue Mar 16, 2016 · 8 comments
Labels
A-frontend Area: Compiler frontend (errors, parsing and HIR) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@engstad
Copy link

engstad commented Mar 16, 2016

It seems that the compiler doesn't understand -Os (optimize for size):

$ rustc -C opt-level=s -Z no-landing-pads --target thumbv7em-none-eabi -g --emit obj -L libcore-thumbv7em -o main.o ./blink/src/main.rs
error: incorrect value `s` for codegen option `opt-level` - a number was expected
@steveklabnik steveklabnik added A-frontend Area: Compiler frontend (errors, parsing and HIR) A-compiler labels Mar 16, 2016
@steveklabnik
Copy link
Member

As far as I know, yes, this isn't a thing.

@engstad
Copy link
Author

engstad commented Mar 17, 2016

I believe both GCC and Clang support the -Os option, and this is normally an embedded application's default. Support for this in Rust would follow that tradition.

@nodakai
Copy link
Contributor

nodakai commented Mar 17, 2016

Give this a try:

rustc -C opt-level=3 -C lto -C link-args=-s foo.rs

I guess static linking of libstd is "to blame" for the output size rather than the optimization level.

By the way, Rust FAQ must have an item on the issue of "huge" binary size.

@engstad
Copy link
Author

engstad commented Mar 17, 2016

Embedded systems don't usually include libstd, only libcore. However, for embedded systems (and IoT), size is often more important than speed.

@engstad engstad closed this as completed Mar 17, 2016
@engstad engstad reopened this Mar 17, 2016
@MagaTailor
Copy link

@engstad, the idea of -Os can be safely decoupled from anything embedded or binary size in general - it used to be a rule that hot C++ code compiled for size ran faster (better fit in cache) than -O3.

So yeah, that would be a welcome addition with benchmarking in mind.

@nodakai
Copy link
Contributor

nodakai commented Mar 18, 2016

@engstad Are you working on an embedded system already without libstd, and the output binary size is still that important? Well, I tend to agree that smaller binaries are faster to load...

FYI, when I compiled GNU Make with Clang 4.8 x86_64 -Ofast and -Os, the stripped binary sizes were 192 KB vs 175 KB, 10 % difference.

brandonedens added a commit to brandonedens/rust that referenced this issue Mar 22, 2016
bors added a commit that referenced this issue May 2, 2016
Add CodeGen options to optimize for size.

Add CodeGen options to annotate functions with the attributes OptimizeSize and/or MinSize used by LLVM to reduce .text size.
Closes #32296
@polarathene
Copy link

@engstad @nodakai I've been looking for up to date information in regards to using Rust with embedded systems such as the Cortex M range of MCU that do not run Linux OS due to lack of resources. Is there any reference/information you can refer me to? I'm aware of Zinc.rs but that is not quite ready yet from what I understand. Are you able to interact with Arduino or mbed libraries?

Are you using Rust freestanding/bare metal or with an RTOS like FreeRTOS/ChibiOS/Zephyr? I've read that for MCU to have Rust working we must avoid using the std lib, @nodakai gives me the impression that higher end MCU do not have to avoid this?

@steveklabnik steveklabnik added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-frontend Area: Compiler frontend (errors, parsing and HIR) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants