1# Build file for OpenMP library that is part of llvm 2 3load( 4 "@org_tensorflow//third_party/llvm:llvm.bzl", 5 "cmake_var_string", 6 "expand_cmake_vars", 7) 8load( 9 "@org_tensorflow//third_party/llvm_openmp:openmp.bzl", 10 "dict_add", 11) 12 13exports_files(["LICENSE.txt"]) 14 15genrule( 16 name = "kmp_i18n_id", 17 srcs = [ 18 "runtime/tools/message-converter.pl", 19 "runtime/src/i18n/en_US.txt", 20 ], 21 outs = ["include/kmp_i18n_id.inc"], 22 cmd = "perl $(location runtime/tools/message-converter.pl) --os=lin --prefix=kmp_i18n --enum=$@ $(location runtime/src/i18n/en_US.txt)", 23) 24 25genrule( 26 name = "kmp_i18n_default", 27 srcs = [ 28 "runtime/tools/message-converter.pl", 29 "runtime/src/i18n/en_US.txt", 30 ], 31 outs = ["include/kmp_i18n_default.inc"], 32 cmd = "perl $(location runtime/tools/message-converter.pl) --os=lin --prefix=kmp_i18n --default=$@ $(location runtime/src/i18n/en_US.txt)", 33) 34 35# Bazel doesn't accept .txt as an input, rename the ldscript to .inc to workaround. 36genrule( 37 name = "ldscript", 38 srcs = ["runtime/src/exports_so.txt"], 39 outs = ["exports_so.inc"], 40 cmd = "cp $(location runtime/src/exports_so.txt) $@", 41) 42 43genrule( 44 name = "openmp_asm", 45 srcs = [ 46 "runtime/src/z_Windows_NT-586_asm.asm", 47 ], 48 outs = [ 49 "z_Windows_NT-586_asm.S", 50 ], 51 cmd = "cp $(location runtime/src/z_Windows_NT-586_asm.asm) $@", 52 visibility = ["//visibility:public"], 53) 54 55# Common Cmake vars to expand. 56omp_vars = { 57 "LIBOMP_ENABLE_SHARED": 1, 58 "LIBOMP_LEGAL_ARCH": "Intel(R) 64", 59 "LIBOMP_LIB_FILE": "libiomp5", 60 "LIBOMP_VERSION_MAJOR": 5, 61 "LIBOMP_VERSION_MINOR": 0, 62} 63 64# Linux Cmake vars to expand. 65omp_vars_linux = { 66 "LIBOMP_USE_VERSION_SYMBOLS": 1, 67 "LIBOMP_HAVE_WEAK_ATTRIBUTE": 1, 68 "LIBOMP_USE_ADAPTIVE_LOCKS": 1, 69 "LIBOMP_ENABLE_ASSERTIONS": 1, 70} 71 72# Windows Cmake vars to expand. 73omp_vars_win = { 74 "MSVC": 1, 75} 76 77omp_all_cmake_vars = select({ 78 "@org_tensorflow//tensorflow:windows": cmake_var_string( 79 dict_add( 80 omp_vars, 81 omp_vars_win, 82 ), 83 ), 84 "//conditions:default": cmake_var_string( 85 dict_add( 86 omp_vars, 87 omp_vars_linux, 88 ), 89 ), 90}) 91 92expand_cmake_vars( 93 name = "config_kmp", 94 src = "runtime/src/kmp_config.h.cmake", 95 cmake_vars = omp_all_cmake_vars, 96 dst = "include/kmp_config.h", 97) 98 99expand_cmake_vars( 100 name = "config_omp", 101 src = "runtime/src/include/omp.h.var", 102 cmake_vars = omp_all_cmake_vars, 103 dst = "include/omp.h", 104) 105 106cppsources = [ 107 "runtime/src/kmp_alloc.cpp", 108 "runtime/src/kmp_atomic.cpp", 109 "runtime/src/kmp_csupport.cpp", 110 "runtime/src/kmp_debug.cpp", 111 "runtime/src/kmp_itt.cpp", 112 "runtime/src/kmp_environment.cpp", 113 "runtime/src/kmp_error.cpp", 114 "runtime/src/kmp_global.cpp", 115 "runtime/src/kmp_i18n.cpp", 116 "runtime/src/kmp_io.cpp", 117 "runtime/src/kmp_runtime.cpp", 118 "runtime/src/kmp_settings.cpp", 119 "runtime/src/kmp_str.cpp", 120 "runtime/src/kmp_tasking.cpp", 121 "runtime/src/kmp_threadprivate.cpp", 122 "runtime/src/kmp_utility.cpp", 123 "runtime/src/kmp_barrier.cpp", 124 "runtime/src/kmp_wait_release.cpp", 125 "runtime/src/kmp_affinity.cpp", 126 "runtime/src/kmp_dispatch.cpp", 127 "runtime/src/kmp_lock.cpp", 128 "runtime/src/kmp_sched.cpp", 129 "runtime/src/kmp_taskdeps.cpp", 130 "runtime/src/kmp_cancel.cpp", 131 "runtime/src/kmp_ftn_cdecl.cpp", 132 "runtime/src/kmp_ftn_extra.cpp", 133 "runtime/src/kmp_version.cpp", 134] 135 136srcdeps = [ 137 ":config_kmp", 138 ":config_omp", 139 ":kmp_i18n_id", 140 ":kmp_i18n_default", 141 ":ldscript", 142] 143 144common_includes = [ 145 "runtime/src/", 146 "include/", 147] 148 149# TODO(Intel-tf) Replace the following 3 calls to cc_binary with cc_library. 150# cc_library should be used for files that are not independently executed. Using 151# cc_library results in linking errors. For e.g on Linux, the build fails 152# with the following error message. 153# ERROR: //tensorflow/BUILD:689:1: Linking of rule '//tensorflow:libtensorflow_framework.so.2.4.0' failed (Exit 1) 154# /usr/bin/ld.gold: error: symbol GOMP_parallel_loop_nonmonotonic_guided has undefined version VERSION 155# /usr/bin/ld.gold: error: symbol GOMP_parallel_start has undefined version GOMP_1.0 156# /usr/bin/ld.gold: error: symbol GOMP_cancellation_point has undefined version GOMP_4.0 157# /usr/bin/ld.gold: error: symbol omp_set_num_threads has undefined version OMP_1.0 158# ...... 159# ...... 160 161cc_binary( 162 name = "libiomp5.so", 163 srcs = cppsources + [ 164 #linux specific files 165 "runtime/src/z_Linux_util.cpp", 166 "runtime/src/kmp_gsupport.cpp", 167 "runtime/src/z_Linux_asm.S", 168 ] + srcdeps, 169 copts = ["-Domp_EXPORTS -D_GNU_SOURCE -D_REENTRANT"], 170 includes = common_includes, 171 linkopts = ["-lpthread -ldl -Wl,--version-script=$(location :ldscript)"], 172 linkshared = True, 173 visibility = ["//visibility:public"], 174) 175 176cc_binary( 177 name = "libiomp5md.dll", 178 srcs = cppsources + [ 179 #window specific files 180 "runtime/src/z_Windows_NT_util.cpp", 181 "runtime/src/z_Windows_NT-586_util.cpp", 182 ] + srcdeps + [":openmp_asm"], 183 copts = ["/Domp_EXPORTS /D_M_AMD64 /DOMPT_SUPPORT=0 /D_WINDOWS /D_WINNT /D_USRDLL"], 184 includes = common_includes, 185 linkopts = ["/MACHINE:X64"], 186 linkshared = True, 187 visibility = ["//visibility:public"], 188) 189 190# MacOS build has not been tested, however since the MacOS build of openmp 191# uses the same configuration as Linux, the following should work. 192cc_binary( 193 name = "libiomp5.dylib", 194 srcs = cppsources + [ 195 #linux/MacOS specific files 196 "runtime/src/z_Linux_util.cpp", 197 "runtime/src/kmp_gsupport.cpp", 198 "runtime/src/z_Linux_asm.S", 199 ] + srcdeps, 200 copts = ["-Domp_EXPORTS -D_GNU_SOURCE -D_REENTRANT"], 201 includes = common_includes, 202 linkopts = ["-lpthread -ldl -Wl,--version-script=$(location :ldscript)"], 203 linkshared = True, 204 visibility = ["//visibility:public"], 205) 206