cisco / ChezScheme

Chez Scheme
Apache License 2.0
6.95k stars 982 forks source link

Final stage in riscv64 and loongarch64 support #700

Closed maoif closed 1 year ago

maoif commented 1 year ago

Hello everyone,

I've been porting ChezScheme to riscv64 and loongarch64 (an ISA by a Chinese company) and so far the majority of the tests are passed. However, there are still some tests that stuck and I don't have a clue why they exist. You can check out my latest code here.

This is rv64le's mats report:

-------- o=0 --------
> cp0.mo:Bug in mat cp0-partial-folding clause 1 at line 1149, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 2 at line 1161, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 5 at line 1193, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 6 at line 1205, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 7 at line 1217, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 8 at line 1229, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 13 at line 1291, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 14 at line 1303, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 15 at line 1315, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 16 at line 1327, char 3 of ../cp0.ms

-------- o=3 --------
> cp0.mo:Bug in mat cp0-partial-folding clause 1 at line 1149, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 2 at line 1161, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 5 at line 1193, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 6 at line 1205, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 7 at line 1217, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 8 at line 1229, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 13 at line 1291, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 14 at line 1303, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 15 at line 1315, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 16 at line 1327, char 3 of ../cp0.ms
> ftype.mo:Bug in mat ftype-lock-operations clause 132 at line 2787, char 3 of ../ftype.ms
> ftype.mo:Bug in mat ftype-lock-operations clause 137 at line 2793, char 3 of ../ftype.ms

-------- o=0 cp0=t cl=3 --------
> cp0.mo:Bug in mat cp0-partial-folding clause 1 at line 1149, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 2 at line 1161, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 5 at line 1193, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 6 at line 1205, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 7 at line 1217, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 8 at line 1229, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 13 at line 1291, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 14 at line 1303, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 15 at line 1315, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 16 at line 1327, char 3 of ../cp0.ms
> fl.mo:Bug in mat flonum->fixnum clause 6 at line 23, char 4 of ../fl.ms

-------- o=3 cp0=t cl=3 --------
> cp0.mo:Bug in mat cp0-partial-folding clause 1 at line 1149, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 2 at line 1161, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 5 at line 1193, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 6 at line 1205, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 7 at line 1217, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 8 at line 1229, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 13 at line 1291, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 14 at line 1303, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 15 at line 1315, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 16 at line 1327, char 3 of ../cp0.ms

-------- o=0 spi=t p=t rmg=2 --------
> cp0.mo:Bug in mat cp0-partial-folding clause 1 at line 1149, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 2 at line 1161, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 5 at line 1193, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 6 at line 1205, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 7 at line 1217, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 8 at line 1229, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 13 at line 1291, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 14 at line 1303, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 15 at line 1315, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 16 at line 1327, char 3 of ../cp0.ms

-------- o=3 spi=t p=t rmg=2 --------
> 5_1.mo:Bug in mat new-equal? clause 33 at line 509, char 3 of ../5_1.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 1 at line 1149, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 2 at line 1161, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 5 at line 1193, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 6 at line 1205, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 7 at line 1217, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 8 at line 1229, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 13 at line 1291, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 14 at line 1303, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 15 at line 1315, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 16 at line 1327, char 3 of ../cp0.ms

-------- o=0 eval=interpret cl=6 --------
> cp0.mo:Bug in mat cp0-partial-folding clause 1 at line 1149, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 2 at line 1161, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 5 at line 1193, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 6 at line 1205, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 7 at line 1217, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 8 at line 1229, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 13 at line 1291, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 14 at line 1303, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 15 at line 1315, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 16 at line 1327, char 3 of ../cp0.ms
> 5_8.mo:Bug in mat box-cas! clause 4 at line 41, char 3 of ../5_8.ms
> 5_8.mo:Bug in mat box-cas! clause 5 at line 42, char 3 of ../5_8.ms

-------- o=3 eval=interpret cl=6 --------
> cp0.mo:Bug in mat cp0-partial-folding clause 1 at line 1149, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 2 at line 1161, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 5 at line 1193, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 6 at line 1205, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 7 at line 1217, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 8 at line 1229, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 13 at line 1291, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 14 at line 1303, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 15 at line 1315, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 16 at line 1327, char 3 of ../cp0.ms

