Port assembly code for JIT_MemSet and JIT_MemCpy to Linux and Mac
These VM functions are needed to unblock implementation of System.IO.FileSystem.dll. Windows implementation of these assembly routines is located in coreclr\src\vm\amd64\crthelpers.asm. We need to create a corresponding file for Linux (i.e. vm/amd64/crthelpers.S – please note that the extension must be uppercase ‘S’) and implement it these functions in this file.
Hi @sergiy-k I had some time tonight so I went ahead and did this:
https://github.com/kangaroo/coreclr/compare/issue-199
I haven't done extensive testing yet to ensure I didn't fat finger anything.
@kangaroo Wow! That was quick! Thank you! I will review it the morning when I can actually pay attention to details :) and test it. Unless, of course, @janvorli gets to it first :)
I guess we'll have the short vs long jumps on Mac issue here.
.byte 0x66, 0x66, 0x66, 0x90
.byte 0x66, 0x66, 0x90
mset70: cmp r9, CACHE_LIMIT_MEMSET / 64 // check if large fill
jae mset90 // if ae, large fill
Here the jae must be short so that the nop formed by the previous .byte "eats" both the cmp and jae and also make the mset80 aligned on 16 bytes.
Preserving the alignment at other loops is also important for the performance and there are many short jumps in this code.
Here is a disassembly of the windows version so that you can compare it to the result you get on Mac:
coreclr!JIT_MemSet [D:\ProjectK\src\NDP\clr\src\vm\amd64\CrtHelpers.asm @ 74]:
00000001`806c1270 488bc1 mov rax,rcx
00000001`806c1273 4983f808 cmp r8,8
00000001`806c1277 7263 jb coreclr!JIT_MemSet+0x6c (00000001`806c12dc)
00000001`806c1279 0fb6d2 movzx edx,dl
00000001`806c127c 49b90101010101010101 mov r9,101010101010101h
00000001`806c1286 490fafd1 imul rdx,r9
00000001`806c128a 4983f840 cmp r8,40h
00000001`806c128e 7221 jb coreclr!JIT_MemSet+0x41 (00000001`806c12b1)
00000001`806c1290 48f7d9 neg rcx
00000001`806c1293 83e107 and ecx,7
00000001`806c1296 7406 je coreclr!JIT_MemSet+0x2e (00000001`806c129e)
00000001`806c1298 4c2bc1 sub r8,rcx
00000001`806c129b 488910 mov qword ptr [rax],rdx
00000001`806c129e 4803c8 add rcx,rax
00000001`806c12a1 4d8bc8 mov r9,r8
00000001`806c12a4 4983e03f and r8,3Fh
00000001`806c12a8 49c1e906 shr r9,6
00000001`806c12ac 4d85c9 test r9,r9
00000001`806c12af 7546 jne coreclr!JIT_MemSet+0x87 (00000001`806c12f7)
00000001`806c12b1 4d8bc8 mov r9,r8
00000001`806c12b4 4983e007 and r8,7
00000001`806c12b8 49c1e903 shr r9,3
00000001`806c12bc 4d85c9 test r9,r9
00000001`806c12bf 741b je coreclr!JIT_MemSet+0x6c (00000001`806c12dc)
00000001`806c12c1 666666666666660f1f840000000000 nop word ptr [rax+rax]
00000001`806c12d0 488911 mov qword ptr [rcx],rdx
00000001`806c12d3 4883c108 add rcx,8
00000001`806c12d7 49ffc9 dec r9
00000001`806c12da 75f4 jne coreclr!JIT_MemSet+0x60 (00000001`806c12d0)
00000001`806c12dc 4d85c0 test r8,r8
00000001`806c12df 740a je coreclr!JIT_MemSet+0x7b (00000001`806c12eb)
00000001`806c12e1 8811 mov byte ptr [rcx],dl
00000001`806c12e3 48ffc1 inc rcx
00000001`806c12e6 49ffc8 dec r8
00000001`806c12e9 75f6 jne coreclr!JIT_MemSet+0x71 (00000001`806c12e1)
00000001`806c12eb f3c3 rep ret
00000001`806c12ed 0f1f00 nop dword ptr [rax]
00000001`806c12f0 66666690 xchg ax,ax
00000001`806c12f4 666690 xchg ax,ax
00000001`806c12f7 4981f9001c0000 cmp r9,1C00h
00000001`806c12fe 7330 jae coreclr!JIT_MemSet+0xc0 (00000001`806c1330)
00000001`806c1300 488911 mov qword ptr [rcx],rdx
00000001`806c1303 48895108 mov qword ptr [rcx+8],rdx
00000001`806c1307 48895110 mov qword ptr [rcx+10h],rdx
00000001`806c130b 4883c140 add rcx,40h
00000001`806c130f 488951d8 mov qword ptr [rcx-28h],rdx
00000001`806c1313 488951e0 mov qword ptr [rcx-20h],rdx
00000001`806c1317 49ffc9 dec r9
00000001`806c131a 488951e8 mov qword ptr [rcx-18h],rdx
00000001`806c131e 488951f0 mov qword ptr [rcx-10h],rdx
00000001`806c1322 488951f8 mov qword ptr [rcx-8],rdx
00000001`806c1326 75d8 jne coreclr!JIT_MemSet+0x90 (00000001`806c1300)
00000001`806c1328 eb87 jmp coreclr!JIT_MemSet+0x41 (00000001`806c12b1)
00000001`806c132a 660f1f440000 nop word ptr [rax+rax]
00000001`806c1330 480fc311 movnti qword ptr [rcx],rdx
00000001`806c1334 480fc35108 movnti qword ptr [rcx+8],rdx
00000001`806c1339 480fc35110 movnti qword ptr [rcx+10h],rdx
00000001`806c133e 4883c140 add rcx,40h
00000001`806c1342 480fc351d8 movnti qword ptr [rcx-28h],rdx
00000001`806c1347 480fc351e0 movnti qword ptr [rcx-20h],rdx
00000001`806c134c 49ffc9 dec r9
00000001`806c134f 480fc351e8 movnti qword ptr [rcx-18h],rdx
00000001`806c1354 480fc351f0 movnti qword ptr [rcx-10h],rdx
00000001`806c1359 480fc351f8 movnti qword ptr [rcx-8],rdx
00000001`806c135e 75d0 jne coreclr!JIT_MemSet+0xc0 (00000001`806c1330)
00000001`806c1360 f0800c2400 lock or byte ptr [rsp],0
00000001`806c1365 e947ffffff jmp coreclr!JIT_MemSet+0x41 (00000001`806c12b1)
coreclr!JIT_MemSet_End [D:\ProjectK\src\NDP\clr\src\vm\amd64\CrtHelpers.asm @ 181]:
00000001`806c136a 90 nop
00000001`806c136b 0f1f440000 nop dword ptr [rax+rax]
coreclr!JIT_MemCpy [D:\ProjectK\src\NDP\clr\src\vm\amd64\CrtHelpers.asm @ 215]:
00000001`806c1370 4c8bd9 mov r11,rcx
00000001`806c1373 482bd1 sub rdx,rcx
00000001`806c1376 0f82ae010000 jb coreclr!JIT_MemCpy+0x1ba (00000001`806c152a)
00000001`806c137c 4983f808 cmp r8,8
00000001`806c1380 7272 jb coreclr!JIT_MemCpy+0x84 (00000001`806c13f4)
00000001`806c1382 f6c107 test cl,7
00000001`806c1385 7436 je coreclr!JIT_MemCpy+0x4d (00000001`806c13bd)
00000001`806c1387 f6c101 test cl,1
00000001`806c138a 740b je coreclr!JIT_MemCpy+0x27 (00000001`806c1397)
00000001`806c138c 8a040a mov al,byte ptr [rdx+rcx]
00000001`806c138f 49ffc8 dec r8
00000001`806c1392 8801 mov byte ptr [rcx],al
00000001`806c1394 48ffc1 inc rcx
00000001`806c1397 f6c102 test cl,2
00000001`806c139a 740f je coreclr!JIT_MemCpy+0x3b (00000001`806c13ab)
00000001`806c139c 668b040a mov ax,word ptr [rdx+rcx]
00000001`806c13a0 4983e802 sub r8,2
00000001`806c13a4 668901 mov word ptr [rcx],ax
00000001`806c13a7 4883c102 add rcx,2
00000001`806c13ab f6c104 test cl,4
00000001`806c13ae 740d je coreclr!JIT_MemCpy+0x4d (00000001`806c13bd)
00000001`806c13b0 8b040a mov eax,dword ptr [rdx+rcx]
00000001`806c13b3 4983e804 sub r8,4
00000001`806c13b7 8901 mov dword ptr [rcx],eax
00000001`806c13b9 4883c104 add rcx,4
00000001`806c13bd 4d8bc8 mov r9,r8
00000001`806c13c0 49c1e905 shr r9,5
00000001`806c13c4 4d85c9 test r9,r9
00000001`806c13c7 755e jne coreclr!JIT_MemCpy+0xb7 (00000001`806c1427)
00000001`806c13c9 0f1f8000000000 nop dword ptr [rax]
00000001`806c13d0 4d8bc8 mov r9,r8
00000001`806c13d3 49c1e903 shr r9,3
00000001`806c13d7 4d85c9 test r9,r9
00000001`806c13da 7418 je coreclr!JIT_MemCpy+0x84 (00000001`806c13f4)
00000001`806c13dc 0f1f4000 nop dword ptr [rax]
00000001`806c13e0 488b040a mov rax,qword ptr [rdx+rcx]
00000001`806c13e4 488901 mov qword ptr [rcx],rax
00000001`806c13e7 4883c108 add rcx,8
00000001`806c13eb 49ffc9 dec r9
00000001`806c13ee 75f0 jne coreclr!JIT_MemCpy+0x70 (00000001`806c13e0)
00000001`806c13f0 4983e007 and r8,7
00000001`806c13f4 4d85c0 test r8,r8
00000001`806c13f7 7507 jne coreclr!JIT_MemCpy+0x90 (00000001`806c1400)
00000001`806c13f9 498bc3 mov rax,r11
00000001`806c13fc c3 ret
00000001`806c13fd 0f1f00 nop dword ptr [rax]
00000001`806c1400 8a040a mov al,byte ptr [rdx+rcx]
00000001`806c1403 8801 mov byte ptr [rcx],al
00000001`806c1405 48ffc1 inc rcx
00000001`806c1408 49ffc8 dec r8
00000001`806c140b 75f3 jne coreclr!JIT_MemCpy+0x90 (00000001`806c1400)
00000001`806c140d 498bc3 mov rax,r11
00000001`806c1410 c3 ret
00000001`806c1411 666666666666660f1f840000000000 nop word ptr [rax+rax]
00000001`806c1420 66666690 xchg ax,ax
00000001`806c1424 666690 xchg ax,ax
00000001`806c1427 4981f900200000 cmp r9,2000h
00000001`806c142e 7342 jae coreclr!JIT_MemCpy+0x102 (00000001`806c1472)
00000001`806c1430 488b040a mov rax,qword ptr [rdx+rcx]
00000001`806c1434 4c8b540a08 mov r10,qword ptr [rdx+rcx+8]
00000001`806c1439 4883c120 add rcx,20h
00000001`806c143d 488941e0 mov qword ptr [rcx-20h],rax
00000001`806c1441 4c8951e8 mov qword ptr [rcx-18h],r10
00000001`806c1445 488b440af0 mov rax,qword ptr [rdx+rcx-10h]
00000001`806c144a 4c8b540af8 mov r10,qword ptr [rdx+rcx-8]
00000001`806c144f 49ffc9 dec r9
00000001`806c1452 488941f0 mov qword ptr [rcx-10h],rax
00000001`806c1456 4c8951f8 mov qword ptr [rcx-8],r10
00000001`806c145a 75d4 jne coreclr!JIT_MemCpy+0xc0 (00000001`806c1430)
00000001`806c145c 4983e01f and r8,1Fh
00000001`806c1460 e96bffffff jmp coreclr!JIT_MemCpy+0x60 (00000001`806c13d0)
00000001`806c1465 6666660f1f840000000000 nop word ptr [rax+rax]
00000001`806c1470 6690 xchg ax,ax
00000001`806c1472 4881fa00100000 cmp rdx,1000h
00000001`806c1479 72b5 jb coreclr!JIT_MemCpy+0xc0 (00000001`806c1430)
00000001`806c147b b820000000 mov eax,20h
00000001`806c1480 0f18040a prefetchnta [rdx+rcx]
00000001`806c1484 0f18440a40 prefetchnta [rdx+rcx+40h]
00000001`806c1489 4881c180000000 add rcx,80h
00000001`806c1490 ffc8 dec eax
00000001`806c1492 75ec jne coreclr!JIT_MemCpy+0x110 (00000001`806c1480)
00000001`806c1494 4881e900100000 sub rcx,1000h
00000001`806c149b b840000000 mov eax,40h
00000001`806c14a0 4c8b0c0a mov r9,qword ptr [rdx+rcx]
00000001`806c14a4 4c8b540a08 mov r10,qword ptr [rdx+rcx+8]
00000001`806c14a9 4c0fc309 movnti qword ptr [rcx],r9
00000001`806c14ad 4c0fc35108 movnti qword ptr [rcx+8],r10
00000001`806c14b2 4c8b4c0a10 mov r9,qword ptr [rdx+rcx+10h]
00000001`806c14b7 4c8b540a18 mov r10,qword ptr [rdx+rcx+18h]
00000001`806c14bc 4c0fc34910 movnti qword ptr [rcx+10h],r9
00000001`806c14c1 4c0fc35118 movnti qword ptr [rcx+18h],r10
00000001`806c14c6 4c8b4c0a20 mov r9,qword ptr [rdx+rcx+20h]
00000001`806c14cb 4c8b540a28 mov r10,qword ptr [rdx+rcx+28h]
00000001`806c14d0 4883c140 add rcx,40h
00000001`806c14d4 4c0fc349e0 movnti qword ptr [rcx-20h],r9
00000001`806c14d9 4c0fc351e8 movnti qword ptr [rcx-18h],r10
00000001`806c14de 4c8b4c0af0 mov r9,qword ptr [rdx+rcx-10h]
00000001`806c14e3 4c8b540af8 mov r10,qword ptr [rdx+rcx-8]
00000001`806c14e8 ffc8 dec eax
00000001`806c14ea 4c0fc349f0 movnti qword ptr [rcx-10h],r9
00000001`806c14ef 4c0fc351f8 movnti qword ptr [rcx-8],r10
00000001`806c14f4 75aa jne coreclr!JIT_MemCpy+0x130 (00000001`806c14a0)
00000001`806c14f6 4981e800100000 sub r8,1000h
00000001`806c14fd 4981f800100000 cmp r8,1000h
00000001`806c1504 0f8371ffffff jae coreclr!JIT_MemCpy+0x10b (00000001`806c147b)
00000001`806c150a f0800c2400 lock or byte ptr [rsp],0
00000001`806c150f e9a9feffff jmp coreclr!JIT_MemCpy+0x4d (00000001`806c13bd)
00000001`806c1514 666666660f1f840000000000 nop word ptr [rax+rax]
00000001`806c1520 66666690 xchg ax,ax
00000001`806c1524 66666690 xchg ax,ax
00000001`806c1528 6690 xchg ax,ax
00000001`806c152a 4903c8 add rcx,r8
00000001`806c152d 4983f808 cmp r8,8
00000001`806c1531 7271 jb coreclr!JIT_MemCpy+0x234 (00000001`806c15a4)
00000001`806c1533 f6c107 test cl,7
00000001`806c1536 7436 je coreclr!JIT_MemCpy+0x1fe (00000001`806c156e)
00000001`806c1538 f6c101 test cl,1
00000001`806c153b 740b je coreclr!JIT_MemCpy+0x1d8 (00000001`806c1548)
00000001`806c153d 48ffc9 dec rcx
00000001`806c1540 8a040a mov al,byte ptr [rdx+rcx]
00000001`806c1543 49ffc8 dec r8
00000001`806c1546 8801 mov byte ptr [rcx],al
00000001`806c1548 f6c102 test cl,2
00000001`806c154b 740f je coreclr!JIT_MemCpy+0x1ec (00000001`806c155c)
00000001`806c154d 4883e902 sub rcx,2
00000001`806c1551 668b040a mov ax,word ptr [rdx+rcx]
00000001`806c1555 4983e802 sub r8,2
00000001`806c1559 668901 mov word ptr [rcx],ax
00000001`806c155c f6c104 test cl,4
00000001`806c155f 740d je coreclr!JIT_MemCpy+0x1fe (00000001`806c156e)
00000001`806c1561 4883e904 sub rcx,4
00000001`806c1565 8b040a mov eax,dword ptr [rdx+rcx]
00000001`806c1568 4983e804 sub r8,4
00000001`806c156c 8901 mov dword ptr [rcx],eax
00000001`806c156e 4d8bc8 mov r9,r8
00000001`806c1571 49c1e905 shr r9,5
00000001`806c1575 4d85c9 test r9,r9
00000001`806c1578 755d jne coreclr!JIT_MemCpy+0x267 (00000001`806c15d7)
00000001`806c157a 660f1f440000 nop word ptr [rax+rax]
00000001`806c1580 4d8bc8 mov r9,r8
00000001`806c1583 49c1e903 shr r9,3
00000001`806c1587 4d85c9 test r9,r9
00000001`806c158a 7418 je coreclr!JIT_MemCpy+0x234 (00000001`806c15a4)
00000001`806c158c 0f1f4000 nop dword ptr [rax]
00000001`806c1590 4883e908 sub rcx,8
00000001`806c1594 488b040a mov rax,qword ptr [rdx+rcx]
00000001`806c1598 49ffc9 dec r9
00000001`806c159b 488901 mov qword ptr [rcx],rax
00000001`806c159e 75f0 jne coreclr!JIT_MemCpy+0x220 (00000001`806c1590)
00000001`806c15a0 4983e007 and r8,7
00000001`806c15a4 4d85c0 test r8,r8
00000001`806c15a7 7507 jne coreclr!JIT_MemCpy+0x240 (00000001`806c15b0)
00000001`806c15a9 498bc3 mov rax,r11
00000001`806c15ac c3 ret
00000001`806c15ad 0f1f00 nop dword ptr [rax]
00000001`806c15b0 48ffc9 dec rcx
00000001`806c15b3 8a040a mov al,byte ptr [rdx+rcx]
00000001`806c15b6 49ffc8 dec r8
00000001`806c15b9 8801 mov byte ptr [rcx],al
00000001`806c15bb 75f3 jne coreclr!JIT_MemCpy+0x240 (00000001`806c15b0)
00000001`806c15bd 498bc3 mov rax,r11
00000001`806c15c0 c3 ret
00000001`806c15c1 666666666666660f1f840000000000 nop word ptr [rax+rax]
00000001`806c15d0 66666690 xchg ax,ax
00000001`806c15d4 666690 xchg ax,ax
00000001`806c15d7 4981f900200000 cmp r9,2000h
00000001`806c15de 7342 jae coreclr!JIT_MemCpy+0x2b2 (00000001`806c1622)
00000001`806c15e0 488b440af8 mov rax,qword ptr [rdx+rcx-8]
00000001`806c15e5 4c8b540af0 mov r10,qword ptr [rdx+rcx-10h]
00000001`806c15ea 4883e920 sub rcx,20h
00000001`806c15ee 48894118 mov qword ptr [rcx+18h],rax
00000001`806c15f2 4c895110 mov qword ptr [rcx+10h],r10
00000001`806c15f6 488b440a08 mov rax,qword ptr [rdx+rcx+8]
00000001`806c15fb 4c8b140a mov r10,qword ptr [rdx+rcx]
00000001`806c15ff 49ffc9 dec r9
00000001`806c1602 48894108 mov qword ptr [rcx+8],rax
00000001`806c1606 4c8911 mov qword ptr [rcx],r10
00000001`806c1609 75d5 jne coreclr!JIT_MemCpy+0x270 (00000001`806c15e0)
00000001`806c160b 4983e01f and r8,1Fh
00000001`806c160f e96cffffff jmp coreclr!JIT_MemCpy+0x210 (00000001`806c1580)
00000001`806c1614 666666660f1f840000000000 nop word ptr [rax+rax]
00000001`806c1620 6690 xchg ax,ax
00000001`806c1622 4881fa00f0ffff cmp rdx,0FFFFFFFFFFFFF000h
00000001`806c1629 77b5 ja coreclr!JIT_MemCpy+0x270 (00000001`806c15e0)
00000001`806c162b b820000000 mov eax,20h
00000001`806c1630 4881e980000000 sub rcx,80h
00000001`806c1637 0f18040a prefetchnta [rdx+rcx]
00000001`806c163b 0f18440a40 prefetchnta [rdx+rcx+40h]
00000001`806c1640 ffc8 dec eax
00000001`806c1642 75ec jne coreclr!JIT_MemCpy+0x2c0 (00000001`806c1630)
00000001`806c1644 4881c100100000 add rcx,1000h
00000001`806c164b b840000000 mov eax,40h
00000001`806c1650 4c8b4c0af8 mov r9,qword ptr [rdx+rcx-8]
00000001`806c1655 4c8b540af0 mov r10,qword ptr [rdx+rcx-10h]
00000001`806c165a 4c0fc349f8 movnti qword ptr [rcx-8],r9
00000001`806c165f 4c0fc351f0 movnti qword ptr [rcx-10h],r10
00000001`806c1664 4c8b4c0ae8 mov r9,qword ptr [rdx+rcx-18h]
00000001`806c1669 4c8b540ae0 mov r10,qword ptr [rdx+rcx-20h]
00000001`806c166e 4c0fc349e8 movnti qword ptr [rcx-18h],r9
00000001`806c1673 4c0fc351e0 movnti qword ptr [rcx-20h],r10
00000001`806c1678 4c8b4c0ad8 mov r9,qword ptr [rdx+rcx-28h]
00000001`806c167d 4c8b540ad0 mov r10,qword ptr [rdx+rcx-30h]
00000001`806c1682 4883e940 sub rcx,40h
00000001`806c1686 4c0fc34918 movnti qword ptr [rcx+18h],r9
00000001`806c168b 4c0fc35110 movnti qword ptr [rcx+10h],r10
00000001`806c1690 4c8b4c0a08 mov r9,qword ptr [rdx+rcx+8]
00000001`806c1695 4c8b140a mov r10,qword ptr [rdx+rcx]
00000001`806c1699 ffc8 dec eax
00000001`806c169b 4c0fc34908 movnti qword ptr [rcx+8],r9
00000001`806c16a0 4c0fc311 movnti qword ptr [rcx],r10
00000001`806c16a4 75aa jne coreclr!JIT_MemCpy+0x2e0 (00000001`806c1650)
00000001`806c16a6 4981e800100000 sub r8,1000h
00000001`806c16ad 4981f800100000 cmp r8,1000h
00000001`806c16b4 0f8371ffffff jae coreclr!JIT_MemCpy+0x2bb (00000001`806c162b)
00000001`806c16ba f0800c2400 lock or byte ptr [rsp],0
00000001`806c16bf e9aafeffff jmp coreclr!JIT_MemCpy+0x1fe (00000001`806c156e)
coreclr!JIT_MemCpy_End [D:\ProjectK\src\NDP\clr\src\vm\amd64\CrtHelpers.asm @ 524]:
00000001`806c16c4 90 nop
I am wondering whether it is a good idea to have a full copy of the assembly implementation for non-Windows platforms. It does not need to be bug-for-bug compatible with Windows. Also, the Windows assembly code is pretty old - it may not have the best possible performance for current processors anyway because of there are performance tweaks required in it for every new generation of hardware. (BTW: The fastest way to do memcpy on the state of the art Intel hardware is to just do old fashioned "rep movsb". Hurray!)
I think that delegating to C runtime implementation for non-Windows platforms should work just fine here. Something like:
test rdx, rdx jz DoNothing
// Make sure to trap null reference exceptions here so that they can be handled cmp byte ptr [rsi],0 cmp byte ptr [rdi],0 jmp memcpy // Tail call C runtime memcpy
DoNothing: ret
Similar for memset.
@jkotas That makes sense to me. I also prefer using assembler code only if it is inevitable. Maybe we could even do without this thin assembler wrapper and use a C wrapper if we can prevent inlining of the memcpy in it using a compiler attribute.
PR submitted @ https://github.com/dotnet/coreclr/pull/211
PR merged, can someone close this out please?
Thanks, Geoff. Fixed with dotnet/coreclr#211.