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/LLVM.h"
14 #include "clang/Driver/Types.h"
15 #include "clang/Driver/Util.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/iterator_range.h"
21 #include <string>
22
23 namespace llvm {
24 namespace opt {
25
maria_close(register MARIA_HA * info)26 class Arg;
27
28 } // namespace opt
29 } // namespace llvm
30
31 namespace clang {
32 namespace driver {
33
34 class ToolChain;
35
36 /// Action - Represent an abstract compilation step to perform.
37 ///
38 /// An action represents an edge in the compilation graph; typically
39 /// it is a job to transform an input using some tool.
40 ///
41 /// The current driver is hard wired to expect actions which produce a
42 /// single primary output, at least in terms of controlling the
43 /// compilation. Actions can produce auxiliary files, but can only
44 /// produce a single output to feed into subsequent actions.
45 ///
46 /// Actions are usually owned by a Compilation, which creates new
47 /// actions via MakeAction().
48 class Action {
49 public:
50 using size_type = ActionList::size_type;
51 using input_iterator = ActionList::iterator;
52 using input_const_iterator = ActionList::const_iterator;
53 using input_range = llvm::iterator_range<input_iterator>;
54 using input_const_range = llvm::iterator_range<input_const_iterator>;
55
56 enum ActionClass {
57 InputClass = 0,
58 BindArchClass,
59 OffloadClass,
60 PreprocessJobClass,
61 PrecompileJobClass,
62 AnalyzeJobClass,
63 MigrateJobClass,
64 CompileJobClass,
65 BackendJobClass,
66 AssembleJobClass,
67 LinkJobClass,
68 LipoJobClass,
69 DsymutilJobClass,
70 VerifyDebugInfoJobClass,
71 VerifyPCHJobClass,
72 OffloadBundlingJobClass,
73 OffloadUnbundlingJobClass,
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:
97 ActionClass Kind;
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(); }
152 input_const_range inputs() const {
153 return input_const_range(input_begin(), input_end());
154 }
155
156 /// Mark this action as not legal to collapse.
157 void setCannotBeCollapsedWithNextDependentAction() {
158 CanBeCollapsedWithNextDependentAction = false;
159 }
160
161 /// Return true if this function can be collapsed with others.
162 bool isCollapsingWithNextDependentActionLegal() const {
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:
255 using ToolChainList = SmallVector<const ToolChain *, 3>;
256 using BoundArchList = SmallVector<const char *, 3>;
257 using OffloadKindList = SmallVector<OffloadKind, 3>;
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; }
287 const OffloadKindList &getOffloadKinds() const {
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:
377 JobAction(ActionClass Kind, Action *Input, types::ID Type);
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
387 class PreprocessJobAction : public JobAction {
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
398 class PrecompileJobAction : public JobAction {
399 void anchor() override;
400
401 public:
402 PrecompileJobAction(Action *Input, types::ID OutputType);
403
404 static bool classof(const Action *A) {
405 return A->getKind() == PrecompileJobClass;
406 }
407 };
408
409 class AnalyzeJobAction : public JobAction {
410 void anchor() override;
411
412 public:
413 AnalyzeJobAction(Action *Input, types::ID OutputType);
414
415 static bool classof(const Action *A) {
416 return A->getKind() == AnalyzeJobClass;
417 }
418 };
419
420 class MigrateJobAction : public JobAction {
421 void anchor() override;
422
423 public:
424 MigrateJobAction(Action *Input, types::ID OutputType);
425
426 static bool classof(const Action *A) {
427 return A->getKind() == MigrateJobClass;
428 }
429 };
430
431 class CompileJobAction : public JobAction {
432 void anchor() override;
433
434 public:
435 CompileJobAction(Action *Input, types::ID OutputType);
436
437 static bool classof(const Action *A) {
438 return A->getKind() == CompileJobClass;
439 }
440 };
441
442 class BackendJobAction : public JobAction {
443 void anchor() override;
444
445 public:
446 BackendJobAction(Action *Input, types::ID OutputType);
447
448 static bool classof(const Action *A) {
449 return A->getKind() == BackendJobClass;
450 }
451 };
452
453 class AssembleJobAction : public JobAction {
454 void anchor() override;
455
456 public:
457 AssembleJobAction(Action *Input, types::ID OutputType);
458
459 static bool classof(const Action *A) {
460 return A->getKind() == AssembleJobClass;
461 }
462 };
463
464 class LinkJobAction : public JobAction {
465 void anchor() override;
466
467 public:
468 LinkJobAction(ActionList &Inputs, types::ID Type);
469
470 static bool classof(const Action *A) {
471 return A->getKind() == LinkJobClass;
472 }
473 };
474
475 class LipoJobAction : public JobAction {
476 void anchor() override;
477
478 public:
479 LipoJobAction(ActionList &Inputs, types::ID Type);
480
481 static bool classof(const Action *A) {
482 return A->getKind() == LipoJobClass;
483 }
484 };
485
486 class DsymutilJobAction : public JobAction {
487 void anchor() override;
488
489 public:
490 DsymutilJobAction(ActionList &Inputs, types::ID Type);
491
492 static bool classof(const Action *A) {
493 return A->getKind() == DsymutilJobClass;
494 }
495 };
496
497 class VerifyJobAction : public JobAction {
498 void anchor() override;
499
500 public:
501 VerifyJobAction(ActionClass Kind, Action *Input, types::ID Type);
502
503 static bool classof(const Action *A) {
504 return A->getKind() == VerifyDebugInfoJobClass ||
505 A->getKind() == VerifyPCHJobClass;
506 }
507 };
508
509 class VerifyDebugInfoJobAction : public VerifyJobAction {
510 void anchor() override;
511
512 public:
513 VerifyDebugInfoJobAction(Action *Input, types::ID Type);
514
515 static bool classof(const Action *A) {
516 return A->getKind() == VerifyDebugInfoJobClass;
517 }
518 };
519
520 class VerifyPCHJobAction : public VerifyJobAction {
521 void anchor() override;
522
523 public:
524 VerifyPCHJobAction(Action *Input, types::ID Type);
525
526 static bool classof(const Action *A) {
527 return A->getKind() == VerifyPCHJobClass;
528 }
529 };
530
531 class OffloadBundlingJobAction : public JobAction {
532 void anchor() override;
533
534 public:
535 // Offloading bundling doesn't change the type of output.
536 OffloadBundlingJobAction(ActionList &Inputs);
537
538 static bool classof(const Action *A) {
539 return A->getKind() == OffloadBundlingJobClass;
540 }
541 };
542
543 class OffloadUnbundlingJobAction final : public JobAction {
544 void anchor() override;
545
546 public:
547 /// Type that provides information about the actions that depend on this
548 /// unbundling action.
549 struct DependentActionInfo final {
550 /// The tool chain of the dependent action.
551 const ToolChain *DependentToolChain = nullptr;
552
553 /// The bound architecture of the dependent action.
554 StringRef DependentBoundArch;
555
556 /// The offload kind of the dependent action.
557 const OffloadKind DependentOffloadKind = OFK_None;
558
559 DependentActionInfo(const ToolChain *DependentToolChain,
560 StringRef DependentBoundArch,
561 const OffloadKind DependentOffloadKind)
562 : DependentToolChain(DependentToolChain),
563 DependentBoundArch(DependentBoundArch),
564 DependentOffloadKind(DependentOffloadKind) {}
565 };
566
567 private:
568 /// Container that keeps information about each dependence of this unbundling
569 /// action.
570 SmallVector<DependentActionInfo, 6> DependentActionInfoArray;
571
572 public:
573 // Offloading unbundling doesn't change the type of output.
574 OffloadUnbundlingJobAction(Action *Input);
575
576 /// Register information about a dependent action.
577 void registerDependentActionInfo(const ToolChain *TC, StringRef BoundArch,
578 OffloadKind Kind) {
579 DependentActionInfoArray.push_back({TC, BoundArch, Kind});
580 }
581
582 /// Return the information about all depending actions.
583 ArrayRef<DependentActionInfo> getDependentActionsInfo() const {
584 return DependentActionInfoArray;
585 }
586
587 static bool classof(const Action *A) {
588 return A->getKind() == OffloadUnbundlingJobClass;
589 }
590 };
591
592 } // namespace driver
593 } // namespace clang
594
595 #endif // LLVM_CLANG_DRIVER_ACTION_H
596