Skip to content

Commit

Permalink
Compiler specs: dispose LLVM JIT as soon as possible.
Browse files Browse the repository at this point in the history
This seems to fix an issue with unwind stopping to work well after using a JIT
without disposing it. See #3439
Ary Borenszweig committed Oct 21, 2016

Verified

This commit was signed with the committer’s verified signature.
headius Charles Oliver Nutter
1 parent 13b11d7 commit 7517056
Showing 2 changed files with 27 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/compiler/crystal/codegen/codegen.cr
Original file line number Diff line number Diff line change
@@ -45,8 +45,19 @@ module Crystal

llvm_mod.verify

engine = LLVM::JITCompiler.new(llvm_mod)
engine.run_function wrapper, [] of LLVM::GenericValue
# We use the block form so we dispose the JIT as soon as possible.
# This isn't really needed, but on LLVM 3.9 it sometimes messes the
# stack trace for unwind, or so it seems, or there's something else
# that for now we can't understand.
#
# Since this only affects specs (not the code that is generated by
# the compiler) we can postpone the understanding and continue
# running specs just fine.
#
# See https://github.com/crystal-lang/crystal/pull/3439
LLVM::JITCompiler.new(llvm_mod) do |jit|
jit.run_function wrapper, [] of LLVM::GenericValue
end
end

def codegen(node, single_module = false, debug = false, llvm_mod = LLVM::Module.new("main_module"), expose_crystal_main = true)
14 changes: 14 additions & 0 deletions src/llvm/jit_compiler.cr
Original file line number Diff line number Diff line change
@@ -8,6 +8,13 @@ class LLVM::JITCompiler
if LibLLVM.create_mc_jit_compiler_for_module(out @unwrap, mod, nil, 0, out error) != 0
raise LLVM.string_and_dispose(error)
end

@finalized = false
end

def self.new(mod)
jit = new(mod)
yield jit ensure jit.dispose
end

def run_function(func)
@@ -28,7 +35,14 @@ class LLVM::JITCompiler
@unwrap
end

def dispose
return if @finalized
@finalized = true
finalize
end

def finalize
return if @finalized
LibLLVM.dispose_execution_engine(@unwrap)
end
end

0 comments on commit 7517056

Please sign in to comment.