1 //===- Action.h - Abstract compilation steps --------------------*- C++ -*-===//
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 #ifndef LLVM_CLANG_DRIVER_ACTION_H
11 #define LLVM_CLANG_DRIVER_ACTION_H
13 #include "clang/Basic/LLVM.h"
14 #include "clang/Driver/Types.h"
15 #include "clang/Driver/Util.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/iterator_range.h"
36 /// Action - Represent an abstract compilation step to perform.
38 /// An action represents an edge in the compilation graph; typically
39 /// it is a job to transform an input using some tool.
41 /// The current driver is hard wired to expect actions which produce a
42 /// single primary output, at least in terms of controlling the
43 /// compilation. Actions can produce auxiliary files, but can only
44 /// produce a single output to feed into subsequent actions.
46 /// Actions are usually owned by a Compilation, which creates new
47 /// actions via MakeAction().
50 using size_type = ActionList::size_type;
51 using input_iterator = ActionList::iterator;
52 using input_const_iterator = ActionList::const_iterator;
53 using input_range = llvm::iterator_range<input_iterator>;
54 using input_const_range = llvm::iterator_range<input_const_iterator>;
70 VerifyDebugInfoJobClass,
72 OffloadBundlingJobClass,
73 OffloadUnbundlingJobClass,
75 JobClassFirst = PreprocessJobClass,
76 JobClassLast = OffloadUnbundlingJobClass
79 // The offloading kind determines if this action is binded to a particular
80 // programming model. Each entry reserves one bit. We also have a special kind
81 // to designate the host offloading tool chain.
85 // The host offloading tool chain.
88 // The device offloading tool chains - one bit for each programming model.
94 static const char *getClassName(ActionClass AC);
99 /// The output type of this action.
104 /// Flag that is set to true if this action can be collapsed with others
105 /// actions that depend on it. This is true by default and set to false when
106 /// the action is used by two different tool chains, which is enabled by the
107 /// offloading support implementation.
108 bool CanBeCollapsedWithNextDependentAction = true;
112 /// Offload information.
115 /// The host offloading kind - a combination of kinds encoded in a mask.
116 /// Multiple programming models may be supported simultaneously by the same
118 unsigned ActiveOffloadKindMask = 0u;
120 /// Offloading kind of the device.
121 OffloadKind OffloadingDeviceKind = OFK_None;
123 /// The Offloading architecture associated with this action.
124 const char *OffloadingArch = nullptr;
126 Action(ActionClass Kind, types::ID Type) : Action(Kind, ActionList(), Type) {}
127 Action(ActionClass Kind, Action *Input, types::ID Type)
128 : Action(Kind, ActionList({Input}), Type) {}
129 Action(ActionClass Kind, Action *Input)
130 : Action(Kind, ActionList({Input}), Input->getType()) {}
131 Action(ActionClass Kind, const ActionList &Inputs, types::ID Type)
132 : Kind(Kind), Type(Type), Inputs(Inputs) {}
137 const char *getClassName() const { return Action::getClassName(getKind()); }
139 ActionClass getKind() const { return Kind; }
140 types::ID getType() const { return Type; }
142 ActionList &getInputs() { return Inputs; }
143 const ActionList &getInputs() const { return Inputs; }
145 size_type size() const { return Inputs.size(); }
147 input_iterator input_begin() { return Inputs.begin(); }
148 input_iterator input_end() { return Inputs.end(); }
149 input_range inputs() { return input_range(input_begin(), input_end()); }
150 input_const_iterator input_begin() const { return Inputs.begin(); }
151 input_const_iterator input_end() const { return Inputs.end(); }
152 input_const_range inputs() const {
153 return input_const_range(input_begin(), input_end());
156 /// Mark this action as not legal to collapse.
157 void setCannotBeCollapsedWithNextDependentAction() {
158 CanBeCollapsedWithNextDependentAction = false;
161 /// Return true if this function can be collapsed with others.
162 bool isCollapsingWithNextDependentActionLegal() const {
163 return CanBeCollapsedWithNextDependentAction;
166 /// Return a string containing the offload kind of the action.
167 std::string getOffloadingKindPrefix() const;
169 /// Return a string that can be used as prefix in order to generate unique
170 /// files for each offloading kind. By default, no prefix is used for
171 /// non-device kinds, except if \a CreatePrefixForHost is set.
173 GetOffloadingFileNamePrefix(OffloadKind Kind,
174 StringRef NormalizedTriple,
175 bool CreatePrefixForHost = false);
177 /// Return a string containing a offload kind name.
178 static StringRef GetOffloadKindName(OffloadKind Kind);
180 /// Set the device offload info of this action and propagate it to its
182 void propagateDeviceOffloadInfo(OffloadKind OKind, const char *OArch);
184 /// Append the host offload info of this action and propagate it to its
186 void propagateHostOffloadInfo(unsigned OKinds, const char *OArch);
188 /// Set the offload info of this action to be the same as the provided action,
189 /// and propagate it to its dependences.
190 void propagateOffloadInfo(const Action *A);
192 unsigned getOffloadingHostActiveKinds() const {
193 return ActiveOffloadKindMask;
196 OffloadKind getOffloadingDeviceKind() const { return OffloadingDeviceKind; }
197 const char *getOffloadingArch() const { return OffloadingArch; }
199 /// Check if this action have any offload kinds. Note that host offload kinds
200 /// are only set if the action is a dependence to a host offload action.
201 bool isHostOffloading(OffloadKind OKind) const {
202 return ActiveOffloadKindMask & OKind;
204 bool isDeviceOffloading(OffloadKind OKind) const {
205 return OffloadingDeviceKind == OKind;
207 bool isOffloading(OffloadKind OKind) const {
208 return isHostOffloading(OKind) || isDeviceOffloading(OKind);
212 class InputAction : public Action {
213 const llvm::opt::Arg &Input;
215 virtual void anchor();
218 InputAction(const llvm::opt::Arg &Input, types::ID Type);
220 const llvm::opt::Arg &getInputArg() const { return Input; }
222 static bool classof(const Action *A) {
223 return A->getKind() == InputClass;
227 class BindArchAction : public Action {
228 virtual void anchor();
230 /// The architecture to bind, or 0 if the default architecture
235 BindArchAction(Action *Input, StringRef ArchName);
237 StringRef getArchName() const { return ArchName; }
239 static bool classof(const Action *A) {
240 return A->getKind() == BindArchClass;
244 /// An offload action combines host or/and device actions according to the
245 /// programming model implementation needs and propagates the offloading kind to
247 class OffloadAction final : public Action {
248 virtual void anchor();
251 /// Type used to communicate device actions. It associates bound architecture,
252 /// toolchain, and offload kind to each action.
253 class DeviceDependences final {
255 using ToolChainList = SmallVector<const ToolChain *, 3>;
256 using BoundArchList = SmallVector<const char *, 3>;
257 using OffloadKindList = SmallVector<OffloadKind, 3>;
260 // Lists that keep the information for each dependency. All the lists are
261 // meant to be updated in sync. We are adopting separate lists instead of a
262 // list of structs, because that simplifies forwarding the actions list to
263 // initialize the inputs of the base Action class.
265 /// The dependence actions.
266 ActionList DeviceActions;
268 /// The offloading toolchains that should be used with the action.
269 ToolChainList DeviceToolChains;
271 /// The architectures that should be used with this action.
272 BoundArchList DeviceBoundArchs;
274 /// The offload kind of each dependence.
275 OffloadKindList DeviceOffloadKinds;
278 /// Add a action along with the associated toolchain, bound arch, and
280 void add(Action &A, const ToolChain &TC, const char *BoundArch,
283 /// Get each of the individual arrays.
284 const ActionList &getActions() const { return DeviceActions; }
285 const ToolChainList &getToolChains() const { return DeviceToolChains; }
286 const BoundArchList &getBoundArchs() const { return DeviceBoundArchs; }
287 const OffloadKindList &getOffloadKinds() const {
288 return DeviceOffloadKinds;
292 /// Type used to communicate host actions. It associates bound architecture,
293 /// toolchain, and offload kinds to the host action.
294 class HostDependence final {
295 /// The dependence action.
298 /// The offloading toolchain that should be used with the action.
299 const ToolChain &HostToolChain;
301 /// The architectures that should be used with this action.
302 const char *HostBoundArch = nullptr;
304 /// The offload kind of each dependence.
305 unsigned HostOffloadKinds = 0u;
308 HostDependence(Action &A, const ToolChain &TC, const char *BoundArch,
309 const unsigned OffloadKinds)
310 : HostAction(A), HostToolChain(TC), HostBoundArch(BoundArch),
311 HostOffloadKinds(OffloadKinds) {}
313 /// Constructor version that obtains the offload kinds from the device
315 HostDependence(Action &A, const ToolChain &TC, const char *BoundArch,
316 const DeviceDependences &DDeps);
317 Action *getAction() const { return &HostAction; }
318 const ToolChain *getToolChain() const { return &HostToolChain; }
319 const char *getBoundArch() const { return HostBoundArch; }
320 unsigned getOffloadKinds() const { return HostOffloadKinds; }
323 using OffloadActionWorkTy =
324 llvm::function_ref<void(Action *, const ToolChain *, const char *)>;
327 /// The host offloading toolchain that should be used with the action.
328 const ToolChain *HostTC = nullptr;
330 /// The tool chains associated with the list of actions.
331 DeviceDependences::ToolChainList DevToolChains;
334 OffloadAction(const HostDependence &HDep);
335 OffloadAction(const DeviceDependences &DDeps, types::ID Ty);
336 OffloadAction(const HostDependence &HDep, const DeviceDependences &DDeps);
338 /// Execute the work specified in \a Work on the host dependence.
339 void doOnHostDependence(const OffloadActionWorkTy &Work) const;
341 /// Execute the work specified in \a Work on each device dependence.
342 void doOnEachDeviceDependence(const OffloadActionWorkTy &Work) const;
344 /// Execute the work specified in \a Work on each dependence.
345 void doOnEachDependence(const OffloadActionWorkTy &Work) const;
347 /// Execute the work specified in \a Work on each host or device dependence if
348 /// \a IsHostDependenceto is true or false, respectively.
349 void doOnEachDependence(bool IsHostDependence,
350 const OffloadActionWorkTy &Work) const;
352 /// Return true if the action has a host dependence.
353 bool hasHostDependence() const;
355 /// Return the host dependence of this action. This function is only expected
356 /// to be called if the host dependence exists.
357 Action *getHostDependence() const;
359 /// Return true if the action has a single device dependence. If \a
360 /// DoNotConsiderHostActions is set, ignore the host dependence, if any, while
361 /// accounting for the number of dependences.
362 bool hasSingleDeviceDependence(bool DoNotConsiderHostActions = false) const;
364 /// Return the single device dependence of this action. This function is only
365 /// expected to be called if a single device dependence exists. If \a
366 /// DoNotConsiderHostActions is set, a host dependence is allowed.
368 getSingleDeviceDependence(bool DoNotConsiderHostActions = false) const;
370 static bool classof(const Action *A) { return A->getKind() == OffloadClass; }
373 class JobAction : public Action {
374 virtual void anchor();
377 JobAction(ActionClass Kind, Action *Input, types::ID Type);
378 JobAction(ActionClass Kind, const ActionList &Inputs, types::ID Type);
381 static bool classof(const Action *A) {
382 return (A->getKind() >= JobClassFirst &&
383 A->getKind() <= JobClassLast);
387 class PreprocessJobAction : public JobAction {
388 void anchor() override;
391 PreprocessJobAction(Action *Input, types::ID OutputType);
393 static bool classof(const Action *A) {
394 return A->getKind() == PreprocessJobClass;
398 class PrecompileJobAction : public JobAction {
399 void anchor() override;
402 PrecompileJobAction(Action *Input, types::ID OutputType);
404 static bool classof(const Action *A) {
405 return A->getKind() == PrecompileJobClass;
409 class AnalyzeJobAction : public JobAction {
410 void anchor() override;
413 AnalyzeJobAction(Action *Input, types::ID OutputType);
415 static bool classof(const Action *A) {
416 return A->getKind() == AnalyzeJobClass;
420 class MigrateJobAction : public JobAction {
421 void anchor() override;
424 MigrateJobAction(Action *Input, types::ID OutputType);
426 static bool classof(const Action *A) {
427 return A->getKind() == MigrateJobClass;
431 class CompileJobAction : public JobAction {
432 void anchor() override;
435 CompileJobAction(Action *Input, types::ID OutputType);
437 static bool classof(const Action *A) {
438 return A->getKind() == CompileJobClass;
442 class BackendJobAction : public JobAction {
443 void anchor() override;
446 BackendJobAction(Action *Input, types::ID OutputType);
448 static bool classof(const Action *A) {
449 return A->getKind() == BackendJobClass;
453 class AssembleJobAction : public JobAction {
454 void anchor() override;
457 AssembleJobAction(Action *Input, types::ID OutputType);
459 static bool classof(const Action *A) {
460 return A->getKind() == AssembleJobClass;
464 class LinkJobAction : public JobAction {
465 void anchor() override;
468 LinkJobAction(ActionList &Inputs, types::ID Type);
470 static bool classof(const Action *A) {
471 return A->getKind() == LinkJobClass;
475 class LipoJobAction : public JobAction {
476 void anchor() override;
479 LipoJobAction(ActionList &Inputs, types::ID Type);
481 static bool classof(const Action *A) {
482 return A->getKind() == LipoJobClass;
486 class DsymutilJobAction : public JobAction {
487 void anchor() override;
490 DsymutilJobAction(ActionList &Inputs, types::ID Type);
492 static bool classof(const Action *A) {
493 return A->getKind() == DsymutilJobClass;
497 class VerifyJobAction : public JobAction {
498 void anchor() override;
501 VerifyJobAction(ActionClass Kind, Action *Input, types::ID Type);
503 static bool classof(const Action *A) {
504 return A->getKind() == VerifyDebugInfoJobClass ||
505 A->getKind() == VerifyPCHJobClass;
509 class VerifyDebugInfoJobAction : public VerifyJobAction {
510 void anchor() override;
513 VerifyDebugInfoJobAction(Action *Input, types::ID Type);
515 static bool classof(const Action *A) {
516 return A->getKind() == VerifyDebugInfoJobClass;
520 class VerifyPCHJobAction : public VerifyJobAction {
521 void anchor() override;
524 VerifyPCHJobAction(Action *Input, types::ID Type);
526 static bool classof(const Action *A) {
527 return A->getKind() == VerifyPCHJobClass;
531 class OffloadBundlingJobAction : public JobAction {
532 void anchor() override;
535 // Offloading bundling doesn't change the type of output.
536 OffloadBundlingJobAction(ActionList &Inputs);
538 static bool classof(const Action *A) {
539 return A->getKind() == OffloadBundlingJobClass;
543 class OffloadUnbundlingJobAction final : public JobAction {
544 void anchor() override;
547 /// Type that provides information about the actions that depend on this
548 /// unbundling action.
549 struct DependentActionInfo final {
550 /// The tool chain of the dependent action.
551 const ToolChain *DependentToolChain = nullptr;
553 /// The bound architecture of the dependent action.
554 StringRef DependentBoundArch;
556 /// The offload kind of the dependent action.
557 const OffloadKind DependentOffloadKind = OFK_None;
559 DependentActionInfo(const ToolChain *DependentToolChain,
560 StringRef DependentBoundArch,
561 const OffloadKind DependentOffloadKind)
562 : DependentToolChain(DependentToolChain),
563 DependentBoundArch(DependentBoundArch),
564 DependentOffloadKind(DependentOffloadKind) {}
568 /// Container that keeps information about each dependence of this unbundling
570 SmallVector<DependentActionInfo, 6> DependentActionInfoArray;
573 // Offloading unbundling doesn't change the type of output.
574 OffloadUnbundlingJobAction(Action *Input);
576 /// Register information about a dependent action.
577 void registerDependentActionInfo(const ToolChain *TC, StringRef BoundArch,
579 DependentActionInfoArray.push_back({TC, BoundArch, Kind});
582 /// Return the information about all depending actions.
583 ArrayRef<DependentActionInfo> getDependentActionsInfo() const {
584 return DependentActionInfoArray;
587 static bool classof(const Action *A) {
588 return A->getKind() == OffloadUnbundlingJobClass;
592 } // namespace driver
595 #endif // LLVM_CLANG_DRIVER_ACTION_H