This is an archive of the discontinued LLVM Phabricator instance.

[ConstantFolding] Constant-fold llvm.sqrt(x) like other intrinsics.
ClosedPublic

Authored by jlebar on Jan 19 2017, 4:50 PM.

Details

Summary

Currently we return undef, but we're in the process of changing the
LangRef so that llvm.sqrt behaves like the other math intrinsics,
matching the return value of the standard libcall but not setting errno.

This change is legal even without the LangRef change because currently
calling llvm.sqrt(x) where x is negative is spec'ed to be UB. But in
practice it's also safe because we're simply constant-folding fewer
inputs: Inputs >= -0 get constant-folded as before, but inputs < -0 now
aren't constant-folded, because ConstantFoldFP aborts if the host math
function raises an fp exception.

Diff Detail

Repository
rL LLVM

Event Timeline

jlebar created this revision.Jan 19 2017, 4:50 PM
efriedma added inline comments.Jan 19 2017, 5:04 PM
llvm/test/Transforms/InstCombine/constant-fold-math.ll
54 ↗(On Diff #85064)

Leave the test, and just update the CHECK line?

jlebar added inline comments.Jan 19 2017, 5:27 PM
llvm/test/Transforms/InstCombine/constant-fold-math.ll
54 ↗(On Diff #85064)

The CHECK in this case would be checking that the sqrt is not constant-folded. (sqrt(-2) raises an fp exception, and we don't currently constant-fold intrinsics whose libm equivalents raise exceptions.) I didn't think this was an interesting test, because it's a limitation of the compiler that we don't currently constant-fold it, not a correctness issue.

efriedma added inline comments.Jan 19 2017, 5:36 PM
llvm/test/Transforms/InstCombine/constant-fold-math.ll
54 ↗(On Diff #85064)

Well, we'd like to know if the compiler is doing something unexpected. We can update the test when we teach the compiler that sqrt(-1) == NaN.

jlebar updated this revision to Diff 85078.Jan 19 2017, 5:47 PM
jlebar marked 3 inline comments as done.

Add test checking that we don't constant-fold llvm.sqrt(-2).

jlebar added inline comments.Jan 19 2017, 5:47 PM
llvm/test/Transforms/InstCombine/constant-fold-math.ll
54 ↗(On Diff #85064)

OK, done.

efriedma accepted this revision.Jan 19 2017, 5:59 PM

Looks fine.

This revision is now accepted and ready to land.Jan 19 2017, 5:59 PM
This revision was automatically updated to reflect the committed changes.