Add a MachBuffer::defer_trap method (#6011)

* Add a `MachBuffer::defer_trap` method

This commit adds a new method to `MachBuffer` to defer trap opcodes to
the end of a function in a similar manner to how constants are deferred
to the end of the function. This is useful for backends which frequently
use `TrapIf`-style opcodes. Currently a jump is emitted which skips the
next instruction, a trap, and then execution continues normally. While
there isn't any pressing problem with this construction the trap opcode
is in the middle of the instruction stream as opposed to "off on the
side" despite rarely being taken.

With this method in place all the backends (except riscv64 since I
couldn't figure it out easily enough) have a new lowering of their
`TrapIf` opcode. Now a trap is deferred, which returns a label, and then
that label is jumped to when executing the trap. A fixup is then
recorded in `MachBuffer` to get patched later on during emission, or at
the end of the function. Subsequently all `TrapIf` instructions
translate to a single branch plus a single trap at the end of the
function.

I've additionally further updated some more lowerings in the x64 backend
which were explicitly using traps to instead use `TrapIf` where
applicable to avoid jumping over traps mid-function. Other backends
didn't appear to have many jump-over-the-next-trap patterns.

Lots of tests have had their expectations updated here which should
reflect all the traps being sunk to the end of functions.

* Print trap code on all platforms

* Emit traps before constants

* Preserve source location information for traps

* Fix test expectations

* Attempt to fix s390x

The MachBuffer was registering trap codes with the first byte of the
trap, but the SIGILL handler was expecting it to be registered with the
last byte of the trap. Exploit that SIGILL is always represented with a
2-byte instruction and always march 2-backwards for SIGILL, continuing
to march backwards 1 byte for SIGFPE-generating instructions.

* Back out s390x changes

* Back out more s390x bits

* Review comments
This commit is contained in:
Alex Crichton
2023-03-20 16:24:47 -05:00
committed by GitHub
parent 6a03398faf
commit a3b21031d4
52 changed files with 702 additions and 588 deletions

View File

@@ -12,9 +12,9 @@
;; 4: b90a000000 mov ecx, 0xa
;; 9: b814000000 mov eax, 0x14
;; e: 83f900 cmp ecx, 0
;; 11: 0f8502000000 jne 0x19
;; 17: 0f0b ud2
;; 19: 99 cdq
;; 1a: f7f9 idiv ecx
;; 1c: 5d pop rbp
;; 1d: c3 ret
;; 11: 0f8405000000 je 0x1c
;; 17: 99 cdq
;; 18: f7f9 idiv ecx
;; 1a: 5d pop rbp
;; 1b: c3 ret
;; 1c: 0f0b ud2

View File

@@ -12,9 +12,9 @@
;; 4: b900000000 mov ecx, 0
;; 9: b801000000 mov eax, 1
;; e: 83f900 cmp ecx, 0
;; 11: 0f8502000000 jne 0x19
;; 17: 0f0b ud2
;; 19: 99 cdq
;; 1a: f7f9 idiv ecx
;; 1c: 5d pop rbp
;; 1d: c3 ret
;; 11: 0f8405000000 je 0x1c
;; 17: 99 cdq
;; 18: f7f9 idiv ecx
;; 1a: 5d pop rbp
;; 1b: c3 ret
;; 1c: 0f0b ud2

View File

@@ -12,9 +12,9 @@
;; 4: b9ffffffff mov ecx, 0xffffffff
;; 9: b800000080 mov eax, 0x80000000
;; e: 83f900 cmp ecx, 0
;; 11: 0f8502000000 jne 0x19
;; 17: 0f0b ud2
;; 19: 99 cdq
;; 1a: f7f9 idiv ecx
;; 1c: 5d pop rbp
;; 1d: c3 ret
;; 11: 0f8405000000 je 0x1c
;; 17: 99 cdq
;; 18: f7f9 idiv ecx
;; 1a: 5d pop rbp
;; 1b: c3 ret
;; 1c: 0f0b ud2

View File

@@ -15,10 +15,10 @@
;; f: 8b0c24 mov ecx, dword ptr [rsp]
;; 12: 8b442404 mov eax, dword ptr [rsp + 4]
;; 16: 83f900 cmp ecx, 0
;; 19: 0f8502000000 jne 0x21
;; 1f: 0f0b ud2
;; 21: 99 cdq
;; 22: f7f9 idiv ecx
;; 24: 4883c408 add rsp, 8
;; 28: 5d pop rbp
;; 29: c3 ret
;; 19: 0f8409000000 je 0x28
;; 1f: 99 cdq
;; 20: f7f9 idiv ecx
;; 22: 4883c408 add rsp, 8
;; 26: 5d pop rbp
;; 27: c3 ret
;; 28: 0f0b ud2

View File

@@ -12,9 +12,9 @@
;; 4: b900000000 mov ecx, 0
;; 9: b800000000 mov eax, 0
;; e: 83f900 cmp ecx, 0
;; 11: 0f8502000000 jne 0x19
;; 17: 0f0b ud2
;; 19: 99 cdq
;; 1a: f7f9 idiv ecx
;; 1c: 5d pop rbp
;; 1d: c3 ret
;; 11: 0f8405000000 je 0x1c
;; 17: 99 cdq
;; 18: f7f9 idiv ecx
;; 1a: 5d pop rbp
;; 1b: c3 ret
;; 1c: 0f0b ud2

View File

@@ -12,9 +12,9 @@
;; 4: 48c7c10a000000 mov rcx, 0xa
;; b: 48c7c014000000 mov rax, 0x14
;; 12: 4883f900 cmp rcx, 0
;; 16: 0f8502000000 jne 0x1e
;; 1c: 0f0b ud2
;; 1e: 4899 cqo
;; 20: 48f7f9 idiv rcx
;; 23: 5d pop rbp
;; 24: c3 ret
;; 16: 0f8407000000 je 0x23
;; 1c: 4899 cqo
;; 1e: 48f7f9 idiv rcx
;; 21: 5d pop rbp
;; 22: c3 ret
;; 23: 0f0b ud2

View File

@@ -12,9 +12,9 @@
;; 4: 48c7c100000000 mov rcx, 0
;; b: 48c7c001000000 mov rax, 1
;; 12: 4883f900 cmp rcx, 0
;; 16: 0f8502000000 jne 0x1e
;; 1c: 0f0b ud2
;; 1e: 4899 cqo
;; 20: 48f7f9 idiv rcx
;; 23: 5d pop rbp
;; 24: c3 ret
;; 16: 0f8407000000 je 0x23
;; 1c: 4899 cqo
;; 1e: 48f7f9 idiv rcx
;; 21: 5d pop rbp
;; 22: c3 ret
;; 23: 0f0b ud2

View File

@@ -13,9 +13,9 @@
;; b: 48b80000000000000080
;; movabs rax, 0x8000000000000000
;; 15: 4883f900 cmp rcx, 0
;; 19: 0f8502000000 jne 0x21
;; 1f: 0f0b ud2
;; 21: 4899 cqo
;; 23: 48f7f9 idiv rcx
;; 26: 5d pop rbp
;; 27: c3 ret
;; 19: 0f8407000000 je 0x26
;; 1f: 4899 cqo
;; 21: 48f7f9 idiv rcx
;; 24: 5d pop rbp
;; 25: c3 ret
;; 26: 0f0b ud2

View File

@@ -15,10 +15,10 @@
;; 11: 488b0c24 mov rcx, qword ptr [rsp]
;; 15: 488b442408 mov rax, qword ptr [rsp + 8]
;; 1a: 4883f900 cmp rcx, 0
;; 1e: 0f8502000000 jne 0x26
;; 24: 0f0b ud2
;; 26: 4899 cqo
;; 28: 48f7f9 idiv rcx
;; 2b: 4883c410 add rsp, 0x10
;; 2f: 5d pop rbp
;; 30: c3 ret
;; 1e: 0f840b000000 je 0x2f
;; 24: 4899 cqo
;; 26: 48f7f9 idiv rcx
;; 29: 4883c410 add rsp, 0x10
;; 2d: 5d pop rbp
;; 2e: c3 ret
;; 2f: 0f0b ud2

View File

@@ -12,9 +12,9 @@
;; 4: 48c7c100000000 mov rcx, 0
;; b: 48c7c000000000 mov rax, 0
;; 12: 4883f900 cmp rcx, 0
;; 16: 0f8502000000 jne 0x1e
;; 1c: 0f0b ud2
;; 1e: 4899 cqo
;; 20: 48f7f9 idiv rcx
;; 23: 5d pop rbp
;; 24: c3 ret
;; 16: 0f8407000000 je 0x23
;; 1c: 4899 cqo
;; 1e: 48f7f9 idiv rcx
;; 21: 5d pop rbp
;; 22: c3 ret
;; 23: 0f0b ud2