Skip to content
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

Dispatch directly with function pointer address instead of lookup through table #186

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

malcolmstill
Copy link
Owner

@malcolmstill malcolmstill commented Jan 22, 2023

Description

  • We were using tail call dispatch, but still performing a runtime lookup of the function to jump to, based on the opcode.
  • This PR adds an instructions array and populates it at module parse time alongside the parsed_code array.
  • The tail calls then use the function pointer directly rather than having to do a lookup

Note: I had some issues with a recursive definition for InstructionFunction so I added a similar dummy type and do some casting which is a bit ugly.

Problem

Looking at the machine code for a few instructions we might expect this change to give us some speed increase, as the instruction count seems to have come down (as we might have expected from getting rid of a superfluous table lookup), but fib actually takes longer to run with this change. Why could that be?

nop

Before:

10000918c: 48 14 01 8b 	add	x8, x2, x1, lsl #5
100009190: 09 01 00 f0 	adrp	x9, 0x10002c000 <_instance.vm.VirtualMachine.loop+0x14>
100009194: 29 21 03 91 	add	x9, x9, #200
100009198: 21 04 00 91 	add	x1, x1, #1
10000919c: 08 e1 40 39 	ldrb	w8, [x8, #56]
1000091a0: 24 79 68 f8 	ldr	x4, [x9, x8, lsl #3]
1000091a4: 80 00 1f d6 	br	x4

After:

1000082bc: 88 0c 01 8b 	add	x8, x4, x1, lsl #3
1000082c0: 21 04 00 91 	add	x1, x1, #1
1000082c4: 06 05 40 f9 	ldr	x6, [x8, #8]
1000082c8: c0 00 1f d6 	br	x6

else

Before:

1000092e0: 08 18 40 f9 	ldr	x8, [x0, #48]
1000092e4: 0a 03 80 52 	mov	w10, #24
1000092e8: 09 20 40 f9 	ldr	x9, [x0, #64]
1000092ec: 28 21 0a 9b 	madd	x8, x9, x10, x8
1000092f0: 01 01 5f f8 	ldur	x1, [x8, #-16]
1000092f4: 28 05 00 d1 	sub	x8, x9, #1
1000092f8: 49 14 01 8b 	add	x9, x2, x1, lsl #5
1000092fc: 08 20 00 f9 	str	x8, [x0, #64]
100009300: 28 61 40 39 	ldrb	w8, [x9, #24]
100009304: 09 01 00 f0 	adrp	x9, 0x10002c000 <_instance.vm.VirtualMachine.if_no_else+0x7c>
100009308: 29 21 03 91 	add	x9, x9, #200
10000930c: 24 79 68 f8 	ldr	x4, [x9, x8, lsl #3]
100009310: 80 00 1f d6 	br	x4

After:

1000083e4: 08 18 40 f9 	ldr	x8, [x0, #48]
1000083e8: 0a 03 80 52 	mov	w10, #24
1000083ec: 09 20 40 f9 	ldr	x9, [x0, #64]
1000083f0: 28 21 0a 9b 	madd	x8, x9, x10, x8
1000083f4: 29 05 00 d1 	sub	x9, x9, #1
1000083f8: 01 01 5f f8 	ldur	x1, [x8, #-16]
1000083fc: 09 20 00 f9 	str	x9, [x0, #64]
100008400: 86 78 61 f8 	ldr	x6, [x4, x1, lsl #3]
100008404: c0 00 1f d6 	br	x6

@malcolmstill malcolmstill marked this pull request as draft January 23, 2023 00:38
@malcolmstill malcolmstill force-pushed the malcolm/full-direct-threaded branch from 8676bb8 to 5a2ebec Compare July 23, 2023 19:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant