xref: /freebsd-src/contrib/llvm-project/clang/lib/Driver/ToolChains/SPIRV.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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"
10*0fca6ea1SDimitry Andric #include "clang/Basic/Version.h"
11349cc55cSDimitry Andric #include "clang/Driver/Compilation.h"
12349cc55cSDimitry Andric #include "clang/Driver/Driver.h"
13349cc55cSDimitry Andric #include "clang/Driver/InputInfo.h"
14349cc55cSDimitry Andric #include "clang/Driver/Options.h"
15349cc55cSDimitry Andric 
16349cc55cSDimitry Andric using namespace clang::driver;
170eae32dcSDimitry Andric using namespace clang::driver::toolchains;
18349cc55cSDimitry Andric using namespace clang::driver::tools;
19349cc55cSDimitry Andric using namespace llvm::opt;
20349cc55cSDimitry Andric 
21349cc55cSDimitry Andric void SPIRV::constructTranslateCommand(Compilation &C, const Tool &T,
22349cc55cSDimitry Andric                                       const JobAction &JA,
23349cc55cSDimitry Andric                                       const InputInfo &Output,
24349cc55cSDimitry Andric                                       const InputInfo &Input,
25349cc55cSDimitry Andric                                       const llvm::opt::ArgStringList &Args) {
26349cc55cSDimitry Andric   llvm::opt::ArgStringList CmdArgs(Args);
27349cc55cSDimitry Andric   CmdArgs.push_back(Input.getFilename());
28349cc55cSDimitry Andric 
29349cc55cSDimitry Andric   if (Input.getType() == types::TY_PP_Asm)
30349cc55cSDimitry Andric     CmdArgs.push_back("-to-binary");
31349cc55cSDimitry Andric   if (Output.getType() == types::TY_PP_Asm)
320eae32dcSDimitry Andric     CmdArgs.push_back("--spirv-tools-dis");
33349cc55cSDimitry Andric 
34349cc55cSDimitry Andric   CmdArgs.append({"-o", Output.getFilename()});
35349cc55cSDimitry Andric 
36*0fca6ea1SDimitry Andric   // Try to find "llvm-spirv-<LLVM_VERSION_MAJOR>". Otherwise, fall back to
37*0fca6ea1SDimitry Andric   // plain "llvm-spirv".
38*0fca6ea1SDimitry Andric   using namespace std::string_literals;
39*0fca6ea1SDimitry Andric   auto VersionedTool = "llvm-spirv-"s + std::to_string(LLVM_VERSION_MAJOR);
40*0fca6ea1SDimitry Andric   std::string ExeCand = T.getToolChain().GetProgramPath(VersionedTool.c_str());
41*0fca6ea1SDimitry Andric   if (!llvm::sys::fs::can_execute(ExeCand))
42*0fca6ea1SDimitry Andric     ExeCand = T.getToolChain().GetProgramPath("llvm-spirv");
43*0fca6ea1SDimitry Andric 
44*0fca6ea1SDimitry Andric   const char *Exec = C.getArgs().MakeArgString(ExeCand);
45349cc55cSDimitry Andric   C.addCommand(std::make_unique<Command>(JA, T, ResponseFileSupport::None(),
46349cc55cSDimitry Andric                                          Exec, CmdArgs, Input, Output));
47349cc55cSDimitry Andric }
48349cc55cSDimitry Andric 
49349cc55cSDimitry Andric void SPIRV::Translator::ConstructJob(Compilation &C, const JobAction &JA,
50349cc55cSDimitry Andric                                      const InputInfo &Output,
51349cc55cSDimitry Andric                                      const InputInfoList &Inputs,
52349cc55cSDimitry Andric                                      const ArgList &Args,
53349cc55cSDimitry Andric                                      const char *LinkingOutput) const {
54349cc55cSDimitry Andric   claimNoWarnArgs(Args);
55349cc55cSDimitry Andric   if (Inputs.size() != 1)
56349cc55cSDimitry Andric     llvm_unreachable("Invalid number of input files.");
57349cc55cSDimitry Andric   constructTranslateCommand(C, *this, JA, Output, Inputs[0], {});
58349cc55cSDimitry Andric }
590eae32dcSDimitry Andric 
600eae32dcSDimitry Andric clang::driver::Tool *SPIRVToolChain::getTranslator() const {
610eae32dcSDimitry Andric   if (!Translator)
620eae32dcSDimitry Andric     Translator = std::make_unique<SPIRV::Translator>(*this);
630eae32dcSDimitry Andric   return Translator.get();
640eae32dcSDimitry Andric }
650eae32dcSDimitry Andric 
660eae32dcSDimitry Andric clang::driver::Tool *SPIRVToolChain::SelectTool(const JobAction &JA) const {
670eae32dcSDimitry Andric   Action::ActionClass AC = JA.getKind();
680eae32dcSDimitry Andric   return SPIRVToolChain::getTool(AC);
690eae32dcSDimitry Andric }
700eae32dcSDimitry Andric 
710eae32dcSDimitry Andric clang::driver::Tool *SPIRVToolChain::getTool(Action::ActionClass AC) const {
720eae32dcSDimitry Andric   switch (AC) {
730eae32dcSDimitry Andric   default:
740eae32dcSDimitry Andric     break;
750eae32dcSDimitry Andric   case Action::BackendJobClass:
760eae32dcSDimitry Andric   case Action::AssembleJobClass:
770eae32dcSDimitry Andric     return SPIRVToolChain::getTranslator();
780eae32dcSDimitry Andric   }
790eae32dcSDimitry Andric   return ToolChain::getTool(AC);
800eae32dcSDimitry Andric }
8104eeddc0SDimitry Andric clang::driver::Tool *SPIRVToolChain::buildLinker() const {
8204eeddc0SDimitry Andric   return new tools::SPIRV::Linker(*this);
8304eeddc0SDimitry Andric }
8404eeddc0SDimitry Andric 
8504eeddc0SDimitry Andric void SPIRV::Linker::ConstructJob(Compilation &C, const JobAction &JA,
8604eeddc0SDimitry Andric                                  const InputInfo &Output,
8704eeddc0SDimitry Andric                                  const InputInfoList &Inputs,
8804eeddc0SDimitry Andric                                  const ArgList &Args,
8904eeddc0SDimitry Andric                                  const char *LinkingOutput) const {
9004eeddc0SDimitry Andric   const ToolChain &ToolChain = getToolChain();
9104eeddc0SDimitry Andric   std::string Linker = ToolChain.GetProgramPath(getShortName());
9204eeddc0SDimitry Andric   ArgStringList CmdArgs;
9304eeddc0SDimitry Andric   AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
9404eeddc0SDimitry Andric 
9504eeddc0SDimitry Andric   CmdArgs.push_back("-o");
9604eeddc0SDimitry Andric   CmdArgs.push_back(Output.getFilename());
9704eeddc0SDimitry Andric 
9804eeddc0SDimitry Andric   C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
9904eeddc0SDimitry Andric                                          Args.MakeArgString(Linker), CmdArgs,
10004eeddc0SDimitry Andric                                          Inputs, Output));
10104eeddc0SDimitry Andric }
102