Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/clang/lib/Driver/Action.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- Action.cpp - Abstract compilation steps ----------------------------===//
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
#include "clang/Driver/Action.h"
10
#include "llvm/Support/ErrorHandling.h"
11
#include <cassert>
12
#include <string>
13
14
using namespace clang;
15
using namespace driver;
16
using namespace llvm::opt;
17
18
0
Action::~Action() = default;
19
20
0
const char *Action::getClassName(ActionClass AC) {
21
0
  switch (AC) {
22
0
  case InputClass: return "input";
23
0
  case BindArchClass: return "bind-arch";
24
0
  case OffloadClass:
25
0
    return "offload";
26
0
  case PreprocessJobClass: return "preprocessor";
27
0
  case PrecompileJobClass: return "precompiler";
28
0
  case ExtractAPIJobClass:
29
0
    return "api-extractor";
30
0
  case AnalyzeJobClass: return "analyzer";
31
0
  case MigrateJobClass: return "migrator";
32
0
  case CompileJobClass: return "compiler";
33
0
  case BackendJobClass: return "backend";
34
0
  case AssembleJobClass: return "assembler";
35
0
  case IfsMergeJobClass: return "interface-stub-merger";
36
0
  case LinkJobClass: return "linker";
37
0
  case LipoJobClass: return "lipo";
38
0
  case DsymutilJobClass: return "dsymutil";
39
0
  case VerifyDebugInfoJobClass: return "verify-debug-info";
40
0
  case VerifyPCHJobClass: return "verify-pch";
41
0
  case OffloadBundlingJobClass:
42
0
    return "clang-offload-bundler";
43
0
  case OffloadUnbundlingJobClass:
44
0
    return "clang-offload-unbundler";
45
0
  case OffloadPackagerJobClass:
46
0
    return "clang-offload-packager";
47
0
  case LinkerWrapperJobClass:
48
0
    return "clang-linker-wrapper";
49
0
  case StaticLibJobClass:
50
0
    return "static-lib-linker";
51
0
  case BinaryAnalyzeJobClass:
52
0
    return "binary-analyzer";
53
0
  }
54
55
0
  llvm_unreachable("invalid class");
56
0
}
57
58
void Action::propagateDeviceOffloadInfo(OffloadKind OKind, const char *OArch,
59
0
                                        const ToolChain *OToolChain) {
60
  // Offload action set its own kinds on their dependences.
61
0
  if (Kind == OffloadClass)
62
0
    return;
63
  // Unbundling actions use the host kinds.
64
0
  if (Kind == OffloadUnbundlingJobClass)
65
0
    return;
66
67
0
  assert((OffloadingDeviceKind == OKind || OffloadingDeviceKind == OFK_None) &&
68
0
         "Setting device kind to a different device??");
69
0
  assert(!ActiveOffloadKindMask && "Setting a device kind in a host action??");
70
0
  OffloadingDeviceKind = OKind;
71
0
  OffloadingArch = OArch;
72
0
  OffloadingToolChain = OToolChain;
73
74
0
  for (auto *A : Inputs)
75
0
    A->propagateDeviceOffloadInfo(OffloadingDeviceKind, OArch, OToolChain);
76
0
}
77
78
0
void Action::propagateHostOffloadInfo(unsigned OKinds, const char *OArch) {
79
  // Offload action set its own kinds on their dependences.
80
0
  if (Kind == OffloadClass)
81
0
    return;
82
83
0
  assert(OffloadingDeviceKind == OFK_None &&
84
0
         "Setting a host kind in a device action.");
85
0
  ActiveOffloadKindMask |= OKinds;
86
0
  OffloadingArch = OArch;
87
88
0
  for (auto *A : Inputs)
89
0
    A->propagateHostOffloadInfo(ActiveOffloadKindMask, OArch);
90
0
}
91
92
0
void Action::propagateOffloadInfo(const Action *A) {
93
0
  if (unsigned HK = A->getOffloadingHostActiveKinds())
94
0
    propagateHostOffloadInfo(HK, A->getOffloadingArch());
95
0
  else
96
0
    propagateDeviceOffloadInfo(A->getOffloadingDeviceKind(),
97
0
                               A->getOffloadingArch(),
98
0
                               A->getOffloadingToolChain());
99
0
}
100
101
0
std::string Action::getOffloadingKindPrefix() const {
102
0
  switch (OffloadingDeviceKind) {
103
0
  case OFK_None:
104
0
    break;
105
0
  case OFK_Host:
106
0
    llvm_unreachable("Host kind is not an offloading device kind.");
107
0
    break;
108
0
  case OFK_Cuda:
109
0
    return "device-cuda";
110
0
  case OFK_OpenMP:
111
0
    return "device-openmp";
112
0
  case OFK_HIP:
113
0
    return "device-hip";
114
115
    // TODO: Add other programming models here.
116
0
  }
117
118
0
  if (!ActiveOffloadKindMask)
119
0
    return {};
120
121
0
  std::string Res("host");
122
0
  assert(!((ActiveOffloadKindMask & OFK_Cuda) &&
123
0
           (ActiveOffloadKindMask & OFK_HIP)) &&
124
0
         "Cannot offload CUDA and HIP at the same time");
125
0
  if (ActiveOffloadKindMask & OFK_Cuda)
126
0
    Res += "-cuda";
127
0
  if (ActiveOffloadKindMask & OFK_HIP)
128
0
    Res += "-hip";
129
0
  if (ActiveOffloadKindMask & OFK_OpenMP)
130
0
    Res += "-openmp";
131
132
  // TODO: Add other programming models here.
133
134
0
  return Res;
135
0
}
136
137
/// Return a string that can be used as prefix in order to generate unique files
138
/// for each offloading kind.
139
std::string
140
Action::GetOffloadingFileNamePrefix(OffloadKind Kind,
141
                                    StringRef NormalizedTriple,
142
0
                                    bool CreatePrefixForHost) {
143
  // Don't generate prefix for host actions unless required.
144
0
  if (!CreatePrefixForHost && (Kind == OFK_None || Kind == OFK_Host))
145
0
    return {};
146
147
0
  std::string Res("-");
148
0
  Res += GetOffloadKindName(Kind);
149
0
  Res += "-";
150
0
  Res += NormalizedTriple;
151
0
  return Res;
152
0
}
153
154
/// Return a string with the offload kind name. If that is not defined, we
155
/// assume 'host'.
156
0
StringRef Action::GetOffloadKindName(OffloadKind Kind) {
157
0
  switch (Kind) {
158
0
  case OFK_None:
159
0
  case OFK_Host:
160
0
    return "host";
161
0
  case OFK_Cuda:
162
0
    return "cuda";
163
0
  case OFK_OpenMP:
164
0
    return "openmp";
165
0
  case OFK_HIP:
166
0
    return "hip";
167
168
    // TODO: Add other programming models here.
169
0
  }
170
171
0
  llvm_unreachable("invalid offload kind");
172
0
}
173
174
0
void InputAction::anchor() {}
175
176
InputAction::InputAction(const Arg &_Input, types::ID _Type, StringRef _Id)
177
0
    : Action(InputClass, _Type), Input(_Input), Id(_Id.str()) {}
