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

[WebAssembly] Treat 'rethrow' as terminator in custom isel #95967

Merged
merged 1 commit into from
Jun 19, 2024

Commits on Jun 18, 2024

  1. [WebAssembly] Treat 'rethrow' as terminator in custom isel

    `rethrow` instruction is a terminator, but when when its DAG is built in
    `SelectionDAGBuilder` in a custom routine, it was NOT treated as such.
    
    ```ll
    rethrow:                                          ; preds = %catch.start
      invoke void @llvm.wasm.rethrow() llvm#1 [ "funclet"(token %1) ]
              to label %unreachable unwind label %ehcleanup
    
    ehcleanup:                                        ; preds = %rethrow, %catch.dispatch
      %tmp = phi i32 [ 10, %catch.dispatch ], [ 20, %rethrow ]
      ...
    ```
    
    In this bitcode, because of the `phi`, a `CONST_I32` will be created
    in the `rethrow` BB. Without this patch, the DAG for the `rethrow` BB
    looks like this:
    ```
      t0: ch,glue = EntryToken
          t3: ch = CopyToReg t0, Register:i32 %9, Constant:i32<20>
          t5: ch = llvm.wasm.rethrow t0, TargetConstant:i32<12161>
        t6: ch = TokenFactor t3, t5
      t8: ch = br t6, BasicBlock:ch<unreachable 0x562532e43c50>
    ```
    Note that `CopyToReg` and `llvm.wasm.rethrow` don't have dependence so
    either can come first in the selected code, which can result in the code
    like
    ```mir
    bb.3.rethrow:
      RETHROW 0, implicit-def dead $arguments
      %9:i32 = CONST_I32 20, implicit-def dead $arguments
      BR %bb.6, implicit-def dead $arguments
    ```
    
    After this patch, `llvm.wasm.rethrow` is treated as a terminator, and
    the DAG will look like
    ```
            t0: ch,glue = EntryToken
          t3: ch = CopyToReg t0, Register:i32 %9, Constant:i32<20>
        t5: ch = llvm.wasm.rethrow t3, TargetConstant:i32<12161>
      t7: ch = br t5, BasicBlock:ch<unreachable 0x5555e3d32c70>
    ```
    Note that now `rethrow` takes a token from `CopyToReg`, so `rethrow` has
    to come after `CopyToReg`. And the resulting code will be
    ```mir
    bb.3.rethrow:
      %9:i32 = CONST_I32 20, implicit-def dead $arguments
      RETHROW 0, implicit-def dead $arguments
      BR %bb.6, implicit-def dead $arguments
    ```
    
    I'm not very familiar with the internals of `getRoot` vs.
    `getControlRoot`, but other terminator instructions seem to use the
    latter, and using it for `rethrow` too worked.
    aheejin committed Jun 18, 2024
    Configuration menu
    Copy the full SHA
    3de5602 View commit details
    Browse the repository at this point in the history