clang 23.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:
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";
42 return "clang-offload-bundler";
44 return "clang-offload-unbundler";
46 return "llvm-offload-binary";
48 return "clang-linker-wrapper";
50 return "static-lib-linker";
52 return "binary-analyzer";
54 return "binary-translator";
55 case ObjcopyJobClass:
56 return "objcopy";
57 }
58
59 llvm_unreachable("invalid class");
60}
61
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??");
75 OffloadingArch = OArch;
76 OffloadingToolChain = OToolChain;
77
78 for (auto *A : Inputs)
79 A->propagateDeviceOffloadInfo(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
88 "Setting a host kind in a device action.");
89 ActiveOffloadKindMask |= OKinds;
90 OffloadingArch = OArch;
91
92 for (auto *A : Inputs)
93 A->propagateHostOffloadInfo(ActiveOffloadKindMask, OArch);
94}
95
104
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
125 return {};
126
127 std::string Res("host");
128 assert(!((ActiveOffloadKindMask & OFK_Cuda) &&
130 "Cannot offload CUDA and HIP at the same time");
132 Res += "-cuda";
134 Res += "-hip";
136 Res += "-openmp";
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
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'.
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
192 : Action(BindArchClass, Input), ArchName(ArchName) {}
193
194void OffloadAction::anchor() {}
195
197 : Action(OffloadClass, HDep.getAction()), HostTC(HDep.getToolChain()) {
202}
203
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(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(OKinds[i], BArchs[i], OTCs[i]);
222}
223
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();
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(A);
238 A->propagateDeviceOffloadInfo(DDeps.getOffloadKinds()[i],
239 DDeps.getBoundArchs()[i],
240 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
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
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
282
283void OffloadAction::doOnEachDependence(bool IsHostDependence,
284 const OffloadActionWorkTy &Work) const {
285 if (IsHostDependence)
286 doOnHostDependence(Work);
287 else
289}
290
291bool OffloadAction::hasHostDependence() const { return HostTC != nullptr; }
292
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
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
316 BoundArch BA, OffloadKind OKind) {
317 DeviceActions.push_back(&A);
318 DeviceToolChains.push_back(&TC);
319 DeviceBoundArchs.push_back(BA);
320 DeviceOffloadKinds.push_back(OKind);
321}
322
324 BoundArch BA,
325 unsigned OffloadKindMask) {
326 DeviceActions.push_back(&A);
327 DeviceToolChains.push_back(&TC);
328 DeviceBoundArchs.push_back(BA);
329
330 // Add each active offloading kind from a mask.
332 if (OKind & OffloadKindMask)
333 DeviceOffloadKinds.push_back(OKind);
334}
335
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
347 : Action(Kind, Input, Type) {}
348
350 : Action(Kind, Inputs, Type) {}
351
352void PreprocessJobAction::anchor() {}
353
356
357void PrecompileJobAction::anchor() {}
358
361
363 types::ID OutputType)
364 : JobAction(Kind, Input, OutputType) {
365 assert(isa<PrecompileJobAction>((Action*)this) && "invalid action kind");
366}
367
368void ExtractAPIJobAction::anchor() {}
369
372
373void AnalyzeJobAction::anchor() {}
374
376 : JobAction(AnalyzeJobClass, Input, OutputType) {}
377
378void CompileJobAction::anchor() {}
379
381 : JobAction(CompileJobClass, Input, OutputType) {}
382
383void BackendJobAction::anchor() {}
384
386 : JobAction(BackendJobClass, Input, OutputType) {}
387
388void AssembleJobAction::anchor() {}
389
391 : JobAction(AssembleJobClass, Input, OutputType) {}
392
393void IfsMergeJobAction::anchor() {}
394
397
398void LinkJobAction::anchor() {}
399
402
403void LipoJobAction::anchor() {}
404
407
408void DsymutilJobAction::anchor() {}
409
412
413void VerifyJobAction::anchor() {}
414
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
427
428void VerifyPCHJobAction::anchor() {}
429
432
433void OffloadBundlingJobAction::anchor() {}
434
437
438void OffloadUnbundlingJobAction::anchor() {}
439
442
443void OffloadPackagerJobAction::anchor() {}
444
448
449void LinkerWrapperJobAction::anchor() {}
450
454
455void StaticLibJobAction::anchor() {}
456
459
460void BinaryAnalyzeJobAction::anchor() {}
461
464
465void BinaryTranslatorJobAction::anchor() {}
466
470
471void ObjcopyJobAction::anchor() {}
472
Action - Represent an abstract compilation step to perform.
Definition Action.h:48
OffloadKind OffloadingDeviceKind
Offloading kind of the device.
Definition Action.h:132
Action(ActionClass Kind, types::ID Type)
Definition Action.h:140
size_type size() const
Definition Action.h:159
types::ID getType() const
Definition Action.h:154
std::string getOffloadingKindPrefix() const
Return a string containing the offload kind of the action.
Definition Action.cpp:105
const ToolChain * getOffloadingToolChain() const
Definition Action.h:218
const ToolChain * OffloadingToolChain
The Offloading toolchain associated with this device action.
Definition Action.h:138
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:148
BoundArch getOffloadingArch() const
Definition Action.h:217
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:96
static StringRef GetOffloadKindName(OffloadKind Kind)
Return a string containing a offload kind name.
Definition Action.cpp:164
const char * getClassName() const
Definition Action.h:151
OffloadKind getOffloadingDeviceKind() const
Definition Action.h:216
void propagateDeviceOffloadInfo(OffloadKind OKind, BoundArch OArch, const ToolChain *OToolChain)
Set the device offload info of this action and propagate it to its dependences.
Definition Action.cpp:62
unsigned ActiveOffloadKindMask
Offload information.
Definition Action.h:129
void propagateHostOffloadInfo(unsigned OKinds, BoundArch OArch)
Append the host offload info of this action and propagate it to its dependences.
Definition Action.cpp:82
BoundArch OffloadingArch
The Offloading architecture associated with this action.
Definition Action.h:135
ActionList & getInputs()
Definition Action.h:156
unsigned getOffloadingHostActiveKinds() const
Definition Action.h:212
AnalyzeJobAction(Action *Input, types::ID OutputType)
Definition Action.cpp:375
AssembleJobAction(Action *Input, types::ID OutputType)
Definition Action.cpp:390
BackendJobAction(Action *Input, types::ID OutputType)
Definition Action.cpp:385
BinaryAnalyzeJobAction(Action *Input, types::ID Type)
Definition Action.cpp:462
BinaryTranslatorJobAction(Action *Input, types::ID Type)
Definition Action.cpp:467
BindArchAction(Action *Input, BoundArch ArchName)
Definition Action.cpp:191
CompileJobAction(Action *Input, types::ID OutputType)
Definition Action.cpp:380
DsymutilJobAction(ActionList &Inputs, types::ID Type)
Definition Action.cpp:410
ExtractAPIJobAction(Action *Input, types::ID OutputType)
Definition Action.cpp:370
IfsMergeJobAction(ActionList &Inputs, types::ID Type)
Definition Action.cpp:395
InputAction(const llvm::opt::Arg &Input, types::ID Type, StringRef Id=StringRef())
Definition Action.cpp:186
JobAction(ActionClass Kind, Action *Input, types::ID Type)
Definition Action.cpp:346
LinkJobAction(ActionList &Inputs, types::ID Type)
Definition Action.cpp:400
LinkerWrapperJobAction(ActionList &Inputs, types::ID Type)
Definition Action.cpp:451
LipoJobAction(ActionList &Inputs, types::ID Type)
Definition Action.cpp:405
ObjcopyJobAction(Action *Input, types::ID Type)
Definition Action.cpp:473
Type used to communicate device actions.
Definition Action.h:280
const BoundArchList & getBoundArchs() const
Definition Action.h:317
const OffloadKindList & getOffloadKinds() const
Definition Action.h:318
const ActionList & getActions() const
Get each of the individual arrays.
Definition Action.h:315
void add(Action &A, const ToolChain &TC, BoundArch BA, OffloadKind OKind)
Add an action along with the associated toolchain, bound arch, and offload kind.
Definition Action.cpp:315
const ToolChainList & getToolChains() const
Definition Action.h:316
Type used to communicate host actions.
Definition Action.h:325
HostDependence(Action &A, const ToolChain &TC, BoundArch BA, const unsigned OffloadKinds)
Definition Action.h:339
void doOnEachDependence(const OffloadActionWorkTy &Work) const
Execute the work specified in Work on each dependence.
Definition Action.cpp:278
Action * getSingleDeviceDependence(bool DoNotConsiderHostActions=false) const
Return the single device dependence of this action.
Definition Action.cpp:307
bool hasSingleDeviceDependence(bool DoNotConsiderHostActions=false) const
Return true if the action has a single device dependence.
Definition Action.cpp:299
Action * getHostDependence() const
Return the host dependence of this action.
Definition Action.cpp:293
void doOnEachDeviceDependence(const OffloadActionWorkTy &Work) const
Execute the work specified in Work on each device dependence.
Definition Action.cpp:256
bool hasHostDependence() const
Return true if the action has a host dependence.
Definition Action.cpp:291
llvm::function_ref< void(Action *, const ToolChain *, BoundArch)> OffloadActionWorkTy
Definition Action.h:354
void doOnHostDependence(const OffloadActionWorkTy &Work) const
Execute the work specified in Work on the host dependence.
Definition Action.cpp:248
OffloadAction(const HostDependence &HDep)
Definition Action.cpp:196
OffloadBundlingJobAction(ActionList &Inputs)
Definition Action.cpp:435
OffloadPackagerJobAction(ActionList &Inputs, types::ID Type)
Definition Action.cpp:445
PrecompileJobAction(ActionClass Kind, Action *Input, types::ID OutputType)
Definition Action.cpp:362
PreprocessJobAction(Action *Input, types::ID OutputType)
Definition Action.cpp:354
StaticLibJobAction(ActionList &Inputs, types::ID Type)
Definition Action.cpp:457
ToolChain - Access to tools for a single platform.
Definition ToolChain.h:96
VerifyDebugInfoJobAction(Action *Input, types::ID Type)
Definition Action.cpp:424
VerifyJobAction(ActionClass Kind, Action *Input, types::ID Type)
Definition Action.cpp:415
VerifyPCHJobAction(Action *Input, types::ID Type)
Definition Action.cpp:430
SmallVector< Action *, 3 > ActionList
ActionList - Type used for lists of actions.
Definition Util.h:25
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition Address.h:330
if(T->getSizeExpr()) TRY_TO(TraverseStmt(const_cast< Expr * >(T -> getSizeExpr())))
for(const auto &A :T->param_types())
Represents a bound architecture for offload / multiple architecture compilation.