1 //===--- Action.h - Abstract compilation steps ------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_CLANG_DRIVER_ACTION_H
11 #define LLVM_CLANG_DRIVER_ACTION_H
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"
33 /// Action - Represent an abstract compilation step to perform.
35 /// An action represents an edge in the compilation graph; typically
36 /// it is a job to transform an input using some tool.
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.
43 /// Actions are usually owned by a Compilation, which creates new
44 /// actions via MakeAction().
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;
67 VerifyDebugInfoJobClass,
70 JobClassFirst = PreprocessJobClass,
71 JobClassLast = VerifyPCHJobClass
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.
79 // The host offloading tool chain.
81 // The device offloading tool chains - one bit for each programming model.
85 static const char *getClassName(ActionClass AC);
90 /// The output type of this action.
97 /// Offload information.
100 /// The host offloading kind - a combination of kinds encoded in a mask.
101 /// Multiple programming models may be supported simultaneously by the same
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;
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) {}
120 const char *getClassName() const { return Action::getClassName(getKind()); }
122 ActionClass getKind() const { return Kind; }
123 types::ID getType() const { return Type; }
125 ActionList &getInputs() { return Inputs; }
126 const ActionList &getInputs() const { return Inputs; }
128 size_type size() const { return Inputs.size(); }
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());
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.
144 getOffloadingFileNamePrefix(llvm::StringRef NormalizedTriple) const;
146 /// Set the device offload info of this action and propagate it to its
148 void propagateDeviceOffloadInfo(OffloadKind OKind, const char *OArch);
149 /// Append the host offload info of this action and propagate it to its
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);
156 unsigned getOffloadingHostActiveKinds() const {
157 return ActiveOffloadKindMask;
159 OffloadKind getOffloadingDeviceKind() const { return OffloadingDeviceKind; }
160 const char *getOffloadingArch() const { return OffloadingArch; }
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;
167 bool isDeviceOffloading(OffloadKind OKind) const {
168 return OffloadingDeviceKind == OKind;
170 bool isOffloading(OffloadKind OKind) const {
171 return isHostOffloading(OKind) || isDeviceOffloading(OKind);
175 class InputAction : public Action {
176 virtual void anchor();
177 const llvm::opt::Arg &Input;
180 InputAction(const llvm::opt::Arg &Input, types::ID Type);
182 const llvm::opt::Arg &getInputArg() const { return Input; }
184 static bool classof(const Action *A) {
185 return A->getKind() == InputClass;
189 class BindArchAction : public Action {
190 virtual void anchor();
191 /// The architecture to bind, or 0 if the default architecture
193 const char *ArchName;
196 BindArchAction(Action *Input, const char *ArchName);
198 const char *getArchName() const { return ArchName; }
200 static bool classof(const Action *A) {
201 return A->getKind() == BindArchClass;
205 /// An offload action combines host or/and device actions according to the
206 /// programming model implementation needs and propagates the offloading kind to
208 class OffloadAction final : public Action {
209 virtual void anchor();
212 /// Type used to communicate device actions. It associates bound architecture,
213 /// toolchain, and offload kind to each action.
214 class DeviceDependences final {
216 typedef SmallVector<const ToolChain *, 3> ToolChainList;
217 typedef SmallVector<const char *, 3> BoundArchList;
218 typedef SmallVector<OffloadKind, 3> OffloadKindList;
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.
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;
236 /// Add a action along with the associated toolchain, bound arch, and
238 void add(Action &A, const ToolChain &TC, const char *BoundArch,
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;
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.
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;
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
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; };
277 typedef llvm::function_ref<void(Action *, const ToolChain *, const char *)>
281 /// The host offloading toolchain that should be used with the action.
282 const ToolChain *HostTC = nullptr;
284 /// The tool chains associated with the list of actions.
285 DeviceDependences::ToolChainList DevToolChains;
288 OffloadAction(const HostDependence &HDep);
289 OffloadAction(const DeviceDependences &DDeps, types::ID Ty);
290 OffloadAction(const HostDependence &HDep, const DeviceDependences &DDeps);
292 /// Execute the work specified in \a Work on the host dependence.
293 void doOnHostDependence(const OffloadActionWorkTy &Work) const;
295 /// Execute the work specified in \a Work on each device dependence.
296 void doOnEachDeviceDependence(const OffloadActionWorkTy &Work) const;
298 /// Execute the work specified in \a Work on each dependence.
299 void doOnEachDependence(const OffloadActionWorkTy &Work) const;
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;
306 /// Return true if the action has a host dependence.
307 bool hasHostDependence() const;
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;
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;
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.
322 getSingleDeviceDependence(bool DoNotConsiderHostActions = false) const;
324 static bool classof(const Action *A) { return A->getKind() == OffloadClass; }
327 class JobAction : public Action {
328 virtual void anchor();
330 JobAction(ActionClass Kind, Action *Input, types::ID Type);
331 JobAction(ActionClass Kind, const ActionList &Inputs, types::ID Type);
334 static bool classof(const Action *A) {
335 return (A->getKind() >= JobClassFirst &&
336 A->getKind() <= JobClassLast);
340 class PreprocessJobAction : public JobAction {
341 void anchor() override;
343 PreprocessJobAction(Action *Input, types::ID OutputType);
345 static bool classof(const Action *A) {
346 return A->getKind() == PreprocessJobClass;
350 class PrecompileJobAction : public JobAction {
351 void anchor() override;
353 PrecompileJobAction(Action *Input, types::ID OutputType);
355 static bool classof(const Action *A) {
356 return A->getKind() == PrecompileJobClass;
360 class AnalyzeJobAction : public JobAction {
361 void anchor() override;
363 AnalyzeJobAction(Action *Input, types::ID OutputType);
365 static bool classof(const Action *A) {
366 return A->getKind() == AnalyzeJobClass;
370 class MigrateJobAction : public JobAction {
371 void anchor() override;
373 MigrateJobAction(Action *Input, types::ID OutputType);
375 static bool classof(const Action *A) {
376 return A->getKind() == MigrateJobClass;
380 class CompileJobAction : public JobAction {
381 void anchor() override;
383 CompileJobAction(Action *Input, types::ID OutputType);
385 static bool classof(const Action *A) {
386 return A->getKind() == CompileJobClass;
390 class BackendJobAction : public JobAction {
391 void anchor() override;
393 BackendJobAction(Action *Input, types::ID OutputType);
395 static bool classof(const Action *A) {
396 return A->getKind() == BackendJobClass;
400 class AssembleJobAction : public JobAction {
401 void anchor() override;
403 AssembleJobAction(Action *Input, types::ID OutputType);
405 static bool classof(const Action *A) {
406 return A->getKind() == AssembleJobClass;
410 class LinkJobAction : public JobAction {
411 void anchor() override;
413 LinkJobAction(ActionList &Inputs, types::ID Type);
415 static bool classof(const Action *A) {
416 return A->getKind() == LinkJobClass;
420 class LipoJobAction : public JobAction {
421 void anchor() override;
423 LipoJobAction(ActionList &Inputs, types::ID Type);
425 static bool classof(const Action *A) {
426 return A->getKind() == LipoJobClass;
430 class DsymutilJobAction : public JobAction {
431 void anchor() override;
433 DsymutilJobAction(ActionList &Inputs, types::ID Type);
435 static bool classof(const Action *A) {
436 return A->getKind() == DsymutilJobClass;
440 class VerifyJobAction : public JobAction {
441 void anchor() override;
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;
450 class VerifyDebugInfoJobAction : public VerifyJobAction {
451 void anchor() override;
453 VerifyDebugInfoJobAction(Action *Input, types::ID Type);
454 static bool classof(const Action *A) {
455 return A->getKind() == VerifyDebugInfoJobClass;
459 class VerifyPCHJobAction : public VerifyJobAction {
460 void anchor() override;
462 VerifyPCHJobAction(Action *Input, types::ID Type);
463 static bool classof(const Action *A) {
464 return A->getKind() == VerifyPCHJobClass;
468 } // end namespace driver
469 } // end namespace clang