Skip to content
Snippets Groups Projects

test-primops

This is a QuickCheck-based testsuite for GHC's C-- pipeline and native code generation backends.

Command-line usage

Simply invoke the test-primops executable, which is a standard tasty testsuite.

In general it can be useful to use distinct compilers to build the test-primops executable (called the bootstrap compiler) and the testcases (called the compiler-under-test). The path to the compiler-under-test can be set using --ghc-path=.... It is also necessary to compile the run-it executable with the compiler-under-test. The path to the this executable can be given using the --run-it-path=... flag.

There is a convenient script, run.sh, which handles this for you; just set the BOOT_GHC environment variable to the bootstrap compiler and TEST_GHC to the compiler-under-test.

Usage with cross-compilers

test-primops can test cross-compilers by running run.sh with TEST_GHC set to the path to the cross-compiler and EMULATOR to the path to an emulator which can run a target executable (e.g. qemu-user or wasmtime).

REPL usage

It can often be useful to load the test-primops executable into GHCi. Below is a quick example showing some common tasks:

λ> -- Setup a compiler
λ> let comp = basicCompiler "/opt/exp/ghc/ghc-8.10/_build/stage1/bin/ghc" `addArgs` ["-O0", "-fllvm"]
λ> -- Check expression correctness
λ> quickCheck $ expr_prop comp
λ> -- Parse an expression (e.g. taken from the test output)
λ> let e = parseExpr @W64 "zext[W16→W64](42::W16)"
λ> -- Evaluate the expression using the reference interpreter
λ> interpret e
42
λ> -- Show the Cmm for the expression
λ> putStrLn $ exprToCmm e
λ> -- Manually evaluate the expression using GHC
λ> evalGhcDyn comp e
42
λ> -- Compile and dump assembly of an expression
λ> dumpExprAsm comp e >>= putStrLn
.section .text
.align 8
.globl test
.type test, @function
test:
.Lc3:
        movl $42,%eax
        movq %rbx,%rcx
        movq %rax,%rbx
        jmp *(%rbp)
        .size test, .-test
.section .note.GNU-stack,"",@progbits
.ident "GHC 8.10.7"

To ease experimentation, there is also a simple test executable in the simple directory; just paste some Cmm into test.cmm, and run run.sh. This will drop you into a debugger at the beginning of the Cmm.

simple can be run with QEMU: Just set the environment variable EMULATOR to the QEMU executable. E.g.

cd simple
EMULATOR=qemu-riscv64 TEST_GHC=<path of GHC under test> ./run.sh

TODO

  • test machops
  • test callish machops
  • test ccalls
  • test varargs ccalls
  • test stores
  • batch testing of expressions
  • test Expr parser roundtripping
  • testing floating point primops