1 //===- Action.cpp - Abstract compilation steps ----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/Driver/Action.h"
11 #include "llvm/Support/ErrorHandling.h"
15 using namespace clang;
16 using namespace driver;
17 using namespace llvm::opt;
19 Action::~Action() = default;
21 const char *Action::getClassName(ActionClass AC) {
23 case InputClass: return "input";
24 case BindArchClass: return "bind-arch";
27 case PreprocessJobClass: return "preprocessor";
28 case PrecompileJobClass: return "precompiler";
29 case AnalyzeJobClass: return "analyzer";
30 case MigrateJobClass: return "migrator";
31 case CompileJobClass: return "compiler";
32 case BackendJobClass: return "backend";
33 case AssembleJobClass: return "assembler";
34 case LinkJobClass: return "linker";
35 case LipoJobClass: return "lipo";
36 case DsymutilJobClass: return "dsymutil";
37 case VerifyDebugInfoJobClass: return "verify-debug-info";
38 case VerifyPCHJobClass: return "verify-pch";
39 case OffloadBundlingJobClass:
40 return "clang-offload-bundler";
41 case OffloadUnbundlingJobClass:
42 return "clang-offload-unbundler";
45 llvm_unreachable("invalid class");
48 void Action::propagateDeviceOffloadInfo(OffloadKind OKind, const char *OArch) {
49 // Offload action set its own kinds on their dependences.
50 if (Kind == OffloadClass)
52 // Unbundling actions use the host kinds.
53 if (Kind == OffloadUnbundlingJobClass)
56 assert((OffloadingDeviceKind == OKind || OffloadingDeviceKind == OFK_None) &&
57 "Setting device kind to a different device??");
58 assert(!ActiveOffloadKindMask && "Setting a device kind in a host action??");
59 OffloadingDeviceKind = OKind;
60 OffloadingArch = OArch;
62 for (auto *A : Inputs)
63 A->propagateDeviceOffloadInfo(OffloadingDeviceKind, OArch);
66 void Action::propagateHostOffloadInfo(unsigned OKinds, const char *OArch) {
67 // Offload action set its own kinds on their dependences.
68 if (Kind == OffloadClass)
71 assert(OffloadingDeviceKind == OFK_None &&
72 "Setting a host kind in a device action.");
73 ActiveOffloadKindMask |= OKinds;
74 OffloadingArch = OArch;
76 for (auto *A : Inputs)
77 A->propagateHostOffloadInfo(ActiveOffloadKindMask, OArch);
80 void Action::propagateOffloadInfo(const Action *A) {
81 if (unsigned HK = A->getOffloadingHostActiveKinds())
82 propagateHostOffloadInfo(HK, A->getOffloadingArch());
84 propagateDeviceOffloadInfo(A->getOffloadingDeviceKind(),
85 A->getOffloadingArch());
88 std::string Action::getOffloadingKindPrefix() const {
89 switch (OffloadingDeviceKind) {
93 llvm_unreachable("Host kind is not an offloading device kind.");
98 return "device-openmp";
102 // TODO: Add other programming models here.
105 if (!ActiveOffloadKindMask)
108 std::string Res("host");
109 assert(!((ActiveOffloadKindMask & OFK_Cuda) &&
110 (ActiveOffloadKindMask & OFK_HIP)) &&
111 "Cannot offload CUDA and HIP at the same time");
112 if (ActiveOffloadKindMask & OFK_Cuda)
114 if (ActiveOffloadKindMask & OFK_HIP)
116 if (ActiveOffloadKindMask & OFK_OpenMP)
119 // TODO: Add other programming models here.
124 /// Return a string that can be used as prefix in order to generate unique files
125 /// for each offloading kind.
127 Action::GetOffloadingFileNamePrefix(OffloadKind Kind,
128 StringRef NormalizedTriple,
129 bool CreatePrefixForHost) {
130 // Don't generate prefix for host actions unless required.
131 if (!CreatePrefixForHost && (Kind == OFK_None || Kind == OFK_Host))
134 std::string Res("-");
135 Res += GetOffloadKindName(Kind);
137 Res += NormalizedTriple;
141 /// Return a string with the offload kind name. If that is not defined, we
143 StringRef Action::GetOffloadKindName(OffloadKind Kind) {
155 // TODO: Add other programming models here.
158 llvm_unreachable("invalid offload kind");
161 void InputAction::anchor() {}
163 InputAction::InputAction(const Arg &_Input, types::ID _Type)
164 : Action(InputClass, _Type), Input(_Input) {}
166 void BindArchAction::anchor() {}
168 BindArchAction::BindArchAction(Action *Input, StringRef ArchName)
169 : Action(BindArchClass, Input), ArchName(ArchName) {}
171 void OffloadAction::anchor() {}
173 OffloadAction::OffloadAction(const HostDependence &HDep)
174 : Action(OffloadClass, HDep.getAction()), HostTC(HDep.getToolChain()) {
175 OffloadingArch = HDep.getBoundArch();
176 ActiveOffloadKindMask = HDep.getOffloadKinds();
177 HDep.getAction()->propagateHostOffloadInfo(HDep.getOffloadKinds(),
178 HDep.getBoundArch());
181 OffloadAction::OffloadAction(const DeviceDependences &DDeps, types::ID Ty)
182 : Action(OffloadClass, DDeps.getActions(), Ty),
183 DevToolChains(DDeps.getToolChains()) {
184 auto &OKinds = DDeps.getOffloadKinds();
185 auto &BArchs = DDeps.getBoundArchs();
187 // If all inputs agree on the same kind, use it also for this action.
188 if (llvm::all_of(OKinds, [&](OffloadKind K) { return K == OKinds.front(); }))
189 OffloadingDeviceKind = OKinds.front();
191 // If we have a single dependency, inherit the architecture from it.
192 if (OKinds.size() == 1)
193 OffloadingArch = BArchs.front();
195 // Propagate info to the dependencies.
196 for (unsigned i = 0, e = getInputs().size(); i != e; ++i)
197 getInputs()[i]->propagateDeviceOffloadInfo(OKinds[i], BArchs[i]);
200 OffloadAction::OffloadAction(const HostDependence &HDep,
201 const DeviceDependences &DDeps)
202 : Action(OffloadClass, HDep.getAction()), HostTC(HDep.getToolChain()),
203 DevToolChains(DDeps.getToolChains()) {
204 // We use the kinds of the host dependence for this action.
205 OffloadingArch = HDep.getBoundArch();
206 ActiveOffloadKindMask = HDep.getOffloadKinds();
207 HDep.getAction()->propagateHostOffloadInfo(HDep.getOffloadKinds(),
208 HDep.getBoundArch());
210 // Add device inputs and propagate info to the device actions. Do work only if
211 // we have dependencies.
212 for (unsigned i = 0, e = DDeps.getActions().size(); i != e; ++i)
213 if (auto *A = DDeps.getActions()[i]) {
214 getInputs().push_back(A);
215 A->propagateDeviceOffloadInfo(DDeps.getOffloadKinds()[i],
216 DDeps.getBoundArchs()[i]);
220 void OffloadAction::doOnHostDependence(const OffloadActionWorkTy &Work) const {
223 assert(!getInputs().empty() && "No dependencies for offload action??");
224 auto *A = getInputs().front();
225 Work(A, HostTC, A->getOffloadingArch());
228 void OffloadAction::doOnEachDeviceDependence(
229 const OffloadActionWorkTy &Work) const {
230 auto I = getInputs().begin();
231 auto E = getInputs().end();
235 // We expect to have the same number of input dependences and device tool
236 // chains, except if we also have a host dependence. In that case we have one
237 // more dependence than we have device tool chains.
238 assert(getInputs().size() == DevToolChains.size() + (HostTC ? 1 : 0) &&
239 "Sizes of action dependences and toolchains are not consistent!");
245 auto TI = DevToolChains.begin();
246 for (; I != E; ++I, ++TI)
247 Work(*I, *TI, (*I)->getOffloadingArch());
250 void OffloadAction::doOnEachDependence(const OffloadActionWorkTy &Work) const {
251 doOnHostDependence(Work);
252 doOnEachDeviceDependence(Work);
255 void OffloadAction::doOnEachDependence(bool IsHostDependence,
256 const OffloadActionWorkTy &Work) const {
257 if (IsHostDependence)
258 doOnHostDependence(Work);
260 doOnEachDeviceDependence(Work);
263 bool OffloadAction::hasHostDependence() const { return HostTC != nullptr; }
265 Action *OffloadAction::getHostDependence() const {
266 assert(hasHostDependence() && "Host dependence does not exist!");
267 assert(!getInputs().empty() && "No dependencies for offload action??");
268 return HostTC ? getInputs().front() : nullptr;
271 bool OffloadAction::hasSingleDeviceDependence(
272 bool DoNotConsiderHostActions) const {
273 if (DoNotConsiderHostActions)
274 return getInputs().size() == (HostTC ? 2 : 1);
275 return !HostTC && getInputs().size() == 1;
279 OffloadAction::getSingleDeviceDependence(bool DoNotConsiderHostActions) const {
280 assert(hasSingleDeviceDependence(DoNotConsiderHostActions) &&
281 "Single device dependence does not exist!");
282 // The previous assert ensures the number of entries in getInputs() is
283 // consistent with what we are doing here.
284 return HostTC ? getInputs()[1] : getInputs().front();
287 void OffloadAction::DeviceDependences::add(Action &A, const ToolChain &TC,
288 const char *BoundArch,
290 DeviceActions.push_back(&A);
291 DeviceToolChains.push_back(&TC);
292 DeviceBoundArchs.push_back(BoundArch);
293 DeviceOffloadKinds.push_back(OKind);
296 OffloadAction::HostDependence::HostDependence(Action &A, const ToolChain &TC,
297 const char *BoundArch,
298 const DeviceDependences &DDeps)
299 : HostAction(A), HostToolChain(TC), HostBoundArch(BoundArch) {
300 for (auto K : DDeps.getOffloadKinds())
301 HostOffloadKinds |= K;
304 void JobAction::anchor() {}
306 JobAction::JobAction(ActionClass Kind, Action *Input, types::ID Type)
307 : Action(Kind, Input, Type) {}
309 JobAction::JobAction(ActionClass Kind, const ActionList &Inputs, types::ID Type)
310 : Action(Kind, Inputs, Type) {}
312 void PreprocessJobAction::anchor() {}
314 PreprocessJobAction::PreprocessJobAction(Action *Input, types::ID OutputType)
315 : JobAction(PreprocessJobClass, Input, OutputType) {}
317 void PrecompileJobAction::anchor() {}
319 PrecompileJobAction::PrecompileJobAction(Action *Input, types::ID OutputType)
320 : JobAction(PrecompileJobClass, Input, OutputType) {}
322 void AnalyzeJobAction::anchor() {}
324 AnalyzeJobAction::AnalyzeJobAction(Action *Input, types::ID OutputType)
325 : JobAction(AnalyzeJobClass, Input, OutputType) {}
327 void MigrateJobAction::anchor() {}
329 MigrateJobAction::MigrateJobAction(Action *Input, types::ID OutputType)
330 : JobAction(MigrateJobClass, Input, OutputType) {}
332 void CompileJobAction::anchor() {}
334 CompileJobAction::CompileJobAction(Action *Input, types::ID OutputType)
335 : JobAction(CompileJobClass, Input, OutputType) {}
337 void BackendJobAction::anchor() {}
339 BackendJobAction::BackendJobAction(Action *Input, types::ID OutputType)
340 : JobAction(BackendJobClass, Input, OutputType) {}
342 void AssembleJobAction::anchor() {}
344 AssembleJobAction::AssembleJobAction(Action *Input, types::ID OutputType)
345 : JobAction(AssembleJobClass, Input, OutputType) {}
347 void LinkJobAction::anchor() {}
349 LinkJobAction::LinkJobAction(ActionList &Inputs, types::ID Type)
350 : JobAction(LinkJobClass, Inputs, Type) {}
352 void LipoJobAction::anchor() {}
354 LipoJobAction::LipoJobAction(ActionList &Inputs, types::ID Type)
355 : JobAction(LipoJobClass, Inputs, Type) {}
357 void DsymutilJobAction::anchor() {}
359 DsymutilJobAction::DsymutilJobAction(ActionList &Inputs, types::ID Type)
360 : JobAction(DsymutilJobClass, Inputs, Type) {}
362 void VerifyJobAction::anchor() {}
364 VerifyJobAction::VerifyJobAction(ActionClass Kind, Action *Input,
366 : JobAction(Kind, Input, Type) {
367 assert((Kind == VerifyDebugInfoJobClass || Kind == VerifyPCHJobClass) &&
368 "ActionClass is not a valid VerifyJobAction");
371 void VerifyDebugInfoJobAction::anchor() {}
373 VerifyDebugInfoJobAction::VerifyDebugInfoJobAction(Action *Input,
375 : VerifyJobAction(VerifyDebugInfoJobClass, Input, Type) {}
377 void VerifyPCHJobAction::anchor() {}
379 VerifyPCHJobAction::VerifyPCHJobAction(Action *Input, types::ID Type)
380 : VerifyJobAction(VerifyPCHJobClass, Input, Type) {}
382 void OffloadBundlingJobAction::anchor() {}
384 OffloadBundlingJobAction::OffloadBundlingJobAction(ActionList &Inputs)
385 : JobAction(OffloadBundlingJobClass, Inputs, Inputs.front()->getType()) {}
387 void OffloadUnbundlingJobAction::anchor() {}
389 OffloadUnbundlingJobAction::OffloadUnbundlingJobAction(Action *Input)
390 : JobAction(OffloadUnbundlingJobClass, Input, Input->getType()) {}