Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try/catch handling #36

Merged
merged 2 commits into from
Jan 16, 2020
Merged

Try/catch handling #36

merged 2 commits into from
Jan 16, 2020

Conversation

MikeInnes
Copy link
Member

@MikeInnes MikeInnes commented Jan 16, 2020

Representing try/catch in SSA form, and converting it back to the usual lowered-code form for code generation.

Previously IRTools had no notion of try/catch at all. I never tested this but presumably it blew up badly in those cases. Now try/catch works great with dynamos and tools like Zygote can handle it as they wish.

Example usage with interesting dataflow:

julia> function foo()
         x = 1
         y = 1
         try
           y = bar()
         catch e
           x, y
         end
         x, y
       end
foo (generic function with 2 methods)

julia> @code_ir foo()
1: (%1)
  %2 = try #2
  %3 = catch 2 (1, 1)
  %4 = Main.bar()
  %5 = catch 2 (1, %4)
  %6 = end try
  br 3 (1, %4)
2: (%7, %8)
  %9 = end try
  %10 = $(Expr(:the_exception))
  %11 = Core.tuple(%7, %8)
  %12 = pop exception %2
  br 3 (%7, %8)
3: (%13, %14)
  %15 = Core.tuple(%13, %14)
  return %15

try and end try correspond to the usual enter and leave. catch is a pseudo-branch that says "if an error is thrown after this point, branch to #2 with the following arguments". Those argument can change over the course of the try block, so multiple catch branches are allowed.

@aviatesk
Copy link
Contributor

@MikeInnes thanks for addressing this !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants