Open AlexDenisov opened 7 years ago
mentioned in issue llvm/llvm-bugzilla-archive#36743
I think this needs some clarification. llvm/llvm-bugzilla-archive#36743 states that: "-fno-plt -mx32" at any optimization level higher than -O0 will break at isel In this report neither -fno-plt nor -Ox are involved. It's about -m32 and not -mx32. While I don't know much about their difference, I see:
$ bin/clang --version clang version 9.0.0 (https://github.com/llvm/llvm-project.git 51dc59d0903fc86b37e9d910aa6f33dd87fdaae5) Target: x86_64-apple-darwin18.5.0 Thread model: posix
$ cat main.c int square(int x) { return x * x; } int main(int argc, char **argv) { return square(argc); }
$ bin/clang -m32 main.c -o main-m32 $ ./main-m32 $ echo $? 1
$ bin/clang -m32 -mx32 main.c -o main-m32 $ ./main-m32 $ echo $? 1
$ bin/clang -c -emit-llvm -m32 main.c -o - | bin/lli Illegal instruction: 4
$ bin/clang -c -emit-llvm -m32 -mx32 main.c -o - | bin/lli $ echo $? 1
This means passing -m32 and avoiding the -mx32 flag causes clang to emit bitcode that lli doesn't interpret correctly. How the failure manifests depends on the JIT engine:
$ bin/clang -c -emit-llvm -m32 main.c -o - | bin/lli -jit-kind=orc-lazy Segmentation fault: 11 $ bin/clang -c -emit-llvm -m32 main.c -o - | bin/lli -jit-kind=orc-mcjit Illegal instruction: 4
In fact I can reproduce the instruction selection issue (only) with an incompatible target (I couldn't link that statically either):
$ bin/clang -c -emit-llvm -mx32 --target=i386-apple-darwin main.c -o - | bin/lli $ echo $? 1
$ bin/clang -c -emit-llvm -mx32 --target=i386-unknown-unknown main.c -o - | bin/lli LLVM ERROR: Cannot select: t9: ch,glue = X86ISD::CALL t7, t17, Register:i32 $edi, RegisterMask:Untyped, t7:1 t17: i32 = X86ISD::Wrapper TargetGlobalAddress:i32<i32 (i32) @square> 0 t16: i32 = TargetGlobalAddress<i32 (i32) @square> 0 t6: i32 = Register $edi t8: Untyped = RegisterMask t7: ch,glue = CopyToReg t5, Register:i32 $edi, t3 t6: i32 = Register $edi t3: i32,ch = CopyFromReg t0, Register:i32 %5 t2: i32 = Register %5 In function: main
So either I am missing something, or the situation changed entirely since the report, or there was some other factor that we didn't consider.
Recommended workaround: build LLVM and Orc for x32:
cmake -DLLVM_BUILD_32_BITS=On path-to-llvm-sources
Looks like this may be the same bug as llvm/llvm-bugzilla-archive#36743 . If so, it is not JIT specific, but even if it were fixed it is possible we would need to do some work to support the X32 ABI in ORC.
CC'ing Lang Hames, jfyi.
Extended Description
I compile a simple code:
// main.c
include
int square(int x) { return x * x; }
int main() { printf("%d\n", square(4)); return 0; }
Using the following command:
Then I try to run the bitcode using lli. Based on jit kind it either crashes or shows an instruction selection error:
===
I tested it only on Linux, but other systems might be affected as well.