clang  3.9.0
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;
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 {
69 
72  };
73 
74  // The offloading kind determines if this action is binded to a particular
75  // programming model. Each entry reserves one bit. We also have a special kind
76  // to designate the host offloading tool chain.
77  enum OffloadKind {
78  OFK_None = 0x00,
79  // The host offloading tool chain.
80  OFK_Host = 0x01,
81  // The device offloading tool chains - one bit for each programming model.
82  OFK_Cuda = 0x02,
83  };
84 
85  static const char *getClassName(ActionClass AC);
86 
87 private:
89 
90  /// The output type of this action.
92 
93  ActionList Inputs;
94 
95 protected:
96  ///
97  /// Offload information.
98  ///
99 
100  /// The host offloading kind - a combination of kinds encoded in a mask.
101  /// Multiple programming models may be supported simultaneously by the same
102  /// host.
103  unsigned ActiveOffloadKindMask = 0u;
104  /// Offloading kind of the device.
106  /// The Offloading architecture associated with this action.
107  const char *OffloadingArch = nullptr;
108 
111  : Action(Kind, ActionList({Input}), Type) {}
113  : Action(Kind, ActionList({Input}), Input->getType()) {}
114  Action(ActionClass Kind, const ActionList &Inputs, types::ID Type)
115  : Kind(Kind), Type(Type), Inputs(Inputs) {}
116 
117 public:
118  virtual ~Action();
119 
120  const char *getClassName() const { return Action::getClassName(getKind()); }
121 
122  ActionClass getKind() const { return Kind; }
123  types::ID getType() const { return Type; }
124 
125  ActionList &getInputs() { return Inputs; }
126  const ActionList &getInputs() const { return Inputs; }
127 
128  size_type size() const { return Inputs.size(); }
129 
130  input_iterator input_begin() { return Inputs.begin(); }
131  input_iterator input_end() { return Inputs.end(); }
133  input_const_iterator input_begin() const { return Inputs.begin(); }
134  input_const_iterator input_end() const { return Inputs.end(); }
137  }
138 
139  /// Return a string containing the offload kind of the action.
140  std::string getOffloadingKindPrefix() const;
141  /// Return a string that can be used as prefix in order to generate unique
142  /// files for each offloading kind.
143  std::string
144  getOffloadingFileNamePrefix(llvm::StringRef NormalizedTriple) const;
145 
146  /// Set the device offload info of this action and propagate it to its
147  /// dependences.
148  void propagateDeviceOffloadInfo(OffloadKind OKind, const char *OArch);
149  /// Append the host offload info of this action and propagate it to its
150  /// dependences.
151  void propagateHostOffloadInfo(unsigned OKinds, const char *OArch);
152  /// Set the offload info of this action to be the same as the provided action,
153  /// and propagate it to its dependences.
154  void propagateOffloadInfo(const Action *A);
155 
156  unsigned getOffloadingHostActiveKinds() const {
157  return ActiveOffloadKindMask;
158  }
160  const char *getOffloadingArch() const { return OffloadingArch; }
161 
162  /// Check if this action have any offload kinds. Note that host offload kinds
163  /// are only set if the action is a dependence to a host offload action.
164  bool isHostOffloading(OffloadKind OKind) const {
165  return ActiveOffloadKindMask & OKind;
166  }
167  bool isDeviceOffloading(OffloadKind OKind) const {
168  return OffloadingDeviceKind == OKind;
169  }
170  bool isOffloading(OffloadKind OKind) const {
171  return isHostOffloading(OKind) || isDeviceOffloading(OKind);
172  }
173 };
174 
175 class InputAction : public Action {
176  virtual void anchor();
177  const llvm::opt::Arg &Input;
178 
179 public:
180  InputAction(const llvm::opt::Arg &Input, types::ID Type);
181 
182  const llvm::opt::Arg &getInputArg() const { return Input; }
183 
184  static bool classof(const Action *A) {
185  return A->getKind() == InputClass;
186  }
187 };
188 
189 class BindArchAction : public Action {
190  virtual void anchor();
191  /// The architecture to bind, or 0 if the default architecture
192  /// should be bound.
193  const char *ArchName;
194 
195 public:
196  BindArchAction(Action *Input, const char *ArchName);
197 
198  const char *getArchName() const { return ArchName; }
199 
200  static bool classof(const Action *A) {
201  return A->getKind() == BindArchClass;
202  }
203 };
204 
205 /// An offload action combines host or/and device actions according to the
206 /// programming model implementation needs and propagates the offloading kind to
207 /// its dependences.
208 class OffloadAction final : public Action {
209  virtual void anchor();
210 
211 public:
212  /// Type used to communicate device actions. It associates bound architecture,
213  /// toolchain, and offload kind to each action.
214  class DeviceDependences final {
215  public:
219 
220  private:
221  // Lists that keep the information for each dependency. All the lists are
222  // meant to be updated in sync. We are adopting separate lists instead of a
223  // list of structs, because that simplifies forwarding the actions list to
224  // initialize the inputs of the base Action class.
225 
226  /// The dependence actions.
227  ActionList DeviceActions;
228  /// The offloading toolchains that should be used with the action.
229  ToolChainList DeviceToolChains;
230  /// The architectures that should be used with this action.
231  BoundArchList DeviceBoundArchs;
232  /// The offload kind of each dependence.
233  OffloadKindList DeviceOffloadKinds;
234 
235  public:
236  /// Add a action along with the associated toolchain, bound arch, and
237  /// offload kind.
238  void add(Action &A, const ToolChain &TC, const char *BoundArch,
239  OffloadKind OKind);
240 
241  /// Get each of the individual arrays.
242  const ActionList &getActions() const { return DeviceActions; };
243  const ToolChainList &getToolChains() const { return DeviceToolChains; };
244  const BoundArchList &getBoundArchs() const { return DeviceBoundArchs; };
246  return DeviceOffloadKinds;
247  };
248  };
249 
250  /// Type used to communicate host actions. It associates bound architecture,
251  /// toolchain, and offload kinds to the host action.
252  class HostDependence final {
253  /// The dependence action.
254  Action &HostAction;
255  /// The offloading toolchain that should be used with the action.
256  const ToolChain &HostToolChain;
257  /// The architectures that should be used with this action.
258  const char *HostBoundArch = nullptr;
259  /// The offload kind of each dependence.
260  unsigned HostOffloadKinds = 0u;
261 
262  public:
263  HostDependence(Action &A, const ToolChain &TC, const char *BoundArch,
264  const unsigned OffloadKinds)
265  : HostAction(A), HostToolChain(TC), HostBoundArch(BoundArch),
266  HostOffloadKinds(OffloadKinds){};
267  /// Constructor version that obtains the offload kinds from the device
268  /// dependencies.
269  HostDependence(Action &A, const ToolChain &TC, const char *BoundArch,
270  const DeviceDependences &DDeps);
271  Action *getAction() const { return &HostAction; };
272  const ToolChain *getToolChain() const { return &HostToolChain; };
273  const char *getBoundArch() const { return HostBoundArch; };
274  unsigned getOffloadKinds() const { return HostOffloadKinds; };
275  };
276 
277  typedef llvm::function_ref<void(Action *, const ToolChain *, const char *)>
279 
280 private:
281  /// The host offloading toolchain that should be used with the action.
282  const ToolChain *HostTC = nullptr;
283 
284  /// The tool chains associated with the list of actions.
285  DeviceDependences::ToolChainList DevToolChains;
286 
287 public:
288  OffloadAction(const HostDependence &HDep);
289  OffloadAction(const DeviceDependences &DDeps, types::ID Ty);
290  OffloadAction(const HostDependence &HDep, const DeviceDependences &DDeps);
291 
292  /// Execute the work specified in \a Work on the host dependence.
293  void doOnHostDependence(const OffloadActionWorkTy &Work) const;
294 
295  /// Execute the work specified in \a Work on each device dependence.
296  void doOnEachDeviceDependence(const OffloadActionWorkTy &Work) const;
297 
298  /// Execute the work specified in \a Work on each dependence.
299  void doOnEachDependence(const OffloadActionWorkTy &Work) const;
300 
301  /// Execute the work specified in \a Work on each host or device dependence if
302  /// \a IsHostDependenceto is true or false, respectively.
303  void doOnEachDependence(bool IsHostDependence,
304  const OffloadActionWorkTy &Work) const;
305 
306  /// Return true if the action has a host dependence.
307  bool hasHostDependence() const;
308 
309  /// Return the host dependence of this action. This function is only expected
310  /// to be called if the host dependence exists.
311  Action *getHostDependence() const;
312 
313  /// Return true if the action has a single device dependence. If \a
314  /// DoNotConsiderHostActions is set, ignore the host dependence, if any, while
315  /// accounting for the number of dependences.
316  bool hasSingleDeviceDependence(bool DoNotConsiderHostActions = false) const;
317 
318  /// Return the single device dependence of this action. This function is only
319  /// expected to be called if a single device dependence exists. If \a
320  /// DoNotConsiderHostActions is set, a host dependence is allowed.
321  Action *
322  getSingleDeviceDependence(bool DoNotConsiderHostActions = false) const;
323 
324  static bool classof(const Action *A) { return A->getKind() == OffloadClass; }
325 };
326 
327 class JobAction : public Action {
328  virtual void anchor();
329 protected:
331  JobAction(ActionClass Kind, const ActionList &Inputs, types::ID Type);
332 
333 public:
334  static bool classof(const Action *A) {
335  return (A->getKind() >= JobClassFirst &&
336  A->getKind() <= JobClassLast);
337  }
338 };
339 
341  void anchor() override;
342 public:
344 
345  static bool classof(const Action *A) {
346  return A->getKind() == PreprocessJobClass;
347  }
348 };
349 
351  void anchor() override;
352 public:
354 
355  static bool classof(const Action *A) {
356  return A->getKind() == PrecompileJobClass;
357  }
358 };
359 
360 class AnalyzeJobAction : public JobAction {
361  void anchor() override;
362 public:
363  AnalyzeJobAction(Action *Input, types::ID OutputType);
364 
365  static bool classof(const Action *A) {
366  return A->getKind() == AnalyzeJobClass;
367  }
368 };
369 
370 class MigrateJobAction : public JobAction {
371  void anchor() override;
372 public:
373  MigrateJobAction(Action *Input, types::ID OutputType);
374 
375  static bool classof(const Action *A) {
376  return A->getKind() == MigrateJobClass;
377  }
378 };
379 
380 class CompileJobAction : public JobAction {
381  void anchor() override;
382 public:
383  CompileJobAction(Action *Input, types::ID OutputType);
384 
385  static bool classof(const Action *A) {
386  return A->getKind() == CompileJobClass;
387  }
388 };
389 
390 class BackendJobAction : public JobAction {
391  void anchor() override;
392 public:
393  BackendJobAction(Action *Input, types::ID OutputType);
394 
395  static bool classof(const Action *A) {
396  return A->getKind() == BackendJobClass;
397  }
398 };
399 
400 class AssembleJobAction : public JobAction {
401  void anchor() override;
402 public:
403  AssembleJobAction(Action *Input, types::ID OutputType);
404 
405  static bool classof(const Action *A) {
406  return A->getKind() == AssembleJobClass;
407  }
408 };
409 
410 class LinkJobAction : public JobAction {
411  void anchor() override;
412 public:
414 
415  static bool classof(const Action *A) {
416  return A->getKind() == LinkJobClass;
417  }
418 };
419 
420 class LipoJobAction : public JobAction {
421  void anchor() override;
422 public:
424 
425  static bool classof(const Action *A) {
426  return A->getKind() == LipoJobClass;
427  }
428 };
429 
430 class DsymutilJobAction : public JobAction {
431  void anchor() override;
432 public:
434 
435  static bool classof(const Action *A) {
436  return A->getKind() == DsymutilJobClass;
437  }
438 };
439 
440 class VerifyJobAction : public JobAction {
441  void anchor() override;
442 public:
444  static bool classof(const Action *A) {
445  return A->getKind() == VerifyDebugInfoJobClass ||
446  A->getKind() == VerifyPCHJobClass;
447  }
448 };
449 
451  void anchor() override;
452 public:
454  static bool classof(const Action *A) {
455  return A->getKind() == VerifyDebugInfoJobClass;
456  }
457 };
458 
460  void anchor() override;
461 public:
463  static bool classof(const Action *A) {
464  return A->getKind() == VerifyPCHJobClass;
465  }
466 };
467 
468 } // end namespace driver
469 } // end namespace clang
470 
471 #endif
const ActionList & getActions() const
Get each of the individual arrays.
Definition: Action.h:242
AssembleJobAction(Action *Input, types::ID OutputType)
Definition: Action.cpp:305
OffloadAction(const HostDependence &HDep)
Definition: Action.cpp:133
PrecompileJobAction(Action *Input, types::ID OutputType)
Definition: Action.cpp:280
BackendJobAction(Action *Input, types::ID OutputType)
Definition: Action.cpp:300
static bool classof(const Action *A)
Definition: Action.h:345
Action(ActionClass Kind, Action *Input, types::ID Type)
Definition: Action.h:110
llvm::iterator_range< input_iterator > input_range
Definition: Action.h:50
MigrateJobAction(Action *Input, types::ID OutputType)
Definition: Action.cpp:290
size_type size() const
Definition: Action.h:128
static bool classof(const Action *A)
Definition: Action.h:184
bool hasSingleDeviceDependence(bool DoNotConsiderHostActions=false) const
Return true if the action has a single device dependence.
Definition: Action.cpp:231
The base class of the type hierarchy.
Definition: Type.h:1281
input_range inputs()
Definition: Action.h:132
static bool classof(const Action *A)
Definition: Action.h:385
LinkJobAction(ActionList &Inputs, types::ID Type)
Definition: Action.cpp:310
Action(ActionClass Kind, types::ID Type)
Definition: Action.h:109
Type used to communicate device actions.
Definition: Action.h:214
llvm::function_ref< void(Action *, const ToolChain *, const char *)> OffloadActionWorkTy
Definition: Action.h:278
InputAction(const llvm::opt::Arg &Input, types::ID Type)
Definition: Action.cpp:122
types::ID getType() const
Definition: Action.h:123
VerifyJobAction(ActionClass Kind, Action *Input, types::ID Type)
Definition: Action.cpp:328
VerifyDebugInfoJobAction(Action *Input, types::ID Type)
Definition: Action.cpp:337
input_const_iterator input_begin() const
Definition: Action.h:133
CompileJobAction(Action *Input, types::ID OutputType)
Definition: Action.cpp:295
JobAction(ActionClass Kind, Action *Input, types::ID Type)
Definition: Action.cpp:266
OffloadKind OffloadingDeviceKind
Offloading kind of the device.
Definition: Action.h:105
ActionList & getInputs()
Definition: Action.h:125
OffloadKind getOffloadingDeviceKind() const
Definition: Action.h:159
LipoJobAction(ActionList &Inputs, types::ID Type)
Definition: Action.cpp:316
static bool classof(const Action *A)
Definition: Action.h:435
Type used to communicate host actions.
Definition: Action.h:252
ActionClass getKind() const
Definition: Action.h:122
unsigned getOffloadingHostActiveKinds() const
Definition: Action.h:156
Action - Represent an abstract compilation step to perform.
Definition: Action.h:45
static bool classof(const Action *A)
Definition: Action.h:334
std::string getOffloadingKindPrefix() const
Return a string containing the offload kind of the action.
Definition: Action.cpp:81
void propagateOffloadInfo(const Action *A)
Set the offload info of this action to be the same as the provided action, and propagate it to its de...
Definition: Action.cpp:73
void doOnHostDependence(const OffloadActionWorkTy &Work) const
Execute the work specified in Work on the host dependence.
Definition: Action.cpp:180
static bool classof(const Action *A)
Definition: Action.h:415
ActionList::size_type size_type
Definition: Action.h:47
DsymutilJobAction(ActionList &Inputs, types::ID Type)
Definition: Action.cpp:322
PreprocessJobAction(Action *Input, types::ID OutputType)
Definition: Action.cpp:275
static bool classof(const Action *A)
Definition: Action.h:454
input_iterator input_begin()
Definition: Action.h:130
SmallVector< const char *, 3 > BoundArchList
Definition: Action.h:217
input_iterator input_end()
Definition: Action.h:131
void propagateDeviceOffloadInfo(OffloadKind OKind, const char *OArch)
Set the device offload info of this action and propagate it to its dependences.
Definition: Action.cpp:44
void add(Action &A, const ToolChain &TC, const char *BoundArch, OffloadKind OKind)
Add a action along with the associated toolchain, bound arch, and offload kind.
Definition: Action.cpp:247
SmallVector< const ToolChain *, 3 > ToolChainList
Definition: Action.h:216
static bool classof(const Action *A)
Definition: Action.h:355
unsigned ActiveOffloadKindMask
Offload information.
Definition: Action.h:103
virtual ~Action()
Definition: Action.cpp:19
void doOnEachDeviceDependence(const OffloadActionWorkTy &Work) const
Execute the work specified in Work on each device dependence.
Definition: Action.cpp:188
const OffloadKindList & getOffloadKinds() const
Definition: Action.h:245
static bool classof(const Action *A)
Definition: Action.h:395
VerifyPCHJobAction(Action *Input, types::ID Type)
Definition: Action.cpp:343
const char * getOffloadingArch() const
Definition: Action.h:160
static bool classof(const Action *A)
Definition: Action.h:463
const llvm::opt::Arg & getInputArg() const
Definition: Action.h:182
bool isHostOffloading(OffloadKind OKind) const
Check if this action have any offload kinds.
Definition: Action.h:164
static bool classof(const Action *A)
Definition: Action.h:324
const ActionList & getInputs() const
Definition: Action.h:126
HostDependence(Action &A, const ToolChain &TC, const char *BoundArch, const unsigned OffloadKinds)
Definition: Action.h:263
const char * OffloadingArch
The Offloading architecture associated with this action.
Definition: Action.h:107
const ToolChain * getToolChain() const
Definition: Action.h:272
void doOnEachDependence(const OffloadActionWorkTy &Work) const
Execute the work specified in Work on each dependence.
Definition: Action.cpp:210
Kind
const TemplateArgument * iterator
Definition: Type.h:4233
static bool classof(const Action *A)
Definition: Action.h:405
const ToolChainList & getToolChains() const
Definition: Action.h:243
AnalyzeJobAction(Action *Input, types::ID OutputType)
Definition: Action.cpp:285
An offload action combines host or/and device actions according to the programming model implementati...
Definition: Action.h:208
const char * getArchName() const
Definition: Action.h:198
SmallVector< Action *, 3 > ActionList
ActionList - Type used for lists of actions.
Definition: Util.h:27
Action * getHostDependence() const
Return the host dependence of this action.
Definition: Action.cpp:225
static bool classof(const Action *A)
Definition: Action.h:425
input_const_range inputs() const
Definition: Action.h:135
const char * getClassName() const
Definition: Action.h:120
BindArchAction(Action *Input, const char *ArchName)
Definition: Action.cpp:128
const BoundArchList & getBoundArchs() const
Definition: Action.h:244
bool isOffloading(OffloadKind OKind) const
Definition: Action.h:170
llvm::iterator_range< input_const_iterator > input_const_range
Definition: Action.h:51
SmallVector< OffloadKind, 3 > OffloadKindList
Definition: Action.h:218
Action * getSingleDeviceDependence(bool DoNotConsiderHostActions=false) const
Return the single device dependence of this action.
Definition: Action.cpp:239
std::string getOffloadingFileNamePrefix(llvm::StringRef NormalizedTriple) const
Return a string that can be used as prefix in order to generate unique files for each offloading kind...
Definition: Action.cpp:107
ActionList::const_iterator input_const_iterator
Definition: Action.h:49
bool hasHostDependence() const
Return true if the action has a host dependence.
Definition: Action.cpp:223
ActionList::iterator input_iterator
Definition: Action.h:48
bool isDeviceOffloading(OffloadKind OKind) const
Definition: Action.h:167
void propagateHostOffloadInfo(unsigned OKinds, const char *OArch)
Append the host offload info of this action and propagate it to its dependences.
Definition: Action.cpp:59
static bool classof(const Action *A)
Definition: Action.h:365
const StringRef Input
input_const_iterator input_end() const
Definition: Action.h:134
static bool classof(const Action *A)
Definition: Action.h:375
static bool classof(const Action *A)
Definition: Action.h:200
static bool classof(const Action *A)
Definition: Action.h:444
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:48