Coverage Report

Created: 2019-02-20 07:29

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/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
    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
41.4k
  Action(ActionClass Kind, types::ID Type) : Action(Kind, ActionList(), Type) {}
127
  Action(ActionClass Kind, Action *Input, types::ID Type)
128
81.9k
      : Action(Kind, ActionList({Input}), Type) {}
129
  Action(ActionClass Kind, Action *Input)
130
22.0k
      : Action(Kind, ActionList({Input}), Input->getType()) {}
131
  Action(ActionClass Kind, const ActionList &Inputs, types::ID Type)
132
152k
      : Kind(Kind), Type(Type), Inputs(Inputs) {}
133
134
public:
135
  virtual ~Action();
136
137
0
  const char *getClassName() const { return Action::getClassName(getKind()); }
138
139
1.54M
  ActionClass getKind() const { return Kind; }
140
436k
  types::ID getType() const { return Type; }
141
142
1.99k
  ActionList &getInputs() { return Inputs; }
143
184k
  const ActionList &getInputs() const { return Inputs; }
144
145
35.9k
  size_type size() const { return Inputs.size(); }
146
147
28
  input_iterator input_begin() { return Inputs.begin(); }
148
0
  input_iterator input_end() { return Inputs.end(); }
149
0
  input_range inputs() { return input_range(input_begin(), input_end()); }
150
88.2k
  input_const_iterator input_begin() const { return Inputs.begin(); }
151
33.9k
  input_const_iterator input_end() const { return Inputs.end(); }
152
12.0k
  input_const_range inputs() const {
153
12.0k
    return input_const_range(input_begin(), input_end());
154
12.0k
  }
155
156
  /// Mark this action as not legal to collapse.
157
77
  void setCannotBeCollapsedWithNextDependentAction() {
158
77
    CanBeCollapsedWithNextDependentAction = false;
159
77
  }
160
161
  /// Return true if this function can be collapsed with others.
162
132k
  bool isCollapsingWithNextDependentActionLegal() const {
163
132k
    return CanBeCollapsedWithNextDependentAction;
164
132k
  }
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
28.6k
  unsigned getOffloadingHostActiveKinds() const {
193
28.6k
    return ActiveOffloadKindMask;
194
28.6k
  }
195
196
217k
  OffloadKind getOffloadingDeviceKind() const { return OffloadingDeviceKind; }
197
2.09k
  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
250k
  bool isHostOffloading(OffloadKind OKind) const {
202
250k
    return ActiveOffloadKindMask & OKind;
203
250k
  }
204
218k
  bool isDeviceOffloading(OffloadKind OKind) const {
205
218k
    return OffloadingDeviceKind == OKind;
206
218k
  }
207
79.8k
  bool isOffloading(OffloadKind OKind) const {
208
79.8k
    return isHostOffloading(OKind) || 
isDeviceOffloading(OKind)79.4k
;
209
79.8k
  }
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
45.2k
  const llvm::opt::Arg &getInputArg() const { return Input; }
221
222
104k
  static bool classof(const Action *A) {
223
104k
    return A->getKind() == InputClass;
224
104k
  }
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
21.6k
  StringRef getArchName() const { return ArchName; }
238
239
58.1k
  static bool classof(const Action *A) {
240
58.1k
    return A->getKind() == BindArchClass;
241
58.1k
  }
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
104k
    const ActionList &getActions() const { return DeviceActions; }
285
841
    const ToolChainList &getToolChains() const { return DeviceToolChains; }
286
858
    const BoundArchList &getBoundArchs() const { return DeviceBoundArchs; }
287
1.03k
    const OffloadKindList &getOffloadKinds() const {
288
1.03k
      return DeviceOffloadKinds;
289
1.03k
    }
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
145
          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
676
    Action *getAction() const { return &HostAction; }
318
338
    const ToolChain *getToolChain() const { return &HostToolChain; }
319
676
    const char *getBoundArch() const { return HostBoundArch; }
320
676
    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
236k
  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
