dmd codegen improvements

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Tue Aug 18 16:05:40 PDT 2015


On Tue, Aug 18, 2015 at 03:25:38PM -0700, Walter Bright via Digitalmars-d wrote:
> On 8/18/2015 2:57 PM, H. S. Teoh via Digitalmars-d wrote:
> >From the little that I've seen of dmd's output, it seems that it's
> >rather weak in the areas of inlining and loop unrolling /
> >refactoring.
> 
> DMD does not do loop unrolling. I've thought about it many times, but
> just never did it.

What's the reason for it?


> >In both cases, it seems that the optimizer gives up too quickly -- an
> >if-else function body will get inlined, but an if without an else
> >doesn't, etc..
> 
> It should do this. An example would be nice.

Sorry, I wrote this from memory, so I don't have an example handy. But
IIRC it was either a lambda or a function with a single-line body, where
if the function has the form:

	auto f() {
		if (cond)
			return a;
		else
			return b;
	}

it would be inlined, but if it was written:

	auto f() {
		if (cond)
			return a;
		return b;
	}

it would remain as a function call. (I didn't test this, btw, like I
said, I'm writing this from memory.)


> >There's also the more general optimizations, like eliminating
> >redundant loads, eliding useless allocation of stack space in
> >functions that return constant values, etc.. While DMD does do some
> >of this, it's not as thorough as GDC. While it may sound like only a
> >small difference, if they happen to run inside an inner loop, they
> >can add up to quite a significant difference.
> 
> dmd has a full data flow analysis pass, which includes dead code
> elimination and dead store elimination. It goes as far as possible
> with the intermediate code. Any dead stores still generated are an
> artifact of the the detailed code generation, which I agree is a
> problem.
> 
> 
> >DMD needs to be much more aggressive in eliminating useless /
> >redundant code blocks; a lot of this comes not from people writing
> >unusually redundant code, but from template expansions and inlined
> >range-based code, which sometimes produce a lot of redundant
> >operations if translated directly.  Aggressively reducing these
> >generated code blocks will often open up further optimization
> >opportunities.
> 
> I'm not aware of any case of DMD generating dead code blocks. I'd like
> to see it if you have one.

Sorry, I didn't write it clearly. I meant dead or redundant
loads/stores, caused either by detailed codegen or from template
expansion or inlining(?). Overall, the assembly produced by GDC tends to
be "cleaner" or "leaner", whereas the assembly produced by DMD tends to
be more "frilly" (doing the same thing in more instructions than GDC
does in fewer).

Maybe when I get some free time this week, I could look at the
disassembly of one of my programs again to give some specific examples.


T

-- 
Doubtless it is a good thing to have an open mind, but a truly open mind should be open at both ends, like the food-pipe, with the capacity for excretion as well as absorption. -- Northrop Frye


More information about the Digitalmars-d mailing list