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