blob: 79e3da37baf9b4b49ec3846d63c1d8c5fba09ab2 [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"
Justin Lebar29bfa892016-01-12 22:23:04 +000012#include "llvm/ADT/StringSwitch.h"
David Blaikie79000202011-09-23 05:57:42 +000013#include "llvm/Support/ErrorHandling.h"
Justin Lebar7bf77982016-01-11 23:27:13 +000014#include "llvm/Support/Regex.h"
Daniel Dunbarf479c122009-03-12 18:40:18 +000015#include <cassert>
16using namespace clang::driver;
Reid Kleckner898229a2013-06-14 17:17:23 +000017using namespace llvm::opt;
Daniel Dunbarf479c122009-03-12 18:40:18 +000018
Justin Lebar41094612016-01-11 23:07:27 +000019Action::~Action() {}
Daniel Dunbar80665fb2009-03-13 12:17:08 +000020
21const char *Action::getClassName(ActionClass AC) {
22 switch (AC) {
23 case InputClass: return "input";
24 case BindArchClass: return "bind-arch";
Samuel Antaod06239d2016-07-15 23:13:27 +000025 case OffloadClass:
26 return "offload";
Daniel Dunbar7326ad52009-03-13 17:52:07 +000027 case PreprocessJobClass: return "preprocessor";
28 case PrecompileJobClass: return "precompiler";
29 case AnalyzeJobClass: return "analyzer";
Ted Kremenekf7639e12012-03-06 20:06:33 +000030 case MigrateJobClass: return "migrator";
Daniel Dunbar7326ad52009-03-13 17:52:07 +000031 case CompileJobClass: return "compiler";
Bob Wilson23a55f12014-12-21 07:00:00 +000032 case BackendJobClass: return "backend";
Daniel Dunbar7326ad52009-03-13 17:52:07 +000033 case AssembleJobClass: return "assembler";
34 case LinkJobClass: return "linker";
Daniel Dunbar80665fb2009-03-13 12:17:08 +000035 case LipoJobClass: return "lipo";
Daniel Dunbar88299622010-06-04 18:28:36 +000036 case DsymutilJobClass: return "dsymutil";
Ben Langmuir9b9a8d32014-02-06 18:53:25 +000037 case VerifyDebugInfoJobClass: return "verify-debug-info";
38 case VerifyPCHJobClass: return "verify-pch";
Samuel Antao69d6f312016-10-27 17:50:43 +000039 case OffloadBundlingJobClass:
40 return "clang-offload-bundler";
Samuel Antaofab4f372016-10-27 18:00:51 +000041 case OffloadUnbundlingJobClass:
42 return "clang-offload-unbundler";
Daniel Dunbar80665fb2009-03-13 12:17:08 +000043 }
Mike Stump11289f42009-09-09 15:08:12 +000044
David Blaikie83d382b2011-09-23 05:06:16 +000045 llvm_unreachable("invalid class");
Daniel Dunbar80665fb2009-03-13 12:17:08 +000046}
Daniel Dunbar3f261ff2009-03-13 23:08:03 +000047
Samuel Antaod06239d2016-07-15 23:13:27 +000048void Action::propagateDeviceOffloadInfo(OffloadKind OKind, const char *OArch) {
49 // Offload action set its own kinds on their dependences.
50 if (Kind == OffloadClass)
51 return;
Samuel Antaofab4f372016-10-27 18:00:51 +000052 // Unbundling actions use the host kinds.
53 if (Kind == OffloadUnbundlingJobClass)
54 return;
Samuel Antaod06239d2016-07-15 23:13:27 +000055
56 assert((OffloadingDeviceKind == OKind || OffloadingDeviceKind == OFK_None) &&
57 "Setting device kind to a different device??");
58 assert(!ActiveOffloadKindMask && "Setting a device kind in a host action??");
59 OffloadingDeviceKind = OKind;
60 OffloadingArch = OArch;
61
62 for (auto *A : Inputs)
63 A->propagateDeviceOffloadInfo(OffloadingDeviceKind, OArch);
64}
65
66void Action::propagateHostOffloadInfo(unsigned OKinds, const char *OArch) {
67 // Offload action set its own kinds on their dependences.
68 if (Kind == OffloadClass)
69 return;
70
71 assert(OffloadingDeviceKind == OFK_None &&
72 "Setting a host kind in a device action.");
73 ActiveOffloadKindMask |= OKinds;
74 OffloadingArch = OArch;
75
76 for (auto *A : Inputs)
77 A->propagateHostOffloadInfo(ActiveOffloadKindMask, OArch);
78}
79
80void Action::propagateOffloadInfo(const Action *A) {
81 if (unsigned HK = A->getOffloadingHostActiveKinds())
82 propagateHostOffloadInfo(HK, A->getOffloadingArch());
83 else
84 propagateDeviceOffloadInfo(A->getOffloadingDeviceKind(),
85 A->getOffloadingArch());
86}
87
88std::string Action::getOffloadingKindPrefix() const {
89 switch (OffloadingDeviceKind) {
90 case OFK_None:
91 break;
92 case OFK_Host:
93 llvm_unreachable("Host kind is not an offloading device kind.");
94 break;
95 case OFK_Cuda:
96 return "device-cuda";
Samuel Antao39f9da22016-10-27 16:38:05 +000097 case OFK_OpenMP:
98 return "device-openmp";
Samuel Antaod06239d2016-07-15 23:13:27 +000099
100 // TODO: Add other programming models here.
101 }
102
103 if (!ActiveOffloadKindMask)
104 return "";
105
106 std::string Res("host");
107 if (ActiveOffloadKindMask & OFK_Cuda)
108 Res += "-cuda";
Samuel Antao39f9da22016-10-27 16:38:05 +0000109 if (ActiveOffloadKindMask & OFK_OpenMP)
110 Res += "-openmp";
Samuel Antaod06239d2016-07-15 23:13:27 +0000111
112 // TODO: Add other programming models here.
113
114 return Res;
115}
116
117std::string
Samuel Antao2fd32132016-07-15 23:51:21 +0000118Action::getOffloadingFileNamePrefix(llvm::StringRef NormalizedTriple) const {
Samuel Antaod06239d2016-07-15 23:13:27 +0000119 // A file prefix is only generated for device actions and consists of the
120 // offload kind and triple.
121 if (!OffloadingDeviceKind)
122 return "";
123
124 std::string Res("-");
125 Res += getOffloadingKindPrefix();
126 Res += "-";
127 Res += NormalizedTriple;
128 return Res;
129}
130
David Blaikie68e081d2011-12-20 02:48:34 +0000131void InputAction::anchor() {}
132
Mike Stump11289f42009-09-09 15:08:12 +0000133InputAction::InputAction(const Arg &_Input, types::ID _Type)
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000134 : Action(InputClass, _Type), Input(_Input) {
135}
136
David Blaikie68e081d2011-12-20 02:48:34 +0000137void BindArchAction::anchor() {}
138
Mehdi Amini087f1fb2016-10-07 22:03:03 +0000139BindArchAction::BindArchAction(Action *Input, llvm::StringRef ArchName)
140 : Action(BindArchClass, Input), ArchName(ArchName) {}
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000141
Samuel Antaod06239d2016-07-15 23:13:27 +0000142void OffloadAction::anchor() {}
Artem Belevich0ff05cd2015-07-13 23:27:56 +0000143
Samuel Antaod06239d2016-07-15 23:13:27 +0000144OffloadAction::OffloadAction(const HostDependence &HDep)
145 : Action(OffloadClass, HDep.getAction()), HostTC(HDep.getToolChain()) {
146 OffloadingArch = HDep.getBoundArch();
147 ActiveOffloadKindMask = HDep.getOffloadKinds();
148 HDep.getAction()->propagateHostOffloadInfo(HDep.getOffloadKinds(),
149 HDep.getBoundArch());
Eric Christopher2a7248f2016-07-16 00:58:34 +0000150}
Artem Belevich0ff05cd2015-07-13 23:27:56 +0000151
Samuel Antaod06239d2016-07-15 23:13:27 +0000152OffloadAction::OffloadAction(const DeviceDependences &DDeps, types::ID Ty)
153 : Action(OffloadClass, DDeps.getActions(), Ty),
154 DevToolChains(DDeps.getToolChains()) {
155 auto &OKinds = DDeps.getOffloadKinds();
156 auto &BArchs = DDeps.getBoundArchs();
Artem Belevich0ff05cd2015-07-13 23:27:56 +0000157
Samuel Antaod06239d2016-07-15 23:13:27 +0000158 // If all inputs agree on the same kind, use it also for this action.
159 if (llvm::all_of(OKinds, [&](OffloadKind K) { return K == OKinds.front(); }))
160 OffloadingDeviceKind = OKinds.front();
161
162 // If we have a single dependency, inherit the architecture from it.
163 if (OKinds.size() == 1)
164 OffloadingArch = BArchs.front();
165
166 // Propagate info to the dependencies.
167 for (unsigned i = 0, e = getInputs().size(); i != e; ++i)
168 getInputs()[i]->propagateDeviceOffloadInfo(OKinds[i], BArchs[i]);
169}
170
171OffloadAction::OffloadAction(const HostDependence &HDep,
172 const DeviceDependences &DDeps)
173 : Action(OffloadClass, HDep.getAction()), HostTC(HDep.getToolChain()),
174 DevToolChains(DDeps.getToolChains()) {
175 // We use the kinds of the host dependence for this action.
176 OffloadingArch = HDep.getBoundArch();
177 ActiveOffloadKindMask = HDep.getOffloadKinds();
178 HDep.getAction()->propagateHostOffloadInfo(HDep.getOffloadKinds(),
179 HDep.getBoundArch());
180
181 // Add device inputs and propagate info to the device actions. Do work only if
182 // we have dependencies.
183 for (unsigned i = 0, e = DDeps.getActions().size(); i != e; ++i)
184 if (auto *A = DDeps.getActions()[i]) {
185 getInputs().push_back(A);
186 A->propagateDeviceOffloadInfo(DDeps.getOffloadKinds()[i],
187 DDeps.getBoundArchs()[i]);
188 }
189}
190
191void OffloadAction::doOnHostDependence(const OffloadActionWorkTy &Work) const {
192 if (!HostTC)
193 return;
194 assert(!getInputs().empty() && "No dependencies for offload action??");
195 auto *A = getInputs().front();
196 Work(A, HostTC, A->getOffloadingArch());
197}
198
199void OffloadAction::doOnEachDeviceDependence(
200 const OffloadActionWorkTy &Work) const {
201 auto I = getInputs().begin();
202 auto E = getInputs().end();
203 if (I == E)
204 return;
205
206 // We expect to have the same number of input dependences and device tool
207 // chains, except if we also have a host dependence. In that case we have one
208 // more dependence than we have device tool chains.
209 assert(getInputs().size() == DevToolChains.size() + (HostTC ? 1 : 0) &&
210 "Sizes of action dependences and toolchains are not consistent!");
211
212 // Skip host action
213 if (HostTC)
214 ++I;
215
216 auto TI = DevToolChains.begin();
217 for (; I != E; ++I, ++TI)
218 Work(*I, *TI, (*I)->getOffloadingArch());
219}
220
221void OffloadAction::doOnEachDependence(const OffloadActionWorkTy &Work) const {
222 doOnHostDependence(Work);
223 doOnEachDeviceDependence(Work);
224}
225
226void OffloadAction::doOnEachDependence(bool IsHostDependence,
227 const OffloadActionWorkTy &Work) const {
228 if (IsHostDependence)
229 doOnHostDependence(Work);
230 else
231 doOnEachDeviceDependence(Work);
232}
233
234bool OffloadAction::hasHostDependence() const { return HostTC != nullptr; }
235
236Action *OffloadAction::getHostDependence() const {
237 assert(hasHostDependence() && "Host dependence does not exist!");
238 assert(!getInputs().empty() && "No dependencies for offload action??");
239 return HostTC ? getInputs().front() : nullptr;
240}
241
242bool OffloadAction::hasSingleDeviceDependence(
243 bool DoNotConsiderHostActions) const {
244 if (DoNotConsiderHostActions)
245 return getInputs().size() == (HostTC ? 2 : 1);
246 return !HostTC && getInputs().size() == 1;
247}
248
249Action *
250OffloadAction::getSingleDeviceDependence(bool DoNotConsiderHostActions) const {
251 assert(hasSingleDeviceDependence(DoNotConsiderHostActions) &&
252 "Single device dependence does not exist!");
253 // The previous assert ensures the number of entries in getInputs() is
254 // consistent with what we are doing here.
255 return HostTC ? getInputs()[1] : getInputs().front();
256}
257
258void OffloadAction::DeviceDependences::add(Action &A, const ToolChain &TC,
259 const char *BoundArch,
260 OffloadKind OKind) {
261 DeviceActions.push_back(&A);
262 DeviceToolChains.push_back(&TC);
263 DeviceBoundArchs.push_back(BoundArch);
264 DeviceOffloadKinds.push_back(OKind);
265}
266
267OffloadAction::HostDependence::HostDependence(Action &A, const ToolChain &TC,
268 const char *BoundArch,
269 const DeviceDependences &DDeps)
270 : HostAction(A), HostToolChain(TC), HostBoundArch(BoundArch) {
271 for (auto K : DDeps.getOffloadKinds())
272 HostOffloadKinds |= K;
273}
Artem Belevich0ff05cd2015-07-13 23:27:56 +0000274
David Blaikie68e081d2011-12-20 02:48:34 +0000275void JobAction::anchor() {}
276
Justin Lebar41094612016-01-11 23:07:27 +0000277JobAction::JobAction(ActionClass Kind, Action *Input, types::ID Type)
278 : Action(Kind, Input, Type) {}
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000279
Mike Stump11289f42009-09-09 15:08:12 +0000280JobAction::JobAction(ActionClass Kind, const ActionList &Inputs, types::ID Type)
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000281 : Action(Kind, Inputs, Type) {
282}
283
David Blaikie68e081d2011-12-20 02:48:34 +0000284void PreprocessJobAction::anchor() {}
285
Justin Lebar41094612016-01-11 23:07:27 +0000286PreprocessJobAction::PreprocessJobAction(Action *Input, types::ID OutputType)
287 : JobAction(PreprocessJobClass, Input, OutputType) {}
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000288
David Blaikie68e081d2011-12-20 02:48:34 +0000289void PrecompileJobAction::anchor() {}
290
Justin Lebar41094612016-01-11 23:07:27 +0000291PrecompileJobAction::PrecompileJobAction(Action *Input, types::ID OutputType)
292 : JobAction(PrecompileJobClass, Input, OutputType) {}
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000293
David Blaikie68e081d2011-12-20 02:48:34 +0000294void AnalyzeJobAction::anchor() {}
295
Justin Lebar41094612016-01-11 23:07:27 +0000296AnalyzeJobAction::AnalyzeJobAction(Action *Input, types::ID OutputType)
297 : JobAction(AnalyzeJobClass, Input, OutputType) {}
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000298
Ted Kremenekf7639e12012-03-06 20:06:33 +0000299void MigrateJobAction::anchor() {}
300
Justin Lebar41094612016-01-11 23:07:27 +0000301MigrateJobAction::MigrateJobAction(Action *Input, types::ID OutputType)
302 : JobAction(MigrateJobClass, Input, OutputType) {}
Ted Kremenekf7639e12012-03-06 20:06:33 +0000303
David Blaikie68e081d2011-12-20 02:48:34 +0000304void CompileJobAction::anchor() {}
305
Justin Lebar41094612016-01-11 23:07:27 +0000306CompileJobAction::CompileJobAction(Action *Input, types::ID OutputType)
307 : JobAction(CompileJobClass, Input, OutputType) {}
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000308
Bob Wilson23a55f12014-12-21 07:00:00 +0000309void BackendJobAction::anchor() {}
310
Justin Lebar41094612016-01-11 23:07:27 +0000311BackendJobAction::BackendJobAction(Action *Input, types::ID OutputType)
312 : JobAction(BackendJobClass, Input, OutputType) {}
Bob Wilson23a55f12014-12-21 07:00:00 +0000313
David Blaikie68e081d2011-12-20 02:48:34 +0000314void AssembleJobAction::anchor() {}
315
Justin Lebar41094612016-01-11 23:07:27 +0000316AssembleJobAction::AssembleJobAction(Action *Input, types::ID OutputType)
317 : JobAction(AssembleJobClass, Input, OutputType) {}
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000318
David Blaikie68e081d2011-12-20 02:48:34 +0000319void LinkJobAction::anchor() {}
320
Mike Stump11289f42009-09-09 15:08:12 +0000321LinkJobAction::LinkJobAction(ActionList &Inputs, types::ID Type)
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000322 : JobAction(LinkJobClass, Inputs, Type) {
323}
324
David Blaikie68e081d2011-12-20 02:48:34 +0000325void LipoJobAction::anchor() {}
326
Mike Stump11289f42009-09-09 15:08:12 +0000327LipoJobAction::LipoJobAction(ActionList &Inputs, types::ID Type)
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000328 : JobAction(LipoJobClass, Inputs, Type) {
329}
Daniel Dunbar88299622010-06-04 18:28:36 +0000330
David Blaikie68e081d2011-12-20 02:48:34 +0000331void DsymutilJobAction::anchor() {}
332
Daniel Dunbar88299622010-06-04 18:28:36 +0000333DsymutilJobAction::DsymutilJobAction(ActionList &Inputs, types::ID Type)
334 : JobAction(DsymutilJobClass, Inputs, Type) {
335}
Eric Christopher551ef452011-08-23 17:56:55 +0000336
David Blaikie68e081d2011-12-20 02:48:34 +0000337void VerifyJobAction::anchor() {}
338
Justin Lebar41094612016-01-11 23:07:27 +0000339VerifyJobAction::VerifyJobAction(ActionClass Kind, Action *Input,
340 types::ID Type)
341 : JobAction(Kind, Input, Type) {
Ben Langmuir9b9a8d32014-02-06 18:53:25 +0000342 assert((Kind == VerifyDebugInfoJobClass || Kind == VerifyPCHJobClass) &&
343 "ActionClass is not a valid VerifyJobAction");
344}
345
Ben Langmuir9b9a8d32014-02-06 18:53:25 +0000346void VerifyDebugInfoJobAction::anchor() {}
347
Justin Lebar41094612016-01-11 23:07:27 +0000348VerifyDebugInfoJobAction::VerifyDebugInfoJobAction(Action *Input,
349 types::ID Type)
350 : VerifyJobAction(VerifyDebugInfoJobClass, Input, Type) {}
Ben Langmuir9b9a8d32014-02-06 18:53:25 +0000351
352void VerifyPCHJobAction::anchor() {}
353
Justin Lebar41094612016-01-11 23:07:27 +0000354VerifyPCHJobAction::VerifyPCHJobAction(Action *Input, types::ID Type)
355 : VerifyJobAction(VerifyPCHJobClass, Input, Type) {}
Samuel Antao69d6f312016-10-27 17:50:43 +0000356
357void OffloadBundlingJobAction::anchor() {}
358
359OffloadBundlingJobAction::OffloadBundlingJobAction(ActionList &Inputs)
360 : JobAction(OffloadBundlingJobClass, Inputs, Inputs.front()->getType()) {}
Samuel Antaofab4f372016-10-27 18:00:51 +0000361
362void OffloadUnbundlingJobAction::anchor() {}
363
364OffloadUnbundlingJobAction::OffloadUnbundlingJobAction(Action *Input)
365 : JobAction(OffloadUnbundlingJobClass, Input, Input->getType()) {}