-
Notifications
You must be signed in to change notification settings - Fork 3k
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
The code is optimized when use operator= for DigitalOut #863
Comments
LIne of code:
is just like:
so probably changing code to:
will not help. But actually have you tried? I think it might be GCC problem :/ |
@leibin2014 What device are you compiling for? Typically those operators get inlined all the way down to a device specific implementation so the issue might be device specific. Did you look at the disassembly to verify that there are missing STR instructions or are you just seeing external behavior that would indicate those statements were optimized out? It might be helpful if you had a build-able project that you could point us to so that we could try reproducing the problem. I have recently been playing with similar code to blink LEDs on a few different devices with the 4.9.3 compiler and I haven't hit this issue. |
@leibin2014 as above, please share assembly output, for both scenarios described in your first post. As Adam proposed, the simple code snippet, which would reproduce hte problem would be helpful, a main code file with using DigitalOut, and the behavior you described. |
@PrzemekWirkus I think if change to cs.write(0) it should work, but I want to keep use cs = 0. Since cs = 0 is a normal use in mbed, I have a lot of code with this format. I wouldn't like to change them all to cs.write(0). |
@adamgreen and @0xc0170 I found this problem just happens in some functions. I also have a lot of code in the same project using the format cs = 0, it works. Even in the same file, some functions have this problem, the others don't. I run this code on nrf51822.
|
@adamgreen and @0xc0170 I have reproduced this problem with below simple code:
Assembly code for function setup():
|
Here is the CFLAGS: arm-none-eabi-g++.exe -g -O2 -Wall -Wextra -pipe -x c "-std=gnu11" -fno-hosted -Wno-old-style-declaration "-mtune=cortex-m0" "-mcpu=cortex-m0" -msoft-float -gdwarf-2 -Wall -Wextra -Wno-missing-field-initializers -Wno-unused-parameter -Wno-sign-compare -Wno-comment -Wno-switch -fno-delete-null-pointer-checks -fno-strict-aliasing -ffunction-sections "-fmessage-length=0" -fdata-sections -fsigned-char -fno-builtin -ffast-math -mno-sched-prolog -mthumb -nostdlib -MMD -DTOOLCHAIN_GCC_ARM -DTOOLCHAIN_GCC "-D__MBED__=1" -DNDEBUG -D__CORTEX_M0 -DARM_MATH_CM0 |
I built your sample code with the following g++ command line:
That produced the expected assembly code for main where setup was actually inlined:
Where did you get your compiler flags? You should look for support from whomever gave you those options to use for the NRF51822 device with GCC. They aren't the recommended ones. The flags I used above were from a project exported from the mbed online compiler for GCC_ARM so they are the recommended ones. I did change the -Os optimization flag to -O2. |
@adamgreen
|
If you look closely at the disassembly of my main(), you will see that there is no call to setup as it was inlined.
First, they aren't my flags, they are the ones you get if you export a project for GCC_ARM. The only thing that happens if I make your recommended flag changes are that it inlines some assert code from the operator= as well.
Why do you care about flags like "-mno-sched-prolog" anyway? I could see using that if your version of GDB got confused by an optimized prolog but I haven't seen that happen in my use of GCC_ARM's build of GDB. It has code to manually walk the prolog and figure out where things currently are based on how far you have progressed into a function. Have you tried the recommended flags? Does the problem happen with those? |
@adamgreen Thanks for your reply and sorry for making you confused! I tried below CFLAGS, CXXFLAGS, and LDFLAGS, but can't solve my original problem. ############################################################################################################# ############################################################################################################# ############################################################################################################# |
This is a makefile exported from the mbed online compiler for a simple NRF51822 blinky sample:
|
@adamgreen
|
Looks like the code also can be simplified as below:
Here is the disassembly code:
|
@adamgreen @0xc0170 |
Looks like it's not due to GPIO setting is optimized. My original problem is caused by the code running too fast after using -O2. |
Nope but when I look at your disassembly it looks to obviously be incomplete since there are gaps in the address ranges. What follows is the disassembly from when I build your sample with the exported makefile switched to use -O2.
Command used to create this disassembly:
That makes sense. |
Yes, Adamgreen, my code works fine after adding some wait_us(1) in the code. Thanks again for all your help! |
According to the handbook I use a lot of operator= for setting chipselect, reset pins. The code works if I set optimization flag to -O0 or -O1, but sometimes can't work if set to -O2. Take below code as an example:
The code cs = 0; and dc = 1 in function configureRectangle is optimized if using -O2 as compiler flag.
Is there a way to avoid this problem if I want to use -O2 flag and also the operator= for DigitalOut?
My compiler is arm-none-eabi-gcc version 4.9.3.
The text was updated successfully, but these errors were encountered: