none
Compiler/Processor reordering in single processor single thread code RRS feed

  • Question

  • Hi,

    Assume the following code:

    if (x == 10)

    {

      /* do some operation */

       x=11;

    }

    In the above code, reordering can happen either by the compiler or processor in such a way the x value will be updated to 11, before checking the x value is equal to 10. Am i correct? 

    Just want to understand reordering can be issue in Single processor/Single thread also. If it is not what is the expected behaviour in the above case?

    How MemoryBarrier works? 

    Is it releated to that particular processor where the thread executing the MemoryBarrier instruction or it send message to all the processor?

    • Edited by Boomi.s Friday, May 15, 2015 1:56 AM
    Friday, May 15, 2015 1:31 AM

Answers

  • In the above code, reordering can happen either by the compiler or processor in such a way the x value will be updated to 11, before checking the x value is equal to 10. Am i correct? 

    Only when update and checking are in different threads. Not in the same thread. (Can you tell why?)

    How MemoryBarrier works? Is it releated to that particular processor where the thread executing the MemoryBarrier instruction or it send message to all the processor?

    Basically it's local (but needs cooperation among all processors that access the data in question).

    -- pa

    Friday, May 15, 2015 3:22 AM
  • Yes, some code lines can be reordered as far as the user does not care (or the compiler believes so). The compiler definitely does have some knowledge of which operations depend on which data in a single threaded flow.

    -- pa


    • Edited by Pavel A Sunday, May 17, 2015 2:13 AM
    • Marked as answer by Boomi.s Sunday, May 17, 2015 6:35 AM
    Sunday, May 17, 2015 2:10 AM

All replies

  • In the above code, reordering can happen either by the compiler or processor in such a way the x value will be updated to 11, before checking the x value is equal to 10. Am i correct? 

    Only when update and checking are in different threads. Not in the same thread. (Can you tell why?)

    How MemoryBarrier works? Is it releated to that particular processor where the thread executing the MemoryBarrier instruction or it send message to all the processor?

    Basically it's local (but needs cooperation among all processors that access the data in question).

    -- pa

    Friday, May 15, 2015 3:22 AM
  • Only when update and checking are in different threads. Not in the same thread. (Can you tell why?)

    I dont know. But assume the following situation, the number in the bracket shows the order. Can this happen?

        f->A = 4; // (4)<o:p></o:p>

        f->B = 8; // (3)<o:p></o:p>

        f->C = 15; // (1)<o:p></o:p>

        f->D = 16; // (5)<o:p></o:p>

        f->Initialized = TRUE; // (2)

    Basically it's local (but needs cooperation among all processors that access the data in question).

    - So how this is implemented. Any reference to get more information?

    Friday, May 15, 2015 4:05 AM
  • http://channel9.msdn.com/Shows/Going+Deep/Cpp-and-Beyond-2012-Herb-Sutter-atomic-Weapons-1-of-2

    This links says that the out of order can happen in single thread also. 

    Can transform this                  to 
    x = "life"           z = "everything"
    y = "universe" y = "universe"
    z = "everything" x = "life"

    I assume in the following reordering can't happen assuming compiler has some knowledge

    if (x == 10)                
    {
    /* do operation */
    x = 11;
    }           
    // No reordering on the x variable operations.

    a = 10;
    b = 11;
    c = 12;

    d = a + b + c +d;

    // Reordering can happen in the initialization of a,b,c. But not between initialization of a,b,c and d.


    • Edited by Boomi.s Saturday, May 16, 2015 7:41 PM
    Saturday, May 16, 2015 5:28 PM
  • If I would be interested in compiler instruction reordering, I'd look for documentation/source of free compilers like this llvm:
    http://llvm.org/docs/
    to get an impression, how contemporary compilers are ticking 
    http://llvm.org/docs/Passes.html#passes-die
    which gives also keywords, like control-flow, basic blocks, dominators, dead .. .

    With kind regards

    Besides, in your first piece of code, there is no control-flow, second there is one ... probably one reason for a compiler, not to reorder the second one - at first glance ...


    Sunday, May 17, 2015 7:31 AM