Uploaded image for project: 'Embedded Software & Tools'
  1. Embedded Software & Tools
  2. EXT_EP-11936

Compiler incorrectly optimizes away a conditional test of a static variable

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Medium Medium
    • Code Generation Tools
    • CODEGEN-12833
    • ARMCLANG_3.2.0.LTS
    • ARMCLANG_4.0.2.LTS*
    • default
    • Hide
      Workaround #1:

      There is an empty while loop at the end of the function that contains the condition statement in question:

        while (true) ;

      The 4.0.0.LTS compiler makes the assumption that this is a noreturn function and so the "if (!assert_fired)" condition check is optimized out.

      To avoid having the "if (!assert_fired)" condition optimized away, insert an asm(" nop\n"); statement inside the while (true) loop at the end of the assertHandler() function in the test case source file.

         while (true) {
            asm(" nop\n");
         }

      Workaround #2:

      A more heavy-handed workaround is to apply an "optnone" attribute to the assertHandler() function like this:

      __attribute__((optnone))
      void Server::assertHandler(etl::string_view description, etl::string_view pretty_function,
                                 etl::string_view file, uint32_t line)
      Show
      Workaround #1: There is an empty while loop at the end of the function that contains the condition statement in question:   while (true) ; The 4.0.0.LTS compiler makes the assumption that this is a noreturn function and so the "if (!assert_fired)" condition check is optimized out. To avoid having the "if (!assert_fired)" condition optimized away, insert an asm(" nop\n"); statement inside the while (true) loop at the end of the assertHandler() function in the test case source file.    while (true) {       asm(" nop\n");    } Workaround #2: A more heavy-handed workaround is to apply an "optnone" attribute to the assertHandler() function like this: __attribute__((optnone)) void Server::assertHandler(etl::string_view description, etl::string_view pretty_function,                            etl::string_view file, uint32_t line)

      The attached file has a function that begins ...

      void Server::assertHandler(etl::string_view description, etl::string_view pretty_function,
                                 etl::string_view file, uint32_t line) {
        static bool assert_fired = false;
      
        // Protection from the infinite calling to the handler
        // if some of the code bellow triggers the assert.
        if (!assert_fired) {
          assert_fired = true;
      

      Build it ...

      % tiarmclang @options.txt file.cpp
      

      Ignore the warning and note diagnostics.

      Create the interlisted, demangled, disassembly with the command ...

      % tiarmobjdump --demangle --source file.o > disassembly.txt
      

      Inspect the disassembly to find the function begins ...

      00000000 <figure::lib::logger::Server::assertHandler(etl::basic_string_view<char, etl::char_traits<char>>, etl::basic_string_view<char, etl::char_traits<char>>, etl::basic_string_view<char, etl::char_traits<char>>, unsigned int)>:
      ;                            etl::string_view file, uint32_t line) {
             0: b08c         	sub	sp, #48
      ;       : mbegin(begin_)
             2: f240 0700    	movw	r7, #0
      ;     assert_fired = true;
      

      The static variable is assert_fired is assigned the value 1 without the conditional check that appears in the C++ source.

            syncuser TI User
            syncuser TI User
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: