June 16, 2023 [Issue 23996] New: pragma(assume) | ||||
---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=23996 Issue ID: 23996 Summary: pragma(assume) Product: D Version: D2 Hardware: All OS: All Status: NEW Severity: enhancement Priority: P1 Component: dmd Assignee: nobody@puremagic.com Reporter: qs.il.paperinik@gmail.com Add a pragma with the identifier assume. It takes 1 argument that must be an expression convertible to `bool` and (depending on decisions) 1 optional additional argument that must be a string or multiple that are used to make a string. The `bool` expression is not evaluated. There are 2 ways to use `pragma(assume)`: * `pragma(assume, x > 0);` The optimizer may freely assume that `x > 0` and use this information even backwards in time. (This is the strongest usage.) * `pragma(assume, x > 0) NoScopeStatement` The optimizer may assume that `x > 0` in the code generation for the `NoScopeStatement`, which may be a single statement or a block statement. After the `NoScopeStatement`, the assumption must not be used anymore (except, of course, it was redundant and the information could be derived from elsewhere). An example could be that a function with an unknown implementation is known by its specification to return non-negative values or -1 (an example is `indexOf`). After handling the `-1` case, its result is known to be non-negative. It’s essentially impossible to provide real-world use-cases as they depend on the very details of the optimizer. C++23 formally added `assume` as an attribute as to streamline various compiler extensions. Practically, an assumption should not be put in code unless profiling revealed that it made a difference. One reason is that assumptions cause undefined behavior (UB) if they’re violated. For D, a consideration is `@safe`. A D compiler may definitely make use of an assumption in `@system` code as much as it wants; it may also make use of them in `@safe` code if either we accept it causing UB there (despite what `@safe` advertises, it’s an intentional loophole like `@trusted` is) or in cases where using the assumption leads to different code-gen without causing UB. A trivial example of the latter would be: `pragma(assume, x == 3) writeln(x)` Even if `x` is not `3`, replacing `writeln(x)` by `writeln(3)` does not constitute UB. -- |
Copyright © 1999-2021 by the D Language Foundation