178
179
0
void BindArchAction::anchor() {}
180
181
BindArchAction::BindArchAction(Action *Input, StringRef ArchName)
182
0
    : Action(BindArchClass, Input), ArchName(ArchName) {}
183
184
0
void OffloadAction::anchor() {}
185
186
OffloadAction::OffloadAction(const HostDependence &HDep)
187
0
    : Action(OffloadClass, HDep.getAction()), HostTC(HDep.getToolChain()) {
188
0
  OffloadingArch = HDep.getBoundArch();
189
0
  ActiveOffloadKindMask = HDep.getOffloadKinds();
190
0
  HDep.getAction()->propagateHostOffloadInfo(HDep.getOffloadKinds(),
191
0
                                             HDep.getBoundArch());
192
0
}
193
194
OffloadAction::OffloadAction(const DeviceDependences &DDeps, types::ID Ty)
195
    : Action(OffloadClass, DDeps.getActions(), Ty),
196
0
      DevToolChains(DDeps.getToolChains()) {
197
0
  auto &OKinds = DDeps.getOffloadKinds();
198
0
  auto &BArchs = DDeps.getBoundArchs();
199
0
  auto &OTCs = DDeps.getToolChains();
200
201
  // If all inputs agree on the same kind, use it also for this action.
202
0
  if (llvm::all_equal(OKinds))
203
0
    OffloadingDeviceKind = OKinds.front();
204
205
  // If we have a single dependency, inherit the architecture from it.
206
0
  if (OKinds.size() == 1)
207
0
    OffloadingArch = BArchs.front();
208
209
  // Propagate info to the dependencies.
210
0
  for (unsigned i = 0, e = getInputs().size(); i != e; ++i)
211
0
    getInputs()[i]->propagateDeviceOffloadInfo(OKinds[i], BArchs[i], OTCs[i]);
212
0
}
213
214
OffloadAction::OffloadAction(const HostDependence &HDep,
215
                             const DeviceDependences &DDeps)
