Skip to content

Instantly share code, notes, and snippets.

@ysbaddaden
Created August 29, 2024 09:55
Show Gist options
  • Save ysbaddaden/ad396b903db8fc8631e41011da0296a6 to your computer and use it in GitHub Desktop.
Save ysbaddaden/ad396b903db8fc8631e41011da0296a6 to your computer and use it in GitHub Desktop.
Crystal: debug codegen failures
diff --git a/src/compiler/crystal/compiler.cr b/src/compiler/crystal/compiler.cr
index 38880ee9e..966e2f954 100644
--- a/src/compiler/crystal/compiler.cr
+++ b/src/compiler/crystal/compiler.cr
@@ -523,13 +523,17 @@ module Crystal
private def fork_codegen(units, n_threads)
workers = fork_workers(n_threads) do |input, output|
while i = input.gets(chomp: true).presence
+ Crystal::System.print_error "CODEGEN pid=#{LibC.getpid} STDIN => #{i}\n"
unit = units[i.to_i]
+ Crystal::System.print_error "CODEGEN pid=#{LibC.getpid} COMPILE unit.name=#{unit.name}\n"
unit.compile
result = {name: unit.name, reused: unit.reused_previous_compilation?}
+ Crystal::System.print_error "CODEGEN pid=#{LibC.getpid} STDOUT <= #{result.to_json}\n"
output.puts result.to_json
end
rescue ex
result = {exception: {name: ex.class.name, message: ex.message, backtrace: ex.backtrace}}
+ Crystal::System.print_error "CODEGEN pid=#{LibC.getpid} EXCEPTION #{ex.inspect_with_backtrace}\n"
output.puts result.to_json
end
@@ -539,32 +543,35 @@ module Crystal
completed = Channel(Nil).new(n_threads)
workers.each do |pid, input, output|
- spawn do
+ spawn(name: "codegen-#{pid}") do
overqueued = 0
overqueue.times do
if (index = indexes.add(1)) < units.size
+ Crystal::System.print_error "MAIN => #{pid} STDIN <= #{index}\n"
input.puts index
overqueued += 1
end
end
while (index = indexes.add(1)) < units.size
+ Crystal::System.print_error "MAIN => #{pid} STDIN <= #{index}\n"
input.puts index
if response = output.gets(chomp: true)
channel.send response
else
- Crystal::System.print_error "\nBUG: a codegen process failed\n"
+ Crystal::System.print_error "\nBUG: a codegen process exited early\n"
exit 1
end
end
overqueued.times do
if response = output.gets(chomp: true)
+ Crystal::System.print_error "MAIN => #{pid} STDOUT => #{response}\n"
channel.send response
else
- Crystal::System.print_error "\nBUG: a codegen process failed\n"
+ Crystal::System.print_error "\nBUG: a codegen process exited early\n"
exit 1
end
end
@@ -574,11 +581,12 @@ module Crystal
output.close
Process.new(pid).wait
+ ensure
completed.send(nil)
end
end
- spawn do
+ spawn(name: "codegen-waiter") do
n_threads.times { completed.receive }
channel.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment