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