1//===- Action.cpp - Abstract compilation steps ----------------------------===//
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
9#include "clang/Driver/Action.h"
10#include "llvm/Support/ErrorHandling.h"
11#include <cassert>
12#include <string>
13
14using namespace clang;
15using namespace driver;
16using namespace llvm::opt;
17
18Action::~Action() = default;
19
20const char *Action::getClassName(ActionClass AC) {
21 switch (AC) {
22 case InputClass: return "input";
23 case BindArchClass: return "bind-arch";
24 case OffloadClass:
25 return "offload";
26 case PreprocessJobClass: return "preprocessor";
27 case PrecompileJobClass: return "precompiler";
28 case ExtractAPIJobClass:
29 return "api-extractor";
30 case AnalyzeJobClass:
31 return "analyzer";
32 case CompileJobClass: return "compiler";
33 case BackendJobClass: return "backend";
34 case AssembleJobClass: return "assembler";
35 case IfsMergeJobClass: return "interface-stub-merger";
36 case LinkJobClass: return "linker";
37 case LipoJobClass: return "lipo";
38 case DsymutilJobClass: return "dsymutil";
39 case VerifyDebugInfoJobClass: return "verify-debug-info";
40 case VerifyPCHJobClass: return "verify-pch";
41 case OffloadBundlingJobClass:
42 return "clang-offload-bundler";
43 case OffloadUnbundlingJobClass:
44 return "clang-offload-unbundler";
45 case OffloadPackagerJobClass:
46 return "llvm-offload-binary";
47 case LinkerWrapperJobClass:
48 return "clang-linker-wrapper";
49 case StaticLibJobClass:
50 return "static-lib-linker";
51 case BinaryAnalyzeJobClass:
52 return "binary-analyzer";
53 case BinaryTranslatorJobClass:
54 return "binary-translator";
55 case ObjcopyJobClass:
56 return "objcopy";
57 }
58
59 llvm_unreachable("invalid class");
60}
61
62void Action::propagateDeviceOffloadInfo(OffloadKind OKind, BoundArch OArch,
63 const ToolChain *OToolChain) {
64 // Offload action set its own kinds on their dependences.
65 if (Kind == OffloadClass)
66 return;
67 // Unbundling actions use the host kinds.
68 if (Kind == OffloadUnbundlingJobClass)
69 return;
70
71 assert((OffloadingDeviceKind == OKind || OffloadingDeviceKind == OFK_None) &&
72 "Setting device kind to a different device??");
73 assert(!ActiveOffloadKindMask && "Setting a device kind in a host action??");
74 OffloadingDeviceKind = OKind;
75 OffloadingArch = OArch;
76 OffloadingToolChain = OToolChain;
77
78 for (auto *A : Inputs)
79 A->propagateDeviceOffloadInfo(OKind: OffloadingDeviceKind, OArch, OToolChain);
80}
81
82void Action::propagateHostOffloadInfo(unsigned OKinds, BoundArch OArch) {
83 // Offload action set its own kinds on their dependences.
84 if (Kind == OffloadClass)
85 return;
86
87 assert(OffloadingDeviceKind == OFK_None &&
88 "Setting a host kind in a device action.");
89 ActiveOffloadKindMask |= OKinds;
90 OffloadingArch = OArch;
91
92 for (auto *A : Inputs)
93 A->propagateHostOffloadInfo(OKinds: ActiveOffloadKindMask, OArch);
94}
95
96void Action::propagateOffloadInfo(const Action *A) {
97 if (unsigned HK = A->getOffloadingHostActiveKinds())
98 propagateHostOffloadInfo(OKinds: HK, OArch: A->getOffloadingArch());
99 else
100 propagateDeviceOffloadInfo(OKind: A->getOffloadingDeviceKind(),
101 OArch: A->getOffloadingArch(),
102 OToolChain: A->getOffloadingToolChain());
103}
104
105std::string Action::getOffloadingKindPrefix() const {
106 switch (OffloadingDeviceKind) {
107 case OFK_None:
108 break;
109 case OFK_Host:
110 llvm_unreachable("Host kind is not an offloading device kind.");
111 break;
112 case OFK_Cuda:
113 return "device-cuda";
114 case OFK_OpenMP:
115 return "device-openmp";
116 case OFK_HIP:
117 return "device-hip";
118 case OFK_SYCL:
119 return "device-sycl";
120
121 // TODO: Add other programming models here.
122 }
123
124 if (!ActiveOffloadKindMask)
125 return {};
126
127 std::string Res("host");
128 assert(!((ActiveOffloadKindMask & OFK_Cuda) &&
129 (ActiveOffloadKindMask & OFK_HIP)) &&
130 "Cannot offload CUDA and HIP at the same time");
131 if (ActiveOffloadKindMask & OFK_Cuda)
132 Res += "-cuda";
133 if (ActiveOffloadKindMask & OFK_HIP)
134 Res += "-hip";
135 if (ActiveOffloadKindMask & OFK_OpenMP)
136 Res += "-openmp";
137 if (ActiveOffloadKindMask & OFK_SYCL)
138 Res += "-sycl";
139
140 // TODO: Add other programming models here.
141
142 return Res;
143}
144
145/// Return a string that can be used as prefix in order to generate unique files
146/// for each offloading kind.
147std::string
148Action::GetOffloadingFileNamePrefix(OffloadKind Kind,
149 StringRef NormalizedTriple,
150 bool CreatePrefixForHost) {
151 // Don't generate prefix for host actions unless required.
152 if (!CreatePrefixForHost && (Kind == OFK_None || Kind == OFK_Host))
153 return {};
154
155 std::string Res("-");
156 Res += GetOffloadKindName(Kind);
157 Res += "-";
158 Res += NormalizedTriple;
159 return Res;
160}
161
162/// Return a string with the offload kind name. If that is not defined, we
163/// assume 'host'.
164StringRef Action::GetOffloadKindName(OffloadKind Kind) {
165 switch (Kind) {
166 case OFK_None:
167 case OFK_Host:
168 return "host";
169 case OFK_Cuda:
170 return "cuda";
171 case OFK_OpenMP:
172 return "openmp";
173 case OFK_HIP:
174 return "hip";
175 case OFK_SYCL:
176 return "sycl";
177
178 // TODO: Add other programming models here.
179 }
180
181 llvm_unreachable("invalid offload kind");
182}
183
184void InputAction::anchor() {}
185
186InputAction::InputAction(const Arg &_Input, types::ID _Type, StringRef _Id)
187 : Action(InputClass, _Type), Input(_Input), Id(_Id.str()) {}
188
189void BindArchAction::anchor() {}
190
191BindArchAction::BindArchAction(Action *Input, BoundArch ArchName)
192 : Action(BindArchClass, Input), ArchName(ArchName) {}
193
194void OffloadAction::anchor() {}
195
196OffloadAction::OffloadAction(const HostDependence &HDep)
197 : Action(OffloadClass, HDep.getAction()), HostTC(HDep.getToolChain()) {
198 OffloadingArch = HDep.getBoundArch();
199 ActiveOffloadKindMask = HDep.getOffloadKinds();
200 HDep.getAction()->propagateHostOffloadInfo(OKinds: HDep.getOffloadKinds(),
201 OArch: OffloadingArch);
202}
203
204OffloadAction::OffloadAction(const DeviceDependences &DDeps, types::ID Ty)
205 : Action(OffloadClass, DDeps.getActions(), Ty),
206 DevToolChains(DDeps.getToolChains()) {
207 auto &OKinds = DDeps.getOffloadKinds();
208 auto &BArchs = DDeps.getBoundArchs();
209 auto &OTCs = DDeps.getToolChains();
210
211 // If all inputs agree on the same kind, use it also for this action.
212 if (llvm::all_equal(Range: OKinds))
213 OffloadingDeviceKind = OKinds.front();
214
215 // If we have a single dependency, inherit the architecture from it.
216 if (OKinds.size() == 1)
217 OffloadingArch = BArchs.front();
218
219 // Propagate info to the dependencies.
220 for (unsigned i = 0, e = getInputs().size(); i != e; ++i)
221 getInputs()[i]->propagateDeviceOffloadInfo(OKind: OKinds[i], OArch: BArchs[i], OToolChain: OTCs[i]);
222}
223
224OffloadAction::OffloadAction(const HostDependence &HDep,
225 const DeviceDependences &DDeps)
226 : Action(OffloadClass, HDep.getAction()), HostTC(HDep.getToolChain()),
227 DevToolChains(DDeps.getToolChains()) {
228 // We use the kinds of the host dependence for this action.
229 BoundArch BA = HDep.getBoundArch();
230 ActiveOffloadKindMask = HDep.getOffloadKinds();
231 HDep.getAction()->propagateHostOffloadInfo(OKinds: HDep.getOffloadKinds(), OArch: BA);
232
233 // Add device inputs and propagate info to the device actions. Do work only if
234 // we have dependencies.
235 for (unsigned i = 0, e = DDeps.getActions().size(); i != e; ++i) {
236 if (auto *A = DDeps.getActions()[i]) {
237 getInputs().push_back(Elt: A);
238 A->propagateDeviceOffloadInfo(OKind: DDeps.getOffloadKinds()[i],
239 OArch: DDeps.getBoundArchs()[i],
240 OToolChain: DDeps.getToolChains()[i]);
241 // If this action is used to forward single dependency, set the toolchain.
242 if (DDeps.getActions().size() == 1)
243 OffloadingToolChain = DDeps.getToolChains()[i];
244 }
245 }
246}
247
248void OffloadAction::doOnHostDependence(const OffloadActionWorkTy &Work) const {
249 if (!HostTC)
250 return;
251 assert(!getInputs().empty() && "No dependencies for offload action??");
252 auto *A = getInputs().front();
253 Work(A, HostTC, A->getOffloadingArch());
254}
255
256void OffloadAction::doOnEachDeviceDependence(
257 const OffloadActionWorkTy &Work) const {
258 auto I = getInputs().begin();
259 auto E = getInputs().end();
260 if (I == E)
261 return;
262
263 // We expect to have the same number of input dependences and device tool
264 // chains, except if we also have a host dependence. In that case we have one
265 // more dependence than we have device tool chains.
266 assert(getInputs().size() == DevToolChains.size() + (HostTC ? 1 : 0) &&
267 "Sizes of action dependences and toolchains are not consistent!");
268
269 // Skip host action
270 if (HostTC)
271 ++I;
272
273 auto TI = DevToolChains.begin();
274 for (; I != E; ++I, ++TI)
275 Work(*I, *TI, (*I)->getOffloadingArch());
276}
277
278void OffloadAction::doOnEachDependence(const OffloadActionWorkTy &Work) const {
279 doOnHostDependence(Work);
280 doOnEachDeviceDependence(Work);
281}
282
283void OffloadAction::doOnEachDependence(bool IsHostDependence,
284 const OffloadActionWorkTy &Work) const {
285 if (IsHostDependence)
286 doOnHostDependence(Work);
287 else
288 doOnEachDeviceDependence(Work);
289}
290
291bool OffloadAction::hasHostDependence() const { return HostTC != nullptr; }
292
293Action *OffloadAction::getHostDependence() const {
294 assert(hasHostDependence() && "Host dependence does not exist!");
295 assert(!getInputs().empty() && "No dependencies for offload action??");
296 return HostTC ? getInputs().front() : nullptr;
297}
298
299bool OffloadAction::hasSingleDeviceDependence(
300 bool DoNotConsiderHostActions) const {
301 if (DoNotConsiderHostActions)
302 return getInputs().size() == (HostTC ? 2 : 1);
303 return !HostTC && getInputs().size() == 1;
304}
305
306Action *
307OffloadAction::getSingleDeviceDependence(bool DoNotConsiderHostActions) const {
308 assert(hasSingleDeviceDependence(DoNotConsiderHostActions) &&
309 "Single device dependence does not exist!");
310 // The previous assert ensures the number of entries in getInputs() is
311 // consistent with what we are doing here.
312 return HostTC ? getInputs()[1] : getInputs().front();
313}
314
315void OffloadAction::DeviceDependences::add(Action &A, const ToolChain &TC,
316 BoundArch BA, OffloadKind OKind) {
317 DeviceActions.push_back(Elt: &A);
318 DeviceToolChains.push_back(Elt: &TC);
319 DeviceBoundArchs.push_back(Elt: BA);
320 DeviceOffloadKinds.push_back(Elt: OKind);
321}
322
323void OffloadAction::DeviceDependences::add(Action &A, const ToolChain &TC,
324 BoundArch BA,
325 unsigned OffloadKindMask) {
326 DeviceActions.push_back(Elt: &A);
327 DeviceToolChains.push_back(Elt: &TC);
328 DeviceBoundArchs.push_back(Elt: BA);
329
330 // Add each active offloading kind from a mask.
331 for (OffloadKind OKind : {OFK_OpenMP, OFK_Cuda, OFK_HIP, OFK_SYCL})
332 if (OKind & OffloadKindMask)
333 DeviceOffloadKinds.push_back(Elt: OKind);
334}
335
336OffloadAction::HostDependence::HostDependence(Action &A, const ToolChain &TC,
337 BoundArch BA,
338 const DeviceDependences &DDeps)
339 : HostAction(A), HostToolChain(TC), HostBoundArch(BA) {
340 for (auto K : DDeps.getOffloadKinds())
341 HostOffloadKinds |= K;
342}
343
344void JobAction::anchor() {}
345
346JobAction::JobAction(ActionClass Kind, Action *Input, types::ID Type)
347 : Action(Kind, Input, Type) {}
348
349JobAction::JobAction(ActionClass Kind, const ActionList &Inputs, types::ID Type)
350 : Action(Kind, Inputs, Type) {}
351
352void PreprocessJobAction::anchor() {}
353
354PreprocessJobAction::PreprocessJobAction(Action *Input, types::ID OutputType)
355 : JobAction(PreprocessJobClass, Input, OutputType) {}
356
357void PrecompileJobAction::anchor() {}
358
359PrecompileJobAction::PrecompileJobAction(Action *Input, types::ID OutputType)
360 : JobAction(PrecompileJobClass, Input, OutputType) {}
361
362PrecompileJobAction::PrecompileJobAction(ActionClass Kind, Action *Input,
363 types::ID OutputType)
364 : JobAction(Kind, Input, OutputType) {
365 assert(isa<PrecompileJobAction>((Action*)this) && "invalid action kind");
366}
367
368void ExtractAPIJobAction::anchor() {}
369
370ExtractAPIJobAction::ExtractAPIJobAction(Action *Inputs, types::ID OutputType)
371 : JobAction(ExtractAPIJobClass, Inputs, OutputType) {}
372
373void AnalyzeJobAction::anchor() {}
374
375AnalyzeJobAction::AnalyzeJobAction(Action *Input, types::ID OutputType)
376 : JobAction(AnalyzeJobClass, Input, OutputType) {}
377
378void CompileJobAction::anchor() {}
379
380CompileJobAction::CompileJobAction(Action *Input, types::ID OutputType)
381 : JobAction(CompileJobClass, Input, OutputType) {}
382
383void BackendJobAction::anchor() {}
384
385BackendJobAction::BackendJobAction(Action *Input, types::ID OutputType)
386 : JobAction(BackendJobClass, Input, OutputType) {}
387
388void AssembleJobAction::anchor() {}
389
390AssembleJobAction::AssembleJobAction(Action *Input, types::ID OutputType)
391 : JobAction(AssembleJobClass, Input, OutputType) {}
392
393void IfsMergeJobAction::anchor() {}
394
395IfsMergeJobAction::IfsMergeJobAction(ActionList &Inputs, types::ID Type)
396 : JobAction(IfsMergeJobClass, Inputs, Type) {}
397
398void LinkJobAction::anchor() {}
399
400LinkJobAction::LinkJobAction(ActionList &Inputs, types::ID Type)
401 : JobAction(LinkJobClass, Inputs, Type) {}
402
403void LipoJobAction::anchor() {}
404
405LipoJobAction::LipoJobAction(ActionList &Inputs, types::ID Type)
406 : JobAction(LipoJobClass, Inputs, Type) {}
407
408void DsymutilJobAction::anchor() {}
409
410DsymutilJobAction::DsymutilJobAction(ActionList &Inputs, types::ID Type)
411 : JobAction(DsymutilJobClass, Inputs, Type) {}
412
413void VerifyJobAction::anchor() {}
414
415VerifyJobAction::VerifyJobAction(ActionClass Kind, Action *Input,
416 types::ID Type)
417 : JobAction(Kind, Input, Type) {
418 assert((Kind == VerifyDebugInfoJobClass || Kind == VerifyPCHJobClass) &&
419 "ActionClass is not a valid VerifyJobAction");
420}
421
422void VerifyDebugInfoJobAction::anchor() {}
423
424VerifyDebugInfoJobAction::VerifyDebugInfoJobAction(Action *Input,
425 types::ID Type)
426 : VerifyJobAction(VerifyDebugInfoJobClass, Input, Type) {}
427
428void VerifyPCHJobAction::anchor() {}
429
430VerifyPCHJobAction::VerifyPCHJobAction(Action *Input, types::ID Type)
431 : VerifyJobAction(VerifyPCHJobClass, Input, Type) {}
432
433void OffloadBundlingJobAction::anchor() {}
434
435OffloadBundlingJobAction::OffloadBundlingJobAction(ActionList &Inputs)
436 : JobAction(OffloadBundlingJobClass, Inputs, Inputs.back()->getType()) {}
437
438void OffloadUnbundlingJobAction::anchor() {}
439
440OffloadUnbundlingJobAction::OffloadUnbundlingJobAction(Action *Input)
441 : JobAction(OffloadUnbundlingJobClass, Input, Input->getType()) {}
442
443void OffloadPackagerJobAction::anchor() {}
444
445OffloadPackagerJobAction::OffloadPackagerJobAction(ActionList &Inputs,
446 types::ID Type)
447 : JobAction(OffloadPackagerJobClass, Inputs, Type) {}
448
449void LinkerWrapperJobAction::anchor() {}
450
451LinkerWrapperJobAction::LinkerWrapperJobAction(ActionList &Inputs,
452 types::ID Type)
453 : JobAction(LinkerWrapperJobClass, Inputs, Type) {}
454
455void StaticLibJobAction::anchor() {}
456
457StaticLibJobAction::StaticLibJobAction(ActionList &Inputs, types::ID Type)
458 : JobAction(StaticLibJobClass, Inputs, Type) {}
459
460void BinaryAnalyzeJobAction::anchor() {}
461
462BinaryAnalyzeJobAction::BinaryAnalyzeJobAction(Action *Input, types::ID Type)
463 : JobAction(BinaryAnalyzeJobClass, Input, Type) {}
464
465void BinaryTranslatorJobAction::anchor() {}
466
467BinaryTranslatorJobAction::BinaryTranslatorJobAction(Action *Input,
468 types::ID Type)
469 : JobAction(BinaryTranslatorJobClass, Input, Type) {}
470
471void ObjcopyJobAction::anchor() {}
472
473ObjcopyJobAction::ObjcopyJobAction(Action *Input, types::ID Type)
474 : JobAction(ObjcopyJobClass, Input, Type) {}
475