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

divide by zero exception is not reliable #54

Closed
JeffBezanson opened this issue Jun 17, 2011 · 7 comments
Closed

divide by zero exception is not reliable #54

JeffBezanson opened this issue Jun 17, 2011 · 7 comments
Assignees
Labels
kind:bug Indicates an unexpected problem or unintended behavior

Comments

@JeffBezanson
Copy link
Sponsor Member

In LLVM semantics dividing by zero gives undefined behavior. Basically, in any case where it is able to evaluate the division at compile time you get a strange number answer instead of an exception:

julia> g()=begin
       local b=0
       div(1,b)
       end
Methods for generic function g
g()

julia> g()
-1081290856
@StefanKarpinski
Copy link
Sponsor Member

I guess the solution is to emit code that actually checks this and throws an exception at run time? We want to be careful not to deny ourselves the possibility of evaluating things at compile time, however. How about throwing an exception at compile time? I'm guessing LLVM doesn't support that so we'd have to have our own constant folding at compile time in order to throw a compile-time error, no?

@JeffBezanson
Copy link
Sponsor Member Author

I guess the bright side of this is that we know llvm's constant folding is working for us :)
I checked the vmkit source to see how they handle this for java, and they emit a check for zero with every integer divide. We probably have to do this too.

@StefanKarpinski
Copy link
Sponsor Member

If we could always catch these cases at compile time, then we wouldn't, right? This works as expected:

julia> f(a) = begin
         local b = 0
         div(a,b)
       end

julia> f(1)
error: integer divide by zero

Thus, if Julia's constant folding were as good as LLVM's and we caught the divide-by-zero at compile time, we wouldn't have to emit the check. It would be a little weird to generate a compile-time error for divide-by-zero. The alternative would be to detect the divide-by-zero at compile time and generate code that throws and exception when it's detected.

@JeffBezanson
Copy link
Sponsor Member Author

Yes, if you can statically prove division by zero then you emit code to throw an exception.

Even if we do have our own constant folding eventually, there's no way we can be sure it will handle everything the same as all of LLVM's optimizations. LLVM can do a lot of sophisticated rearrangement, and we can't risk giving a bogus answer like -1081290856. We should do the run-time check, and eventually see if it's possible to add a language-specific hook in llvm to intercept this case.

@JeffBezanson
Copy link
Sponsor Member Author

We can also measure the performance impact and possibly provide a nonzero_div_int intrinsic. If you want you can change int.j to use this --- a great thing about julia. Imagine modifying your JVM for something like this. Totally unrealistic.

@StefanKarpinski
Copy link
Sponsor Member

Nice. That's a lovely solution.

@JeffBezanson
Copy link
Sponsor Member Author

In a quick and dirty test the performance impact seemed to be negligible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

2 participants