Skip to content

Conversation

@AndyAyersMS
Copy link
Member

The count of switch targets does not include the default.

The count of switch targets does not include the default.
Copilot AI review requested due to automatic review settings January 23, 2026 22:40
@github-actions github-actions bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Jan 23, 2026
@AndyAyersMS
Copy link
Member Author

@dotnet/jit PTAL

We can now run examples with switches, like so:

    static int sw(int n)
    {
        switch (n)
        {
            case 1: case 3: return 10;
            case 2: case 4: return 20;
            case 5: case 7: return 30;
            case 6: case 8: return 40;
            default: return 0;
        }
    }

via @kg's harness

Loading test-module.wasm...
Compiling test-module.wasm...
Instantiating test-module.wasm...
OK!
exports=["wasm_test_Program__addTwoFloats","wasm_test_Program__addTwoInts","wasm_test_Program__derefBytePtr","wasm_test_Program__derefIntPtr","wasm_test_Program__increaseFloat","wasm_test_Program__increment","wasm_test_Program__select","wasm_test_Program__sum","wasm_test_Program__sw"]
running 'exports.wasm_test_Program__sw(/* sp */ 1024, 6, /* pep */ 0)...
result was 40

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes an off-by-one error in the RyuJIT WASM code generation for switch statements using the br_table instruction.

Changes:

  • Fixed the count parameter passed to emitIns_I for INS_br_table to correctly exclude the default case from the count


GetEmitter()->emitIns_I(INS_br_table, EA_4BYTE, caseCount);
GetEmitter()->emitIns_I(INS_br_table, EA_4BYTE, caseCount - 1);

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you potentially add a comment here explaining that this loop will handle emitting all the targets, including the default, even though in the above count we excluded it because of Wasm's convention? We could also consider pulling emitting the default out of the loop just to be extra clear here, though I know we also have the assert above to ensure we do have a default case.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. Take another look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch-wasm WebAssembly architecture area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants