Forum Discussion
NIOS II - FW doesn't work with compiler optimization turned on
- Why this behavior?
- Is it possible to use memcpy as a write function on Flash?
- Is it possible to turn off optimization for a block of code?
- Quartus Prima Version 20.1.1
- Nios II/f processor
- Eclipse IDE (version: Mars.2 Release (4.5.2) - build id: 20160218-0600)
- Toolchain used:
The NIOS data cache is disabled, whereas the instruction cache is enabled (see below). I use only the IORD and IOWR functions to perform registers and peripherals accesses.
Best regards.
naand
15 Replies
- wwanalim_intel
Contributor
Hi,
Greetings and welcome to Intel's forum.
Please give me some time to check on this issue and will get back to you with the update.
Thank you.
Regards,
Fathulnaim
- wwanalim_intel
Contributor
Hi,
I am checking this issue with internal teams.
In the meantime, can you provide the project file as I want to try and monitor the detail of the issue on my side.
- naand
New Contributor
Hi,I cannot share the project files. This is a complete project with corporate know-how, and I cannot share it entirely (I could share only small portions).Also, the project is not based on a dev board, but on custom hardware, so I don't know how the code can be tried.Thank you.Regards,naand
- wwanalim_intel
Contributor
Hi,
Since project cannot be shared, then we will suggest ways for you to debug the issue.
We recommend for you to enable a single optimization until the design fails. Once you can find the optimization that cause the issue then can remove it and proceed without using it for your design.
Since -O3 is using bunch of optimizations, we should try one by one:
-O0 (working or not)
-O1 (work with which optimization)
-O2 (work with O1 and plus which optimization)
-O3 (failing because of which optimization)
Based on the closer working/failing pair, we will have a smaller group to investigate.
For example,
If the pair is found to be O2-O3, these are the interested optimization to investigate
https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
Thank you.
Regards,
Fathulnaim
- naand
New Contributor
Hi,
I tried as you suggested.
With optimization O1 the firmware works properly, whereas the firmware doesn't work with optimization O2, so the pair is O1-O2.
To investigate further, is it possible to disable only one optimization flag of O2 and find out which flag generates the issue?
And is it possible to turn off optimization for a block of code?
Thank you.
Regards,
naand
- wwanalim_intel
Contributor
Hi,
As the firmware started not working when enable optimization O2, you can proceed to investigate those pairs. Yes, you will need to disable one by one the optimization flag to find out which flag generates the issue.
For the turning off optimization for a block of code, I will need to get the information from the internal team first then will inform you on the feedback.
- naand
New Contributor
Hi,
Thanks for the feedback.
You said I can disable the optimization flags one by one, but I don't understand how I can disable only one. I read the link you shared with me but there is nothing about it.
For example, with the -O2 optimization, if I want to disable only the -finline-functions flag and enable all the others, how can I do that?
Thank you.
Regards,
naand
- naand
New Contributor
Hi,
I have news about what optimization flag is responsible for the problem; the flag is the -finline-small-functions.
Another test I did was to set the variable flag_status, used to check the Flash status, as volatile, but nothing changed (the flag_status is found in the flash_write_function.c file attached in the first message).
Are there any updates on how to disable optimization for a specific block of code?
Thanks.
Best Regards,
naand
- wwanalim_intel
Contributor
Hi,
Sorry for not updating as I just got back from long holiday.
Will check with engineering team on the step if the optimization can be disable for a specific block of code.
Thank you.
- wwanalim_intel
Contributor
Hi Sir,
So far there is no official document or guide from us on how to disable the optimization for a specific block of code.
However, I found this which you can try.
https://programfan.github.io/blog/2015/04/27/prevent-gcc-optimize-away-code/
Thank you.
- wwanalim_intel
Contributor
Hi Sir,
Any update from your side?
- naand
New Contributor
Hi,
I am sorry for the delayed response. I have been out of the office for the last few days.
Thanks for the link, I tried the #pragma directive to disable GCC optimization for one or more functions and it works.
With this technique I was able to isolate the source of the problem: the function that causes it is flash_read_flag_status_register.
It is implemented in this way:
alt_u32 flash_read_flag_status_register(T_flash_state *st){IOWR(st->csr_base, FLASH_CSR_FL_CMD_SET, 0x00001870);IOWR(st->csr_base, FLASH_CSR_FL_CMD_CTRL, 0x1);return IORD(st->csr_base, FLASH_CSR_FL_CMD_RD0);}and is used to check if the write operation is finished and then checks for errors:
// check for errors
do {flag_status = flash_read_flag_status_register(st);
} while (!(flag_status & FLASH_FSR_READY));
flash_clear_flag_status_register(st);
if (flag_status & FLASH_FSR_PROTECTION_ERR)
{ret_flag = FLASH_SECTOR_PROTECTED;
break;}
if ((flag_status & FLASH_FSR_ERASE_ERR) || (flag_status & FLASH_FSR_PROGRAM_ERR))
{ret_flag = FLASH_ERROR;
break;}
To avoid using #pragma I tried setting the flag_status variable as volatile, but it doesn't work.
Do you know why if I use volatile for flag_status it doesn't work?
Can you suggest other attentions I can have to avoid other problems with GCC optimization?
Thanks.
Best Regards,
naand