crystal-lang / crystal

The Crystal Programming Language
https://crystal-lang.org
Apache License 2.0
19.32k stars 1.61k forks source link

compile error when using --debug: "BUG: called create_llvm_type for TEntity (Exception)" #10241

Open kalinon opened 3 years ago

kalinon commented 3 years ago

I get the following error when compiling my examples with --debug. Im pretty sure it has something to do with my use of generics in my entitas framework: entitas.cr. Getting into the compiler gets a little over my head so figured id pass this one up the chain.

How to reproduce on OSX:

git clone git@github.com:spoved/oid.cr.git
cd oid.cr
git checkout debug-bug

brew install raylib
shards install
crystal build --debug ./examples/05/main.cr -o bin/example_05

Crystal version:

Crystal 0.35.1 (2020-06-19)
LLVM: 9.0.1
Default target: x86_64-apple-macosx

OS Version:

macOS Catalina
10.15.7 (19H15)

Error:

$ crystal build --debug --error-trace ./examples/05/main.cr -o bin/example_05
BUG: called create_llvm_type for TEntity (Exception)
  from raise<Exception>:NoReturn
  from raise<String>:NoReturn
  from Crystal::LLVMTyper#create_llvm_type<Crystal::Type+, Bool>:NoReturn
  from Crystal::LLVMTyper#llvm_type<Crystal::Type+, Bool>:LLVM::Type
  from Crystal::LLVMTyper#create_llvm_type<Crystal::PointerInstanceType, Bool>:LLVM::Type
  from Crystal::LLVMTyper#llvm_type<Crystal::Type+, Bool>:LLVM::Type
  from Crystal::LLVMTyper#create_llvm_struct_type<(Crystal::GenericClassInstanceType+ | Crystal::GenericClassType+ | Crystal::GenericModuleInstanceType | Crystal::GenericModuleType | Crystal::NonGenericClassType | Crystal::NonGenericModuleType+ | Crystal::VirtualType), Bool>:LLVM::Type
  from Crystal::LLVMTyper#llvm_struct_type<(Crystal::GenericClassInstanceType+ | Crystal::GenericClassType+ | Crystal::GenericModuleInstanceType | Crystal::GenericModuleType | Crystal::NonGenericClassType | Crystal::NonGenericModuleType+ | Crystal::VirtualType), Bool>:LLVM::Type
  from Crystal::LLVMTyper#create_llvm_type<(Crystal::GenericClassInstanceType+ | Crystal::GenericClassType+ | Crystal::GenericModuleInstanceType | Crystal::GenericModuleType | Crystal::NonGenericClassType | Crystal::NonGenericModuleType+ | Crystal::VirtualType), Bool>:LLVM::Type
  from Crystal::LLVMTyper#llvm_type<Crystal::Type+, Bool>:LLVM::Type
  from Crystal::LLVMTyper#llvm_type<Crystal::Type+, Bool>:LLVM::Type
  from Crystal::LLVMTyper#create_llvm_struct_type<(Crystal::GenericClassInstanceType+ | Crystal::GenericClassType+ | Crystal::GenericModuleInstanceType | Crystal::GenericModuleType | Crystal::NonGenericClassType | Crystal::NonGenericModuleType+ | Crystal::VirtualType), Bool>:LLVM::Type
  from Crystal::LLVMTyper#llvm_struct_type<(Crystal::GenericClassInstanceType+ | Crystal::GenericClassType+ | Crystal::GenericModuleInstanceType | Crystal::GenericModuleType | Crystal::NonGenericClassType | Crystal::NonGenericModuleType+ | Crystal::VirtualType), Bool>:LLVM::Type
  from Crystal::LLVMTyper#create_llvm_type<(Crystal::GenericClassInstanceType+ | Crystal::GenericClassType+ | Crystal::GenericModuleInstanceType | Crystal::GenericModuleType | Crystal::NonGenericClassType | Crystal::NonGenericModuleType+ | Crystal::VirtualType), Bool>:LLVM::Type
  from Crystal::LLVMTyper#llvm_type<Crystal::Type+, Bool>:LLVM::Type
  from Crystal::LLVMTyper#create_llvm_struct_type<(Crystal::GenericClassInstanceType+ | Crystal::GenericClassType+ | Crystal::GenericModuleInstanceType | Crystal::GenericModuleType | Crystal::NonGenericClassType | Crystal::NonGenericModuleType+ | Crystal::VirtualType), Bool>:LLVM::Type
  from Crystal::LLVMTyper#llvm_struct_type<(Crystal::GenericClassInstanceType+ | Crystal::GenericClassType+ | Crystal::GenericModuleInstanceType | Crystal::GenericModuleType | Crystal::NonGenericClassType | Crystal::NonGenericModuleType+ | Crystal::VirtualType), Bool>:LLVM::Type
  from Crystal::CodeGenVisitor#get_debug_type<Crystal::Type+, Crystal::Type+>:(LibLLVMExt::Metadata | Nil)
  from Crystal::CodeGenVisitor#get_debug_type<Crystal::Type+, Crystal::Type+>:(LibLLVMExt::Metadata | Nil)
  from Crystal::CodeGenVisitor#get_debug_type<Crystal::Type+, Crystal::Type+>:(LibLLVMExt::Metadata | Nil)
  from Crystal::CodeGenVisitor#codegen_fun_signature_non_external<String, Crystal::Def+, Crystal::Type+, Bool, Bool>:Array(Crystal::Arg)
  from Crystal::CodeGenVisitor#codegen_fun<String, Crystal::Def+, Crystal::Type+, Bool, Crystal::CodeGenVisitor::ModuleInfo, Bool, Bool>:LLVM::Function
  from Crystal::CodeGenVisitor#target_def_fun<Crystal::Def+, Crystal::Type+>:LLVM::Function
  from Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool
  from Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil
  from Crystal::CodeGenVisitor#codegen_cond<Crystal::ASTNode+>:LLVM::Value
  from Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil
  from Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil
  from Crystal::CodeGenVisitor#codegen_fun<String, Crystal::Def+, Crystal::Type+, Bool, Crystal::CodeGenVisitor::ModuleInfo, Bool, Bool>:LLVM::Function
  from Crystal::CodeGenVisitor#target_def_fun<Crystal::Def+, Crystal::Type+>:LLVM::Function
  from Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool
  from Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil
  from Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil
  from Crystal::CodeGenVisitor#codegen_fun<String, Crystal::Def+, Crystal::Type+, Bool, Crystal::CodeGenVisitor::ModuleInfo, Bool, Bool>:LLVM::Function
  from Crystal::CodeGenVisitor#target_def_fun<Crystal::Def+, Crystal::Type+>:LLVM::Function
  from Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool
  from Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil
  from Crystal::CodeGenVisitor#codegen_fun<String, Crystal::Def+, Crystal::Type+, Bool, Crystal::CodeGenVisitor::ModuleInfo, Bool, Bool>:LLVM::Function
  from Crystal::CodeGenVisitor#target_def_fun<Crystal::Def+, Crystal::Type+>:LLVM::Function
  from Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool
  from Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil
  from Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil
  from Crystal::CodeGenVisitor#codegen_fun<String, Crystal::Def+, Crystal::Type+, Bool, Crystal::CodeGenVisitor::ModuleInfo, Bool, Bool>:LLVM::Function
  from Crystal::CodeGenVisitor#target_def_fun<Crystal::Def+, Crystal::Type+>:LLVM::Function
  from Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool
  from Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil
  from Crystal::CodeGenVisitor#visit<Crystal::Assign>:(Bool | Nil)
  from Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil
  from Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil
  from Crystal::CodeGenVisitor#codegen_fun<String, Crystal::Def+, Crystal::Type+, Bool, Crystal::CodeGenVisitor::ModuleInfo, Bool, Bool>:LLVM::Function
  from Crystal::CodeGenVisitor#target_def_fun<Crystal::Def+, Crystal::Type+>:LLVM::Function
  from Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool
  from Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil
  from Crystal::CodeGenVisitor#prepare_call_args_non_external<Crystal::Call, Crystal::Def+, Crystal::Type+>:Tuple(Array(LLVM::Value), Bool)
  from Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool
  from Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil
  from Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil
  from Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil
  from Crystal::CodeGenVisitor#codegen_fun<String, Crystal::Def+, Crystal::Type+, Bool, Crystal::CodeGenVisitor::ModuleInfo, Bool, Bool>:LLVM::Function
  from Crystal::CodeGenVisitor#target_def_fun<Crystal::Def+, Crystal::Type+>:LLVM::Function
  from Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool
  from Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil
  from Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil
  from Crystal::Compiler#codegen<Crystal::Program, Crystal::ASTNode+, Array(Crystal::Compiler::Source), String>:(Tuple(Array(Crystal::Compiler::CompilationUnit), Array(String)) | Nil)
  from Crystal::Compiler#compile<Array(Crystal::Compiler::Source), String>:Crystal::Compiler::Result
  from Crystal::Command#run:(Bool | Nil)
  from __crystal_main
  from main
