blob: a351429ad7b84ebac67a4d449bd5c85589329560 [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";
Daniel Dunbar80665fb2009-03-13 12:17:08 +000039 }
Mike Stump11289f42009-09-09 15:08:12 +000040
David Blaikie83d382b2011-09-23 05:06:16 +000041 llvm_unreachable("invalid class");
Daniel Dunbar80665fb2009-03-13 12:17:08 +000042}
Daniel Dunbar3f261ff2009-03-13 23:08:03 +000043
Samuel Antaod06239d2016-07-15 23:13:27 +000044void Action::propagateDeviceOffloadInfo(OffloadKind OKind, const char *OArch) {
45 // Offload action set its own kinds on their dependences.
46 if (Kind == OffloadClass)
47 return;
48
49 assert((OffloadingDeviceKind == OKind || OffloadingDeviceKind == OFK_None) &&
50 "Setting device kind to a different device??");
51 assert(!ActiveOffloadKindMask && "Setting a device kind in a host action??");
52 OffloadingDeviceKind = OKind;
53 OffloadingArch = OArch;
54
55 for (auto *A : Inputs)
56 A->propagateDeviceOffloadInfo(OffloadingDeviceKind, OArch);
57}
58
59void Action::propagateHostOffloadInfo(unsigned OKinds, const char *OArch) {
60 // Offload action set its own kinds on their dependences.
61 if (Kind == OffloadClass)
62 return;
63
64 assert(OffloadingDeviceKind == OFK_None &&
65 "Setting a host kind in a device action.");
66 ActiveOffloadKindMask |= OKinds;
67 OffloadingArch = OArch;
68
69 for (auto *A : Inputs)
70 A->propagateHostOffloadInfo(ActiveOffloadKindMask, OArch);
71}
72
73void Action::propagateOffloadInfo(const Action *A) {
74 if (unsigned HK = A->getOffloadingHostActiveKinds())
75 propagateHostOffloadInfo(HK, A->getOffloadingArch());
76 else
77 propagateDeviceOffloadInfo(A->getOffloadingDeviceKind(),
78 A->getOffloadingArch());
79}
80
81std::string Action::getOffloadingKindPrefix() const {
82 switch (OffloadingDeviceKind) {
83 case OFK_None:
84 break;
85 case OFK_Host:
86 llvm_unreachable("Host kind is not an offloading device kind.");
87 break;
88 case OFK_Cuda:
89 return "device-cuda";
Samuel Antao39f9da22016-10-27 16:38:05 +000090 case OFK_OpenMP:
91 return "device-openmp";
Samuel Antaod06239d2016-07-15 23:13:27 +000092
93 // TODO: Add other programming models here.
94 }
95
96 if (!ActiveOffloadKindMask)
97 return "";
98
99 std::string Res("host");
100 if (ActiveOffloadKindMask & OFK_Cuda)
101 Res += "-cuda";
Samuel Antao39f9da22016-10-27 16:38:05 +0000102 if (ActiveOffloadKindMask & OFK_OpenMP)
103 Res += "-openmp";
Samuel Antaod06239d2016-07-15 23:13:27 +0000104
105 // TODO: Add other programming models here.
106
107 return Res;
108}
109
110std::string
Samuel Antao2fd32132016-07-15 23:51:21 +0000111Action::getOffloadingFileNamePrefix(llvm::StringRef NormalizedTriple) const {
Samuel Antaod06239d2016-07-15 23:13:27 +0000112 // A file prefix is only generated for device actions and consists of the
113 // offload kind and triple.
114 if (!OffloadingDeviceKind)
115 return "";
116
117 std::string Res("-");
118 Res += getOffloadingKindPrefix();
119 Res += "-";
120 Res += NormalizedTriple;
121 return Res;
122}
123
David Blaikie68e081d2011-12-20 02:48:34 +0000124void InputAction::anchor() {}
125
Mike Stump11289f42009-09-09 15:08:12 +0000126InputAction::InputAction(const Arg &_Input, types::ID _Type)
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000127 : Action(InputClass, _Type), Input(_Input) {
128}
129
David Blaikie68e081d2011-12-20 02:48:34 +0000130void BindArchAction::anchor() {}
131
Mehdi Amini087f1fb2016-10-07 22:03:03 +0000132BindArchAction::BindArchAction(Action *Input, llvm::StringRef ArchName)
133 : Action(BindArchClass, Input), ArchName(ArchName) {}
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000134
Samuel Antaod06239d2016-07-15 23:13:27 +0000135void OffloadAction::anchor() {}
Artem Belevich0ff05cd2015-07-13 23:27:56 +0000136
Samuel Antaod06239d2016-07-15 23:13:27 +0000137OffloadAction::OffloadAction(const HostDependence &HDep)
138 : Action(OffloadClass, HDep.getAction()), HostTC(HDep.getToolChain()) {
139 OffloadingArch = HDep.getBoundArch();
140 ActiveOffloadKindMask = HDep.getOffloadKinds();
141 HDep.getAction()->propagateHostOffloadInfo(HDep.getOffloadKinds(),
142 HDep.getBoundArch());
Eric Christopher2a7248f2016-07-16 00:58:34 +0000143}
Artem Belevich0ff05cd2015-07-13 23:27:56 +0000144
Samuel Antaod06239d2016-07-15 23:13:27 +0000145OffloadAction::OffloadAction(const DeviceDependences &DDeps, types::ID Ty)
146 : Action(OffloadClass, DDeps.getActions(), Ty),
147 DevToolChains(DDeps.getToolChains()) {
148 auto &OKinds = DDeps.getOffloadKinds();
149 auto &BArchs = DDeps.getBoundArchs();
Artem Belevich0ff05cd2015-07-13 23:27:56 +0000150
Samuel Antaod06239d2016-07-15 23:13:27 +0000151 // If all inputs agree on the same kind, use it also for this action.
152 if (llvm::all_of(OKinds, [&](OffloadKind K) { return K == OKinds.front(); }))
153 OffloadingDeviceKind = OKinds.front();
154
155 // If we have a single dependency, inherit the architecture from it.
156 if (OKinds.size() == 1)
157 OffloadingArch = BArchs.front();
158
159 // Propagate info to the dependencies.
160 for (unsigned i = 0, e = getInputs().size(); i != e; ++i)
161 getInputs()[i]->propagateDeviceOffloadInfo(OKinds[i], BArchs[i]);
162}
163
164OffloadAction::OffloadAction(const HostDependence &HDep,
165 const DeviceDependences &DDeps)
166 : Action(OffloadClass, HDep.getAction()), HostTC(HDep.getToolChain()),
167 DevToolChains(DDeps.getToolChains()) {
168 // We use the kinds of the host dependence for this action.
169 OffloadingArch = HDep.getBoundArch();
170 ActiveOffloadKindMask = HDep.getOffloadKinds();
171 HDep.getAction()->propagateHostOffloadInfo(HDep.getOffloadKinds(),
172 HDep.getBoundArch());
173
174 // Add device inputs and propagate info to the device actions. Do work only if
175 // we have dependencies.
176 for (unsigned i = 0, e = DDeps.getActions().size(); i != e; ++i)
177 if (auto *A = DDeps.getActions()[i]) {
178 getInputs().push_back(A);
179 A->propagateDeviceOffloadInfo(DDeps.getOffloadKinds()[i],
180 DDeps.getBoundArchs()[i]);
181 }
182}
183
184void OffloadAction::doOnHostDependence(const OffloadActionWorkTy &Work) const {
185 if (!HostTC)
186 return;
187 assert(!getInputs().empty() && "No dependencies for offload action??");
188 auto *A = getInputs().front();
189 Work(A, HostTC, A->getOffloadingArch());
190}
191
192void OffloadAction::doOnEachDeviceDependence(
193 const OffloadActionWorkTy &Work) const {
194 auto I = getInputs().begin();
195 auto E = getInputs().end();
196 if (I == E)
197 return;
198
199 // We expect to have the same number of input dependences and device tool
200 // chains, except if we also have a host dependence. In that case we have one
201 // more dependence than we have device tool chains.
202 assert(getInputs().size() == DevToolChains.size() + (HostTC ? 1 : 0) &&
203 "Sizes of action dependences and toolchains are not consistent!");
204
205 // Skip host action
206 if (HostTC)
207 ++I;
208
209 auto TI = DevToolChains.begin();
210 for (; I != E; ++I, ++TI)
211 Work(*I, *TI, (*I)->getOffloadingArch());
212}
213
214void OffloadAction::doOnEachDependence(const OffloadActionWorkTy &Work) const {
215 doOnHostDependence(Work);
216 doOnEachDeviceDependence(Work);
217}
218
219void OffloadAction::doOnEachDependence(bool IsHostDependence,
220 const OffloadActionWorkTy &Work) const {
221 if (IsHostDependence)
222 doOnHostDependence(Work);
223 else
224 doOnEachDeviceDependence(Work);
225}
226
227bool OffloadAction::hasHostDependence() const { return HostTC != nullptr; }
228
229Action *OffloadAction::getHostDependence() const {
230 assert(hasHostDependence() && "Host dependence does not exist!");
231 assert(!getInputs().empty() && "No dependencies for offload action??");
232 return HostTC ? getInputs().front() : nullptr;
233}
234
235bool OffloadAction::hasSingleDeviceDependence(
236 bool DoNotConsiderHostActions) const {
237 if (DoNotConsiderHostActions)
238 return getInputs().size() == (HostTC ? 2 : 1);
239 return !HostTC && getInputs().size() == 1;
240}
241
242Action *
243OffloadAction::getSingleDeviceDependence(bool DoNotConsiderHostActions) const {
244 assert(hasSingleDeviceDependence(DoNotConsiderHostActions) &&
245 "Single device dependence does not exist!");
246 // The previous assert ensures the number of entries in getInputs() is
247 // consistent with what we are doing here.
248 return HostTC ? getInputs()[1] : getInputs().front();
249}
250
251void OffloadAction::DeviceDependences::add(Action &A, const ToolChain &TC,
252 const char *BoundArch,
253 OffloadKind OKind) {
254 DeviceActions.push_back(&A);
255 DeviceToolChains.push_back(&TC);
256 DeviceBoundArchs.push_back(BoundArch);
257 DeviceOffloadKinds.push_back(OKind);
258}
259
260OffloadAction::HostDependence::HostDependence(Action &A, const ToolChain &TC,
261 const char *BoundArch,
262 const DeviceDependences &DDeps)
263 : HostAction(A), HostToolChain(TC), HostBoundArch(BoundArch) {
264 for (auto K : DDeps.getOffloadKinds())
265 HostOffloadKinds |= K;
266}
Artem Belevich0ff05cd2015-07-13 23:27:56 +0000267
David Blaikie68e081d2011-12-20 02:48:34 +0000268void JobAction::anchor() {}
269
Justin Lebar41094612016-01-11 23:07:27 +0000270JobAction::JobAction(ActionClass Kind, Action *Input, types::ID Type)
271 : Action(Kind, Input, Type) {}
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000272
Mike Stump11289f42009-09-09 15:08:12 +0000273JobAction::JobAction(ActionClass Kind, const ActionList &Inputs, types::ID Type)
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000274 : Action(Kind, Inputs, Type) {
275}
276
David Blaikie68e081d2011-12-20 02:48:34 +0000277void PreprocessJobAction::anchor() {}
278
Justin Lebar41094612016-01-11 23:07:27 +0000279PreprocessJobAction::PreprocessJobAction(Action *Input, types::ID OutputType)
280 : JobAction(PreprocessJobClass, Input, OutputType) {}
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000281
David Blaikie68e081d2011-12-20 02:48:34 +0000282void PrecompileJobAction::anchor() {}
283
Justin Lebar41094612016-01-11 23:07:27 +0000284PrecompileJobAction::PrecompileJobAction(Action *Input, types::ID OutputType)
285 : JobAction(PrecompileJobClass, Input, OutputType) {}
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000286
David Blaikie68e081d2011-12-20 02:48:34 +0000287void AnalyzeJobAction::anchor() {}
288
Justin Lebar41094612016-01-11 23:07:27 +0000289AnalyzeJobAction::AnalyzeJobAction(Action *Input, types::ID OutputType)
290 : JobAction(AnalyzeJobClass, Input, OutputType) {}
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000291
Ted Kremenekf7639e12012-03-06 20:06:33 +0000292void MigrateJobAction::anchor() {}
293
Justin Lebar41094612016-01-11 23:07:27 +0000294MigrateJobAction::MigrateJobAction(Action *Input, types::ID OutputType)
295 : JobAction(MigrateJobClass, Input, OutputType) {}
Ted Kremenekf7639e12012-03-06 20:06:33 +0000296
David Blaikie68e081d2011-12-20 02:48:34 +0000297void CompileJobAction::anchor() {}
298
Justin Lebar41094612016-01-11 23:07:27 +0000299CompileJobAction::CompileJobAction(Action *Input, types::ID OutputType)
300 : JobAction(CompileJobClass, Input, OutputType) {}
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000301
Bob Wilson23a55f12014-12-21 07:00:00 +0000302void BackendJobAction::anchor() {}
303
Justin Lebar41094612016-01-11 23:07:27 +0000304BackendJobAction::BackendJobAction(Action *Input, types::ID OutputType)
305 : JobAction(BackendJobClass, Input, OutputType) {}
Bob Wilson23a55f12014-12-21 07:00:00 +0000306
David Blaikie68e081d2011-12-20 02:48:34 +0000307void AssembleJobAction::anchor() {}
308
Justin Lebar41094612016-01-11 23:07:27 +0000309AssembleJobAction::AssembleJobAction(Action *Input, types::ID OutputType)
310 : JobAction(AssembleJobClass, Input, OutputType) {}
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000311
David Blaikie68e081d2011-12-20 02:48:34 +0000312void LinkJobAction::anchor() {}
313
Mike Stump11289f42009-09-09 15:08:12 +0000314LinkJobAction::LinkJobAction(ActionList &Inputs, types::ID Type)
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000315 : JobAction(LinkJobClass, Inputs, Type) {
316}
317
David Blaikie68e081d2011-12-20 02:48:34 +0000318void LipoJobAction::anchor() {}
319
Mike Stump11289f42009-09-09 15:08:12 +0000320LipoJobAction::LipoJobAction(ActionList &Inputs, types::ID Type)
Daniel Dunbar3f261ff2009-03-13 23:08:03 +0000321 : JobAction(LipoJobClass, Inputs, Type) {
322}
Daniel Dunbar88299622010-06-04 18:28:36 +0000323
David Blaikie68e081d2011-12-20 02:48:34 +0000324void DsymutilJobAction::anchor() {}
325
Daniel Dunbar88299622010-06-04 18:28:36 +0000326DsymutilJobAction::DsymutilJobAction(ActionList &Inputs, types::ID Type)
327 : JobAction(DsymutilJobClass, Inputs, Type) {
328}
Eric Christopher551ef452011-08-23 17:56:55 +0000329
David Blaikie68e081d2011-12-20 02:48:34 +0000330void VerifyJobAction::anchor() {}
331
Justin Lebar41094612016-01-11 23:07:27 +0000332VerifyJobAction::VerifyJobAction(ActionClass Kind, Action *Input,
333 types::ID Type)
334 : JobAction(Kind, Input, Type) {
Ben Langmuir9b9a8d32014-02-06 18:53:25 +0000335 assert((Kind == VerifyDebugInfoJobClass || Kind == VerifyPCHJobClass) &&
336 "ActionClass is not a valid VerifyJobAction");
337}
338
Ben Langmuir9b9a8d32014-02-06 18:53:25 +0000339void VerifyDebugInfoJobAction::anchor() {}
340
Justin Lebar41094612016-01-11 23:07:27 +0000341VerifyDebugInfoJobAction::VerifyDebugInfoJobAction(Action *Input,
342 types::ID Type)
343 : VerifyJobAction(VerifyDebugInfoJobClass, Input, Type) {}
Ben Langmuir9b9a8d32014-02-06 18:53:25 +0000344
345void VerifyPCHJobAction::anchor() {}
346
Justin Lebar41094612016-01-11 23:07:27 +0000347VerifyPCHJobAction::VerifyPCHJobAction(Action *Input, types::ID Type)
348 : VerifyJobAction(VerifyPCHJobClass, Input, Type) {}