mesonbuild / meson

The Meson Build System
http://mesonbuild.com
Apache License 2.0
5.66k stars 1.65k forks source link

Can't build sycl project with intel's icx on windows. #12470

Open borg323 opened 1 year ago

borg323 commented 1 year ago

Describe the bug Meson forces the linker to xilink.exe which doesn't do the required offload code generation. Using icx -fsycl to link works fine.

To Reproduce I used the vector-add example from https://github.com/oneapi-src/oneAPI-samples/tree/master/DirectProgramming/C%2B%2BSYCL/DenseLinearAlgebra/vector-add with the following minimal meson.build and setting CXX=icx:

project('vector-add', 'cpp')

add_project_arguments('-fsycl', language : 'cpp')

executable('vector-add-buffers', 'src/vector-add-buffers.cpp')

Expected behavior Here is a full transcript of the issue and the expected correct behavior when using icx to link:

C:\various\oneapi\vector-add>set CXX=icx

C:\various\oneapi\vector-add>meson setup build --buildtype=release
The Meson build system
Version: 1.2.0
Source dir: C:\various\oneapi\vector-add
Build dir: C:\various\oneapi\vector-add\build
Build type: native build
Project name: vector-add
Project version: undefined
C++ compiler for the host machine: icx (intel-llvm-cl 2023.2.0)
C++ linker for the host machine: xilink.exe xilink 2023.2.0
Host machine cpu family: x86_64
Host machine cpu: x86_64
Build targets in project: 1

vector-add undefined

  User defined options
    buildtype: release

Found ninja-1.10.2 at "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja\ninja.EXE"

C:\various\oneapi\vector-add>cd build

C:\various\oneapi\vector-add\build>ninja -v
[1/2] "icx" "-Ivector-add-buffers.exe.p" "-I." "-I.." "/MD" "/nologo" "/showIncludes" "/utf-8" "/W2" "/EHsc" "/O3" "-fsycl" "/Fdvector-add-buffers.exe.p\src_vector-add-buffers.cpp.pdb" /Fovector-add-buffers.exe.p/src_vector-add-buffers.cpp.obj "/c" ../src/vector-add-buffers.cpp
icx: command line warning #10430: Unsupported command line options encountered
These options as listed are not supported.
For more information, use '/Qnextgen-diag'.
option list:
        /utf-8

[2/2] "xilink.exe"  /MACHINE:x64 /OUT:vector-add-buffers.exe vector-add-buffers.exe.p/src_vector-add-buffers.cpp.obj "/nologo" "/OPT:REF" "/SUBSYSTEM:CONSOLE" "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "comdlg32.lib" "advapi32.lib"

C:\various\oneapi\vector-add\build>vector-add-buffers.exe
Running on device: Intel(R) Iris(R) Xe Graphics
Vector size: 10000
An exception is caught for vector add: No kernel named _ZTSZZ9VectorAddRN4sycl3_V15queueERKSt6vectorIiSaIiEES7_RS5_ENKUlRNS0_7handlerEE_clESA_EUlT_E_ was found -46 (PI_ERROR_INVALID_KERNEL_NAME)

C:\various\oneapi\vector-add\build>icx -fsycl -o vector-add-buffers.exe vector-add-buffers.exe.p/src_vector-add-buffers.cpp.obj "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "comdlg32.lib" "advapi32.lib"
Intel(R) oneAPI DPC++/C++ Compiler for applications running on Intel(R) 64, Version 2023.2.0 Build 20230627
Copyright (C) 1985-2023 Intel Corporation. All rights reserved.

C:\various\oneapi\vector-add\build>vector-add-buffers.exe
Running on device: Intel(R) Iris(R) Xe Graphics
Vector size: 10000
[0]: 0 + 0 = 0
[1]: 1 + 1 = 2
[2]: 2 + 2 = 4
...
[9999]: 9999 + 9999 = 19998
Vector add successfully completed on device.

system parameters

liam-clink commented 1 month ago

I am also having this problem, and am surprised to see it remains... Why is xilink.exe being detected as the linker when icx is the selected compiler?

liam-clink commented 1 month ago

I found XILINK is the Intel "pre-linker"

borg323 commented 1 month ago

I managed to get it to work with meson.add_postconf_script('sycl_build_hack.py') and the following sycl_build_hack.py:

#!/usr/bin/env python3

import os

dir = os.getenv('MESON_BUILD_ROOT')

with open(dir + '/build.ninja', 'r') as file:
  lines = file.readlines()

updated = []
dep_flag = False
link_flag = False

for line in lines:
  # Replace xilink with icx -fsycl as the linker.
  if not link_flag:
    link_flag = 'xilink.exe' in line
  if link_flag:
    line = line.replace('xilink.exe', 'icx')
    line = line.replace('/MACHINE:x64', '-fsycl')
    line = line.replace('/OUT:', '-o ')
    line = line.replace('/SUBSYSTEM:CONSOLE', '')
    line = line.replace('/OPT:REF', '')
  # Replace msvc compatible dependencies with gcc ones as icx output with /showincludes includes
  # temporary header files causing full project rebuilds.
  if line.startswith('rule') or line.startswith('build'):
    dep_flag = 'cpp_COMPILER' in line
  if dep_flag:
    line = line.replace('deps = msvc', 'deps = gcc\n depfile = $out.d')
    line = line.replace('/showIncludes', '/QMD')
    if 'icx' in line:
      line = line.replace('/Fo$out', '/Fo$out /QMF$out.d')
  updated.append(line)

with open(dir + '/build.ninja', 'w') as file:
  file.writelines(updated)
liam-clink commented 1 month ago

It seems based on other issues I've seen that lots of others are able to compile with oneAPI just fine without doing weird crap like this, but I don't know what their meson files look like

borg323 commented 1 month ago

I would be very interested in a cleaner solution, so if you have any pointer do let me know. Note that you only need this if you want to offload to the GPU and build under Windows, for Linux it works with no hacks.

liam-clink commented 4 weeks ago

Ah, my bet is that the development focus is higher on Linux, and most people that do HPC do on Linux, which is what oneAPI is usually used for. Seems to me like the cleaner solution is going to need to be a pull request. I'll look into doing that when I can

liam-clink commented 4 weeks ago

I do still have a couple warnings generated even when I use that script:

[1/2] Compiling C++ object vector_add.exe.p/src_main.cpp.obj
icx: warning: ignoring '-traceback' option as it is not currently supported for target 'spir64-unknown-unknown' [-Woption-ignored]
[2/2] Linking target vector_add.exe
icx: warning: unknown argument ignored in clang-cl: '-PDB:vector_add.pdb' [-Wunknown-argument]
borg323 commented 4 weeks ago

I was planning to revisit it this month, will post here if I make a new version. Does it otherwise work?