Topic : Invasive peephole optimization breaks code

Forum : ST7/STM8

Original Post
Post Information Post
August 29, 2012 - 12:17pm
Guest

While compiling and testing a small test application using ChibiOS (4.2.0) with STM8L (STM8L151U4), I noticed that some code has been removed from the optimizator breaking the OS assert macro.

Compiler is V2.44.12.199.

The following code:

Thread *chSchReadyI(Thread *tp) {
  Thread *cp;

  /* Integrity checks.*/
  chDbgAssert((tp->p_state != THD_STATE_READY) &&
              (tp->p_state != THD_STATE_FINAL),
              "chSchReadyI(), #1",
              "invalid state");

  tp->p_state = THD_STATE_READY;
...

Where the chDbgAssert is defined as:

#define chDbgAssert(c, m, r) {                                          \
  if (!(c))                                                             \
    chDbgPanic(m);                                                      \
}

Is correctly expanded by the preprocessor in:

Thread * chSchReadyI ( Thread * tp ) {
    Thread * cp ;
    {
        if ( ! ( ( tp -> p_state != 0 ) && ( tp -> p_state != 14 ) ) ) chDbgPanic ( "chSchReadyI(), #1") ;
        }
    ;
    tp -> p_state = 0 ;

but once complied with Peephole optimization:

              ; FUNCTION ?chSchReadyI (BEGIN)
              ; Register-parameter tp (XW) is relocated (AUTO)
              ; SOURCE LINE # 82 
0000 89                                PUSHW  X
0001 89                                PUSHW  X
              ; SOURCE LINE # 89 
0002 AE0000     F                      LDW    X,#HIGH(?STR?CHSCHD?BASE)
0005 CD0000     F                      CALL   ?chDbgPanic
              ; SOURCE LINE # 91 
0008 1E03       F                      LDW    X,(003H,SP)   ; [ tp ]
000A 6F07                              CLR    (007H,X)

Without peephole optimization:

              ; FUNCTION ?chSchReadyI (BEGIN)
              ; Register-parameter tp (XW) is relocated (AUTO)
              ; SOURCE LINE # 82 
0000 89                                PUSHW  X
0001 89                                PUSHW  X
              ; SOURCE LINE # 89 
0002 1E03       F                      LDW    X,(003H,SP)   ; [ tp ]
0004 1C0007                            ADDW   X,#00007H
0007 A600                              LD     A,#000H
0009 F1                                CP     A,(X)
000A 270A                              JREQ   ?LAB_0001
000C 1E03       F                      LDW    X,(003H,SP)   ; [ tp ]
000E 1C0007                            ADDW   X,#00007H
0011 A60E                              LD     A,#00EH
0013 F1                                CP     A,(X)
0014 2606                              JRNE   ?NXT_0002
0016         ?LAB_0001:
0016 AE0000     F                      LDW    X,#HIGH(?STR?CHSCHD?BASE)
0019 CD0000     F                      CALL   ?chDbgPanic
001C         ?NXT_0002:
              ; SOURCE LINE # 91 
001C 1E03       F                      LDW    X,(003H,SP)   ; [ tp ]
001E 1C0007                            ADDW   X,#00007H
0021 A600                              LD     A,#000H
0023 F7                                LD     (X),A

Sadly I can't compile the whole project with asserts and peephole opt for my board as I'm breaking the device flash size (16k only).

I had the occasion of testing the non optimized code with a STM8L discovery board and it works as expected while the optimized one is always calling the assert as expected from the assembler shown.
I can also provide complete listings and preprocessor intermediate files if needed.

Code for the OS can be downloaded from:
http://chibios.org/dokuwiki/doku.php?id=start
The version I'm using is 2.4.0, still, I think the problem is generic enough to be reproducible in any other OS version (and I guess witha a small test too).

So, is any workaround available except from disabling peephole that is not possible for my target?

Replies
Post Information Post
+1
0
-1
August 29, 2012 - 1:16pm
Raisonance Support Team

Hello,

Thanks for the detailed reporting. We will enquire this but it can take some time because our STM8 compiler experts are out of office right now.

In the meantime, you should be able to workaround the problem by disabling peephole on the file that contains this function. You don't have to disable it for the whole project. Just set the focus on the file in the project view, and then change the optimization options in the Properties view. Then you will see a red mark next to the file, indicating that it does not use the same options as the other files. To revert this, right-click on the file and click "reset local options".

And you can put this function in a separate file so that there is really only this function that is compiled without optimization, and then you should remain in the flash size boundary.

Of course this is only a temporary workaround. We'll come back to you shortly with a better answer.

Best Regards,

Vincent

+1
0
-1
August 29, 2012 - 2:26pm
Raisonance Support Team

Hello,

I have made some tests and I confirm that it is a bug in our compiler.
Thanks again for the detailed reporting and sorry for the inconvenience.

We will correct it as soon as possible and send you a test version, but not before next week I'm afraid.

In the meantime, the workaround explained in my previous post is still valid, and this other workaround might be better:


Thread *chSchReadyI(Thread *tp) {
  Thread *cp;

  /* Integrity checks.*/
#if 1 //workaround
  //this compiles fine
  volatile unsigned char mythreadstate = tp->p_state;
  chDbgAssert((mythreadstate != THD_STATE_READY) &&
              (mythreadstate != THD_STATE_FINAL),
              "chSchReadyI(), #2",
              "invalid state");
#else //workaround
  //this compiles wrong when optimized (OK without peephole)
  chDbgAssert((tp->p_state != THD_STATE_READY) &&
              (tp->p_state != THD_STATE_FINAL),
              "chSchReadyI(), #1",
              "invalid state");
#endif //workaround

  tp->p_state = THD_STATE_READY;
}

Change the "unsigned char" to match the type of p_state.

I hope it helps.

Best Regards,

Vincent

+1
0
-1
August 29, 2012 - 2:40pm
Raisonance Support Team

Hi again,

Please follow this issue here from now on:
http://support-raisonance.com/extranet/bugtraking/view.php?id=3876

Best Regards,

Vincent

+1
0
-1
September 11, 2012 - 6:21pm
Guest

Hi, I noticed that the issue has been promptly resolved.
Any news about the beta or the full release?

Best Regards,
Egon

+1
0
-1
September 12, 2012 - 11:17am
Raisonance Support Team

Hello,

The official version is expected before a few weeks.

The Raisonance Support Team will provide you information so you'll be able to
download the beta version.

St├ęphane