Questions tagged [as-if]

The as-if rule allows arbitrary compiler optimizations under the premise that the observable behavior does not change.

As long as the compiler guarantees that

  • all volatiles are stable at sequence points (that is all past and future effects have either not started or are globally visible) and accesses to volatiles are not reordered
  • the externally observable behavior is identical (that is, output is ordered correctly in respect of other output and input prompts, output is identical, files being written contain the correct data at program end or after closing the handles, etc.)
  • changes to the floating-point environment are properly realized and not reordered

any binary code that the compiler might have created behaves as if it was the exact same code the programmer wrote. Since it is not possible for an observer to tell the difference, it is legitimate to perform any and all optimizations that do not violate the guarantees of the as-if rule.

16 questions
110
votes
3 answers

What exactly is the "as-if" rule?

As the title says: What exactly is the "as-if" rule? A typical answer one would get is: The rule that allows any and all code transformations that do not change the observable behavior of the program From time to time, we keep getting behaviors…
Alok Save
  • 202,538
  • 53
  • 430
  • 533
75
votes
4 answers

Loop with a zero execution time

Is it possible to have a loop which has a zero execution time? I would think that even an empty loop should have an execution time since there is an overhead associated with it.
SudarakaR
  • 937
  • 6
  • 6
14
votes
3 answers

Is the explanation of relaxed ordering erroneous in cppreference?

In the documentation of std::memory_order on cppreference.com there is an example of relaxed ordering: Relaxed ordering Atomic operations tagged memory_order_relaxed are not synchronization operations; they do not impose an order among concurrent…
abigaile
  • 181
  • 5
9
votes
1 answer

as-if rule and removal of allocation

The "as-if rule" gives the compiler the right to optimize out or reorder expressions that would not make a difference to the output and correctness of a program under certain rules, such as; §1.9.5 A conforming implementation executing a…
Johan Lundberg
  • 26,184
  • 12
  • 71
  • 97
4
votes
2 answers

Where are the statement of or the foundations for the "as if" rule in the C++ Standard?

After a little google search (for instance, site:eel.is "as if rule") I couldn't find a proper place where the so called "as if" rule is clearly stated in the C++ standard. All I could find is that in those places within the standard where it is…
Tarc
  • 3,214
  • 3
  • 29
  • 41
4
votes
2 answers

Timing vs the "as-if" rule

There is a great question about the "as-if" rule in general, but I wonder if there are any exceptions when it comes to measuring time. Consider this (taken from here slightly modified): using std::chrono; auto begin = steady_clock::now(); auto…
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
4
votes
2 answers

Is store reordering allowed by C++ as-if rule?

The "as-if" rule basically defines what transformations an implementation is allowed to perform on a legal C++ program. In short, all transformations that do not affect a program's observable behavior are allowed. As to what exactly "observable…
Eric Z
  • 14,327
  • 7
  • 45
  • 69
3
votes
2 answers

Why can't the C++ compiler elide the move when moving a POD into an optional with RVO?

Consider the following code (godbolt): #include #include struct LargeType { std::array largeContents; }; LargeType doSomething(); std::optional wrapIntoOptional(){ return std::optional
gexicide
  • 38,535
  • 21
  • 92
  • 152
3
votes
2 answers

What is the rationale behind copy elision in C++?

What is the reason the C++ standard allows(requires) compilers to optimize away calls to copy constructor(in certain cases), even though it might contain observable side effects? If I'm not mistaken, the "as if" rule already allows compilers to…
wolfofuniverse
  • 441
  • 4
  • 8
3
votes
3 answers

About time/space complexity in C/C++ standards

Recently I've read things about abstract machine and as-if rule (What exactly is the "as-if" rule?), and the requirements on time complexity of standard library (like this one: Is list::size() really O(n)?). Are the time/space complexity…
Willy
  • 581
  • 2
  • 10
2
votes
1 answer

Aliasing signed and unsigned integers during optimization

A signed& cannot be initialized from an unsigned& (and vice versa), but strict aliasing rules allow to read/write a signed object through an unsigned& (and vice versa), see C++20 standard [basic.lval]#11.2. This theoretically could be used in…
Dr. Gut
  • 2,053
  • 7
  • 26
2
votes
4 answers

Evaluation of constants in for loop condition

for(int i = 0; i < my_function(MY_CONSTANT); ++i){ //code using i } In this example, will my_function(MY_CONSTANT) be evaluated at each iteration, or will it be stored automatically? Would this depend on the optimization flags used?
Learning is a mess
  • 7,479
  • 7
  • 35
  • 71
1
vote
0 answers

Is this execution reordering possible

As we know, the compiler or the CPU may reorder the execution as they want, only if they follow the as-if rule. For example, if we have such a piece of code: C = A + B; D = E + F; The compiler or the CPU may execute D = E + F before C = A + B. I…
Yves
  • 11,597
  • 17
  • 83
  • 180
1
vote
1 answer

Does as-if rule allow this kind of execution reordering

As we know, the compiler or the CPU may reorder the execution as they want, only if they follow the as-if rule. For example, if we have such a piece of code: C = A + B; D = E + F; The compiler or the CPU may execute D = E + F before C = A + B. I…
Yves
  • 11,597
  • 17
  • 83
  • 180
1
vote
1 answer

Why can't an infinite loop be eliminated?

The "as-if" rule is covered by these rules: The least requirements on a conforming implementation are: Access to volatile objects are evaluated strictly according to the rules of the abstract machine. At program termination, all data written into…
1
2