Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dmd backend: redundant x86 instruction in a simple loop #17634

Open
dlangBugzillaToGithub opened this issue Dec 26, 2013 · 4 comments
Open

dmd backend: redundant x86 instruction in a simple loop #17634

dlangBugzillaToGithub opened this issue Dec 26, 2013 · 4 comments
Labels

Comments

@dlangBugzillaToGithub
Copy link

Ivan Kazmenko (@GassaFM) reported this on 2013-12-26T02:12:34Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=11821

CC List

Description

I am trying to figure out why win32 executables compiled from D source by dmd are usually somewhat slower than similar win32 programs compiled from C++ source by, for example, mingw-gcc.

I believe I found a relatively simple case where dmd puts a redundant instruction into the object code.

I have this simple D program:

-----
immutable int MAX_N = 1_000_000;
int main () {
    int [MAX_N] a;
    foreach (i; 0..MAX_N)
        a[i] = i;
    return a[7];
}
-----

The assembly (dmd -O -release -inline -noboundscheck, then obj2asm) has the following piece corresponding to the cycle:

-----
L2C:		mov	-03D0900h[EDX*4][EBP],EDX
		mov	ECX,EDX
		inc	EDX
		cmp	EDX,0F4240h
		jb	L2C
-----

Here, the second line "mov ECX, EDX" does not seem to serve any purpose at all.  If this observation is correct, this instruction is an indication of a bug in code generation, and fixing that bug may improve performance in more general case.

The "return a[7]" part is to assure the whole loop need not be optimized out.  The ldmd2 compiler reportedly does that when no return is present.  DMD however does not, however that is irrelevant to this issue.

Previous discussion:
http://forum.dlang.org/thread/nfobptpqpiueelhehbfy@forum.dlang.org

Will attach source and disassembly in comments.

Ivan Kazmenko.

!!!There are attachements in the bugzilla issue that have not been copied over!!!

@dlangBugzillaToGithub
Copy link
Author

gassa commented on 2013-12-26T02:13:47Z

Created attachment 1307
source code of the demonstrating example

@dlangBugzillaToGithub
Copy link
Author

gassa commented on 2013-12-26T02:14:21Z

Created attachment 1308
disassembly of the demonstrating example

@dlangBugzillaToGithub
Copy link
Author

gassa commented on 2013-12-26T02:27:09Z

I should note that the exact compile command must be some sort of:

dmd a0.d -O -release -inline -noboundscheck -L/STACK:268435456

Otherwise, the default stack limit makes the program crash at runtime.

The "-L/STACK:268435456" does not affect the generated object file since it is used on linking stage.

@dlangBugzillaToGithub
Copy link
Author

maxim commented on 2013-12-26T08:08:29Z

This may be remainders from internally created variables. Compiler often rewrites high-level constructions to lower ones with implicitly introducing new variables. What you see from asm is their usage. 

By the way, it is not a 'code generation bug', it is poor optimization.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant