Skip to main content
MLE S.1
Associate II
June 1, 2020
Solved

Compiler optimisation issue (remove used functions)[solved]

  • June 1, 2020
  • 13 replies
  • 13194 views

Hi all,

On our card with a STM32L4 we use a siliconLabs BGM13P32 for the BLE.

The L4 communicate with the BLE SOC in NCP mode (see here for more details).

To do that i have integrated the SiliconLabs libs (BGLIB) to our project on stm32cubeIDE.

The project build fine, with no errors/warnings (default options) and i can communicate with the BLE SoC ...only if i use -O0 option in optimization options.

0693W000001pmqpQAA.png

If i use for example -Og, the functions used in BGLIB (siliconLabs) is removed from the binary (checked on .map file). The project build without errors but the com with the BLE SoC fails !

For information our project is in C++. Join with this post the class of our Bluetooth Com. (bluetoothCom.h/cpp).

The whole project is build with -Og optimisation option, just bluetoothCom.cpp use -O0 option to have a correct communication.

Do you have an idea how to resolve this ?

This topic has been closed for replies.
Best answer by MLE S.1

I have found the issue ...

On stm32 side i use DMA to transfert the data from uart to a circular buffer. The problem is that the SiliconLabs rxcallBack is implemented to read directly on uart register ...

I just modified my code like this :

int32_t BluetoothCom::BGAPI_Rx(uint32_t len1, uint8_t* data1) {
	uint32_t length = len1;
	while (length) {
		if(uart2.bytesAvailable())//needed to be sure DMA have transfert data from uart...
		{
			*data1 = uart2.readc();
			length --;
			data1 ++;
		}
	}
	return len1;
}

added -fno-inline option to file compilation option and now all work fine !

Thanks all for your answer, i have learn a lot with compilation options !

13 replies

Brian TIDAL
Technical Moderator
June 1, 2020

Hi,

you may try removing --gc-sections in the linker options.

Rgds

BT

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
MLE S.1
MLE S.1Author
Associate II
June 1, 2020

I suppose you talk about this option :

0693W000001pnDFQAY.png

MLE S.1
MLE S.1Author
Associate II
June 1, 2020

But this didn't working ...

Brian TIDAL
Technical Moderator
June 1, 2020

Hi,

I guess the functions are not removed from the binary but just inlined. Can you add the following option : -fno-inline and verify that the functions are present in the map file?

Rgds

BT

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
MLE S.1
MLE S.1Author
Associate II
June 1, 2020

I added -fno-inline here but the function is yet removed from the map file

0693W000001poIQQAY.png

waclawek.jan
Super User
June 1, 2020

> I guess the functions are not removed from the binary but just inlined.

+1

"Works only if optimizations off" means crappy code, usually missing volatile.

It appeaars that you have the source code, so debug it as usually - establish precisely what "does not work" means, develop theories why is it so, perform experiments, etc - it's all yours now.

JW

MLE S.1
MLE S.1Author
Associate II
June 1, 2020

Thanks for your help ... For your information for the moment i'm just use the code give by SiliconLabs with no modifications (appart for the C++) --> "means crappy code"

I make some test and i post a message here because i'm stuck and need help. If i'm writing a message here is not to hear --> RTFM ...

Tesla DeLorean
Guru
June 1, 2020

You're in a very small room, and practically no one is using the same combination of softwares, you'll need to root-cause what's going on.

You've got the .MAP file, you can generate a .LST or .LSS and see what actually got built.

Is the compiler folding the code, or is the linker jettisoning it?

With C++ got to watch the name mangling. The linker is going to throw out things which aren't explicitly bound.

Watch WEAK usages and link ordering. If the linker can find easy closure it will throw out stuff that takes more effort.

Watch for appropriate use of volatile and static.

Watch for over aggressive optimization as it walks back up the call-chain, seen Keil eat things where coders assume a function provides fencing to things which are inherently volatile.

Scope optimization with #pragma / attributes around code blocks to bisect where the issue actually is.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Pavel A.
Super User
June 1, 2020

Is the SiliconLabs library pre-built or you build it from source?

If the former, there can be some incompatibility between your code and their library (difference in esoteric code generation options).

Try to generate a binary file for your executable and compare sizes with -O0 vs. -Og

Try to step into the SiLabs library (in disassembly mode).

-- pa

MLE S.1
MLE S.1Author
Associate II
June 1, 2020

Thanks for your answers.

@Pavel A.​  i use source code (.c/.h) not a linked library.

@Community member​ , i suppose you talk about .list file generated by stm32cubeIDE ?

Under the result of this file for the method

void BluetoothCom::appHandleEvents(struct gecko_cmd_packet *evt)

With option -O0

0693W000001podsQAA.png

With option -Og

0693W000001poe7QAA.png

In debug mode (disassembly )

code working with -O0

0693W000001poelQAA.png

With -Og

0693W000001pof0QAA.png

All the siliconLabs function is declared as static inline0693W000001pofPQAQ.png

0693W000001pofoQAA.png

Tesla DeLorean
Guru
June 1, 2020

It has flipped the logic such that the case's body code are after the return()

The mvn.w r2,#95 is a byte load of 160, it is filling the structures directly.

The use of static functions allows the compiler much freer reign to fold the functions within the single object (source).

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
MLE S.1
MLE S.1Author
Associate II
June 1, 2020

0693W000001pok5QAA.pngIf it can help, the callGraph with -O0 option

We can see the SiliconLabs functions

with -Og option ?!

0693W000001pokAQAQ.png

waclawek.jan
Super User
June 1, 2020

> Code not executed with -Og

Did you verify that it should be executed, i.e. that evt != NULL and BGLIB_MSG_ID(evt->header)==gecko_evt_system_boot_id?

JW

Pavel A.
Super User
June 1, 2020

That's strange.

So what does -Og really mean: maximum optimization, that still allows debugging?

Why use -Og rather than -g -O0/O1?

-- pa

waclawek.jan
Super User
June 2, 2020

> yes i verify that in step mode after a breakpoint, the code in the switch is not executed

I did not ask if the code *was* executed, I asked, whether there *was a reason* for it to be executed:

>> Did you verify that it should be executed, i.e. that evt != NULL and BGLIB_MSG_ID(evt->header)==gecko_evt_system_boot_id?

Observe the content of registers respective to the variables in those test.

JW

MLE S.1
MLE S.1Author
Associate II
June 2, 2020

Sorry you are right, only the case gecko_evt_le_connection_closed_id is triggered with breakpoint in "C++" code.

But if put a breakpoint in Disassembly side it is executed :

0693W000001pqQMQAY.png