blob: 4e0c224c3b1275d68f3eab3716b5ea3d0283f842 [file] [log] [blame]
Nick Lewycky6da90772010-12-31 17:31:54 +00001//===--- Action.cpp - Abstract compilation steps --------------------------===//
Daniel Dunbarf479c122009-03-12 18:40:18 +00002//
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#include "clang/Driver/Action.h"
Samuel Antaod06239d2016-07-15 23:13:27 +000011#include "clang/Driver/ToolChain.h"
Samuel Antao7cab8f12016-10-27 18:04:42 +000012#include "llvm/ADT/StringRef.h"
Justin Lebar29bfa892016-01-12 22:23:04 +000013#include "llvm/ADT/StringSwitch.h"
David Blaikie79000202011-09-23 05:57:42 +000014#include "llvm/Support/ErrorHandling.h"
Justin Lebar7bf77982016-01-11 23:27:13 +000015#include "llvm/Support/Regex.h"
Daniel Dunbarf479c122009-03-12 18:40:18 +000016#include <cassert>
17using namespace clang::driver;
Reid Kleckner898229a2013-06-14 17:17:23 +000018using namespace llvm::opt;
Daniel Dunbarf479c122009-03-12 18:40:18 +000019
Justin Lebar41094612016-01-11 23:07:27 +000020Action::~Action() {}
Daniel Dunbar80665fb2009-03-13 12:17:08 +000021
22const char *Action::getClassName(ActionClass AC) {
23 switch (AC) {
24 case InputClass: return "input";
25 case BindArchClass: return "bind-arch";
Samuel Antaod06239d2016-07-15 23:13:27 +000026 case OffloadClass:
27 return "offload";
Daniel Dunbar7326ad52009-03-13 17:52:07 +000028 case PreprocessJobClass: return "preprocessor";
29 case PrecompileJobClass: return "precompiler";
30 case AnalyzeJobClass: return "analyzer";
Ted Kremenekf7639e12012-03-06 20:06:33 +000031 case MigrateJobClass: return "migrator";
Daniel Dunbar7326ad52009-03-13 17:52:07 +000032 case CompileJobClass: return "compiler";
Bob Wilson23a55f12014-12-21 07:00:00 +000033 case BackendJobClass: return "backend";
Daniel Dunbar7326ad52009-03-13 17:52:07 +000034 case AssembleJobClass: return "assembler";
35 case LinkJobClass: return "linker";
Daniel Dunbar80665fb2009-03-13 12:17:08 +000036 case LipoJobClass: return "lipo";
Daniel Dunbar88299622010-06-04 18:28:36 +000037 case DsymutilJobClass: return "dsymutil";
Ben Langmuir9b9a8d32014-02-06 18:53:25 +000038 case VerifyDebugInfoJobClass: return "verify-debug-info";
39 case VerifyPCHJobClass: return "verify-pch";
Samuel Antao69d6f312016-10-27 17:50:43 +000040 case OffloadBundlingJobClass:
41 return "clang-offload-bundler";
Samuel Antaofab4f372016-10-27 18:00:51 +000042 case OffloadUnbundlingJobClass:
43 return "clang-offload-unbundler";
Daniel Dunbar80665fb2009-03-13 12:17:08 +000044 }
Mike Stump11289f42009-09-09 15:08:12 +000045
David Blaikie83d382b2011-09-23 05:06:16 +000046 llvm_unreachable("invalid class");
Daniel Dunbar80665fb2009-03-13 12:17:08 +000047}
Daniel Dunbar3f261ff2009-03-13 23:08:03 +000048
Samuel Antaod06239d2016-07-15 23:13:27 +000049void Action::propagateDeviceOffloadInfo(OffloadKind OKind, const char *OArch) {
50 // Offload action set its own kinds on their dependences.
51 if (Kind == OffloadClass)
52 return;
Samuel Antaofab4f372016-10-27 18:00:51 +000053 // Unbundling actions use the host kinds.
54 if (Kind == OffloadUnbundlingJobClass)
55 return;
Samuel Antaod06239d2016-07-15 23:13:27 +000056
57 assert((OffloadingDeviceKind == OKind || OffloadingDeviceKind == OFK_None) &&
58 "Setting device kind to a different device??");
59 assert(!ActiveOffloadKindMask && "Setting a device kind in a host action??");
60 OffloadingDeviceKind = OKind;
61 OffloadingArch = OArch;
62
63 for (auto *A : Inputs)
64 A->propagateDeviceOffloadInfo(OffloadingDeviceKind, OArch);
65}
66
67void Action::propagateHostOffloadInfo(unsigned OKinds, const char *OArch) {
68 // Offload action set its own kinds on their dependences.
69 if (Kind == OffloadClass)
70 return;
71
72 assert(OffloadingDeviceKind == OFK_None &&
73 "Setting a host kind in a device action.");
74 ActiveOffloadKindMask |= OKinds;
75 OffloadingArch = OArch;
76
77 for (auto *A : Inputs)
78 A->propagateHostOffloadInfo(ActiveOffloadKindMask, OArch);
79}
80
81void Action::propagateOffloadInfo(const Action *A) {
82 if (unsigned HK = A->getOffloadingHostActiveKinds())
83 propagateHostOffloadInfo(HK, A->getOffloadingArch());
84 else
85 propagateDeviceOffloadInfo(A->getOffloadingDeviceKind(),
86 A->getOffloadingArch());
87}
88
89std::string Action::getOffloadingKindPrefix() const {
90 switch (OffloadingDeviceKind) {
91 case OFK_None:
92 break;
93 case OFK_Host:
94 llvm_unreachable("Host kind is not an offloading device kind.");
95 break;
96 case OFK_Cuda:
97 return "device-cuda";
Samuel Antao39f9da22016-10-27 16:38:05 +000098 case OFK_OpenMP:
99 return "device-openmp";
Samuel Antaod06239d2016-07-15 23:13:27 +0000100
101 // TODO: Add other programming models here.
102 }
103
104 if (!ActiveOffloadKindMask)
105 return "";
106
107 std::string Res("host");
108 if (ActiveOffloadKindMask & OFK_Cuda)
109 Res += "-cuda";
Samuel Antao39f9da22016-10-27 16:38:05 +0000110 if (ActiveOffloadKindMask & OFK_OpenMP)
111 Res += "-openmp";
Samuel Antaod06239d2016-07-15 23:13:27 +0000112
113 // TODO: Add other programming models here.
114
115 return Res;
116}
117
Samuel Antao3b7e38b2016-10-27 18:14:55 +0000118/// Return a string that can be used as prefix in order to generate unique files
119/// for each offloading kind.
Samuel Antaod06239d2016-07-15 23:13:27 +0000120std::string
Samuel Antao3b7e38b2016-10-27 18:14:55 +0000121Action::GetOffloadingFileNamePrefix(OffloadKind Kind,
122 llvm::StringRef NormalizedTriple,
123 bool CreatePrefixForHost) {
124 // Don't generate prefix for host actions unless required.
125 if (!CreatePrefixForHost && (Kind == OFK_None || Kind == OFK_Host))
Samuel Antaod06239d2016-07-15 23:13:27 +0000126 return "";
127
128 std::string Res("-");
Samuel Antao3b7e38b2016-10-27 18:14:55 +0000129 Res += GetOffloadKindName(Kind);
Samuel Antaod06239d2016-07-15 23:13:27 +0000130 Res += "-";
131 Res += NormalizedTriple;
132 return Res;
133}
134
Samuel Antao7cab8f12016-10-27 18:04:42 +0000135/// Return a string with the offload kind name. If that is not defined, we
136/// assume 'host'.
137llvm::StringRef Action::GetOffloadKindName(OffloadKind Kind) {
138 switch (Kind) {
139 case OFK_None:
140 case OFK_Host:
141 return "host";
142 case OFK_Cuda:
143 return "cuda";
144 case OFK_OpenMP:
145 return "openmp";
146
147 // TODO: Add other programming models here.
148 }
149}
150
David Blaikie68e081d2011-12-20 02:48:34 +0000151void InputAction::anchor() {}
152
Mike Stump11289f42009-09-09 15:08:12 +0000153InputAction::InputAction(const Arg &_Input, types::ID _Type)
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000154 : Action(InputClass, _Type), Input(_Input) {
155}
156
David Blaikie68e081d2011-12-20 02:48:34 +0000157void BindArchAction::anchor() {}
158
Mehdi Amini087f1fb2016-10-07 22:03:03 +0000159BindArchAction::BindArchAction(Action *Input, llvm::StringRef ArchName)
160 : Action(BindArchClass, Input), ArchName(ArchName) {}
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000161
Samuel Antaod06239d2016-07-15 23:13:27 +0000162void OffloadAction::anchor() {}
Artem Belevich0ff05cd2015-07-13 23:27:56 +0000163
Samuel Antaod06239d2016-07-15 23:13:27 +0000164OffloadAction::OffloadAction(const HostDependence &HDep)
165 : Action(OffloadClass, HDep.getAction()), HostTC(HDep.getToolChain()) {
166 OffloadingArch = HDep.getBoundArch();
167 ActiveOffloadKindMask = HDep.getOffloadKinds();
168 HDep.getAction()->propagateHostOffloadInfo(HDep.getOffloadKinds(),
169 HDep.getBoundArch());
Eric Christopher2a7248f2016-07-16 00:58:34 +0000170}
Artem Belevich0ff05cd2015-07-13 23:27:56 +0000171
Samuel Antaod06239d2016-07-15 23:13:27 +0000172OffloadAction::OffloadAction(const DeviceDependences &DDeps, types::ID Ty)
173 : Action(OffloadClass, DDeps.getActions(), Ty),
174 DevToolChains(DDeps.getToolChains()) {
175 auto &OKinds = DDeps.getOffloadKinds();
176 auto &BArchs = DDeps.getBoundArchs();
Artem Belevich0ff05cd2015-07-13 23:27:56 +0000177
Samuel Antaod06239d2016-07-15 23:13:27 +0000178 // If all inputs agree on the same kind, use it also for this action.
179 if (llvm::all_of(OKinds, [&](OffloadKind K) { return K == OKinds.front(); }))
180 OffloadingDeviceKind = OKinds.front();
181
182 // If we have a single dependency, inherit the architecture from it.
183 if (OKinds.size() == 1)
184 OffloadingArch = BArchs.front();
185
186 // Propagate info to the dependencies.
187 for (unsigned i = 0, e = getInputs().size(); i != e; ++i)
188 getInputs()[i]->propagateDeviceOffloadInfo(OKinds[i], BArchs[i]);
189}
190
191OffloadAction::OffloadAction(const HostDependence &HDep,
192 const DeviceDependences &DDeps)
193 : Action(OffloadClass, HDep.getAction()), HostTC(HDep.getToolChain()),
194 DevToolChains(DDeps.getToolChains()) {
195 // We use the kinds of the host dependence for this action.
196 OffloadingArch = HDep.getBoundArch();
197 ActiveOffloadKindMask = HDep.getOffloadKinds();
198 HDep.getAction()->propagateHostOffloadInfo(HDep.getOffloadKinds(),
199 HDep.getBoundArch());
200
201 // Add device inputs and propagate info to the device actions. Do work only if
202 // we have dependencies.
203 for (unsigned i = 0, e = DDeps.getActions().size(); i != e; ++i)
204 if (auto *A = DDeps.getActions()[i]) {
205 getInputs().push_back(A);
206 A->propagateDeviceOffloadInfo(DDeps.getOffloadKinds()[i],
207 DDeps.getBoundArchs()[i]);
208 }
209}
210
211void OffloadAction::doOnHostDependence(const OffloadActionWorkTy &Work) const {
212 if (!HostTC)
213 return;
214 assert(!getInputs().empty() && "No dependencies for offload action??");
215 auto *A = getInputs().front();
216 Work(A, HostTC, A->getOffloadingArch());
217}
218
219void OffloadAction::doOnEachDeviceDependence(
220 const OffloadActionWorkTy &Work) const {
221 auto I = getInputs().begin();
222 auto E = getInputs().end();
223 if (I == E)
224 return;
225
226 // We expect to have the same number of input dependences and device tool
227 // chains, except if we also have a host dependence. In that case we have one
228 // more dependence than we have device tool chains.
229 assert(getInputs().size() == DevToolChains.size() + (HostTC ? 1 : 0) &&
230 "Sizes of action dependences and toolchains are not consistent!");
231
232 // Skip host action
233 if (HostTC)
234 ++I;
235
236 auto TI = DevToolChains.begin();
237 for (; I != E; ++I, ++TI)
238 Work(*I, *TI, (*I)->getOffloadingArch());
239}
240
241void OffloadAction::doOnEachDependence(const OffloadActionWorkTy &Work) const {
242 doOnHostDependence(Work);
243 doOnEachDeviceDependence(Work);
244}
245
246void OffloadAction::doOnEachDependence(bool IsHostDependence,
247 const OffloadActionWorkTy &Work) const {
248 if (IsHostDependence)
249 doOnHostDependence(Work);
250 else
251 doOnEachDeviceDependence(Work);
252}
253
254bool OffloadAction::hasHostDependence() const { return HostTC != nullptr; }
255
256Action *OffloadAction::getHostDependence() const {
257 assert(hasHostDependence() && "Host dependence does not exist!");
258 assert(!getInputs().empty() && "No dependencies for offload action??");
259 return HostTC ? getInputs().front() : nullptr;
260}
261
262bool OffloadAction::hasSingleDeviceDependence(
263 bool DoNotConsiderHostActions) const {
264 if (DoNotConsiderHostActions)
265 return getInputs().size() == (HostTC ? 2 : 1);
266 return !HostTC && getInputs().size() == 1;
267}
268
269Action *
270OffloadAction::getSingleDeviceDependence(bool DoNotConsiderHostActions) const {
271 assert(hasSingleDeviceDependence(DoNotConsiderHostActions) &&
272 "Single device dependence does not exist!");
273 // The previous assert ensures the number of entries in getInputs() is
274 // consistent with what we are doing here.
275 return HostTC ? getInputs()[1] : getInputs().front();
276}
277
278void OffloadAction::DeviceDependences::add(Action &A, const ToolChain &TC,
279 const char *BoundArch,
280 OffloadKind OKind) {
281 DeviceActions.push_back(&A);
282 DeviceToolChains.push_back(&TC);
283 DeviceBoundArchs.push_back(BoundArch);
284 DeviceOffloadKinds.push_back(OKind);
285}
286
287OffloadAction::HostDependence::HostDependence(Action &A, const ToolChain &TC,
288 const char *BoundArch,
289 const DeviceDependences &DDeps)
290 : HostAction(A), HostToolChain(TC), HostBoundArch(BoundArch) {
291 for (auto K : DDeps.getOffloadKinds())
292 HostOffloadKinds |= K;
293}
Artem Belevich0ff05cd2015-07-13 23:27:56 +0000294
David Blaikie68e081d2011-12-20 02:48:34 +0000295void JobAction::anchor() {}
296
Justin Lebar41094612016-01-11 23:07:27 +0000297JobAction::JobAction(ActionClass Kind, Action *Input, types::ID Type)
298 : Action(Kind, Input, Type) {}
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000299
Mike Stump11289f42009-09-09 15:08:12 +0000300JobAction::JobAction(ActionClass Kind, const ActionList &Inputs, types::ID Type)
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000301 : Action(Kind, Inputs, Type) {
302}
303
David Blaikie68e081d2011-12-20 02:48:34 +0000304void PreprocessJobAction::anchor() {}
305
Justin Lebar41094612016-01-11 23:07:27 +0000306PreprocessJobAction::PreprocessJobAction(Action *Input, types::ID OutputType)
307 : JobAction(PreprocessJobClass, Input, OutputType) {}
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000308
David Blaikie68e081d2011-12-20 02:48:34 +0000309void PrecompileJobAction::anchor() {}
310
Justin Lebar41094612016-01-11 23:07:27 +0000311PrecompileJobAction::PrecompileJobAction(Action *Input, types::ID OutputType)
312 : JobAction(PrecompileJobClass, Input, OutputType) {}
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000313
David Blaikie68e081d2011-12-20 02:48:34 +0000314void AnalyzeJobAction::anchor() {}
315
Justin Lebar41094612016-01-11 23:07:27 +0000316AnalyzeJobAction::AnalyzeJobAction(Action *Input, types::ID OutputType)
317 : JobAction(AnalyzeJobClass, Input, OutputType) {}
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000318
Ted Kremenekf7639e12012-03-06 20:06:33 +0000319void MigrateJobAction::anchor() {}
320
Justin Lebar41094612016-01-11 23:07:27 +0000321MigrateJobAction::MigrateJobAction(Action *Input, types::ID OutputType)
322 : JobAction(MigrateJobClass, Input, OutputType) {}
Ted Kremenekf7639e12012-03-06 20:06:33 +0000323
David Blaikie68e081d2011-12-20 02:48:34 +0000324void CompileJobAction::anchor() {}
325
Justin Lebar41094612016-01-11 23:07:27 +0000326CompileJobAction::CompileJobAction(Action *Input, types::ID OutputType)
327 : JobAction(CompileJobClass, Input, OutputType) {}
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000328
Bob Wilson23a55f12014-12-21 07:00:00 +0000329void BackendJobAction::anchor() {}
330
Justin Lebar41094612016-01-11 23:07:27 +0000331BackendJobAction::BackendJobAction(Action *Input, types::ID OutputType)
332 : JobAction(BackendJobClass, Input, OutputType) {}
Bob Wilson23a55f12014-12-21 07:00:00 +0000333
David Blaikie68e081d2011-12-20 02:48:34 +0000334void AssembleJobAction::anchor() {}
335
Justin Lebar41094612016-01-11 23:07:27 +0000336AssembleJobAction::AssembleJobAction(Action *Input, types::ID OutputType)
337 : JobAction(AssembleJobClass, Input, OutputType) {}
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000338
David Blaikie68e081d2011-12-20 02:48:34 +0000339void LinkJobAction::anchor() {}
340
Mike Stump11289f42009-09-09 15:08:12 +0000341LinkJobAction::LinkJobAction(ActionList &Inputs, types::ID Type)
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000342 : JobAction(LinkJobClass, Inputs, Type) {
343}
344
David Blaikie68e081d2011-12-20 02:48:34 +0000345void LipoJobAction::anchor() {}
346
Mike Stump11289f42009-09-09 15:08:12 +0000347LipoJobAction::LipoJobAction(ActionList &Inputs, types::ID Type)
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000348 : JobAction(LipoJobClass, Inputs, Type) {
349}
Daniel Dunbar88299622010-06-04 18:28:36 +0000350
David Blaikie68e081d2011-12-20 02:48:34 +0000351void DsymutilJobAction::anchor() {}
352
Daniel Dunbar88299622010-06-04 18:28:36 +0000353DsymutilJobAction::DsymutilJobAction(ActionList &Inputs, types::ID Type)
354 : JobAction(DsymutilJobClass, Inputs, Type) {
355}
Eric Christopher551ef452011-08-23 17:56:55 +0000356
David Blaikie68e081d2011-12-20 02:48:34 +0000357void VerifyJobAction::anchor() {}
358
Justin Lebar41094612016-01-11 23:07:27 +0000359VerifyJobAction::VerifyJobAction(ActionClass Kind, Action *Input,
360 types::ID Type)
361 : JobAction(Kind, Input, Type) {
Ben Langmuir9b9a8d32014-02-06 18:53:25 +0000362 assert((Kind == VerifyDebugInfoJobClass || Kind == VerifyPCHJobClass) &&
363 "ActionClass is not a valid VerifyJobAction");
364}
365
Ben Langmuir9b9a8d32014-02-06 18:53:25 +0000366void VerifyDebugInfoJobAction::anchor() {}
367
Justin Lebar41094612016-01-11 23:07:27 +0000368VerifyDebugInfoJobAction::VerifyDebugInfoJobAction(Action *Input,
369 types::ID Type)
370 : VerifyJobAction(VerifyDebugInfoJobClass, Input, Type) {}
Ben Langmuir9b9a8d32014-02-06 18:53:25 +0000371
372void VerifyPCHJobAction::anchor() {}
373
Justin Lebar41094612016-01-11 23:07:27 +0000374VerifyPCHJobAction::VerifyPCHJobAction(Action *Input, types::ID Type)
375 : VerifyJobAction(VerifyPCHJobClass, Input, Type) {}
Samuel Antao69d6f312016-10-27 17:50:43 +0000376
377void OffloadBundlingJobAction::anchor() {}
378
379OffloadBundlingJobAction::OffloadBundlingJobAction(ActionList &Inputs)
380 : JobAction(OffloadBundlingJobClass, Inputs, Inputs.front()->getType()) {}
Samuel Antaofab4f372016-10-27 18:00:51 +0000381
382void OffloadUnbundlingJobAction::anchor() {}
383
384OffloadUnbundlingJobAction::OffloadUnbundlingJobAction(Action *Input)
385 : JobAction(OffloadUnbundlingJobClass, Input, Input->getType()) {}