Skip to content

Add __crystal_raise_cast_failed for non-interpreted code#15708

Merged
straight-shoota merged 1 commit intocrystal-lang:masterfrom
HertzDevil:refactor/codegen-type-cast-error
Apr 27, 2025
Merged

Add __crystal_raise_cast_failed for non-interpreted code#15708
straight-shoota merged 1 commit intocrystal-lang:masterfrom
HertzDevil:refactor/codegen-type-cast-error

Conversation

@HertzDevil
Copy link
Contributor

@HertzDevil HertzDevil commented Apr 25, 2025

Resolves #15705.

This completely circumvents the slow MainVisitor invocation per compiler-generated raise(TypeCastError.new(...)) call by reimplementing the raise using a top-level fun plus LLVM instructions. It also partially addresses the issue where the codegen phase could theoretically instantiate new types due to the use of a MainVisitor (the only remaining place is #cant_pass_closure_to_c_exception_call).

There is now an internal ~type_id_to_class_name function that returns the type name corresponding to a given instance type ID. It works even when Class#name is not implemented in the standard library. This might be useful for other purposes such as debugger tooling.

Copy link
Collaborator

@ysbaddaden ysbaddaden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Such a lovely fix. One less codegen calls into semantics ❤️

@ysbaddaden ysbaddaden added this to the 1.17.0 milestone Apr 25, 2025
@straight-shoota straight-shoota modified the milestones: 1.17.0, 1.16.2 Apr 25, 2025
@straight-shoota straight-shoota merged commit e3c1224 into crystal-lang:master Apr 27, 2025
37 checks passed
@crysbot
Copy link
Collaborator

crysbot commented Apr 27, 2025

Successfully created backport PR for release/1.16:

@ysbaddaden
Copy link
Collaborator

I'm wondering if we could go one step further and move the expansion to the semantics pass, where it would belong better? Codegen would then merely generate, or interpret, whatever it's told to, and the LLVM codegen wouldn't need to map type ids to class names.

straight-shoota added a commit that referenced this pull request Apr 28, 2025
…15712)

Co-authored-by: Quinton Miller <nicetas.c@gmail.com>
Co-authored-by: Johannes Müller <straightshoota@gmail.com>
@HertzDevil HertzDevil deleted the refactor/codegen-type-cast-error branch May 1, 2025 04:20
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.

Compiling crystal itself with execution contexts makes crystal codegen 5 times slower

4 participants