llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[AArch64] Extra unconditional branch instruction being generated

Open ceseo opened this issue 2 years ago • 7 comments

Following up the discussion on the AArch64 Sync call (2024-01-09):

While debugging OpenMP (ompt/synchronization/master.c), I noticed this:

 cec:   97ffff19        bl      950 <__kmpc_end_master@plt>
 cf0:   14000001        b       cf4 <main.omp_outlined+0xd4>
 cf4:   d503201f        nop
 cf8:   14000001        b       cfc <main.omp_outlined+0xdc>
 cfc:   d0000088        adrp    x8, 12000 <__gmon_start__@Base>
 d00:   f9404d08        ldr     x8, [x8, #152]

I'd expect the nop to be followed by the adrp. Instead, there is an unconditional branch to the adrp.

I've seen this pattern in other parts of the code as well, but this is the example I had at hand. I can try to create a smaller reproducer, if necessary.

ceseo avatar Jan 09 '24 18:01 ceseo

Thanks for the reproducer. I've not looked in great detail, but it may be something to do with the inline assembly blocks in the code. I was compiling the master.c file manually - I had to cobble it together to make it work and so might have got it wrong, but didn't see the same branches. Do you know which compiler options it uses?

davemgreen avatar Jan 09 '24 18:01 davemgreen

@llvm/issue-subscribers-backend-aarch64

Author: Carlos Eduardo Seo (ceseo)

Following up the discussion on the AArch64 Sync call (2024-01-09):

While debugging OpenMP (ompt/synchronization/master.c), I noticed this:

 cec:   97ffff19        bl      950 &lt;__kmpc_end_master@<!-- -->plt&gt;
 cf0:   14000001        b       cf4 &lt;main.omp_outlined+0xd4&gt;
 cf4:   d503201f        nop
 cf8:   14000001        b       cfc &lt;main.omp_outlined+0xdc&gt;
 cfc:   d0000088        adrp    x8, 12000 &lt;__gmon_start__@<!-- -->Base&gt;
 d00:   f9404d08        ldr     x8, [x8, #<!-- -->152]

I'd expect the nop to be followed by the adrp. Instead, there is an unconditional branch to the adrp.

I've seen this pattern in other parts of the code as well, but this is the example I had at hand. I can try to create a smaller reproducer, if necessary.

llvmbot avatar Jan 09 '24 19:01 llvmbot

Thanks for the reproducer. I've not looked in great detail, but it may be something to do with the inline assembly blocks in the code. I was compiling the master.c file manually - I had to cobble it together to make it work and so might have got it wrong, but didn't see the same branches. Do you know which compiler options it uses?

Nothing out of the ordinary. The only extra flag I can see is -fopenmp.

ceseo avatar Jan 09 '24 19:01 ceseo

Is it -O0 by any chance? If so it would not be a surprise to see unoptimized code.

davemgreen avatar Jan 11 '24 08:01 davemgreen

Is it -O0 by any chance? If so it would not be a surprise to see unoptimized code.

No. It's just getting flags passed to cmake (in this case, -g and -O2).

ceseo avatar Jan 11 '24 13:01 ceseo

@davemgreen I stand corrected. I rechecked this and it's -O0. Sorry about that... it's very convoluted to get the correct compiler options for these tests.

ceseo avatar Jan 24 '24 14:01 ceseo

as Dave mentions unoptimized code at O0 is pretty typical, redundant branches used to be optimized away at O0 to help code size but that was changed to help debugging (https://github.com/llvm/llvm-project/commit/c5d84d2eb35c1f0d3b08e1d1e95c1a22a28904d1)

c-rhodes avatar Aug 18 '26 10:08 c-rhodes