AMDGPU.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //===--- AMDGPU.h - AMDGPU ToolChain 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. #ifndef LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_AMDGPU_H
  9. #define LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_AMDGPU_H
  10. #include "Gnu.h"
  11. #include "ROCm.h"
  12. #include "clang/Basic/TargetID.h"
  13. #include "clang/Driver/Options.h"
  14. #include "clang/Driver/Tool.h"
  15. #include "clang/Driver/ToolChain.h"
  16. #include "llvm/ADT/SmallString.h"
  17. #include "llvm/Support/TargetParser.h"
  18. #include <map>
  19. namespace clang {
  20. namespace driver {
  21. namespace tools {
  22. namespace amdgpu {
  23. class LLVM_LIBRARY_VISIBILITY Linker : public Tool {
  24. public:
  25. Linker(const ToolChain &TC) : Tool("amdgpu::Linker", "ld.lld", TC) {}
  26. bool isLinkJob() const override { return true; }
  27. bool hasIntegratedCPP() const override { return false; }
  28. void ConstructJob(Compilation &C, const JobAction &JA,
  29. const InputInfo &Output, const InputInfoList &Inputs,
  30. const llvm::opt::ArgList &TCArgs,
  31. const char *LinkingOutput) const override;
  32. };
  33. void getAMDGPUTargetFeatures(const Driver &D, const llvm::Triple &Triple,
  34. const llvm::opt::ArgList &Args,
  35. std::vector<StringRef> &Features);
  36. } // end namespace amdgpu
  37. } // end namespace tools
  38. namespace toolchains {
  39. class LLVM_LIBRARY_VISIBILITY AMDGPUToolChain : public Generic_ELF {
  40. protected:
  41. const std::map<options::ID, const StringRef> OptionsDefault;
  42. Tool *buildLinker() const override;
  43. StringRef getOptionDefault(options::ID OptID) const {
  44. auto opt = OptionsDefault.find(OptID);
  45. assert(opt != OptionsDefault.end() && "No Default for Option");
  46. return opt->second;
  47. }
  48. public:
  49. AMDGPUToolChain(const Driver &D, const llvm::Triple &Triple,
  50. const llvm::opt::ArgList &Args);
  51. unsigned GetDefaultDwarfVersion() const override { return 5; }
  52. bool IsIntegratedAssemblerDefault() const override { return true; }
  53. bool IsMathErrnoDefault() const override { return false; }
  54. bool useIntegratedAs() const override { return true; }
  55. bool isCrossCompiling() const override { return true; }
  56. bool isPICDefault() const override { return false; }
  57. bool isPIEDefault(const llvm::opt::ArgList &Args) const override {
  58. return false;
  59. }
  60. bool isPICDefaultForced() const override { return false; }
  61. bool SupportsProfiling() const override { return false; }
  62. llvm::opt::DerivedArgList *
  63. TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef BoundArch,
  64. Action::OffloadKind DeviceOffloadKind) const override;
  65. void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
  66. llvm::opt::ArgStringList &CC1Args,
  67. Action::OffloadKind DeviceOffloadKind) const override;
  68. /// Return whether denormals should be flushed, and treated as 0 by default
  69. /// for the subtarget.
  70. static bool getDefaultDenormsAreZeroForTarget(llvm::AMDGPU::GPUKind GPUKind);
  71. llvm::DenormalMode getDefaultDenormalModeForType(
  72. const llvm::opt::ArgList &DriverArgs, const JobAction &JA,
  73. const llvm::fltSemantics *FPType = nullptr) const override;
  74. static bool isWave64(const llvm::opt::ArgList &DriverArgs,
  75. llvm::AMDGPU::GPUKind Kind);
  76. /// Needed for using lto.
  77. bool HasNativeLLVMSupport() const override {
  78. return true;
  79. }
  80. /// Needed for translating LTO options.
  81. const char *getDefaultLinker() const override { return "ld.lld"; }
  82. /// Should skip argument.
  83. bool shouldSkipArgument(const llvm::opt::Arg *Arg) const;
  84. /// Uses amdgpu_arch tool to get arch of the system GPU. Will return error
  85. /// if unable to find one.
  86. llvm::Error getSystemGPUArch(const llvm::opt::ArgList &Args,
  87. std::string &GPUArch) const;
  88. protected:
  89. /// Check and diagnose invalid target ID specified by -mcpu.
  90. virtual void checkTargetID(const llvm::opt::ArgList &DriverArgs) const;
  91. /// The struct type returned by getParsedTargetID.
  92. struct ParsedTargetIDType {
  93. Optional<std::string> OptionalTargetID;
  94. Optional<std::string> OptionalGPUArch;
  95. Optional<llvm::StringMap<bool>> OptionalFeatures;
  96. };
  97. /// Get target ID, GPU arch, and target ID features if the target ID is
  98. /// specified and valid.
  99. ParsedTargetIDType
  100. getParsedTargetID(const llvm::opt::ArgList &DriverArgs) const;
  101. /// Get GPU arch from -mcpu without checking.
  102. StringRef getGPUArch(const llvm::opt::ArgList &DriverArgs) const;
  103. llvm::Error detectSystemGPUs(const llvm::opt::ArgList &Args,
  104. SmallVector<std::string, 1> &GPUArchs) const;
  105. };
  106. class LLVM_LIBRARY_VISIBILITY ROCMToolChain : public AMDGPUToolChain {
  107. public:
  108. ROCMToolChain(const Driver &D, const llvm::Triple &Triple,
  109. const llvm::opt::ArgList &Args);
  110. void
  111. addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
  112. llvm::opt::ArgStringList &CC1Args,
  113. Action::OffloadKind DeviceOffloadKind) const override;
  114. // Returns a list of device library names shared by different languages
  115. llvm::SmallVector<std::string, 12>
  116. getCommonDeviceLibNames(const llvm::opt::ArgList &DriverArgs,
  117. const std::string &GPUArch) const;
  118. };
  119. } // end namespace toolchains
  120. } // end namespace driver
  121. } // end namespace clang
  122. #endif // LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_AMDGPU_H