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 @@...@@ -1,9 +1,8 @@
1//===-- cc1_main.cpp - Clang CC1 Compiler Frontend ------------------------===//1//===-- cc1_main.cpp - Clang CC1 Compiler Frontend ------------------------===//
2//2//
3// The LLVM Compiler Infrastructure3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4//4// See https://llvm.org/LICENSE.txt for license information.
5// This file is distributed under the University of Illinois Open Source5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6// License. See LICENSE.TXT for details.
7//6//
8//===----------------------------------------------------------------------===//7//===----------------------------------------------------------------------===//
9//8//
...@@ -14,6 +13,7 @@...@@ -14,6 +13,7 @@
14//===----------------------------------------------------------------------===//13//===----------------------------------------------------------------------===//
1514
16#include "clang/Basic/Stack.h"15#include "clang/Basic/Stack.h"
16#include "clang/Basic/TargetOptions.h"
17#include "clang/CodeGen/ObjectFilePCHContainerOperations.h"17#include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
18#include "clang/Config/config.h"18#include "clang/Config/config.h"
19#include "clang/Driver/DriverDiagnostic.h"19#include "clang/Driver/DriverDiagnostic.h"
...@@ -35,10 +35,14 @@...@@ -35,10 +35,14 @@
35#include "llvm/Support/Compiler.h"35#include "llvm/Support/Compiler.h"
36#include "llvm/Support/ErrorHandling.h"36#include "llvm/Support/ErrorHandling.h"
37#include "llvm/Support/ManagedStatic.h"37#include "llvm/Support/ManagedStatic.h"
38#include "llvm/Support/Path.h"
38#include "llvm/Support/Signals.h"39#include "llvm/Support/Signals.h"
40#include "llvm/Support/TargetRegistry.h"
39#include "llvm/Support/TargetSelect.h"41#include "llvm/Support/TargetSelect.h"
42#include "llvm/Support/TimeProfiler.h"
40#include "llvm/Support/Timer.h"43#include "llvm/Support/Timer.h"
41#include "llvm/Support/raw_ostream.h"44#include "llvm/Support/raw_ostream.h"
45#include "llvm/Target/TargetMachine.h"
42#include <cstdio>46#include <cstdio>
4347
44#ifdef CLANG_HAVE_RLIMITS48#ifdef CLANG_HAVE_RLIMITS
...@@ -159,6 +163,23 @@ static void ensureSufficientStack() {...@@ -159,6 +163,23 @@ static void ensureSufficientStack() {
159static void ensureSufficientStack() {}163static void ensureSufficientStack() {}
160#endif164#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
162int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {183int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
163 ensureSufficientStack();184 ensureSufficientStack();
164185
...@@ -184,6 +205,13 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {...@@ -184,6 +205,13 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
184 bool Success = CompilerInvocation::CreateFromArgs(205 bool Success = CompilerInvocation::CreateFromArgs(
185 Clang->getInvocation(), Argv.begin(), Argv.end(), Diags);206 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
187 // Infer the builtin include path if unspecified.215 // Infer the builtin include path if unspecified.
188 if (Clang->getHeaderSearchOpts().UseBuiltinIncludes &&216 if (Clang->getHeaderSearchOpts().UseBuiltinIncludes &&
189 Clang->getHeaderSearchOpts().ResourceDir.empty())217 Clang->getHeaderSearchOpts().ResourceDir.empty())
...@@ -205,12 +233,36 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {...@@ -205,12 +233,36 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
205 return 1;233 return 1;
206234
207 // Execute the frontend actions.235 // Execute the frontend actions.
208 Success = ExecuteCompilerInvocation(Clang.get());236 {
237 llvm::TimeTraceScope TimeScope("ExecuteCompiler", StringRef(""));
238 Success = ExecuteCompilerInvocation(Clang.get());
239 }
209240
210 // If any timers were active but haven't been destroyed yet, print their241 // If any timers were active but haven't been destroyed yet, print their
211 // results now. This happens in -disable-free mode.242 // results now. This happens in -disable-free mode.
212 llvm::TimerGroup::printAll(llvm::errs());243 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
214 // Our error handler depends on the Diagnostics object, which we're266 // Our error handler depends on the Diagnostics object, which we're
215 // potentially about to delete. Uninstall the handler now so that any267 // potentially about to delete. Uninstall the handler now so that any
216 // later errors use the default handling behavior instead.268 // later errors use the default handling behavior instead.
...@@ -224,4 +276,3 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {...@@ -224,4 +276,3 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
224276
225 return !Success;277 return !Success;
226}278}
227
src/zig_clang_cc1as_main.cpp+28-16
...@@ -1,9 +1,8 @@...@@ -1,9 +1,8 @@
1//===-- cc1as_main.cpp - Clang Assembler ---------------------------------===//1//===-- cc1as_main.cpp - Clang Assembler ---------------------------------===//
2//2//
3// The LLVM Compiler Infrastructure3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4//4// See https://llvm.org/LICENSE.txt for license information.
5// This file is distributed under the University of Illinois Open Source5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6// License. See LICENSE.TXT for details.
7//6//
8//===----------------------------------------------------------------------===//7//===----------------------------------------------------------------------===//
9//8//
...@@ -99,7 +98,7 @@ struct AssemblerInvocation {...@@ -99,7 +98,7 @@ struct AssemblerInvocation {
99 llvm::DebugCompressionType CompressDebugSections =98 llvm::DebugCompressionType CompressDebugSections =
100 llvm::DebugCompressionType::None;99 llvm::DebugCompressionType::None;
101 std::string MainFileName;100 std::string MainFileName;
102 std::string SplitDwarfFile;101 std::string SplitDwarfOutput;
103102
104 /// @}103 /// @}
105 /// @name Frontend Options104 /// @name Frontend Options
...@@ -138,6 +137,10 @@ struct AssemblerInvocation {...@@ -138,6 +137,10 @@ struct AssemblerInvocation {
138 /// The name of the relocation model to use.137 /// The name of the relocation model to use.
139 std::string RelocationModel;138 std::string RelocationModel;
140139
140 /// The ABI targeted by the backend. Specified using -target-abi. Empty
141 /// otherwise.
142 std::string TargetABI;
143
141 /// @}144 /// @}
142145
143public:146public:
...@@ -255,7 +258,7 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,...@@ -255,7 +258,7 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
255 }258 }
256 Opts.LLVMArgs = Args.getAllArgValues(OPT_mllvm);259 Opts.LLVMArgs = Args.getAllArgValues(OPT_mllvm);
257 Opts.OutputPath = Args.getLastArgValue(OPT_o);260 Opts.OutputPath = Args.getLastArgValue(OPT_o);
258 Opts.SplitDwarfFile = Args.getLastArgValue(OPT_split_dwarf_file);261 Opts.SplitDwarfOutput = Args.getLastArgValue(OPT_split_dwarf_output);
259 if (Arg *A = Args.getLastArg(OPT_filetype)) {262 if (Arg *A = Args.getLastArg(OPT_filetype)) {
260 StringRef Name = A->getValue();263 StringRef Name = A->getValue();
261 unsigned OutputType = StringSwitch<unsigned>(Name)264 unsigned OutputType = StringSwitch<unsigned>(Name)
...@@ -283,6 +286,7 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,...@@ -283,6 +286,7 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
283 Opts.NoExecStack = Args.hasArg(OPT_mno_exec_stack);286 Opts.NoExecStack = Args.hasArg(OPT_mno_exec_stack);
284 Opts.FatalWarnings = Args.hasArg(OPT_massembler_fatal_warnings);287 Opts.FatalWarnings = Args.hasArg(OPT_massembler_fatal_warnings);
285 Opts.RelocationModel = Args.getLastArgValue(OPT_mrelocation_model, "pic");288 Opts.RelocationModel = Args.getLastArgValue(OPT_mrelocation_model, "pic");
289 Opts.TargetABI = Args.getLastArgValue(OPT_target_abi);
286 Opts.IncrementalLinkerCompatible =290 Opts.IncrementalLinkerCompatible =
287 Args.hasArg(OPT_mincremental_linker_compatible);291 Args.hasArg(OPT_mincremental_linker_compatible);
288 Opts.SymbolDefs = Args.getAllArgValues(OPT_defsym);292 Opts.SymbolDefs = Args.getAllArgValues(OPT_defsym);
...@@ -337,7 +341,7 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts,...@@ -337,7 +341,7 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts,
337 SourceMgr SrcMgr;341 SourceMgr SrcMgr;
338342
339 // Tell SrcMgr about this buffer, which is what the parser will pick up.343 // 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
342 // Record the location of the include directories so that the lexer can find346 // Record the location of the include directories so that the lexer can find
343 // it later.347 // it later.
...@@ -363,8 +367,8 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts,...@@ -363,8 +367,8 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts,
363 if (!FDOS)367 if (!FDOS)
364 return true;368 return true;
365 std::unique_ptr<raw_fd_ostream> DwoOS;369 std::unique_ptr<raw_fd_ostream> DwoOS;
366 if (!Opts.SplitDwarfFile.empty())370 if (!Opts.SplitDwarfOutput.empty())
367 DwoOS = getOutputStream(Opts.SplitDwarfFile, Diags, IsBinary);371 DwoOS = getOutputStream(Opts.SplitDwarfOutput, Diags, IsBinary);
368372
369 // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and373 // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
370 // MCObjectFileInfo needs a MCContext reference in order to initialize itself.374 // MCObjectFileInfo needs a MCContext reference in order to initialize itself.
...@@ -394,12 +398,21 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts,...@@ -394,12 +398,21 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts,
394 Ctx.setDwarfDebugProducer(StringRef(Opts.DwarfDebugProducer));398 Ctx.setDwarfDebugProducer(StringRef(Opts.DwarfDebugProducer));
395 if (!Opts.DebugCompilationDir.empty())399 if (!Opts.DebugCompilationDir.empty())
396 Ctx.setCompilationDir(Opts.DebugCompilationDir);400 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 }
397 if (!Opts.DebugPrefixMap.empty())407 if (!Opts.DebugPrefixMap.empty())
398 for (const auto &KV : Opts.DebugPrefixMap)408 for (const auto &KV : Opts.DebugPrefixMap)
399 Ctx.addDebugPrefixMapEntry(KV.first, KV.second);409 Ctx.addDebugPrefixMapEntry(KV.first, KV.second);
400 if (!Opts.MainFileName.empty())410 if (!Opts.MainFileName.empty())
401 Ctx.setMainFileName(StringRef(Opts.MainFileName));411 Ctx.setMainFileName(StringRef(Opts.MainFileName));
402 Ctx.setDwarfVersion(Opts.DwarfVersion);412 Ctx.setDwarfVersion(Opts.DwarfVersion);
413 if (Opts.GenDwarfForAssembly)
414 Ctx.setGenDwarfRootFile(Opts.InputFile,
415 SrcMgr.getMemoryBuffer(BufferIndex)->getBuffer());
403416
404 // Build up the feature string from the target feature list.417 // Build up the feature string from the target feature list.
405 std::string FS;418 std::string FS;
...@@ -418,6 +431,9 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts,...@@ -418,6 +431,9 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts,
418 raw_pwrite_stream *Out = FDOS.get();431 raw_pwrite_stream *Out = FDOS.get();
419 std::unique_ptr<buffer_ostream> BOS;432 std::unique_ptr<buffer_ostream> BOS;
420433
434 MCTargetOptions MCOptions;
435 MCOptions.ABIName = Opts.TargetABI;
436
421 // FIXME: There is a bit of code duplication with addPassesToEmitFile.437 // FIXME: There is a bit of code duplication with addPassesToEmitFile.
422 if (Opts.OutputType == AssemblerInvocation::FT_Asm) {438 if (Opts.OutputType == AssemblerInvocation::FT_Asm) {
423 MCInstPrinter *IP = TheTarget->createMCInstPrinter(439 MCInstPrinter *IP = TheTarget->createMCInstPrinter(
...@@ -426,7 +442,6 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts,...@@ -426,7 +442,6 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts,
426 std::unique_ptr<MCCodeEmitter> CE;442 std::unique_ptr<MCCodeEmitter> CE;
427 if (Opts.ShowEncoding)443 if (Opts.ShowEncoding)
428 CE.reset(TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx));444 CE.reset(TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx));
429 MCTargetOptions MCOptions;
430 std::unique_ptr<MCAsmBackend> MAB(445 std::unique_ptr<MCAsmBackend> MAB(
431 TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions));446 TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions));
432447
...@@ -447,7 +462,6 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts,...@@ -447,7 +462,6 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts,
447462
448 std::unique_ptr<MCCodeEmitter> CE(463 std::unique_ptr<MCCodeEmitter> CE(
449 TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx));464 TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx));
450 MCTargetOptions MCOptions;
451 std::unique_ptr<MCAsmBackend> MAB(465 std::unique_ptr<MCAsmBackend> MAB(
452 TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions));466 TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions));
453 std::unique_ptr<MCObjectWriter> OW =467 std::unique_ptr<MCObjectWriter> OW =
...@@ -481,9 +495,8 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts,...@@ -481,9 +495,8 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts,
481 createMCAsmParser(SrcMgr, Ctx, *Str.get(), *MAI));495 createMCAsmParser(SrcMgr, Ctx, *Str.get(), *MAI));
482496
483 // FIXME: init MCTargetOptions from sanitizer flags here.497 // FIXME: init MCTargetOptions from sanitizer flags here.
484 MCTargetOptions Options;
485 std::unique_ptr<MCTargetAsmParser> TAP(498 std::unique_ptr<MCTargetAsmParser> TAP(
486 TheTarget->createMCAsmParser(*STI, *Parser, *MCII, Options));499 TheTarget->createMCAsmParser(*STI, *Parser, *MCII, MCOptions));
487 if (!TAP)500 if (!TAP)
488 Failed = Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;501 Failed = Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
489502
...@@ -514,8 +527,8 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts,...@@ -514,8 +527,8 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts,
514 if (Failed) {527 if (Failed) {
515 if (Opts.OutputPath != "-")528 if (Opts.OutputPath != "-")
516 sys::fs::remove(Opts.OutputPath);529 sys::fs::remove(Opts.OutputPath);
517 if (!Opts.SplitDwarfFile.empty() && Opts.SplitDwarfFile != "-")530 if (!Opts.SplitDwarfOutput.empty() && Opts.SplitDwarfOutput != "-")
518 sys::fs::remove(Opts.SplitDwarfFile);531 sys::fs::remove(Opts.SplitDwarfOutput);
519 }532 }
520533
521 return Failed;534 return Failed;
...@@ -594,4 +607,3 @@ int cc1as_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {...@@ -594,4 +607,3 @@ int cc1as_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
594607
595 return !!Failed;608 return !!Failed;
596}609}
597
src/zig_clang_driver.cpp+4-6
...@@ -1,9 +1,8 @@...@@ -1,9 +1,8 @@
1//===-- driver.cpp - Clang GCC-Compatible Driver --------------------------===//1//===-- driver.cpp - Clang GCC-Compatible Driver --------------------------===//
2//2//
3// The LLVM Compiler Infrastructure3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4//4// See https://llvm.org/LICENSE.txt for license information.
5// This file is distributed under the University of Illinois Open Source5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6// License. See LICENSE.TXT for details.
7//6//
8//===----------------------------------------------------------------------===//7//===----------------------------------------------------------------------===//
9//8//
...@@ -339,7 +338,7 @@ int ZigClang_main(int argc_, const char **argv_) {...@@ -339,7 +338,7 @@ int ZigClang_main(int argc_, const char **argv_) {
339 // response files written by clang will tokenize the same way in either mode.338 // response files written by clang will tokenize the same way in either mode.
340 bool ClangCLMode = false;339 bool ClangCLMode = false;
341 if (StringRef(TargetAndMode.DriverMode).equals("--driver-mode=cl") ||340 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) {
343 return F && strcmp(F, "--driver-mode=cl") == 0;342 return F && strcmp(F, "--driver-mode=cl") == 0;
344 }) != argv.end()) {343 }) != argv.end()) {
345 ClangCLMode = true;344 ClangCLMode = true;
...@@ -510,4 +509,3 @@ int ZigClang_main(int argc_, const char **argv_) {...@@ -510,4 +509,3 @@ int ZigClang_main(int argc_, const char **argv_) {
510 // failing command.509 // failing command.
511 return Res;510 return Res;
512}511}
513