This is an archive of the discontinued LLVM Phabricator instance.

[SPIR] Prevent SPIR targets from using half conversion intrinsics
ClosedPublic

Authored by smcgro on Jun 14 2018, 12:53 PM.

Details

Summary

The SPIR target currently allows for half precision floating point types to be emitted using the LLVM intrinsic functions which convert half types to floats and doubles. However, this is illegal in SPIR as the only intrinsic allowed by SPIR is memcpy, as per section 3 of the SPIR specification. Currently this is leading to an assert being hit in the Clang CodeGen when attempting to emit a constant or literal _Float16 type in a comparison operation on a SPIR or SPIR64 target. This assert stems from the CodeGen attempting to emit a constant half value as an integer because the backend has specified that it is using these half conversion intrinsics (which represents half as i16). This patch prevents SPIR targets from using these intrinsics by overloading the responsible target info method, marks SPIR targets as having a legal half type and provides additional regression testing for the _Float16 type on SPIR targets.

Diff Detail

Repository
rC Clang

Event Timeline

smcgro created this revision.Jun 14 2018, 12:53 PM

I know very little about SPIR, and Initially didn't understand this:

The SPIR target currently allows for half precision floating point types to use the LLVM intrinsic functions to convert to floats and doubles. This is illegal in SPIR as the only intrinsic allowed by SPIR is memcpy ...

until I looked at the implementation what you're trying to achieve here. Perhaps you can make the commit message a bit more descriptive and specific.

lib/Basic/Targets/SPIR.h
50

It doesn't hurt to set this, but you're not using it so you could omit it. I had to introduce this to deal differently with half types depending on architecture extensions, but don't you think have this problem.

65

just a note: this is the only functional change, but you're testing a lot more in test/CodeGen/spir-half-type.cpp

test/CodeGen/spir-half-type.cpp
4

I think you need one reproducer to test:

// CHECK-NOT: llvm.convert.from.fp16

The other tests, like all the compares are valid tests, but not related to this change, and also not specific to SPIR. I put my _Float16 "smoke tests" in test/CodeGenCXX/float16-declarations.cpp, perhaps you can move some of these generic tests there because I for example see I didn't add any compares there.

5

nit: this comment exceeds 80 columns, same for the other comment below.

Thanks for the feedback @SjoerdMeijer I've updated the commit message and responded to the comments

lib/Basic/Targets/SPIR.h
50

Yeah that's true that it doesn't affect the assert being hit, I included it as it is true for the SPIR target as the SPIR targets do have a legal half type which is brings the SPIR target file in line with what the specification says. If this is problematic or it needs a different set of tests I can remove it

65

That's true that the fix is only for the assert being hit when attempting to emit comparison operators. The additional tests I added are mainly to prevent regressions in the future since the cross section of the _Float16 C/C++ type and the SPIR target doesn't have any tests at the moment so we'd like to be sure that it will always be compiled correctly. In addition it helps make sure that this change doesn't have any unintended consequences with any of the other operators.

test/CodeGen/spir-half-type.cpp
4

My thought process was that we should have a test just for this _Float16/SPIR cross section as that is where this issue emerged from. These are specific to SPIR insofar that the comparison operators previously wouldn't compile (on Debug or Release with asserts builds) so their inclusion is necessary to check that this now compiles. As I mentioned above the others are there to prevent future regressions on this target and to check that this change doesn't have any unintended side effects with the other operators. I agree that the smoke tests should have the operator tests but we do need these operations to be tested on the SPIR target as well to make sure they compile. Perhaps including these operations in those float16 specific regression tests should be a separate issue however, as it unrelated to this SPIR change?

5

Fixed now, thanks

smcgro edited the summary of this revision. (Show Details)Jun 15 2018, 7:17 AM
smcgro updated this revision to Diff 151512.Jun 15 2018, 7:36 AM
SjoerdMeijer accepted this revision.Jun 19 2018, 12:58 AM

Looks OK to me.

test/CodeGen/spir-half-type.cpp
90

Nit: let one of these functions take a _Float16 function argument?

This revision is now accepted and ready to land.Jun 19 2018, 12:58 AM
smcgro updated this revision to Diff 151938.Jun 19 2018, 10:23 AM

Added a function parameter to one of the functions

That's great @SjoerdMeijer , thanks for the review. I don't have commit access to clang so if there are no more reviews then it would be great if someone could commit this for me, thanks.

No problem, will commit this today.

This revision was automatically updated to reflect the committed changes.