Skip to content

Commit 8ace319

Browse files
authored
Merge pull request #215 from andrjohns/tbb-winarm64
Support building TBB on Windows arm64
2 parents 5aa08f8 + f59d3bf commit 8ace319

File tree

9 files changed

+93
-29
lines changed

9 files changed

+93
-29
lines changed

.Rbuildignore

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717
^tests/testthat/pkg/RcppParallelTest/src/.*\.s?o$
1818
^tools/tbb$
1919
^\.github$
20+
^patches

R/tbb.R

+7-5
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,14 @@ tbbCxxFlags <- function() {
4949
flags <- character()
5050

5151
# opt-in to TBB on Windows
52-
if (is_windows())
52+
if (is_windows()) {
5353
flags <- c(flags, "-DRCPP_PARALLEL_USE_TBB=1")
54+
if (R.version$arch == "aarch64") {
55+
# TBB does not have assembly code for Windows ARM64
56+
# so we need to use compiler builtins
57+
flags <- c(flags, "-DTBB_USE_GCC_BUILTINS")
58+
}
59+
}
5460

5561
# if TBB_INC is set, apply those library paths
5662
tbbInc <- Sys.getenv("TBB_INC", unset = TBB_INC)
@@ -81,10 +87,6 @@ tbbLdFlags <- function() {
8187
return(sprintf(fmt, asBuildPath(tbbLib)))
8288
}
8389

84-
# on Aarch64 builds, use the version of TBB provided by Rtools
85-
if (is_windows() && R.version$arch == "aarch64")
86-
return("-ltbb12 -ltbbmalloc")
87-
8890
# on Mac, Windows and Solaris, we need to explicitly link (#206)
8991
needsExplicitFlags <- is_mac() || is_windows() || (is_solaris() && !is_sparc())
9092
if (needsExplicitFlags) {

R/zzz.R

+1-5
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,8 @@ loadTbbLibrary <- function(name) {
2727

2828
.onLoad <- function(libname, pkgname) {
2929

30-
tbbLibraryName <- "tbb"
31-
if (is_windows() && R.version$arch == "aarch64")
32-
tbbLibraryName <- "tbb12"
33-
3430
# load tbb, tbbmalloc
35-
.tbbDllInfo <<- loadTbbLibrary(tbbLibraryName)
31+
.tbbDllInfo <<- loadTbbLibrary("tbb")
3632
.tbbMallocDllInfo <<- loadTbbLibrary("tbbmalloc")
3733

3834
# load tbbmalloc_proxy, but only if requested

inst/include/RcppParallel.h

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
#endif
1919

2020
#if RCPP_PARALLEL_USE_TBB
21+
#if defined(WINNT) && defined(__aarch64__) && !defined(TBB_USE_GCC_BUILTINS)
22+
#define TBB_USE_GCC_BUILTINS 1
23+
#endif
2124
# include "RcppParallel/TBB.h"
2225
#endif
2326

patches/windows_arm64.diff

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
diff --git a/src/tbb/build/Makefile.tbb b/src/tbb/build/Makefile.tbb
2+
index 8d155f80..c58f4fb1 100644
3+
--- a/src/tbb/build/Makefile.tbb
4+
+++ b/src/tbb/build/Makefile.tbb
5+
@@ -91,7 +91,11 @@ ifneq (,$(TBB.DEF))
6+
tbb.def: $(TBB.DEF) $(TBB.LST)
7+
$(CPLUS) $(PREPROC_ONLY) $< $(CPLUS_FLAGS) $(INCLUDES) > $@
8+
9+
-LIB_LINK_FLAGS += $(EXPORT_KEY)tbb.def
10+
+# LLVM on Windows doesn't need --version-script export
11+
+# https://reviews.llvm.org/D63743
12+
+ifeq (, $(WINARM64_CLANG))
13+
+ LIB_LINK_FLAGS += $(EXPORT_KEY)tbb.def
14+
+endif
15+
$(TBB.DLL): tbb.def
16+
endif
17+
18+
diff --git a/src/tbb/build/Makefile.tbbmalloc b/src/tbb/build/Makefile.tbbmalloc
19+
index 421e95c5..e7c38fa4 100644
20+
--- a/src/tbb/build/Makefile.tbbmalloc
21+
+++ b/src/tbb/build/Makefile.tbbmalloc
22+
@@ -74,7 +74,11 @@ ifneq (,$(MALLOC.DEF))
23+
tbbmalloc.def: $(MALLOC.DEF)
24+
$(CPLUS) $(PREPROC_ONLY) $< $(M_CPLUS_FLAGS) $(WARNING_SUPPRESS) $(INCLUDES) > $@
25+
26+
-MALLOC_LINK_FLAGS += $(EXPORT_KEY)tbbmalloc.def
27+
+# LLVM on Windows doesn't need --version-script export
28+
+# https://reviews.llvm.org/D63743
29+
+ifeq (, $(WINARM64_CLANG))
30+
+ MALLOC_LINK_FLAGS += $(EXPORT_KEY)tbbmalloc.def
31+
+endif
32+
$(MALLOC.DLL): tbbmalloc.def
33+
endif
34+
35+
diff --git a/src/tbb/src/tbbmalloc/TypeDefinitions.h b/src/tbb/src/tbbmalloc/TypeDefinitions.h
36+
index 3178442e..fd4b7956 100644
37+
--- a/src/tbb/src/tbbmalloc/TypeDefinitions.h
38+
+++ b/src/tbb/src/tbbmalloc/TypeDefinitions.h
39+
@@ -25,7 +25,7 @@
40+
# define __ARCH_ipf 1
41+
# elif defined(_M_IX86)||defined(__i386__) // the latter for MinGW support
42+
# define __ARCH_x86_32 1
43+
-# elif defined(_M_ARM)
44+
+# elif defined(_M_ARM) || defined(__aarch64__)
45+
# define __ARCH_other 1
46+
# else
47+
# error Unknown processor architecture for Windows

src/Makevars.in

+23-16
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,28 @@ ifeq ($(OS), Windows_NT)
2121
USE_TBB=Windows
2222
TBB_COPY_PATTERN=tbb*.dll
2323

24+
ARCH=$(shell "${R_HOME}/bin/R" --vanilla -s -e 'cat(R.version$$arch)')
25+
TBB_CXXFLAGS = @CXX11FLAGS@ -DTBB_NO_LEGACY=1
26+
ifeq "$(ARCH)" "aarch64"
27+
PKG_CPPFLAGS += -DTBB_USE_GCC_BUILTINS
28+
TBB_CXXFLAGS += -DTBB_USE_GCC_BUILTINS
29+
CLANG_CHECK := $(shell echo | $(CC) -E -dM - | findstr __clang__)
30+
ifneq ($(CLANG_CHECK), )
31+
WINARM64_CLANG=true
32+
endif
33+
endif
34+
2435
MAKE = make
2536
MAKEFLAGS = -e -j1
2637
MAKE_CMD = \
2738
MSYS2_ARG_CONV_EXCL="*" \
2839
CYGWIN=nodosfilewarning \
2940
CONLY="@WINDOWS_CC@" \
3041
CPLUS="@WINDOWS_CXX11@" \
31-
CXXFLAGS="@CXX11FLAGS@ -DTBB_NO_LEGACY=1" \
42+
CXXFLAGS="$(TBB_CXXFLAGS)" \
3243
PIC_KEY="@CXX11PICFLAGS@" \
3344
WARNING_SUPPRESS="" \
45+
WINARM64_CLANG="$(WINARM64_CLANG)" \
3446
$(MAKE)
3547

3648
else
@@ -77,26 +89,21 @@ ifeq ($(USE_TBB), Windows)
7789
# rtools: turn on hacks to compensate for make and shell differences rtools<=>MinGW
7890
# compiler: overwrite default (which is cl = MS compiler)
7991
MAKE_ARGS += rtools=true compiler=gcc
80-
ifeq ($(WIN), 64)
81-
MAKE_ARGS += arch=intel64 runtime=mingw
82-
ARCH_DIR=x64/
83-
else
84-
MAKE_ARGS += arch=ia32 runtime=mingw
85-
ARCH_DIR=i386/
92+
# TBB configure will detect mingw runtime with unknown arch on WINARM64_CLANG but not an
93+
# issue as we are using compiler built-ins instead of arch-specific code
94+
ifneq ($(WINARM64_CLANG), true)
95+
ifeq ($(WIN), 64)
96+
MAKE_ARGS += arch=intel64 runtime=mingw
97+
ARCH_DIR=x64/
98+
else
99+
MAKE_ARGS += arch=ia32 runtime=mingw
100+
ARCH_DIR=i386/
101+
endif
86102
endif
87103

88104
# Linker needs access to the tbb dll; otherwise you get errors such as:
89105
# "undefined reference to `tbb::task_scheduler_init::terminate()'"
90106
PKG_LIBS += -Ltbb/build/lib_release -ltbb -ltbbmalloc
91-
92-
# override for aarch64 (experimental) to use Rtools TBB
93-
ARCH=$(shell "${R_HOME}/bin/R" --vanilla -s -e 'cat(R.version$$arch)')
94-
ifeq "$(ARCH)" "aarch64"
95-
TBB_LIB = ${R_TOOLS_SOFT}
96-
TBB_INC = ${R_TOOLS_SOFT}
97-
PKG_LIBS = -ltbb12 -ltbbmalloc
98-
endif
99-
100107
endif
101108

102109
# write compiler if set

src/tbb/build/Makefile.tbb

+5-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ ifneq (,$(TBB.DEF))
9191
tbb.def: $(TBB.DEF) $(TBB.LST)
9292
$(CPLUS) $(PREPROC_ONLY) $< $(CPLUS_FLAGS) $(INCLUDES) > $@
9393

94-
LIB_LINK_FLAGS += $(EXPORT_KEY)tbb.def
94+
# LLVM on Windows doesn't need --version-script export
95+
# https://reviews.llvm.org/D63743
96+
ifeq (, $(WINARM64_CLANG))
97+
LIB_LINK_FLAGS += $(EXPORT_KEY)tbb.def
98+
endif
9599
$(TBB.DLL): tbb.def
96100
endif
97101

src/tbb/build/Makefile.tbbmalloc

+5-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ ifneq (,$(MALLOC.DEF))
7474
tbbmalloc.def: $(MALLOC.DEF)
7575
$(CPLUS) $(PREPROC_ONLY) $< $(M_CPLUS_FLAGS) $(WARNING_SUPPRESS) $(INCLUDES) > $@
7676

77-
MALLOC_LINK_FLAGS += $(EXPORT_KEY)tbbmalloc.def
77+
# LLVM on Windows doesn't need --version-script export
78+
# https://reviews.llvm.org/D63743
79+
ifeq (, $(WINARM64_CLANG))
80+
MALLOC_LINK_FLAGS += $(EXPORT_KEY)tbbmalloc.def
81+
endif
7882
$(MALLOC.DLL): tbbmalloc.def
7983
endif
8084

src/tbb/src/tbbmalloc/TypeDefinitions.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
# define __ARCH_ipf 1
2626
# elif defined(_M_IX86)||defined(__i386__) // the latter for MinGW support
2727
# define __ARCH_x86_32 1
28-
# elif defined(_M_ARM)
28+
# elif defined(_M_ARM) || defined(__aarch64__)
2929
# define __ARCH_other 1
3030
# else
3131
# error Unknown processor architecture for Windows

0 commit comments

Comments
 (0)