Skip to content

Terrible codegen for AsSpan(stringLiteral) on Mono #111932

Open
@EgorBo

Description

@EgorBo

A small change in #110955 caused terrible regressions (>150 benchmarks) on Mono. I am going to workaround it in the codebase, but I guess it'd be nice to also optimize it in the mono runtime

    static ReadOnlySpan<byte> TwoDigitsCharsAsBytes => MemoryMarshal.AsBytes<char>(
                                    "00010203040506070809" +
                                    "10111213141516171819" +
                                    "20212223242526272829" +
                                    "30313233343536373839" +
                                    "40414243444546474849" +
                                    "50515253545556575859" +
                                    "60616263646566676869" +
                                    "70717273747576777879" +
                                    "80818283848586878889" +
                                    "90919293949596979899");


    static byte TestMethod(int i)
    {
        return TwoDigitsCharsAsBytes[i];
    }

Codegen on CoreCLR:

; Method Benchmarks:TestMethod(int):ubyte (FullOpts)
       sub      rsp, 40
       mov      rax, 0x2C88020576C
       cmp      ecx, 400
       jae      SHORT G_M2810_IG04
       mov      ecx, ecx
       movzx    rax, byte  ptr [rax+rcx]
       add      rsp, 40
       ret      
G_M2810_IG04:
       call     CORINFO_HELP_RNGCHKFAIL
       int3     
; Total bytes of code: 39

Codegen on Mono (JIT, x64):

*** ASM for Benchmarks:TestMethod (int) ***
/tmp/.OJH5YA:     file format elf64-x86-64
Disassembly of section .text:
0000000000000000 <chmarks_TestMethod__int_>:
chmarks_TestMethod__int_():
   0:	48 81 ec 88 00 00 00 	sub    $0x88,%rsp
   7:	4c 89 34 24          	mov    %r14,(%rsp)
   b:	48 89 7c 24 78       	mov    %rdi,0x78(%rsp)
  10:	48 b8 78 4e 5f 0c 2f 	movabs $0x7c2f0c5f4e78,%rax
  17:	7c 00 00 
  1a:	f7 00 01 00 00 00    	testl  $0x1,(%rax)
  20:	74 06                	je     28 <chmarks_TestMethod__int_+0x28>
  22:	90                   	nop
  23:	e8 6c a8 f3 ff       	call   fffffffffff3a894 <chmarks_TestMethod__int_+0xfffffffffff3a894>
<BB>:2
  28:	33 c0                	xor    %eax,%eax
  2a:	48 89 44 24 68       	mov    %rax,0x68(%rsp)
  2f:	48 89 44 24 70       	mov    %rax,0x70(%rsp)
  34:	49 be 30 01 a7 0c 2f 	movabs $0x7c2f0ca70130,%r14
  3b:	7c 00 00 
  3e:	33 c0                	xor    %eax,%eax
  40:	48 89 44 24 48       	mov    %rax,0x48(%rsp)
  45:	48 89 44 24 50       	mov    %rax,0x50(%rsp)
  4a:	4d 85 f6             	test   %r14,%r14
  4d:	75 25                	jne    74 <chmarks_TestMethod__int_+0x74>
<BB>:15
  4f:	33 c0                	xor    %eax,%eax
  51:	48 89 44 24 48       	mov    %rax,0x48(%rsp)
  56:	48 89 44 24 50       	mov    %rax,0x50(%rsp)
  5b:	48 8b 44 24 48       	mov    0x48(%rsp),%rax
  60:	48 89 44 24 58       	mov    %rax,0x58(%rsp)
  65:	48 8b 44 24 50       	mov    0x50(%rsp),%rax
  6a:	48 89 44 24 60       	mov    %rax,0x60(%rsp)
  6f:	e9 3d 00 00 00       	jmp    b1 <chmarks_TestMethod__int_+0xb1>
