hylo-lang / Swifty-LLVM

Swifty interface for the LLVM compiler infrastructure.
Apache License 2.0
28 stars 5 forks source link

Likely LLVM precondition violation #29

Closed dabrahams closed 8 months ago

dabrahams commented 8 months ago

https://github.com/hylo-lang/Swifty-LLVM/blob/291474a13e3304e5ac072b1149b6032600744027/Tests/LLVMTests/Values/GlobalVariableTests.swift#L38

Causes an LLVM built with assertions on to assert:

Assertion failed: Val && "isa<> used on a null pointer", file S:\src\llvm-17.0.6\llvm\include\llvm/Support/Casting.h, line 109

kyouko-taiga commented 8 months ago

AFAIK there's no documented precondition on any of the LLVM methods called by this expression. It might be the same possible cause as #28, that is the module owning g got deinitialized by Swift before g.initializer was executed.

dabrahams commented 8 months ago

That the module is live is surely a precondition, then, n'est-ce-pas?

dabrahams commented 8 months ago

Stack trace:

Thread 1 Queue : com.apple.main-thread (serial)
#0  0x000000018470e0dc in __pthread_kill ()
#1  0x0000000184745cc0 in pthread_kill ()
#2  0x0000000184651a40 in abort ()
#3  0x0000000184650d30 in __assert_rtn ()
#4  0x000000011448a348 in llvm::isa_impl_cl<llvm::Constant, llvm::Value const*>::doit(llvm::Value const*) at /Users/runner/work/llvm-build/llvm-build/SourceCache/llvm-project/llvm/include/llvm/Support/Casting.h:109
#5  0x000000011448a2dc in llvm::isa_impl_wrap<llvm::Constant, llvm::Value const*, llvm::Value const*>::doit(llvm::Value const* const&) at /Users/runner/work/llvm-build/llvm-build/SourceCache/llvm-project/llvm/include/llvm/Support/Casting.h:137
#6  0x000000011448a2b0 in llvm::isa_impl_wrap<llvm::Constant, llvm::Value const* const, llvm::Value const*>::doit(llvm::Value const* const&) at /Users/runner/work/llvm-build/llvm-build/SourceCache/llvm-project/llvm/include/llvm/Support/Casting.h:127
#7  0x000000011448a278 in llvm::CastIsPossible<llvm::Constant, llvm::Value const*, void>::isPossible(llvm::Value const* const&) at /Users/runner/work/llvm-build/llvm-build/SourceCache/llvm-project/llvm/include/llvm/Support/Casting.h:255
#8  0x000000011448a250 in llvm::CastInfo<llvm::Constant, llvm::Value* const, void>::isPossible(llvm::Value* const&) at /Users/runner/work/llvm-build/llvm-build/SourceCache/llvm-project/llvm/include/llvm/Support/Casting.h:509
#9  0x000000011448a1f0 in bool llvm::isa<llvm::Constant, llvm::Value*>(llvm::Value* const&) at /Users/runner/work/llvm-build/llvm-build/SourceCache/llvm-project/llvm/include/llvm/Support/Casting.h:549
#10 0x000000011448a178 in decltype(auto) llvm::cast<llvm::Constant, llvm::Value>(llvm::Value*) at /Users/runner/work/llvm-build/llvm-build/SourceCache/llvm-project/llvm/include/llvm/Support/Casting.h:578
#11 0x0000000118919f9c in llvm::Constant* llvm::unwrap<llvm::Constant>(LLVMOpaqueValue*) at /Users/runner/work/llvm-build/llvm-build/SourceCache/llvm-project/llvm/include/llvm/IR/Value.h:1039
#12 0x000000011891cc30 in ::LLVMSetInitializer(LLVMValueRef, LLVMValueRef) at /Users/runner/work/llvm-build/llvm-build/SourceCache/llvm-project/llvm/lib/IR/Core.cpp:2158
#13 0x00000001142721fc in Module.setInitializer(_:for:) at /Users/dave/src/Swifty-LLVM/Sources/SwiftyLLVM/Module.swift:355
#14 0x000000011426286c in GlobalVariableTests.testInitializer() at /Users/dave/src/Swifty-LLVM/Tests/LLVMTests/Values/GlobalVariableTests.swift:37
#15 0x0000000114262d24 in @objc GlobalVariableTests.testInitializer() ()
kyouko-taiga commented 8 months ago

The stack trace is incredibly valuable, thanks! Sadly my hopes are down again because this one doesn't involve any surprising code.

Looking at the sources, it seems like the unwrapping of the constant value passed to the initializer (which should be a null pointer) causes the assertion. It is frustrating because llvm::GlobalVariable::setInitializer clearly documents that it is acceptable usage, but the corresponding C bindings has no documentation at all. AFAICT, the behavior of unwrap is also undocumented (it is an internal utility of the C bindings) so I'm not sure that it supports converting null pointers.

dabrahams commented 8 months ago

Yep. It looks like there are LLVM bug reports to file.

dabrahams commented 8 months ago

https://github.com/llvm/llvm-project/pull/82517