authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-07-19 18:07:31-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-07-19 18:07:31-04:00
log05454123d45ca9a5fc4578d4ebffec9b158cf089
treef389bb16cdb6e8770d1dc7aa3daa6433f2e6b3a4
parent70e05c67ce6f044b5b99e01a5d972bbe92c38344
signaturelock-open Commit is signed but in an unrecognized format.

update clang driver code to llvm9

upstream commit 1931d3cb20a00da732c5210b123656632982fde0

3 files changed, 89 insertions(+), 28 deletions(-)

src/zig_clang_cc1_main.cpp+57-6
......@@ -1,9 +1,8 @@
11//===-- cc1_main.cpp - Clang CC1 Compiler Frontend ------------------------===//
22//
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.
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
76//
87//===----------------------------------------------------------------------===//
98//
......@@ -14,6 +13,7 @@
1413//===----------------------------------------------------------------------===//
1514
1615#include "clang/Basic/Stack.h"
16#include "clang/Basic/TargetOptions.h"
1717#include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
1818#include "clang/Config/config.h"
1919#include "clang/Driver/DriverDiagnostic.h"
......@@ -35,10 +35,14 @@
3535#include "llvm/Support/Compiler.h"
3636#include "llvm/Support/ErrorHandling.h"
3737#include "llvm/Support/ManagedStatic.h"
38#include "llvm/Support/Path.h"
3839#include "llvm/Support/Signals.h"
40#include "llvm/Support/TargetRegistry.h"
3941#include "llvm/Support/TargetSelect.h"
42#include "llvm/Support/TimeProfiler.h"
4043#include "llvm/Support/Timer.h"
4144#include "llvm/Support/raw_ostream.h"
45#include "llvm/Target/TargetMachine.h"
4246#include <cstdio>
4347
4448#ifdef CLANG_HAVE_RLIMITS
......@@ -159,6 +163,23 @@ static void ensureSufficientStack() {
159163static void ensureSufficientStack() {}
160164#endif
161165
166/// Print supported cpus of the given target.
167static int PrintSupportedCPUs(std::string TargetStr) {
168 std::string Error;
169 const llvm::Target *TheTarget =
170 llvm::TargetRegistry::lookupTarget(TargetStr, Error);
171 if (!TheTarget) {
172 llvm::errs() << Error;
173 return 1;
174 }
175
176 // the target machine will handle the mcpu printing
177 llvm::TargetOptions Options;
178 std::unique_ptr<llvm::TargetMachine> TheTargetMachine(
179 TheTarget->createTargetMachine(TargetStr, "", "+cpuHelp", Options, None));
180 return 0;
181}
182
162183int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
163184 ensureSufficientStack();
164185
......@@ -184,6 +205,13 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
184205 bool Success = CompilerInvocation::CreateFromArgs(
185206 Clang->getInvocation(), Argv.begin(), Argv.end(), Diags);
186207
208 if (Clang->getFrontendOpts().TimeTrace)
209 llvm::timeTraceProfilerInitialize();
210
211 // --print-supported-cpus takes priority over the actual compilation.
212 if (Clang->getFrontendOpts().PrintSupportedCPUs)
213 return PrintSupportedCPUs(Clang->getTargetOpts().Triple);
214
187215 // Infer the builtin include path if unspecified.
188216 if (Clang->getHeaderSearchOpts().UseBuiltinIncludes &&
189217 Clang->getHeaderSearchOpts().ResourceDir.empty())
......@@ -205,12 +233,36 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
205233 return 1;
206234
207235 // Execute the frontend actions.
208 Success = ExecuteCompilerInvocation(Clang.get());
236 {
237 llvm::TimeTraceScope TimeScope("ExecuteCompiler", StringRef(""));
238 Success = ExecuteCompilerInvocation(Clang.get());
239 }
209240
210241 // If any timers were active but haven't been destroyed yet, print their
211242 // results now. This happens in -disable-free mode.
212243 llvm::TimerGroup::printAll(llvm::errs());
213244
245 if (llvm::timeTraceProfilerEnabled()) {
246 SmallString<128> Path(Clang->getFrontendOpts().OutputFile);
247 llvm::sys::path::replace_extension(Path, "json");
248 auto profilerOutput =
249 Clang->createOutputFile(Path.str(),
250 /*Binary=*/false,
251 /*RemoveFileOnSignal=*/false, "",
252 /*Extension=*/"json",
253 /*useTemporary=*/false);
254
255 llvm::timeTraceProfilerWrite(*profilerOutput);
256 // FIXME(ibiryukov): make profilerOutput flush in destructor instead.
257 profilerOutput->flush();
258 llvm::timeTraceProfilerCleanup();
259
260 llvm::errs() << "Time trace json-file dumped to " << Path.str() << "\n";
261 llvm::errs()
262 << "Use chrome://tracing or Speedscope App "
263 "(https://www.speedscope.app) for flamegraph visualization\n";
264 }
265
214266 // Our error handler depends on the Diagnostics object, which we're
215267 // potentially about to delete. Uninstall the handler now so that any
216268 // later errors use the default handling behavior instead.
......@@ -224,4 +276,3 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
224276
225277 return !Success;
226278}
227
src/zig_clang_cc1as_main.cpp+28-16
......@@ -1,9 +1,8 @@
11//===-- cc1as_main.cpp - Clang Assembler ---------------------------------===//
22//
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.
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
76//
87//===----------------------------------------------------------------------===//
98//
......@@ -99,7 +98,7 @@ struct AssemblerInvocation {
9998 llvm::DebugCompressionType CompressDebugSections =
10099 llvm::DebugCompressionType::None;
101100 std::string MainFileName;
102 std::string SplitDwarfFile;
101 std::string SplitDwarfOutput;
103102
104103 /// @}
105104 /// @name Frontend Options
......@@ -138,6 +137,10 @@ struct AssemblerInvocation {
138137 /// The name of the relocation model to use.
139138 std::string RelocationModel;
140139
140 /// The ABI targeted by the backend. Specified using -target-abi. Empty
141 /// otherwise.
142 std::string TargetABI;
143
141144 /// @}
142145
143146public:
......@@ -255,7 +258,7 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
255258 }
256259 Opts.LLVMArgs = Args.getAllArgValues(OPT_mllvm);
257260 Opts.OutputPath = Args.getLastArgValue(OPT_o);
258 Opts.SplitDwarfFile = Args.getLastArgValue(OPT_split_dwarf_file);
261 Opts.SplitDwarfOutput = Args.getLastArgValue(OPT_split_dwarf_output);
259262 if (Arg *A = Args.getLastArg(OPT_filetype)) {
260263 StringRef Name = A->getValue();
261264 unsigned OutputType = StringSwitch<unsigned>(Name)
......@@ -283,6 +286,7 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
283286 Opts.NoExecStack = Args.hasArg(OPT_mno_exec_stack);
284287 Opts.FatalWarnings = Args.hasArg(OPT_massembler_fatal_warnings);
285288 Opts.RelocationModel = Args.getLastArgValue(OPT_mrelocation_model, "pic");
289 Opts.TargetABI = Args.getLastArgValue(OPT_target_abi);
286290 Opts.IncrementalLinkerCompatible =
287291 Args.hasArg(OPT_mincremental_linker_compatible);
288292 Opts.SymbolDefs = Args.getAllArgValues(OPT_defsym);
......@@ -337,7 +341,7 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts,
337341 SourceMgr SrcMgr;
338342
339343 // Tell SrcMgr about this buffer, which is what the parser will pick up.
340 SrcMgr.AddNewSourceBuffer(std::move(*Buffer), SMLoc());
344 unsigned BufferIndex = SrcMgr.AddNewSourceBuffer(std::move(*Buffer), SMLoc());
341345
342346 // Record the location of the include directories so that the lexer can find
343347 // it later.
......@@ -363,8 +367,8 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts,
363367 if (!FDOS)
364368 return true;
365369 std::unique_ptr<raw_fd_ostream> DwoOS;
366 if (!Opts.SplitDwarfFile.empty())
367 DwoOS = getOutputStream(Opts.SplitDwarfFile, Diags, IsBinary);
370 if (!Opts.SplitDwarfOutput.empty())
371 DwoOS = getOutputStream(Opts.SplitDwarfOutput, Diags, IsBinary);
368372
369373 // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
370374 // MCObjectFileInfo needs a MCContext reference in order to initialize itself.
......@@ -394,12 +398,21 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts,
394398 Ctx.setDwarfDebugProducer(StringRef(Opts.DwarfDebugProducer));
395399 if (!Opts.DebugCompilationDir.empty())
396400 Ctx.setCompilationDir(Opts.DebugCompilationDir);
401 else {
402 // If no compilation dir is set, try to use the current directory.
403 SmallString<128> CWD;
404 if (!sys::fs::current_path(CWD))
405 Ctx.setCompilationDir(CWD);
406 }
397407 if (!Opts.DebugPrefixMap.empty())
398408 for (const auto &KV : Opts.DebugPrefixMap)
399409 Ctx.addDebugPrefixMapEntry(KV.first, KV.second);
400410 if (!Opts.MainFileName.empty())
401411 Ctx.setMainFileName(StringRef(Opts.MainFileName));
402412 Ctx.setDwarfVersion(Opts.DwarfVersion);
413 if (Opts.GenDwarfForAssembly)
414 Ctx.setGenDwarfRootFile(Opts.InputFile,
415 SrcMgr.getMemoryBuffer(BufferIndex)->getBuffer());
403416
404417 // Build up the feature string from the target feature list.
405418 std::string FS;
......@@ -418,6 +431,9 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts,
418431 raw_pwrite_stream *Out = FDOS.get();
419432 std::unique_ptr<buffer_ostream> BOS;
420433
434 MCTargetOptions MCOptions;
435 MCOptions.ABIName = Opts.TargetABI;
436
421437 // FIXME: There is a bit of code duplication with addPassesToEmitFile.
422438 if (Opts.OutputType == AssemblerInvocation::FT_Asm) {
423439 MCInstPrinter *IP = TheTarget->createMCInstPrinter(
......@@ -426,7 +442,6 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts,
426442 std::unique_ptr<MCCodeEmitter> CE;
427443 if (Opts.ShowEncoding)
428444 CE.reset(TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx));
429 MCTargetOptions MCOptions;
430445 std::unique_ptr<MCAsmBackend> MAB(
431446 TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions));
432447
......@@ -447,7 +462,6 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts,
447462
448463 std::unique_ptr<MCCodeEmitter> CE(
449464 TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx));
450 MCTargetOptions MCOptions;
451465 std::unique_ptr<MCAsmBackend> MAB(
452466 TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions));
453467 std::unique_ptr<MCObjectWriter> OW =
......@@ -481,9 +495,8 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts,
481495 createMCAsmParser(SrcMgr, Ctx, *Str.get(), *MAI));
482496
483497 // FIXME: init MCTargetOptions from sanitizer flags here.
484 MCTargetOptions Options;
485498 std::unique_ptr<MCTargetAsmParser> TAP(
486 TheTarget->createMCAsmParser(*STI, *Parser, *MCII, Options));
499 TheTarget->createMCAsmParser(*STI, *Parser, *MCII, MCOptions));
487500 if (!TAP)
488501 Failed = Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
489502
......@@ -514,8 +527,8 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts,
514527 if (Failed) {
515528 if (Opts.OutputPath != "-")
516529 sys::fs::remove(Opts.OutputPath);
517 if (!Opts.SplitDwarfFile.empty() && Opts.SplitDwarfFile != "-")
518 sys::fs::remove(Opts.SplitDwarfFile);
530 if (!Opts.SplitDwarfOutput.empty() && Opts.SplitDwarfOutput != "-")
531 sys::fs::remove(Opts.SplitDwarfOutput);
519532 }
520533
521534 return Failed;
......@@ -594,4 +607,3 @@ int cc1as_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
594607
595608 return !!Failed;
596609}
597
src/zig_clang_driver.cpp+4-6
......@@ -1,9 +1,8 @@
11//===-- driver.cpp - Clang GCC-Compatible Driver --------------------------===//
22//
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.
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
76//
87//===----------------------------------------------------------------------===//
98//
......@@ -339,7 +338,7 @@ int ZigClang_main(int argc_, const char **argv_) {
339338 // response files written by clang will tokenize the same way in either mode.
340339 bool ClangCLMode = false;
341340 if (StringRef(TargetAndMode.DriverMode).equals("--driver-mode=cl") ||
342 std::find_if(argv.begin(), argv.end(), [](const char *F) {
341 llvm::find_if(argv, [](const char *F) {
343342 return F && strcmp(F, "--driver-mode=cl") == 0;
344343 }) != argv.end()) {
345344 ClangCLMode = true;
......@@ -510,4 +509,3 @@ int ZigClang_main(int argc_, const char **argv_) {
510509 // failing command.
511510 return Res;
512511}
513