Skip to content

[Patterns] Generate SwitchStatement in kernel AST when possible #51391

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

Closed
alexmarkov opened this issue Feb 13, 2023 · 0 comments
Closed

[Patterns] Generate SwitchStatement in kernel AST when possible #51391

alexmarkov opened this issue Feb 13, 2023 · 0 comments
Assignees
Labels
legacy-area-front-end Legacy: Use area-dart-model instead. P2 A bug or feature request we're likely to work on

Comments

@alexmarkov
Copy link
Contributor

AOT is able to generate switch statements over int and enums more efficiently using jump table or binary search. However, these optimizations are applied only to SwitchStatement kernel AST nodes.

With new patterns language feature, front-end now represents switch statements as a sequence of if statements, even if patterns are not used. As a result, switch optimizations are disabled in the new language version.

It would be nice if front-end would be able to generate old SwitchStatement kernel AST nodes if patterns are not used, at least for switch statements over int and enum expressions.

Example:

int x = int.parse('2');
main() {
  switch (x) {
    case 0: print(0); break;
    case 1: print(1); break;
    case 2: print(2); break;
    case 3: print(3); break;
    case 4: print(4); break;
    case 5: print(5); break;
    case 6: print(6); break;
    case 7: print(7); break;
    case 8: print(8); break;
    case 9: print(9); break;
    case 10: print(10); break;
    case 11: print(11); break;
    case 12: print(12); break;
    case 13: print(13); break;
    case 14: print(14); break;
    case 15: print(15); break;
  }
}

Kernel AST before patterns:

    switch(foo::x) {
      #L2:
      case #C1:
        {
          core::print(0);
          break #L1;
        }
      #L3:
      case #C2:
        {
          core::print(1);
          break #L1;
        }
...

Code generated by AOT compiler before patterns:

        ;; Branch if RelationalOp(>=, v145 T{_Smi}, v146) T{bool} goto (7, 8)
0x7fc34fc7f241    4883f900               cmpq rcx,0
0x7fc34fc7f245    0f8c5e010000           jl 0x00007fc34fc7f3a9
        ;; B7
        ;; Branch if RelationalOp(<=, v145 T{_Smi}, v147) T{bool} goto (9, 10)
0x7fc34fc7f24b    4883f90f               cmpq rcx,0xf
0x7fc34fc7f24f    0f8f54010000           jg 0x00007fc34fc7f3a9
        ;; B9
        ;; igoto:(v141 T{_Smi})
0x7fc34fc7f255    498b8faf4c0000         movq rcx,[pp+0x4caf]   _Int32List
0x7fc34fc7f25c    48634c4117             movsxdq rcx,[rcx+rax*2+0x17]
0x7fc34fc7f261    4c8d1d88ffffff         leaq tmp,[rip-0x78]
0x7fc34fc7f268    4c03d9                 addq tmp,rcx
0x7fc34fc7f26b    41ffe3                 jmp tmp

Kernel AST with --enable-experiment=patterns:

      if(#C1 =={core::num::==}{(core::Object) → core::bool} #0#0) {
        {
          core::print(0);
          break #L1;
        }
      }
      else
        if(#C2 =={core::num::==}{(core::Object) → core::bool} #0#0) {
          {
            core::print(1);
            break #L1;
          }
        }
        else
...

Code generated by AOT compiler with --enable-experiment=patterns:

        ;; Branch if EqualityCompare(v183 == v182 T{int}) T{bool} goto (3, 5)
0x7f19d0eff229    4883f800               cmpq rax,0
0x7f19d0eff22d    0f8514000000           jnz 0x00007f19d0eff247
...

        ;; Branch if EqualityCompare(v184 == v182 T{int}) T{bool} goto (6, 7)
0x7f19d0eff247    4883f801               cmpq rax,1
0x7f19d0eff24b    0f8514000000           jnz 0x00007f19d0eff265

...

@johnniwinther

@alexmarkov alexmarkov added the legacy-area-front-end Legacy: Use area-dart-model instead. label Feb 13, 2023
@johnniwinther johnniwinther self-assigned this Feb 13, 2023
@johnniwinther johnniwinther added the P2 A bug or feature request we're likely to work on label Feb 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
legacy-area-front-end Legacy: Use area-dart-model instead. P2 A bug or feature request we're likely to work on
Projects
None yet
Development

No branches or pull requests

2 participants