clang  9.0.0svn
Action.h
Go to the documentation of this file.
1 //===- Action.h - Abstract compilation steps --------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_CLANG_DRIVER_ACTION_H
10 #define LLVM_CLANG_DRIVER_ACTION_H
11 
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"
20 #include <string>
21 
22 namespace llvm {
23 namespace opt {
24 
25 class Arg;
26 
27 } // namespace opt
28 } // namespace llvm
29 
30 namespace clang {
31 namespace driver {
32 
33 class ToolChain;
34 
35 /// Action - Represent an abstract compilation step to perform.
36 ///
37 /// An action represents an edge in the compilation graph; typically
38 /// it is a job to transform an input using some tool.
39 ///
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.
44 ///
45 /// Actions are usually owned by a Compilation, which creates new
46 /// actions via MakeAction().
47 class Action {
48 public:
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>;
54 
55  enum ActionClass {
56  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 protected:
402  PrecompileJobAction(ActionClass Kind, Action *Input, types::ID OutputType);
403 
404 public:
405  PrecompileJobAction(Action *Input, types::ID OutputType);
406 
407  static bool classof(const Action *A) {
408  return A->getKind() == PrecompileJobClass ||
409  A->getKind() == HeaderModulePrecompileJobClass;
410  }
411 };
412 
414  void anchor() override;
415 
416  const char *ModuleName;
417 
418 public:
420  const char *ModuleName);
421 
422  static bool classof(const Action *A) {
423  return A->getKind() == HeaderModulePrecompileJobClass;
424  }
425 
427  getInputs().push_back(Input);
428  }
429 
430  const char *getModuleName() const { return ModuleName; }
431 };
432 
433 class AnalyzeJobAction : public JobAction {
434  void anchor() override;
435 
436 public:
437  AnalyzeJobAction(Action *Input, types::ID OutputType);
438 
439  static bool classof(const Action *A) {
440  return A->getKind() == AnalyzeJobClass;
441  }
442 };
443 
444 class MigrateJobAction : public JobAction {
445  void anchor() override;
446 
447 public:
448  MigrateJobAction(Action *Input, types::ID OutputType);
449 
450  static bool classof(const Action *A) {
451  return A->getKind() == MigrateJobClass;
452  }
453 };
454 
455 class CompileJobAction : public JobAction {
456  void anchor() override;
457 
458 public:
459  CompileJobAction(Action *Input, types::ID OutputType);
460 
461  static bool classof(const Action *A) {
462  return A->getKind() == CompileJobClass;
463  }
464 };
465 
466 class BackendJobAction : public JobAction {
467  void anchor() override;
468 
469 public:
470  BackendJobAction(Action *Input, types::ID OutputType);
471 
472  static bool classof(const Action *A) {
473  return A->getKind() == BackendJobClass;
474  }
475 };
476 
477 class AssembleJobAction : public JobAction {
478  void anchor() override;
479 
480 public:
481  AssembleJobAction(Action *Input, types::ID OutputType);
482 
483  static bool classof(const Action *A) {
484  return A->getKind() == AssembleJobClass;
485  }
486 };
487 
488 class LinkJobAction : public JobAction {
489  void anchor() override;
490 
491 public:
493 
494  static bool classof(const Action *A) {
495  return A->getKind() == LinkJobClass;
496  }
497 };
498 
499 class LipoJobAction : public JobAction {
500  void anchor() override;
501 
502 public:
504 
505  static bool classof(const Action *A) {
506  return A->getKind() == LipoJobClass;
507  }
508 };
509 
510 class DsymutilJobAction : public JobAction {
511  void anchor() override;
512 
513 public:
515 
516  static bool classof(const Action *A) {
517  return A->getKind() == DsymutilJobClass;
518  }
519 };
520 
521 class VerifyJobAction : public JobAction {
522  void anchor() override;
523 
524 public:
526 
527  static bool classof(const Action *A) {
528  return A->getKind() == VerifyDebugInfoJobClass ||
529  A->getKind() == VerifyPCHJobClass;
530  }
531 };
532 
534  void anchor() override;
535 
536 public:
538 
539  static bool classof(const Action *A) {
540  return A->getKind() == VerifyDebugInfoJobClass;
541  }
542 };
543 
545  void anchor() override;
546 
547 public:
549 
550  static bool classof(const Action *A) {
551  return A->getKind() == VerifyPCHJobClass;
552  }
553 };
554 
556  void anchor() override;
557 
558 public:
559  // Offloading bundling doesn't change the type of output.
561 
562  static bool classof(const Action *A) {
563  return A->getKind() == OffloadBundlingJobClass;
564  }
565 };
566 
567 class OffloadUnbundlingJobAction final : public JobAction {
568  void anchor() override;
569 
570 public:
571  /// Type that provides information about the actions that depend on this
572  /// unbundling action.
573  struct DependentActionInfo final {
574  /// The tool chain of the dependent action.
575  const ToolChain *DependentToolChain = nullptr;
576 
577  /// The bound architecture of the dependent action.
579 
580  /// The offload kind of the dependent action.
581  const OffloadKind DependentOffloadKind = OFK_None;
582 
583  DependentActionInfo(const ToolChain *DependentToolChain,
584  StringRef DependentBoundArch,
585  const OffloadKind DependentOffloadKind)
586  : DependentToolChain(DependentToolChain),
587  DependentBoundArch(DependentBoundArch),
588  DependentOffloadKind(DependentOffloadKind) {}
589  };
590 
591 private:
592  /// Container that keeps information about each dependence of this unbundling
593  /// action.
594  SmallVector<DependentActionInfo, 6> DependentActionInfoArray;
595 
596 public:
597  // Offloading unbundling doesn't change the type of output.
599 
600  /// Register information about a dependent action.
601  void registerDependentActionInfo(const ToolChain *TC, StringRef BoundArch,
602  OffloadKind Kind) {
603  DependentActionInfoArray.push_back({TC, BoundArch, Kind});
604  }
605 
606  /// Return the information about all depending actions.
608  return DependentActionInfoArray;
609  }
610 
611  static bool classof(const Action *A) {
612  return A->getKind() == OffloadUnbundlingJobClass;
613  }
614 };
615 
616 } // namespace driver
617 } // namespace clang
618 
619 #endif // LLVM_CLANG_DRIVER_ACTION_H
Type that provides information about the actions that depend on this unbundling action.
Definition: Action.h:573
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:49
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:29
static bool classof(const Action *A)
Definition: Action.h:222
DependentActionInfo(const ToolChain *DependentToolChain, StringRef DependentBoundArch, const OffloadKind DependentOffloadKind)
Definition: Action.h:583
The base class of the type hierarchy.
Definition: Type.h:1418
input_range inputs()
Definition: Action.h:149
static bool classof(const Action *A)
Definition: Action.h:461
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:601
StringRef DependentBoundArch
The bound architecture of the dependent action.
Definition: Action.h:578
const llvm::opt::Arg & getInputArg() const
Definition: Action.h:220
llvm::iterator_range< input_const_iterator > input_const_range
Definition: Action.h:53
ActionList & getInputs()
Definition: Action.h:142
static bool classof(const Action *A)
Definition: Action.h:516
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:47
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:494
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:52
static bool classof(const Action *A)
Definition: Action.h:539
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:611
static bool classof(const Action *A)
Definition: Action.h:407
ActionList::const_iterator input_const_iterator
Definition: Action.h:51
ActionClass getKind() const
Definition: Action.h:139
const ActionList & getInputs() const
Definition: Action.h:143
static bool classof(const Action *A)
Definition: Action.h:472
static bool classof(const Action *A)
Definition: Action.h:550
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:607
static bool classof(const Action *A)
Definition: Action.h:483
static bool classof(const Action *A)
Definition: Action.h:422
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:505
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:50
static bool classof(const Action *A)
Definition: Action.h:439
static Decl::Kind getKind(const Decl *D)
Definition: DeclBase.cpp:943
static bool classof(const Action *A)
Definition: Action.h:450
static bool classof(const Action *A)
Definition: Action.h:239
static bool classof(const Action *A)
Definition: Action.h:527
const char * getOffloadingArch() const
Definition: Action.h:197
StringRef getArchName() const
Definition: Action.h:237
static bool classof(const Action *A)
Definition: Action.h:562
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:88