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

LTO fails with inline asm labels referenced in C

Open llvmbot opened this issue 17 years ago • 10 comments

Bugzilla Link 5623
Version trunk
OS All
Reporter LLVM Bugzilla Contributor
CC @asl,@mrvacbob,@lattner,@echristo,@isanbard

Extended Description

This is a bit ugly, but it's used in ffmpeg to create a jump table. Clang and llvm-gcc work at -O3 but fail with -O4 on OS X, both the versions in XCode 3.2.1 and r89800. Adding .globl doesn't help. Remove the _ prefix for Linux; I didn't test there because I can't get gold compiled.

#include <stdio.h>

extern void ff_mlp_firorder_8;

static const void *firtable[1] = {&ff_mlp_firorder_8};

int main()
{
    int x = 1;
    
    asm volatile(
        "jmp *%1 \n\t"
        "xor %0, %0 \n\t"
//        ".globl _ff_mlp_firorder_8 \n\t"
        "_ff_mlp_firorder_8: \n\t"
        "add $1, %0 \n\t"
        : "+r"(x)
        : "g"(firtable[0])
    );
    printf("x = %d\n", x);
}

llvmbot avatar Nov 27 '09 07:11 llvmbot

The general issue of "we don't parse inline asm in fuction bodies is still with us".

But in this case the failure is far more mundane, nothing to do with LTO. Running

llc named.ll -o named.s -no-integrated-as opt -S named.ll -o named2.ll -globalopt llc named2.ll -o named2.s -no-integrated-as

We see that the "jmp *-16(%rbp)" becomes jmp *$ff_mlp_firorder_8

named.ll is just the output of "clang -c -flto test.c"

llvmbot avatar Mar 27 '15 20:03 llvmbot

hand waves There's something going on between the asm printer and the mc level that's causing an earlier emission of globals to solve the unknown symbol part of this problem. I'm guessing that the parsing of the inline assembly happens in a way that's divorced from the rest of everything... or something. Gave up tracking it down after a bit.

echristo avatar Mar 27 '15 19:03 echristo

To expand upon the global emission problem:

We can't fix this at the moment because we would then emit globals during doInitialize which happens before any of the rest of the pass machinery runs.

Possible solution: Turn AsmPrinter into a Module pass and have it iterate over the functions itself after doing things like emitting globals.

I am not sure I follow. We don't parse the bodies at all for deciding which symbols are exported from a .bc file.

llvmbot avatar Mar 27 '15 16:03 llvmbot

To expand upon the global emission problem:

We can't fix this at the moment because we would then emit globals during doInitialize which happens before any of the rest of the pass machinery runs.

Possible solution: Turn AsmPrinter into a Module pass and have it iterate over the functions itself after doing things like emitting globals.

echristo avatar Mar 27 '15 01:03 echristo

The undefined reference is due to the order in which we're outputting globals versus adding functions to the file. This part can be fixed by emitting globals first in the file.

The particular use in ffmpeg is pretty unsettling, at no point do we handle starting a new function in the middle of another one. (and on elf this is really weird in that you'll have wrong sizes for functions I don't even want to talk about how things get emitted, etc).

echristo avatar Mar 27 '15 01:03 echristo

It doesn't work for me:

$ clang -c  test.c 
$ nm test.o
000000000000002b t ff_mlp_firorder_8
0000000000000000 d firtable
0000000000000000 T main
                 U printf

$ clang -c  test.c 
$ nm test.o
         U ff_mlp_firorder_8
00000000 T main
         U printf

note the undefined reference to ff_mlp_firorder_8.

also

$ clang test.o -o t -flto
<inline asm>:1:2: error: unknown use of instruction mnemonic without a size suffix
        jmp *$ff_mlp_firorder_8 
        ^

LLVM ERROR: Error parsing inline asm

clang-3.4: error: linker command failed with exit code 1 (use -v to see invocation)

llvmbot avatar Oct 18 '13 18:10 llvmbot

This appears to be working now. Magic maybe?

isanbard avatar Oct 18 '13 10:10 isanbard

Currently we only scan module level inline assembly, so no.

llvmbot avatar Jul 11 '11 17:07 llvmbot

Rafael, didn't you fix this?

lattner avatar Jul 11 '11 17:07 lattner

This works on trunk (2026-10), tested on x86_64 Linux with the testcase (minus the _ prefix): non-LTO, full LTO, and ThinLTO at -O0/-O2/-O3, linked with lld, all print x = 2. Making the label .globl and taking its address from a second TU also links and runs under full LTO and ThinLTO.

The GlobalOpt failure from comment 7 (jmp *$ff_mlp_firorder_8) no longer reproduces; the "g" (imr) operand is now lowered as a memory operand (jmpq *-16(%rbp) after opt -passes=globalopt, jmpq *(%rsp) at -O2). Note that GCC -O2 rejects this testcase ("immediate operand illegal with absolute jump"), so using "g" with jmp *%1 is fragile regardless of compiler.

The general limitation that labels defined in function-body inline asm don't appear in the bitcode symbol table (they show as U) remains, but it doesn't cause a failure here. Closing.

isanbard avatar Oct 23 '26 21:10 isanbard