Skip to content

Commit

Permalink
wip: investigation and first attempt at fixing float boxing causing i…
Browse files Browse the repository at this point in the history
…ssues
  • Loading branch information
aboeglin committed Apr 11, 2024
1 parent 3187435 commit 35d16bb
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
20 changes: 13 additions & 7 deletions compiler/main/Generate/LLVM/LLVM.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -2893,6 +2898,7 @@ generateLLVMModule env isMain currentModulePaths initialSymbolTable [email protected]
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
Expand Down
6 changes: 3 additions & 3 deletions runtime/src/number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand Down
4 changes: 2 additions & 2 deletions runtime/src/number.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion runtime/src/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__);
}

Expand Down
4 changes: 3 additions & 1 deletion scripts/build-runtime-libs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 35d16bb

Please sign in to comment.