Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macOS support + update code to fetch and build llvm #1239

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -180,5 +180,10 @@ if(APPLE)
target_link_libraries(${PROJECT_NAME} R)
# to resolve build error from
# https://www.gnu.org/software/gettext/FAQ.html#integrating_undefined
target_link_libraries(${PROJECT_NAME} -lintl)
# target_link_libraries(${PROJECT_NAME} -lintl)
include_directories(${LLVM_DIR}/include)
target_link_directories(${PROJECT_NAME} INTERFACE ${LLVM_DIR}/lib)
# Note: May need to update the version here
include_directories(/opt/homebrew/Cellar/gettext/0.21.1/include)
target_link_libraries(${PROJECT_NAME} /opt/homebrew/Cellar/gettext/0.21.1/lib/libintl.dylib)
endif(APPLE)
1 change: 1 addition & 0 deletions rir/src/compiler/analysis/reference_count.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "dead.h"
#include "generic_static_analysis.h"
#include "utils/Map.h"
#include <map>

namespace rir {
namespace pir {
Expand Down
3 changes: 0 additions & 3 deletions rir/src/compiler/native/allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,7 @@ void NativeAllocator::compute() {
}
};

size_t pos = 0;
for (auto i : *bb) {
++pos;

if (!needsASlot(i))
continue;

Expand Down
4 changes: 4 additions & 0 deletions rir/src/compiler/native/builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,11 @@ void recordTypefeedbackImpl(Opcode* pos, rir::Code* code, SEXP value) {

void assertFailImpl(const char* msg) {
std::cout << "Assertion in jitted code failed: '" << msg << "'\n";
#ifdef __ARM_ARCH
__builtin_debugtrap();
#else
asm("int3");
#endif
}

void printValueImpl(SEXP v) { Rf_PrintValue(v); }
Expand Down
8 changes: 5 additions & 3 deletions rir/src/compiler/native/pir_jit_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ void PirJitLLVM::initializeLLVM() {
// name. symbols starting with "ept_" are external pointers, the ones
// starting with "efn_" are external function pointers. these must exist in
// the host process.
// NEW: On macOS ARM the symbols start with _ept_ and _epn_
class ExtSymbolGenerator : public llvm::orc::DefinitionGenerator {
public:
Error tryToGenerate(LookupState& LS, LookupKind K, JITDylib& JD,
Expand All @@ -565,11 +566,12 @@ void PirJitLLVM::initializeLLVM() {
for (auto s : LookupSet) {
auto& Name = s.first;
auto n = (*Name).str();
auto ept = n.substr(0, 4) == "ept_";
auto efn = n.substr(0, 4) == "efn_";
auto ept = n.substr(0, 4) == "ept_" || n.substr(0, 5) == "_ept_";
auto efn = n.substr(0, 4) == "efn_" || n.substr(0, 5) == "_efn_";

if (ept || efn) {
auto addrStr = n.substr(4);
auto isUnderscoreVariant = n.substr(0, 1) == "_";
auto addrStr = n.substr(isUnderscoreVariant ? 5 : 4);
auto addr = std::strtoul(addrStr.c_str(), nullptr, 16);
NewSymbols[Name] = JITEvaluatedSymbol(
static_cast<JITTargetAddress>(
Expand Down
4 changes: 4 additions & 0 deletions rir/src/interpreter/interp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3888,7 +3888,11 @@ SEXP evalRirCode(Code* c, SEXP env, const CallContext* callCtxt,
INSTRUCTION(ret_) { goto eval_done; }

INSTRUCTION(int3_) {
#ifdef __ARM_ARCH
__builtin_debugtrap();
#else
asm("int3");
#endif
NEXT();
}

Expand Down
1 change: 1 addition & 0 deletions rir/src/interpreter/profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ void RuntimeProfiler::initProfiler() {

#else
void RuntimeProfiler::initProfiler() {}
bool RuntimeProfiler::enabled() { return false; }
#endif

} // namespace rir
2 changes: 2 additions & 0 deletions rir/src/runtime/Code.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
#include <ostream>
#include <time.h>

#ifndef __ARM_ARCH
#include <asm/msr.h>
#endif

namespace rir {

Expand Down
6 changes: 6 additions & 0 deletions rir/src/runtime/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,15 @@ struct Function : public RirRuntimeObject<Function, FUNCTION_MAGIC> {
void addDeoptCount(size_t n) { deoptCount_ += n; }

static inline unsigned long rdtsc() {
#ifdef __ARM_ARCH
uint64_t val;
asm volatile("mrs %0, cntvct_el0" : "=r" (val));
return val;
#else
unsigned low, high;
asm volatile("rdtsc" : "=a"(low), "=d"(high));
return ((low) | ((uint64_t)(high) << 32));
#endif
}
static constexpr unsigned long MAX_TIME_MEASURE = 1e9;

Expand Down
9 changes: 7 additions & 2 deletions tools/build-gnur.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function build_r {
echo "-> configure $NAME"
cd $R_DIR
if [ $USING_OSX -eq 1 ]; then
CFLAGS="-O2 -g -DSWITCH_TO_NAMED=$SND" ./configure --enable-R-shlib --with-internal-tzcode --with-ICU=no || cat config.log
CFLAGS="-O2 -g -DSWITCH_TO_NAMED=$SND -I/opt/homebrew/include" LDFLAGS="-L/opt/homebrew/lib" ./configure --enable-R-shlib --with-internal-tzcode --with-ICU=no --with-x=no --with-aqua=no || cat config.log
else
CFLAGS="-O2 -g -DSWITCH_TO_NAMED=$SND" ./configure
fi
Expand Down Expand Up @@ -100,7 +100,12 @@ function build_r {
fi

echo "-> building $NAME"
make -j8
if [ $USING_OSX -eq 1 ]; then
# We need `C_INCLUDE_PATH` here AND we need to include `/opt/homebrew/bin` in `./configure`
C_INCLUDE_PATH=/opt/homebrew/include make -j8
else
make -j8
fi
}

build_r custom-r
20 changes: 10 additions & 10 deletions tools/build-llvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ fi
SRC_DIR=`cd ${SCRIPTPATH}/.. && pwd`
. "${SCRIPTPATH}/script_include.sh"

if [ -d "${SRC_DIR}/external/llvm-8.0.0" ]; then
echo "${SRC_DIR}/external/llvm-8.0.0 already exists. Remove it to install a debug version of llvm."
if [ -d "${SRC_DIR}/external/llvm-12" ]; then
echo "${SRC_DIR}/external/llvm-12 already exists. Remove it to install a debug version of llvm."
exit 1
fi

cd "${SRC_DIR}/external"

if [ ! -f llvm-8.0.0.src.tar.xz ]; then
wget http://releases.llvm.org/8.0.0/llvm-8.0.0.src.tar.xz
if [ ! -f llvm-12.0.0.src.tar.xz ]; then
wget https://github.com/llvm/llvm-project/releases/download/llvmorg-12.0.0/llvm-12.0.0.src.tar.xz
fi
if [ ! -d "llvm-8.0.0.src" ]; then
tar xf llvm-8.0.0.src.tar.xz
mkdir llvm-8.0.0
cd llvm-8.0.0.src
if [ ! -d "llvm-12.0.0.src" ]; then
tar xf llvm-12.0.0.src.tar.xz
mkdir llvm-12
cd llvm-12.0.0.src
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Debug -GNinja ..
ninja
else
cd llvm-8.0.0.src/build
cd llvm-12.0.0.src/build
fi
cmake -DCMAKE_INSTALL_PREFIX=../../llvm-8.0.0 -P cmake_install.cmake
cmake -DCMAKE_INSTALL_PREFIX=../../llvm-12 -P cmake_install.cmake
4 changes: 4 additions & 0 deletions tools/fetch-llvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ fi
SRC_DIR=`cd ${SCRIPTPATH}/.. && pwd`
. "${SCRIPTPATH}/script_include.sh"

if [[ $(uname -m) == "arm64" ]]; then
echo "there is no LLVM 12 distribution for ARM64, so we will try to build instead"
exec "${SCRIPTPATH}/build-llvm.sh"
fi

if [[ "$OSTYPE" == "darwin"* ]]; then
USING_OSX=1
Expand Down