[PATCH] D152221: Avoid cross-section branches in AArch64 inline asm

Daniel Hoekwater via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 6 20:01:38 PDT 2023


dhoekwater added a comment.

In D152221#4401085 <https://reviews.llvm.org/D152221#4401085>, @efriedma wrote:

> Sure, but we don't need to disable function splitting and bb sections to handle that case.  The distance between an "asm goto" and its destination doesn't matter; we can apply branch relaxation like we would for normal conditional branches.

Ah, my mistake. My main understanding of inline assembly was that LLVM treats it as a black box and for that reason we couldn't make any correctness guarantees about cross-section branches. I chatted with @nickdesaulniers; he helped clarify things and pointed me to inline assembly documentation. Here are some better examples: https://godbolt.org/z/9fv1TYs37.

`asm_goto_conditional` may fail to link if `bar` is placed in a different section than the entry block because "A linker may use a veneer to implement a relocated branch if the relocation is either R_<CLS>_CALL26, R_<CLS>_JUMP26, or R_<CLS>_PLT32 (...) In all other cases a linker shall diagnose an error if relocation cannot be effected without a veneer. <https://github.com/ARM-software/abi-aa/blob/main/aaelf64/aaelf64.rst#577call-and-jump-relocations>" In this context, the limitation means that an inline assembly statement cannot rely on conditional branches to Goto Labels, as the compiler or linker may place the blocks such that the branch target is out of range. This is already the case, as the compiler is free to rearrange instructions, insert padding, etc. (https://godbolt.org/z/njKKsq7r1 demonstrates this without function splitting). However, MFS is much more likely to expose inline assembly that misuses conditional jumps because it's more likely that the Goto Label will be out of `B.cond`'s range if it's in a different text section. (The same limitation makes conditional tail calls to functions in a different section generate very suboptimal assembly, but that's a separate conversation.)

`asm_goto_unconditional` is similar, but the onus on the inline assembly user is less strict. If an inline asm statement contains an unconditional branch to a Goto Label, it must clobber X16 and X17 or else the branch may be out of range (for the same reason as with conditional branches), and the linker will overwrite the value of X16, which may be needed after the branch. https://godbolt.org/z/srjYqbE4Y demonstrates this without function splitting.

So all this to say: I agree that this patch isn't needed for Basic Block Sections or Machine Function Splitting. Every AArch64 inline assembly statement that branches to a Goto Label should do so unconditionally and should clobber X16 and X17.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D152221/new/

https://reviews.llvm.org/D152221



More information about the llvm-commits mailing list