From 0d37efdbc599e61ce2a0418723a66d6b45aea8d7 Mon Sep 17 00:00:00 2001 From: Ben Shi Date: Thu, 9 Mar 2023 12:04:54 +0800 Subject: [PATCH] [clang][driver] Enable '-flto' on AVR Reviewed By: MaskRay Closes https://github.com/llvm/llvm-project/issues/55940 Differential Revision: https://reviews.llvm.org/D145646 --- clang/lib/Driver/ToolChains/AVR.cpp | 6 ++++++ clang/lib/Driver/ToolChains/AVR.h | 2 ++ clang/test/Driver/avr-ld.c | 14 ++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/clang/lib/Driver/ToolChains/AVR.cpp b/clang/lib/Driver/ToolChains/AVR.cpp index be2749e..0e1f8dd 100644 --- a/clang/lib/Driver/ToolChains/AVR.cpp +++ b/clang/lib/Driver/ToolChains/AVR.cpp @@ -506,6 +506,12 @@ void AVR::Linker::ConstructJob(Compilation &C, const JobAction &JA, D.Diag(diag::warn_drv_avr_linker_section_addresses_not_implemented) << CPU; } + if (D.isUsingLTO()) { + assert(!Inputs.empty() && "Must have at least one input."); + addLTOOptions(getToolChain(), Args, CmdArgs, Output, Inputs[0], + D.getLTOMode() == LTOK_Thin); + } + // If the family name is known, we can link with the device-specific libgcc. // Without it, libgcc will simply not be linked. This matches avr-gcc // behavior. diff --git a/clang/lib/Driver/ToolChains/AVR.h b/clang/lib/Driver/ToolChains/AVR.h index ea161fe..d432d81 100644 --- a/clang/lib/Driver/ToolChains/AVR.h +++ b/clang/lib/Driver/ToolChains/AVR.h @@ -36,6 +36,8 @@ public: std::string getCompilerRT(const llvm::opt::ArgList &Args, StringRef Component, FileType Type) const override; + bool HasNativeLLVMSupport() const override { return true; } + protected: Tool *buildLinker() const override; diff --git a/clang/test/Driver/avr-ld.c b/clang/test/Driver/avr-ld.c index 80bb428..3088bc0 100644 --- a/clang/test/Driver/avr-ld.c +++ b/clang/test/Driver/avr-ld.c @@ -43,3 +43,17 @@ // RUN: %clang -### --target=avr -mmcu=atxmega128a1 --rtlib=libgcc --sysroot %S/Inputs/basic_avr_tree %s 2>&1 | FileCheck -check-prefix LINKO %s // LINKO: {{".*ld.*"}} {{.*}} {{"-L.*avrxmega7"}} {{.*}} "--defsym=__DATA_REGION_ORIGIN__=0x802000" "--start-group" {{.*}} "-latxmega128a1" {{.*}} "--end-group" "--relax" "-mavrxmega7" + +// RUN: %clang -### --target=avr -mmcu=atmega328 -flto --sysroot %S/Inputs/basic_avr_tree %s 2>&1 | FileCheck -check-prefix LINKP %s +// LINKP: {{".*ld.*"}} {{.*}} "--defsym=__DATA_REGION_ORIGIN__=0x800100" "-plugin" {{.*}} "-plugin-opt=mcpu=atmega328" + +// RUN: %clang -### --target=avr -flto --sysroot %S/Inputs/basic_avr_tree %s 2>&1 | FileCheck -check-prefix LINKQ %s +// LINKQ: {{".*ld.*"}} {{.*}} "-plugin" +// LINKQ-NOT: "-plugin-opt=mcpu" + +// RUN: %clang -### --target=avr -mmcu=atmega328 -flto=thin --sysroot %S/Inputs/basic_avr_tree %s 2>&1 | FileCheck -check-prefix LINKR %s +// LINKR: {{".*ld.*"}} {{.*}} "--defsym=__DATA_REGION_ORIGIN__=0x800100" "-plugin" {{.*}} "-plugin-opt=mcpu=atmega328" "-plugin-opt=thinlto" + +// RUN: %clang -### --target=avr -mmcu=atmega328 -flto --sysroot %S/Inputs/basic_avr_tree %s 2>&1 | FileCheck -check-prefix LINKS %s +// LINKS: {{".*ld.*"}} {{.*}} "--defsym=__DATA_REGION_ORIGIN__=0x800100" "-plugin" {{.*}} "-plugin-opt=mcpu=atmega328" +// LINKS-NOT: "-plugin-opt=thinlto" -- 2.7.4