Optimization bug on function and lambda return
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
GNU Arm Embedded Toolchain |
New
|
Undecided
|
Unassigned |
Bug Description
When calling a function or a lambda to create function arguments, the stack is not reused properly.
See question (no discussion here):
https:/
Discussion about this bug can be found here :
https:/
The following code can reproduce the problem :
-------
#include <stdint.h>
#include <stdio.h>
struct TestStruct
{
uint32_t field1;
uint32_t field2;
uint32_t field3;
uint32_t field4;
uint32_t field11;
uint32_t field21;
uint32_t field31;
uint32_t field41;
} ;
struct TestStruct initStructure(
{
struct TestStruct myStruct;
return myStruct;
}
#define MACROLAMBDA(
[&]() -> struct TestStruct { struct TestStruct ${}; \
$.field1 = f1; \
$.field2 = f2; \
$.field3 = f3; \
$.field4 = f4; \
$.field11 = f5; \
$.field21 = f6; \
$.field31 = f7; \
$.field41 = f8; \
return $; \
}()
void __attribute_
{
printf("f1 = %d, f2 = %d, f3 = %d, f4 = %d, f1 = %d, f2 = %d, f3 = %d, f4 = %d", myStruct.field1, myStruct.field2, myStruct.field3, myStruct.
}
void __attribute_
{
}
void __attribute_
{
}
void __attribute_
{
}
void __attribute_
{
}
int main(void)
{
return 0;
}
-------
Consider doStuff as non inlinable.
With -O2 or -O3 wrapper1LAMBDA uses 52 bytes of stack whereas wrapper2LAMBDA uses 80. The same happens for wrapper1func and wrapper2func.
It seems that the compiler does not reused the stack slot but it should in this case.
The bug was encountered with :
- GCC: (GNU Tools for Arm Embedded Processors 8-2019-q3-update) 8.3.1 20190703 (release) [gcc-8-branch revision 273027]
- Toolchain binary downloaded at https:/
- Host machine is a Windows 10 64bits
- Target System is a STM32L4
- Preprocessed test cases are attached with assembly generated for comparison
- Bug happens when compiling in C++ (and in C without lambdas)