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

Runtime intrinsics: fix fpext and fptrunc behaviour on Float16/BFloat16 #57160

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

xal-0
Copy link
Contributor

@xal-0 xal-0 commented Jan 24, 2025

This makes two changes fpext and fptrunc to match the behaviour specified in their error strings:
-fpext works when converting from Float16 => Float16,
-fptrunc is prevented from truncating Float16 => Float16

Both are re-written to make it explicit what conversions are possible, and how they are done.

Closes #57130.

@xal-0
Copy link
Contributor Author

xal-0 commented Jan 24, 2025

I'm still not sure what the right thing to do is when doing fpext/fptrunc to/from BFloat16. Currently, it is allowed when "output bits >= input bits" holds for fpext, and when "input bits < output bits" for fptrunc. This has the weird effect of losing information on fpext:

julia> using .Core.Intrinsics
julia> using .Core: BFloat16
julia> fpext(Float16, fpext(BFloat16, Float16(12.3)))
Float16(12.31)

@xal-0 xal-0 force-pushed the fix-float16-fptrunc-fpext branch from 29628b6 to d4ad742 Compare January 24, 2025 22:04
@LilithHafner LilithHafner added maths Mathematical functions float16 labels Jan 24, 2025
@oscardssmith
Copy link
Member

oscardssmith commented Jan 24, 2025

I believe the correct behavior would be for fpext to allow going from Float16 or Bfloat16 to Float32/Float64 but not from Float16 to BFloat16 (or from Bfloat16 to Float16). I think that fpext is supposed to only allow transitions from src to dest if all possible src values are losslessly encodable in dest.

See also https://discourse.llvm.org/t/converting-between-different-but-same-sized-floating-point-types/59538 which suggests that there might not be any general understanding of what should happen here.

@xal-0
Copy link
Contributor Author

xal-0 commented Jan 25, 2025

LLVM seems to prohibit fpext and fptrunc with input size = output size:

llc: error: llc: fpstuff.ll:2:14: error: invalid cast opcode for cast from 'float' to 'float'
  %y = fpext float %x to float
             ^

llc: error: llc: fpstuff.ll:70:16: error: invalid cast opcode for cast from 'half' to 'bfloat'
  %y = fptrunc half %x to bfloat
               ^

llc: error: llc: fpstuff.ll:75:14: error: invalid cast opcode for cast from 'half' to 'bfloat'
  %y = fpext half %x to bfloat
             ^

@vchuravy vchuravy requested a review from maleadt January 25, 2025 09:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
float16 maths Mathematical functions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Core.Intrinsics.fpext cannot extend Float16 to Float16
3 participants