Error: you've found a bug in the Crystal compiler. Please open an issue, including source code that will allow us to reproduce the bug: https://github.com/crystal-lang/crystal/issues
Blacksmoke16 commented 3 years ago

Duplicate of https://github.com/crystal-lang/crystal/issues/8551?

kalinon commented 3 years ago

@Blacksmoke16 possibly, but i did not see any mention of --debug. my issue only occurs when using that flag. Compile/run works fine without.

kalinon commented 3 years ago

This is still present in 0.36.1

kalinon commented 3 years ago

The code from #8968 also produces a similar error. So probably is duplicate of #8968 & #8551. Seems to be happening during the codegen step.

kalinon commented 3 years ago

I was able to track this down to the union of generic types during codegen. Using the example code from #8968 (https://play.crystal-lang.org/#/r/8sai) i was able to get the following stack trace:

BUG: called create_llvm_type for ValueType (Exception)
  from src/compiler/crystal/codegen/llvm_typer.cr:254:7 in 'create_llvm_type'
  from src/compiler/crystal/codegen/llvm_typer.cr:88:37 in 'llvm_type'
  from src/compiler/crystal/codegen/llvm_typer.cr:374:9 in 'llvm_embedded_type'
  from src/compiler/crystal/codegen/llvm_typer.cr:308:32 in 'create_llvm_struct_type'
  from src/compiler/crystal/codegen/llvm_typer.cr:261:44 in 'llvm_struct_type'
  from src/compiler/crystal/codegen/llvm_typer.cr:140:20 in 'create_llvm_type'
  from src/compiler/crystal/codegen/llvm_typer.cr:88:37 in 'llvm_type'
  from src/compiler/crystal/codegen/unions.cr:34:28 in 'create_llvm_type'
  from src/compiler/crystal/codegen/llvm_typer.cr:90:26 in 'llvm_type'
  from src/compiler/crystal/codegen/llvm_typer.cr:84:5 in 'llvm_type'
  from src/compiler/crystal/codegen/llvm_builder_helper.cr:182:5 in 'llvm_type'
  from src/compiler/crystal/codegen/llvm_builder_helper.cr:179:23 in 'cast_to_pointer'
  from src/compiler/crystal/codegen/unions.cr:121:7 in 'downcast_distinct_union_types'
  from src/compiler/crystal/codegen/cast.cr:415:7 in 'downcast_distinct'
  from src/compiler/crystal/codegen/cast.cr:291:15 in 'downcast'
  from src/compiler/crystal/codegen/codegen.cr:1145:17 in 'visit'
  from src/compiler/crystal/syntax/visitor.cr:27:12 in 'accept'
  from src/compiler/crystal/codegen/codegen.cr:2184:7 in 'accept'
  from src/compiler/crystal/codegen/call.cr:100:7 in 'prepare_call_args_non_external'
  from src/compiler/crystal/codegen/call.cr:57:7 in 'prepare_call_args'
  from src/compiler/crystal/codegen/call.cr:21:26 in 'visit'
  from src/compiler/crystal/syntax/visitor.cr:27:12 in 'accept'
  from src/compiler/crystal/codegen/codegen.cr:2184:7 in 'accept'
  from src/compiler/crystal/codegen/call.cr:405:11 in 'codegen_dispatch'
  from src/compiler/crystal/codegen/call.cr:15:7 in 'visit'
  from src/compiler/crystal/syntax/visitor.cr:27:12 in 'accept'
  from src/compiler/crystal/codegen/codegen.cr:2184:7 in 'accept'
  from src/compiler/crystal/codegen/codegen.cr:619:9 in 'visit'
  from src/compiler/crystal/syntax/visitor.cr:27:12 in 'accept'
  from src/compiler/crystal/codegen/codegen.cr:2184:7 in 'accept'
  from src/compiler/crystal/codegen/codegen.cr:67:7 in 'codegen'
  from src/compiler/crystal/codegen/codegen.cr:65:5 in 'codegen:debug:single_module'
  from src/compiler/crystal/progress_tracker.cr:22:7 in 'codegen'
  from src/compiler/crystal/compiler.cr:172:16 in 'compile'
  from src/compiler/crystal/command.cr:296:3 in 'compile'
  from src/compiler/crystal/command.cr:294:5 in 'compile'
  from src/compiler/crystal/command.cr:187:5 in 'build'
  from src/compiler/crystal/command.cr:73:7 in 'run'
  from src/compiler/crystal/command.cr:49:5 in 'run'
  from src/compiler/crystal/command.cr:48:3 in 'run'
  from src/compiler/crystal.cr:11:1 in '__crystal_main'
  from src/crystal/main.cr:110:5 in 'main_user_code'
  from src/crystal/main.cr:96:7 in 'main'
  from src/crystal/main.cr:119:3 in 'main'

