Skip to content

Commit

Permalink
Use statepoints to compute stack roots when invoking koreCollect (#426)
Browse files Browse the repository at this point in the history
* add llvm file with opaque function definitions

* add llvm pass

* implement getMangledTypeStr

* introduce references to address space 1

* refactor allocateTerm

* add statepoint gc strategy

* add noreturn to finish_rewriting

* add missing typed alloction functions

* add code to retrieve stack map

* parse stack map when application boots

* remove manual root tracking

* use libunwind to walk stack for roots

* wire up gc in scripts

* use tailcc instead of fastcc

* add gc-leaf-function attribute to tail calls

* fix bug involving match reason functions

* reverse order of relocations in llvm pass

* fixes for llvm 10

* fix llvm pass for llvm 10

* fix bug on mac os

* fix cmake

* use LLVM_VERSION_MAJOR

* fix mac os

* link against libunwind

* fix name mangling issue on mac os

* fix ciscript

* fix linking against libunwind on mac os

* add back missing dependencies on libunwind

* try to fix nix on mac os

* add comments

* add LLVM_LINK variable to cmake

* install opaque.ll in separate directory

* change llvm-kompile script to invoke llvm-link

* make sure opaque functions will actually get inlined

* break long line

* skip constants in stack map

* remove gc-leaf-function attribute from tail calls

* add llvm pass to mark tail calls as gc-leaf-function

* fix gc offsets

* format new files

* fix bug on llvm 10

* fix format

* don't reverse on older llvm

* use slightly lower threshold for iterated optimization on nix

This lowered setting is required because nix still uses MacOS SDK 10.12
which has a bug when the binary size is too big

* fix code review comments

* fix llvm 12

* Fix merge conflict

* Update lib/llvm/EmitGCLayoutInfo.cpp

Co-authored-by: Bruce Collie <brucecollie82@gmail.com>

* Update MarkTailCallsAsGCLeaf.cpp

Co-authored-by: Bruce Collie <brucecollie82@gmail.com>
  • Loading branch information
Dwight Guth and Baltoli authored Sep 29, 2021
1 parent b638e48 commit 4c6095d
Show file tree
Hide file tree
Showing 29 changed files with 874 additions and 292 deletions.
16 changes: 14 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,31 @@ install(
DESTINATION lib/kllvm/llvm/main
)

install(
FILES runtime/opaque/opaque.ll
DESTINATION lib/kllvm/llvm/opaque
)

find_program(OPT opt-12)
find_program(OPT opt-11)
find_program(OPT opt-10)
find_program(OPT opt)
find_program(LLVM_LINK llvm-link-12)
find_program(LLVM_LINK llvm-link-11)
find_program(LLVM_LINK llvm-link-10)
find_program(LLVM_LINK llvm-link)
find_program(LLC llc-12)
find_program(LLC llc-11)
find_program(LLC llc-10)
find_program(LLC llc)
if(${OPT} STREQUAL "OPT-NOTFOUND")
message(FATAL_ERROR "Could not find an opt binary. Is llvm installed on your PATH?")
endif()
if(${LLC} STREQUAL "OPT-NOTFOUND")
message(FATAL_ERROR "Could not find an llvm binary. Is llvm installed on your PATH?")
if(${LLC} STREQUAL "LLC-NOTFOUND")
message(FATAL_ERROR "Could not find an llc binary. Is llvm installed on your PATH?")
endif()
if(${LLVM_LINK} STREQUAL "LLVM_LINK-NOTFOUND")
message(FATAL_ERROR "Could not find an llvm-link binary. Is llvm installed on your PATH?")
endif()

file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin)
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ENV TZ America/Chicago
ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && \
apt-get install -y git cmake clang-10 llvm-10-tools lld-10 zlib1g-dev flex libboost-test-dev libgmp-dev libmpfr-dev libyaml-dev libjemalloc-dev curl maven pkg-config
apt-get install -y git cmake clang-10 llvm-10-tools lld-10 zlib1g-dev flex libboost-test-dev libgmp-dev libmpfr-dev libyaml-dev libjemalloc-dev libunwind-dev curl maven pkg-config

