SpenceKonde / megaTinyCore

Arduino core for the tinyAVR 0/1/2-series - Ones's digit 2,4,5,7 (pincount, 8,14,20,24), tens digit 0, 1, or 2 (featureset), preceded by flash in kb. Library maintainers: porting help available!
Other
554 stars 144 forks source link

ATTiny 1616, MTC 2.6.5, Optimization problem? #916

Closed SpenceKonde closed 1 year ago

SpenceKonde commented 1 year ago

Discussed in https://github.com/SpenceKonde/megaTinyCore/discussions/915

Originally posted by **unhinged-engineer** March 1, 2023 Please be kind, this is my first ever post on github, so I really have no idea what I'm doing here. I'm trying to write code for a Tiny 1616, and just getting started. The project is going to be processing speed critical, so I'm trying to use -O3, instead of -OS for the optimization. When I use -OS, it compiles fine, but when I use -O3, I get the following error: C:\Users\jrector\AppData\Local\Temp\ccIg8Jos.s: Assembler messages: C:\Users\jrector\AppData\Local\Temp\ccIg8Jos.s:454: Error: symbol `_poll_dre_done' is already defined C:\Users\jrector\AppData\Local\Temp\ccIg8Jos.s:728: Error: symbol `_poll_dre_done' is already defined lto-wrapper.exe: fatal error: C:\Users\jrector\AppData\Local\Arduino15\packages\DxCore\tools\avr-gcc\7.3.0-atmel3.6.1-azduino6/bin/avr-gcc returned 1 exit status compilation terminated. c:/users/jrector/appdata/local/arduino15/packages/dxcore/tools/avr-gcc/7.3.0-atmel3.6.1-azduino6/bin/../lib/gcc/avr/7.3.0/../../../../avr/bin/ld.exe: error: lto-wrapper failed collect2.exe: error: ld returned 1 exit status exit status 1 Compilation error: exit status 1 The code that I'm trying to compile is VERY simple, for now, because I'm just getting started. `void setup() { //SPI.swap(1); Serial.swap(1); Serial.begin(512000, SERIAL_8N1 | SERIAL_HALF_DUPLEX | SERIAL_TX_ONLY); } void loop() { Serial.print("hello world"); delay(1000); }` for some reason my code formatting isn't working correctly, because of the error? I think you can see what I'm doing, regardless. Upshot: Is there some setting or command that I'm doing wrong, to make the error happen, but only when -O3 optimized?
SpenceKonde commented 1 year ago

I belive this is caused by the asm sections being inlined instead of kept separate, That causes there to be two copies of the same label in the generated assembly. That is illegal, and so the assembler cannot continue.

This bug impacts both DxC and mTC, and needs to be fixed ASAP.

There are likely a large number of other places where this can crop up as well. I will need to read up on the extensive (hah! One single sparsely worded webpage is all we fucking get, last updated when dinosaurs roamed the earth) to try to figure out how to solve this, but I need to get new versions of both cores out ASAP now to fix this issue and the fact that I broke analogWrite on the majority of pins on DxC in the last release

SpenceKonde commented 1 year ago

Ugh.I don't really understand what the compiler is turning it into, but I can't readily get human processable semi-compiled code out because of LTO. The USART code is full of painstakingly hand optimized naked ASM that executes within interrupt context - I can't tell why it's duplicating that label, but the fact that it is almost inescapably means it's generating less-optimal code than -Os does in that part of the program.

I can give no ETA on fixing it, nd generally I don't feel that it would be worth the amount of time it would take to fix because I have to manually flip a whole bunch of switches, putting the core into a state where none of the output it generates is any good - at which point the issue might well no longer manifest at all. This has time sink written all over it and I don't have time to sink. Use -Os. It usually doesn't generate code markedly slower. The optimizer on avr-gcc isn't very good at it's job, quite frankly, and every later version of avr-gcc has been getting worse or not changing.

If I didn't actually want to have the optimization menu there for my own testing purposes, I would totally just pull the optimization level menu out. It's worth the half hour it took to put in. It's not work several days of debugging effort.

SpenceKonde commented 1 year ago

Yeah I think I'm just gonna pull any options that cause compile failures out of the menu, and that might mean the whole menu goes away, until I learn how to fix this