https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128218
Bug ID: 128218
Summary: transfer intrinsic interferes with optimizations
Product: gcc
Version: 16.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: fortran
Assignee: unassigned at gcc dot gnu.org
Reporter: henri at henrimenke dot de
Target Milestone: ---
The code below is not a particularly good use of the transfer intrinsic, but I
believe that it should work nevertheless. When I compile the example with
optimization and vectorization enabled, I expect the compiler to vectorize the
assignments, but instead the generated code has almost no resemblance of the
code as written and also contains tons of redundant statements.
$ gfortran --version
GNU Fortran (GCC) 16.2.1 20260819 (Red Hat 16.2.1-2)
Copyright (C) 2026 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ cat test.f90
subroutine g(a, e, z1, z2)
real(8), intent(in) :: a(0:*)
integer(8), value :: e
complex(8), intent(out) :: z1, z2
z1 = transfer(a(e:e+1), z1) ! cmplx(a(e), a(e+1), 8)
z2 = transfer(a(e+2:e+3), z2) ! cmplx(a(e+2), a(e+3), 8)
end subroutine
$ gfortran -O3 -mavx2 -c test.f90
$ objdump --disassemble --disassembler-options=intel test.o
test.o: file format elf64-x86-64
Disassembly of section .text:
0000000000000000 <g_>:
0: 55 push rbp
1: b8 10 00 00 00 mov eax,0x10
6: 48 89 e5 mov rbp,rsp
9: c5 fb 10 0c f7 vmovsd xmm1,QWORD PTR [rdi+rsi*8]
e: c5 f1 16 44 f7 08 vmovhpd xmm0,xmm1,QWORD PTR [rdi+rsi*8+0x8]
14: c5 f9 11 02 vmovupd XMMWORD PTR [rdx],xmm0
18: 48 8d 54 f7 10 lea rdx,[rdi+rsi*8+0x10]
1d: 48 85 c0 test rax,rax
20: 75 2e jne 50 <g_+0x50>
22: 85 c0 test eax,eax
24: 75 12 jne 38 <g_+0x38>
26: c5 f9 28 44 24 f0 vmovapd xmm0,XMMWORD PTR [rsp-0x10]
2c: c5 f9 11 01 vmovupd XMMWORD PTR [rcx],xmm0
30: 5d pop rbp
31: c3 ret
32: 66 0f 1f 44 00 00 nop WORD PTR [rax+rax*1+0x0]
38: 0f b6 02 movzx eax,BYTE PTR [rdx]
3b: 88 44 24 f0 mov BYTE PTR [rsp-0x10],al
3f: c5 f9 28 44 24 f0 vmovapd xmm0,XMMWORD PTR [rsp-0x10]
45: c5 f9 11 01 vmovupd XMMWORD PTR [rcx],xmm0
49: 5d pop rbp
4a: c3 ret
4b: 0f 1f 44 00 00 nop DWORD PTR [rax+rax*1+0x0]
50: c5 fa 6f 02 vmovdqu xmm0,XMMWORD PTR [rdx]
54: 89 c0 mov eax,eax
56: c5 fa 7f 44 24 f0 vmovdqu XMMWORD PTR [rsp-0x10],xmm0
5c: c5 fa 6f 44 02 f0 vmovdqu xmm0,XMMWORD PTR [rdx+rax*1-0x10]
62: c5 fa 7f 44 04 e0 vmovdqu XMMWORD PTR [rsp+rax*1-0x20],xmm0
68: eb bc jmp 26 <g_+0x26>