github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/third_party/sycl/crosstool/computecpp.tpl (about)

     1  #!/usr/bin/env python
     2  
     3  import os
     4  import sys
     5  import tempfile
     6  from subprocess import call, Popen, PIPE
     7  
     8  CPU_CXX_COMPILER = ('%{host_cxx_compiler}')
     9  CPU_C_COMPILER = ('%{host_c_compiler}')
    10  
    11  CURRENT_DIR = os.path.dirname(sys.argv[0])
    12  COMPUTECPP_ROOT = CURRENT_DIR + '/../sycl/'
    13  COMPUTECPP_DRIVER= COMPUTECPP_ROOT + 'bin/compute++'
    14  COMPUTECPP_INCLUDE = COMPUTECPP_ROOT + 'include'
    15  
    16  def main():
    17    remove_flags = ('-Wl,--no-undefined', '-Wno-unused-but-set-variable', '-Wignored-attributes')
    18    # remove -fsanitize-coverage from string with g++
    19    if 'g++' in CPU_CXX_COMPILER:
    20      remove_flags += ('-fsanitize-coverage',)
    21    compiler_flags = [flag for flag in sys.argv[1:] if not flag.startswith(remove_flags)]
    22  
    23    output_file_index = compiler_flags.index('-o') + 1
    24    output_file_name = compiler_flags[output_file_index]
    25  
    26    if output_file_index == 1:
    27      # we are linking
    28      return call([CPU_CXX_COMPILER] + compiler_flags + ['-Wl,--no-undefined'])
    29  
    30    # find what we compile
    31    compiling_cpp = False
    32    if '-c' in compiler_flags:
    33      compiled_file_index = compiler_flags.index('-c') + 1
    34      compiled_file_name = compiler_flags[compiled_file_index]
    35      compiling_cpp = compiled_file_name.endswith(('.cc', '.c++', '.cpp', '.CPP', '.C', '.cxx'))
    36  
    37    # add -D_GLIBCXX_USE_CXX11_ABI=0 to the command line if you have custom installation of GCC/Clang
    38    compiler_flags = compiler_flags + ['-DEIGEN_USE_SYCL=1', '-DTENSORFLOW_USE_SYCL', '-DEIGEN_HAS_C99_MATH']
    39  
    40    if not compiling_cpp:
    41      # compile for C
    42      return call([CPU_C_COMPILER] + compiler_flags)
    43  
    44    # create a blacklist of folders that will be skipped when compiling with ComputeCpp
    45    skip_extensions = [".cu.cc"]
    46    skip_folders = ["tensorflow/compiler", "tensorflow/docs_src", "third_party", "external", "hexagon"]
    47    skip_folders = [(folder + '/') for folder in skip_folders]
    48    # if compiling external project skip computecpp
    49    if any(compiled_file_name.endswith(_ext) for _ext in skip_extensions) or any(_folder in output_file_name for _folder in skip_folders):
    50      return call([CPU_CXX_COMPILER] + compiler_flags)
    51  
    52    # this is an optimisation that will check if compiled file has to be compiled with ComputeCpp
    53    flags_without_output = list(compiler_flags)
    54    del flags_without_output[output_file_index]   # remove output_file_name
    55    del flags_without_output[output_file_index - 1] # remove '-o'
    56    # create preprocessed of the file and store it for later use
    57    pipe = Popen([CPU_CXX_COMPILER] + flags_without_output + ["-E"], stdout=PIPE)
    58    preprocessed_file_str = pipe.communicate()[0]
    59    if pipe.returncode != 0:
    60      return pipe.returncode
    61  
    62    # check if it has parallel_for in it
    63    if not '.parallel_for' in preprocessed_file_str:
    64      # call CXX compiler like usual
    65      with tempfile.NamedTemporaryFile(suffix=".ii") as preprocessed_file: # Force '.ii' extension so that g++ does not preprocess the file again
    66        preprocessed_file.write(preprocessed_file_str)
    67        preprocessed_file.flush()
    68        compiler_flags[compiled_file_index] = preprocessed_file.name
    69        return call([CPU_CXX_COMPILER] + compiler_flags)
    70    del preprocessed_file_str   # save some memory as this string can be quite big
    71  
    72    filename, file_extension = os.path.splitext(output_file_name)
    73    bc_out = filename + '.sycl'
    74  
    75    # strip asan for the device
    76    computecpp_device_compiler_flags = ['-sycl-compress-name', '-Wno-unused-variable', '-Wno-c++11-narrowing',
    77                                        '-I', COMPUTECPP_INCLUDE, '-isystem', COMPUTECPP_INCLUDE,
    78                                        '-std=c++11', '-sycl', '-emit-llvm', '-no-serial-memop',
    79                                        '-Xclang', '-cl-denorms-are-zero', '-Xclang', '-cl-fp32-correctly-rounded-divide-sqrt']
    80    # disable flags enabling SIMD instructions
    81    computecpp_device_compiler_flags += [flag for flag in compiler_flags if \
    82      not any(x in flag.lower() for x in ('-fsanitize', '-fno-canonical-system-headers', '=native', '=core2', 'msse', 'vectorize', 'mavx', 'mmmx', 'm3dnow', 'fma'))]
    83  
    84    x = call([COMPUTECPP_DRIVER] + computecpp_device_compiler_flags)
    85    if x == 0:
    86      # dont want that in case of compiling with computecpp first
    87      host_compiler_flags = [flag for flag in compiler_flags if (not flag.startswith(('-MF', '-MD',)) and not '.d' in flag)]
    88      host_compiler_flags[host_compiler_flags.index('-c')] = "--include"
    89      host_compiler_flags = ['-xc++', '-Wno-unused-variable', '-I', COMPUTECPP_INCLUDE, '-c', bc_out] + host_compiler_flags
    90      x = call([CPU_CXX_COMPILER] + host_compiler_flags)
    91    return x
    92  
    93  if __name__ == '__main__':
    94    sys.exit(main())