132k
  static bool classof(const Action *A) {
382
132k
    return (A->getKind() >= JobClassFirst &&
383
132k
            
A->getKind() <= JobClassLast93.8k
);
384
132k
  }
385
};
386
387
class PreprocessJobAction : public JobAction {
388
  void anchor() override;
389
390
public:
391
  PreprocessJobAction(Action *Input, types::ID OutputType);
392
393
111k
  static bool classof(const Action *A) {
394
111k
    return A->getKind() == PreprocessJobClass;
395
111k
  }
396
};
397
398
class PrecompileJobAction : public JobAction {
399
  void anchor() override;
400
401
protected:
402
  PrecompileJobAction(ActionClass Kind, Action *Input, types::ID OutputType);
403
404
public:
405
  PrecompileJobAction(Action *Input, types::ID OutputType);
406
407
43.8k
  static bool classof(const Action *A) {
408
43.8k
    return A->getKind() == PrecompileJobClass ||
409
43.8k
           
A->getKind() == HeaderModulePrecompileJobClass43.6k
;
410
43.8k
  }
411
};
412
413
class HeaderModulePrecompileJobAction : public PrecompileJobAction {
414
  void anchor() override;
415
416
  const char *ModuleName;
417
418
public:
419
  HeaderModulePrecompileJobAction(Action *Input, types::ID OutputType,
420
                                  const char *ModuleName);
421
422
144k
  static bool classof(const Action *A) {
423
144k
    return A->getKind() == HeaderModulePrecompileJobClass;
424
144k
  }
425
426
4
  void addModuleHeaderInput(Action *Input) {
427
4
    getInputs().push_back(Input);
428
4
  }
429
430
2
  const char *getModuleName() const { return ModuleName; }
431
};
432
433
class AnalyzeJobAction : public JobAction {
434
  void anchor() override;
435
436
public:
437
  AnalyzeJobAction(Action *Input, types::ID OutputType);
438
439
55.7k
  static bool classof(const Action *A) {
440
55.7k
    return A->getKind() == AnalyzeJobClass;
441
55.7k
  }
442
};
443
444
class MigrateJobAction : public JobAction {
445
  void anchor() override;
446
447
public:
448
  MigrateJobAction(Action *Input, types::ID OutputType);
449
450
27.8k
  static bool classof(const Action *A) {
451
27.8k
    return A->getKind() == MigrateJobClass;
452
27.8k
  }
453
};
454
455
class CompileJobAction : public JobAction {
456
  void anchor() override;
457
458
public:
459
  CompileJobAction(Action *Input, types::ID OutputType);
460
461
78.5k
  static bool classof(const Action *A) {
462
78.5k
    return A->getKind() == CompileJobClass;
463
78.5k
  }
464
};
465
466
class BackendJobAction : public JobAction {
467
  void anchor() override;
468
469
public:
470
  BackendJobAction(Action *Input, types::ID OutputType);
471
472
63.8k
  static bool classof(const Action *A) {
473
63.8k
    return A->getKind() == BackendJobClass;
474
63.8k
  }
475
};
476
477
class AssembleJobAction : public JobAction {
478
  void anchor() override;
479
480
public:
481
  AssembleJobAction(Action *Input, types::ID OutputType);
482
483
63.0k
  static bool classof(const Action *A) {
484
63.0k
    return A->getKind() == AssembleJobClass;
485
63.0k
  }
486
};
487
488
class LinkJobAction : public JobAction {
489
  void anchor() override;
490
491
public:
492
  LinkJobAction(ActionList &Inputs, types::ID Type);
493
494
913
  static bool classof(const Action *A) {
495
913
    return A->getKind() == LinkJobClass;
496
913
  }
497
};
498
499
class LipoJobAction : public JobAction {
500
  void anchor() override;
501
502
public:
503
  LipoJobAction(ActionList &Inputs, types::ID Type);
504
505
28.4k
  static bool classof(const Action *A) {
506
28.4k
    return A->getKind() == LipoJobClass;
507
28.4k
  }
508
};
509
510
class DsymutilJobAction : public JobAction {
511
  void anchor() override;
512
513
public:
514
  DsymutilJobAction(ActionList &Inputs, types::ID Type);
515
516
67.9k
  static bool classof(const Action *A) {
517
67.9k
    return A->getKind() == DsymutilJobClass;
518
67.9k
  }
519
};
520
521
class VerifyJobAction : public JobAction {
522
  void anchor() override;
523
524
public:
525
  VerifyJobAction(ActionClass Kind, Action *Input, types::ID Type);
526
527
67.8k
  static bool classof(const Action *A) {
528
67.8k
    return A->getKind() == VerifyDebugInfoJobClass ||
529
67.8k
           
A->getKind() == VerifyPCHJobClass67.8k
;
530
67.8k
  }
531
};
532
533
class VerifyDebugInfoJobAction : public VerifyJobAction {
534
  void anchor() override;
535
536
public:
537
  VerifyDebugInfoJobAction(Action *Input, types::ID Type);
538
539
0
  static bool classof(const Action *A) {
540
0
    return A->getKind() == VerifyDebugInfoJobClass;
541
0
  }
542
};
543
544
class VerifyPCHJobAction : public VerifyJobAction {
545
  void anchor() override;
546
547
public:
548
  VerifyPCHJobAction(Action *Input, types::ID Type);
549
550
9.18k
  static bool classof(const Action *A) {
551
9.18k
    return A->getKind() == VerifyPCHJobClass;
552
9.18k
  }
553
};
554
555
class OffloadBundlingJobAction : public JobAction {
556
  void anchor() override;
557
558
public:
559
  // Offloading bundling doesn't change the type of output.
560
  OffloadBundlingJobAction(ActionList &Inputs);
561
562
0
  static bool classof(const Action *A) {
563
0
    return A->getKind() == OffloadBundlingJobClass;
564
0
  }
565
};
566
567
class OffloadUnbundlingJobAction final : public JobAction {
568
  void anchor() override;
569
570
public:
571
  /// Type that provides information about the actions that depend on this
572
  /// unbundling action.
573
  struct DependentActionInfo final {
574
    /// The tool chain of the dependent action.
575
    const ToolChain *DependentToolChain = nullptr;
576
577
    /// The bound architecture of the dependent action.
578
    StringRef DependentBoundArch;
579
580
    /// The offload kind of the dependent action.
581
    const OffloadKind DependentOffloadKind = OFK_None;
582
583
    DependentActionInfo(const ToolChain *DependentToolChain,
584
                        StringRef DependentBoundArch,
585
                        const OffloadKind DependentOffloadKind)
586
        : DependentToolChain(DependentToolChain),
587
          DependentBoundArch(DependentBoundArch),
588
45
          DependentOffloadKind(DependentOffloadKind) {}
589
  };
590
591
private:
592
  /// Container that keeps information about each dependence of this unbundling
593
  /// action.
594
  SmallVector<DependentActionInfo, 6> DependentActionInfoArray;
595
596
public:
597
  // Offloading unbundling doesn't change the type of output.
598
  OffloadUnbundlingJobAction(Action *Input);
599
600
  /// Register information about a dependent action.
601
  void registerDependentActionInfo(const ToolChain *TC, StringRef BoundArch,
602
45
                                   OffloadKind Kind) {
603
45
    DependentActionInfoArray.push_back({TC, BoundArch, Kind});
604
45
  }
605
606
  /// Return the information about all depending actions.
607
30
  ArrayRef<DependentActionInfo> getDependentActionsInfo() const {
608
30
    return DependentActionInfoArray;
609
30
  }
610
611
37.1k
  static bool classof(const Action *A) {
612
37.1k
    return A->getKind() == OffloadUnbundlingJobClass;
613
37.1k
  }
614
};
615
616
} // namespace driver
617
} // namespace clang
618
619
#endif // LLVM_CLANG_DRIVER_ACTION_H