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  OFK_HIP = 0x08,
92  };
93 
94  static const char *getClassName(ActionClass AC);
95 
96 private:
98 
99  /// The output type of this action.
100  types::ID Type;
101 
102  ActionList Inputs;
103 
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;
109 
110 protected:
111  ///
112  /// Offload information.
113  ///
114 
115  /// The host offloading kind - a combination of kinds encoded in a mask.
116  /// Multiple programming models may be supported simultaneously by the same
117  /// host.
118  unsigned ActiveOffloadKindMask = 0u;
119 
120  /// Offloading kind of the device.
121  OffloadKind OffloadingDeviceKind = OFK_None;
122 
123  /// The Offloading architecture associated with this action.
124  const char *OffloadingArch = nullptr;
125 
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) {}
133 
134 public:
135  virtual ~Action();
136 
137  const char *getClassName() const { return Action::getClassName(getKind()); }
138 
139  ActionClass getKind() const { return Kind; }
140  types::ID getType() const { return Type; }
141 
142  ActionList &getInputs() { return Inputs; }
143  const ActionList &getInputs() const { return Inputs; }
144 
145  size_type size() const { return Inputs.size(); }
146 
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(); }
153  return input_const_range(input_begin(), input_end());
154  }
155 
156  /// Mark this action as not legal to collapse.
158  CanBeCollapsedWithNextDependentAction = false;
159  }
160 
161  /// Return true if this function can be collapsed with others.
163  return CanBeCollapsedWithNextDependentAction;
164  }
165 
166  /// Return a string containing the offload kind of the action.
167  std::string getOffloadingKindPrefix() const;
168 
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.
172  static std::string
173  GetOffloadingFileNamePrefix(OffloadKind Kind,
174  StringRef NormalizedTriple,
175  bool CreatePrefixForHost = false);
176 
177  /// Return a string containing a offload kind name.
178  static StringRef GetOffloadKindName(OffloadKind Kind);
179 
180  /// Set the device offload info of this action and propagate it to its
181  /// dependences.
182  void propagateDeviceOffloadInfo(OffloadKind OKind, const char *OArch);
183 
184  /// Append the host offload info of this action and propagate it to its
185  /// dependences.
186  void propagateHostOffloadInfo(unsigned OKinds, const char *OArch);
187 
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);
191 
192  unsigned getOffloadingHostActiveKinds() const {
193  return ActiveOffloadKindMask;
194  }
195 
196  OffloadKind getOffloadingDeviceKind() const { return OffloadingDeviceKind; }
197  const char *getOffloadingArch() const { return OffloadingArch; }
198 
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;
203  }
204  bool isDeviceOffloading(OffloadKind OKind) const {
205  return OffloadingDeviceKind == OKind;
206  }
207  bool isOffloading(OffloadKind OKind) const {
208  return isHostOffloading(OKind) || isDeviceOffloading(OKind);
209  }
210 };
211 
212 class InputAction : public Action {
213  const llvm::opt::Arg &Input;
214 
215  virtual void anchor();
216 
217 public:
218  InputAction(const llvm::opt::Arg &Input, types::ID Type);
219 
220  const llvm::opt::Arg &getInputArg() const { return Input; }
221 
222  static bool classof(const Action *A) {
223  return A->getKind() == InputClass;
224  }
225 };
226 
227 class BindArchAction : public Action {
228  virtual void anchor();
229 
230  /// The architecture to bind, or 0 if the default architecture
231  /// should be bound.
232  StringRef ArchName;
233 
234 public:
235  BindArchAction(Action *Input, StringRef ArchName);
236 
237  StringRef getArchName() const { return ArchName; }
238 
239  static bool classof(const Action *A) {
240  return A->getKind() == BindArchClass;
241  }
242 };
243 
244 /// An offload action combines host or/and device actions according to the
245 /// programming model implementation needs and propagates the offloading kind to
246 /// its dependences.
247 class OffloadAction final : public Action {
248  virtual void anchor();
249 
250 public:
251  /// Type used to communicate device actions. It associates bound architecture,
252  /// toolchain, and offload kind to each action.
253  class DeviceDependences final {
254  public:
258 
259  private:
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.
264 
265  /// The dependence actions.
266  ActionList DeviceActions;
267 
268  /// The offloading toolchains that should be used with the action.
269  ToolChainList DeviceToolChains;
270 
271  /// The architectures that should be used with this action.
272  BoundArchList DeviceBoundArchs;
273 
274  /// The offload kind of each dependence.
275  OffloadKindList DeviceOffloadKinds;
276 
277  public:
278  /// Add a action along with the associated toolchain, bound arch, and
279  /// offload kind.
280  void add(Action &A, const ToolChain &TC, const char *BoundArch,
281  OffloadKind OKind);
282 
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; }
288  return DeviceOffloadKinds;
289  }
290  };
291 
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.
296  Action &HostAction;
297 
298  /// The offloading toolchain that should be used with the action.
299  const ToolChain &HostToolChain;
300 
301  /// The architectures that should be used with this action.
302  const char *HostBoundArch = nullptr;
303 
304  /// The offload kind of each dependence.
305  unsigned HostOffloadKinds = 0u;
306 
307  public:
308  HostDependence(Action &A, const ToolChain &TC, const char *BoundArch,
309  const unsigned OffloadKinds)
310  : HostAction(A), HostToolChain(TC), HostBoundArch(BoundArch),
311  HostOffloadKinds(OffloadKinds) {}
312 
313  /// Constructor version that obtains the offload kinds from the device
314  /// dependencies.
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; }
321  };
322 
323  using OffloadActionWorkTy =
324  llvm::function_ref<void(Action *, const ToolChain *, const char *)>;
325 
326 private:
327  /// The host offloading toolchain that should be used with the action.
328  const ToolChain *HostTC = nullptr;
329 
330  /// The tool chains associated with the list of actions.
331  DeviceDependences::ToolChainList DevToolChains;
332 
333 public:
334  OffloadAction(const HostDependence &HDep);
335  OffloadAction(const DeviceDependences &DDeps, types::ID Ty);
336  OffloadAction(const HostDependence &HDep, const DeviceDependences &DDeps);
337 
338  /// Execute the work specified in \a Work on the host dependence.
339  void doOnHostDependence(const OffloadActionWorkTy &Work) const;
340 
341  /// Execute the work specified in \a Work on each device dependence.
342  void doOnEachDeviceDependence(const OffloadActionWorkTy &Work) const;
343 
344  /// Execute the work specified in \a Work on each dependence.
345  void doOnEachDependence(const OffloadActionWorkTy &Work) const;
346 
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;
351 
352  /// Return true if the action has a host dependence.
353  bool hasHostDependence() const;
354 
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;
358 
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;
363 
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.
367  Action *
368  getSingleDeviceDependence(bool DoNotConsiderHostActions = false) const;
369 
370  static bool classof(const Action *A) { return A->getKind() == OffloadClass; }
371 };
372 
373 class JobAction : public Action {
374  virtual void anchor();
375 
376 protected:
378  JobAction(ActionClass Kind, const ActionList &Inputs, types::ID Type);
379 
380 public:
381  static bool classof(const Action *A) {
382  return (A->getKind() >= JobClassFirst &&
383  A->getKind() <= JobClassLast);
384  }
385 };
386 
388  void anchor() override;
389 
390 public:
391  PreprocessJobAction(Action *Input, types::ID OutputType);
392 
393  static bool classof(const Action *A) {
394  return A->getKind() == PreprocessJobClass;
395  }
396 };
397 
399  void anchor() override;
400 
401 public:
402  PrecompileJobAction(Action *Input, types::ID OutputType);
403 
404  static bool classof(const Action *A) {
405  return A->getKind() == PrecompileJobClass;
406  }
407 };
408 
409 class AnalyzeJobAction : public JobAction {
410  void anchor() override;
411 
412 public:
413  AnalyzeJobAction(Action *Input, types::ID OutputType);
414 
415  static bool classof(const Action *A) {
416  return A->getKind() == AnalyzeJobClass;
417  }
418 };
419 
420 class MigrateJobAction : public JobAction {
421  void anchor() override;
422 
423 public:
424  MigrateJobAction(Action *Input, types::ID OutputType);
425 
426  static bool classof(const Action *A) {
427  return A->getKind() == MigrateJobClass;
428  }
429 };
430 
431 class CompileJobAction : public JobAction {
432  void anchor() override;
433 
434 public:
435  CompileJobAction(Action *Input, types::ID OutputType);
436 
437  static bool classof(const Action *A) {
438  return A->getKind() == CompileJobClass;
439  }
440 };
441 
442 class BackendJobAction : public JobAction {
443  void anchor() override;
444 
445 public:
446  BackendJobAction(Action *Input, types::ID OutputType);
447 
448  static bool classof(const Action *A) {
449  return A->getKind() == BackendJobClass;
450  }
451 };
452 
453 class AssembleJobAction : public JobAction {
454  void anchor() override;
455 
456 public:
457  AssembleJobAction(Action *Input, types::ID OutputType);
458 
459  static bool classof(const Action *A) {
460  return A->getKind() == AssembleJobClass;
461  }
462 };
463 
464 class LinkJobAction : public JobAction {
465  void anchor() override;
466 
467 public:
469 
470  static bool classof(const Action *A) {
471  return A->getKind() == LinkJobClass;
472  }
473 };
474 
475 class LipoJobAction : public JobAction {
476  void anchor() override;
477 
478 public:
480 
481  static bool classof(const Action *A) {
482  return A->getKind() == LipoJobClass;
483  }
484 };
485 
486 class DsymutilJobAction : public JobAction {
487  void anchor() override;
488 
489 public:
491 
492  static bool classof(const Action *A) {
493  return A->getKind() == DsymutilJobClass;
494  }
495 };
496 
497 class VerifyJobAction : public JobAction {
498  void anchor() override;
499 
500 public:
502 
503  static bool classof(const Action *A) {
504  return A->getKind() == VerifyDebugInfoJobClass ||
505  A->getKind() == VerifyPCHJobClass;
506  }
507 };
508 
510  void anchor() override;
511 
512 public:
514 
515  static bool classof(const Action *A) {
516  return A->getKind() == VerifyDebugInfoJobClass;
517  }
518 };
519 
521  void anchor() override;
522 
523 public:
525 
526  static bool classof(const Action *A) {
527  return A->getKind() == VerifyPCHJobClass;
528  }
529 };
530 
532  void anchor() override;
533 
534 public:
535  // Offloading bundling doesn't change the type of output.
537 
538  static bool classof(const Action *A) {
539  return A->getKind() == OffloadBundlingJobClass;
540  }
541 };
542 
543 class OffloadUnbundlingJobAction final : public JobAction {
544  void anchor() override;
545 
546 public:
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;
552 
553  /// The bound architecture of the dependent action.
555 
556  /// The offload kind of the dependent action.
557  const OffloadKind DependentOffloadKind = OFK_None;
558 
559  DependentActionInfo(const ToolChain *DependentToolChain,
560  StringRef DependentBoundArch,
561  const OffloadKind DependentOffloadKind)
562  : DependentToolChain(DependentToolChain),
563  DependentBoundArch(DependentBoundArch),
564  DependentOffloadKind(DependentOffloadKind) {}
565  };
566 
567 private:
568  /// Container that keeps information about each dependence of this unbundling
569  /// action.
570  SmallVector<DependentActionInfo, 6> DependentActionInfoArray;
571 
572 public:
573  // Offloading unbundling doesn't change the type of output.
575 
576  /// Register information about a dependent action.
577  void registerDependentActionInfo(const ToolChain *TC, StringRef BoundArch,
578  OffloadKind Kind) {
579  DependentActionInfoArray.push_back({TC, BoundArch, Kind});
580  }
581 
582  /// Return the information about all depending actions.
584  return DependentActionInfoArray;
585  }
586 
587  static bool classof(const Action *A) {
588  return A->getKind() == OffloadUnbundlingJobClass;
589  }
590 };
591 
592 } // namespace driver
593 } // namespace clang
594 
595 #endif // LLVM_CLANG_DRIVER_ACTION_H
Type that provides information about the actions that depend on this unbundling action.
Definition: Action.h:549
static bool classof(const Action *A)
Definition: Action.h:393
Action(ActionClass Kind, Action *Input, types::ID Type)
Definition: Action.h:127
bool isCollapsingWithNextDependentActionLegal() const
Return true if this function can be collapsed with others.
Definition: Action.h:162
const OffloadKindList & getOffloadKinds() const
Definition: Action.h:287
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:222
DependentActionInfo(const ToolChain *DependentToolChain, StringRef DependentBoundArch, const OffloadKind DependentOffloadKind)
Definition: Action.h:559
The base class of the type hierarchy.
Definition: Type.h:1428
input_range inputs()
Definition: Action.h:149
static bool classof(const Action *A)
Definition: Action.h:437
bool isHostOffloading(OffloadKind OKind) const
Check if this action have any offload kinds.
Definition: Action.h:201
Action(ActionClass Kind, types::ID Type)
Definition: Action.h:126
Type used to communicate device actions.
Definition: Action.h:253
const char * getClassName() const
Definition: Action.h:137
void registerDependentActionInfo(const ToolChain *TC, StringRef BoundArch, OffloadKind Kind)
Register information about a dependent action.
Definition: Action.h:577
StringRef DependentBoundArch
The bound architecture of the dependent action.
Definition: Action.h:554
const llvm::opt::Arg & getInputArg() const
Definition: Action.h:220
llvm::iterator_range< input_const_iterator > input_const_range
Definition: Action.h:54
ActionList & getInputs()
Definition: Action.h:142
static bool classof(const Action *A)
Definition: Action.h:492
Type used to communicate host actions.
Definition: Action.h:294
const ToolChain * getToolChain() const
Definition: Action.h:318
bool isOffloading(OffloadKind OKind) const
Definition: Action.h:207
Action - Represent an abstract compilation step to perform.
Definition: Action.h:48
static bool classof(const Action *A)
Definition: Action.h:381
input_const_iterator input_begin() const
Definition: Action.h:150
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
types::ID getType() const
Definition: Action.h:140
static bool classof(const Action *A)
Definition: Action.h:470
bool isDeviceOffloading(OffloadKind OKind) const
Definition: Action.h:204
OffloadKind getOffloadingDeviceKind() const
Definition: Action.h:196
llvm::iterator_range< input_iterator > input_range
Definition: Action.h:53
static bool classof(const Action *A)
Definition: Action.h:515
input_iterator input_begin()
Definition: Action.h:147
input_iterator input_end()
Definition: Action.h:148
const ToolChainList & getToolChains() const
Definition: Action.h:285
static bool classof(const Action *A)
Definition: Action.h:587
static bool classof(const Action *A)
Definition: Action.h:404
ActionList::const_iterator input_const_iterator
Definition: Action.h:52
ActionClass getKind() const
Definition: Action.h:139
const ActionList & getInputs() const
Definition: Action.h:143
static bool classof(const Action *A)
Definition: Action.h:448
static bool classof(const Action *A)
Definition: Action.h:526
const BoundArchList & getBoundArchs() const
Definition: Action.h:286
input_const_iterator input_end() const
Definition: Action.h:151
size_type size() const
Definition: Action.h:145
static bool classof(const Action *A)
Definition: Action.h:370
void setCannotBeCollapsedWithNextDependentAction()
Mark this action as not legal to collapse.
Definition: Action.h:157
const ActionList & getActions() const
Get each of the individual arrays.
Definition: Action.h:284
HostDependence(Action &A, const ToolChain &TC, const char *BoundArch, const unsigned OffloadKinds)
Definition: Action.h:308
Action(ActionClass Kind, Action *Input)
Definition: Action.h:129
Kind
ArrayRef< DependentActionInfo > getDependentActionsInfo() const
Return the information about all depending actions.
Definition: Action.h:583
static bool classof(const Action *A)
Definition: Action.h:459
An offload action combines host or/and device actions according to the programming model implementati...
Definition: Action.h:247
input_const_range inputs() const
Definition: Action.h:152
static bool classof(const Action *A)
Definition: Action.h:481
unsigned getOffloadingHostActiveKinds() const
Definition: Action.h:192
Dataflow Directional Tag Classes.
llvm::function_ref< void(Action *, const ToolChain *, const char *)> OffloadActionWorkTy
Definition: Action.h:324
Action(ActionClass Kind, const ActionList &Inputs, types::ID Type)
Definition: Action.h:131
ActionList::iterator input_iterator
Definition: Action.h:51
static bool classof(const Action *A)
Definition: Action.h:415
static Decl::Kind getKind(const Decl *D)
Definition: DeclBase.cpp:930
static bool classof(const Action *A)
Definition: Action.h:426
static bool classof(const Action *A)
Definition: Action.h:239
static bool classof(const Action *A)
Definition: Action.h:503
const char * getOffloadingArch() const
Definition: Action.h:197
StringRef getArchName() const
Definition: Action.h:237
static bool classof(const Action *A)
Definition: Action.h:538
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:88