ChezScheme icon indicating copy to clipboard operation
ChezScheme copied to clipboard

Building for the x32 ABI

Open svenha opened this issue 1 year ago • 5 comments

Hello.

I was able to build the scheme binary for the x32 ABI. I am interested in the x32 ABI because it can give significant speedups for GC-intensive programs and reduce memory usage by 50 % (observed for other Schemes like bigloo).

The following error during make shows that there seems to be some asserts or assumptions that differ for x32 compared to x86-64. Can I just modify them? If so, where and how?

make all
echo '(reset-handler abort)'\
            '(base-exception-handler (lambda (c) (fresh-line) (display-condition c) (newline) (reset)))'\
            '(keyboard-interrupt-handler (lambda () (display "interrupted---aborting\n") (reset)))'\
            '(optimize-level 3)'\
            '(debug-level 0)'\
            '(commonization-level (commonization-level))'\
            '(fasl-compressed #t)'\
            '(compress-format (compress-format))'\
            '(compress-level (compress-level))'\
            '(generate-inspector-information #f)'\
            '(subset-mode (quote system))'\
            '(compile-file "cmacros.ss" "cmacros.so")'\
            | ../bin/a6le/scheme -q
sizeof(ptr) * 8 [4] != ptr_bits [64]
sizeof(long) * 8 [4] != long_bits [64]
sizeof(size_t) * 8 [4] != size_t_bits [64]
sizeof(ssize_t) * 8 [4] != size_t_bits [64]
sizeof(ptrdiff_t) * 8 [4] != ptrdiff_t_bits [64]
sizeof(bigitbigit) [4] != sizeof(bigit) [4] * 2
sizeof(I64) [4] != 8
sizeof(U64) [4] != 8
make[4]: *** [Mf-base:372: cmacros.so] Error 1
make[3]: *** [Mf-base:176: allx] Error 2
make[2]: *** [Mf-base:193: bootstrap] Error 2
make[1]: *** [Makefile:22: build] Error 2
make: *** [Makefile:20: build] Error 2

I built the x32 binary by modifying the build process as follows (one needs x32 versions of ncurses, libuuid, libz, liblz4):

./configure ZLIB=/usr/local/libx32/libz.a LZ4=/usr/local/libx32/liblz4.a --machine=a6le --installbin=/usr/local/binx32 --installlib=/usr/local/libx32 --installman=/usr/local/share/man
 sed -i.orig -e '/s/ -m64/ -mx32/g' -e '/ -Werror/ / g' 's/elf_x86_64/elf32_x86_64/g' c/Mf-a6le

svenha avatar Feb 28 '23 09:02 svenha

Please use the i3le target, which is a 32-bit Intel architecture on Linux.

burgerrg avatar Feb 28 '23 15:02 burgerrg

x32 ABI is not i386, but more like x86-64 with 32bit pointers, see https://en.wikipedia.org/wiki/X32_ABI for details. You get the benefits of x86-64 (more registers, more instructions) without the memory overhead. The downside: your process cannot use more than 4 GB.

svenha avatar Feb 28 '23 19:02 svenha

You could try modifying address-bits and ptr-bits in s/a6le.def and s/ta6le.def and see what happens.

melted avatar Mar 01 '23 12:03 melted

@melted Thanks for the suggestions. I also changed ptrdiff_t-bits and size_t-bits (from 64 to 32), but the error message is the same.

svenha avatar Mar 01 '23 18:03 svenha

I returned to the issue with a current git version.

Good news is that I ended up with a usable pb/scheme, which is an X32 binary:

pb/bin/pb/scheme: ELF 32-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /libx32/ld-linux-x32.so.2, BuildID[sha1]=2b0a68bb0e47dbfb0e2b6b45f7619eee514bbb38, for GNU/Linux 3.4.0, not stripped

The make step ends now as follows:

running pb/bin/pb/scheme to build all at once
loading ta6le cross compiler
compiling s/library.ss with output to xc-ta6le/s/library.ta6le
Exception in assembler-internal: ax-ea-imm-data size=quad imm-data=(literal 33 (library #(libspec nonprocedure-code 160)))
failed
in build-one
in loop
in module->hash
make: *** [Makefile:10: build] Error 1

For context, this is how I build chez-scheme:

CC=gccx32 ./configure --installprefix=/usr/local # gccx32 is script specifying gcc -mx32
sed -i.orig -e 's/-m64 /-mx32 /g' -e 's/ -Werror/ / g' -e 's/elf_x86_64/elf32_x86_64/g' ta6le/Mf-config
sed -i.orig -e 's/ptr-bits 64/ptr-bits 32/g' s/a6.def
sed -i.orig -e 's/ long-bits .*/ long-bits 64)/g' s/default.def
make

svenha avatar Feb 27 '24 09:02 svenha