Keyword argument type info appears to be lost

Bug #2029274 reported by Andrew Kravchuk
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
SBCL
Fix Released
Undecided
Unassigned

Bug Description

Hello. It looks like the type info from &KEY FTYPE declaration is somehow lost in the process of type inference with optimization cranked up to maximum. Here are two examples to illustrate:

(proclaim '(optimize (speed 3) (safety 0) (debug 0) (compilation-speed 0)))
(declaim (type (simple-array single-float) a))
(defparameter a (make-array 1 :element-type 'single-float))

(declaim (ftype (function (single-float)) test1))
(defun test1 (x) (equalp x (aref a 0)))
(disassemble #'test1)
; disassembly for TEST1
; Size: 52 bytes. Origin: #x5366950F ; TEST1
; 0F: 488B05CAFFFFFF MOV RAX, [RIP-54] ; 'A
; 16: 8B48F5 MOV ECX, [RAX-11]
; 19: 4A8B0C29 MOV RCX, [RCX+R13]
; 1D: 4883F9FF CMP RCX, -1
; 21: 480F444801 CMOVEQ RCX, [RAX+1]
; 26: F30F104901 MOVSS XMM1, [RCX+1]
; 2B: 0F2FC1 COMISS XMM0, XMM1
; 2E: BA4F010050 MOV EDX, #x5000014F ; T
; 33: B817010050 MOV EAX, #x50000117 ; NIL
; 38: 480F4AD0 CMOVP RDX, RAX
; 3C: 480F45D0 CMOVNE RDX, RAX
; 40: C9 LEAVE
; 41: F8 CLC
; 42: C3 RET
NIL

;; we can see the call to EQUALP was nicely optimized out to single COMISS

(declaim (ftype (function (&key (:x single-float))) test2))
(defun test2 (&key x) (equalp x (aref a 0)))
(disassemble #'test2)
; disassembly for TEST2
; Size: 55 bytes. Origin: #x53669651 ; TEST2
; 51: 488B0540FFFFFF MOV RAX, [RIP-192] ; 'A
; 58: 8B48F5 MOV ECX, [RAX-11]
; 5B: 4A8B0C29 MOV RCX, [RCX+R13]
; 5F: 4883F9FF CMP RCX, -1
; 63: 480F444801 CMOVEQ RCX, [RAX+1]
; 68: F30F104101 MOVSS XMM0, [RCX+1]
; 6D: 660F7EC7 MOVD EDI, XMM0
; 71: 48C1E720 SHL RDI, 32
; 75: 4080CF19 OR DIL, 25
; 79: B904000000 MOV ECX, 4
; 7E: FF7508 PUSH QWORD PTR [RBP+8]
; 81: FF2425C8040050 JMP [#x500004C8] ; EQUALP
NIL

;; ...and here we see the full call to EQUALP although compiler still has the information that X is a SINGLE-FLOAT

;; additional info:

(lisp-implementation-version) ;; => "2.3.5"

*features*
(:SLYNK :OCICL :CLPM-CLIENT :QUICKLISP :ASDF3.3 :ASDF3.2 :ASDF3.1 :ASDF3 :ASDF2
 :ASDF :OS-UNIX :NON-BASE-CHARS-EXIST-P :ASDF-UNICODE :ARENA-ALLOCATOR :X86-64
 :GENCGC :64-BIT :ANSI-CL :COMMON-LISP :ELF :IEEE-FLOATING-POINT :LINUX
 :LITTLE-ENDIAN :PACKAGE-LOCAL-NICKNAMES :SB-CORE-COMPRESSION :SB-LDB
 :SB-PACKAGE-LOCKS :SB-THREAD :SB-UNICODE :SBCL :UNIX)

$ uname -a
Linux melchior 6.2.13-zen #1 ZEN SMP PREEMPT Sun Jun 11 00:28:58 CEST 2023 x86_64 AMD Ryzen 5 3600X 6-Core Processor AuthenticAMD GNU/Linux

Revision history for this message
Andrew Kravchuk (awkravchuk) wrote :

It just occured to me that keyword argument might be NIL, in which case that would require EQUALP, so no bug here. Sorry for the fuss.

Revision history for this message
Stas Boukarev (stassats) wrote :

I suppose (when x (equalp x float)) could be a valid optimization.

Revision history for this message
Andrew Kravchuk (awkravchuk) wrote :

Yep, that does the trick. Thank you Stas!

Revision history for this message
Stas Boukarev (stassats) wrote :

But SBCL could do that automatically.

Stas Boukarev (stassats)
Changed in sbcl:
status: New → Fix Committed
Stas Boukarev (stassats)
Changed in sbcl:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.