Skip to content

Add function name to CRYSTAL_DEBUG_CODEGEN log helper#15506

Merged
straight-shoota merged 1 commit intocrystal-lang:masterfrom
HertzDevil:feature/debug-codegen-function-name
Feb 23, 2025
Merged

Add function name to CRYSTAL_DEBUG_CODEGEN log helper#15506
straight-shoota merged 1 commit intocrystal-lang:masterfrom
HertzDevil:feature/debug-codegen-function-name

Conversation

@HertzDevil
Copy link
Contributor

When diagnosing bad code generation, it is helpful to know whether a program actually reached an unreachable instruction, because this is usually where observable undefined behavior stems from. There is already a little-known way, which is setting the environment variable CRYSTAL_DEBUG_CODEGEN=1:

lib LibC
  # actually returns Int32
  fun puts(str : UInt8*) : NoReturn
end

def foo
  LibC.puts "a"
end

foo
# since `LibC.puts` and `foo` are not actually `NoReturn`, the program will
# reach this line where an `unreachable` instruction is emitted
a
<block: entry @ C:\M\B\src\mingw-w64-ucrt-x86_64-crystal-1.15.1\src\compiler\crystal\codegen\call.cr:528> Reached the unreachable!
Program hit a breakpoint and no debugger was attached

The log includes the LLVM basic block name, as well as the source location of the compiler call that builds the unreachable. However, this is insufficient to pinpoint the unreachable in the LLVM IR, since the block name can be something very generic, e.g. entry or current_def3. This PR adds the LLVM function name as well; all functions have unique names, as do basic blocks within a single function. The above now prints:

<function=*foo:NoReturn block=entry source=src\compiler\crystal\codegen\call.cr:537> Reached the unreachable!

@straight-shoota straight-shoota added this to the 1.16.0 milestone Feb 22, 2025
@straight-shoota straight-shoota merged commit 4f3b503 into crystal-lang:master Feb 23, 2025
42 checks passed
@HertzDevil HertzDevil deleted the feature/debug-codegen-function-name branch March 6, 2025 23:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants