xref: /freebsd-src/contrib/llvm-project/clang/lib/Driver/ToolChains/SPIRV.cpp (revision 04eeddc0aa8e0a417a16eaf9d7d095207f4a8623)
1349cc55cSDimitry Andric //===--- SPIRV.cpp - SPIR-V Tool Implementations ----------------*- C++ -*-===//
2349cc55cSDimitry Andric //
3349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6349cc55cSDimitry Andric //
7349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
8349cc55cSDimitry Andric #include "SPIRV.h"
9349cc55cSDimitry Andric #include "CommonArgs.h"
10349cc55cSDimitry Andric #include "clang/Driver/Compilation.h"
11349cc55cSDimitry Andric #include "clang/Driver/Driver.h"
12349cc55cSDimitry Andric #include "clang/Driver/InputInfo.h"
13349cc55cSDimitry Andric #include "clang/Driver/Options.h"
14349cc55cSDimitry Andric 
15349cc55cSDimitry Andric using namespace clang::driver;
160eae32dcSDimitry Andric using namespace clang::driver::toolchains;
17349cc55cSDimitry Andric using namespace clang::driver::tools;
18349cc55cSDimitry Andric using namespace llvm::opt;
19349cc55cSDimitry Andric 
20349cc55cSDimitry Andric void SPIRV::constructTranslateCommand(Compilation &C, const Tool &T,
21349cc55cSDimitry Andric                                       const JobAction &JA,
22349cc55cSDimitry Andric                                       const InputInfo &Output,
23349cc55cSDimitry Andric                                       const InputInfo &Input,
24349cc55cSDimitry Andric                                       const llvm::opt::ArgStringList &Args) {
25349cc55cSDimitry Andric   llvm::opt::ArgStringList CmdArgs(Args);
26349cc55cSDimitry Andric   CmdArgs.push_back(Input.getFilename());
27349cc55cSDimitry Andric 
28349cc55cSDimitry Andric   if (Input.getType() == types::TY_PP_Asm)
29349cc55cSDimitry Andric     CmdArgs.push_back("-to-binary");
30349cc55cSDimitry Andric   if (Output.getType() == types::TY_PP_Asm)
310eae32dcSDimitry Andric     CmdArgs.push_back("--spirv-tools-dis");
32349cc55cSDimitry Andric 
33349cc55cSDimitry Andric   CmdArgs.append({"-o", Output.getFilename()});
34349cc55cSDimitry Andric 
35349cc55cSDimitry Andric   const char *Exec =
36349cc55cSDimitry Andric       C.getArgs().MakeArgString(T.getToolChain().GetProgramPath("llvm-spirv"));
37349cc55cSDimitry Andric   C.addCommand(std::make_unique<Command>(JA, T, ResponseFileSupport::None(),
38349cc55cSDimitry Andric                                          Exec, CmdArgs, Input, Output));
39349cc55cSDimitry Andric }
40349cc55cSDimitry Andric 
41349cc55cSDimitry Andric void SPIRV::Translator::ConstructJob(Compilation &C, const JobAction &JA,
42349cc55cSDimitry Andric                                      const InputInfo &Output,
43349cc55cSDimitry Andric                                      const InputInfoList &Inputs,
44349cc55cSDimitry Andric                                      const ArgList &Args,
45349cc55cSDimitry Andric                                      const char *LinkingOutput) const {
46349cc55cSDimitry Andric   claimNoWarnArgs(Args);
47349cc55cSDimitry Andric   if (Inputs.size() != 1)
48349cc55cSDimitry Andric     llvm_unreachable("Invalid number of input files.");
49349cc55cSDimitry Andric   constructTranslateCommand(C, *this, JA, Output, Inputs[0], {});
50349cc55cSDimitry Andric }
510eae32dcSDimitry Andric 
520eae32dcSDimitry Andric clang::driver::Tool *SPIRVToolChain::getTranslator() const {
530eae32dcSDimitry Andric   if (!Translator)
540eae32dcSDimitry Andric     Translator = std::make_unique<SPIRV::Translator>(*this);
550eae32dcSDimitry Andric   return Translator.get();
560eae32dcSDimitry Andric }
570eae32dcSDimitry Andric 
580eae32dcSDimitry Andric clang::driver::Tool *SPIRVToolChain::SelectTool(const JobAction &JA) const {
590eae32dcSDimitry Andric   Action::ActionClass AC = JA.getKind();
600eae32dcSDimitry Andric   return SPIRVToolChain::getTool(AC);
610eae32dcSDimitry Andric }
620eae32dcSDimitry Andric 
630eae32dcSDimitry Andric clang::driver::Tool *SPIRVToolChain::getTool(Action::ActionClass AC) const {
640eae32dcSDimitry Andric   switch (AC) {
650eae32dcSDimitry Andric   default:
660eae32dcSDimitry Andric     break;
670eae32dcSDimitry Andric   case Action::BackendJobClass:
680eae32dcSDimitry Andric   case Action::AssembleJobClass:
690eae32dcSDimitry Andric     return SPIRVToolChain::getTranslator();
700eae32dcSDimitry Andric   }
710eae32dcSDimitry Andric   return ToolChain::getTool(AC);
720eae32dcSDimitry Andric }
73*04eeddc0SDimitry Andric clang::driver::Tool *SPIRVToolChain::buildLinker() const {
74*04eeddc0SDimitry Andric   return new tools::SPIRV::Linker(*this);
75*04eeddc0SDimitry Andric }
76*04eeddc0SDimitry Andric 
77*04eeddc0SDimitry Andric void SPIRV::Linker::ConstructJob(Compilation &C, const JobAction &JA,
78*04eeddc0SDimitry Andric                                  const InputInfo &Output,
79*04eeddc0SDimitry Andric                                  const InputInfoList &Inputs,
80*04eeddc0SDimitry Andric                                  const ArgList &Args,
81*04eeddc0SDimitry Andric                                  const char *LinkingOutput) const {
82*04eeddc0SDimitry Andric   const ToolChain &ToolChain = getToolChain();
83*04eeddc0SDimitry Andric   std::string Linker = ToolChain.GetProgramPath(getShortName());
84*04eeddc0SDimitry Andric   ArgStringList CmdArgs;
85*04eeddc0SDimitry Andric   AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
86*04eeddc0SDimitry Andric 
87*04eeddc0SDimitry Andric   CmdArgs.push_back("-o");
88*04eeddc0SDimitry Andric   CmdArgs.push_back(Output.getFilename());
89*04eeddc0SDimitry Andric 
90*04eeddc0SDimitry Andric   C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
91*04eeddc0SDimitry Andric                                          Args.MakeArgString(Linker), CmdArgs,
92*04eeddc0SDimitry Andric                                          Inputs, Output));
93*04eeddc0SDimitry Andric }
94