blob: 85e466a4409d8e127408fc4a650b47babcfa297e [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 }
Simon Pilgrim3315a6a2016-10-28 10:09:35 +0000149
150 llvm_unreachable("invalid offload kind");
Samuel Antao7cab8f12016-10-27 18:04:42 +0000151}
152
David Blaikie68e081d2011-12-20 02:48:34 +0000153void InputAction::anchor() {}
154
Mike Stump11289f42009-09-09 15:08:12 +0000155InputAction::InputAction(const Arg &_Input, types::ID _Type)
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000156 : Action(InputClass, _Type), Input(_Input) {
157}
158
David Blaikie68e081d2011-12-20 02:48:34 +0000159void BindArchAction::anchor() {}
160
Mehdi Amini087f1fb2016-10-07 22:03:03 +0000161BindArchAction::BindArchAction(Action *Input, llvm::StringRef ArchName)
162 : Action(BindArchClass, Input), ArchName(ArchName) {}
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000163
Samuel Antaod06239d2016-07-15 23:13:27 +0000164void OffloadAction::anchor() {}
Artem Belevich0ff05cd2015-07-13 23:27:56 +0000165
Samuel Antaod06239d2016-07-15 23:13:27 +0000166OffloadAction::OffloadAction(const HostDependence &HDep)
167 : Action(OffloadClass, HDep.getAction()), HostTC(HDep.getToolChain()) {
168 OffloadingArch = HDep.getBoundArch();
169 ActiveOffloadKindMask = HDep.getOffloadKinds();
170 HDep.getAction()->propagateHostOffloadInfo(HDep.getOffloadKinds(),
171 HDep.getBoundArch());
Eric Christopher2a7248f2016-07-16 00:58:34 +0000172}
Artem Belevich0ff05cd2015-07-13 23:27:56 +0000173
Samuel Antaod06239d2016-07-15 23:13:27 +0000174OffloadAction::OffloadAction(const DeviceDependences &DDeps, types::ID Ty)
175 : Action(OffloadClass, DDeps.getActions(), Ty),
176 DevToolChains(DDeps.getToolChains()) {
177 auto &OKinds = DDeps.getOffloadKinds();
178 auto &BArchs = DDeps.getBoundArchs();
Artem Belevich0ff05cd2015-07-13 23:27:56 +0000179
Samuel Antaod06239d2016-07-15 23:13:27 +0000180 // If all inputs agree on the same kind, use it also for this action.
181 if (llvm::all_of(OKinds, [&](OffloadKind K) { return K == OKinds.front(); }))
182 OffloadingDeviceKind = OKinds.front();
183
184 // If we have a single dependency, inherit the architecture from it.
185 if (OKinds.size() == 1)
186 OffloadingArch = BArchs.front();
187
188 // Propagate info to the dependencies.
189 for (unsigned i = 0, e = getInputs().size(); i != e; ++i)
190 getInputs()[i]->propagateDeviceOffloadInfo(OKinds[i], BArchs[i]);
191}
192
193OffloadAction::OffloadAction(const HostDependence &HDep,
194 const DeviceDependences &DDeps)
195 : Action(OffloadClass, HDep.getAction()), HostTC(HDep.getToolChain()),
196 DevToolChains(DDeps.getToolChains()) {
197 // We use the kinds of the host dependence for this action.
198 OffloadingArch = HDep.getBoundArch();
199 ActiveOffloadKindMask = HDep.getOffloadKinds();
200 HDep.getAction()->propagateHostOffloadInfo(HDep.getOffloadKinds(),
201 HDep.getBoundArch());
202
203 // Add device inputs and propagate info to the device actions. Do work only if
204 // we have dependencies.
205 for (unsigned i = 0, e = DDeps.getActions().size(); i != e; ++i)
206 if (auto *A = DDeps.getActions()[i]) {
207 getInputs().push_back(A);
208 A->propagateDeviceOffloadInfo(DDeps.getOffloadKinds()[i],
209 DDeps.getBoundArchs()[i]);
210 }
211}
212
213void OffloadAction::doOnHostDependence(const OffloadActionWorkTy &Work) const {
214 if (!HostTC)
215 return;
216 assert(!getInputs().empty() && "No dependencies for offload action??");
217 auto *A = getInputs().front();
218 Work(A, HostTC, A->getOffloadingArch());
219}
220
221void OffloadAction::doOnEachDeviceDependence(
222 const OffloadActionWorkTy &Work) const {
223 auto I = getInputs().begin();
224 auto E = getInputs().end();
225 if (I == E)
226 return;
227
228 // We expect to have the same number of input dependences and device tool
229 // chains, except if we also have a host dependence. In that case we have one
230 // more dependence than we have device tool chains.
231 assert(getInputs().size() == DevToolChains.size() + (HostTC ? 1 : 0) &&
232 "Sizes of action dependences and toolchains are not consistent!");
233
234 // Skip host action
235 if (HostTC)
236 ++I;
237
238 auto TI = DevToolChains.begin();
239 for (; I != E; ++I, ++TI)
240 Work(*I, *TI, (*I)->getOffloadingArch());
241}
242
243void OffloadAction::doOnEachDependence(const OffloadActionWorkTy &Work) const {
244 doOnHostDependence(Work);
245 doOnEachDeviceDependence(Work);
246}
247
248void OffloadAction::doOnEachDependence(bool IsHostDependence,
249 const OffloadActionWorkTy &Work) const {
250 if (IsHostDependence)
251 doOnHostDependence(Work);
252 else
253 doOnEachDeviceDependence(Work);
254}
255
256bool OffloadAction::hasHostDependence() const { return HostTC != nullptr; }
257
258Action *OffloadAction::getHostDependence() const {
259 assert(hasHostDependence() && "Host dependence does not exist!");
260 assert(!getInputs().empty() && "No dependencies for offload action??");
261 return HostTC ? getInputs().front() : nullptr;
262}
263
264bool OffloadAction::hasSingleDeviceDependence(
265 bool DoNotConsiderHostActions) const {
266 if (DoNotConsiderHostActions)
267 return getInputs().size() == (HostTC ? 2 : 1);
268 return !HostTC && getInputs().size() == 1;
269}
270
271Action *
272OffloadAction::getSingleDeviceDependence(bool DoNotConsiderHostActions) const {
273 assert(hasSingleDeviceDependence(DoNotConsiderHostActions) &&
274 "Single device dependence does not exist!");
275 // The previous assert ensures the number of entries in getInputs() is
276 // consistent with what we are doing here.
277 return HostTC ? getInputs()[1] : getInputs().front();
278}
279
280void OffloadAction::DeviceDependences::add(Action &A, const ToolChain &TC,
281 const char *BoundArch,
282 OffloadKind OKind) {
283 DeviceActions.push_back(&A);
284 DeviceToolChains.push_back(&TC);
285 DeviceBoundArchs.push_back(BoundArch);
286 DeviceOffloadKinds.push_back(OKind);
287}
288
289OffloadAction::HostDependence::HostDependence(Action &A, const ToolChain &TC,
290 const char *BoundArch,
291 const DeviceDependences &DDeps)
292 : HostAction(A), HostToolChain(TC), HostBoundArch(BoundArch) {
293 for (auto K : DDeps.getOffloadKinds())
294 HostOffloadKinds |= K;
295}
Artem Belevich0ff05cd2015-07-13 23:27:56 +0000296
David Blaikie68e081d2011-12-20 02:48:34 +0000297void JobAction::anchor() {}
298
Justin Lebar41094612016-01-11 23:07:27 +0000299JobAction::JobAction(ActionClass Kind, Action *Input, types::ID Type)
300 : Action(Kind, Input, Type) {}
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000301
Mike Stump11289f42009-09-09 15:08:12 +0000302JobAction::JobAction(ActionClass Kind, const ActionList &Inputs, types::ID Type)
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000303 : Action(Kind, Inputs, Type) {
304}
305
David Blaikie68e081d2011-12-20 02:48:34 +0000306void PreprocessJobAction::anchor() {}
307
Justin Lebar41094612016-01-11 23:07:27 +0000308PreprocessJobAction::PreprocessJobAction(Action *Input, types::ID OutputType)
309 : JobAction(PreprocessJobClass, Input, OutputType) {}
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000310
David Blaikie68e081d2011-12-20 02:48:34 +0000311void PrecompileJobAction::anchor() {}
312
Justin Lebar41094612016-01-11 23:07:27 +0000313PrecompileJobAction::PrecompileJobAction(Action *Input, types::ID OutputType)
314 : JobAction(PrecompileJobClass, Input, OutputType) {}
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000315
David Blaikie68e081d2011-12-20 02:48:34 +0000316void AnalyzeJobAction::anchor() {}
317
Justin Lebar41094612016-01-11 23:07:27 +0000318AnalyzeJobAction::AnalyzeJobAction(Action *Input, types::ID OutputType)
319 : JobAction(AnalyzeJobClass, Input, OutputType) {}
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000320
Ted Kremenekf7639e12012-03-06 20:06:33 +0000321void MigrateJobAction::anchor() {}
322
Justin Lebar41094612016-01-11 23:07:27 +0000323MigrateJobAction::MigrateJobAction(Action *Input, types::ID OutputType)
324 : JobAction(MigrateJobClass, Input, OutputType) {}
Ted Kremenekf7639e12012-03-06 20:06:33 +0000325
David Blaikie68e081d2011-12-20 02:48:34 +0000326void CompileJobAction::anchor() {}
327
Justin Lebar41094612016-01-11 23:07:27 +0000328CompileJobAction::CompileJobAction(Action *Input, types::ID OutputType)
329 : JobAction(CompileJobClass, Input, OutputType) {}
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000330
Bob Wilson23a55f12014-12-21 07:00:00 +0000331void BackendJobAction::anchor() {}
332
Justin Lebar41094612016-01-11 23:07:27 +0000333BackendJobAction::BackendJobAction(Action *Input, types::ID OutputType)
334 : JobAction(BackendJobClass, Input, OutputType) {}
Bob Wilson23a55f12014-12-21 07:00:00 +0000335
David Blaikie68e081d2011-12-20 02:48:34 +0000336void AssembleJobAction::anchor() {}
337
Justin Lebar41094612016-01-11 23:07:27 +0000338AssembleJobAction::AssembleJobAction(Action *Input, types::ID OutputType)
339 : JobAction(AssembleJobClass, Input, OutputType) {}
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000340
David Blaikie68e081d2011-12-20 02:48:34 +0000341void LinkJobAction::anchor() {}
342
Mike Stump11289f42009-09-09 15:08:12 +0000343LinkJobAction::LinkJobAction(ActionList &Inputs, types::ID Type)
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000344 : JobAction(LinkJobClass, Inputs, Type) {
345}
346
David Blaikie68e081d2011-12-20 02:48:34 +0000347void LipoJobAction::anchor() {}
348
Mike Stump11289f42009-09-09 15:08:12 +0000349LipoJobAction::LipoJobAction(ActionList &Inputs, types::ID Type)
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000350 : JobAction(LipoJobClass, Inputs, Type) {
351}
Daniel Dunbar88299622010-06-04 18:28:36 +0000352
David Blaikie68e081d2011-12-20 02:48:34 +0000353void DsymutilJobAction::anchor() {}
354
Daniel Dunbar88299622010-06-04 18:28:36 +0000355DsymutilJobAction::DsymutilJobAction(ActionList &Inputs, types::ID Type)
356 : JobAction(DsymutilJobClass, Inputs, Type) {
357}
Eric Christopher551ef452011-08-23 17:56:55 +0000358
David Blaikie68e081d2011-12-20 02:48:34 +0000359void VerifyJobAction::anchor() {}
360
Justin Lebar41094612016-01-11 23:07:27 +0000361VerifyJobAction::VerifyJobAction(ActionClass Kind, Action *Input,
362 types::ID Type)
363 : JobAction(Kind, Input, Type) {
Ben Langmuir9b9a8d32014-02-06 18:53:25 +0000364 assert((Kind == VerifyDebugInfoJobClass || Kind == VerifyPCHJobClass) &&
365 "ActionClass is not a valid VerifyJobAction");
366}
367
Ben Langmuir9b9a8d32014-02-06 18:53:25 +0000368void VerifyDebugInfoJobAction::anchor() {}
369
Justin Lebar41094612016-01-11 23:07:27 +0000370VerifyDebugInfoJobAction::VerifyDebugInfoJobAction(Action *Input,
371 types::ID Type)
372 : VerifyJobAction(VerifyDebugInfoJobClass, Input, Type) {}
Ben Langmuir9b9a8d32014-02-06 18:53:25 +0000373
374void VerifyPCHJobAction::anchor() {}
375
Justin Lebar41094612016-01-11 23:07:27 +0000376VerifyPCHJobAction::VerifyPCHJobAction(Action *Input, types::ID Type)
377 : VerifyJobAction(VerifyPCHJobClass, Input, Type) {}
Samuel Antao69d6f312016-10-27 17:50:43 +0000378
379void OffloadBundlingJobAction::anchor() {}
380
381OffloadBundlingJobAction::OffloadBundlingJobAction(ActionList &Inputs)
382 : JobAction(OffloadBundlingJobClass, Inputs, Inputs.front()->getType()) {}
Samuel Antaofab4f372016-10-27 18:00:51 +0000383
384void OffloadUnbundlingJobAction::anchor() {}
385
386OffloadUnbundlingJobAction::OffloadUnbundlingJobAction(Action *Input)
387 : JobAction(OffloadUnbundlingJobClass, Input, Input->getType()) {}