C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Every variable is volatile, everything is laundered, no optimisation

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Fri, 25 Aug 2023 11:44:42 +0200
Hi Frederick, I like that you try to describe the effects of what you want as how the code would have been written instead, at least for (1) and (2). But it is much more involved than that:  - What about called functions? Are the rules also valid within those? What about template functions? What about used operators?  - All objects are treated as volatile. Including parameters, return values, parameters of function calls (if DoSomething would have parameters)? Including member variables of used classes/structs?  - All pointers are automatically std::launder'ed. When? After each line, after each instruction, after each sub-expression? With launder, you mean p = std::launder(p). For which pointers is this valid? Any pointer used in the C++ program (are they registered somewhere)? Pointers used in the instructions within the function? What about called functions, operators, ..., which internally use pointers?  - All objects are treated as volatile. Does that do, what you expect? Variables can be put into processor registers. With the as-if-rule, the optimizer can remove if conditions even for volatile variables. In this case the pointers are provided by the caller, but perhaps this function is inlined and the target of the pointers is known and local to the calling functions.   The overall question is, why would you want to have such a feature? For making wrong code valid? For debugging purposes?   Most of the effects you expect would be visible only at the assembler level or in multithreaded code. For multithreaded code one should use correct synchronization to begin with. Debugging assembler level is kind of outside of the scope of the C++ standard. Better each implementation provides a way to generate debug-friendly code, e.g. with a switch like '-O0'.   Best, Sebastian   -----Ursprüngliche Nachricht----- Von:Frederick Virchanza Gotham via Std-Proposals <std-proposals_at_[hidden]> Gesendet:Fr 25.08.2023 11:24 Betreff:[std-proposals] Every variable is volatile, everything is laundered, no optimisation An:std-proposals <std-proposals_at_[hidden]>; CC:Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>; I've occasionally seen people use the compiler switch "-O0" with the GNU compiler and also with LLVM. What if we had a standardised way of marking a function as 'verbose' to tell the compiler: (1) All objects are to be treated as 'volatile' (2) All pointers are automatically std::launder'ed (3) No shortcuts are taken for aliasing Something like as follows: void Func(int *const p, double *const q) __verbose {    *p = 0;    *q = 67.5;    if ( *p ) DoSomething(); } In the above function, normally the 'if' statement would be removed by the optimiser. But since the function is marked as 'verbose', the two pointers 'p' and 'q' are treated as pointing to volatile, and also there's no anti-aliasing assumption between 'p' and 'q'. -- Std-Proposals mailing list Std-Proposals_at_[hidden] https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2023-08-25 09:44:44