[clang] [CIR][WIP] Make BeginCatchOp target-independent (PR #190612)
Amr Hesham via cfe-commits
cfe-commits at lists.llvm.org
Sat Apr 18 06:11:23 PDT 2026
AmrDeveloper wrote:
> > I think we may have a bug when calculating the exn_ptr for BeginCatch, for example
>
> I think it's OK. I modified your example slightly to look like this:
>
> ```
> int foo() {
> int rv = 0;
> try { division(); } catch (int x) { rv = x; }
> return rv;
> }
>
> int foo2() {
> int rv = 0;
> try { division(); } catch (int *x) { rv = *x; }
> return rv;
> }
> ```
>
> That results in this CIR after flattening:
>
> ```
> cir.func no_inline dso_local @_Z3foov() {{.*}} {
> ...
> %1 = cir.alloca !s32i, !cir.ptr<!s32i>, ["rv", init] {alignment = 4 : i64}
> ...
> %3 = cir.alloca !s32i, !cir.ptr<!s32i>, ["x"]
> ...
> ^bb6(%6: !cir.eh_token): // pred: ^bb5
> %catch_token, %exn_ptr = cir.begin_catch %6 : !cir.eh_token -> (!cir.catch_token, !cir.ptr<!s32i>)
> cir.br ^bb7
> ^bb7: // pred: ^bb6
> %7 = cir.load align(4) %exn_ptr : !cir.ptr<!s32i>, !s32i
> cir.store align(4) %7, %3 : !s32i, !cir.ptr<!s32i>
> %8 = cir.load align(4) %3 : !cir.ptr<!s32i>, !s32i
> cir.store align(4) %8, %1 : !s32i, !cir.ptr<!s32i>
> cir.br ^bb8
> ^bb8: // pred: ^bb7
> cir.end_catch %catch_token : !cir.catch_token
> cir.br ^bb9
> ...
> }
>
> cir.func no_inline dso_local @_Z4foo2v() {{.(}} {
> ...
> ...
> %1 = cir.alloca !s32i, !cir.ptr<!s32i>, ["rv", init] {alignment = 4 : i64}
> ...
> %3 = cir.alloca !s32i, !cir.ptr<!s32i>, ["x"]
> ...
> ^bb6(%6: !cir.eh_token): // pred: ^bb5
> %catch_token, %exn_ptr = cir.begin_catch %6 : !cir.eh_token -> (!cir.catch_token, !cir.ptr<!s32i>)
> cir.br ^bb7
> ^bb7: // pred: ^bb6
> cir.store align(8) %exn_ptr, %3 : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
> %7 = cir.load deref align(8) %3 : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i>
> %8 = cir.load align(4) %7 : !cir.ptr<!s32i>, !s32i
> cir.store align(4) %8, %1 : !s32i, !cir.ptr<!s32i>
> cir.br ^bb8
> ^bb8: // pred: ^bb7
> cir.end_catch %catch_token : !cir.catch_token
> ...
> }
> ```
>
> So even though they both use `!cir.ptr<!s32i>` for the exception pointer, it gets interpreted correctly with regard to the source type.
In the current implementation (on main branch), it will be okay because we don't use the exn_ptr directly, we use the alloca that we created, and we store the exn_ptr using store/load-store/memcopy... on it, and the type of this alloca is a pointer of the type of the exception so it will always be correct :D.
But now we will use the exn_ptr directly in most of the cases (When we can), so we need to make the operation type accurate for the verifier or for other codegen parts when they use it, for example, to extract or dereference it.
I suggest making it `ptr<ExceptionTy>`, whatever the exception type is, we will make the exn_ptr a pointer to it.
https://github.com/llvm/llvm-project/pull/190612
More information about the cfe-commits
mailing list