216
    : Action(OffloadClass, HDep.getAction()), HostTC(HDep.getToolChain()),
217
0
      DevToolChains(DDeps.getToolChains()) {
218
  // We use the kinds of the host dependence for this action.
219
0
  OffloadingArch = HDep.getBoundArch();
220
0
  ActiveOffloadKindMask = HDep.getOffloadKinds();
221
0
  HDep.getAction()->propagateHostOffloadInfo(HDep.getOffloadKinds(),
222
0
                                             HDep.getBoundArch());
223
224
  // Add device inputs and propagate info to the device actions. Do work only if
225
  // we have dependencies.
226
0
  for (unsigned i = 0, e = DDeps.getActions().size(); i != e; ++i) {
227
0
    if (auto *A = DDeps.getActions()[i]) {
228
0
      getInputs().push_back(A);
229
0
      A->propagateDeviceOffloadInfo(DDeps.getOffloadKinds()[i],
230
0
                                    DDeps.getBoundArchs()[i],
231
0
                                    DDeps.getToolChains()[i]);
232
      // If this action is used to forward single dependency, set the toolchain.
233
0
      if (DDeps.getActions().size() == 1)
234
0
        OffloadingToolChain = DDeps.getToolChains()[i];
235
0
    }
236
0
  }
237
0
}
238
239
0
void OffloadAction::doOnHostDependence(const OffloadActionWorkTy &Work) const {
240
0
  if (!HostTC)
241
0
    return;
242
0
  assert(!getInputs().empty() && "No dependencies for offload action??");
243
0
  auto *A = getInputs().front();
244
0
  Work(A, HostTC, A->getOffloadingArch());
245
0
}
246
247
void OffloadAction::doOnEachDeviceDependence(
248
0
    const OffloadActionWorkTy &Work) const {
249
0
  auto I = getInputs().begin();
250
0
  auto E = getInputs().end();
251
0
  if (I == E)
252
0
    return;
253
254
  // We expect to have the same number of input dependences and device tool
255
  // chains, except if we also have a host dependence. In that case we have one
256
  // more dependence than we have device tool chains.
257
0
  assert(getInputs().size() == DevToolChains.size() + (HostTC ? 1 : 0) &&
258
0
         "Sizes of action dependences and toolchains are not consistent!");
259
260
  // Skip host action
261
0
  if (HostTC)
262
0
    ++I;
263
264
0
  auto TI = DevToolChains.begin();
265
0
  for (; I != E; ++I, ++TI)
266
0
    Work(*I, *TI, (*I)->getOffloadingArch());
267
0
}
268
269
0
void OffloadAction::doOnEachDependence(const OffloadActionWorkTy &Work) const {
270
0
  doOnHostDependence(Work);
271
0
  doOnEachDeviceDependence(Work);
272
0
}
273
274
void OffloadAction::doOnEachDependence(bool IsHostDependence,
275
0
                                       const OffloadActionWorkTy &Work) const {
276
0
  if (IsHostDependence)
277
0
    doOnHostDependence(Work);
278
0
  else
279
0
    doOnEachDeviceDependence(Work);
280
0
}
281
282
0
bool OffloadAction::hasHostDependence() const { return HostTC != nullptr; }
283
284
0
Action *OffloadAction::getHostDependence() const {
285
0
  assert(hasHostDependence() && "Host dependence does not exist!");
286
0
  assert(!getInputs().empty() && "No dependencies for offload action??");
287
0
  return HostTC ? getInputs().front() : nullptr;
288
0
}
289
290
bool OffloadAction::hasSingleDeviceDependence(
291
0
    bool DoNotConsiderHostActions) const {
292
0
  if (DoNotConsiderHostActions)
293
0
    return getInputs().size() == (HostTC ? 2 : 1);
294
0
  return !HostTC && getInputs().size() == 1;
295
0
}
296
297
Action *
298
0
OffloadAction::getSingleDeviceDependence(bool DoNotConsiderHostActions) const {
299
0
  assert(hasSingleDeviceDependence(DoNotConsiderHostActions) &&
300
0
         "Single device dependence does not exist!");
301
  // The previous assert ensures the number of entries in getInputs() is
302
  // consistent with what we are doing here.
303
0
  return HostTC ? getInputs()[1] : getInputs().front();
304
0
}
305
306
void OffloadAction::DeviceDependences::add(Action &A, const ToolChain &TC,
307
                                           const char *BoundArch,
308
0
                                           OffloadKind OKind) {
309
0
  DeviceActions.push_back(&A);
310
0
  DeviceToolChains.push_back(&TC);
311
0
  DeviceBoundArchs.push_back(BoundArch);
312
0
  DeviceOffloadKinds.push_back(OKind);
313
0
}
314
315
void OffloadAction::DeviceDependences::add(Action &A, const ToolChain &TC,
316
                                           const char *BoundArch,
317
0
                                           unsigned OffloadKindMask) {
318
0
  DeviceActions.push_back(&A);
319
0
  DeviceToolChains.push_back(&TC);
320
0
  DeviceBoundArchs.push_back(BoundArch);
321
322
  // Add each active offloading kind from a mask.
323
0
  for (OffloadKind OKind : {OFK_OpenMP, OFK_Cuda, OFK_HIP})
324
0
    if (OKind & OffloadKindMask)
325
0
      DeviceOffloadKinds.push_back(OKind);
326
0
}
327
328
OffloadAction::HostDependence::HostDependence(Action &A, const ToolChain &TC,
329
                                              const char *BoundArch,
330
                                              const DeviceDependences &DDeps)
