csynchronizationcpu

Out of order execution - can it bypass control statements?


Regarding OOO, lets assume I have one process only (with one thread) that runs this code:

void foo() {

    if (x == 0) {
        return;
    }

    y->data = 5;

}

Now, lets assume I know that y is valid only if x is not zero. from hardware perspective, can the CPU execute y->data = 5 before reading x? It may cause the CPU to access a NULL/garbage pointer and crashs.

And if not, what is the reason for this? because if/while/for/goto are control statements and the CPU will not fetch ahead instructions when it sees a control statement?

A I remember, OOO should be 100% transparent to one thread executing its instructions.


Solution

  • Depends on how you look at it.

    From the user's perspective, the behavior of the program must be "as if" it was run sequentially.
    In other words, there is no visible difference between being run sequentially and being run with OOE. (aside from maybe performance)

    From the CPU's perspective, yes it actually can bypass the if-statement and execute y->data = 5;. But this is because of branch prediction rather than OOE.


    On a modern processor, it is possible for the thread to mispredict the branch:

    if (x == 0) {
        return;
    }
    

    and actually try to execute y->data = 5;...

    If this happens and y is a bad pointer, it will get hardware exception, but that exception is withheld since the execution is still in speculation mode.

    Once the thread realizes that it has mispredicted the branch, it will throw away everything past the branch (including the exception).

    So in the end, there is nothing to worry. Even if the processor tries to do something it can't, it won't affect the sequential behavior.

    In other words, a modern processor will clean up after itself if it makes a mess that isn't your fault.


    Things get uglier when you have multiple threads, but that's outside the scope of this question.