clang  7.0.0svn
Action.h
Go to the documentation of this file.
1 //===- Action.h - Abstract compilation steps --------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_CLANG_DRIVER_ACTION_H
11 #define LLVM_CLANG_DRIVER_ACTION_H
12 
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"
21 #include <string>
22 
23 namespace llvm {
24 namespace opt {
25 
26 class Arg;
27 
28 } // namespace opt
29 } // namespace llvm
30 
31 namespace clang {
32 namespace driver {
33 
34 class ToolChain;
35 
36 /// Action - Represent an abstract compilation step to perform.
37 ///
38 /// An action represents an edge in the compilation graph; typically
39 /// it is a job to transform an input using some tool.
40 ///
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.
45 ///
46 /// Actions are usually owned by a Compilation, which creates new
47 /// actions via MakeAction().
48 class Action {
49 public:
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>;
55 
56  enum ActionClass {
57  InputClass = 0,
74 
75  JobClassFirst = PreprocessJobClass,
76  JobClassLast = OffloadUnbundlingJobClass
77  };
78 
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.
82  enum OffloadKind {
83  OFK_None = 0x00,
84 
85  // The host offloading tool chain.
86  OFK_Host = 0x01,
87 
88  // The device offloading tool chains - one bit for each programming model.
89  OFK_Cuda = 0x02,
90  OFK_OpenMP = 0x04,
91  };
92 
93  static const char *getClassName(ActionClass AC);
94 
95 private:
97 
98  /// The output type of this action.
100 
101  ActionList Inputs;
102 
103  /// Flag that is set to true if this action can be collapsed with others
104  /// actions that depend on it. This is true by default and set to false when
105  /// the action is used by two different tool chains, which is enabled by the
106  /// offloading support implementation.
107  bool CanBeCollapsedWithNextDependentAction = true;
108 
109 protected:
110  ///
111  /// Offload information.
112  ///
113 
114  /// The host offloading kind - a combination of kinds encoded in a mask.
115  /// Multiple programming models may be supported simultaneously by the same
116  /// host.
117  unsigned ActiveOffloadKindMask = 0u;
118 
119  /// Offloading kind of the device.
120  OffloadKind OffloadingDeviceKind = OFK_None;
121 
122  /// The Offloading architecture associated with this action.
123  const char *OffloadingArch = nullptr;
124 
125  Action(ActionClass Kind, types::ID Type) : Action(Kind, ActionList(), Type) {}
126  Action(ActionClass Kind, Action *Input, types::ID Type)
127  : Action(Kind, ActionList({Input}), Type) {}
128  Action(ActionClass Kind, Action *Input)
129  : Action(Kind, ActionList({Input}), Input->getType()) {}
130  Action(ActionClass Kind, const ActionList &Inputs, types::ID Type)
131  : Kind(Kind), Type(Type), Inputs(Inputs) {}
132 
133 public:
134  virtual ~Action();
135 
136  const char *getClassName() const { return Action::getClassName(getKind()); }
137 
138  ActionClass getKind() const { return Kind; }
139  types::ID getType() const { return Type; }
140 
141  ActionList &getInputs() { return Inputs; }
142  const ActionList &getInputs() const { return Inputs; }
143 
144  size_type size() const { return Inputs.size(); }
145 
146  input_iterator input_begin() { return Inputs.begin(); }
147  input_iterator input_end() { return Inputs.end(); }
148  input_range inputs() { return input_range(input_begin(), input_end()); }
149  input_const_iterator input_begin() const { return Inputs.begin(); }
150  input_const_iterator input_end() const { return Inputs.end(); }
152  return input_const_range(input_begin(), input_end());
153  }
154 
155  /// Mark this action as not legal to collapse.
157  CanBeCollapsedWithNextDependentAction = false;
158  }
159 
160  /// Return true if this function can be collapsed with others.
162  return CanBeCollapsedWithNextDependentAction;
163  }
164 
165  /// Return a string containing the offload kind of the action.
166  std::string getOffloadingKindPrefix() const;
167 
168  /// Return a string that can be used as prefix in order to generate unique
169  /// files for each offloading kind. By default, no prefix is used for
170  /// non-device kinds, except if \a CreatePrefixForHost is set.
171  static std::string
172  GetOffloadingFileNamePrefix(OffloadKind Kind,
173  StringRef NormalizedTriple,
174  bool CreatePrefixForHost = false);
175 
176  /// Return a string containing a offload kind name.
177  static StringRef GetOffloadKindName(OffloadKind Kind);
178 
179  /// Set the device offload info of this action and propagate it to its
180  /// dependences.
181  void propagateDeviceOffloadInfo(OffloadKind OKind, const char *OArch);
182 
183  /// Append the host offload info of this action and propagate it to its
184  /// dependences.
185  void propagateHostOffloadInfo(unsigned OKinds, const char *OArch);
186 
187  /// Set the offload info of this action to be the same as the provided action,
188  /// and propagate it to its dependences.
189  void propagateOffloadInfo(const Action *A);
190 
191  unsigned getOffloadingHostActiveKinds() const {
192  return ActiveOffloadKindMask;
193  }
194 
195  OffloadKind getOffloadingDeviceKind() const { return OffloadingDeviceKind; }
196  const char *getOffloadingArch() const { return OffloadingArch; }
197 
198  /// Check if this action have any offload kinds. Note that host offload kinds
199  /// are only set if the action is a dependence to a host offload action.
200  bool isHostOffloading(OffloadKind OKind) const {
201  return ActiveOffloadKindMask & OKind;
202  }
203  bool isDeviceOffloading(OffloadKind OKind) const {
204  return OffloadingDeviceKind == OKind;
205  }
206  bool isOffloading(OffloadKind OKind) const {
207  return isHostOffloading(OKind) || isDeviceOffloading(OKind);
208  }
209 };
210 
211 class InputAction : public Action {
212  const llvm::opt::Arg &Input;
213 
214  virtual void anchor();
215 
216 public:
217  InputAction(const llvm::opt::Arg &Input, types::ID Type);
218 
219  const llvm::opt::Arg &getInputArg() const { return Input; }
220 
221  static bool classof(const Action *A) {
222  return A->getKind() == InputClass;
223  }
224 };
225 
226 class BindArchAction : public Action {
227  virtual void anchor();
228 
229  /// The architecture to bind, or 0 if the default architecture
230  /// should be bound.
231  StringRef ArchName;
232 
233 public:
234  BindArchAction(Action *Input, StringRef ArchName);
235 
236  StringRef getArchName() const { return ArchName; }
237 
238  static bool classof(const Action *A) {
239  return A->getKind() == BindArchClass;
240  }
241 };
242 
243 /// An offload action combines host or/and device actions according to the
244 /// programming model implementation needs and propagates the offloading kind to
245 /// its dependences.
246 class OffloadAction final : public Action {
247  virtual void anchor();
248 
249 public:
250  /// Type used to communicate device actions. It associates bound architecture,
251  /// toolchain, and offload kind to each action.
252  class DeviceDependences final {
253  public:
257 
258  private:
259  // Lists that keep the information for each dependency. All the lists are
260  // meant to be updated in sync. We are adopting separate lists instead of a
261  // list of structs, because that simplifies forwarding the actions list to
262  // initialize the inputs of the base Action class.
263 
264  /// The dependence actions.
265  ActionList DeviceActions;
266 
267  /// The offloading toolchains that should be used with the action.
268  ToolChainList DeviceToolChains;
269 
270  /// The architectures that should be used with this action.
271  BoundArchList DeviceBoundArchs;
272 
273  /// The offload kind of each dependence.
274  OffloadKindList DeviceOffloadKinds;
275 
276  public:
277  /// Add a action along with the associated toolchain, bound arch, and
278  /// offload kind.
279  void add(Action &A, const ToolChain &TC, const char *BoundArch,
280  OffloadKind OKind);
281 
282  /// Get each of the individual arrays.
283  const ActionList &getActions() const { return DeviceActions; }
284  const ToolChainList &getToolChains() const { return DeviceToolChains; }
285  const BoundArchList &getBoundArchs() const { return DeviceBoundArchs; }
287  return DeviceOffloadKinds;
288  }
289  };
290 
291  /// Type used to communicate host actions. It associates bound architecture,
292  /// toolchain, and offload kinds to the host action.
293  class HostDependence final {
294  /// The dependence action.
295  Action &HostAction;
296 
297  /// The offloading toolchain that should be used with the action.
298  const ToolChain &HostToolChain;
299 
300  /// The architectures that should be used with this action.
301  const char *HostBoundArch = nullptr;
302 
303  /// The offload kind of each dependence.
304  unsigned HostOffloadKinds = 0u;
305 
306  public:
307  HostDependence(Action &A, const ToolChain &TC, const char *BoundArch,
308  const unsigned OffloadKinds)
309  : HostAction(A), HostToolChain(TC), HostBoundArch(BoundArch),
310  HostOffloadKinds(OffloadKinds) {}
311 
312  /// Constructor version that obtains the offload kinds from the device
313  /// dependencies.
314  HostDependence(Action &A, const ToolChain &TC, const char *BoundArch,
315  const DeviceDependences &DDeps);
316  Action *getAction() const { return &HostAction; }
317  const ToolChain *getToolChain() const { return &HostToolChain; }
318  const char *getBoundArch() const { return HostBoundArch; }
319  unsigned getOffloadKinds() const { return HostOffloadKinds; }
320  };
321 
322  using OffloadActionWorkTy =
323  llvm::function_ref<void(Action *, const ToolChain *, const char *)>;
324 
325 private:
326  /// The host offloading toolchain that should be used with the action.
327  const ToolChain *HostTC = nullptr;
328 
329  /// The tool chains associated with the list of actions.
330  DeviceDependences::ToolChainList DevToolChains;
331 
332 public:
333  OffloadAction(const HostDependence &HDep);
334  OffloadAction(const DeviceDependences &DDeps, types::ID Ty);
335  OffloadAction(const HostDependence &HDep, const DeviceDependences &DDeps);
336 
337  /// Execute the work specified in \a Work on the host dependence.
338  void doOnHostDependence(const OffloadActionWorkTy &Work) const;
339 
340  /// Execute the work specified in \a Work on each device dependence.
341  void doOnEachDeviceDependence(const OffloadActionWorkTy &Work) const;
342 
343  /// Execute the work specified in \a Work on each dependence.
344  void doOnEachDependence(const OffloadActionWorkTy &Work) const;
345 
346  /// Execute the work specified in \a Work on each host or device dependence if
347  /// \a IsHostDependenceto is true or false, respectively.
348  void doOnEachDependence(bool IsHostDependence,
349  const OffloadActionWorkTy &Work) const;
350 
351  /// Return true if the action has a host dependence.
352  bool hasHostDependence() const;
353 
354  /// Return the host dependence of this action. This function is only expected
355  /// to be called if the host dependence exists.
356  Action *getHostDependence() const;
357 
358  /// Return true if the action has a single device dependence. If \a
359  /// DoNotConsiderHostActions is set, ignore the host dependence, if any, while
360  /// accounting for the number of dependences.
361  bool hasSingleDeviceDependence(bool DoNotConsiderHostActions = false) const;
362 
363  /// Return the single device dependence of this action. This function is only
364  /// expected to be called if a single device dependence exists. If \a
365  /// DoNotConsiderHostActions is set, a host dependence is allowed.
366  Action *
367  getSingleDeviceDependence(bool DoNotConsiderHostActions = false) const;
368 
369  static bool classof(const Action *A) { return A->getKind() == OffloadClass; }
370 };
371 
372 class JobAction : public Action {
373  virtual void anchor();
374 
375 protected:
377  JobAction(ActionClass Kind, const ActionList &Inputs, types::ID Type);
378 
379 public:
380  static bool classof(const Action *A) {
381  return (A->getKind() >= JobClassFirst &&
382  A->getKind() <= JobClassLast);
383  }
384 };
385 
387  void anchor() override;
388 
389 public:
390  PreprocessJobAction(Action *Input, types::ID OutputType);
391 
392  static bool classof(const Action *A) {
393  return A->getKind() == PreprocessJobClass;
394  }
395 };
396 
398  void anchor() override;
399 
400 public:
401  PrecompileJobAction(Action *Input, types::ID OutputType);
402 
403  static bool classof(const Action *A) {
404  return A->getKind() == PrecompileJobClass;
405  }
406 };
407 
408 class AnalyzeJobAction : public JobAction {
409  void anchor() override;
410 
411 public:
412  AnalyzeJobAction(Action *Input, types::ID OutputType);
413 
414  static bool classof(const Action *A) {
415  return A->getKind() == AnalyzeJobClass;
416  }
417 };
418 
419 class MigrateJobAction : public JobAction {
420  void anchor() override;
421 
422 public:
423  MigrateJobAction(Action *Input, types::ID OutputType);
424 
425  static bool classof(const Action *A) {
426  return A->getKind() == MigrateJobClass;
427  }
428 };
429 
430 class CompileJobAction : public JobAction {
431  void anchor() override;
432 
433 public:
434  CompileJobAction(Action *Input, types::ID OutputType);
435 
436  static bool classof(const Action *A) {
437  return A->getKind() == CompileJobClass;
438  }
439 };
440 
441 class BackendJobAction : public JobAction {
442  void anchor() override;
443 
444 public:
445  BackendJobAction(Action *Input, types::ID OutputType);
446 
447  static bool classof(const Action *A) {
448  return A->getKind() == BackendJobClass;
449  }
450 };
451 
452 class AssembleJobAction : public JobAction {
453  void anchor() override;
454 
455 public:
456  AssembleJobAction(Action *Input, types::ID OutputType);
457 
458  static bool classof(const Action *A) {
459  return A->getKind() == AssembleJobClass;
460  }
461 };
462 
463 class LinkJobAction : public JobAction {
464  void anchor() override;
465 
466 public:
468 
469  static bool classof(const Action *A) {
470  return A->getKind() == LinkJobClass;
471  }
472 };
473 
474 class LipoJobAction : public JobAction {
475  void anchor() override;
476 
477 public:
479 
480  static bool classof(const Action *A) {
481  return A->getKind() == LipoJobClass;
482  }
483 };
484 
485 class DsymutilJobAction : public JobAction {
486  void anchor() override;
487 
488 public:
490 
491  static bool classof(const Action *A) {
492  return A->getKind() == DsymutilJobClass;
493  }
494 };
495 
496 class VerifyJobAction : public JobAction {
497  void anchor() override;
498 
499 public:
501 
502  static bool classof(const Action *A) {
503  return A->getKind() == VerifyDebugInfoJobClass ||
504  A->getKind() == VerifyPCHJobClass;
505  }
506 };
507 
509  void anchor() override;
510 
511 public:
513 
514  static bool classof(const Action *A) {
515  return A->getKind() == VerifyDebugInfoJobClass;
516  }
517 };
518 
520  void anchor() override;
521 
522 public:
524 
525  static bool classof(const Action *A) {
526  return A->getKind() == VerifyPCHJobClass;
527  }
528 };
529 
531  void anchor() override;
532 
533 public:
534  // Offloading bundling doesn't change the type of output.
536 
537  static bool classof(const Action *A) {
538  return A->getKind() == OffloadBundlingJobClass;
539  }
540 };
541 
542 class OffloadUnbundlingJobAction final : public JobAction {
543  void anchor() override;
544 
545 public:
546  /// Type that provides information about the actions that depend on this
547  /// unbundling action.
548  struct DependentActionInfo final {
549  /// \brief The tool chain of the dependent action.
550  const ToolChain *DependentToolChain = nullptr;
551 
552  /// \brief The bound architecture of the dependent action.
554 
555  /// \brief The offload kind of the dependent action.
556  const OffloadKind DependentOffloadKind = OFK_None;
557 
558  DependentActionInfo(const ToolChain *DependentToolChain,
559  StringRef DependentBoundArch,
560  const OffloadKind DependentOffloadKind)
561  : DependentToolChain(DependentToolChain),
562  DependentBoundArch(DependentBoundArch),
563  DependentOffloadKind(DependentOffloadKind) {}
564  };
565 
566 private:
567  /// Container that keeps information about each dependence of this unbundling
568  /// action.
569  SmallVector<DependentActionInfo, 6> DependentActionInfoArray;
570 
571 public:
572  // Offloading unbundling doesn't change the type of output.
574 
575  /// Register information about a dependent action.
576  void registerDependentActionInfo(const ToolChain *TC, StringRef BoundArch,
577  OffloadKind Kind) {
578  DependentActionInfoArray.push_back({TC, BoundArch, Kind});
579  }
580 
581  /// Return the information about all depending actions.
583  return DependentActionInfoArray;
584  }
585 
586  static bool classof(const Action *A) {
587  return A->getKind() == OffloadUnbundlingJobClass;
588  }
589 };
590 
591 } // namespace driver
592 } // namespace clang
593 
594 #endif // LLVM_CLANG_DRIVER_ACTION_H
Type that provides information about the actions that depend on this unbundling action.
Definition: Action.h:548
static bool classof(const Action *A)
Definition: Action.h:392
Action(ActionClass Kind, Action *Input, types::ID Type)
Definition: Action.h:126
bool isCollapsingWithNextDependentActionLegal() const
Return true if this function can be collapsed with others.
Definition: Action.h:161
const OffloadKindList & getOffloadKinds() const
Definition: Action.h:286
ActionList::size_type size_type
Definition: Action.h:50
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:30
static bool classof(const Action *A)
Definition: Action.h:221
DependentActionInfo(const ToolChain *DependentToolChain, StringRef DependentBoundArch, const OffloadKind DependentOffloadKind)
Definition: Action.h:558
The base class of the type hierarchy.
Definition: Type.h:1419
input_range inputs()
Definition: Action.h:148
static bool classof(const Action *A)
Definition: Action.h:436
bool isHostOffloading(OffloadKind OKind) const
Check if this action have any offload kinds.
Definition: Action.h:200
Action(ActionClass Kind, types::ID Type)
Definition: Action.h:125
Type used to communicate device actions.
Definition: Action.h:252
const char * getClassName() const
Definition: Action.h:136
void registerDependentActionInfo(const ToolChain *TC, StringRef BoundArch, OffloadKind Kind)
Register information about a dependent action.
Definition: Action.h:576
StringRef DependentBoundArch
The bound architecture of the dependent action.
Definition: Action.h:553
const llvm::opt::Arg & getInputArg() const
Definition: Action.h:219
llvm::iterator_range< input_const_iterator > input_const_range
Definition: Action.h:54
ActionList & getInputs()
Definition: Action.h:141
static bool classof(const Action *A)
Definition: Action.h:491
Type used to communicate host actions.
Definition: Action.h:293
const ToolChain * getToolChain() const
Definition: Action.h:317
bool isOffloading(OffloadKind OKind) const
Definition: Action.h:206
Action - Represent an abstract compilation step to perform.
Definition: Action.h:48
static bool classof(const Action *A)
Definition: Action.h:380
input_const_iterator input_begin() const
Definition: Action.h:149
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
types::ID getType() const
Definition: Action.h:139
static bool classof(const Action *A)
Definition: Action.h:469
bool isDeviceOffloading(OffloadKind OKind) const
Definition: Action.h:203
OffloadKind getOffloadingDeviceKind() const
Definition: Action.h:195
llvm::iterator_range< input_iterator > input_range
Definition: Action.h:53
static bool classof(const Action *A)
Definition: Action.h:514
input_iterator input_begin()
Definition: Action.h:146
input_iterator input_end()
Definition: Action.h:147
const ToolChainList & getToolChains() const
Definition: Action.h:284
static bool classof(const Action *A)
Definition: Action.h:586
static bool classof(const Action *A)
Definition: Action.h:403
ActionList::const_iterator input_const_iterator
Definition: Action.h:52
ActionClass getKind() const
Definition: Action.h:138
const ActionList & getInputs() const
Definition: Action.h:142
static bool classof(const Action *A)
Definition: Action.h:447
static bool classof(const Action *A)
Definition: Action.h:525
const BoundArchList & getBoundArchs() const
Definition: Action.h:285
input_const_iterator input_end() const
Definition: Action.h:150
size_type size() const
Definition: Action.h:144
static bool classof(const Action *A)
Definition: Action.h:369
void setCannotBeCollapsedWithNextDependentAction()
Mark this action as not legal to collapse.
Definition: Action.h:156
const ActionList & getActions() const
Get each of the individual arrays.
Definition: Action.h:283
HostDependence(Action &A, const ToolChain &TC, const char *BoundArch, const unsigned OffloadKinds)
Definition: Action.h:307
Action(ActionClass Kind, Action *Input)
Definition: Action.h:128
Kind
ArrayRef< DependentActionInfo > getDependentActionsInfo() const
Return the information about all depending actions.
Definition: Action.h:582
static bool classof(const Action *A)
Definition: Action.h:458
An offload action combines host or/and device actions according to the programming model implementati...
Definition: Action.h:246
input_const_range inputs() const
Definition: Action.h:151
static bool classof(const Action *A)
Definition: Action.h:480
unsigned getOffloadingHostActiveKinds() const
Definition: Action.h:191
Dataflow Directional Tag Classes.
llvm::function_ref< void(Action *, const ToolChain *, const char *)> OffloadActionWorkTy
Definition: Action.h:323
Action(ActionClass Kind, const ActionList &Inputs, types::ID Type)
Definition: Action.h:130
ActionList::iterator input_iterator
Definition: Action.h:51
static bool classof(const Action *A)
Definition: Action.h:414
static Decl::Kind getKind(const Decl *D)
Definition: DeclBase.cpp:928
static bool classof(const Action *A)
Definition: Action.h:425
static bool classof(const Action *A)
Definition: Action.h:238
static bool classof(const Action *A)
Definition: Action.h:502
const char * getOffloadingArch() const
Definition: Action.h:196
StringRef getArchName() const
Definition: Action.h:236
static bool classof(const Action *A)
Definition: Action.h:537
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:84