Debugging and prints got me the following information when downcast_distinct_union_types is being called.

  from src/compiler/crystal/codegen/unions.cr:121:7 in 'downcast_distinct_union_types'

  value:      %84 = phi %"(GreaterThan(Int32) | NotBlank)"* [ %8, %current_def7 ], [ %10, %current_def10 ], !dbg !61
  to_type:    (AbstractComparison(Int32) | AbstractComparison(ValueType) | GreaterThan(Int32))

The to_type has both AbstractComparison(Int32) and its generic type AbstractComparison(ValueType) defined. This eventually leads to the error. I was able to clumsily reject the generic type by adding a reject in including_types defined in types.cr:

https://github.com/crystal-lang/crystal/blob/bb5a7938eccc51a0feab2f16063a1ae46ad416c0/src/compiler/crystal/types.cr#L1824-L1835

The reject will filter out any instance_types that is the same as its generic_type.

    def including_types
      instances = generic_types.values
      subclasses.each do |subclass|
        if subclass.is_a?(GenericClassType)
          subtypes = subclass.including_types
          instances << subtypes if subtypes
        else
          instances << subclass
        end
      end
      instances.reject! {|i| i.is_a?(GenericClassInstanceType) && i.instance_type.to_s == i.generic_type.to_s}
      program.union_of instances
    end

