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/Driver/Compilation.h" |
11 | #include "clang/Driver/Driver.h" |
12 | #include "clang/Driver/InputInfo.h" |
13 | #include "clang/Driver/Options.h" |
14 | |
15 | using namespace clang::driver; |
16 | using namespace clang::driver::toolchains; |
17 | using namespace clang::driver::tools; |
18 | using namespace llvm::opt; |
19 | |
20 | void SPIRV::constructTranslateCommand(Compilation &C, const Tool &T, |
21 | const JobAction &JA, |
22 | const InputInfo &Output, |
23 | const InputInfo &Input, |
24 | const llvm::opt::ArgStringList &Args) { |
25 | llvm::opt::ArgStringList CmdArgs(Args); |
26 | CmdArgs.push_back(Elt: Input.getFilename()); |
27 | |
28 | if (Input.getType() == types::TY_PP_Asm) |
29 | CmdArgs.push_back(Elt: "-to-binary" ); |
30 | if (Output.getType() == types::TY_PP_Asm) |
31 | CmdArgs.push_back(Elt: "--spirv-tools-dis" ); |
32 | |
33 | CmdArgs.append(IL: {"-o" , Output.getFilename()}); |
34 | |
35 | const char *Exec = |
36 | C.getArgs().MakeArgString(Str: T.getToolChain().GetProgramPath(Name: "llvm-spirv" )); |
37 | C.addCommand(C: std::make_unique<Command>(args: JA, args: T, args: ResponseFileSupport::None(), |
38 | args&: Exec, args&: CmdArgs, args: Input, args: Output)); |
39 | } |
40 | |
41 | void SPIRV::Translator::ConstructJob(Compilation &C, const JobAction &JA, |
42 | const InputInfo &Output, |
43 | const InputInfoList &Inputs, |
44 | const ArgList &Args, |
45 | const char *LinkingOutput) const { |
46 | claimNoWarnArgs(Args); |
47 | if (Inputs.size() != 1) |
48 | llvm_unreachable("Invalid number of input files." ); |
49 | constructTranslateCommand(C, T: *this, JA, Output, Input: Inputs[0], Args: {}); |
50 | } |
51 | |
52 | clang::driver::Tool *SPIRVToolChain::getTranslator() const { |
53 | if (!Translator) |
54 | Translator = std::make_unique<SPIRV::Translator>(args: *this); |
55 | return Translator.get(); |
56 | } |
57 | |
58 | clang::driver::Tool *SPIRVToolChain::SelectTool(const JobAction &JA) const { |
59 | Action::ActionClass AC = JA.getKind(); |
60 | return SPIRVToolChain::getTool(AC); |
61 | } |
62 | |
63 | clang::driver::Tool *SPIRVToolChain::getTool(Action::ActionClass AC) const { |
64 | switch (AC) { |
65 | default: |
66 | break; |
67 | case Action::BackendJobClass: |
68 | case Action::AssembleJobClass: |
69 | return SPIRVToolChain::getTranslator(); |
70 | } |
71 | return ToolChain::getTool(AC); |
72 | } |
73 | clang::driver::Tool *SPIRVToolChain::buildLinker() const { |
74 | return new tools::SPIRV::Linker(*this); |
75 | } |
76 | |
77 | void SPIRV::Linker::ConstructJob(Compilation &C, const JobAction &JA, |
78 | const InputInfo &Output, |
79 | const InputInfoList &Inputs, |
80 | const ArgList &Args, |
81 | const char *LinkingOutput) const { |
82 | const ToolChain &ToolChain = getToolChain(); |
83 | std::string Linker = ToolChain.GetProgramPath(Name: getShortName()); |
84 | ArgStringList CmdArgs; |
85 | AddLinkerInputs(TC: getToolChain(), Inputs, Args, CmdArgs, JA); |
86 | |
87 | CmdArgs.push_back(Elt: "-o" ); |
88 | CmdArgs.push_back(Elt: Output.getFilename()); |
89 | |
90 | C.addCommand(C: std::make_unique<Command>(args: JA, args: *this, args: ResponseFileSupport::None(), |
91 | args: Args.MakeArgString(Str: Linker), args&: CmdArgs, |
92 | args: Inputs, args: Output)); |
93 | } |
94 | |