-------- o=0 cp0=t eval=interpret rmg=2 --------
> 5_1.mo:Bug in mat new-equal? clause 33 at line 509, char 3 of ../5_1.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 1 at line 1149, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 2 at line 1161, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 5 at line 1193, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 6 at line 1205, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 7 at line 1217, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 8 at line 1229, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 13 at line 1291, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 14 at line 1303, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 15 at line 1315, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 16 at line 1327, char 3 of ../cp0.ms
> fl.mo:Bug in mat flonum->fixnum clause 6 at line 23, char 4 of ../fl.ms

-------- o=3 cp0=t eval=interpret rmg=2 --------
> cp0.mo:Bug in mat cp0-partial-folding clause 1 at line 1149, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 2 at line 1161, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 5 at line 1193, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 6 at line 1205, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 7 at line 1217, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 8 at line 1229, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 13 at line 1291, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 14 at line 1303, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 15 at line 1315, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 16 at line 1327, char 3 of ../cp0.ms

-------- o=0 hci=101 eoc=f cl=9 --------
> 5_1.mo:Bug in mat new-equal? clause 33 at line 509, char 3 of ../5_1.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 1 at line 1149, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 2 at line 1161, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 5 at line 1193, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 6 at line 1205, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 7 at line 1217, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 8 at line 1229, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 13 at line 1291, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 14 at line 1303, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 15 at line 1315, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 16 at line 1327, char 3 of ../cp0.ms

-------- o=3 hci=101 eval=interpret rmg=2 --------
> 5_1.mo:Bug in mat new-equal? clause 34 at line 523, char 3 of ../5_1.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 1 at line 1149, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 2 at line 1161, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 5 at line 1193, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 6 at line 1205, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 7 at line 1217, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 8 at line 1229, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 13 at line 1291, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 14 at line 1303, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 15 at line 1315, char 3 of ../cp0.ms
> cp0.mo:Bug in mat cp0-partial-folding clause 16 at line 1327, char 3 of ../cp0.ms
> 5_6.mo:Bug in mat vector-cas! clause 4 at line 1265, char 3 of ../5_6.ms
> 5_6.mo:Bug in mat vector-cas! clause 5 at line 1266, char 3 of ../5_6.ms
> 5_6.mo:Bug in mat vector-cas! clause 6 at line 1267, char 3 of ../5_6.ms

This is la64le's (loongarch64) mats report, which is better:

-------- o=0 --------

-------- o=3 --------

-------- o=0 cp0=t cl=3 --------
> fl.mo:Bug in mat flonum->fixnum clause 6 at line 23, char 4 of ../fl.ms
-------- o=3 cp0=t cl=3 --------

-------- o=0 spi=t p=t rmg=2 --------
> 5_1.mo:Bug in mat new-equal? clause 33 at line 509, char 3 of ../5_1.ms
-------- o=3 spi=t p=t rmg=2 --------

-------- o=0 eval=interpret cl=6 --------

-------- o=3 eval=interpret cl=6 --------

-------- o=0 cp0=t eval=interpret rmg=2 --------
> fl.mo:Bug in mat flonum->fixnum clause 6 at line 23, char 4 of ../fl.ms

-------- o=3 cp0=t eval=interpret rmg=2 --------

-------- o=0 hci=101 eoc=f cl=9 --------

-------- o=3 hci=101 eval=interpret rmg=2 --------

Threaded versions have similar issues so I omit the reports here.

riscv64 test environment:

Debian riscv64 emulated by
qemu-system-riscv64 -machine virt -cpu rv64 ...

loongarch64 test environment:

Debian loongarch64 emulated by
qemu-system-loongarch64 -machine virt -cpu la464-loongarch-cpu ...

I looked at the cp0-partial-folding bug and found it leads to:

failure in equivalent-expansion?:
+nan.0
is not equivalent to
+nan.0

It seems that the (eqv? x y) in equivalent-expansion? fails to compare the two +nan.0's in the two lists.

