This is an archive of the discontinued LLVM Phabricator instance.

[X86] Fix wrong handle with "-mno-x87"
Needs ReviewPublic

Authored by LiuChen3 on Apr 7 2021, 11:28 PM.

Details

Summary

When the '-mno-x87' command line option is used, it should prevent all generation
of x87 instructions. Currently there are several circumstances under which it does not.
For example: https://godbolt.org/z/qnPaW7dd6

This patch adds "HasX87" predicates to each X87 instructions and when 80-bit
float pointer is supposed to pass or return on the x87 stack, reporting
an error if X87 is not supported.

Diff Detail

Event Timeline

LiuChen3 created this revision.Apr 7 2021, 11:28 PM
LiuChen3 requested review of this revision.Apr 7 2021, 11:28 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 7 2021, 11:28 PM
LiuChen3 retitled this revision from [X86] Fix wrong handle wih "-mno-x87" to [X86] Fix wrong handle with "-mno-x87".

What about cases that use x87 to do float/double conversions? I think it’s i64 conversions in 32-bit mode. But maybe other cases. Does gcc disable those with -mno-80387?

What about cases that use x87 to do float/double conversions? I think it’s i64 conversions in 32-bit mode. But maybe other cases. Does gcc disable those with -mno-80387?

You are right! GCC can do the conversions: https://godbolt.org/z/87ez838oc .
With this patch, llvm will fail in ISEL. I want to make another patch to do this.

What about cases that use x87 to do float/double conversions? I think it’s i64 conversions in 32-bit mode. But maybe other cases. Does gcc disable those with -mno-80387?

You are right! GCC can do the conversions: https://godbolt.org/z/87ez838oc .
With this patch, llvm will fail in ISEL. I want to make another patch to do this.

I'm more concerned about these cases that don't use long double and aren't missed constant folding. https://godbolt.org/z/qGv54ef34

I'm more concerned about these cases that don't use long double and aren't missed constant folding. https://godbolt.org/z/qGv54ef34

I see. This also break the ABI. Even when returning fp80 on 32bit target, gcc does not report an error. Can llvm report an error just like on a 64bit target?
Should we discuss the ABI issue on llvm-dev?

What about cases that use x87 to do float/double conversions? I think it’s i64 conversions in 32-bit mode. But maybe other cases. Does gcc disable those with -mno-80387?

You are right! GCC can do the conversions: https://godbolt.org/z/87ez838oc .
With this patch, llvm will fail in ISEL. I want to make another patch to do this.

I'm more concerned about these cases that don't use long double and aren't missed constant folding. https://godbolt.org/z/qGv54ef34

It seems not easy to handle this. I want fix a bug in this patch. For this part, It looks more like a feature. We want to do that in another patch. What's your opinion here?

What about cases that use x87 to do float/double conversions? I think it’s i64 conversions in 32-bit mode. But maybe other cases. Does gcc disable those with -mno-80387?

You are right! GCC can do the conversions: https://godbolt.org/z/87ez838oc .
With this patch, llvm will fail in ISEL. I want to make another patch to do this.

I'm more concerned about these cases that don't use long double and aren't missed constant folding. https://godbolt.org/z/qGv54ef34

It seems not easy to handle this. I want fix a bug in this patch. For this part, It looks more like a feature. We want to do that in another patch. What's your opinion here?

What is the usage model you’re trying to enable? This has been broken for a long time. Why is it important now?

Does adding HasX87 predicates to the instructions cause the conversions in my example to fail?

Does adding HasX87 predicates to the instructions cause the conversions in my example to fail?

Yes, it is. But this fail appeared before isel because of breaking ABI.

What is the usage model you’re trying to enable? This has been broken for a long time. Why is it important now?

I don't know any specific case for now. This was found by @andrew.w.kaylor. I will check with him.

What is the usage model you’re trying to enable? This has been broken for a long time. Why is it important now?

I don't know any specific case for now. This was found by @andrew.w.kaylor. I will check with him.

We have libraries that for performance reasons want to be able to avoid saving and restoring the x87 state and to safely do this, they need to rely on the fact that we won't generate x87 instructions if the compiler is told not to. That's the general motivation. This particular case is an easy contrived case that I came up with to show that the no-x87 option isn't always being respected. I don't think this case matters for our library because it's essentially user error. In theory, the front end code that sets up the ABI conformance could detect this case and report an error. Of course, that means relying on every front end to the right thing, so I think it would be better to (also?) reject this in the backend.

Basically, my ultimate goal here is to achieve a robust solution where "no-x87" means no x87 in all circumstances.

What is the usage model you’re trying to enable? This has been broken for a long time. Why is it important now?

I don't know any specific case for now. This was found by @andrew.w.kaylor. I will check with him.

We have libraries that for performance reasons want to be able to avoid saving and restoring the x87 state and to safely do this, they need to rely on the fact that we won't generate x87 instructions if the compiler is told not to. That's the general motivation. This particular case is an easy contrived case that I came up with to show that the no-x87 option isn't always being respected. I don't think this case matters for our library because it's essentially user error. In theory, the front end code that sets up the ABI conformance could detect this case and report an error. Of course, that means relying on every front end to the right thing, so I think it would be better to (also?) reject this in the backend.

Basically, my ultimate goal here is to achieve a robust solution where "no-x87" means no x87 in all circumstances.

Thanks. Is the compiler still allowed to use the xmm registers? Is this needed for 32-bit mode or just 64-bit? Trying to understand how many problems need to be fixed.

pengfei added inline comments.Nov 17 2021, 7:34 PM
llvm/lib/Target/X86/X86ISelLowering.cpp
2737–2740

We don't need it after D112143.

3248–3249

ditto.

llvm/test/CodeGen/X86/nox87-error.ll
1

ditto.