<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Kinetis MicrocontrollersのトピックEnable LTO in compiler options break MWS-based projects</title>
    <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Enable-LTO-in-compiler-options-break-MWS-based-projects/m-p/1151646#M58044</link>
    <description>&lt;P&gt;Hi.&lt;/P&gt;&lt;P&gt;I am developing a BLE project featuring MWS, composed of central + peripheral devices. I met a problem on central device, when it is build in release mode with LTO compiler option enabled. I am using MCUXpresso 11.2.0 and SDK 2.6.6 on KW38.&lt;/P&gt;&lt;P&gt;In debug build configuration, everything works fine: central device is able to scan, detect and connect to peripheral device.&lt;/P&gt;&lt;P&gt;In release configuration, central is not able to connect anymore. Related BLE connection events are not coming at all. In release configuration, I enabled LTO both at compile and link time and disabled debug info.&lt;/P&gt;&lt;P&gt;If I disabled LTO at compile time, all works fine. But final binary is too large.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I managed to reproduce the problem on SDK examples: TM central and sensor. In central, I made the following changes:&lt;/P&gt;&lt;P&gt;#define gMWS_Enabled_d 1&amp;nbsp; // In app_preinclude.h&lt;/P&gt;&lt;P&gt;void App_Init(void)&amp;nbsp; &amp;nbsp;// In temperature_collector.c&lt;BR /&gt;{&lt;BR /&gt;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;In debug configuration TM demo works fine.&lt;/P&gt;&lt;P&gt;In release configuration, after having enable LTO at compile and link time and disabled debug info, central does not connect anymore to peripheral. So exactly like in case on my application.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If MWS is not enabled, so with original TMC sources, the problem does not occur. So I think the problem is in mws.c.&lt;/P&gt;&lt;P&gt;This guess is confirm by the workaround I managed to apply in my own project: I set mws.c file-specific compiler options and disabled LTO at compile time. However, this does not work on TMC.&lt;/P&gt;&lt;P&gt;I am afraid there might be other problems due to LTO like this, so I need proper solution and not a workaround. Thank you.&lt;/P&gt;</description>
    <pubDate>Thu, 10 Sep 2020 15:14:12 GMT</pubDate>
    <dc:creator>FedericoWegher</dc:creator>
    <dc:date>2020-09-10T15:14:12Z</dc:date>
    <item>
      <title>Enable LTO in compiler options break MWS-based projects</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Enable-LTO-in-compiler-options-break-MWS-based-projects/m-p/1151646#M58044</link>
      <description>&lt;P&gt;Hi.&lt;/P&gt;&lt;P&gt;I am developing a BLE project featuring MWS, composed of central + peripheral devices. I met a problem on central device, when it is build in release mode with LTO compiler option enabled. I am using MCUXpresso 11.2.0 and SDK 2.6.6 on KW38.&lt;/P&gt;&lt;P&gt;In debug build configuration, everything works fine: central device is able to scan, detect and connect to peripheral device.&lt;/P&gt;&lt;P&gt;In release configuration, central is not able to connect anymore. Related BLE connection events are not coming at all. In release configuration, I enabled LTO both at compile and link time and disabled debug info.&lt;/P&gt;&lt;P&gt;If I disabled LTO at compile time, all works fine. But final binary is too large.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I managed to reproduce the problem on SDK examples: TM central and sensor. In central, I made the following changes:&lt;/P&gt;&lt;P&gt;#define gMWS_Enabled_d 1&amp;nbsp; // In app_preinclude.h&lt;/P&gt;&lt;P&gt;void App_Init(void)&amp;nbsp; &amp;nbsp;// In temperature_collector.c&lt;BR /&gt;{&lt;BR /&gt;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;In debug configuration TM demo works fine.&lt;/P&gt;&lt;P&gt;In release configuration, after having enable LTO at compile and link time and disabled debug info, central does not connect anymore to peripheral. So exactly like in case on my application.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If MWS is not enabled, so with original TMC sources, the problem does not occur. So I think the problem is in mws.c.&lt;/P&gt;&lt;P&gt;This guess is confirm by the workaround I managed to apply in my own project: I set mws.c file-specific compiler options and disabled LTO at compile time. However, this does not work on TMC.&lt;/P&gt;&lt;P&gt;I am afraid there might be other problems due to LTO like this, so I need proper solution and not a workaround. Thank you.&lt;/P&gt;</description>
      <pubDate>Thu, 10 Sep 2020 15:14:12 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Enable-LTO-in-compiler-options-break-MWS-based-projects/m-p/1151646#M58044</guid>
      <dc:creator>FedericoWegher</dc:creator>
      <dc:date>2020-09-10T15:14:12Z</dc:date>
    </item>
    <item>
      <title>Re: Enable LTO in compiler options break MWS-based projects</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Enable-LTO-in-compiler-options-break-MWS-based-projects/m-p/1152144#M58055</link>
      <description>&lt;P&gt;I've tried LTO many times in the embedded space and have yet to get working code from it.&lt;BR /&gt;In some versions of the compiler LTO itself is simply broken, always check the compiler notes/changelog.&lt;BR /&gt;&lt;BR /&gt;Predominantly LTO has issues with Volatile and it removes "unused code" that is actually used such as interrupt service routines that are not marked as 'used'.&lt;BR /&gt;&lt;BR /&gt;Any function that is decorated with attribute IRQ must also be decorated with 'used'.&lt;BR /&gt;IRQs should also be decorated with&amp;nbsp;no_instrument_function, and for really obscure reasons needs to be done on both the declarations and definition unlike the other attributes.&lt;BR /&gt;&lt;BR /&gt;Example of a UART ISR:&lt;BR /&gt;&lt;BR /&gt;Declaration:&lt;BR /&gt;&lt;BR /&gt;__attribute__( ( no_instrument_function ) ) void LPUART1_IRQHandler( void ) __attribute__( ( interrupt( "IRQ" ), used, no_instrument_function ) );&lt;BR /&gt;&lt;BR /&gt;Definition:&lt;BR /&gt;&lt;BR /&gt;__attribute__( ( no_instrument_function ) ) void LPUART1_IRQHandler( void )&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;The Instrument stuff has to do with using GCC's built in profiler.&lt;BR /&gt;&lt;BR /&gt;See&amp;nbsp;See Poor Man’s Trace: Free-of-Charge Function Entry/Exit Trace with GNU Tools | MCU on Eclipse&lt;BR /&gt;&lt;A href="http://mcuoneclipse.com/2015/04/04/poor-mans-trace-free-of-charge-function-entryexit-trace-with-gnu-tools/" target="_blank"&gt;http://mcuoneclipse.com/2015/04/04/poor-mans-trace-free-of-charge-function-entryexit-trace-with-gnu-tools/&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I can post my trace.c if anyone cares about it.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Sep 2020 12:54:12 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Enable-LTO-in-compiler-options-break-MWS-based-projects/m-p/1152144#M58055</guid>
      <dc:creator>bobpaddock</dc:creator>
      <dc:date>2020-09-11T12:54:12Z</dc:date>
    </item>
    <item>
      <title>Re: Enable LTO in compiler options break MWS-based projects</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Enable-LTO-in-compiler-options-break-MWS-based-projects/m-p/1152155#M58056</link>
      <description>&lt;P&gt;Thank you for your reply.&lt;/P&gt;&lt;P&gt;Does it mean MCUXpresso SDK and examples cannot be compiled with LTO options?&lt;/P&gt;&lt;P&gt;Please share your trace.c and explain how to use it. It is not clear to me you mentioned instrumentation and tracing.&lt;/P&gt;&lt;P&gt;Best regards.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Sep 2020 13:13:21 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Enable-LTO-in-compiler-options-break-MWS-based-projects/m-p/1152155#M58056</guid>
      <dc:creator>FedericoWegher</dc:creator>
      <dc:date>2020-09-11T13:13:21Z</dc:date>
    </item>
    <item>
      <title>Re: Enable LTO in compiler options break MWS-based projects</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Enable-LTO-in-compiler-options-break-MWS-based-projects/m-p/1152222#M58059</link>
      <description>&lt;P&gt;&lt;SPAN&gt;"Does it mean MCUXpresso SDK and examples cannot be compiled with LTO options?"&lt;BR /&gt;&lt;BR /&gt;I would assume so, as I expect they would not have put in the proper attributes&amp;nbsp;such as 'used'.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Reasons like this is why I only look at the SDK code source to see how it does something and never actually use the code in my products.&lt;BR /&gt;&lt;BR /&gt;Here are the LTO notes from my Makefile from trying to get it to work over the years.&lt;BR /&gt;At least give -flto-report a shot and see if tells you what it removed.&lt;BR /&gt;&lt;BR /&gt;See also if MCU added which I believe&amp;nbsp;are required:&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;#LDFLAGS += -flto -ffunction-sections -fdata-sections&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;# Link-time optimization does not work well with generation of&lt;BR /&gt;# debugging information. Combining -flto with -g is currently&lt;BR /&gt;# experimental and expected to produce unexpected results.&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;# -fwhole-program&lt;/P&gt;&lt;P&gt;# Assume that the current compilation unit represents the whole&lt;BR /&gt;# program being compiled. All public functions and variables with the&lt;BR /&gt;# exception of main and those merged by attribute externally_visible&lt;BR /&gt;# become static functions and in effect are optimized more&lt;BR /&gt;# aggressively by interprocedural optimizers.&lt;/P&gt;&lt;P&gt;# This option should not be used in combination with -flto. Instead&lt;BR /&gt;# relying on a linker plugin should provide safer and more precise&lt;BR /&gt;# information.&lt;/P&gt;&lt;P&gt;# Link Time Optimization (LTO) gives GCC the capability of dumping its&lt;BR /&gt;# internal representation (GIMPLE) to disk, so that all the different&lt;BR /&gt;# compilation units that make up a single executable can be optimized&lt;BR /&gt;# as a single module. This expands the scope of inter-procedural&lt;BR /&gt;# optimizations to encompass the whole program (or, rather, everything&lt;BR /&gt;# that is visible at link time).&lt;/P&gt;&lt;P&gt;# To see it in action, complle with -v -Wl,-v -save-temps and spot for&lt;BR /&gt;# /cc1, /as, /collect2, /lto-plugin, /lto1 and /ld.&lt;/P&gt;&lt;P&gt;# LTO is broken until ARM version GCC 8.3.&lt;/P&gt;&lt;P&gt;# Note that the same optimization flags (-flto -Os) should be passed&lt;BR /&gt;# both at compile time and at link time.&lt;/P&gt;&lt;P&gt;# LTO requires the same optimization setting be passed to the linker&lt;BR /&gt;# that the compile phase is using. Keep this even when LTO is&lt;BR /&gt;# disabled for consistency in the options between compile and linkng.&lt;BR /&gt;LDFLAGS += -O$(OPT)&lt;/P&gt;&lt;P&gt;#CFLAGS += -flto&lt;BR /&gt;#CPPFLAGS += -flto&lt;/P&gt;&lt;P&gt;# Enables the use of a linker plugin during link-time optimization.&lt;BR /&gt;#LDFLAGS += -fuse-linker-plugin&lt;/P&gt;&lt;P&gt;# Specify -flto=jobserver to use GNU make's job server mode to&lt;BR /&gt;# determine the number of parallel jobs. This is useful when the&lt;BR /&gt;# Makefile calling GCC is already executing in parallel. You must&lt;BR /&gt;# prepend a ‘+’ to the command recipe in the parent Makefile for this&lt;BR /&gt;# to work. This option likely only works if MAKE is GNU make.&lt;BR /&gt;#CFLAGS += -flto=jobserver&lt;BR /&gt;#CPPFLAGS += -flto=jobserver&lt;BR /&gt;#LDFLAGS += -flto=jobserver&lt;/P&gt;&lt;P&gt;#LDFLAGS += -flto -ffunction-sections -fdata-sections&lt;/P&gt;&lt;P&gt;# Prints a report with internal details on the workings of the&lt;BR /&gt;# link-time optimizer. The contents of this report vary from version&lt;BR /&gt;# to version. It is meant to be useful to GCC developers when&lt;BR /&gt;# processing object files in LTO mode (via -flto).&lt;BR /&gt;#LDFLAGS += -flto-report&lt;/P&gt;&lt;P&gt;# -why_live symbol_name&lt;BR /&gt;# Logs a chain of references to symbol_name. Only applicable with&lt;BR /&gt;# -dead_strip. It can help debug why something that you think should&lt;BR /&gt;# be dead strip removed is not removed.&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;A href="https://gcc.gnu.org/wiki/LinkTimeOptimization" target="_blank"&gt;https://gcc.gnu.org/wiki/LinkTimeOptimization&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;If the code is to big without LTO the code may simply be to big.&lt;BR /&gt;&lt;BR /&gt;In the past I would have suggested using Gimpel Lint for static analysis to find out how to make the code smaller (In effect doing what LTO was doing).&lt;BR /&gt;Alas they don't sell Lint any more only Lint+ with a bizarre&amp;nbsp;unusable license model. &lt;LI-EMOJI id="lia_disappointed-face" title=":disappointed_face:"&gt;&lt;/LI-EMOJI&gt;&lt;BR /&gt;&lt;BR /&gt;Trace is unrelated to LTO issue, it is related&amp;nbsp;to the attributes.&amp;nbsp; I'll make a separate&amp;nbsp;post about that sometime.&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Sep 2020 15:32:59 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Enable-LTO-in-compiler-options-break-MWS-based-projects/m-p/1152222#M58059</guid>
      <dc:creator>bobpaddock</dc:creator>
      <dc:date>2020-09-11T15:32:59Z</dc:date>
    </item>
    <item>
      <title>Re: Enable LTO in compiler options break MWS-based projects</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Enable-LTO-in-compiler-options-break-MWS-based-projects/m-p/1153952#M58122</link>
      <description>&lt;P&gt;Thank you for your replies.&lt;/P&gt;&lt;P&gt;Unfortunately, all flags are already set properly for linker and compierl: -flto, -ffunction-sections, -fdata-section, --gc-sections, -Os, -s. ALso -fwhole-program cannot be applied to SDK case, being several translation units.&lt;/P&gt;&lt;P&gt;Finally, I checked differences in MWS.o build with and without -flto at compile time. I see nothing relevant. So I cannot explain why removing -ftlo as compiler option to MWS directory in sdk fixes the issues.&lt;/P&gt;&lt;P&gt;I really need NXP feedback about this point.&lt;/P&gt;</description>
      <pubDate>Wed, 16 Sep 2020 07:33:17 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Enable-LTO-in-compiler-options-break-MWS-based-projects/m-p/1153952#M58122</guid>
      <dc:creator>FedericoWegher</dc:creator>
      <dc:date>2020-09-16T07:33:17Z</dc:date>
    </item>
  </channel>
</rss>

