Skip to content

Commit

Permalink
Support LLVM 5.0 (#4821)
Browse files Browse the repository at this point in the history
* Drop LLVM 3.5 and LLVM 3.6 support

The supported LLVM versions are 3.8, 3.9 and 4.0.

* Support LLVM 5.0
  • Loading branch information
ysbaddaden authored and RX14 committed Sep 8, 2017
1 parent cd4c132 commit 3d48a96
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 328 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ all_spec
/tmp
/doc/
/src/llvm/ext/llvm_ext.o
/src/llvm/ext/llvm_ext.dwo
/src/ext/*.o
/src/ext/libcrystal.a
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ LLVM_CONFIG_FINDER := \
(command -v llvm-config > /dev/null && (case "$(llvm-config --version)" in 3.9*) command -v llvm-config;; *) false;; esac)) || \
command -v llvm-config-3.8 || command -v llvm-config38 || \
(command -v llvm-config > /dev/null && (case "$(llvm-config --version)" in 3.8*) command -v llvm-config;; *) false;; esac)) || \
command -v llvm-config-3.6 || command -v llvm-config36 || \
command -v llvm-config-3.5 || command -v llvm-config35 || \
command -v llvm-config
LLVM_CONFIG := $(shell $(LLVM_CONFIG_FINDER))
LLVM_EXT_DIR = src/llvm/ext
Expand Down
69 changes: 19 additions & 50 deletions src/compiler/crystal/compiler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,6 @@ module Crystal

protected def optimize(llvm_mod)
fun_pass_manager = llvm_mod.new_function_pass_manager
{% if LibLLVM::IS_35 || LibLLVM::IS_36 %}
fun_pass_manager.add_target_data target_machine.data_layout
{% end %}
pass_manager_builder.populate fun_pass_manager
fun_pass_manager.run llvm_mod
module_pass_manager.run llvm_mod
Expand All @@ -460,9 +457,6 @@ module Crystal
private def module_pass_manager
@module_pass_manager ||= begin
mod_pass_manager = LLVM::ModulePassManager.new
{% if LibLLVM::IS_35 || LibLLVM::IS_36 %}
mod_pass_manager.add_target_data target_machine.data_layout
{% end %}
pass_manager_builder.populate mod_pass_manager
mod_pass_manager
end
Expand Down Expand Up @@ -554,54 +548,29 @@ module Crystal
can_reuse_previous_compilation =
!compiler.emit && !@bc_flags_changed && File.exists?(bc_name) && File.exists?(object_name)

{% if LibLLVM::IS_35 %}
# In LLVM 3.5 we can't write a bitcode to memory,
# so instead we write it to another file
bc_name_new = self.bc_name_new
llvm_mod.write_bitcode_to_file(bc_name_new)

if can_reuse_previous_compilation
if FileUtils.cmp(bc_name, bc_name_new)
# If the user cancelled a previous compilation it might be that
# the .o file is empty
if File.size(object_name) > 0
File.delete bc_name_new
must_compile = false
end
end
end
memory_buffer = llvm_mod.write_bitcode_to_memory_buffer

if must_compile
# Create/overwrite the .bc file (for next compilations)
File.rename(bc_name_new, bc_name)
compiler.optimize llvm_mod if compiler.release?
compiler.target_machine.emit_obj_to_file llvm_mod, object_name
end
{% else %}
memory_buffer = llvm_mod.write_bitcode_to_memory_buffer

if can_reuse_previous_compilation
memory_io = IO::Memory.new(memory_buffer.to_slice)
changed = File.open(bc_name) { |bc_file| !FileUtils.cmp(bc_file, memory_io) }

# If the user cancelled a previous compilation
# it might be that the .o file is empty
if !changed && File.size(object_name) > 0
must_compile = false
memory_buffer.dispose
memory_buffer = nil
else
# We need to compile, so we'll write the memory buffer to file
end
end
if can_reuse_previous_compilation
memory_io = IO::Memory.new(memory_buffer.to_slice)
changed = File.open(bc_name) { |bc_file| !FileUtils.cmp(bc_file, memory_io) }

# If there's a memory buffer, it means we must create a .o from it
if memory_buffer
# Create the .bc file (for next compilations)
File.write(bc_name, memory_buffer.to_slice)
# If the user cancelled a previous compilation
# it might be that the .o file is empty
if !changed && File.size(object_name) > 0
must_compile = false
memory_buffer.dispose
memory_buffer = nil
else
# We need to compile, so we'll write the memory buffer to file
end
{% end %}
end

# If there's a memory buffer, it means we must create a .o from it
if memory_buffer
# Create the .bc file (for next compilations)
File.write(bc_name, memory_buffer.to_slice)
memory_buffer.dispose
end

if must_compile
compiler.optimize llvm_mod if compiler.release?
Expand Down
8 changes: 1 addition & 7 deletions src/llvm.cr
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,5 @@ module LLVM
string
end

{% if LibLLVM::IS_35 %}
DEBUG_METADATA_VERSION = 1
{% elsif LibLLVM::IS_36 %}
DEBUG_METADATA_VERSION = 2
{% else %}
DEBUG_METADATA_VERSION = 3
{% end %}
DEBUG_METADATA_VERSION = 3
end
8 changes: 4 additions & 4 deletions src/llvm/context.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class LLVM::Context
end

def new_module(name : String) : Module
{% if LibLLVM::IS_38 || LibLLVM::IS_36 || LibLLVM::IS_35 %}
{% if LibLLVM::IS_38 %}
Module.new(LibLLVM.module_create_with_name_in_context(name, self), name, self)
{% else %}
{% else %} # LLVM >= 3.9
Module.new(LibLLVM.module_create_with_name_in_context(name, self), self)
{% end %}
end
Expand Down Expand Up @@ -104,9 +104,9 @@ class LLVM::Context
if ret != 0 && msg
raise LLVM.string_and_dispose(msg)
end
{% if LibLLVM::IS_38 || LibLLVM::IS_36 || LibLLVM::IS_35 %}
{% if LibLLVM::IS_38 %}
Module.new(mod, "unknown", self)
{% else %}
{% else %} # LLVM >= 3.9
Module.new(mod, self)
{% end %}
end
Expand Down
24 changes: 4 additions & 20 deletions src/llvm/di_builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,8 @@ struct LLVM::DIBuilder

def create_function(scope, name, linkage_name, file, line, composite_type, is_local_to_unit, is_definition,
scope_line, flags, is_optimized, func)
{% if LibLLVM::IS_36 || LibLLVM::IS_35 %}
LibLLVMExt.di_builder_create_function(self, scope, name, linkage_name, file, line, composite_type,
is_local_to_unit ? 1 : 0,
is_definition ? 1 : 0,
scope_line, flags,
is_optimized ? 1 : 0, func)
{% else %}
LibLLVMExt.di_builder_create_function(self, scope, name, linkage_name, file, line, composite_type,
is_local_to_unit, is_definition, scope_line, flags, is_optimized, func)
{% end %}
LibLLVMExt.di_builder_create_function(self, scope, name, linkage_name, file, line, composite_type,
is_local_to_unit, is_definition, scope_line, flags, is_optimized, func)
end

def create_auto_variable(scope, name, file, line, type, align_in_bits)
Expand Down Expand Up @@ -87,19 +79,11 @@ struct LLVM::DIBuilder
end

def create_replaceable_composite_type(scope, name, file, line, context : Context)
{% if LibLLVM::IS_35 || LibLLVM::IS_36 %}
LibLLVMExt.temporary_md_node(context, nil, 0).as(LibLLVMExt::Metadata)
{% else %}
LibLLVMExt.di_builder_create_replaceable_composite_type(self, scope, name, file, line)
{% end %}
LibLLVMExt.di_builder_create_replaceable_composite_type(self, scope, name, file, line)
end

def replace_temporary(from, to)
{% if LibLLVM::IS_35 || LibLLVM::IS_36 %}
LibLLVMExt.metadata_replace_all_uses_with(from, to)
{% else %}
LibLLVMExt.di_builder_replace_temporary(self, from, to)
{% end %}
LibLLVMExt.di_builder_replace_temporary(self, from, to)
end

def end
Expand Down
Loading

0 comments on commit 3d48a96

Please sign in to comment.