From cefd3729c18a2eb3184d7c786ac07ac737d115b7 Mon Sep 17 00:00:00 2001 From: jakobeha Date: Mon, 22 May 2023 18:39:15 -0400 Subject: [PATCH 1/8] R builds on macOS but getting: JIT session error: Symbols not found: [ _ept_60000346b6c0, _ept_60000346be40, _ept_60000346bdb0, _ept_60000346b900, _efn_10252c0c0, _ept_10273d898, _ept_10273dd20, _ept_11a823e28, _ept_11a823f08, _ept_139812080, _ept_139812438, _ept_139812470, _ept_1398124e0, _ept_13981e690, _ept_13984b900, _ept_1712214d3, _ept_171221544, _ept_17122168c, _ept_171221781, _ept_171221794, _ept_171221aea, _ept_171222e24, _ept_600002f44c80, _ept_600002f453c0, _ept_600002f45f00, _ept_600002f46400, _ept_600002f4a480, _ept_600002f4e200, _ept_600002f4ed00, _ept_600002f4f700, _ept_600002f4fcc0, _ept_600002f4ff40, _ept_60000344a340, _ept_60000344a670, _ept_60000344a850, _ept_60000344ab80, _ept_60000344abb0, _ept_60000344af70, _ept_60000344b0f0, _ept_60000344b390, _ept_60000344b510, _ept_60000344b570, _ept_60000344b6f0, _ept_60000344b870, _ept_60000344b8d0, _ept_60000344ba50, _ept_60000344bae0, _ept_60000344bb70, _ept_60000344bd50, _ept_60000344bed0, _ept_60000346b480, _ept_60000346b540, _ept_60000346b570, _ept_60000346b600, _ept_60000346b630 ] --- CMakeLists.txt | 6 +++++- rir/src/compiler/analysis/reference_count.h | 1 + rir/src/compiler/native/builtins.cpp | 4 ++++ rir/src/interpreter/interp.cpp | 4 ++++ rir/src/interpreter/profiler.cpp | 1 + rir/src/runtime/Code.h | 2 ++ rir/src/runtime/Function.h | 6 ++++++ tools/build-gnur.sh | 8 ++++++-- 8 files changed, 29 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f5436cefe..eb48b7f22 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -180,5 +180,9 @@ 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) + # 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/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/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..ab4baa8c6 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" LDFLAGS="-L/opt/homebrew/lib" ./configure --enable-R-shlib --with-internal-tzcode --with-ICU=no --with-x=no || cat config.log else CFLAGS="-O2 -g -DSWITCH_TO_NAMED=$SND" ./configure fi @@ -100,7 +100,11 @@ function build_r { fi echo "-> building $NAME" - make -j8 + if [ $USING_OSX -eq 1 ]; then + MACOSX_DEPLOYMENT_TARGET=12.0 C_INCLUDE_PATH=/opt/homebrew/include make -j8 + else + make -j8 + fi } build_r custom-r From a086c9e2a1387f18fa94d3c1f83b22e4c872ca6e Mon Sep 17 00:00:00 2001 From: jakobeha Date: Mon, 22 May 2023 23:04:56 -0400 Subject: [PATCH 2/8] Compile R on the correct macOS by not using devQuartz.c, which uses obsolete symbols --- tools/build-gnur.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tools/build-gnur.sh b/tools/build-gnur.sh index ab4baa8c6..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" LDFLAGS="-L/opt/homebrew/lib" ./configure --enable-R-shlib --with-internal-tzcode --with-ICU=no --with-x=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 @@ -101,7 +101,8 @@ function build_r { echo "-> building $NAME" if [ $USING_OSX -eq 1 ]; then - MACOSX_DEPLOYMENT_TARGET=12.0 C_INCLUDE_PATH=/opt/homebrew/include make -j8 + # 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 From afc32964eef1cb52e97a48fb120159f77d84ec3c Mon Sep 17 00:00:00 2001 From: jakobeha Date: Mon, 22 May 2023 23:05:58 -0400 Subject: [PATCH 3/8] support LLVM anonymous symbols names in macOS, or maybe llvm 12.01 or some other difference in my environment --- rir/src/compiler/native/pir_jit_llvm.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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( From 666e5d5f37ecf1c5895290e7decbd28b8a9acc6d Mon Sep 17 00:00:00 2001 From: jakobeha Date: Tue, 23 May 2023 09:17:50 -0400 Subject: [PATCH 4/8] update build-llvm to use llvm 12.0.1 --- tools/build-llvm.sh | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tools/build-llvm.sh b/tools/build-llvm.sh index ad167b730..e5b23fc1e 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.1.src.tar.xz ]; then + wget http://releases.llvm.org/12.0.1/llvm-12.0.1.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.1.src" ]; then + tar xf llvm-12.0.1.src.tar.xz + mkdir llvm-12 + cd llvm-12.0.1.src mkdir build cd build cmake -DCMAKE_BUILD_TYPE=Debug -GNinja .. ninja else - cd llvm-8.0.0.src/build + cd llvm-12.0.1.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 From 926347249e55b2f8ee4a0f2b8891a714656a295a Mon Sep 17 00:00:00 2001 From: jakobeha Date: Tue, 23 May 2023 09:19:28 -0400 Subject: [PATCH 5/8] remove unused variable because it gets promoted to compile error on release build --- rir/src/compiler/native/allocator.cpp | 3 --- 1 file changed, 3 deletions(-) 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; From 63ed7f76388b3e535a1949e7ee03a208af000009 Mon Sep 17 00:00:00 2001 From: jakobeha Date: Wed, 24 May 2023 11:02:13 -0400 Subject: [PATCH 6/8] revert to LLVM 12.0.0, 12.0.1 should work the same but just in case --- tools/build-llvm.sh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tools/build-llvm.sh b/tools/build-llvm.sh index e5b23fc1e..ae18537cd 100755 --- a/tools/build-llvm.sh +++ b/tools/build-llvm.sh @@ -18,18 +18,18 @@ fi cd "${SRC_DIR}/external" -if [ ! -f llvm-12.0.1.src.tar.xz ]; then - wget http://releases.llvm.org/12.0.1/llvm-12.0.1.src.tar.xz +if [ ! -f llvm-12.0.0.src.tar.xz ]; then + wget http://releases.llvm.org/12.0.0/llvm-12.0.0.src.tar.xz fi -if [ ! -d "llvm-12.0.1.src" ]; then - tar xf llvm-12.0.1.src.tar.xz +if [ ! -d "llvm-12.0.0.src" ]; then + tar xf llvm-12.0.0.src.tar.xz mkdir llvm-12 - cd llvm-12.0.1.src + cd llvm-12.0.0.src mkdir build cd build cmake -DCMAKE_BUILD_TYPE=Debug -GNinja .. ninja else - cd llvm-12.0.1.src/build + cd llvm-12.0.0.src/build fi cmake -DCMAKE_INSTALL_PREFIX=../../llvm-12 -P cmake_install.cmake From dccf28b1cab449f4d80910bc5bccfb11061bea66 Mon Sep 17 00:00:00 2001 From: jakobeha Date: Wed, 24 May 2023 13:42:37 -0400 Subject: [PATCH 7/8] fixes to fetch, build, and link llvm --- CMakeLists.txt | 1 + tools/build-llvm.sh | 2 +- tools/fetch-llvm.sh | 4 ++++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index eb48b7f22..468b63ec9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -182,6 +182,7 @@ if(APPLE) # https://www.gnu.org/software/gettext/FAQ.html#integrating_undefined # 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) diff --git a/tools/build-llvm.sh b/tools/build-llvm.sh index ae18537cd..00461bd40 100755 --- a/tools/build-llvm.sh +++ b/tools/build-llvm.sh @@ -19,7 +19,7 @@ fi cd "${SRC_DIR}/external" if [ ! -f llvm-12.0.0.src.tar.xz ]; then - wget http://releases.llvm.org/12.0.0/llvm-12.0.0.src.tar.xz + wget https://github.com/llvm/llvm-project/releases/download/llvmorg-12.0.0/llvm-12.0.0.src.tar.xz fi if [ ! -d "llvm-12.0.0.src" ]; then tar xf llvm-12.0.0.src.tar.xz 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 From 443d7321c55751d803269466b148a0e7fbcbf7f0 Mon Sep 17 00:00:00 2001 From: jakobeha Date: Mon, 29 May 2023 16:44:54 -0400 Subject: [PATCH 8/8] bump to get CI run manually re-triggering wasn't working, and test-sanitize failure seems random unless it's a formatting issue