On 04/02/2026 07:13, Søren Rune Nissen via Gcc-bugs wrote:
> Dear gcc
> 
> gcc 15.2 at -O2
> 
> Accumulating iota [1,10] in a vector
> 
> https://godbolt.org/z/Pz7GTqP1e
> 
> The optimizer somehow has a case where it doesn't see the allocation can be 
> skipped, even though it successfully pre-calculates the sum without the 
> allocation.
> 
> There's no allocation when
> 
>  * the sum is returned from main
>  * the sum is returned from not_main
>  * the sum is printed from not_main
> 
> But if you write the code in main, and you print the sum instead of returning 
> it, there's an allocation.
> 
> Best regards
> Søren Rune Nissen
> 

Please report bugs in our tracker: https://gcc.gnu.org/bugzilla.

I'm afraid we can't track issues posted on this mailing list, which is only 
used for bugzilla generated traffic these days.

Thanks,
R.

> 
> 
> ////////////////////////////////////////////////////////////////////////////////
> int main(){
> std::vector<int>test(10, 0);
> std::iota(test.begin(), test.end(), 1);
> int sum = std::accumulate(test.begin(), test.end(), 0);
> return sum;
> }
> main:
> mov eax, 55
> ret
> ////////////////////////////////////////////////////////////////////////////////
> void not_main(){
> std::vector<int>test(10, 0);
> std::iota(test.begin(), test.end(), 1);
> int sum = std::accumulate(test.begin(), test.end(), 0);
> printf("Sum is %d\n", sum);
> }
> .LC0:
> .string "Sum is %d\n"
> not_main():
> sub rsp, 8
> mov esi, 55
> mov edi, OFFSET FLAT:.LC0
> xor eax, eax
> call printf
> add rsp, 8
> ////////////////////////////////////////////////////////////////////////////////
> int main(){
> std::vector<int>test(10, 0);
> std::iota(test.begin(), test.end(), 1);
> int sum = std::accumulate(test.begin(), test.end(), 0);
> printf("Sum is %d\n", sum);
> }
> .LC3:
> .string "Sum is %d\n"
> main:
> push rbx
> mov edi, 40
> sub rsp, 32
> call operator new(unsigned long)
> mov esi, 55
> mov edi, OFFSET FLAT:.LC3
> movdqa xmm0, XMMWORD PTR .LC0[rip]
> lea rdx, [rax+40]
> mov QWORD PTR [rsp], rax
> mov QWORD PTR [rsp+16], rdx
> mov QWORD PTR [rsp+8], rdx
> mov rdx, QWORD PTR .LC2[rip]
> movups XMMWORD PTR [rax], xmm0
> movdqa xmm0, XMMWORD PTR .LC1[rip]
> mov QWORD PTR [rax+32], rdx
> movups XMMWORD PTR [rax+16], xmm0
> xor eax, eax
> call printf
> mov rdi, rsp
> call std::_Vector_base<int,std::allocator<int>>::~_Vector_base() [base object 
> destructor]
> add rsp, 32
> xor eax, eax
> pop rbx
> ret
> mov rbx, rax
> jmp .L5

Reply via email to