CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
217 lines
9.0 KiB
C++
217 lines
9.0 KiB
C++
//===--- SPIRV.cpp - SPIR-V Tool Implementations ----------------*- C++ -*-===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
#include "SPIRV.h"
|
|
#include "clang/Driver/CommonArgs.h"
|
|
#include "clang/Driver/Compilation.h"
|
|
#include "clang/Driver/Driver.h"
|
|
#include "clang/Driver/InputInfo.h"
|
|
#include "clang/Options/Options.h"
|
|
|
|
using namespace clang::driver;
|
|
using namespace clang::driver::toolchains;
|
|
using namespace clang::driver::tools;
|
|
using namespace llvm::opt;
|
|
|
|
void SPIRV::constructTranslateCommand(Compilation &C, const Tool &T,
|
|
const JobAction &JA,
|
|
const InputInfo &Output,
|
|
const InputInfo &Input,
|
|
const llvm::opt::ArgStringList &Args) {
|
|
llvm::opt::ArgStringList CmdArgs(Args);
|
|
CmdArgs.push_back(Input.getFilename());
|
|
|
|
assert(Input.getType() != types::TY_PP_Asm && "Unexpected input type");
|
|
|
|
if (Output.getType() == types::TY_PP_Asm)
|
|
CmdArgs.push_back("--spirv-tools-dis");
|
|
|
|
CmdArgs.append({"-o", Output.getFilename()});
|
|
|
|
// Try to find "llvm-spirv-<LLVM_VERSION_MAJOR>". Otherwise, fall back to
|
|
// plain "llvm-spirv".
|
|
// AMD FORK ONLY: instead of llvm-spirv we look for the amd-llvm-spirv, which
|
|
// is our ephemeral, temporary build of the translator that nests changes that
|
|
// are not in upstream. This will be removed in the future.
|
|
using namespace std::string_literals;
|
|
auto VersionedTool = "llvm-spirv-"s + std::to_string(LLVM_VERSION_MAJOR);
|
|
if (T.getToolChain().getTriple().getVendor() == llvm::Triple::VendorType::AMD)
|
|
VersionedTool.insert(0, "amd-");
|
|
std::string ExeCand = T.getToolChain().GetProgramPath(VersionedTool.c_str());
|
|
if (!llvm::sys::fs::can_execute(ExeCand))
|
|
ExeCand = T.getToolChain().GetProgramPath(
|
|
VersionedTool.substr(0, VersionedTool.find_last_of('-')).c_str());
|
|
|
|
const char *Exec = C.getArgs().MakeArgString(ExeCand);
|
|
C.addCommand(std::make_unique<Command>(JA, T, ResponseFileSupport::None(),
|
|
Exec, CmdArgs, Input, Output));
|
|
}
|
|
|
|
void SPIRV::constructAssembleCommand(Compilation &C, const Tool &T,
|
|
const JobAction &JA,
|
|
const InputInfo &Output,
|
|
const InputInfo &Input,
|
|
const llvm::opt::ArgStringList &Args) {
|
|
llvm::opt::ArgStringList CmdArgs(Args);
|
|
CmdArgs.push_back(Input.getFilename());
|
|
|
|
assert(Input.getType() == types::TY_PP_Asm && "Unexpected input type");
|
|
|
|
CmdArgs.append({"-o", Output.getFilename()});
|
|
|
|
// Try to find "spirv-as-<LLVM_VERSION_MAJOR>". Otherwise, fall back to
|
|
// plain "spirv-as".
|
|
using namespace std::string_literals;
|
|
auto VersionedTool = "spirv-as-"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("spirv-as");
|
|
|
|
if (!llvm::sys::fs::can_execute(ExeCand) &&
|
|
!C.getArgs().hasArg(clang::options::OPT__HASH_HASH_HASH)) {
|
|
C.getDriver().Diag(clang::diag::err_drv_no_spv_tools) << "spirv-as";
|
|
return;
|
|
}
|
|
const char *Exec = C.getArgs().MakeArgString(ExeCand);
|
|
C.addCommand(std::make_unique<Command>(JA, T, ResponseFileSupport::None(),
|
|
Exec, CmdArgs, Input, Output));
|
|
}
|
|
|
|
void SPIRV::constructLLVMLinkCommand(Compilation &C, const Tool &T,
|
|
const JobAction &JA,
|
|
const InputInfo &Output,
|
|
const InputInfoList &Inputs,
|
|
const llvm::opt::ArgList &Args) {
|
|
|
|
ArgStringList LlvmLinkArgs;
|
|
|
|
for (auto Input : Inputs) {
|
|
if (Input.isFilename()) {
|
|
LlvmLinkArgs.push_back(Input.getFilename());
|
|
} else {
|
|
// Warn that any linker arguments will be dropped.
|
|
assert(Input.isInputArg() && "Unexpected linker input");
|
|
const llvm::opt::Arg &LinkerOpt = Input.getInputArg();
|
|
std::string LinkerOptStr = LinkerOpt.getAsString(Args);
|
|
const llvm::opt::Arg *EmitLLVM = Args.getLastArg(options::OPT_emit_llvm);
|
|
assert(EmitLLVM && "Unexpected linker input");
|
|
std::string EmitLLVMStr = EmitLLVM ? EmitLLVM->getAsString(Args) : "";
|
|
llvm::Triple Triple(T.getToolChain().getTriple());
|
|
C.getDriver().Diag(clang::diag::warn_drv_input_file_unused)
|
|
<< Triple.getTriple() << LinkerOptStr << false << EmitLLVMStr;
|
|
}
|
|
}
|
|
|
|
tools::constructLLVMLinkCommand(C, T, JA, Inputs, LlvmLinkArgs, Output, Args);
|
|
}
|
|
|
|
void SPIRV::Translator::ConstructJob(Compilation &C, const JobAction &JA,
|
|
const InputInfo &Output,
|
|
const InputInfoList &Inputs,
|
|
const ArgList &Args,
|
|
const char *LinkingOutput) const {
|
|
claimNoWarnArgs(Args);
|
|
if (Inputs.size() != 1)
|
|
llvm_unreachable("Invalid number of input files.");
|
|
constructTranslateCommand(C, *this, JA, Output, Inputs[0], {});
|
|
}
|
|
|
|
void SPIRV::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
|
|
const InputInfo &Output,
|
|
const InputInfoList &Inputs,
|
|
const ArgList &Args,
|
|
const char *AssembleOutput) const {
|
|
claimNoWarnArgs(Args);
|
|
if (Inputs.size() != 1)
|
|
llvm_unreachable("Invalid number of input files.");
|
|
constructAssembleCommand(C, *this, JA, Output, Inputs[0], {});
|
|
}
|
|
|
|
clang::driver::Tool *SPIRVToolChain::getAssembler() const {
|
|
if (!Assembler)
|
|
Assembler = std::make_unique<SPIRV::Assembler>(*this);
|
|
return Assembler.get();
|
|
}
|
|
|
|
clang::driver::Tool *SPIRVToolChain::SelectTool(const JobAction &JA) const {
|
|
Action::ActionClass AC = JA.getKind();
|
|
return SPIRVToolChain::getTool(AC);
|
|
}
|
|
|
|
clang::driver::Tool *SPIRVToolChain::getTool(Action::ActionClass AC) const {
|
|
switch (AC) {
|
|
default:
|
|
break;
|
|
case Action::AssembleJobClass:
|
|
return SPIRVToolChain::getAssembler();
|
|
}
|
|
return ToolChain::getTool(AC);
|
|
}
|
|
clang::driver::Tool *SPIRVToolChain::buildLinker() const {
|
|
return new tools::SPIRV::Linker(*this);
|
|
}
|
|
|
|
void SPIRV::Linker::ConstructJob(Compilation &C, const JobAction &JA,
|
|
const InputInfo &Output,
|
|
const InputInfoList &Inputs,
|
|
const ArgList &Args,
|
|
const char *LinkingOutput) const {
|
|
if (JA.getType() == types::TY_LLVM_BC) {
|
|
constructLLVMLinkCommand(C, *this, JA, Output, Inputs, Args);
|
|
return;
|
|
}
|
|
const ToolChain &ToolChain = getToolChain();
|
|
std::string Linker = ToolChain.GetProgramPath(getShortName());
|
|
ArgStringList CmdArgs;
|
|
AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
|
|
|
|
CmdArgs.push_back("-o");
|
|
CmdArgs.push_back(Output.getFilename());
|
|
|
|
// TODO: Consider moving SPIR-V linking to a separate tool.
|
|
if (ToolChain.isUsingLTO(Args)) {
|
|
// Implement limited LTO support through llvm-lto.
|
|
if (Args.hasArg(options::OPT_sycl_link)) {
|
|
// For unsupported cases, throw the same error as when LTO isn't supported
|
|
// at all.
|
|
C.getDriver().Diag(clang::diag::err_drv_no_linker_llvm_support)
|
|
<< ToolChain.getTriple().getTriple();
|
|
return;
|
|
}
|
|
Linker = ToolChain.GetProgramPath("llvm-lto");
|
|
// Disable internalization, otherwise GlobalDCE will optimize everything
|
|
// out.
|
|
CmdArgs.push_back("-enable-lto-internalization=false");
|
|
} else if (Args.hasArg(options::OPT_sycl_link)) {
|
|
// Use of --sycl-link will call the clang-sycl-linker instead of
|
|
// the default linker (spirv-link).
|
|
Linker = ToolChain.GetProgramPath("clang-sycl-linker");
|
|
if (Args.hasArg(options::OPT_v))
|
|
CmdArgs.push_back("-v");
|
|
} else if (!llvm::sys::fs::can_execute(Linker) &&
|
|
!C.getArgs().hasArg(clang::options::OPT__HASH_HASH_HASH)) {
|
|
C.getDriver().Diag(clang::diag::err_drv_no_spv_tools) << getShortName();
|
|
return;
|
|
}
|
|
C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
|
|
Args.MakeArgString(Linker), CmdArgs,
|
|
Inputs, Output));
|
|
}
|
|
|
|
SPIRVToolChain::SPIRVToolChain(const Driver &D, const llvm::Triple &Triple,
|
|
const ArgList &Args)
|
|
: ToolChain(D, Triple, Args) {
|
|
// TODO: Revisit need/use of --sycl-link option once SYCL toolchain is
|
|
// available and SYCL linking support is moved there.
|
|
NativeLLVMSupport = Args.hasArg(options::OPT_sycl_link) || isUsingLTO(Args);
|
|
|
|
// Lookup binaries into the driver directory.
|
|
getProgramPaths().push_back(getDriver().Dir);
|
|
}
|
|
|
|
bool SPIRVToolChain::HasNativeLLVMSupport() const { return NativeLLVMSupport; }
|