clang 20.0.0git
SPIRV.cpp
Go to the documentation of this file.
1//===--- SPIRV.cpp - SPIR-V Tool Implementations ----------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8#include "SPIRV.h"
9#include "CommonArgs.h"
10#include "clang/Basic/Version.h"
12#include "clang/Driver/Driver.h"
15
16using namespace clang::driver;
17using namespace clang::driver::toolchains;
18using namespace clang::driver::tools;
19using namespace llvm::opt;
20
22 const JobAction &JA,
23 const InputInfo &Output,
24 const InputInfo &Input,
25 const llvm::opt::ArgStringList &Args) {
26 llvm::opt::ArgStringList CmdArgs(Args);
27 CmdArgs.push_back(Input.getFilename());
28
29 if (Input.getType() == types::TY_PP_Asm)
30 CmdArgs.push_back("-to-binary");
31 if (Output.getType() == types::TY_PP_Asm)
32 CmdArgs.push_back("--spirv-tools-dis");
33
34 CmdArgs.append({"-o", Output.getFilename()});
35
36 // Try to find "llvm-spirv-<LLVM_VERSION_MAJOR>". Otherwise, fall back to
37 // plain "llvm-spirv".
38 using namespace std::string_literals;
39 auto VersionedTool = "llvm-spirv-"s + std::to_string(LLVM_VERSION_MAJOR);
40 std::string ExeCand = T.getToolChain().GetProgramPath(VersionedTool.c_str());
41 if (!llvm::sys::fs::can_execute(ExeCand))
42 ExeCand = T.getToolChain().GetProgramPath("llvm-spirv");
43
44 const char *Exec = C.getArgs().MakeArgString(ExeCand);
45 C.addCommand(std::make_unique<Command>(JA, T, ResponseFileSupport::None(),
46 Exec, CmdArgs, Input, Output));
47}
48
50 const InputInfo &Output,
51 const InputInfoList &Inputs,
52 const ArgList &Args,
53 const char *LinkingOutput) const {
54 claimNoWarnArgs(Args);
55 if (Inputs.size() != 1)
56 llvm_unreachable("Invalid number of input files.");
57 constructTranslateCommand(C, *this, JA, Output, Inputs[0], {});
58}
59
60clang::driver::Tool *SPIRVToolChain::getTranslator() const {
61 if (!Translator)
62 Translator = std::make_unique<SPIRV::Translator>(*this);
63 return Translator.get();
64}
65
68 return SPIRVToolChain::getTool(AC);
69}
70
72 switch (AC) {
73 default:
74 break;
77 return SPIRVToolChain::getTranslator();
78 }
79 return ToolChain::getTool(AC);
80}
82 return new tools::SPIRV::Linker(*this);
83}
84
86 const InputInfo &Output,
87 const InputInfoList &Inputs,
88 const ArgList &Args,
89 const char *LinkingOutput) const {
90 const ToolChain &ToolChain = getToolChain();
91 std::string Linker = ToolChain.GetProgramPath(getShortName());
92 ArgStringList CmdArgs;
93 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
94
95 CmdArgs.push_back("-o");
96 CmdArgs.push_back(Output.getFilename());
97
98 // Use of --sycl-link will call the clang-sycl-linker instead of
99 // the default linker (spirv-link).
100 if (Args.hasArg(options::OPT_sycl_link))
101 Linker = ToolChain.GetProgramPath("clang-sycl-linker");
102 C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
103 Args.MakeArgString(Linker), CmdArgs,
104 Inputs, Output));
105}
106
107SPIRVToolChain::SPIRVToolChain(const Driver &D, const llvm::Triple &Triple,
108 const ArgList &Args)
109 : ToolChain(D, Triple, Args) {
110 // TODO: Revisit need/use of --sycl-link option once SYCL toolchain is
111 // available and SYCL linking support is moved there.
112 NativeLLVMSupport = Args.hasArg(options::OPT_sycl_link);
113}
114
115bool SPIRVToolChain::HasNativeLLVMSupport() const { return NativeLLVMSupport; }
const Decl * D
Defines version macros and version-related utility functions for Clang.
__device__ __2f16 float __ockl_bool s
ActionClass getKind() const
Definition: Action.h:147
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:45
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:77
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:22
const char * getFilename() const
Definition: InputInfo.h:83
types::ID getType() const
Definition: InputInfo.h:77
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:92
std::string GetProgramPath(const char *Name) const
Definition: ToolChain.cpp:953
virtual Tool * getTool(Action::ActionClass AC) const
Definition: ToolChain.cpp:609
Tool - Information on a specific compilation tool.
Definition: Tool.h:32
Tool * buildLinker() const override
Definition: SPIRV.cpp:81
clang::driver::Tool * getTool(Action::ActionClass AC) const override
Definition: SPIRV.cpp:71
clang::driver::Tool * SelectTool(const JobAction &JA) const override
Choose a tool to use to handle the action JA.
Definition: SPIRV.cpp:66
bool HasNativeLLVMSupport() const override
HasNativeLTOLinker - Check whether the linker and related tools have native LLVM support.
Definition: SPIRV.cpp:115
SPIRVToolChain(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
Definition: SPIRV.cpp:107
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs,...
Definition: SPIRV.cpp:85
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs,...
Definition: SPIRV.cpp:49
void constructTranslateCommand(Compilation &C, const Tool &T, const JobAction &JA, const InputInfo &Output, const InputInfo &Input, const llvm::opt::ArgStringList &Args)
Definition: SPIRV.cpp:21
void claimNoWarnArgs(const llvm::opt::ArgList &Args)
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
const FunctionProtoType * T
static constexpr ResponseFileSupport None()
Returns a ResponseFileSupport indicating that response files are not supported.
Definition: Job.h:78