From 35d16bb5e29f504c9068b1cfb5f05b32d0bc3c0b Mon Sep 17 00:00:00 2001 From: Arnaud BOEGLIN Date: Thu, 11 Apr 2024 23:14:45 +0200 Subject: [PATCH] wip: investigation and first attempt at fixing float boxing causing issues --- compiler/main/Generate/LLVM/LLVM.hs | 20 +++++++++++++------- runtime/src/number.cpp | 6 +++--- runtime/src/number.hpp | 4 ++-- runtime/src/process.cpp | 2 +- scripts/build-runtime-libs | 4 +++- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/compiler/main/Generate/LLVM/LLVM.hs b/compiler/main/Generate/LLVM/LLVM.hs index cf575ed06..f85af5938 100644 --- a/compiler/main/Generate/LLVM/LLVM.hs +++ b/compiler/main/Generate/LLVM/LLVM.hs @@ -209,6 +209,10 @@ selectField :: Operand selectField = Operand.ConstantOperand (Constant.GlobalReference (Type.ptr $ Type.FunctionType boxType [stringType, recordType] False) (AST.mkName "madlib__record__internal__selectField")) +boxDouble :: Operand +boxDouble = + Operand.ConstantOperand (Constant.GlobalReference (Type.ptr $ Type.FunctionType boxType [Type.double] False) (AST.mkName "boxDouble")) + madlistHasMinLength :: Operand madlistHasMinLength = Operand.ConstantOperand (Constant.GlobalReference (Type.ptr $ Type.FunctionType Type.i1 [Type.i64, listType] False) (AST.mkName "madlib__list__internal__hasMinLength")) @@ -551,13 +555,14 @@ box :: (MonadIRBuilder m, MonadModuleBuilder m) => Operand -> m Operand box what = case typeOf what of -- Float Type.FloatingPointType _ -> do - ptr <- alloca Type.double Nothing 0 - boxWrap <- alloca (Type.ptr boxType) Nothing 0 - store ptr 0 what - ptr' <- bitcast ptr (Type.ptr boxType) - store boxWrap 0 ptr' - loaded <- load boxWrap 0 - load loaded 0 + call boxDouble [(what, [])] + -- ptr <- alloca Type.double Nothing 0 + -- boxWrap <- alloca (Type.ptr boxType) Nothing 0 + -- store ptr 0 what + -- ptr' <- bitcast ptr (Type.ptr boxType) + -- store boxWrap 0 ptr' + -- loaded <- load boxWrap 0 + -- load loaded 0 -- Integer Type.IntegerType 64 -> do @@ -2893,6 +2898,7 @@ generateLLVMModule env isMain currentModulePaths initialSymbolTable ast@Core.AST declareWithAttributes [FunctionAttribute.NoUnwind, FunctionAttribute.ReadNone, FunctionAttribute.OptimizeNone, FunctionAttribute.NoInline] (AST.mkName "llvm.dbg.declare") [Type.MetadataType, Type.MetadataType, Type.MetadataType] Type.void -- extern (AST.mkName "__dict_ctor__") [boxType, boxType] boxType + extern (AST.mkName "boxDouble") [Type.double] boxType extern (AST.mkName "madlib__record__internal__selectField") [stringType, recordType] boxType extern (AST.mkName "madlib__string__internal__areStringsEqual") [stringType, stringType] Type.i1 extern (AST.mkName "madlib__string__internal__areStringsNotEqual") [stringType, stringType] Type.i1 diff --git a/runtime/src/number.cpp b/runtime/src/number.cpp index 2ddcd4ca0..e1ccd186e 100644 --- a/runtime/src/number.cpp +++ b/runtime/src/number.cpp @@ -47,14 +47,14 @@ char *madlib__number__internal__showFloat(double d) { } -double unboxDouble(double *x) { +double unboxDouble(void *x) { return *(double*)&x; } -double *boxDouble(double x) { +void *boxDouble(double x) { double **w = (double**) &x; - return *w; + return (void*) *w; } diff --git a/runtime/src/number.hpp b/runtime/src/number.hpp index a5e0ec435..d67c9d675 100644 --- a/runtime/src/number.hpp +++ b/runtime/src/number.hpp @@ -15,8 +15,8 @@ char *madlib__number__internal__showByte(unsigned char i); madlib__maybe__Maybe_t *madlib__number__scanByte(char *s); // Float -double unboxDouble(double *x); -double *boxDouble(double x); +double unboxDouble(void *x); +void *boxDouble(double x); char *madlib__number__internal__showFloat(double d); diff --git a/runtime/src/process.cpp b/runtime/src/process.cpp index 2decd3c44..959de53c5 100644 --- a/runtime/src/process.cpp +++ b/runtime/src/process.cpp @@ -47,7 +47,6 @@ static char **ARGV = NULL; void __main__init__(int argc, char **argv) { GC_set_dont_precollect(1); - GC_allow_register_threads(); // TODO: make min alloc and initial heap size available as compilation options size_t minAlloc = 50 * 1024 * 1024; // 50MB @@ -67,6 +66,7 @@ void __main__init__(int argc, char **argv) { }; GC_set_stackbottom(NULL, &stackBase); + GC_allow_register_threads(); madlib__stack__init(stackBottom, __main__start__); } diff --git a/scripts/build-runtime-libs b/scripts/build-runtime-libs index dca0bf2b3..874f4ca3d 100755 --- a/scripts/build-runtime-libs +++ b/scripts/build-runtime-libs @@ -32,7 +32,7 @@ fi # build libgc -LIBGC_VERSION="8.2.0" +LIBGC_VERSION="8.2.6" wget "https://github.com/ivmai/bdwgc/archive/refs/tags/v${LIBGC_VERSION}.zip" unzip "v${LIBGC_VERSION}.zip" -d libgc @@ -41,6 +41,8 @@ sed -i -e 's/scan_size = 2/scan_size = 8/g' alloc.c git clone https://github.com/ivmai/libatomic_ops.git make -f Makefile.direct CFLAGS_EXTRA="-O3 -DDONT_ADD_BYTE_AT_END -DNO_DEBUGGING -DGC_THREADS -DLARGE_CONFIG -DGC_DISABLE_INCREMENTAL" c++ make -f Makefile.direct CFLAGS_EXTRA="-O3 -DDONT_ADD_BYTE_AT_END -DNO_DEBUGGING -DGC_THREADS -DLARGE_CONFIG -DGC_DISABLE_INCREMENTAL" +# make -f Makefile.direct CFLAGS_EXTRA="-O3 -DTHREAD_LOCAL_ALLOC -DGC_DISCOVER_TASK_THREADS -DDEBUG_THREADS -DGC_ASSERTIONS -DDONT_ADD_BYTE_AT_END -DFIND_LEAK -DGC_DEBUG -DGC_DUMP_REGULARLY -DKEEP_BACK_PTRS -DALL_INTERIOR_POINTERS -DGC_PRINT_VERBOSE_STATS -DPRINT_BLACK_LIST -DGC_THREADS -DLARGE_CONFIG -DGC_DISABLE_INCREMENTAL" c++ +# make -f Makefile.direct CFLAGS_EXTRA="-O3 -DTHREAD_LOCAL_ALLOC -DGC_DISCOVER_TASK_THREADS -DDEBUG_THREADS -DGC_ASSERTIONS -DDONT_ADD_BYTE_AT_END -DFIND_LEAK -DGC_DEBUG -DGC_DUMP_REGULARLY -DKEEP_BACK_PTRS -DALL_INTERIOR_POINTERS -DGC_PRINT_VERBOSE_STATS -DPRINT_BLACK_LIST -DGC_THREADS -DLARGE_CONFIG -DGC_DISABLE_INCREMENTAL" cd ../.. cp "libgc/bdwgc-${LIBGC_VERSION}/gc.a" ../runtime/lib/libgc.a cp "libgc/bdwgc-${LIBGC_VERSION}/gccpp.a" ../runtime/lib/libgccpp.a