D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7625 - inlining only works with explicit else branch
Summary: inlining only works with explicit else branch
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P5 normal
Assignee: No Owner
URL:
Keywords: performance, pull
: 15483 (view as issue list)
Depends on:
Blocks:
 
Reported: 2012-03-01 16:10 UTC by Martin Nowak
Modified: 2022-02-18 17:14 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Martin Nowak 2012-03-01 16:10:26 UTC
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.
Comment 1 Martin Nowak 2014-09-29 02:27:28 UTC
Workaround for a performance regression in std.utf caused by this.
https://github.com/D-Programming-Language/phobos/pull/2566
Comment 2 github-bugzilla 2014-09-29 08:29:03 UTC
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
Comment 5 github-bugzilla 2015-09-21 12:10:49 UTC
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
Comment 6 hsteoh 2016-03-30 16:53:48 UTC
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.
Comment 7 Kenji Hara 2016-04-03 15:57:24 UTC
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
Comment 8 thomas.bockman 2016-04-04 18:59:08 UTC
Related? https://issues.dlang.org/show_bug.cgi?id=15483
Comment 9 Kenji Hara 2016-04-05 13:07:21 UTC
*** Issue 15483 has been marked as a duplicate of this issue. ***
Comment 10 Dlang Bot 2022-02-18 13:35:42 UTC
@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
Comment 11 Iain Buclaw 2022-02-18 14:17:42 UTC
This issue was fixed by https://github.com/dlang/dmd/pull/11236
Comment 12 Dlang Bot 2022-02-18 17:14:23 UTC
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