xref: /openbsd-src/gnu/llvm/clang/lib/Driver/ToolChains/AMDGPU.h (revision 12c855180aad702bbcca06e0398d774beeafb155)
1e5dd7070Spatrick //===--- AMDGPU.h - AMDGPU ToolChain Implementations ----------*- C++ -*-===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick 
9e5dd7070Spatrick #ifndef LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_AMDGPU_H
10e5dd7070Spatrick #define LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_AMDGPU_H
11e5dd7070Spatrick 
12e5dd7070Spatrick #include "Gnu.h"
13ec727ea7Spatrick #include "ROCm.h"
14a9ac8606Spatrick #include "clang/Basic/TargetID.h"
15e5dd7070Spatrick #include "clang/Driver/Options.h"
16e5dd7070Spatrick #include "clang/Driver/Tool.h"
17e5dd7070Spatrick #include "clang/Driver/ToolChain.h"
18ec727ea7Spatrick #include "llvm/ADT/SmallString.h"
19ec727ea7Spatrick #include "llvm/Support/TargetParser.h"
20ec727ea7Spatrick 
21e5dd7070Spatrick #include <map>
22e5dd7070Spatrick 
23e5dd7070Spatrick namespace clang {
24e5dd7070Spatrick namespace driver {
25ec727ea7Spatrick 
26e5dd7070Spatrick namespace tools {
27e5dd7070Spatrick namespace amdgpu {
28e5dd7070Spatrick 
29ec727ea7Spatrick class LLVM_LIBRARY_VISIBILITY Linker : public Tool {
30e5dd7070Spatrick public:
Linker(const ToolChain & TC)31ec727ea7Spatrick   Linker(const ToolChain &TC) : Tool("amdgpu::Linker", "ld.lld", TC) {}
isLinkJob()32e5dd7070Spatrick   bool isLinkJob() const override { return true; }
hasIntegratedCPP()33e5dd7070Spatrick   bool hasIntegratedCPP() const override { return false; }
34e5dd7070Spatrick   void ConstructJob(Compilation &C, const JobAction &JA,
35e5dd7070Spatrick                     const InputInfo &Output, const InputInfoList &Inputs,
36e5dd7070Spatrick                     const llvm::opt::ArgList &TCArgs,
37e5dd7070Spatrick                     const char *LinkingOutput) const override;
38e5dd7070Spatrick };
39e5dd7070Spatrick 
40a9ac8606Spatrick void getAMDGPUTargetFeatures(const Driver &D, const llvm::Triple &Triple,
41a9ac8606Spatrick                              const llvm::opt::ArgList &Args,
42e5dd7070Spatrick                              std::vector<StringRef> &Features);
43e5dd7070Spatrick 
44e5dd7070Spatrick } // end namespace amdgpu
45e5dd7070Spatrick } // end namespace tools
46e5dd7070Spatrick 
47e5dd7070Spatrick namespace toolchains {
48e5dd7070Spatrick 
49e5dd7070Spatrick class LLVM_LIBRARY_VISIBILITY AMDGPUToolChain : public Generic_ELF {
50ec727ea7Spatrick protected:
51e5dd7070Spatrick   const std::map<options::ID, const StringRef> OptionsDefault;
52e5dd7070Spatrick 
53e5dd7070Spatrick   Tool *buildLinker() const override;
getOptionDefault(options::ID OptID)54*12c85518Srobert   StringRef getOptionDefault(options::ID OptID) const {
55e5dd7070Spatrick     auto opt = OptionsDefault.find(OptID);
56e5dd7070Spatrick     assert(opt != OptionsDefault.end() && "No Default for Option");
57e5dd7070Spatrick     return opt->second;
58e5dd7070Spatrick   }
59e5dd7070Spatrick 
60e5dd7070Spatrick public:
61e5dd7070Spatrick   AMDGPUToolChain(const Driver &D, const llvm::Triple &Triple,
62e5dd7070Spatrick                   const llvm::opt::ArgList &Args);
GetDefaultDwarfVersion()63*12c85518Srobert   unsigned GetDefaultDwarfVersion() const override { return 5; }
64e5dd7070Spatrick 
IsMathErrnoDefault()65*12c85518Srobert   bool IsMathErrnoDefault() const override { return false; }
isCrossCompiling()66a9ac8606Spatrick   bool isCrossCompiling() const override { return true; }
isPICDefault()67a9ac8606Spatrick   bool isPICDefault() const override { return false; }
isPIEDefault(const llvm::opt::ArgList & Args)68*12c85518Srobert   bool isPIEDefault(const llvm::opt::ArgList &Args) const override {
69*12c85518Srobert     return false;
70*12c85518Srobert   }
isPICDefaultForced()71a9ac8606Spatrick   bool isPICDefaultForced() const override { return false; }
SupportsProfiling()72a9ac8606Spatrick   bool SupportsProfiling() const override { return false; }
73a9ac8606Spatrick 
74e5dd7070Spatrick   llvm::opt::DerivedArgList *
75e5dd7070Spatrick   TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef BoundArch,
76e5dd7070Spatrick                 Action::OffloadKind DeviceOffloadKind) const override;
77e5dd7070Spatrick 
78e5dd7070Spatrick   void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
79e5dd7070Spatrick                              llvm::opt::ArgStringList &CC1Args,
80e5dd7070Spatrick                              Action::OffloadKind DeviceOffloadKind) const override;
81ec727ea7Spatrick 
82ec727ea7Spatrick   /// Return whether denormals should be flushed, and treated as 0 by default
83ec727ea7Spatrick   /// for the subtarget.
84ec727ea7Spatrick   static bool getDefaultDenormsAreZeroForTarget(llvm::AMDGPU::GPUKind GPUKind);
85ec727ea7Spatrick 
86ec727ea7Spatrick   llvm::DenormalMode getDefaultDenormalModeForType(
87ec727ea7Spatrick       const llvm::opt::ArgList &DriverArgs, const JobAction &JA,
88ec727ea7Spatrick       const llvm::fltSemantics *FPType = nullptr) const override;
89ec727ea7Spatrick 
90ec727ea7Spatrick   static bool isWave64(const llvm::opt::ArgList &DriverArgs,
91ec727ea7Spatrick                        llvm::AMDGPU::GPUKind Kind);
92ec727ea7Spatrick   /// Needed for using lto.
HasNativeLLVMSupport()93ec727ea7Spatrick   bool HasNativeLLVMSupport() const override {
94ec727ea7Spatrick     return true;
95ec727ea7Spatrick   }
96ec727ea7Spatrick 
97ec727ea7Spatrick   /// Needed for translating LTO options.
getDefaultLinker()98ec727ea7Spatrick   const char *getDefaultLinker() const override { return "ld.lld"; }
99a9ac8606Spatrick 
100a9ac8606Spatrick   /// Should skip argument.
101a9ac8606Spatrick   bool shouldSkipArgument(const llvm::opt::Arg *Arg) const;
102a9ac8606Spatrick 
103*12c85518Srobert   /// Uses amdgpu-arch tool to get arch of the system GPU. Will return error
104a9ac8606Spatrick   /// if unable to find one.
105*12c85518Srobert   virtual Expected<SmallVector<std::string>>
106*12c85518Srobert   getSystemGPUArchs(const llvm::opt::ArgList &Args) const override;
107a9ac8606Spatrick 
108a9ac8606Spatrick protected:
109a9ac8606Spatrick   /// Check and diagnose invalid target ID specified by -mcpu.
110a9ac8606Spatrick   virtual void checkTargetID(const llvm::opt::ArgList &DriverArgs) const;
111a9ac8606Spatrick 
112a9ac8606Spatrick   /// The struct type returned by getParsedTargetID.
113a9ac8606Spatrick   struct ParsedTargetIDType {
114*12c85518Srobert     std::optional<std::string> OptionalTargetID;
115*12c85518Srobert     std::optional<std::string> OptionalGPUArch;
116*12c85518Srobert     std::optional<llvm::StringMap<bool>> OptionalFeatures;
117a9ac8606Spatrick   };
118a9ac8606Spatrick 
119a9ac8606Spatrick   /// Get target ID, GPU arch, and target ID features if the target ID is
120a9ac8606Spatrick   /// specified and valid.
121a9ac8606Spatrick   ParsedTargetIDType
122a9ac8606Spatrick   getParsedTargetID(const llvm::opt::ArgList &DriverArgs) const;
123a9ac8606Spatrick 
124a9ac8606Spatrick   /// Get GPU arch from -mcpu without checking.
125a9ac8606Spatrick   StringRef getGPUArch(const llvm::opt::ArgList &DriverArgs) const;
126a9ac8606Spatrick 
127ec727ea7Spatrick };
128ec727ea7Spatrick 
129ec727ea7Spatrick class LLVM_LIBRARY_VISIBILITY ROCMToolChain : public AMDGPUToolChain {
130ec727ea7Spatrick public:
131ec727ea7Spatrick   ROCMToolChain(const Driver &D, const llvm::Triple &Triple,
132ec727ea7Spatrick                 const llvm::opt::ArgList &Args);
133ec727ea7Spatrick   void
134ec727ea7Spatrick   addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
135ec727ea7Spatrick                         llvm::opt::ArgStringList &CC1Args,
136ec727ea7Spatrick                         Action::OffloadKind DeviceOffloadKind) const override;
137a9ac8606Spatrick 
138a9ac8606Spatrick   // Returns a list of device library names shared by different languages
139a9ac8606Spatrick   llvm::SmallVector<std::string, 12>
140a9ac8606Spatrick   getCommonDeviceLibNames(const llvm::opt::ArgList &DriverArgs,
141*12c85518Srobert                           const std::string &GPUArch,
142*12c85518Srobert                           bool isOpenMP = false) const;
143e5dd7070Spatrick };
144e5dd7070Spatrick 
145e5dd7070Spatrick } // end namespace toolchains
146e5dd7070Spatrick } // end namespace driver
147e5dd7070Spatrick } // end namespace clang
148e5dd7070Spatrick 
149e5dd7070Spatrick #endif // LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_AMDGPU_H
150