Author: Henry Linjamäki Date: 2024-05-31T12:21:21+02:00 New Revision: a65771fce4a2f25f16d4b3918ad6a11370637f7b
URL: https://github.com/llvm/llvm-project/commit/a65771fce4a2f25f16d4b3918ad6a11370637f7b DIFF: https://github.com/llvm/llvm-project/commit/a65771fce4a2f25f16d4b3918ad6a11370637f7b.diff LOG: [SPIR-V] Prefer llvm-spirv-<LLVM_VERSION_MAJOR> tool (#77897) Prefer using `llvm-spirv-<LLVM_VERSION_MAJOR>` tool (i.e. `llvm-spirv-18`) over plain `llvm-spirv`. If the versioned tool is not found in PATH, fall back to use the plain `llvm-spirv`. An issue with the using `llvm-spirv` is that the one found in PATH might be compiled against older LLVM version which could lead to crashes or obscure bugs. For example, `llvm-spirv` distributed by Ubuntu links against different LLVM version depending on the Ubuntu release (LLVM-10 in 20.04LTS, LLVM-13 in 22.04LTS). Added: Modified: clang/docs/UsersManual.rst clang/lib/Driver/ToolChains/SPIRV.cpp clang/test/Driver/hipspv-toolchain.hip clang/test/Driver/spirv-toolchain.cl clang/test/lit.site.cfg.py.in Removed: ################################################################################ diff --git a/clang/docs/UsersManual.rst b/clang/docs/UsersManual.rst index 80ba70f67126f..f954857b0235a 100644 --- a/clang/docs/UsersManual.rst +++ b/clang/docs/UsersManual.rst @@ -4430,9 +4430,9 @@ To generate SPIR-V binaries, Clang uses the external ``llvm-spirv`` tool from th Prior to the generation of SPIR-V binary with Clang, ``llvm-spirv`` should be built or installed. Please refer to `the following instructions <https://github.com/KhronosGroup/SPIRV-LLVM-Translator#build-instructions>`_ -for more details. Clang will expect the ``llvm-spirv`` executable to -be present in the ``PATH`` environment variable. Clang uses ``llvm-spirv`` -with `the widely adopted assembly syntax package +for more details. Clang will look for ``llvm-spirv-<LLVM-major-version>`` and +``llvm-spirv`` executables, in this order, in the ``PATH`` environment variable. +Clang uses ``llvm-spirv`` with `the widely adopted assembly syntax package <https://github.com/KhronosGroup/SPIRV-LLVM-Translator/#build-with-spirv-tools>`_. `The versioning diff --git a/clang/lib/Driver/ToolChains/SPIRV.cpp b/clang/lib/Driver/ToolChains/SPIRV.cpp index 27de69550853c..ce900600cbee5 100644 --- a/clang/lib/Driver/ToolChains/SPIRV.cpp +++ b/clang/lib/Driver/ToolChains/SPIRV.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "SPIRV.h" #include "CommonArgs.h" +#include "clang/Basic/Version.h" #include "clang/Driver/Compilation.h" #include "clang/Driver/Driver.h" #include "clang/Driver/InputInfo.h" @@ -32,8 +33,15 @@ void SPIRV::constructTranslateCommand(Compilation &C, const Tool &T, CmdArgs.append({"-o", Output.getFilename()}); - const char *Exec = - C.getArgs().MakeArgString(T.getToolChain().GetProgramPath("llvm-spirv")); + // Try to find "llvm-spirv-<LLVM_VERSION_MAJOR>". Otherwise, fall back to + // plain "llvm-spirv". + using namespace std::string_literals; + auto VersionedTool = "llvm-spirv-"s + std::to_string(LLVM_VERSION_MAJOR); + std::string ExeCand = T.getToolChain().GetProgramPath(VersionedTool.c_str()); + if (!llvm::sys::fs::can_execute(ExeCand)) + ExeCand = T.getToolChain().GetProgramPath("llvm-spirv"); + + const char *Exec = C.getArgs().MakeArgString(ExeCand); C.addCommand(std::make_unique<Command>(JA, T, ResponseFileSupport::None(), Exec, CmdArgs, Input, Output)); } diff --git a/clang/test/Driver/hipspv-toolchain.hip b/clang/test/Driver/hipspv-toolchain.hip index 4602cd3fc8d68..4005d9889051f 100644 --- a/clang/test/Driver/hipspv-toolchain.hip +++ b/clang/test/Driver/hipspv-toolchain.hip @@ -34,3 +34,16 @@ // CHECK-SAME: "-o" [[OBJ_HOST:".*o"]] "-x" "hip" // CHECK: {{".*ld.*"}} {{.*}}[[OBJ_HOST]] + +//----------------------------------------------------------------------------- +// Check llvm-spirv-<LLVM_VERSION_MAJOR> is used if it is found in PATH. +// RUN: mkdir -p %t/versioned +// RUN: touch %t/versioned/llvm-spirv-%llvm-version-major \ +// RUN: && chmod +x %t/versioned/llvm-spirv-%llvm-version-major +// RUN: env "PATH=%t/versioned" %clang -### -target x86_64-linux-gnu \ +// RUN: --offload=spirv64 --hip-path=%S/Inputs/hipspv -nohipwrapperinc \ +// RUN: %s 2>&1 \ +// RUN: | FileCheck -DVERSION=%llvm-version-major \ +// RUN: --check-prefix=VERSIONED %s + +// VERSIONED: {{.*}}llvm-spirv-[[VERSION]] diff --git a/clang/test/Driver/spirv-toolchain.cl b/clang/test/Driver/spirv-toolchain.cl index db3ee4d3fe02f..de818177cb19f 100644 --- a/clang/test/Driver/spirv-toolchain.cl +++ b/clang/test/Driver/spirv-toolchain.cl @@ -77,3 +77,13 @@ // XTOR: {{llvm-spirv.*"}} // BACKEND-NOT: {{llvm-spirv.*"}} + +//----------------------------------------------------------------------------- +// Check llvm-spirv-<LLVM_VERSION_MAJOR> is used if it is found in PATH. +// RUN: mkdir -p %t/versioned +// RUN: touch %t/versioned/llvm-spirv-%llvm-version-major \ +// RUN: && chmod +x %t/versioned/llvm-spirv-%llvm-version-major +// RUN: env "PATH=%t/versioned" %clang -### --target=spirv64 -x cl -c %s 2>&1 \ +// RUN: | FileCheck -DVERSION=%llvm-version-major --check-prefix=VERSIONED %s + +// VERSIONED: {{.*}}llvm-spirv-[[VERSION]] diff --git a/clang/test/lit.site.cfg.py.in b/clang/test/lit.site.cfg.py.in index ec6d30e6c2203..1cbd876ac5bb9 100644 --- a/clang/test/lit.site.cfg.py.in +++ b/clang/test/lit.site.cfg.py.in @@ -43,6 +43,7 @@ config.llvm_external_lit = path(r"@LLVM_EXTERNAL_LIT@") config.standalone_build = @CLANG_BUILT_STANDALONE@ config.ppc_linux_default_ieeelongdouble = @PPC_LINUX_DEFAULT_IEEELONGDOUBLE@ config.have_llvm_driver = @LLVM_TOOL_LLVM_DRIVER_BUILD@ +config.substitutions.append(("%llvm-version-major", "@LLVM_VERSION_MAJOR@")) import lit.llvm lit.llvm.initialize(lit_config, config) _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits