Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/compiler/crystal/codegen/target.cr
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ class Crystal::Codegen::Target
end

target = LLVM::Target.from_triple(self.to_s)
machine = target.create_target_machine(self.to_s, cpu: cpu, features: features, opt_level: opt_level, reloc: reloc, code_model: code_model).not_nil!
emulated_tls = true if emulated_tls?
machine = target.create_target_machine(self.to_s, cpu, features, opt_level, reloc, code_model, emulated_tls).not_nil!
# FIXME: We need to disable global isel until https://reviews.llvm.org/D80898 is released,
# or we fixed generating values for 0 sized types.
# When removing this, also remove it from the ABI specs and jit compiler.
Expand All @@ -249,6 +250,10 @@ class Crystal::Codegen::Target
machine
end

def emulated_tls?
android? || openbsd? || (windows? && gnu?)
end

def to_s(io : IO) : Nil
io << architecture << '-' << vendor << '-' << environment
end
Expand Down
20 changes: 20 additions & 0 deletions src/llvm/lib_llvm/target_machine.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@ lib LibLLVM
fun get_target_name = LLVMGetTargetName(t : TargetRef) : Char*
fun get_target_description = LLVMGetTargetDescription(t : TargetRef) : Char*

{% if compare_versions(LibLLVM::VERSION, "18.0.0") >= 0 %}
type TargetMachineOptionsRef = Void*

fun create_target_machine_options = LLVMCreateTargetMachineOptions : TargetMachineOptionsRef
fun dispose_target_machine_options = LLVMDisposeTargetMachineOptions(TargetMachineOptionsRef)
fun target_machine_options_set_cpu = LLVMTargetMachineOptionsSetCPU(TargetMachineOptionsRef, Char*)
fun target_machine_options_set_features = LLVMTargetMachineOptionsSetFeatures(TargetMachineOptionsRef, Char*)
fun target_machine_options_set_abi = LLVMTargetMachineOptionsSetABI(TargetMachineOptionsRef, Char*)
fun target_machine_options_set_code_gen_opt_level = LLVMTargetMachineOptionsSetCodeGenOptLevel(TargetMachineOptionsRef, LLVM::CodeGenOptLevel)
fun target_machine_options_set_code_model = LLVMTargetMachineOptionsSetCodeModel(TargetMachineOptionsRef, LLVM::CodeModel)
fun target_machine_options_set_reloc_mode = LLVMTargetMachineOptionsSetRelocMode(TargetMachineOptionsRef, LLVM::RelocMode)
fun create_target_machine_with_options = LLVMCreateTargetMachineWithOptions(TargetRef, Char*, TargetMachineOptionsRef) : TargetMachineRef

# NOTE: https://github.com/llvm/llvm-project/pull/161155
{% if compare_versions(LibLLVM::VERSION, "22.0.0") >= 0 %}
fun target_machine_options_set_emulated_tls = LLVMTargetMachineOptionsSetEmulatedTLS(TargetMachineOptionsRef, Bool)
fun target_machine_options_set_enable_tls_desc = LLVMTargetMachineOptionsSetEnableTLSDESC(TargetMachineOptionsRef, Bool)
{% end %}
{% end %}

fun create_target_machine = LLVMCreateTargetMachine(t : TargetRef, triple : Char*, cpu : Char*, features : Char*, level : LLVM::CodeGenOptLevel, reloc : LLVM::RelocMode, code_model : LLVM::CodeModel) : TargetMachineRef
fun dispose_target_machine = LLVMDisposeTargetMachine(t : TargetMachineRef)
fun get_target_machine_target = LLVMGetTargetMachineTarget(t : TargetMachineRef) : TargetRef
Expand Down
25 changes: 23 additions & 2 deletions src/llvm/target.cr
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,29 @@ struct LLVM::Target
def create_target_machine(triple, cpu = "", features = "",
opt_level = LLVM::CodeGenOptLevel::Default,
reloc = LLVM::RelocMode::PIC,
code_model = LLVM::CodeModel::Default) : LLVM::TargetMachine
target_machine = LibLLVM.create_target_machine(self, triple, cpu, features, opt_level, reloc, code_model)
code_model = LLVM::CodeModel::Default,
emulated_tls = nil,
enable_tls_desc = nil) : LLVM::TargetMachine
target_machine =
{% if LibLLVM.has_method?(:create_target_machine_options) %}
begin
options = LibLLVM.create_target_machine_options
LibLLVM.target_machine_options_set_cpu(options, cpu)
LibLLVM.target_machine_options_set_features(options, features)
LibLLVM.target_machine_options_set_code_gen_opt_level(options, opt_level)
LibLLVM.target_machine_options_set_code_model(options, code_model)
LibLLVM.target_machine_options_set_reloc_mode(options, reloc)
{% if LibLLVM.has_method?(:target_machine_options_set_emulated_tls) %}
LibLLVM.target_machine_options_set_emulated_tls(options, emulated_tls ? 1 : 0) unless emulated_tls.nil?
LibLLVM.target_machine_options_set_enable_tls_desc(options, enable_tls_desc ? 1 : 0) unless enable_tls_desc.nil?
{% end %}
machine = LibLLVM.create_target_machine_with_options(self, triple, options)
LibLLVM.dispose_target_machine_options(options)
machine
end
{% else %}
LibLLVM.create_target_machine(self, triple, cpu, features, opt_level, reloc, code_model)
{% end %}
target_machine ? TargetMachine.new(target_machine) : raise "Couldn't create target machine"
end

Expand Down
Loading