1 //===- Action.h - Abstract compilation steps --------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CLANG_DRIVER_ACTION_H
10 #define LLVM_CLANG_DRIVER_ACTION_H
12 #include "clang/Basic/LLVM.h"
13 #include "clang/Driver/Types.h"
14 #include "clang/Driver/Util.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/iterator_range.h"
35 /// Action - Represent an abstract compilation step to perform.
37 /// An action represents an edge in the compilation graph; typically
38 /// it is a job to transform an input using some tool.
40 /// The current driver is hard wired to expect actions which produce a
41 /// single primary output, at least in terms of controlling the
42 /// compilation. Actions can produce auxiliary files, but can only
43 /// produce a single output to feed into subsequent actions.
45 /// Actions are usually owned by a Compilation, which creates new
46 /// actions via MakeAction().
49 using size_type = ActionList::size_type;
50 using input_iterator = ActionList::iterator;
51 using input_const_iterator = ActionList::const_iterator;
52 using input_range = llvm::iterator_range<input_iterator>;
53 using input_const_range = llvm::iterator_range<input_const_iterator>;
61 HeaderModulePrecompileJobClass,
71 VerifyDebugInfoJobClass,
73 OffloadBundlingJobClass,
74 OffloadUnbundlingJobClass,
75 OffloadWrapperJobClass,
77 JobClassFirst = PreprocessJobClass,
78 JobClassLast = OffloadWrapperJobClass
81 // The offloading kind determines if this action is binded to a particular
82 // programming model. Each entry reserves one bit. We also have a special kind
83 // to designate the host offloading tool chain.
87 // The host offloading tool chain.
90 // The device offloading tool chains - one bit for each programming model.
96 static const char *getClassName(ActionClass AC);
101 /// The output type of this action.
106 /// Flag that is set to true if this action can be collapsed with others
107 /// actions that depend on it. This is true by default and set to false when
108 /// the action is used by two different tool chains, which is enabled by the
109 /// offloading support implementation.
110 bool CanBeCollapsedWithNextDependentAction = true;
114 /// Offload information.
117 /// The host offloading kind - a combination of kinds encoded in a mask.
118 /// Multiple programming models may be supported simultaneously by the same
120 unsigned ActiveOffloadKindMask = 0u;
122 /// Offloading kind of the device.
123 OffloadKind OffloadingDeviceKind = OFK_None;
125 /// The Offloading architecture associated with this action.
126 const char *OffloadingArch = nullptr;
128 Action(ActionClass Kind, types::ID Type) : Action(Kind, ActionList(), Type) {}
129 Action(ActionClass Kind, Action *Input, types::ID Type)
130 : Action(Kind, ActionList({Input}), Type) {}
131 Action(ActionClass Kind, Action *Input)
132 : Action(Kind, ActionList({Input}), Input->getType()) {}
133 Action(ActionClass Kind, const ActionList &Inputs, types::ID Type)
134 : Kind(Kind), Type(Type), Inputs(Inputs) {}
139 const char *getClassName() const { return Action::getClassName(getKind()); }
141 ActionClass getKind() const { return Kind; }
142 types::ID getType() const { return Type; }
144 ActionList &getInputs() { return Inputs; }
145 const ActionList &getInputs() const { return Inputs; }
147 size_type size() const { return Inputs.size(); }
149 input_iterator input_begin() { return Inputs.begin(); }
150 input_iterator input_end() { return Inputs.end(); }
151 input_range inputs() { return input_range(input_begin(), input_end()); }
152 input_const_iterator input_begin() const { return Inputs.begin(); }
153 input_const_iterator input_end() const { return Inputs.end(); }
154 input_const_range inputs() const {
155 return input_const_range(input_begin(), input_end());
158 /// Mark this action as not legal to collapse.
159 void setCannotBeCollapsedWithNextDependentAction() {
160 CanBeCollapsedWithNextDependentAction = false;
163 /// Return true if this function can be collapsed with others.
164 bool isCollapsingWithNextDependentActionLegal() const {
165 return CanBeCollapsedWithNextDependentAction;
168 /// Return a string containing the offload kind of the action.
169 std::string getOffloadingKindPrefix() const;
171 /// Return a string that can be used as prefix in order to generate unique
172 /// files for each offloading kind. By default, no prefix is used for
173 /// non-device kinds, except if \a CreatePrefixForHost is set.
175 GetOffloadingFileNamePrefix(OffloadKind Kind,
176 StringRef NormalizedTriple,
177 bool CreatePrefixForHost = false);
179 /// Return a string containing a offload kind name.
180 static StringRef GetOffloadKindName(OffloadKind Kind);
182 /// Set the device offload info of this action and propagate it to its
184 void propagateDeviceOffloadInfo(OffloadKind OKind, const char *OArch);
186 /// Append the host offload info of this action and propagate it to its
188 void propagateHostOffloadInfo(unsigned OKinds, const char *OArch);
190 /// Set the offload info of this action to be the same as the provided action,
191 /// and propagate it to its dependences.
192 void propagateOffloadInfo(const Action *A);
194 unsigned getOffloadingHostActiveKinds() const {
195 return ActiveOffloadKindMask;
198 OffloadKind getOffloadingDeviceKind() const { return OffloadingDeviceKind; }
199 const char *getOffloadingArch() const { return OffloadingArch; }
201 /// Check if this action have any offload kinds. Note that host offload kinds
202 /// are only set if the action is a dependence to a host offload action.
203 bool isHostOffloading(OffloadKind OKind) const {
204 return ActiveOffloadKindMask & OKind;
206 bool isDeviceOffloading(OffloadKind OKind) const {
207 return OffloadingDeviceKind == OKind;
209 bool isOffloading(OffloadKind OKind) const {
210 return isHostOffloading(OKind) || isDeviceOffloading(OKind);
214 class InputAction : public Action {
215 const llvm::opt::Arg &Input;
217 virtual void anchor();
220 InputAction(const llvm::opt::Arg &Input, types::ID Type);
222 const llvm::opt::Arg &getInputArg() const { return Input; }
224 static bool classof(const Action *A) {
225 return A->getKind() == InputClass;
229 class BindArchAction : public Action {
230 virtual void anchor();
232 /// The architecture to bind, or 0 if the default architecture
237 BindArchAction(Action *Input, StringRef ArchName);
239 StringRef getArchName() const { return ArchName; }
241 static bool classof(const Action *A) {
242 return A->getKind() == BindArchClass;
246 /// An offload action combines host or/and device actions according to the
247 /// programming model implementation needs and propagates the offloading kind to
249 class OffloadAction final : public Action {
250 virtual void anchor();
253 /// Type used to communicate device actions. It associates bound architecture,
254 /// toolchain, and offload kind to each action.
255 class DeviceDependences final {
257 using ToolChainList = SmallVector<const ToolChain *, 3>;
258 using BoundArchList = SmallVector<const char *, 3>;
259 using OffloadKindList = SmallVector<OffloadKind, 3>;
262 // Lists that keep the information for each dependency. All the lists are
263 // meant to be updated in sync. We are adopting separate lists instead of a
264 // list of structs, because that simplifies forwarding the actions list to
265 // initialize the inputs of the base Action class.
267 /// The dependence actions.
268 ActionList DeviceActions;
270 /// The offloading toolchains that should be used with the action.
271 ToolChainList DeviceToolChains;
273 /// The architectures that should be used with this action.
274 BoundArchList DeviceBoundArchs;
276 /// The offload kind of each dependence.
277 OffloadKindList DeviceOffloadKinds;
280 /// Add a action along with the associated toolchain, bound arch, and
282 void add(Action &A, const ToolChain &TC, const char *BoundArch,
285 /// Get each of the individual arrays.
286 const ActionList &getActions() const { return DeviceActions; }
287 const ToolChainList &getToolChains() const { return DeviceToolChains; }
288 const BoundArchList &getBoundArchs() const { return DeviceBoundArchs; }
289 const OffloadKindList &getOffloadKinds() const {
290 return DeviceOffloadKinds;
294 /// Type used to communicate host actions. It associates bound architecture,
295 /// toolchain, and offload kinds to the host action.
296 class HostDependence final {
297 /// The dependence action.
300 /// The offloading toolchain that should be used with the action.
301 const ToolChain &HostToolChain;
303 /// The architectures that should be used with this action.
304 const char *HostBoundArch = nullptr;
306 /// The offload kind of each dependence.
307 unsigned HostOffloadKinds = 0u;
310 HostDependence(Action &A, const ToolChain &TC, const char *BoundArch,
311 const unsigned OffloadKinds)
312 : HostAction(A), HostToolChain(TC), HostBoundArch(BoundArch),
313 HostOffloadKinds(OffloadKinds) {}
315 /// Constructor version that obtains the offload kinds from the device
317 HostDependence(Action &A, const ToolChain &TC, const char *BoundArch,
318 const DeviceDependences &DDeps);
319 Action *getAction() const { return &HostAction; }
320 const ToolChain *getToolChain() const { return &HostToolChain; }
321 const char *getBoundArch() const { return HostBoundArch; }
322 unsigned getOffloadKinds() const { return HostOffloadKinds; }
325 using OffloadActionWorkTy =
326 llvm::function_ref<void(Action *, const ToolChain *, const char *)>;
329 /// The host offloading toolchain that should be used with the action.
330 const ToolChain *HostTC = nullptr;
332 /// The tool chains associated with the list of actions.
333 DeviceDependences::ToolChainList DevToolChains;
336 OffloadAction(const HostDependence &HDep);
337 OffloadAction(const DeviceDependences &DDeps, types::ID Ty);
338 OffloadAction(const HostDependence &HDep, const DeviceDependences &DDeps);
340 /// Execute the work specified in \a Work on the host dependence.
341 void doOnHostDependence(const OffloadActionWorkTy &Work) const;
343 /// Execute the work specified in \a Work on each device dependence.
344 void doOnEachDeviceDependence(const OffloadActionWorkTy &Work) const;
346 /// Execute the work specified in \a Work on each dependence.
347 void doOnEachDependence(const OffloadActionWorkTy &Work) const;
349 /// Execute the work specified in \a Work on each host or device dependence if
350 /// \a IsHostDependenceto is true or false, respectively.
351 void doOnEachDependence(bool IsHostDependence,
352 const OffloadActionWorkTy &Work) const;
354 /// Return true if the action has a host dependence.
355 bool hasHostDependence() const;
357 /// Return the host dependence of this action. This function is only expected
358 /// to be called if the host dependence exists.
359 Action *getHostDependence() const;
361 /// Return true if the action has a single device dependence. If \a
362 /// DoNotConsiderHostActions is set, ignore the host dependence, if any, while
363 /// accounting for the number of dependences.
364 bool hasSingleDeviceDependence(bool DoNotConsiderHostActions = false) const;
366 /// Return the single device dependence of this action. This function is only
367 /// expected to be called if a single device dependence exists. If \a
368 /// DoNotConsiderHostActions is set, a host dependence is allowed.
370 getSingleDeviceDependence(bool DoNotConsiderHostActions = false) const;
372 static bool classof(const Action *A) { return A->getKind() == OffloadClass; }
375 class JobAction : public Action {
376 virtual void anchor();
379 JobAction(ActionClass Kind, Action *Input, types::ID Type);
380 JobAction(ActionClass Kind, const ActionList &Inputs, types::ID Type);
383 static bool classof(const Action *A) {
384 return (A->getKind() >= JobClassFirst &&
385 A->getKind() <= JobClassLast);
389 class PreprocessJobAction : public JobAction {
390 void anchor() override;
393 PreprocessJobAction(Action *Input, types::ID OutputType);
395 static bool classof(const Action *A) {
396 return A->getKind() == PreprocessJobClass;
400 class PrecompileJobAction : public JobAction {
401 void anchor() override;
404 PrecompileJobAction(ActionClass Kind, Action *Input, types::ID OutputType);
407 PrecompileJobAction(Action *Input, types::ID OutputType);
409 static bool classof(const Action *A) {
410 return A->getKind() == PrecompileJobClass ||
411 A->getKind() == HeaderModulePrecompileJobClass;
415 class HeaderModulePrecompileJobAction : public PrecompileJobAction {
416 void anchor() override;
418 const char *ModuleName;
421 HeaderModulePrecompileJobAction(Action *Input, types::ID OutputType,
422 const char *ModuleName);
424 static bool classof(const Action *A) {
425 return A->getKind() == HeaderModulePrecompileJobClass;
428 void addModuleHeaderInput(Action *Input) {
429 getInputs().push_back(Input);
432 const char *getModuleName() const { return ModuleName; }
435 class AnalyzeJobAction : public JobAction {
436 void anchor() override;
439 AnalyzeJobAction(Action *Input, types::ID OutputType);
441 static bool classof(const Action *A) {
442 return A->getKind() == AnalyzeJobClass;
446 class MigrateJobAction : public JobAction {
447 void anchor() override;
450 MigrateJobAction(Action *Input, types::ID OutputType);
452 static bool classof(const Action *A) {
453 return A->getKind() == MigrateJobClass;
457 class CompileJobAction : public JobAction {
458 void anchor() override;
461 CompileJobAction(Action *Input, types::ID OutputType);
463 static bool classof(const Action *A) {
464 return A->getKind() == CompileJobClass;
468 class BackendJobAction : public JobAction {
469 void anchor() override;
472 BackendJobAction(Action *Input, types::ID OutputType);
474 static bool classof(const Action *A) {
475 return A->getKind() == BackendJobClass;
479 class AssembleJobAction : public JobAction {
480 void anchor() override;
483 AssembleJobAction(Action *Input, types::ID OutputType);
485 static bool classof(const Action *A) {
486 return A->getKind() == AssembleJobClass;
490 class IfsMergeJobAction : public JobAction {
491 void anchor() override;
494 IfsMergeJobAction(ActionList &Inputs, types::ID Type);
496 static bool classof(const Action *A) {
497 return A->getKind() == IfsMergeJobClass;
501 class LinkJobAction : public JobAction {
502 void anchor() override;
505 LinkJobAction(ActionList &Inputs, types::ID Type);
507 static bool classof(const Action *A) {
508 return A->getKind() == LinkJobClass;
512 class LipoJobAction : public JobAction {
513 void anchor() override;
516 LipoJobAction(ActionList &Inputs, types::ID Type);
518 static bool classof(const Action *A) {
519 return A->getKind() == LipoJobClass;
523 class DsymutilJobAction : public JobAction {
524 void anchor() override;
527 DsymutilJobAction(ActionList &Inputs, types::ID Type);
529 static bool classof(const Action *A) {
530 return A->getKind() == DsymutilJobClass;
534 class VerifyJobAction : public JobAction {
535 void anchor() override;
538 VerifyJobAction(ActionClass Kind, Action *Input, types::ID Type);
540 static bool classof(const Action *A) {
541 return A->getKind() == VerifyDebugInfoJobClass ||
542 A->getKind() == VerifyPCHJobClass;
546 class VerifyDebugInfoJobAction : public VerifyJobAction {
547 void anchor() override;
550 VerifyDebugInfoJobAction(Action *Input, types::ID Type);
552 static bool classof(const Action *A) {
553 return A->getKind() == VerifyDebugInfoJobClass;
557 class VerifyPCHJobAction : public VerifyJobAction {
558 void anchor() override;
561 VerifyPCHJobAction(Action *Input, types::ID Type);
563 static bool classof(const Action *A) {
564 return A->getKind() == VerifyPCHJobClass;
568 class OffloadBundlingJobAction : public JobAction {
569 void anchor() override;
572 // Offloading bundling doesn't change the type of output.
573 OffloadBundlingJobAction(ActionList &Inputs);
575 static bool classof(const Action *A) {
576 return A->getKind() == OffloadBundlingJobClass;
580 class OffloadUnbundlingJobAction final : public JobAction {
581 void anchor() override;
584 /// Type that provides information about the actions that depend on this
585 /// unbundling action.
586 struct DependentActionInfo final {
587 /// The tool chain of the dependent action.
588 const ToolChain *DependentToolChain = nullptr;
590 /// The bound architecture of the dependent action.
591 StringRef DependentBoundArch;
593 /// The offload kind of the dependent action.
594 const OffloadKind DependentOffloadKind = OFK_None;
596 DependentActionInfo(const ToolChain *DependentToolChain,
597 StringRef DependentBoundArch,
598 const OffloadKind DependentOffloadKind)
599 : DependentToolChain(DependentToolChain),
600 DependentBoundArch(DependentBoundArch),
601 DependentOffloadKind(DependentOffloadKind) {}
605 /// Container that keeps information about each dependence of this unbundling
607 SmallVector<DependentActionInfo, 6> DependentActionInfoArray;
610 // Offloading unbundling doesn't change the type of output.
611 OffloadUnbundlingJobAction(Action *Input);
613 /// Register information about a dependent action.
614 void registerDependentActionInfo(const ToolChain *TC, StringRef BoundArch,
616 DependentActionInfoArray.push_back({TC, BoundArch, Kind});
619 /// Return the information about all depending actions.
620 ArrayRef<DependentActionInfo> getDependentActionsInfo() const {
621 return DependentActionInfoArray;
624 static bool classof(const Action *A) {
625 return A->getKind() == OffloadUnbundlingJobClass;
629 class OffloadWrapperJobAction : public JobAction {
630 void anchor() override;
633 OffloadWrapperJobAction(ActionList &Inputs, types::ID Type);
635 static bool classof(const Action *A) {
636 return A->getKind() == OffloadWrapperJobClass;
640 } // namespace driver
643 #endif // LLVM_CLANG_DRIVER_ACTION_H