Skip to content

Commit

Permalink
Do not inline type IDs in LLVM IR
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil committed Feb 17, 2025
1 parent 9b7fd8d commit 9ae6c52
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
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

0 comments on commit 9ae6c52

Please sign in to comment.