331
0
    : HostAction(A), HostToolChain(TC), HostBoundArch(BoundArch) {
332
0
  for (auto K : DDeps.getOffloadKinds())
333
0
    HostOffloadKinds |= K;
334
0
}
335
336
0
void JobAction::anchor() {}
337
338
JobAction::JobAction(ActionClass Kind, Action *Input, types::ID Type)
339
0
    : Action(Kind, Input, Type) {}
340
341
JobAction::JobAction(ActionClass Kind, const ActionList &Inputs, types::ID Type)
342
0
    : Action(Kind, Inputs, Type) {}
343
344
0
void PreprocessJobAction::anchor() {}
345
346
PreprocessJobAction::PreprocessJobAction(Action *Input, types::ID OutputType)
347
0
    : JobAction(PreprocessJobClass, Input, OutputType) {}
348
349
0
void PrecompileJobAction::anchor() {}
350
351
PrecompileJobAction::PrecompileJobAction(Action *Input, types::ID OutputType)
352
0
    : JobAction(PrecompileJobClass, Input, OutputType) {}
353
354
PrecompileJobAction::PrecompileJobAction(ActionClass Kind, Action *Input,
355
                                         types::ID OutputType)
356
0
    : JobAction(Kind, Input, OutputType) {
357
0
  assert(isa<PrecompileJobAction>((Action*)this) && "invalid action kind");
358
0
}
359
360
0
void ExtractAPIJobAction::anchor() {}
361
362
ExtractAPIJobAction::ExtractAPIJobAction(Action *Inputs, types::ID OutputType)
363
0
    : JobAction(ExtractAPIJobClass, Inputs, OutputType) {}
364
365
0
void AnalyzeJobAction::anchor() {}
366
367
AnalyzeJobAction::AnalyzeJobAction(Action *Input, types::ID OutputType)
368
0
    : JobAction(AnalyzeJobClass, Input, OutputType) {}
369
370
0
void MigrateJobAction::anchor() {}
371
372
MigrateJobAction::MigrateJobAction(Action *Input, types::ID OutputType)
373
0
    : JobAction(MigrateJobClass, Input, OutputType) {}