This allowed me to compile successfully, but I do not want to open a PR on this. Perhaps someone like @straight-shoota or @Blacksmoke16 will know how to fix this issue correctly.

kalinon commented 3 years ago

@asterite looks like you wrote the original code (5 years ago!) anything jump out at you?

asterite commented 3 years ago

@kalinon I don't have time to look into this, but my advise is to not use --debug

kalinon commented 3 years ago

@asterite thanks for the response. I'm guessing you mean don't use --debug for everyday compiling? I needed --debug for the symbolic info to trace it via debug tools. Unless there's another reason not to.

b-ncMN commented 2 years ago

I am experiencing the same issue on my linux machine :

BUG: called create_llvm_type for Root (Exception)
  from /crystal/src/compiler/crystal/codegen/llvm_typer.cr:254:7 in 'create_llvm_type'
  from /crystal/src/pointer.cr:117:6 in 'llvm_type'
  from /crystal/src/compiler/crystal/codegen/llvm_typer.cr:379:7 in 'create_llvm_struct_type'
  from /crystal/src/compiler/crystal/codegen/llvm_typer.cr:263:33 in 'llvm_struct_type'
  from /crystal/src/compiler/crystal/codegen/llvm_typer.cr:257:5 in 'get_debug_type'
  from /crystal/src/compiler/crystal/codegen/debug.cr:315:7 in 'get_debug_type'
  from /crystal/src/compiler/crystal/codegen/debug.cr:315:7 in 'get_debug_type'
  from /crystal/src/compiler/crystal/codegen/debug.cr:315:7 in 'get_debug_type'
  from /crystal/src/compiler/crystal/codegen/debug.cr:315:7 in 'get_debug_type'
  from /crystal/src/compiler/crystal/codegen/debug.cr:173:7 in 'get_debug_type'
  from /crystal/src/compiler/crystal/codegen/debug.cr:315:7 in 'get_debug_type'
  from /crystal/src/compiler/crystal/codegen/debug.cr:315:7 in 'get_debug_type'
  from /crystal/src/compiler/crystal/codegen/debug.cr:315:7 in 'get_debug_type'
  from /crystal/src/compiler/crystal/codegen/debug.cr:315:7 in 'get_debug_type'
  from /crystal/src/compiler/crystal/codegen/debug.cr:315:7 in 'get_debug_type'
  from /crystal/src/compiler/crystal/codegen/debug.cr:315:7 in 'get_debug_type'
  from /crystal/src/compiler/crystal/codegen/debug.cr:315:7 in 'get_debug_type'
  from /crystal/src/compiler/crystal/codegen/debug.cr:173:7 in 'get_debug_type'
  from /crystal/src/compiler/crystal/codegen/debug.cr:315:7 in 'get_debug_type'
  from /crystal/src/compiler/crystal/codegen/debug.cr:315:7 in 'get_debug_type'
  from /crystal/src/compiler/crystal/codegen/debug.cr:173:7 in 'get_debug_type'
  from /crystal/src/compiler/crystal/codegen/debug.cr:315:7 in 'get_debug_type'
  from /crystal/src/compiler/crystal/codegen/debug.cr:315:7 in 'get_debug_type'
  from /crystal/src/compiler/crystal/codegen/debug.cr:233:7 in 'get_debug_type'
  from /crystal/src/compiler/crystal/codegen/debug.cr:315:7 in 'get_debug_type'
  from /crystal/src/compiler/crystal/codegen/debug.cr:233:7 in 'get_debug_type'
  from /crystal/src/compiler/crystal/codegen/debug.cr:315:7 in 'get_debug_type'
  from /crystal/src/compiler/crystal/codegen/debug.cr:233:7 in 'get_debug_type'
  from /crystal/src/compiler/crystal/codegen/debug.cr:315:7 in 'get_debug_type'
  from /crystal/src/compiler/crystal/codegen/debug.cr:351:7 in 'declare_variable'
  from /crystal/src/compiler/crystal/codegen/codegen.cr:1837:17 in 'alloca_non_closured_vars'
  from /crystal/src/compiler/crystal/codegen/call.cr:295:7 in 'visit'
  from /crystal/src/compiler/crystal/syntax/visitor.cr:27:12 in 'accept'
  from /crystal/src/compiler/crystal/codegen/codegen.cr:667:9 in 'accept'
  from /crystal/src/compiler/crystal/codegen/codegen.cr:2281:7 in 'codegen_fun'
  from /crystal/src/compiler/crystal/codegen/fun.cr:51:3 in 'accept'
  from /crystal/src/compiler/crystal/codegen/call.cr:331:5 in 'visit'
  from /crystal/src/compiler/crystal/syntax/visitor.cr:27:12 in 'accept'
  from /crystal/src/compiler/crystal/codegen/codegen.cr:667:9 in 'accept'
  from /crystal/src/compiler/crystal/codegen/codegen.cr:727:15 in 'accept'
  from /crystal/src/compiler/crystal/codegen/codegen.cr:667:9 in 'accept'
  from /crystal/src/compiler/crystal/codegen/codegen.cr:437:17 in 'accept'
  from /crystal/src/compiler/crystal/codegen/codegen.cr:667:9 in 'accept'
  from /crystal/src/compiler/crystal/codegen/codegen.cr:437:17 in 'accept'
  from /crystal/src/compiler/crystal/codegen/codegen.cr:667:9 in 'accept'
  from /crystal/src/compiler/crystal/codegen/codegen.cr:437:17 in 'accept'
  from /crystal/src/compiler/crystal/codegen/codegen.cr:667:9 in 'accept'
  from /crystal/src/compiler/crystal/codegen/codegen.cr:2246:7 in 'codegen'
  from /crystal/src/compiler/crystal/compiler.cr:173:16 in 'compile'
  from /crystal/src/compiler/crystal/command.cr:220:5 in 'run_command'
  from /crystal/src/compiler/crystal/command.cr:64:5 in '__crystal_main'
  from /crystal/src/crystal/main.cr:115:5 in 'main'
  from src/env/__libc_start_main.c:94:2 in 'libc_start_main_stage2'

Crystal 1.3.2 [932f193ae] (2022-01-18)

LLVM: 10.0.0 Default target: x86_64-unknown-linux-gnu