<BB>:14
  74:	45 38 36             	cmp    %r14b,(%r14)
  77:	4d 85 f6             	test   %r14,%r14
  7a:	0f 84 e8 00 00 00    	je     168 <chmarks_TestMethod__int_+0x168>
  80:	49 8d 4e 14          	lea    0x14(%r14),%rcx
  84:	49 63 46 10          	movslq 0x10(%r14),%rax
  88:	33 d2                	xor    %edx,%edx
  8a:	48 89 54 24 38       	mov    %rdx,0x38(%rsp)
  8f:	48 89 54 24 40       	mov    %rdx,0x40(%rsp)
  94:	48 89 4c 24 38       	mov    %rcx,0x38(%rsp)
  99:	89 44 24 40          	mov    %eax,0x40(%rsp)
  9d:	48 8b 44 24 38       	mov    0x38(%rsp),%rax
  a2:	48 89 44 24 58       	mov    %rax,0x58(%rsp)
  a7:	48 8b 44 24 40       	mov    0x40(%rsp),%rax
  ac:	48 89 44 24 60       	mov    %rax,0x60(%rsp)
<BB>:11
  b1:	48 8b 44 24 58       	mov    0x58(%rsp),%rax
  b6:	48 89 44 24 28       	mov    %rax,0x28(%rsp)
  bb:	48 8b 44 24 60       	mov    0x60(%rsp),%rax
  c0:	48 89 44 24 30       	mov    %rax,0x30(%rsp)
  c5:	48 8b 44 24 28       	mov    0x28(%rsp),%rax
  ca:	48 89 44 24 18       	mov    %rax,0x18(%rsp)
  cf:	48 8b 44 24 30       	mov    0x30(%rsp),%rax
  d4:	48 89 44 24 20       	mov    %rax,0x20(%rsp)
  d9:	48 8b 4c 24 18       	mov    0x18(%rsp),%rcx
  de:	48 63 44 24 30       	movslq 0x30(%rsp),%rax
  e3:	ba 02 00 00 00       	mov    $0x2,%edx
  e8:	0f af c2             	imul   %edx,%eax
  eb:	0f 80 67 00 00 00    	jo     158 <chmarks_TestMethod__int_+0x158>
  f1:	33 d2                	xor    %edx,%edx
  f3:	48 89 54 24 08       	mov    %rdx,0x8(%rsp)
  f8:	48 89 54 24 10       	mov    %rdx,0x10(%rsp)
  fd:	48 89 4c 24 08       	mov    %rcx,0x8(%rsp)
 102:	89 44 24 10          	mov    %eax,0x10(%rsp)
 106:	48 8b 44 24 08       	mov    0x8(%rsp),%rax
 10b:	48 89 44 24 68       	mov    %rax,0x68(%rsp)
 110:	48 8b 44 24 10       	mov    0x10(%rsp),%rax
 115:	48 89 44 24 70       	mov    %rax,0x70(%rsp)
 11a:	48 8b 44 24 68       	mov    0x68(%rsp),%rax
 11f:	48 63 4c 24 78       	movslq 0x78(%rsp),%rcx
 124:	48 63 c9             	movslq %ecx,%rcx
 127:	48 63 54 24 70       	movslq 0x70(%rsp),%rdx
 12c:	48 3b d1             	cmp    %rcx,%rdx
 12f:	0f 86 12 00 00 00    	jbe    147 <chmarks_TestMethod__int_+0x147>
 135:	48 03 c1             	add    %rcx,%rax
 138:	0f b6 00             	movzbl (%rax),%eax
<BB>:1
 13b:	4c 8b 34 24          	mov    (%rsp),%r14
 13f:	48 81 c4 88 00 00 00 	add    $0x88,%rsp
 146:	c3                   	ret
 147:	be 29 00 00 00       	mov    $0x29,%esi
 14c:	bf 0b 01 00 00       	mov    $0x10b,%edi
 151:	66 90                	xchg   %ax,%ax
 153:	e8 38 2b 5c 01       	call   15c2c90 <chmarks_TestMethod__int_+0x15c2c90>
 158:	be 7d 00 00 00       	mov    $0x7d,%esi
 15d:	bf 51 01 00 00       	mov    $0x151,%edi
 162:	90                   	nop
 163:	e8 28 2b 5c 01       	call   15c2c90 <chmarks_TestMethod__int_+0x15c2c90>
 168:	be fe 00 00 00       	mov    $0xfe,%esi
 16d:	bf 3e 01 00 00       	mov    $0x13e,%edi
 172:	90                   	nop
 173:	e8 18 2b 5c 01       	call   15c2c90 <chmarks_TestMethod__int_+0x15c2c90>

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions