dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.11k stars 1.57k forks source link

VM machine code for `list[someInt & 0xff]` seems to have meaningless stuff #56828

Open jensjoha opened 2 hours ago

jensjoha commented 2 hours ago

Annotating SimpleToken.type with @pragma("vm:never-inline") so I can easily get the disassembly I can extract this code from an AOT build:

102678  ParallelMove rdx <- C, rcx <- C
   0x000000000024bcf0 <+0>:     mov    0x3f97(%r15),%rdx
   0x000000000024bcf7 <+7>:     mov    $0xff,%ecx

102679  v4 <- LoadField(v2 . _typeAndOffset@39236933) [-9223372036854775808, 9223372036854775807] int64
   0x000000000024bcfc <+12>:    mov    0x1f(%rdi),%rsi

102680  ParallelMove rsi <- rsi
102681  v20 <- IntConverter(int64->uint32[tr], v4) uint32
   0x000000000024bd00 <+16>:    mov    %esi,%esi

102682  ParallelMove rsi <- rsi
102683  v6 <- BinaryUint32Op(& [tr], v20 T{int}, v22 T{_Smi}) [0, 255] uint32
   0x000000000024bd02 <+18>:    and    %ecx,%esi

102684  ParallelMove rsi <- rsi
102685  v21 <- IntConverter(uint32->int64, v6) int64
   0x000000000024bd04 <+20>:    mov    %esi,%esi

102686  ParallelMove rax <- C, rbx <- rsi
   0x000000000024bd06 <+22>:    mov    %rsi,%rbx
   0x000000000024bd09 <+25>:    mov    $0x9b,%eax

102687  GenericCheckBound:14(v16 T{_Smi}, v21 T{_Smi}) [-9223372036854775808, 9223372036854775807] int64
   0x000000000024bd0e <+30>:    cmp    %rax,%rbx
   0x000000000024bd11 <+33>:    jae    0x24bd1d <SimpleToken.type+45>

102688  v17 <- LoadIndexed:14([_List] v3, v21 T{_Smi}) T{TokenType}
   0x000000000024bd17 <+39>:    mov    0x17(%rdx,%rsi,8),%rax

102689  ParallelMove rax <- rax
102690  DartReturn:16(v17 T{TokenType})
   0x000000000024bd1c <+44>:    ret

102691  slow path check bound operation
102692  PrologueOffset = 45
   0x000000000024bd1d <+45>:    push   %rbp
   0x000000000024bd1e <+46>:    mov    %rsp,%rbp
   0x000000000024bd21 <+49>:    call   0x6de888 <stub _iso_stub_RangeErrorSharedWithoutFPURegsStub>
End of assembler dump.

which seems massive. Wouldn't something like this do the same thing?

   0x000000000024bcf0 <+0>:     mov    0x3f97(%r15),%rdx
   0x000000000024bcfc <+12>:    mov    0x1f(%rdi),%rsi
   0x000000000024bd00 <+16>:    mov    %sil,%sil
   0x000000000024bd0e <+30>:    cmp    $0x9b,%rsi
   0x000000000024bd11 <+33>:    jae    0x24bd1d <SimpleToken.type+45>
   0x000000000024bd17 <+39>:    mov    0x17(%rdx,%rsi,8),%rax
   0x000000000024bd1c <+44>:    ret

102691  slow path check bound operation
102692  PrologueOffset = 45
   [whatever to make stuff go into the right registries to make the stub call work]
   0x000000000024bd21 <+49>:    call   0x6de888 <stub _iso_stub_RangeErrorSharedWithoutFPURegsStub>
End of assembler dump.

(Helper for at least myself: https://cs.brown.edu/courses/cs033/docs/guides/x64_cheatsheet.pdf)

Maybe someone who actually knows something about the VM and assembly and stuff might come up with something better.

/cc @mraleph

dart-github-bot commented 2 hours ago

Summary: The user observes that the VM generates complex machine code for a simple list access operation with a bitwise AND. They believe the code could be simplified, potentially leading to performance improvements.