1"""This file contains BUILD extensions for building llvm_openmp. 2TODO(Intel-tf): Delete this and reuse a similar function in third_party/llvm 3after the TF 2.4 branch cut has passed. 4""" 5 6load( 7 "//tensorflow/core/platform:rules_cc.bzl", 8 "cc_binary", 9) 10 11WINDOWS_MSVC_LLVM_OPENMP_LIBPATH = "bazel-out/x64_windows-opt/bin/external/llvm_openmp/libiomp5md.dll.if.lib" 12WINDOWS_MSVC_LLVM_OPENMP_LINKOPTS = "/NODEFAULTLIB:libomp /DEFAULTLIB:" + WINDOWS_MSVC_LLVM_OPENMP_LIBPATH 13 14def windows_llvm_openmp_linkopts(): 15 return WINDOWS_MSVC_LLVM_OPENMP_LINKOPTS 16 17def dict_add(*dictionaries): 18 """Returns a new `dict` that has all the entries of the given dictionaries. 19 20 If the same key is present in more than one of the input dictionaries, the 21 last of them in the argument list overrides any earlier ones. 22 23 Args: 24 *dictionaries: Zero or more dictionaries to be added. 25 26 Returns: 27 A new `dict` that has all the entries of the given dictionaries. 28 """ 29 result = {} 30 for d in dictionaries: 31 result.update(d) 32 return result 33 34def select_os_specific(L, M, W): 35 return select({ 36 "@org_tensorflow//tensorflow:linux_x86_64": L, 37 "@org_tensorflow//tensorflow:macos": M, 38 "@org_tensorflow//tensorflow:windows": W, 39 "//conditions:default": L, 40 }) 41 42def select_os_specific_2(LM, W): 43 return select_os_specific(L = LM, M = LM, W = W) 44 45def libname_os_specific(): 46 return "" + select_os_specific(L = "libiomp5.so", M = "libiomp5.dylib", W = "libiomp5md.dll") 47 48# TODO(Intel-tf) Replace the following calls to cc_binary with cc_library. 49# cc_library should be used for files that are not independently executed. Using 50# cc_library results in linking errors. For e.g on Linux, the build fails 51# with the following error message. 52# ERROR: //tensorflow/BUILD:689:1: Linking of rule '//tensorflow:libtensorflow_framework.so.2.4.0' failed (Exit 1) 53# /usr/bin/ld.gold: error: symbol GOMP_parallel_loop_nonmonotonic_guided has undefined version VERSION 54# /usr/bin/ld.gold: error: symbol GOMP_parallel_start has undefined version GOMP_1.0 55# /usr/bin/ld.gold: error: symbol GOMP_cancellation_point has undefined version GOMP_4.0 56# /usr/bin/ld.gold: error: symbol omp_set_num_threads has undefined version OMP_1.0 57# ...... 58# ...... 59 60# MacOS build has not been tested, however since the MacOS build of openmp 61# uses the same configuration as Linux, the following should work. 62def libiomp5_cc_binary(name, cppsources, srcdeps, common_includes): 63 cc_binary( 64 name = name, 65 srcs = cppsources + srcdeps + 66 select_os_specific_2( 67 LM = [ 68 #linux & macos specific files 69 "runtime/src/z_Linux_util.cpp", 70 "runtime/src/kmp_gsupport.cpp", 71 "runtime/src/z_Linux_asm.S", 72 ], 73 W = [ 74 #window specific files 75 "runtime/src/z_Windows_NT_util.cpp", 76 "runtime/src/z_Windows_NT-586_util.cpp", 77 ":openmp_asm", 78 ], 79 ), 80 defines = select_os_specific_2( 81 LM = ["omp_EXPORTS", "_GNU_SOURCE", "_REENTRANT"], 82 W = ["omp_EXPORTS", "_M_AMD64", "OMPT_SUPPORT=0", "_WINDOWS", "_WINNT", "_USRDLL"], 83 ), 84 includes = common_includes, 85 linkopts = select_os_specific_2( 86 LM = ["-lpthread -ldl -Wl,--version-script=$(location :ldscript)"], 87 W = ["/MACHINE:X64"], 88 ), 89 linkshared = True, 90 additional_linker_inputs = select_os_specific_2( 91 LM = [":ldscript"], 92 W = [":generate_def"], 93 ), 94 win_def_file = ":generate_def", # This will be ignored for non Windows builds 95 visibility = ["//visibility:public"], 96 ) 97