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

[CIR][CodeGen] Add initial support for __cxa_rethrow #1290

Merged
merged 7 commits into from
Feb 11, 2025

Conversation

bruteforceboy
Copy link
Contributor

@bruteforceboy bruteforceboy commented Jan 23, 2025

This PR adds an initial support for __cxa_rethrow, and one test that produces a rethrow.

I am very open to suggestions regarding this PR, because I'm still a bit unsure if this replicates the original codegen properly. For example, using the test added, the OG CodeGen produces:

entry:
  invoke void @_ZN1SC2Ev(ptr noundef nonnull align 1 dereferenceable(1) %s)
          to label %invoke.cont unwind label %lpad

invoke.cont:                                      ; preds = %entry
  invoke void @__cxa_rethrow() #2
          to label %unreachable unwind label %lpad

lpad:                                             ; preds = %invoke.cont, %entry
  %0 = landingpad { ptr, i32 }
          catch ptr null
  %1 = extractvalue { ptr, i32 } %0, 0
  store ptr %1, ptr %exn.slot, align 8
  %2 = extractvalue { ptr, i32 } %0, 1
  store i32 %2, ptr %ehselector.slot, align 4
  br label %catch

catch:                                            ; preds = %lpad
  %exn = load ptr, ptr %exn.slot, align 8
  %3 = call ptr @__cxa_begin_catch(ptr %exn) #3
  %4 = load i32, ptr %r, align 4
  %inc = add nsw i32 %4, 1
  store i32 %inc, ptr %r, align 4
  call void @__cxa_end_catch()
  br label %try.cont

and the proposed CIR equivalent produces:

  invoke void @_ZN1SC2Ev(ptr %1)
          to label %5 unwind label %9

5:                                                ; preds = %4
  invoke void @__cxa_rethrow()
          to label %6 unwind label %13

6:                                                ; preds = %5
  br label %7

7:                                                ; preds = %6
  unreachable

8:                                                ; No predecessors!
  br label %22

9:                                                ; preds = %4
  %10 = landingpad { ptr, i32 }
          catch ptr null
  %11 = extractvalue { ptr, i32 } %10, 0
  %12 = extractvalue { ptr, i32 } %10, 1
  br label %17

13:                                               ; preds = %5
  %14 = landingpad { ptr, i32 }
          catch ptr null
  %15 = extractvalue { ptr, i32 } %14, 0
  %16 = extractvalue { ptr, i32 } %14, 1
  br label %17

17:                                               ; preds = %13, %9
  %18 = phi ptr [ %11, %9 ], [ %15, %13 ]
  %19 = call ptr @__cxa_begin_catch(ptr %18)
  %20 = load i32, ptr %2, align 4
  %21 = add i32 %20, 1
  store i32 %21, ptr %2, align 4
  call void @__cxa_end_catch()
  br label %22

There are quite a number of differences: phi in the CIR version VS loading from %exn.slot in the OG, having multiple landing pads, etc.

The CIR version still seems reasonable to me, mostly because currently we are unable to replicate the exact behavior of the OG codegen. Again, I am very open to more discussions and suggestions here)

Copy link
Member

@bcardosolopes bcardosolopes left a comment

Choose a reason for hiding this comment

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

Overall this looks good, some minor change needed.

There are quite a number of differences: phi in the CIR version VS loading from %exn.slot in the OG, having multiple landing pads, etc.

Yes, we use block arguments for the slots, and don't unique landing pads (yet). We might stay with the former but fix the later.

The CIR version still seems reasonable to me, mostly because currently we are unable to replicate the exact behavior of the OG codegen. Again, I am very open to more discussions and suggestions here)

I think it's similar enough!

@bcardosolopes
Copy link
Member

We just went over a rebase against upstream, apologies for the churn but please update your branch for this PR and force-push!

Copy link
Member

@bcardosolopes bcardosolopes left a comment

Choose a reason for hiding this comment

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

Awesome, thanks!

@bruteforceboy
Copy link
Contributor Author

bruteforceboy commented Feb 11, 2025

@bcardosolopes thanks for approving!

I am still a bit worried about the case where we ignore everything below the UnreachableOp (conversation for reference). I currently have a code snippet where I have some operations below the rethrow and this PR doesn't work quite fine.

You mentioned that it is a bigger problem in CIR. So, I am just curious what will be to correct way to fix this? Is there something wrong with the approach using blocks? I can open a new PR or modify this, but I want to know the right way to approach this.

@bcardosolopes
Copy link
Member

I am still a bit worried about the case where we ignore everything below the UnreachableOp (conversation for reference). I currently have a code snippet where I have some operations below the rethrow and this PR doesn't work quite fine.

I'm worried too, thanks for followin up with that!

You mentioned that it is a bigger problem in CIR. So, I am just curious what will be to correct way to fix this? Is there something wrong with the approach using blocks? I can open a new PR or modify this, but I want to know the right way to approach this.

A new PR would be great! Manipulating blocks sounds fine, we can go over specific concerns over reviews.

@bcardosolopes bcardosolopes merged commit 3017a00 into llvm:main Feb 11, 2025
6 checks passed
bcardosolopes pushed a commit that referenced this pull request Feb 20, 2025
This is the second part of
[PR#1290](#1290), adding initial
support for `__cxa_rethrow`.

So, the last PR did not support statements that come after the
`UnreachableOp` from the `rethrow`, we just ignored them, e.g:
```
struct S {
  S() {}
};

void foo() {
  int r = 1;
  try {
    throw;
    S s;
  } catch (...) {
    ++r;
  }
}
```

This PR fixes this. 

A few changes: 
- `rethrow` statements split their block into multiple blocks.
- Tests with statements that come after the `rethrow` were added and old
ones were modified.

Concern:
- The `ScopeOp` workaround still exists which I guess was one of the
main concerns when we tried in the last PR.

As usual, I am open to discussions on this and how to approach it better
-:)
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.

2 participants