crystal-lang / crystal

The Crystal Programming Language
https://crystal-lang.org
Apache License 2.0
19.2k stars 1.61k forks source link

LLVM codegen error can stall the compiler #14761

Closed HertzDevil closed 8 hours ago

HertzDevil commented 4 days ago

LLVM codegen errors that occur after module validation could terminate the fibers spawned during program compilation, unless --single-module or --threads=1 is also given. For example, if I intentionally use an LLVM intrinsic that isn't defined on a given target:

# compile on x86
lib LibIntrinsics
  fun aarch64_clrex = "llvm.aarch64.clrex"
end

LibIntrinsics.aarch64_clrex
# compile on aarch64
lib LibIntrinsics
  fun x86_int = "llvm.x86.int"(Int8)
end

LibIntrinsics.x86_int(3)

then the compiler stalls with:

LLVM ERROR: Cannot select: intrinsic %llvm.x86.int
Unhandled exception in spawn: Nil assertion failed (NilAssertionError)
  from /opt/homebrew/Cellar/crystal/1.12.2/bin/crystal in 'raise<NilAssertionError>:NoReturn'
  from /opt/homebrew/Cellar/crystal/1.12.2/bin/crystal in 'Nil#not_nil!<Nil>:NoReturn'
  from /opt/homebrew/Cellar/crystal/1.12.2/bin/crystal in 'Nil#not_nil!:NoReturn'
  from /opt/homebrew/Cellar/crystal/1.12.2/bin/crystal in '~procProc(Nil)@src/compiler/crystal/compiler.cr:528'
  from /opt/homebrew/Cellar/crystal/1.12.2/bin/crystal in 'Fiber#run:(IO::FileDescriptor | Nil)'

and must be stopped with Ctrl+C. If there is only one thread then the compiler crashes with SIGABRT.

The compiler should shut down gracefully even if multiple threads are used.