LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 36966 - Remove the statement about UB from fptrunc langref
Summary: Remove the statement about UB from fptrunc langref
Status: RESOLVED FIXED
Alias: None
Product: new-bugs
Classification: Unclassified
Component: new bugs (show other bugs)
Version: trunk
Hardware: All All
: P enhancement
Assignee: Unassigned LLVM Bugs
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-03-31 07:26 PDT by simonas+llvm.org
Modified: 2018-04-03 06:10 PDT (History)
4 users (show)

See Also:
Fixed By Commit(s): 329065


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description simonas+llvm.org 2018-03-31 07:26:32 PDT
The Language Reference states that fptrunc (1) has an undefined result "if the value cannot fit within the destination type" and (2) has "undefined" rounding mode. This is quite worrying for frontends that want to avoid UB or even just reliably provide ties-to-even truncation. Furthermore, there does not seem to be any reason for fptrunc to be so undefined. By the assumption about the default floating point environment (also in the Language Reference):

1. the rounding mode is ties-to-even everywhere, this should apply to fptrunc as well

2. no floating-point exception state is maintained, i.e., the overflow exception signaled if the value does not fit in the destination type can't be inspected and is handled in the default way, which gives an infinite result as one would expect

As far as I can tell, fixing this just requires a clarification in the Language Reference. APFloat already does the right thing, and none of the in-tree passes seem to make any attempt to exploit this nominal source of undefs.
Comment 1 Sanjay Patel 2018-04-02 13:10:39 PDT
This sounds right to me. Current constant folding behavior does not follow the existing LangRef example that would supposedly return undef on oveflow: 
http://llvm.org/docs/LangRef.html#fptrunc-to-instruction
...we return infinity there.