Missed optimization?

safety0ff via digitalmars-d-ldc digitalmars-d-ldc at puremagic.com
Fri Oct 21 14:14:56 PDT 2016


Hello,
I was experimenting with pure functions, exceptions and inlining 
and I noticed that LDC seems to generate inferior code to GDC in 
the cases I was considering.

For example:

http://ldc.acomirei.ru/#compilers:!((compiler:ldc,options:'-release+-O3+-boundscheck%3Doff',sourcez:PTBuFMCcGcEsHsB2ACAvMgLgC0vA7gDbjTQDcAUAUgObICGAJgwIwAUVitdAlMgA4BXSOHIBvcsmSwAZslZ006DtQB0AWzoAPbhOTiIMBIlbZchYtB2TJwjEJTL1WiuALQR1zDnzJE4PMgAopoAxuB8GEasAETR3BSSAL66tvb0yADUyMwUyZQ0yNLw8KywamoCGHQARkTIygDaALr0OuI24HaQKHQNAAxNKowsmfT9g8M55HnKyNV0kKXllTV1jS08Yrqz1QA0IQmYkACeerqSGCdnnpLVaGMDQ0xTnonIIXQYIVh6c/cALAAmUjIZKeEL3XqPSaHMHvT7fX4Q9CAgDMILhqW6cwyB2m%2BU4czoAC8lhUqrVwPUaM1WltJDt9odLqd2tY7ugoRNnodJMiHtyWLDdB8vj9RH90ECQfy0RiUp00tVcbkCbQAI4CTRklaU6mcWmbNmMvEXK5s26Q8ZPIXnI6su18q3Qnl2t6ixESjnIaWg5AgZACRDCOjfVYeUEihHiyU%2B4Hve5yyMdLooZV45JAA%3D%3D)),filterAsm:(commentOnly:!t,directives:!t,labels:!t),version:3

Compared to:

https://godbolt.org/g/WI12Bk

Even in the simplest case (function foo) contains duplicated 
memory load and comparison from the add1 function.

Changing to the throwless version LDC generates similar code for 
all cases.

Motivation:

I was thinking about autodecoding and wondering whether it was 
possible for the following example:
auto decode(immutable char[] s) pure
{
struct Result { dchar codepoint; uint advance; }
// throw Exception if s is invalid / erroneous
// return Result (decoded codepoint, number of chars to advance);
}
dchar front(string s) { return s.decode.codepoint; }
void popFront(ref string s) { s = s[s.decode.advance .. $];

ulong loop(string s)
{
   ulong checksum;
   while (s.length)
   {
     checksum += s.front;
     s.popFront;
   }
   return checksum;
}

To optimize the loop function (using inlining and purity):

ulong loop(string s)
{
   ulong checksum;
   while (s.length)
   {
     auto tmp = s.decode
     checksum += tmp.codepoint;
     s = [tmp.advance .. $];
   }
   return checksum;
}


More information about the digitalmars-d-ldc mailing list