[Issue 22277] New: removing strongly pure function calls is an incorrect optimization
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sat Sep 4 12:22:02 UTC 2021
https://issues.dlang.org/show_bug.cgi?id=22277
Issue ID: 22277
Summary: removing strongly pure function calls is an incorrect
optimization
Product: D
Version: D2
Hardware: All
OS: All
Status: NEW
Severity: normal
Priority: P1
Component: dmd
Assignee: nobody at puremagic.com
Reporter: dkorpel at live.nl
Currently, with -O -release and strongly pure nothrow functions, DMD likes to
remove calls to it because it determines it has 'no side effects'. This goes
wrong with:
- functions that halt the program with `assert(0)`
- fake pure functions (memory allocation)
```
import core.stdc.stdio;
void free(immutable void* mem) pure nothrow {
debug printf("memory was freed\n");
}
noreturn panic(string msg) pure nothrow {
assert(0, msg);
}
void assertPositive(int x) pure nothrow {
if (x < 0) {
assert(0);
}
}
void main()
{
free(null);
assertPositive(-3);
panic("panic!");
}
```
Compile with -O -release -debug and you'll see no output. Remove -release and
it works as intended.
I want to make GC.addRange pure so it can be used in custom allocators (issue
16982) but it's very important dmd doesn't remove calls to it.
--
More information about the Digitalmars-d-bugs
mailing list