Skip to content

[SPIR-V] Prefer llvm-spirv-<LLVM_VERSION_MAJOR> tool #77897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions clang/docs/UsersManual.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions clang/lib/Driver/ToolChains/SPIRV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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));
}
Expand Down
13 changes: 13 additions & 0 deletions clang/test/Driver/hipspv-toolchain.hip
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
10 changes: 10 additions & 0 deletions clang/test/Driver/spirv-toolchain.cl
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
1 change: 1 addition & 0 deletions clang/test/lit.site.cfg.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading