Skip to content
Merged
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
4 changes: 2 additions & 2 deletions spec/compiler/codegen/union_type_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ describe "Code gen: union type" do

str = mod.to_s
{% if LibLLVM::IS_LT_150 %}
str.should contain("store i512 1, i512* %2, align 8")
str.should match(/store i512 1, i512\* %\d+, align 8/)
{% else %}
str.should contain("store i512 1, ptr %1, align 8")
str.should match(/store i512 1, ptr %\d+, align 8/)
{% end %}

# an i512 store defaults to 16-byte alignment, which is undefined behavior
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/codegen/codegen.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2462,7 +2462,7 @@ module Crystal
global.linkage = LLVM::Linkage::Private
global.global_constant = true
global.initializer = llvm_context.const_struct [
type_id(@program.string),
int32(@program.llvm_id.type_id(@program.string)), # in practice, should always be 1
int32(str.bytesize),
int32(str.size),
llvm_context.const_string(str),
Expand Down
21 changes: 20 additions & 1 deletion src/compiler/crystal/codegen/type_id.cr
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@ class Crystal::CodeGenVisitor
end

private def type_id_impl(type)
int(@program.llvm_id.type_id(type))
type_id_name = "#{type.llvm_name}:type_id"

global = @main_mod.globals[type_id_name]?
unless global
global = @main_mod.globals.add(@main_llvm_context.int32, type_id_name)
global.linkage = LLVM::Linkage::Internal if @single_module
global.initializer = @main_llvm_context.int32.const_int(@program.llvm_id.type_id(type))
global.global_constant = true
end

if @llvm_mod != @main_mod
global = @llvm_mod.globals[type_id_name]?
unless global
global = @llvm_mod.globals.add(@llvm_context.int32, type_id_name)
global.linkage = LLVM::Linkage::External
global.global_constant = true
end
end

load(@llvm_context.int32, global)
end
end