But if I type the comparison directly in REPL, the result is correct:

> (eqv? +nan.0 +nan.0)
#t

The fl.mo:Bug in mat flonum->fixnum is also interesting. The test code is:

;; fl.ms
(error? (flonum->fixnum
              (* (inexact (most-negative-fixnum)) 2.0)))

The correct behavior of the code is to give an exception, but in rv64 and la64 the flonum->fixnum part returns 0. It's as if the 2nd unless below failed to run:

;; mathprims.ss
(set-who! flonum->fixnum
      (let ([flmnf (fixnum->flonum (most-negative-fixnum))]
            [flmpf (fixnum->flonum (most-positive-fixnum))])
         (lambda (x)
            (unless (flonum? x) (flargerr who x))
            (unless (fl<= flmnf x flmpf)
               ($oops who "result for ~s would be outside of fixnum range" x))
            (#3%flonum->fixnum x))))

BUT, if I run

> (define fl->fix
    (let ([flmnf (fixnum->flonum (most-negative-fixnum))]
          [flmpf (fixnum->flonum (most-positive-fixnum))])
      (lambda (x)
        (unless (flonum? x) (printf "err: ~a" x))
        (unless (fl<= flmnf x flmpf)
          (printf "result for ~s would be outside of fixnum range" x))
        (#3%flonum->fixnum x))))
> (fl->fix (* (inexact (most-negative-fixnum)) 2.0))
result for -2.305843009213694e18 would be outside of fixnum range0

The error appears.

I would like to know if you guys know what the sources of the bugs are.

maoif commented 1 year ago

The flonum->fixnum bug is solved by modifying asm-mul/ovfl in the backend.

maoif commented 1 year ago

The cp0.mo bug is due to the different representations of +nan.0 on x86_64 and on RISC-V.

ufo5260987423 commented 1 year ago

The cp0.mo bug is due to the different representations of +nan.0 on x86_64 and on RISC-V.

Hi, I'm interested in your work, could you please make an overview? Maybe will solve us many potential questions! Thanks.

maoif commented 1 year ago

The cp0.mo bug is due to the different representations of +nan.0 on x86_64 and on RISC-V.

Hi, I'm interested in your work, could you please make an overview? Maybe will solve us many potential questions! Thanks.

Sure. I'm preparing the PR right now, and am writing an article explaining the porting process. Hope I can get them done soon.

Maybe you can let me know what questions you have?

ufo5260987423 commented 1 year ago

The cp0.mo bug is due to the different representations of +nan.0 on x86_64 and on RISC-V.

Hi, I'm interested in your work, could you please make an overview? Maybe will solve us many potential questions! Thanks.

Sure. I'm preparing the PR right now, and am writing an article explaining the porting process. Hope I can get them done soon.

Maybe you can let me know what questions you have?

It's my very pleasure to hear that! And here's my questions:

  1. What's the key difference among riscv64, loongarch64 and x86/64? Especially from your perspective, what make that such porting work costs about 2 and half years?
  2. What's your routine? I've looked your commit, if you could combine these experience with this page's instruction, and write some soundness work, it would be very good!
  3. Some programmers (including me) is focusing on bare metal or lisp machine, could you give some comments in your perspective?

That's all, thank you!

ufo5260987423 commented 1 year ago

The cp0.mo bug is due to the different representations of +nan.0 on x86_64 and on RISC-V.

Hi, I'm interested in your work, could you please make an overview? Maybe will solve us many potential questions! Thanks.

Sure. I'm preparing the PR right now, and am writing an article explaining the porting process. Hope I can get them done soon.

Maybe you can let me know what questions you have?

It's my very pleasure to hear that! And here's my questions:

  1. What's the key difference among riscv64, loongarch64 and x86/64? Especially from your perspective, what make that such porting work costs about 2 and half years?
  2. What's your routine? I've looked your commit, if you could combine these experience with this page's instruction, and write some soundness work, it would be very good!
  3. Some programmers (including me) is focusing on bare metal or lisp machine, could you give some comments in your perspective?

That's all, thank you!