int foo(int v) { return bar(2 * v); } int bar(int a) { if (a > 0) return 1; // else return baz(a); } int baz(int a) { if (a > 0) throw new Exception("a > 0"); return a - 1; } -------- bar is only inlined by foo when 'else' is present.
Workaround for a performance regression in std.utf caused by this. https://github.com/D-Programming-Language/phobos/pull/2566
Commits pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/fea9bad209272b032fc8e38d358f06035676b14f workaround inline Issue 7625 - add explicit else branches so that dmd can inline those functions https://github.com/D-Programming-Language/phobos/commit/335228cb079ccc2060790a2b23bca5db645fa7f0 Merge pull request #2566 from MartinNowak/workaround7625 workaround inline Issue 7625
Commits pushed to 2.067 at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/fea9bad209272b032fc8e38d358f06035676b14f workaround inline Issue 7625 https://github.com/D-Programming-Language/phobos/commit/335228cb079ccc2060790a2b23bca5db645fa7f0 Merge pull request #2566 from MartinNowak/workaround7625
https://github.com/D-Programming-Language/dmd/pull/4919
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/f5261a2a978831e10a2ff36d3e342770f846dd1b fix Issue 7625 - inlining only works with explicit else branch https://github.com/D-Programming-Language/dmd/commit/26bd7451bf69051d7662f807978441cf1eefeb2f Merge pull request #4919 from WalterBright/fix7625 fix Issue 7625 - inlining only works with explicit else branch
The fix was incomplete. Here's a test case for which dmd fails to inline a function that's basically identical to another inlined function, the only difference being the omission of `else`: ------ int tembo(int x, int y) { if (y == 0) return 0; x++; return x / y; } int pembo(int x, int y) { if (y == 0) return 0; else { x++; return x / y; } } int twiga(int x, int y, int z) { auto w = tembo(x, y); return w * z; } int simba(int x, int y, int z) { auto w = pembo(x, y); return w * z; } ------ Compiling with `dmd -O -inline` and disassembling, I found that twiga still contains a function call to tembo, whereas simba inlines pembo. Commenting out the x++ from tembo and pembo causes successful inlining of both functions in twiga and simba. So it seems that the inliner is still unable to deal with the case where the else block (with omitted `else`) contains anything more than just a simple return statement.
https://github.com/D-Programming-Language/dmd/pull/5626(In reply to hsteoh from comment #6) > The fix was incomplete. Do further generalized fix. https://github.com/D-Programming-Language/dmd/pull/5626
Related? https://issues.dlang.org/show_bug.cgi?id=15483
*** Issue 15483 has been marked as a duplicate of this issue. ***
@ibuclaw updated dlang/dmd pull request #7690 "fix Issue 7625 - inlining only works with explicit else branch" fixing this issue: - fix Issue 7625 - inlining only works with explicit else branch https://github.com/dlang/dmd/pull/7690
This issue was fixed by https://github.com/dlang/dmd/pull/11236
dlang/dmd pull request #7690 "fix Issue 7625 - inlining only works with explicit else branch" was merged into master: - f0e3d93b715cda77b6b5cac7966243c5e7b0c232 by Iain Buclaw: fix Issue 7625 - inlining only works with explicit else branch https://github.com/dlang/dmd/pull/7690