SPIRV.cpp 3.5 KB

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