diff --git a/CMakeLists.txt b/CMakeLists.txt index f5436cefe..468b63ec9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/rir/src/compiler/analysis/reference_count.h b/rir/src/compiler/analysis/reference_count.h index 111ed9847..af1c465aa 100644 --- a/rir/src/compiler/analysis/reference_count.h +++ b/rir/src/compiler/analysis/reference_count.h @@ -5,6 +5,7 @@ #include "dead.h" #include "generic_static_analysis.h" #include "utils/Map.h" +#include namespace rir { namespace pir { diff --git a/rir/src/compiler/native/allocator.cpp b/rir/src/compiler/native/allocator.cpp index d63e89987..8fcf22f8d 100644 --- a/rir/src/compiler/native/allocator.cpp +++ b/rir/src/compiler/native/allocator.cpp @@ -102,10 +102,7 @@ void NativeAllocator::compute() { } }; - size_t pos = 0; for (auto i : *bb) { - ++pos; - if (!needsASlot(i)) continue; diff --git a/rir/src/compiler/native/builtins.cpp b/rir/src/compiler/native/builtins.cpp index a6497c76e..e09ee5d42 100644 --- a/rir/src/compiler/native/builtins.cpp +++ b/rir/src/compiler/native/builtins.cpp @@ -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); } diff --git a/rir/src/compiler/native/pir_jit_llvm.cpp b/rir/src/compiler/native/pir_jit_llvm.cpp index f4c688292..1ad733dd2 100644 --- a/rir/src/compiler/native/pir_jit_llvm.cpp +++ b/rir/src/compiler/native/pir_jit_llvm.cpp @@ -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, @@ -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( diff --git a/rir/src/interpreter/interp.cpp b/rir/src/interpreter/interp.cpp index 7e25478d7..53c030d0f 100644 --- a/rir/src/interpreter/interp.cpp +++ b/rir/src/interpreter/interp.cpp @@ -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(); } diff --git a/rir/src/interpreter/profiler.cpp b/rir/src/interpreter/profiler.cpp index edfd921c2..8771ab2b7 100644 --- a/rir/src/interpreter/profiler.cpp +++ b/rir/src/interpreter/profiler.cpp @@ -169,6 +169,7 @@ void RuntimeProfiler::initProfiler() { #else void RuntimeProfiler::initProfiler() {} +bool RuntimeProfiler::enabled() { return false; } #endif } // namespace rir diff --git a/rir/src/runtime/Code.h b/rir/src/runtime/Code.h index a15e0a437..7deb65528 100644 --- a/rir/src/runtime/Code.h +++ b/rir/src/runtime/Code.h @@ -11,7 +11,9 @@ #include #include +#ifndef __ARM_ARCH #include +#endif namespace rir { diff --git a/rir/src/runtime/Function.h b/rir/src/runtime/Function.h index 04d3ea0d3..065159840 100644 --- a/rir/src/runtime/Function.h +++ b/rir/src/runtime/Function.h @@ -80,9 +80,15 @@ struct Function : public RirRuntimeObject { 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; diff --git a/tools/build-gnur.sh b/tools/build-gnur.sh index 8cb64c401..5bfb48eb9 100755 --- a/tools/build-gnur.sh +++ b/tools/build-gnur.sh @@ -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 @@ -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 diff --git a/tools/build-llvm.sh b/tools/build-llvm.sh index ad167b730..00461bd40 100755 --- a/tools/build-llvm.sh +++ b/tools/build-llvm.sh @@ -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 diff --git a/tools/fetch-llvm.sh b/tools/fetch-llvm.sh index 47f9d3a8a..d15ddd48c 100755 --- a/tools/fetch-llvm.sh +++ b/tools/fetch-llvm.sh @@ -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