clang 20.0.0git
Hexagon.cpp
Go to the documentation of this file.
1//===--- Hexagon.cpp - Hexagon ToolChain Implementations --------*- C++ -*-===//
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 "Hexagon.h"
10#include "CommonArgs.h"
12#include "clang/Driver/Driver.h"
16#include "llvm/ADT/StringExtras.h"
17#include "llvm/Option/ArgList.h"
18#include "llvm/Support/FileSystem.h"
19#include "llvm/Support/Path.h"
20#include "llvm/Support/VirtualFileSystem.h"
21
22using namespace clang::driver;
23using namespace clang::driver::tools;
24using namespace clang::driver::toolchains;
25using namespace clang;
26using namespace llvm::opt;
27
28// Default hvx-length for various versions.
29static StringRef getDefaultHvxLength(StringRef HvxVer) {
30 return llvm::StringSwitch<StringRef>(HvxVer)
31 .Case("v60", "64b")
32 .Case("v62", "64b")
33 .Case("v65", "64b")
34 .Default("128b");
35}
36
37static void handleHVXWarnings(const Driver &D, const ArgList &Args) {
38 // Handle the unsupported values passed to mhvx-length.
39 if (Arg *A = Args.getLastArg(options::OPT_mhexagon_hvx_length_EQ)) {
40 StringRef Val = A->getValue();
41 if (!Val.equals_insensitive("64b") && !Val.equals_insensitive("128b"))
42 D.Diag(diag::err_drv_unsupported_option_argument)
43 << A->getSpelling() << Val;
44 }
45}
46
47// Handle hvx target features explicitly.
48static void handleHVXTargetFeatures(const Driver &D, const ArgList &Args,
49 std::vector<StringRef> &Features,
50 StringRef Cpu, bool &HasHVX) {
51 // Handle HVX warnings.
52 handleHVXWarnings(D, Args);
53
54 auto makeFeature = [&Args](Twine T, bool Enable) -> StringRef {
55 const std::string &S = T.str();
56 StringRef Opt(S);
57 Opt.consume_back("=");
58 if (Opt.starts_with("mno-"))
59 Opt = Opt.drop_front(4);
60 else if (Opt.starts_with("m"))
61 Opt = Opt.drop_front(1);
62 return Args.MakeArgString(Twine(Enable ? "+" : "-") + Twine(Opt));
63 };
64
65 auto withMinus = [](StringRef S) -> std::string {
66 return "-" + S.str();
67 };
68
69 // Drop tiny core suffix for HVX version.
70 std::string HvxVer =
71 (Cpu.back() == 'T' || Cpu.back() == 't' ? Cpu.drop_back(1) : Cpu).str();
72 HasHVX = false;
73
74 // Handle -mhvx, -mhvx=, -mno-hvx. If versioned and versionless flags
75 // are both present, the last one wins.
76 Arg *HvxEnablingArg =
77 Args.getLastArg(options::OPT_mhexagon_hvx, options::OPT_mhexagon_hvx_EQ,
78 options::OPT_mno_hexagon_hvx);
79 if (HvxEnablingArg) {
80 if (HvxEnablingArg->getOption().matches(options::OPT_mno_hexagon_hvx))
81 HvxEnablingArg = nullptr;
82 }
83
84 if (HvxEnablingArg) {
85 // If -mhvx[=] was given, it takes precedence.
86 if (Arg *A = Args.getLastArg(options::OPT_mhexagon_hvx,
87 options::OPT_mhexagon_hvx_EQ)) {
88 // If the version was given, set HvxVer. Otherwise HvxVer
89 // will remain equal to the CPU version.
90 if (A->getOption().matches(options::OPT_mhexagon_hvx_EQ))
91 HvxVer = StringRef(A->getValue()).lower();
92 }
93 HasHVX = true;
94 Features.push_back(makeFeature(Twine("hvx") + HvxVer, true));
95 } else if (Arg *A = Args.getLastArg(options::OPT_mno_hexagon_hvx)) {
96 // If there was an explicit -mno-hvx, add -hvx to target features.
97 Features.push_back(makeFeature(A->getOption().getName(), false));
98 }
99
100 StringRef HvxLen = getDefaultHvxLength(HvxVer);
101
102 // Handle -mhvx-length=.
103 if (Arg *A = Args.getLastArg(options::OPT_mhexagon_hvx_length_EQ)) {
104 // These flags are valid only if HVX in enabled.
105 if (!HasHVX)
106 D.Diag(diag::err_drv_needs_hvx) << withMinus(A->getOption().getName());
107 else if (A->getOption().matches(options::OPT_mhexagon_hvx_length_EQ))
108 HvxLen = A->getValue();
109 }
110
111 if (HasHVX) {
112 StringRef L = makeFeature(Twine("hvx-length") + HvxLen.lower(), true);
113 Features.push_back(L);
114 }
115
116 unsigned HvxVerNum;
117 // getAsInteger returns 'true' on error.
118 if (StringRef(HvxVer).drop_front(1).getAsInteger(10, HvxVerNum))
119 HvxVerNum = 0;
120
121 // Handle HVX floating point flags.
122 auto checkFlagHvxVersion =
123 [&](auto FlagOn, auto FlagOff,
124 unsigned MinVerNum) -> std::optional<StringRef> {
125 // Return an std::optional<StringRef>:
126 // - std::nullopt indicates a verification failure, or that the flag was not
127 // present in Args.
128 // - Otherwise the returned value is that name of the feature to add
129 // to Features.
130 Arg *A = Args.getLastArg(FlagOn, FlagOff);
131 if (!A)
132 return std::nullopt;
133
134 StringRef OptName = A->getOption().getName();
135 if (A->getOption().matches(FlagOff))
136 return makeFeature(OptName, false);
137
138 if (!HasHVX) {
139 D.Diag(diag::err_drv_needs_hvx) << withMinus(OptName);
140 return std::nullopt;
141 }
142 if (HvxVerNum < MinVerNum) {
143 D.Diag(diag::err_drv_needs_hvx_version)
144 << withMinus(OptName) << ("v" + std::to_string(HvxVerNum));
145 return std::nullopt;
146 }
147 return makeFeature(OptName, true);
148 };
149
150 if (auto F = checkFlagHvxVersion(options::OPT_mhexagon_hvx_qfloat,
151 options::OPT_mno_hexagon_hvx_qfloat, 68)) {
152 Features.push_back(*F);
153 }
154 if (auto F = checkFlagHvxVersion(options::OPT_mhexagon_hvx_ieee_fp,
155 options::OPT_mno_hexagon_hvx_ieee_fp, 68)) {
156 Features.push_back(*F);
157 }
158}
159
160// Hexagon target features.
162 const llvm::Triple &Triple,
163 const ArgList &Args,
164 std::vector<StringRef> &Features) {
165 handleTargetFeaturesGroup(D, Triple, Args, Features,
166 options::OPT_m_hexagon_Features_Group);
167
168 bool UseLongCalls = false;
169 if (Arg *A = Args.getLastArg(options::OPT_mlong_calls,
170 options::OPT_mno_long_calls)) {
171 if (A->getOption().matches(options::OPT_mlong_calls))
172 UseLongCalls = true;
173 }
174
175 Features.push_back(UseLongCalls ? "+long-calls" : "-long-calls");
176
177 bool HasHVX = false;
179 // 't' in Cpu denotes tiny-core micro-architecture. For now, the co-processors
180 // have no dependency on micro-architecture.
181 const bool TinyCore = Cpu.contains('t');
182
183 if (TinyCore)
184 Cpu = Cpu.take_front(Cpu.size() - 1);
185
186 handleHVXTargetFeatures(D, Args, Features, Cpu, HasHVX);
187
188 if (HexagonToolChain::isAutoHVXEnabled(Args) && !HasHVX)
189 D.Diag(diag::warn_drv_needs_hvx) << "auto-vectorization";
190}
191
192// Hexagon tools start.
194 ArgStringList &CmdArgs) const {
195}
196
198 const InputInfo &Output,
199 const InputInfoList &Inputs,
200 const ArgList &Args,
201 const char *LinkingOutput) const {
202 claimNoWarnArgs(Args);
203
204 auto &HTC = static_cast<const toolchains::HexagonToolChain&>(getToolChain());
205 const Driver &D = HTC.getDriver();
206 ArgStringList CmdArgs;
207
208 CmdArgs.push_back("--arch=hexagon");
209
210 RenderExtraToolArgs(JA, CmdArgs);
211
212 const char *AsName = "llvm-mc";
213 CmdArgs.push_back("-filetype=obj");
214 CmdArgs.push_back(Args.MakeArgString(
215 "-mcpu=hexagon" +
217
218 addSanitizerRuntimes(HTC, Args, CmdArgs);
219
220 assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
221 if (Output.isFilename()) {
222 CmdArgs.push_back("-o");
223 CmdArgs.push_back(Output.getFilename());
224 } else {
225 CmdArgs.push_back("-fsyntax-only");
226 }
227
228 if (Arg *A = Args.getLastArg(options::OPT_mhexagon_hvx_ieee_fp,
229 options::OPT_mno_hexagon_hvx_ieee_fp)) {
230 if (A->getOption().matches(options::OPT_mhexagon_hvx_ieee_fp))
231 CmdArgs.push_back("-mhvx-ieee-fp");
232 }
233
235 CmdArgs.push_back(Args.MakeArgString("-gpsize=" + Twine(*G)));
236 }
237
238 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
239
240 // Only pass -x if gcc will understand it; otherwise hope gcc
241 // understands the suffix correctly. The main use case this would go
242 // wrong in is for linker inputs if they happened to have an odd
243 // suffix; really the only way to get this to happen is a command
244 // like '-x foobar a.c' which will treat a.c like a linker input.
245 //
246 // FIXME: For the linker case specifically, can we safely convert
247 // inputs into '-Wl,' options?
248 for (const auto &II : Inputs) {
249 // Don't try to pass LLVM or AST inputs to a generic gcc.
250 if (types::isLLVMIR(II.getType()))
251 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
252 << HTC.getTripleString();
253 else if (II.getType() == types::TY_AST)
254 D.Diag(clang::diag::err_drv_no_ast_support)
255 << HTC.getTripleString();
256 else if (II.getType() == types::TY_ModuleFile)
257 D.Diag(diag::err_drv_no_module_support)
258 << HTC.getTripleString();
259
260 if (II.isFilename())
261 CmdArgs.push_back(II.getFilename());
262 else
263 // Don't render as input, we need gcc to do the translations.
264 // FIXME: What is this?
265 II.getInputArg().render(Args, CmdArgs);
266 }
267
268 auto *Exec = Args.MakeArgString(HTC.GetProgramPath(AsName));
269 C.addCommand(std::make_unique<Command>(JA, *this,
271 Exec, CmdArgs, Inputs, Output));
272}
273
275 ArgStringList &CmdArgs) const {
276}
277
278static void
281 const InputInfo &Output, const InputInfoList &Inputs,
282 const ArgList &Args, ArgStringList &CmdArgs,
283 const char *LinkingOutput) {
284
285 const Driver &D = HTC.getDriver();
286
287 //----------------------------------------------------------------------------
288 //
289 //----------------------------------------------------------------------------
290 bool IsStatic = Args.hasArg(options::OPT_static);
291 bool IsShared = Args.hasArg(options::OPT_shared);
292 bool IsPIE = Args.hasArg(options::OPT_pie);
293 bool IncStdLib = !Args.hasArg(options::OPT_nostdlib);
294 bool IncStartFiles = !Args.hasArg(options::OPT_nostartfiles);
295 bool IncDefLibs = !Args.hasArg(options::OPT_nodefaultlibs);
296 bool UseG0 = false;
297 bool UseLLD = false;
298 const char *Exec = Args.MakeArgString(HTC.GetLinkerPath(&UseLLD));
299 UseLLD = UseLLD || llvm::sys::path::filename(Exec).ends_with("ld.lld") ||
300 llvm::sys::path::stem(Exec).ends_with("ld.lld");
301 bool UseShared = IsShared && !IsStatic;
303
304 bool NeedsSanitizerDeps = addSanitizerRuntimes(HTC, Args, CmdArgs);
305 bool NeedsXRayDeps = addXRayRuntime(HTC, Args, CmdArgs);
306
307 //----------------------------------------------------------------------------
308 // Silence warnings for various options
309 //----------------------------------------------------------------------------
310 Args.ClaimAllArgs(options::OPT_g_Group);
311 Args.ClaimAllArgs(options::OPT_emit_llvm);
312 Args.ClaimAllArgs(options::OPT_w); // Other warning options are already
313 // handled somewhere else.
314 Args.ClaimAllArgs(options::OPT_static_libgcc);
315
316 //----------------------------------------------------------------------------
317 //
318 //----------------------------------------------------------------------------
319 if (Args.hasArg(options::OPT_s))
320 CmdArgs.push_back("-s");
321
322 if (Args.hasArg(options::OPT_r))
323 CmdArgs.push_back("-r");
324
325 for (const auto &Opt : HTC.ExtraOpts)
326 CmdArgs.push_back(Opt.c_str());
327
328 if (!UseLLD) {
329 CmdArgs.push_back("-march=hexagon");
330 CmdArgs.push_back(Args.MakeArgString("-mcpu=hexagon" + CpuVer));
331 }
332
333 if (IsShared) {
334 CmdArgs.push_back("-shared");
335 // The following should be the default, but doing as hexagon-gcc does.
336 CmdArgs.push_back("-call_shared");
337 }
338
339 if (IsStatic)
340 CmdArgs.push_back("-static");
341
342 if (IsPIE && !IsShared)
343 CmdArgs.push_back("-pie");
344
346 CmdArgs.push_back(Args.MakeArgString("-G" + Twine(*G)));
347 UseG0 = *G == 0;
348 }
349
350 CmdArgs.push_back("-o");
351 CmdArgs.push_back(Output.getFilename());
352
353 if (HTC.getTriple().isMusl()) {
354 if (!Args.hasArg(options::OPT_shared, options::OPT_static))
355 CmdArgs.push_back("-dynamic-linker=/lib/ld-musl-hexagon.so.1");
356
357 if (!Args.hasArg(options::OPT_shared, options::OPT_nostartfiles,
358 options::OPT_nostdlib))
359 CmdArgs.push_back(Args.MakeArgString(D.SysRoot + "/usr/lib/crt1.o"));
360 else if (Args.hasArg(options::OPT_shared) &&
361 !Args.hasArg(options::OPT_nostartfiles, options::OPT_nostdlib))
362 CmdArgs.push_back(Args.MakeArgString(D.SysRoot + "/usr/lib/crti.o"));
363
364 CmdArgs.push_back(
365 Args.MakeArgString(StringRef("-L") + D.SysRoot + "/usr/lib"));
366 Args.addAllArgs(CmdArgs, {options::OPT_T_Group, options::OPT_s,
367 options::OPT_t, options::OPT_u_Group});
368 AddLinkerInputs(HTC, Inputs, Args, CmdArgs, JA);
369
371
372 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
373 if (NeedsSanitizerDeps) {
374 linkSanitizerRuntimeDeps(HTC, Args, CmdArgs);
375
376 if (UNW != ToolChain::UNW_None)
377 CmdArgs.push_back("-lunwind");
378 }
379 if (NeedsXRayDeps)
380 linkXRayRuntimeDeps(HTC, Args, CmdArgs);
381
382 if (!Args.hasArg(options::OPT_nolibc))
383 CmdArgs.push_back("-lc");
384 CmdArgs.push_back("-lclang_rt.builtins-hexagon");
385 }
386 if (D.CCCIsCXX()) {
387 if (HTC.ShouldLinkCXXStdlib(Args))
388 HTC.AddCXXStdlibLibArgs(Args, CmdArgs);
389 }
390 const ToolChain::path_list &LibPaths = HTC.getFilePaths();
391 for (const auto &LibPath : LibPaths)
392 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + LibPath));
393 Args.ClaimAllArgs(options::OPT_L);
394 return;
395 }
396
397 //----------------------------------------------------------------------------
398 // moslib
399 //----------------------------------------------------------------------------
400 std::vector<std::string> OsLibs;
401 bool HasStandalone = false;
402 for (const Arg *A : Args.filtered(options::OPT_moslib_EQ)) {
403 A->claim();
404 OsLibs.emplace_back(A->getValue());
405 HasStandalone = HasStandalone || (OsLibs.back() == "standalone");
406 }
407 if (OsLibs.empty()) {
408 OsLibs.push_back("standalone");
409 HasStandalone = true;
410 }
411
412 //----------------------------------------------------------------------------
413 // Start Files
414 //----------------------------------------------------------------------------
415 const std::string MCpuSuffix = "/" + CpuVer.str();
416 const std::string MCpuG0Suffix = MCpuSuffix + "/G0";
417 const std::string RootDir =
418 HTC.getHexagonTargetDir(D.Dir, D.PrefixDirs) + "/";
419 const std::string StartSubDir =
420 "hexagon/lib" + (UseG0 ? MCpuG0Suffix : MCpuSuffix);
421
422 auto Find = [&HTC] (const std::string &RootDir, const std::string &SubDir,
423 const char *Name) -> std::string {
424 std::string RelName = SubDir + Name;
425 std::string P = HTC.GetFilePath(RelName.c_str());
426 if (llvm::sys::fs::exists(P))
427 return P;
428 return RootDir + RelName;
429 };
430
431 if (IncStdLib && IncStartFiles) {
432 if (!IsShared) {
433 if (HasStandalone) {
434 std::string Crt0SA = Find(RootDir, StartSubDir, "/crt0_standalone.o");
435 CmdArgs.push_back(Args.MakeArgString(Crt0SA));
436 }
437 std::string Crt0 = Find(RootDir, StartSubDir, "/crt0.o");
438 CmdArgs.push_back(Args.MakeArgString(Crt0));
439 }
440 std::string Init = UseShared
441 ? Find(RootDir, StartSubDir + "/pic", "/initS.o")
442 : Find(RootDir, StartSubDir, "/init.o");
443 CmdArgs.push_back(Args.MakeArgString(Init));
444 }
445
446 //----------------------------------------------------------------------------
447 // Library Search Paths
448 //----------------------------------------------------------------------------
449 const ToolChain::path_list &LibPaths = HTC.getFilePaths();
450 for (const auto &LibPath : LibPaths)
451 CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + LibPath));
452 Args.ClaimAllArgs(options::OPT_L);
453
454 //----------------------------------------------------------------------------
455 //
456 //----------------------------------------------------------------------------
457 Args.addAllArgs(CmdArgs, {options::OPT_T_Group, options::OPT_s,
458 options::OPT_t, options::OPT_u_Group});
459
460 AddLinkerInputs(HTC, Inputs, Args, CmdArgs, JA);
461
462 //----------------------------------------------------------------------------
463 // Libraries
464 //----------------------------------------------------------------------------
465 if (IncStdLib && IncDefLibs) {
466 if (D.CCCIsCXX()) {
467 if (HTC.ShouldLinkCXXStdlib(Args))
468 HTC.AddCXXStdlibLibArgs(Args, CmdArgs);
469 CmdArgs.push_back("-lm");
470 }
471
472 CmdArgs.push_back("--start-group");
473
474 if (!IsShared) {
475 for (StringRef Lib : OsLibs)
476 CmdArgs.push_back(Args.MakeArgString("-l" + Lib));
477 if (!Args.hasArg(options::OPT_nolibc))
478 CmdArgs.push_back("-lc");
479 }
480 CmdArgs.push_back("-lgcc");
481
482 CmdArgs.push_back("--end-group");
483 }
484
485 //----------------------------------------------------------------------------
486 // End files
487 //----------------------------------------------------------------------------
488 if (IncStdLib && IncStartFiles) {
489 std::string Fini = UseShared
490 ? Find(RootDir, StartSubDir + "/pic", "/finiS.o")
491 : Find(RootDir, StartSubDir, "/fini.o");
492 CmdArgs.push_back(Args.MakeArgString(Fini));
493 }
494}
495
497 const InputInfo &Output,
498 const InputInfoList &Inputs,
499 const ArgList &Args,
500 const char *LinkingOutput) const {
501 auto &HTC = static_cast<const toolchains::HexagonToolChain&>(getToolChain());
502
503 ArgStringList CmdArgs;
504 constructHexagonLinkArgs(C, JA, HTC, Output, Inputs, Args, CmdArgs,
505 LinkingOutput);
506
507 const char *Exec = Args.MakeArgString(HTC.GetLinkerPath());
508 C.addCommand(std::make_unique<Command>(JA, *this,
510 Exec, CmdArgs, Inputs, Output));
511}
512// Hexagon tools end.
513
514/// Hexagon Toolchain
515
517 const std::string &InstalledDir,
518 const SmallVectorImpl<std::string> &PrefixDirs) const {
519 std::string InstallRelDir;
520 const Driver &D = getDriver();
521
522 // Locate the rest of the toolchain ...
523 for (auto &I : PrefixDirs)
524 if (D.getVFS().exists(I))
525 return I;
526
527 if (getVFS().exists(InstallRelDir = InstalledDir + "/../target"))
528 return InstallRelDir;
529
530 return InstalledDir;
531}
532
533std::optional<unsigned>
535 StringRef Gn = "";
536 if (Arg *A = Args.getLastArg(options::OPT_G)) {
537 Gn = A->getValue();
538 } else if (Args.getLastArg(options::OPT_shared, options::OPT_fpic,
539 options::OPT_fPIC)) {
540 Gn = "0";
541 }
542
543 unsigned G;
544 if (!Gn.getAsInteger(10, G))
545 return G;
546
547 return std::nullopt;
548}
549
551 SmallString<128> Dir(getDriver().SysRoot);
552 llvm::sys::path::append(Dir, "usr", "lib");
553 if (!SelectedMultilibs.empty()) {
554 Dir += SelectedMultilibs.back().gccSuffix();
555 }
556 return std::string(Dir);
557}
558
560 ToolChain::path_list &LibPaths) const {
561 const Driver &D = getDriver();
562
563 //----------------------------------------------------------------------------
564 // -L Args
565 //----------------------------------------------------------------------------
566 for (Arg *A : Args.filtered(options::OPT_L))
567 llvm::append_range(LibPaths, A->getValues());
568
569 //----------------------------------------------------------------------------
570 // Other standard paths
571 //----------------------------------------------------------------------------
572 std::vector<std::string> RootDirs;
573 std::copy(D.PrefixDirs.begin(), D.PrefixDirs.end(),
574 std::back_inserter(RootDirs));
575
576 std::string TargetDir = getHexagonTargetDir(D.Dir, D.PrefixDirs);
577 if (!llvm::is_contained(RootDirs, TargetDir))
578 RootDirs.push_back(TargetDir);
579
580 bool HasPIC = Args.hasArg(options::OPT_fpic, options::OPT_fPIC);
581 // Assume G0 with -shared.
582 bool HasG0 = Args.hasArg(options::OPT_shared);
583 if (auto G = getSmallDataThreshold(Args))
584 HasG0 = *G == 0;
585
586 const std::string CpuVer = GetTargetCPUVersion(Args).str();
587 for (auto &Dir : RootDirs) {
588 std::string LibDir = Dir + "/hexagon/lib";
589 std::string LibDirCpu = LibDir + '/' + CpuVer;
590 if (HasG0) {
591 if (HasPIC)
592 LibPaths.push_back(LibDirCpu + "/G0/pic");
593 LibPaths.push_back(LibDirCpu + "/G0");
594 }
595 LibPaths.push_back(LibDirCpu);
596 LibPaths.push_back(LibDir);
597 }
598}
599
600HexagonToolChain::HexagonToolChain(const Driver &D, const llvm::Triple &Triple,
601 const llvm::opt::ArgList &Args)
602 : Linux(D, Triple, Args) {
603 const std::string TargetDir = getHexagonTargetDir(D.Dir, D.PrefixDirs);
604
605 // Note: Generic_GCC::Generic_GCC adds InstalledDir and getDriver().Dir to
606 // program paths
607 const std::string BinDir(TargetDir + "/bin");
608 if (D.getVFS().exists(BinDir))
609 getProgramPaths().push_back(BinDir);
610
612
613 // Remove paths added by Linux toolchain. Currently Hexagon_TC really targets
614 // 'elf' OS type, so the Linux paths are not appropriate. When we actually
615 // support 'linux' we'll need to fix this up
616 LibPaths.clear();
617 getHexagonLibraryPaths(Args, LibPaths);
618}
619
621
623 ArgStringList &CmdArgs) const {
626 if (UNW != ToolChain::UNW_None && UNW != ToolChain::UNW_CompilerRT) {
627 const Arg *A = Args.getLastArg(options::OPT_unwindlib_EQ);
628 if (A) {
629 getDriver().Diag(diag::err_drv_unsupported_unwind_for_platform)
630 << A->getValue() << getTriple().normalize();
631 return;
632 }
633 }
634
635 switch (Type) {
637 CmdArgs.push_back("-lc++");
638 if (Args.hasArg(options::OPT_fexperimental_library))
639 CmdArgs.push_back("-lc++experimental");
640 CmdArgs.push_back("-lc++abi");
641 if (UNW != ToolChain::UNW_None)
642 CmdArgs.push_back("-lunwind");
643 break;
644
646 CmdArgs.push_back("-lstdc++");
647 break;
648 }
649}
650
652 return new tools::hexagon::Assembler(*this);
653}
654
656 return new tools::hexagon::Linker(*this);
657}
658
660 const llvm::opt::ArgList &DriverArgs) const {
661 // Copied in large part from lib/Frontend/CompilerInvocation.cpp.
662 Arg *A = DriverArgs.getLastArg(options::OPT_O_Group);
663 if (!A)
664 return 0;
665
666 if (A->getOption().matches(options::OPT_O0))
667 return 0;
668 if (A->getOption().matches(options::OPT_Ofast) ||
669 A->getOption().matches(options::OPT_O4))
670 return 3;
671 assert(A->getNumValues() != 0);
672 StringRef S(A->getValue());
673 if (S == "s" || S == "z" || S.empty())
674 return 2;
675 if (S == "g")
676 return 1;
677
678 unsigned OptLevel;
679 if (S.getAsInteger(10, OptLevel))
680 return 0;
681 return OptLevel;
682}
683
684void HexagonToolChain::addClangTargetOptions(const ArgList &DriverArgs,
685 ArgStringList &CC1Args,
686 Action::OffloadKind) const {
687
688 bool UseInitArrayDefault = getTriple().isMusl();
689
690 if (!DriverArgs.hasFlag(options::OPT_fuse_init_array,
691 options::OPT_fno_use_init_array,
692 UseInitArrayDefault))
693 CC1Args.push_back("-fno-use-init-array");
694
695 if (DriverArgs.hasArg(options::OPT_ffixed_r19)) {
696 CC1Args.push_back("-target-feature");
697 CC1Args.push_back("+reserved-r19");
698 }
699 if (isAutoHVXEnabled(DriverArgs)) {
700 CC1Args.push_back("-mllvm");
701 CC1Args.push_back("-hexagon-autohvx");
702 }
703}
704
705void HexagonToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
706 ArgStringList &CC1Args) const {
707 if (DriverArgs.hasArg(options::OPT_nostdinc))
708 return;
709
710 const bool IsELF = !getTriple().isMusl() && !getTriple().isOSLinux();
711 const bool IsLinuxMusl = getTriple().isMusl() && getTriple().isOSLinux();
712
713 const Driver &D = getDriver();
714 SmallString<128> ResourceDirInclude(D.ResourceDir);
715 if (!IsELF) {
716 llvm::sys::path::append(ResourceDirInclude, "include");
717 if (!DriverArgs.hasArg(options::OPT_nobuiltininc) &&
718 (!IsLinuxMusl || DriverArgs.hasArg(options::OPT_nostdlibinc)))
719 addSystemInclude(DriverArgs, CC1Args, ResourceDirInclude);
720 }
721 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
722 return;
723
724 const bool HasSysRoot = !D.SysRoot.empty();
725 if (HasSysRoot) {
726 SmallString<128> P(D.SysRoot);
727 if (IsLinuxMusl)
728 llvm::sys::path::append(P, "usr/include");
729 else
730 llvm::sys::path::append(P, "include");
731
732 addExternCSystemInclude(DriverArgs, CC1Args, P.str());
733 // LOCAL_INCLUDE_DIR
734 addSystemInclude(DriverArgs, CC1Args, P + "/usr/local/include");
735 // TOOL_INCLUDE_DIR
736 AddMultilibIncludeArgs(DriverArgs, CC1Args);
737 }
738
739 if (!DriverArgs.hasArg(options::OPT_nobuiltininc) && IsLinuxMusl)
740 addSystemInclude(DriverArgs, CC1Args, ResourceDirInclude);
741
742 if (HasSysRoot)
743 return;
744 std::string TargetDir = getHexagonTargetDir(D.Dir, D.PrefixDirs);
745 addExternCSystemInclude(DriverArgs, CC1Args, TargetDir + "/hexagon/include");
746}
747
749 const llvm::opt::ArgList &DriverArgs,
750 llvm::opt::ArgStringList &CC1Args) const {
751 const Driver &D = getDriver();
752 if (!D.SysRoot.empty() && getTriple().isMusl())
753 addLibStdCXXIncludePaths(D.SysRoot + "/usr/include/c++/v1", "", "",
754 DriverArgs, CC1Args);
755 else if (getTriple().isMusl())
756 addLibStdCXXIncludePaths("/usr/include/c++/v1", "", "", DriverArgs,
757 CC1Args);
758 else {
759 std::string TargetDir = getHexagonTargetDir(D.Dir, D.PrefixDirs);
760 addLibStdCXXIncludePaths(TargetDir + "/hexagon/include/c++/v1", "", "",
761 DriverArgs, CC1Args);
762 }
763}
765 const llvm::opt::ArgList &DriverArgs,
766 llvm::opt::ArgStringList &CC1Args) const {
767 const Driver &D = getDriver();
768 std::string TargetDir = getHexagonTargetDir(D.Dir, D.PrefixDirs);
769 addLibStdCXXIncludePaths(TargetDir + "/hexagon/include/c++", "", "",
770 DriverArgs, CC1Args);
771}
772
774HexagonToolChain::GetCXXStdlibType(const ArgList &Args) const {
775 Arg *A = Args.getLastArg(options::OPT_stdlib_EQ);
776 if (!A) {
777 if (getTriple().isMusl())
779 else
781 }
782 StringRef Value = A->getValue();
783 if (Value != "libstdc++" && Value != "libc++")
784 getDriver().Diag(diag::err_drv_invalid_stdlib_name) << A->getAsString(Args);
785
786 if (Value == "libstdc++")
788 else if (Value == "libc++")
790 else
792}
793
794bool HexagonToolChain::isAutoHVXEnabled(const llvm::opt::ArgList &Args) {
795 if (Arg *A = Args.getLastArg(options::OPT_fvectorize,
796 options::OPT_fno_vectorize))
797 return A->getOption().matches(options::OPT_fvectorize);
798 return false;
799}
800
801//
802// Returns the default CPU for Hexagon. This is the default compilation target
803// if no Hexagon processor is selected at the command-line.
804//
806 return "hexagonv60";
807}
808
809StringRef HexagonToolChain::GetTargetCPUVersion(const ArgList &Args) {
810 Arg *CpuArg = nullptr;
811 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
812 CpuArg = A;
813
814 StringRef CPU = CpuArg ? CpuArg->getValue() : GetDefaultCPU();
815 CPU.consume_front("hexagon");
816 return CPU;
817}
StringRef P
const Decl * D
static void handleHVXTargetFeatures(const Driver &D, const ArgList &Args, std::vector< StringRef > &Features, StringRef Cpu, bool &HasHVX)
Definition: Hexagon.cpp:48
static void constructHexagonLinkArgs(Compilation &C, const JobAction &JA, const toolchains::HexagonToolChain &HTC, const InputInfo &Output, const InputInfoList &Inputs, const ArgList &Args, ArgStringList &CmdArgs, const char *LinkingOutput)
Definition: Hexagon.cpp:279
static void handleHVXWarnings(const Driver &D, const ArgList &Args)
Definition: Hexagon.cpp:37
static StringRef getDefaultHvxLength(StringRef HvxVer)
Definition: Hexagon.cpp:29
bool IsStatic
Definition: Format.cpp:3044
The base class of the type hierarchy.
Definition: Type.h:1828
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:45
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:77
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:144
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:22
const char * getFilename() const
Definition: InputInfo.h:83
bool isNothing() const
Definition: InputInfo.h:74
bool isFilename() const
Definition: InputInfo.h:75
static void addSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory to CC1 arguments.
Definition: ToolChain.cpp:1265
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const
Returns if the C++ standard library should be linked in.
Definition: ToolChain.cpp:1367
static void addExternCSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory with extern "C" semantics to CC1 arguments.
Definition: ToolChain.cpp:1280
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:949
path_list & getFilePaths()
Definition: ToolChain.h:294
const Driver & getDriver() const
Definition: ToolChain.h:252
path_list & getProgramPaths()
Definition: ToolChain.h:297
const llvm::Triple & getTriple() const
Definition: ToolChain.h:254
virtual UnwindLibType GetUnwindLibType(const llvm::opt::ArgList &Args) const
Definition: ToolChain.cpp:1203
std::string GetLinkerPath(bool *LinkerIsLLD=nullptr) const
Returns the linker path, respecting the -fuse-ld= argument to determine the linker suffix or name.
Definition: ToolChain.cpp:957
Tool - Information on a specific compilation tool.
Definition: Tool.h:32
void AddMultilibIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Definition: Gnu.cpp:3241
bool addLibStdCXXIncludePaths(Twine IncludeDir, StringRef Triple, Twine IncludeSuffix, const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, bool DetectDebian=false) const
Definition: Gnu.cpp:3332
void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const override
AddCXXStdlibLibArgs - Add the system specific linker arguments to use for the given C++ standard libr...
Definition: Hexagon.cpp:622
static bool isAutoHVXEnabled(const llvm::opt::ArgList &Args)
Definition: Hexagon.cpp:794
void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, Action::OffloadKind DeviceOffloadKind) const override
Add options that need to be passed to cc1 for this target.
Definition: Hexagon.cpp:684
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
Definition: Hexagon.cpp:705
std::string getHexagonTargetDir(const std::string &InstalledDir, const SmallVectorImpl< std::string > &PrefixDirs) const
Hexagon Toolchain.
Definition: Hexagon.cpp:516
unsigned getOptimizationLevel(const llvm::opt::ArgList &DriverArgs) const
Definition: Hexagon.cpp:659
void getHexagonLibraryPaths(const llvm::opt::ArgList &Args, ToolChain::path_list &LibPaths) const
Definition: Hexagon.cpp:559
Tool * buildAssembler() const override
Definition: Hexagon.cpp:651
void addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Definition: Hexagon.cpp:764
HexagonToolChain(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
Definition: Hexagon.cpp:600
std::string getCompilerRTPath() const override
Definition: Hexagon.cpp:550
CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const override
Definition: Hexagon.cpp:774
void addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Definition: Hexagon.cpp:748
static std::optional< unsigned > getSmallDataThreshold(const llvm::opt::ArgList &Args)
Definition: Hexagon.cpp:534
static StringRef GetTargetCPUVersion(const llvm::opt::ArgList &Args)
Definition: Hexagon.cpp:809
std::vector< std::string > ExtraOpts
Definition: Linux.h:60
void RenderExtraToolArgs(const JobAction &JA, llvm::opt::ArgStringList &CmdArgs) const
Definition: Hexagon.cpp:193
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs,...
Definition: Hexagon.cpp:197
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs,...
Definition: Hexagon.cpp:496
virtual void RenderExtraToolArgs(const JobAction &JA, llvm::opt::ArgStringList &CmdArgs) const
Definition: Hexagon.cpp:274
void getHexagonTargetFeatures(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args, std::vector< StringRef > &Features)
void handleTargetFeaturesGroup(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args, std::vector< StringRef > &Features, llvm::opt::OptSpecifier Group)
Iterate Args and convert -mxxx to +xxx and -mno-xxx to -xxx and append it to Features.
void linkXRayRuntimeDeps(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
void linkSanitizerRuntimeDeps(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
bool addSanitizerRuntimes(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
bool addXRayRuntime(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
void claimNoWarnArgs(const llvm::opt::ArgList &Args)
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
bool isLLVMIR(ID Id)
Is this LLVM IR.
Definition: Types.cpp:256
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T
static constexpr ResponseFileSupport AtFileCurCP()
Definition: Job.h:92