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,
69 OffloadBundlingJobClass,
70 OffloadUnbundlingJobClass,
72 JobClassFirst = PreprocessJobClass,
73 JobClassLast = OffloadUnbundlingJobClass
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.
81 // The host offloading tool chain.
83 // The device offloading tool chains - one bit for each programming model.
88 static const char *getClassName(ActionClass AC);
93 /// The output type of this action.
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;
106 /// Offload information.
109 /// The host offloading kind - a combination of kinds encoded in a mask.
110 /// Multiple programming models may be supported simultaneously by the same
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;
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) {}
129 const char *getClassName() const { return Action::getClassName(getKind()); }
131 ActionClass getKind() const { return Kind; }
132 types::ID getType() const { return Type; }
134 ActionList &getInputs() { return Inputs; }
135 const ActionList &getInputs() const { return Inputs; }
137 size_type size() const { return Inputs.size(); }
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());
148 /// Mark this action as not legal to collapse.
149 void setCannotBeCollapsedWithNextDependentAction() {
150 CanBeCollapsedWithNextDependentAction = false;
152 /// Return true if this function can be collapsed with others.
153 bool isCollapsingWithNextDependentActionLegal() const {
154 return CanBeCollapsedWithNextDependentAction;
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.
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);
169 /// Set the device offload info of this action and propagate it to its
171 void propagateDeviceOffloadInfo(OffloadKind OKind, const char *OArch);
172 /// Append the host offload info of this action and propagate it to its
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);
179 unsigned getOffloadingHostActiveKinds() const {
180 return ActiveOffloadKindMask;
182 OffloadKind getOffloadingDeviceKind() const { return OffloadingDeviceKind; }
183 const char *getOffloadingArch() const { return OffloadingArch; }
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;
190 bool isDeviceOffloading(OffloadKind OKind) const {
191 return OffloadingDeviceKind == OKind;
193 bool isOffloading(OffloadKind OKind) const {
194 return isHostOffloading(OKind) || isDeviceOffloading(OKind);
198 class InputAction : public Action {
199 virtual void anchor();
200 const llvm::opt::Arg &Input;
203 InputAction(const llvm::opt::Arg &Input, types::ID Type);
205 const llvm::opt::Arg &getInputArg() const { return Input; }
207 static bool classof(const Action *A) {
208 return A->getKind() == InputClass;
212 class BindArchAction : public Action {
213 virtual void anchor();
214 /// The architecture to bind, or 0 if the default architecture
219 BindArchAction(Action *Input, StringRef ArchName);
221 StringRef getArchName() const { return ArchName; }
223 static bool classof(const Action *A) {
224 return A->getKind() == BindArchClass;
228 /// An offload action combines host or/and device actions according to the
229 /// programming model implementation needs and propagates the offloading kind to
231 class OffloadAction final : public Action {
232 virtual void anchor();
235 /// Type used to communicate device actions. It associates bound architecture,
236 /// toolchain, and offload kind to each action.
237 class DeviceDependences final {
239 typedef SmallVector<const ToolChain *, 3> ToolChainList;
240 typedef SmallVector<const char *, 3> BoundArchList;
241 typedef SmallVector<OffloadKind, 3> OffloadKindList;
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.
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;
259 /// Add a action along with the associated toolchain, bound arch, and
261 void add(Action &A, const ToolChain &TC, const char *BoundArch,
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;
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.
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;
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
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; };
300 typedef llvm::function_ref<void(Action *, const ToolChain *, const char *)>
304 /// The host offloading toolchain that should be used with the action.
305 const ToolChain *HostTC = nullptr;
307 /// The tool chains associated with the list of actions.
308 DeviceDependences::ToolChainList DevToolChains;
311 OffloadAction(const HostDependence &HDep);
312 OffloadAction(const DeviceDependences &DDeps, types::ID Ty);
313 OffloadAction(const HostDependence &HDep, const DeviceDependences &DDeps);
315 /// Execute the work specified in \a Work on the host dependence.
316 void doOnHostDependence(const OffloadActionWorkTy &Work) const;
318 /// Execute the work specified in \a Work on each device dependence.
319 void doOnEachDeviceDependence(const OffloadActionWorkTy &Work) const;
321 /// Execute the work specified in \a Work on each dependence.
322 void doOnEachDependence(const OffloadActionWorkTy &Work) const;
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;
329 /// Return true if the action has a host dependence.
330 bool hasHostDependence() const;
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;
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;
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.
345 getSingleDeviceDependence(bool DoNotConsiderHostActions = false) const;
347 static bool classof(const Action *A) { return A->getKind() == OffloadClass; }
350 class JobAction : public Action {
351 virtual void anchor();
353 JobAction(ActionClass Kind, Action *Input, types::ID Type);
354 JobAction(ActionClass Kind, const ActionList &Inputs, types::ID Type);
357 static bool classof(const Action *A) {
358 return (A->getKind() >= JobClassFirst &&
359 A->getKind() <= JobClassLast);
363 class PreprocessJobAction : public JobAction {
364 void anchor() override;
366 PreprocessJobAction(Action *Input, types::ID OutputType);
368 static bool classof(const Action *A) {
369 return A->getKind() == PreprocessJobClass;
373 class PrecompileJobAction : public JobAction {
374 void anchor() override;
376 PrecompileJobAction(Action *Input, types::ID OutputType);
378 static bool classof(const Action *A) {
379 return A->getKind() == PrecompileJobClass;
383 class AnalyzeJobAction : public JobAction {
384 void anchor() override;
386 AnalyzeJobAction(Action *Input, types::ID OutputType);
388 static bool classof(const Action *A) {
389 return A->getKind() == AnalyzeJobClass;
393 class MigrateJobAction : public JobAction {
394 void anchor() override;
396 MigrateJobAction(Action *Input, types::ID OutputType);
398 static bool classof(const Action *A) {
399 return A->getKind() == MigrateJobClass;
403 class CompileJobAction : public JobAction {
404 void anchor() override;
406 CompileJobAction(Action *Input, types::ID OutputType);
408 static bool classof(const Action *A) {
409 return A->getKind() == CompileJobClass;
413 class BackendJobAction : public JobAction {
414 void anchor() override;
416 BackendJobAction(Action *Input, types::ID OutputType);
418 static bool classof(const Action *A) {
419 return A->getKind() == BackendJobClass;
423 class AssembleJobAction : public JobAction {
424 void anchor() override;
426 AssembleJobAction(Action *Input, types::ID OutputType);
428 static bool classof(const Action *A) {
429 return A->getKind() == AssembleJobClass;
433 class LinkJobAction : public JobAction {
434 void anchor() override;
436 LinkJobAction(ActionList &Inputs, types::ID Type);
438 static bool classof(const Action *A) {
439 return A->getKind() == LinkJobClass;
443 class LipoJobAction : public JobAction {
444 void anchor() override;
446 LipoJobAction(ActionList &Inputs, types::ID Type);
448 static bool classof(const Action *A) {
449 return A->getKind() == LipoJobClass;
453 class DsymutilJobAction : public JobAction {
454 void anchor() override;
456 DsymutilJobAction(ActionList &Inputs, types::ID Type);
458 static bool classof(const Action *A) {
459 return A->getKind() == DsymutilJobClass;
463 class VerifyJobAction : public JobAction {
464 void anchor() override;
466 VerifyJobAction(ActionClass Kind, Action *Input, types::ID Type);
467 static bool classof(const Action *A) {
468 return A->getKind() == VerifyDebugInfoJobClass ||
469 A->getKind() == VerifyPCHJobClass;
473 class VerifyDebugInfoJobAction : public VerifyJobAction {
474 void anchor() override;
476 VerifyDebugInfoJobAction(Action *Input, types::ID Type);
477 static bool classof(const Action *A) {
478 return A->getKind() == VerifyDebugInfoJobClass;
482 class VerifyPCHJobAction : public VerifyJobAction {
483 void anchor() override;
485 VerifyPCHJobAction(Action *Input, types::ID Type);
486 static bool classof(const Action *A) {
487 return A->getKind() == VerifyPCHJobClass;
491 class OffloadBundlingJobAction : public JobAction {
492 void anchor() override;
495 // Offloading bundling doesn't change the type of output.
496 OffloadBundlingJobAction(ActionList &Inputs);
498 static bool classof(const Action *A) {
499 return A->getKind() == OffloadBundlingJobClass;
503 class OffloadUnbundlingJobAction final : public JobAction {
504 void anchor() override;
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.
513 StringRef DependentBoundArch;
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){};
525 /// Container that keeps information about each dependence of this unbundling
527 SmallVector<DependentActionInfo, 6> DependentActionInfoArray;
530 // Offloading unbundling doesn't change the type of output.
531 OffloadUnbundlingJobAction(Action *Input);
533 /// Register information about a dependent action.
534 void registerDependentActionInfo(const ToolChain *TC, StringRef BoundArch,
536 DependentActionInfoArray.push_back({TC, BoundArch, Kind});
539 /// Return the information about all depending actions.
540 ArrayRef<DependentActionInfo> getDependentActionsInfo() const {
541 return DependentActionInfoArray;
544 static bool classof(const Action *A) {
545 return A->getKind() == OffloadUnbundlingJobClass;
549 } // end namespace driver
550 } // end namespace clang