]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/clang/Driver/Action.h
Vendor import of clang release_39 branch r276489:
[FreeBSD/FreeBSD.git] / include / clang / Driver / Action.h
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,
55     BindArchClass,
56     OffloadClass,
57     PreprocessJobClass,
58     PrecompileJobClass,
59     AnalyzeJobClass,
60     MigrateJobClass,
61     CompileJobClass,
62     BackendJobClass,
63     AssembleJobClass,
64     LinkJobClass,
65     LipoJobClass,
66     DsymutilJobClass,
67     VerifyDebugInfoJobClass,
68     VerifyPCHJobClass,
69
70     JobClassFirst = PreprocessJobClass,
71     JobClassLast = VerifyPCHJobClass
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:
88   ActionClass Kind;
89
90   /// The output type of this action.
91   types::ID Type;
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.
105   OffloadKind OffloadingDeviceKind = OFK_None;
106   /// The Offloading architecture associated with this action.
107   const char *OffloadingArch = nullptr;
108
109   Action(ActionClass Kind, types::ID Type) : Action(Kind, ActionList(), Type) {}
110   Action(ActionClass Kind, Action *Input, types::ID Type)
111       : Action(Kind, ActionList({Input}), Type) {}
112   Action(ActionClass Kind, Action *Input)
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(); }
132   input_range inputs() { return input_range(input_begin(), input_end()); }
133   input_const_iterator input_begin() const { return Inputs.begin(); }
134   input_const_iterator input_end() const { return Inputs.end(); }
135   input_const_range inputs() const {
136     return input_const_range(input_begin(), input_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   }
159   OffloadKind getOffloadingDeviceKind() const { return OffloadingDeviceKind; }
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:
216     typedef SmallVector<const ToolChain *, 3> ToolChainList;
217     typedef SmallVector<const char *, 3> BoundArchList;
218     typedef SmallVector<OffloadKind, 3> OffloadKindList;
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; };
245     const OffloadKindList &getOffloadKinds() const {
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 *)>
278       OffloadActionWorkTy;
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:
330   JobAction(ActionClass Kind, Action *Input, types::ID Type);
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
340 class PreprocessJobAction : public JobAction {
341   void anchor() override;
342 public:
343   PreprocessJobAction(Action *Input, types::ID OutputType);
344
345   static bool classof(const Action *A) {
346     return A->getKind() == PreprocessJobClass;
347   }
348 };
349
350 class PrecompileJobAction : public JobAction {
351   void anchor() override;
352 public:
353   PrecompileJobAction(Action *Input, types::ID OutputType);
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:
413   LinkJobAction(ActionList &Inputs, types::ID Type);
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:
423   LipoJobAction(ActionList &Inputs, types::ID Type);
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:
433   DsymutilJobAction(ActionList &Inputs, types::ID Type);
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:
443   VerifyJobAction(ActionClass Kind, Action *Input, types::ID Type);
444   static bool classof(const Action *A) {
445     return A->getKind() == VerifyDebugInfoJobClass ||
446            A->getKind() == VerifyPCHJobClass;
447   }
448 };
449
450 class VerifyDebugInfoJobAction : public VerifyJobAction {
451   void anchor() override;
452 public:
453   VerifyDebugInfoJobAction(Action *Input, types::ID Type);
454   static bool classof(const Action *A) {
455     return A->getKind() == VerifyDebugInfoJobClass;
456   }
457 };
458
459 class VerifyPCHJobAction : public VerifyJobAction {
460   void anchor() override;
461 public:
462   VerifyPCHJobAction(Action *Input, types::ID Type);
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