374
375
0
void CompileJobAction::anchor() {}
376
377
CompileJobAction::CompileJobAction(Action *Input, types::ID OutputType)
378
0
    : JobAction(CompileJobClass, Input, OutputType) {}
379
380
0
void BackendJobAction::anchor() {}
381
382
BackendJobAction::BackendJobAction(Action *Input, types::ID OutputType)
383
0
    : JobAction(BackendJobClass, Input, OutputType) {}
384
385
0
void AssembleJobAction::anchor() {}
386
387
AssembleJobAction::AssembleJobAction(Action *Input, types::ID OutputType)
388
0
    : JobAction(AssembleJobClass, Input, OutputType) {}
389
390
0
void IfsMergeJobAction::anchor() {}
391
392
IfsMergeJobAction::IfsMergeJobAction(ActionList &Inputs, types::ID Type)
393
0
    : JobAction(IfsMergeJobClass, Inputs, Type) {}
394
395
0
void LinkJobAction::anchor() {}
396
397
LinkJobAction::LinkJobAction(ActionList &Inputs, types::ID Type)
398
0
    : JobAction(LinkJobClass, Inputs, Type) {}
399
400
0
void LipoJobAction::anchor() {}
401
402
LipoJobAction::LipoJobAction(ActionList &Inputs, types::ID Type)
403
0
    : JobAction(LipoJobClass, Inputs, Type) {}
404
405
0
void DsymutilJobAction::anchor() {}
406
407
DsymutilJobAction::DsymutilJobAction(ActionList &Inputs, types::ID Type)
408
0
    : JobAction(DsymutilJobClass, Inputs, Type) {}
409
410
0
void VerifyJobAction::anchor() {}
411
412
VerifyJobAction::VerifyJobAction(ActionClass Kind, Action *Input,
413
                                 types::ID Type)
414
0
    : JobAction(Kind, Input, Type) {
415
0
  assert((Kind == VerifyDebugInfoJobClass || Kind == VerifyPCHJobClass) &&
416
0
         "ActionClass is not a valid VerifyJobAction");
417
0
}
418
419
0
void VerifyDebugInfoJobAction::anchor() {}
420
421
VerifyDebugInfoJobAction::VerifyDebugInfoJobAction(Action *Input,
422
                                                   types::ID Type)
423
0
    : VerifyJobAction(VerifyDebugInfoJobClass, Input, Type) {}
424
425
0
void VerifyPCHJobAction::anchor() {}
426
427
VerifyPCHJobAction::VerifyPCHJobAction(Action *Input, types::ID Type)
428
0
    : VerifyJobAction(VerifyPCHJobClass, Input, Type) {}
429
430
0
void OffloadBundlingJobAction::anchor() {}
431
432
OffloadBundlingJobAction::OffloadBundlingJobAction(ActionList &Inputs)
433
0
    : JobAction(OffloadBundlingJobClass, Inputs, Inputs.back()->getType()) {}
434
435
0
void OffloadUnbundlingJobAction::anchor() {}
436
437
OffloadUnbundlingJobAction::OffloadUnbundlingJobAction(Action *Input)
438
0
    : JobAction(OffloadUnbundlingJobClass, Input, Input->getType()) {}
439
440
0
void OffloadPackagerJobAction::anchor() {}
441
442
OffloadPackagerJobAction::OffloadPackagerJobAction(ActionList &Inputs,
443
                                                   types::ID Type)
444
0
    : JobAction(OffloadPackagerJobClass, Inputs, Type) {}
445
446
0
void LinkerWrapperJobAction::anchor() {}
447
448
LinkerWrapperJobAction::LinkerWrapperJobAction(ActionList &Inputs,
449
                                               types::ID Type)
450
0
    : JobAction(LinkerWrapperJobClass, Inputs, Type) {}
451
452
0
void StaticLibJobAction::anchor() {}
453
454
StaticLibJobAction::StaticLibJobAction(ActionList &Inputs, types::ID Type)
455
0
    : JobAction(StaticLibJobClass, Inputs, Type) {}
456
457
0
void BinaryAnalyzeJobAction::anchor() {}
458
459
BinaryAnalyzeJobAction::BinaryAnalyzeJobAction(Action *Input, types::ID Type)
460
0
    : JobAction(BinaryAnalyzeJobClass, Input, Type) {}