Coverage Report

Created: 2020-02-25 14:32

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/Driver/Action.h
Line
Count
Source (jump to first uncovered line)
1
//===- Action.h - Abstract compilation steps --------------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#ifndef LLVM_CLANG_DRIVER_ACTION_H
10
#define LLVM_CLANG_DRIVER_ACTION_H
11
12
#include "clang/Basic/LLVM.h"
13
#include "clang/Driver/Types.h"
14
#include "clang/Driver/Util.h"
15
#include "llvm/ADT/ArrayRef.h"
16
#include "llvm/ADT/STLExtras.h"
17
#include "llvm/ADT/SmallVector.h"
18
#include "llvm/ADT/StringRef.h"
19
#include "llvm/ADT/iterator_range.h"
20
#include <string>
21
22
namespace llvm {
23
namespace opt {
24
25
class Arg;
26
27
} // namespace opt
28
} // namespace llvm
29
30
namespace clang {
31
namespace driver {
32
33
class ToolChain;
34
35
/// Action - Represent an abstract compilation step to perform.
36
///
37
/// An action represents an edge in the compilation graph; typically
38
/// it is a job to transform an input using some tool.
39
///
40
/// The current driver is hard wired to expect actions which produce a
41
/// single primary output, at least in terms of controlling the
42
/// compilation. Actions can produce auxiliary files, but can only
43
/// produce a single output to feed into subsequent actions.
44
///
45
/// Actions are usually owned by a Compilation, which creates new
46
/// actions via MakeAction().
47
class Action {
48
public:
49
  using size_type = ActionList::size_type;
50
  using input_iterator = ActionList::iterator;
51
  using input_const_iterator = ActionList::const_iterator;
52
  using input_range = llvm::iterator_range<input_iterator>;
53
  using input_const_range = llvm::iterator_range<input_const_iterator>;
54
55
  enum ActionClass {
56
    InputClass = 0,
57
    BindArchClass,
58
    OffloadClass,
59
    PreprocessJobClass,
60
    PrecompileJobClass,
61
    HeaderModulePrecompileJobClass,
62
    AnalyzeJobClass,
63
    MigrateJobClass,
64
    CompileJobClass,
65
    BackendJobClass,
66
    AssembleJobClass,
67
    LinkJobClass,
68
    IfsMergeJobClass,
69
    LipoJobClass,
70
    DsymutilJobClass,
71
    VerifyDebugInfoJobClass,
72
    VerifyPCHJobClass,
73
    OffloadBundlingJobClass,
74
    OffloadUnbundlingJobClass,
75
    OffloadWrapperJobClass,
76
77
    JobClassFirst = PreprocessJobClass,
78
    JobClassLast = OffloadWrapperJobClass
79
  };
80
81
  // The offloading kind determines if this action is binded to a particular
82
  // programming model. Each entry reserves one bit. We also have a special kind
83
  // to designate the host offloading tool chain.
84
  enum OffloadKind {
85
    OFK_None = 0x00,
86
87
    // The host offloading tool chain.
88
    OFK_Host = 0x01,
89
90
    // The device offloading tool chains - one bit for each programming model.
91
    OFK_Cuda = 0x02,
92
    OFK_OpenMP = 0x04,
93
    OFK_HIP = 0x08,
94
  };
95
96
  static const char *getClassName(ActionClass AC);
97
98
private:
99
  ActionClass Kind;
100
101
  /// The output type of this action.
102
  types::ID Type;
103
104
  ActionList Inputs;
105
106
  /// Flag that is set to true if this action can be collapsed with others
107
  /// actions that depend on it. This is true by default and set to false when
108
  /// the action is used by two different tool chains, which is enabled by the
109
  /// offloading support implementation.
110
  bool CanBeCollapsedWithNextDependentAction = true;
111
112
protected:
113
  ///
114
  /// Offload information.
115
  ///
116
117
  /// The host offloading kind - a combination of kinds encoded in a mask.
118
  /// Multiple programming models may be supported simultaneously by the same
119
  /// host.
120
  unsigned ActiveOffloadKindMask = 0u;
121
122
  /// Offloading kind of the device.
123
  OffloadKind OffloadingDeviceKind = OFK_None;
124
125
  /// The Offloading architecture associated with this action.
126
  const char *OffloadingArch = nullptr;
127
128
30.8k
  Action(ActionClass Kind, types::ID Type) : Action(Kind, ActionList(), Type) {}
129
  Action(ActionClass Kind, Action *Input, types::ID Type)
130
69.7k
      : Action(Kind, ActionList({Input}), Type) {}
131
  Action(ActionClass Kind, Action *Input)
132
20.0k
      : Action(Kind, ActionList({Input}), Input->getType()) {}
133
  Action(ActionClass Kind, const ActionList &Inputs, types::ID Type)
134
127k
      : Kind(Kind), Type(Type), Inputs(Inputs) {}
135
136
public:
137
  virtual ~Action();
138
139
0
  const char *getClassName() const { return Action::getClassName(getKind()); }
140
141
1.92M
  ActionClass getKind() const { return Kind; }
142
414k
  types::ID getType() const { return Type; }
143
144
2.42k
  ActionList &getInputs() { return Inputs; }
145
178k
  const ActionList &getInputs() const { return Inputs; }
146
147
34.3k
  size_type size() const { return Inputs.size(); }
148
149
32
  input_iterator input_begin() { return Inputs.begin(); }
150
0
  input_iterator input_end() { return Inputs.end(); }
151
0
  input_range inputs() { return input_range(input_begin(), input_end()); }
152
112k
  input_const_iterator input_begin() const { return Inputs.begin(); }
153
59.8k
  input_const_iterator input_end() const { return Inputs.end(); }
154
37.4k
  input_const_range inputs() const {
155
37.4k
    return input_const_range(input_begin(), input_end());
156
37.4k
  }
157
158
  /// Mark this action as not legal to collapse.
159
67
  void setCannotBeCollapsedWithNextDependentAction() {
160
67
    CanBeCollapsedWithNextDependentAction = false;
161
67
  }
162
163
  /// Return true if this function can be collapsed with others.
164
117k
  bool isCollapsingWithNextDependentActionLegal() const {
165
117k
    return CanBeCollapsedWithNextDependentAction;
166
117k
  }
167
168
  /// Return a string containing the offload kind of the action.
169
  std::string getOffloadingKindPrefix() const;
170
171
  /// Return a string that can be used as prefix in order to generate unique
172
  /// files for each offloading kind. By default, no prefix is used for
173
  /// non-device kinds, except if \a CreatePrefixForHost is set.
174
  static std::string
175
  GetOffloadingFileNamePrefix(OffloadKind Kind,
176
                              StringRef NormalizedTriple,
177
                              bool CreatePrefixForHost = false);
178
179
  /// Return a string containing a offload kind name.
180
  static StringRef GetOffloadKindName(OffloadKind Kind);
181
182
  /// Set the device offload info of this action and propagate it to its
183
  /// dependences.
184
  void propagateDeviceOffloadInfo(OffloadKind OKind, const char *OArch);
185
186
  /// Append the host offload info of this action and propagate it to its
187
  /// dependences.
188
  void propagateHostOffloadInfo(unsigned OKinds, const char *OArch);
189
190
  /// Set the offload info of this action to be the same as the provided action,
191
  /// and propagate it to its dependences.
192
  void propagateOffloadInfo(const Action *A);
193
194
22.5k
  unsigned getOffloadingHostActiveKinds() const {
195
22.5k
    return ActiveOffloadKindMask;
196
22.5k
  }
197
198
217k
  OffloadKind getOffloadingDeviceKind() const { return OffloadingDeviceKind; }
199
2.25k
  const char *getOffloadingArch() const { return OffloadingArch; }
200
201
  /// Check if this action have any offload kinds. Note that host offload kinds
202
  /// are only set if the action is a dependence to a host offload action.
203
273k
  bool isHostOffloading(OffloadKind OKind) const {
204
273k
    return ActiveOffloadKindMask & OKind;
205
273k
  }
206
325k
  bool isDeviceOffloading(OffloadKind OKind) const {
207
325k
    return OffloadingDeviceKind == OKind;
208
325k
  }
209
80.3k
  bool isOffloading(OffloadKind OKind) const {
210
80.3k
    return isHostOffloading(OKind) || 
isDeviceOffloading(OKind)79.8k
;
211
80.3k
  }
212
};
213
214
class InputAction : public Action {
215
  const llvm::opt::Arg &Input;
216
217
  virtual void anchor();
218
219
public:
220
  InputAction(const llvm::opt::Arg &Input, types::ID Type);
221
222
31.4k
  const llvm::opt::Arg &getInputArg() const { return Input; }
223
224
119k
  static bool classof(const Action *A) {
225
119k
    return A->getKind() == InputClass;
226
119k
  }
227
};
228
229
class BindArchAction : public Action {
230
  virtual void anchor();
231
232
  /// The architecture to bind, or 0 if the default architecture
233
  /// should be bound.
234
  StringRef ArchName;
235
236
public:
237
  BindArchAction(Action *Input, StringRef ArchName);
238
239
19.7k
  StringRef getArchName() const { return ArchName; }
240
241
74.2k
  static bool classof(const Action *A) {
242
74.2k
    return A->getKind() == BindArchClass;
243
74.2k
  }
244
};
245
246
/// An offload action combines host or/and device actions according to the
247
/// programming model implementation needs and propagates the offloading kind to
248
/// its dependences.
249
class OffloadAction final : public Action {
250
  virtual void anchor();
251
252
public:
253
  /// Type used to communicate device actions. It associates bound architecture,
254
  /// toolchain, and offload kind to each action.
255
  class DeviceDependences final {
256
  public:
257
    using ToolChainList = SmallVector<const ToolChain *, 3>;
258
    using BoundArchList = SmallVector<const char *, 3>;
259
    using OffloadKindList = SmallVector<OffloadKind, 3>;
260
261
  private:
262
    // Lists that keep the information for each dependency. All the lists are
263
    // meant to be updated in sync. We are adopting separate lists instead of a
264
    // list of structs, because that simplifies forwarding the actions list to
265
    // initialize the inputs of the base Action class.
266
267
    /// The dependence actions.
268
    ActionList DeviceActions;
269
270
    /// The offloading toolchains that should be used with the action.
271
    ToolChainList DeviceToolChains;
272
273
    /// The architectures that should be used with this action.
274
    BoundArchList DeviceBoundArchs;
275
276
    /// The offload kind of each dependence.
277
    OffloadKindList DeviceOffloadKinds;
278
279
  public:
280
    /// Add a action along with the associated toolchain, bound arch, and
281
    /// offload kind.
282
    void add(Action &A, const ToolChain &TC, const char *BoundArch,
283
             OffloadKind OKind);
284
285
    /// Get each of the individual arrays.
286
82.8k
    const ActionList &getActions() const { return DeviceActions; }
287
929
    const ToolChainList &getToolChains() const { return DeviceToolChains; }
288
933
    const BoundArchList &getBoundArchs() const { return DeviceBoundArchs; }
289
1.13k
    const OffloadKindList &getOffloadKinds() const {
290
1.13k
      return DeviceOffloadKinds;
291
1.13k
    }
292
  };
293
294
  /// Type used to communicate host actions. It associates bound architecture,
295
  /// toolchain, and offload kinds to the host action.
296
  class HostDependence final {
297
    /// The dependence action.
298
    Action &HostAction;
299
300
    /// The offloading toolchain that should be used with the action.
301
    const ToolChain &HostToolChain;
302
303
    /// The architectures that should be used with this action.
304
    const char *HostBoundArch = nullptr;
305
306
    /// The offload kind of each dependence.
307
    unsigned HostOffloadKinds = 0u;
308
309
  public:
310
    HostDependence(Action &A, const ToolChain &TC, const char *BoundArch,
311
                   const unsigned OffloadKinds)
312
        : HostAction(A), HostToolChain(TC), HostBoundArch(BoundArch),
313
74
          HostOffloadKinds(OffloadKinds) {}
314
315
    /// Constructor version that obtains the offload kinds from the device
316
    /// dependencies.
317
    HostDependence(Action &A, const ToolChain &TC, const char *BoundArch,
318
                   const DeviceDependences &DDeps);
319
586
    Action *getAction() const { return &HostAction; }
320
293
    const ToolChain *getToolChain() const { return &HostToolChain; }
321
586
    const char *getBoundArch() const { return HostBoundArch; }
322
586
    unsigned getOffloadKinds() const { return HostOffloadKinds; }
323
  };
324
325
  using OffloadActionWorkTy =
326
      llvm::function_ref<void(Action *, const ToolChain *, const char *)>;
327
328
private:
329
  /// The host offloading toolchain that should be used with the action.
330
  const ToolChain *HostTC = nullptr;
331
332
  /// The tool chains associated with the list of actions.
333
  DeviceDependences::ToolChainList DevToolChains;
334
335
public:
336
  OffloadAction(const HostDependence &HDep);
337
  OffloadAction(const DeviceDependences &DDeps, types::ID Ty);
338
  OffloadAction(const HostDependence &HDep, const DeviceDependences &DDeps);
339
340
  /// Execute the work specified in \a Work on the host dependence.
341
  void doOnHostDependence(const OffloadActionWorkTy &Work) const;
342
343
  /// Execute the work specified in \a Work on each device dependence.
344
  void doOnEachDeviceDependence(const OffloadActionWorkTy &Work) const;
345
346
  /// Execute the work specified in \a Work on each dependence.
347
  void doOnEachDependence(const OffloadActionWorkTy &Work) const;
348
349
  /// Execute the work specified in \a Work on each host or device dependence if
350
  /// \a IsHostDependenceto is true or false, respectively.
351
  void doOnEachDependence(bool IsHostDependence,
352
                          const OffloadActionWorkTy &Work) const;
353
354
  /// Return true if the action has a host dependence.
355
  bool hasHostDependence() const;
356
357
  /// Return the host dependence of this action. This function is only expected
358
  /// to be called if the host dependence exists.
359
  Action *getHostDependence() const;
360
361
  /// Return true if the action has a single device dependence. If \a
362
  /// DoNotConsiderHostActions is set, ignore the host dependence, if any, while
363
  /// accounting for the number of dependences.
364
  bool hasSingleDeviceDependence(bool DoNotConsiderHostActions = false) const;
365
366
  /// Return the single device dependence of this action. This function is only
367
  /// expected to be called if a single device dependence exists. If \a
368
  /// DoNotConsiderHostActions is set, a host dependence is allowed.
369
  Action *
370
  getSingleDeviceDependence(bool DoNotConsiderHostActions = false) const;
371
372
205k
  static bool classof(const Action *A) { return A->getKind() == OffloadClass; }
373
};
374
375
class JobAction : public Action {
376
  virtual void anchor();
377
378
protected:
379
  JobAction(ActionClass Kind, Action *Input, types::ID Type);
380
  JobAction(ActionClass Kind, const ActionList &Inputs, types::ID Type);
381
382
public:
383
231k
  static bool classof(const Action *A) {
384
231k
    return (A->getKind() >= JobClassFirst &&
385
231k
            
A->getKind() <= JobClassLast194k
);
386
231k
  }
387
};
388
389
class PreprocessJobAction : public JobAction {
390
  void anchor() override;
391
392
public:
393
  PreprocessJobAction(Action *Input, types::ID OutputType);
394
395
138k
  static bool classof(const Action *A) {
396
138k
    return A->getKind() == PreprocessJobClass;
397
138k
  }
398
};
399
400
class PrecompileJobAction : public JobAction {
401
  void anchor() override;
402
403
protected:
404
  PrecompileJobAction(ActionClass Kind, Action *Input, types::ID OutputType);
405
406
public:
407
  PrecompileJobAction(Action *Input, types::ID OutputType);
408
409
39.6k
  static bool classof(const Action *A) {
410
39.6k
    return A->getKind() == PrecompileJobClass ||
411
39.6k
           
A->getKind() == HeaderModulePrecompileJobClass39.4k
;
412
39.6k
  }
413
};
414
415
class HeaderModulePrecompileJobAction : public PrecompileJobAction {
416
  void anchor() override;
417
418
  const char *ModuleName;
419
420
public:
421
  HeaderModulePrecompileJobAction(Action *Input, types::ID OutputType,
422
                                  const char *ModuleName);
423
424
128k
  static bool classof(const Action *A) {
425
128k
    return A->getKind() == HeaderModulePrecompileJobClass;
426
128k
  }
427
428
4
  void addModuleHeaderInput(Action *Input) {
429
4
    getInputs().push_back(Input);
430
4
  }
431
432
3
  const char *getModuleName() const { return ModuleName; }
433
};
434
435
class AnalyzeJobAction : public JobAction {
436
  void anchor() override;
437
438
public:
439
  AnalyzeJobAction(Action *Input, types::ID OutputType);
440
441
79.8k
  static bool classof(const Action *A) {
442
79.8k
    return A->getKind() == AnalyzeJobClass;
443
79.8k
  }
444
};
445
446
class MigrateJobAction : public JobAction {
447
  void anchor() override;
448
449
public:
450
  MigrateJobAction(Action *Input, types::ID OutputType);
451
452
26.6k
  static bool classof(const Action *A) {
453
26.6k
    return A->getKind() == MigrateJobClass;
454
26.6k
  }
455
};
456
457
class CompileJobAction : public JobAction {
458
  void anchor() override;
459
460
public:
461
  CompileJobAction(Action *Input, types::ID OutputType);
462
463
127k
  static bool classof(const Action *A) {
464
127k
    return A->getKind() == CompileJobClass;
465
127k
  }
466
};
467
468
class BackendJobAction : public JobAction {
469
  void anchor() override;
470
471
public:
472
  BackendJobAction(Action *Input, types::ID OutputType);
473
474
108k
  static bool classof(const Action *A) {
475
108k
    return A->getKind() == BackendJobClass;
476
108k
  }
477
};
478
479
class AssembleJobAction : public JobAction {
480
  void anchor() override;
481
482
public:
483
  AssembleJobAction(Action *Input, types::ID OutputType);
484
485
112k
  static bool classof(const Action *A) {
486
112k
    return A->getKind() == AssembleJobClass;
487
112k
  }
488
};
489
490
class IfsMergeJobAction : public JobAction {
491
  void anchor() override;
492
493
public:
494
  IfsMergeJobAction(ActionList &Inputs, types::ID Type);
495
496
0
  static bool classof(const Action *A) {
497
0
    return A->getKind() == IfsMergeJobClass;
498
0
  }
499
};
500
501
class LinkJobAction : public JobAction {
502
  void anchor() override;
503
504
public:
505
  LinkJobAction(ActionList &Inputs, types::ID Type);
506
507
898
  static bool classof(const Action *A) {
508
898
    return A->getKind() == LinkJobClass;
509
898
  }
510
};
511
512
class LipoJobAction : public JobAction {
513
  void anchor() override;
514
515
public:
516
  LipoJobAction(ActionList &Inputs, types::ID Type);
517
518
28.9k
  static bool classof(const Action *A) {
519
28.9k
    return A->getKind() == LipoJobClass;
520
28.9k
  }
521
};
522
523
class DsymutilJobAction : public JobAction {
524
  void anchor() override;
525
526
public:
527
  DsymutilJobAction(ActionList &Inputs, types::ID Type);
528
529
54.2k
  static bool classof(const Action *A) {
530
54.2k
    return A->getKind() == DsymutilJobClass;
531
54.2k
  }
532
};
533
534
class VerifyJobAction : public JobAction {
535
  void anchor() override;
536
537
public:
538
  VerifyJobAction(ActionClass Kind, Action *Input, types::ID Type);
539
540
53.9k
  static bool classof(const Action *A) {
541
53.9k
    return A->getKind() == VerifyDebugInfoJobClass ||
542
53.9k
           
A->getKind() == VerifyPCHJobClass53.9k
;
543
53.9k
  }
544
};
545
546
class VerifyDebugInfoJobAction : public VerifyJobAction {
547
  void anchor() override;
548
549
public:
550
  VerifyDebugInfoJobAction(Action *Input, types::ID Type);
551
552
0
  static bool classof(const Action *A) {
553
0
    return A->getKind() == VerifyDebugInfoJobClass;
554
0
  }
555
};
556
557
class VerifyPCHJobAction : public VerifyJobAction {
558
  void anchor() override;
559
560
public:
561
  VerifyPCHJobAction(Action *Input, types::ID Type);
562
563
13.8k
  static bool classof(const Action *A) {
564
13.8k
    return A->getKind() == VerifyPCHJobClass;
565
13.8k
  }
566
};
567
568
class OffloadBundlingJobAction : public JobAction {
569
  void anchor() override;
570
571
public:
572
  // Offloading bundling doesn't change the type of output.
573
  OffloadBundlingJobAction(ActionList &Inputs);
574
575
17
  static bool classof(const Action *A) {
576
17
    return A->getKind() == OffloadBundlingJobClass;
577
17
  }
578
};
579
580
class OffloadUnbundlingJobAction final : public JobAction {
581
  void anchor() override;
582
583
public:
584
  /// Type that provides information about the actions that depend on this
585
  /// unbundling action.
586
  struct DependentActionInfo final {
587
    /// The tool chain of the dependent action.
588
    const ToolChain *DependentToolChain = nullptr;
589
590
    /// The bound architecture of the dependent action.
591
    StringRef DependentBoundArch;
592
593
    /// The offload kind of the dependent action.
594
    const OffloadKind DependentOffloadKind = OFK_None;
595
596
    DependentActionInfo(const ToolChain *DependentToolChain,
597
                        StringRef DependentBoundArch,
598
                        const OffloadKind DependentOffloadKind)
599
        : DependentToolChain(DependentToolChain),
600
          DependentBoundArch(DependentBoundArch),
601
46
          DependentOffloadKind(DependentOffloadKind) {}
602
  };
603
604
private:
605
  /// Container that keeps information about each dependence of this unbundling
606
  /// action.
607
  SmallVector<DependentActionInfo, 6> DependentActionInfoArray;
608
609
public:
610
  // Offloading unbundling doesn't change the type of output.
611
  OffloadUnbundlingJobAction(Action *Input);
612
613
  /// Register information about a dependent action.
614
  void registerDependentActionInfo(const ToolChain *TC, StringRef BoundArch,
615
46
                                   OffloadKind Kind) {
616
46
    DependentActionInfoArray.push_back({TC, BoundArch, Kind});
617
46
  }
618
619
  /// Return the information about all depending actions.
620
30
  ArrayRef<DependentActionInfo> getDependentActionsInfo() const {
621
30
    return DependentActionInfoArray;
622
30
  }
623
624
35.6k
  static bool classof(const Action *A) {
625
35.6k
    return A->getKind() == OffloadUnbundlingJobClass;
626
35.6k
  }
627
};
628
629
class OffloadWrapperJobAction : public JobAction {
630
  void anchor() override;
631
632
public:
633
  OffloadWrapperJobAction(ActionList &Inputs, types::ID Type);
634
635
22.5k
  static bool classof(const Action *A) {
636
22.5k
    return A->getKind() == OffloadWrapperJobClass;
637
22.5k
  }
638
};
639
640
} // namespace driver
641
} // namespace clang
642
643
#endif // LLVM_CLANG_DRIVER_ACTION_H