ARG USER_ID=1000
ARG GROUP_ID=1000
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.arch
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM archlinux:base

RUN pacman -Syyu --noconfirm && \
pacman -S --noconfirm base-devel git cmake clang llvm lld flex boost gmp mpfr libyaml jemalloc curl maven pkg-config
pacman -S --noconfirm base-devel git cmake clang llvm lld flex boost gmp mpfr libyaml jemalloc libunwind curl maven pkg-config

ARG USER_ID=1000
ARG GROUP_ID=1000
Expand Down
2 changes: 1 addition & 1 deletion INSTALL.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
On Ubuntu 20.04:
```shell
sudo apt-get update
sudo apt-get install git cmake clang-10 llvm-10-tools lld-10 zlib1g-dev flex libboost-test-dev libgmp-dev libmpfr-dev libyaml-dev libjemalloc-dev curl maven pkg-config
sudo apt-get install git cmake clang-10 llvm-10-tools lld-10 zlib1g-dev flex libboost-test-dev libgmp-dev libmpfr-dev libyaml-dev libjemalloc-dev libunwind-dev curl maven pkg-config
git clone https://github.com/kframework/llvm-backend --recursive
cd llvm-backend
mkdir build
Expand Down
21 changes: 18 additions & 3 deletions bin/llvm-kompile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ if [ $# -lt 3 ]; then
fi
mod="$(mktemp tmp.XXXXXXXXXX)"
modopt="$(mktemp tmp.XXXXXXXXXX)"
trap "rm -rf $dt_dir $mod $modopt" INT TERM EXIT
modcombined="$(mktemp tmp.XXXXXXXXXX)"
modcombinedopt="$(mktemp tmp.XXXXXXXXXX)"
trap "rm -rf $dt_dir $mod $modopt $modcombined $modcombinedopt" INT TERM EXIT
definition="$1"
shift
compile=true
Expand All @@ -31,12 +33,25 @@ if $compile; then
-g)
debug=1
;;
-O[0-3])
opt_flags="$arg"
;;
-O.)
echo "$0: invalid optimization level"
exit 1
;;
*)
;;
esac
done

# code generation
"$(dirname "$0")"/llvm-kompile-codegen "$definition" "$dt_dir"/dt.yaml "$dt_dir" $debug > "$mod"
@OPT@ -mem2reg -tailcallelim -tailcallopt "$mod" -o "$modopt"

# optimization
@OPT@ -load "$(dirname "$0")"/../lib/kllvm/libLLVMPass.so -mem2reg -tailcallelim $opt_flags -mark-tail-calls-as-gc-leaf -rewrite-statepoints-for-gc -dce -emit-gc-layout-info "$mod" -o "$modopt"
@LLVM_LINK@ "$modopt" "$(dirname "$0")"/../lib/kllvm/llvm/opaque/opaque.ll -o "$modcombined"
@OPT@ "$modcombined" -always-inline -o "$modcombinedopt"
else
main="$1"
shift
Expand All @@ -45,4 +60,4 @@ fi
if [[ "$OSTYPE" != "darwin"* ]]; then
flags=-fuse-ld=lld
fi
"$(dirname "$0")"/llvm-kompile-clang "$modopt" "$main" @LLVM_KOMPILE_LTO@ -fno-stack-protector $flags "$@"
"$(dirname "$0")"/llvm-kompile-clang "$modcombinedopt" "$main" @LLVM_KOMPILE_LTO@ -fno-stack-protector $flags "$@"
10 changes: 5 additions & 5 deletions bin/llvm-kompile-clang
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fi

if [[ "$OSTYPE" == "darwin"* ]]; then
libraries="-liconv -lncurses"
flags="-L/usr/local/opt/libffi/lib -L/usr/local/lib -Wl,-u,_sort_table -I /usr/local/include"
flags="-L/usr/local/opt/libffi/lib -L/usr/local/lib -L$(dirname "@LLC@")/../lib -Wl,-u,_sort_table -I /usr/local/include"
else
libraries="-ltinfo"
flags="-Wl,-u,sort_table"
Expand All @@ -84,7 +84,7 @@ if ! $save_temps; then
fi

if [ "$lto" = "lto" ]; then
flags="$flags -flto -Wl,-mllvm,-tailcallopt"
flags="$flags -flto"
files=("$LIBDIR"/llvm/*.ll)
if ! $link; then
mv "$modopt" "$output_file"
Expand All @@ -96,13 +96,13 @@ else
if ! $link; then
modasm="$output_file"
fi
run @LLC@ -tailcallopt "$modopt" -mtriple=@BACKEND_TARGET_TRIPLE@ -filetype=obj $llc_opt_flags $llc_flags -o "$modasm"
run @LLC@ "$modopt" -mtriple=@BACKEND_TARGET_TRIPLE@ -filetype=obj $llc_opt_flags $llc_flags -o "$modasm"
modopt="$modasm"
fi
if $link; then
for file in "$LIBDIR"/llvm/*.ll; do
tmp="$tmpdir/`basename "$file"`.o"
run @LLC@ -tailcallopt "$file" -mtriple=@BACKEND_TARGET_TRIPLE@ -filetype=obj $llc_opt_flags $llc_flags -o "$tmp"
run @LLC@ "$file" -mtriple=@BACKEND_TARGET_TRIPLE@ -filetype=obj $llc_opt_flags $llc_flags -o "$tmp"
files+=("$tmp")
done
fi
Expand All @@ -111,7 +111,7 @@ fi
if [ "$main" = "static" ]; then
all_libraries=
else
all_libraries="$libraries -lgmp -lmpfr -lpthread -ldl -lffi -ljemalloc"
all_libraries="$libraries -lgmp -lmpfr -lpthread -ldl -lffi -ljemalloc -lunwind"
fi

if $link; then
Expand Down
6 changes: 3 additions & 3 deletions ciscript
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
export PATH=$HOME/.cargo/bin:$HOME/.local/bin:$PATH

if [[ "$OSTYPE" == "darwin"* ]]; then
export PATH="/usr/local/opt/flex/bin:/usr/local/opt/llvm@6/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/llvm@6/lib -L/usr/local/opt/flex/lib"
export CPPFLAGS="-I/usr/local/opt/llvm@6/include -I/usr/local/opt/flex/include"
export PATH="/usr/local/opt/flex/bin:/usr/local/opt/llvm@12/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/llvm@12/lib -L/usr/local/opt/flex/lib"
export CPPFLAGS="-I/usr/local/opt/llvm@12/include -I/usr/local/opt/flex/include"
export FLEX_EXECUTABLE="/usr/local/opt/flex/bin/flex"
fi

Expand Down
2 changes: 1 addition & 1 deletion default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ let
mkdir -p "$out/bin"
cp ${llvm-backend.src}/bin/llvm-kompile-testing "$out/bin"
sed -i "$out/bin/llvm-kompile-testing" \
-e '/@PROJECT_SOURCE_DIR@/ c ${java} -jar ${jar} $definition qbaL $dt_dir 1'
-e '/@PROJECT_SOURCE_DIR@/ c ${java} -jar ${jar} $definition qbaL $dt_dir 9/10'
chmod +x "$out/bin/llvm-kompile-testing"
patchShebangs "$out/bin/llvm-kompile-testing"
'';
Expand Down
24 changes: 18 additions & 6 deletions include/kllvm/codegen/CreateTerm.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class CreateTerm {
llvm::Value *
createHook(KORECompositePattern *hookAtt, KORECompositePattern *pattern);
llvm::Value *createFunctionCall(
std::string name, KORECompositePattern *pattern, bool sret, bool fastcc);
std::string name, KORECompositePattern *pattern, bool sret, bool tailcc);
llvm::Value *
notInjectionCase(KORECompositePattern *constructor, llvm::Value *val);

Expand Down Expand Up @@ -52,12 +52,12 @@ class CreateTerm {
* function actually returns void and the return value is via a pointe. Note
* that this can be set to true even if the function does not return a struct,
* in which case its value is ignored. load: if the function returns a struct
* via sret, then if load is true, we load the value fastcc: true if we should
* use the fastcc calling convention returned from the function before
* via sret, then if load is true, we load the value tailcc: true if we should
* use the tailcc calling convention returned from the function before
* returning it. */
llvm::Value *createFunctionCall(
std::string name, ValueType returnCat,
const std::vector<llvm::Value *> &args, bool sret, bool fastcc);
const std::vector<llvm::Value *> &args, bool sret, bool tailcc);

llvm::BasicBlock *getCurrentBlock() const { return CurrentBlock; }
};
Expand Down Expand Up @@ -103,11 +103,23 @@ llvm::Type *getParamType(ValueType sort, llvm::Module *Module);
void addAbort(llvm::BasicBlock *block, llvm::Module *Module);

llvm::Value *allocateTerm(
llvm::Type *AllocType, llvm::BasicBlock *block,
ValueType Cat, llvm::Type *AllocType, llvm::BasicBlock *block,
const char *allocFn = "koreAlloc");
llvm::Value *allocateTerm(
ValueType Cat, llvm::Type *AllocType, llvm::Value *Len,
llvm::BasicBlock *block, const char *allocFn = "koreAlloc");
llvm::Value *allocateTermNoReloc(
llvm::Type *AllocType, llvm::BasicBlock *block,
const char *allocFn = "koreAllocAlwaysGC");
llvm::Value *allocateTermNoReloc(
llvm::Type *AllocType, llvm::Value *Len, llvm::BasicBlock *block,
const char *allocFn = "koreAlloc");
const char *allocFn = "koreAllocAlwaysGC");

// see comment on runtime/opaque/opaque.ll for explanation about why these
// functions exist
llvm::Value *addrspaceCast0to1(llvm::Value *val, llvm::BasicBlock *block);
llvm::Value *addrspaceCast1to0(llvm::Value *val, llvm::BasicBlock *block);
llvm::Value *ptrToInt(llvm::Value *val, llvm::BasicBlock *block);
} // namespace kllvm

#endif // CREATE_TERM_H
5 changes: 5 additions & 0 deletions include/kllvm/codegen/Util.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#ifndef KLLVM_UTIL_H
#define KLLVM_UTIL_H

#include "kllvm/ast/AST.h"
#include "llvm/IR/Module.h"

namespace kllvm {

// Returns a reference to the function declaration for a memory allocation
// function with the given name, adding a declaration to the current module if
// one does not yet exist
llvm::Function *
koreHeapAlloc(ValueType Cat, std::string name, llvm::Module *module);
llvm::Function *koreHeapAlloc(std::string name, llvm::Module *module);

// If Value is an instance of llvm::Function, cast and return. Otherwise, print
Expand All @@ -29,6 +32,8 @@ static llvm::Function *getOrInsertFunction(llvm::Module *module, Ts... Args) {

llvm::StructType *getTypeByName(llvm::Module *module, std::string name);

std::string getMangledTypeStr(llvm::Type *Ty, bool &HasUnnamedType);

} // namespace kllvm

#endif // KLLVM_UTIL_H
4 changes: 3 additions & 1 deletion include/runtime/collect.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ using map_impl = map::iterator::tree_t;
using set_node = set::iterator::node_t;
using set_impl = set::iterator::tree_t;

void parseStackMap(void);

extern "C" {
extern size_t numBytesLiveAtCollection[1 << AGE_WIDTH];
bool during_gc(void);
Expand All @@ -36,7 +38,7 @@ void migrate_map(void *m);
void migrate_set(void *s);
void migrate_collection_node(void **nodePtr);
void setKoreMemoryFunctionsForGMP(void);
void koreCollect(void **, uint8_t, layoutitem *);
void koreCollect(void);
}

#ifdef GC_DBG
Expand Down
1 change: 1 addition & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
add_subdirectory(parser)
add_subdirectory(ast)
add_subdirectory(codegen)
add_subdirectory(llvm)

separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
add_definitions(${LLVM_DEFINITIONS_LIST})
Loading

0 comments on commit 4c6095d

Please sign in to comment.