From fcac03f4a07e17a76ed526939ca1d8ab33fbe4fb Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Mon, 18 Sep 2023 16:47:56 +0200 Subject: [PATCH] WIP: LLVM 17 support --- Makefile | 2 +- builder/cc1as.cpp | 23 +++++++++++++++++------ builder/cc1as.h | 10 +++++++++- builder/clang.cpp | 2 +- builder/lld.cpp | 29 +++++++++-------------------- builder/tools-builtin.go | 29 +++-------------------------- 6 files changed, 40 insertions(+), 55 deletions(-) diff --git a/Makefile b/Makefile index f9e6f0dde3..1306a51708 100644 --- a/Makefile +++ b/Makefile @@ -238,7 +238,7 @@ gen-device-renesas: build/gen-device-svd # Get LLVM sources. $(LLVM_PROJECTDIR)/llvm: - git clone -b xtensa_release_16.x --depth=1 https://github.com/espressif/llvm-project $(LLVM_PROJECTDIR) + git clone -b release/17.x --depth=1 https://github.com/llvm/llvm-project $(LLVM_PROJECTDIR) llvm-source: $(LLVM_PROJECTDIR)/llvm # Configure LLVM. diff --git a/builder/cc1as.cpp b/builder/cc1as.cpp index 8b28426b05..5298ce453e 100644 --- a/builder/cc1as.cpp +++ b/builder/cc1as.cpp @@ -1,5 +1,12 @@ //go:build byollvm +// Source: https://github.com/llvm/llvm-project/blob/main/clang/tools/driver/cc1as_main.cpp +// This file needs to be updated each LLVM release. +// There are a few small modifications to make, like: +// * ExecuteAssembler is made non-static. +// * The struct AssemlberImplementation is moved to cc1as.h so it can be +// included elsewhere. + //===-- cc1as.cpp - Clang Assembler --------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. @@ -21,8 +28,8 @@ #include "clang/Frontend/TextDiagnosticPrinter.h" #include "clang/Frontend/Utils.h" #include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringSwitch.h" -#include "llvm/ADT/Triple.h" #include "llvm/IR/DataLayout.h" #include "llvm/MC/MCAsmBackend.h" #include "llvm/MC/MCAsmInfo.h" @@ -46,7 +53,6 @@ #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/FormattedStream.h" -#include "llvm/Support/Host.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" #include "llvm/Support/Process.h" @@ -55,6 +61,8 @@ #include "llvm/Support/TargetSelect.h" #include "llvm/Support/Timer.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/TargetParser/Host.h" +#include "llvm/TargetParser/Triple.h" #include #include #include @@ -151,8 +159,7 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts, for (const auto &Arg : Args.getAllArgValues(OPT_fdebug_prefix_map_EQ)) { auto Split = StringRef(Arg).split('='); - Opts.DebugPrefixMap.insert( - {std::string(Split.first), std::string(Split.second)}); + Opts.DebugPrefixMap.emplace_back(Split.first, Split.second); } // Frontend Options @@ -225,6 +232,9 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts, .Case("default", EmitDwarfUnwindType::Default); } + Opts.EmitCompactUnwindNonCanonical = + Args.hasArg(OPT_femit_compact_unwind_non_canonical); + Opts.AsSecureLogFile = Args.getLastArgValue(OPT_as_secure_log_file); return Success; @@ -260,8 +270,8 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts, MemoryBuffer::getFileOrSTDIN(Opts.InputFile, /*IsText=*/true); if (std::error_code EC = Buffer.getError()) { - Error = EC.message(); - return Diags.Report(diag::err_fe_error_reading) << Opts.InputFile; + return Diags.Report(diag::err_fe_error_reading) + << Opts.InputFile << EC.message(); } SourceMgr SrcMgr; @@ -278,6 +288,7 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts, MCTargetOptions MCOptions; MCOptions.EmitDwarfUnwind = Opts.EmitDwarfUnwind; + MCOptions.EmitCompactUnwindNonCanonical = Opts.EmitCompactUnwindNonCanonical; MCOptions.AsSecureLogFile = Opts.AsSecureLogFile; std::unique_ptr MAI( diff --git a/builder/cc1as.h b/builder/cc1as.h index 955ded1193..4b22fc3e81 100644 --- a/builder/cc1as.h +++ b/builder/cc1as.h @@ -1,3 +1,6 @@ +// Source: https://github.com/llvm/llvm-project/blob/main/clang/tools/driver/cc1as_main.cpp +// See cc1as.cpp for details. + //===-- cc1as.h - Clang Assembler ----------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. @@ -44,7 +47,7 @@ struct AssemblerInvocation { std::string DwarfDebugFlags; std::string DwarfDebugProducer; std::string DebugCompilationDir; - std::map DebugPrefixMap; + llvm::SmallVector, 0> DebugPrefixMap; llvm::DebugCompressionType CompressDebugSections = llvm::DebugCompressionType::None; std::string MainFileName; @@ -89,6 +92,10 @@ struct AssemblerInvocation { /// Whether to emit DWARF unwind info. EmitDwarfUnwindType EmitDwarfUnwind; + // Whether to emit compact-unwind for non-canonical entries. + // Note: maybe overriden by other constraints. + unsigned EmitCompactUnwindNonCanonical : 1; + /// The name of the relocation model to use. std::string RelocationModel; @@ -128,6 +135,7 @@ struct AssemblerInvocation { DwarfVersion = 0; EmbedBitcode = 0; EmitDwarfUnwind = EmitDwarfUnwindType::Default; + EmitCompactUnwindNonCanonical = false; } static bool CreateFromArgs(AssemblerInvocation &Res, diff --git a/builder/clang.cpp b/builder/clang.cpp index e51d693389..6ffe75e3e5 100644 --- a/builder/clang.cpp +++ b/builder/clang.cpp @@ -11,7 +11,7 @@ #include #include #include -#include +#include using namespace llvm; using namespace clang; diff --git a/builder/lld.cpp b/builder/lld.cpp index 6cecbebe88..734c5703c6 100644 --- a/builder/lld.cpp +++ b/builder/lld.cpp @@ -5,7 +5,11 @@ #include #include -extern "C" { +LLD_HAS_DRIVER(coff) +LLD_HAS_DRIVER(elf) +LLD_HAS_DRIVER(mingw) +LLD_HAS_DRIVER(macho) +LLD_HAS_DRIVER(wasm) static void configure() { #if _WIN64 @@ -16,28 +20,13 @@ static void configure() { #endif } -bool tinygo_link_elf(int argc, char **argv) { - configure(); - std::vector args(argv, argv + argc); - return lld::elf::link(args, llvm::outs(), llvm::errs(), false, false); -} - -bool tinygo_link_macho(int argc, char **argv) { - configure(); - std::vector args(argv, argv + argc); - return lld::macho::link(args, llvm::outs(), llvm::errs(), false, false); -} - -bool tinygo_link_mingw(int argc, char **argv) { - configure(); - std::vector args(argv, argv + argc); - return lld::mingw::link(args, llvm::outs(), llvm::errs(), false, false); -} +extern "C" { -bool tinygo_link_wasm(int argc, char **argv) { +bool tinygo_link(int argc, char **argv) { configure(); std::vector args(argv, argv + argc); - return lld::wasm::link(args, llvm::outs(), llvm::errs(), false, false); + lld::Result r = lld::lldMain(args, llvm::outs(), llvm::errs(), LLD_ALL_DRIVERS); + return !r.retCode; } } // external "C" diff --git a/builder/tools-builtin.go b/builder/tools-builtin.go index 2b3cba6189..38a00f66b0 100644 --- a/builder/tools-builtin.go +++ b/builder/tools-builtin.go @@ -12,10 +12,7 @@ import ( #include #include bool tinygo_clang_driver(int argc, char **argv); -bool tinygo_link_elf(int argc, char **argv); -bool tinygo_link_macho(int argc, char **argv); -bool tinygo_link_mingw(int argc, char **argv); -bool tinygo_link_wasm(int argc, char **argv); +bool tinygo_link(int argc, char **argv); */ import "C" @@ -26,15 +23,6 @@ const hasBuiltinTools = true // This version actually runs the tools because TinyGo was compiled while // linking statically with LLVM (with the byollvm build tag). func RunTool(tool string, args ...string) error { - linker := "elf" - if tool == "ld.lld" && len(args) >= 2 { - if args[0] == "-m" && (args[1] == "i386pep" || args[1] == "arm64pe") { - linker = "mingw" - } else if args[0] == "-flavor" { - linker = args[1] - args = args[2:] - } - } args = append([]string{"tinygo:" + tool}, args...) var cflag *C.char @@ -51,19 +39,8 @@ func RunTool(tool string, args ...string) error { switch tool { case "clang": ok = C.tinygo_clang_driver(C.int(len(args)), (**C.char)(buf)) - case "ld.lld": - switch linker { - case "darwin": - ok = C.tinygo_link_macho(C.int(len(args)), (**C.char)(buf)) - case "elf": - ok = C.tinygo_link_elf(C.int(len(args)), (**C.char)(buf)) - case "mingw": - ok = C.tinygo_link_mingw(C.int(len(args)), (**C.char)(buf)) - default: - return errors.New("unknown linker: " + linker) - } - case "wasm-ld": - ok = C.tinygo_link_wasm(C.int(len(args)), (**C.char)(buf)) + case "ld.lld", "wasm-ld": + ok = C.tinygo_link(C.int(len(args)), (**C.char)(buf)) default: return errors.New("unknown tool: " + tool) }