Skip to content

Conversation

@ibuclaw
Copy link
Member

@ibuclaw ibuclaw commented Jan 12, 2018

Optimization opportunity noticed in #7677.

For static arrays, slice and indexes using values known at compile time should now never generate array bounds checks at codegen.

Before:

int[10] sa;
enum n = 2;                  // upperInRange
enum m = 5;                  // | lowerLessThan
                             // | |  check code
{ auto s = sa[0 .. 0];     } // 1 1  NULL
{ auto s = sa[0 .. $];     } // 0 1  ($ <= 10           )
{ auto s = sa[1 .. $];     } // 0 0  ($ <= 10 && 1 <= $ )
{ auto s = sa[$ .. $];     } // 0 0  ($ <= 10 && $ <= $ )
{ auto s = sa[0 .. $/n];   } // 0 1  ($/n <= 10         )
{ auto s = sa[$/n .. $];   } // 0 0  ($ <= 10 && 5 <= $ )
{ auto s = sa[$/m .. $/n]; } // 0 0  ($/n <= 10 && $/n <= $/m)

After

int[10] sa;
enum n = 2;                  // upperInRange
enum m = 5;                  // | lowerLessThan
                             // | |  check code
{ auto s = sa[0 .. 0];     } // 1 1  NULL
{ auto s = sa[0 .. $];     } // 1 1  NULL
{ auto s = sa[1 .. $];     } // 1 1  NULL
{ auto s = sa[$ .. $];     } // 1 1  NULL
{ auto s = sa[0 .. $/n];   } // 1 1  NULL
{ auto s = sa[$/n .. $];   } // 1 1  NULL
{ auto s = sa[$/m .. $/n]; } // 1 1  NULL

For reviewing the test changes (mostly just whitespace alignment with the rest of the function).
https://github.com/dlang/dmd/pull/7682/files?w=1

@dlang-bot
Copy link
Contributor

Thanks for your pull request, @ibuclaw!

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

@ibuclaw
Copy link
Member Author

ibuclaw commented Jan 12, 2018

@nordlow - we can do this at least. :-)

@nordlow
Copy link
Contributor

nordlow commented Jan 12, 2018

@ibuclaw - great

@wilzbach wilzbach closed this Jan 12, 2018
@wilzbach wilzbach reopened this Jan 12, 2018
@wilzbach
Copy link
Contributor

closed and reopened to restart SemaphoreCI (after moving it to my account, s.t. @ibuclaw can login there via SSH too). If you want to be able to do the same, login once via GitHub at Semaphore and then let me know.

Copy link
Contributor

@wilzbach wilzbach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't there be tests in fail_compilation for cases where constant folded detected an error?

if (!lengthVar)
return;
if (lengthVar._init && !lengthVar._init.isVoidInitializer())
return; // we have previously calculated the length
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently this caching doesn't work.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean coverage wise? That's probably because semantic only calls it once. Doesn't make the safeguards any less correct.

else
{
Type t = arr.type.toBasetype();
if (t.ty == Tsarray)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this doesn't work.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Control flow has likely moved this to the other function. Should still be kept around though, as it's a utility function.


{ auto s = da[l .. u]; } // 0 0 (u <= 10 && l <= u)
{ auto s = da[0 .. u]; } // 0 1 (u <= 10 )
{ auto s = da[l .. 10]; } // 0 0 (u <= 10 && l <= u)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l <= 10 ?

size_t u = 9; // | lowerLessThan
// | | check code
{ auto s = da[l .. u]; } // 0 0 (u <= 10 && l <= u )
{ auto s = da[1 .. u]; } // 0 0 (u <= 10 && l <= u )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 - not l ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should probably change my font, because all I see is the same character twice in your comment. I assume you mean there's a mixup between number (1) and letter (L)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the non-monospace font shows it more clearly:

  • { auto s = da[l .. u]; } // 0 0 (u <= 10 && l <= u )
  • { auto s = da[1 .. u]; } // 0 0 (u <= 10 && l <= u )

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to figure out why that was even compiling. Line 190 shows size_t l = 0; // upperInRange, so it looks ok, though it could probably use a better name.

{ auto s = da[l .. u]; } // 0 0 (u <= 10 && l <= u )
{ auto s = da[1 .. u]; } // 0 0 (u <= 10 && l <= u )
{ auto s = da[l .. 10]; } // 0 0 (u <= 10 && l <= u )
{ auto s = da[1 .. u%5]; } // 0 0 (u <= 10 && l <= u%5)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 - not l ?

@wilzbach
Copy link
Contributor

(rebased with trivial conflicts)

@ibuclaw
Copy link
Member Author

ibuclaw commented Feb 3, 2018

Rebased, clarified names of vars in the bounds tests.

{ auto s = da[0 .. ub%5]; } // 0 1 (ub%5 <= $ )

{ auto s = da[0 .. 0]; } // 0 1 (0 <= $ )
{ auto s = da[0 .. $]; } // 0 1 ($ <= $ )
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two can be improved in a latter PR by using the check for 0 or $ from #7677.

@JinShil JinShil closed this Feb 3, 2018
@JinShil JinShil reopened this Feb 3, 2018
@JinShil
Copy link
Contributor

JinShil commented Feb 3, 2018

Closed and reopened in an effort to get Jenkins to pick up the latest bits.

@ibuclaw
Copy link
Member Author

ibuclaw commented Feb 3, 2018

Really need to fix that...

@wilzbach
Copy link
Contributor

wilzbach commented Feb 4, 2018

Hopefully dlang/ci#163 helps a bit.

@ibuclaw
Copy link
Member Author

ibuclaw commented Feb 10, 2018

Rebased, again.

@dlang-bot dlang-bot merged commit 3d047fd into dlang:master Feb 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants