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

compiler misses obvious optimization #338

Closed
timotheecour opened this issue May 8, 2013 · 2 comments
Closed

compiler misses obvious optimization #338

timotheecour opened this issue May 8, 2013 · 2 comments

Comments

@timotheecour
Copy link

Should I report such optimization issues ?

Im using flags:
ldmd2 -release -inline -O -noboundscheck -L-no_pie
I'm not sure why ldc gives such different results in this benchmark:
writeln(b.point); // prints 1.91474
meaning that 'x+=x.to!double_((1.2).to!int);' is 2X slower than 'x+=x.to!double_(cast(int)1);'
Basically, the compiler should recognize that ((1.2).to!int) is constant and equal to cast(int)1, so there should be no difference.

This is repeatable.

import std.conv;
import std.datetime;
import std.stdio;

static int counter=0;
void main(){
    enum n=100_000_000;
    static int x=0;
    assert((1.2).to!int==cast(int)1);
    ref int funb(ref int x){//TEMP
        x+=x.to!double*((1.2).to!int);
        return x;
    }
    ref int fun(ref int x){
        x+=x.to!double*(cast(int)1);
        return x;
    }
    void fun1(){
        x+=fun(x);
    }
    void fun2(){
        x+=funb(x);
    }
    auto b = comparingBenchmark!(fun2, fun1,n);
    writeln(b.point); // prints 1.91474
    writeln("counter=",counter);
}
@dnadlinger
Copy link
Member

The problem seems to be that due to the amount of overflow checking in toImpl!(int, double) (including allocating the exceptions, …) LLVM doesn't inline the function into main, and thus can't optimize away the bounds checks.

Not sure what we can do about this.

@dnadlinger
Copy link
Member

Git master now generates the same code for funb and fun.

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

No branches or pull requests

2 participants