llvm / llvm/llvm-project

APFloat::convert() followed by bitcastToAPInt() crashes when converting infinity to FiniteOnly semantics (e.g. Float4E2M1FN)

Open
#182,433 1 comment 0 reactions 0 assignees View on GitHub
llvm:support
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

## Summary

APFloat::convert() followed by bitcastToAPInt() crashes with UNREACHABLE when the source value is infinity and the target semantics has fltNonfiniteBehavior::FiniteOnly (e.g. Float4E2M1FN).
```
semantics don't support inf!
UNREACHABLE executed at llvm/lib/Support/APFloat.cpp
```

## Repro
```c++
#include
using namespace llvm;

int main() {
APFloat val(1.0 / 0.0); // +inf
bool losesInfo;
val.convert(APFloat::Float4E2M1FN(), APFloat::rmNearestTiesToEven, &losesInfo);
APInt bits = val.bitcastToAPInt(); // UNREACHABLE
}
```

## Expected Behavior

Converting infinity into Float4E2M1FN format should produce a valid result (e.g. saturate to the largest magnitude and preserve sign), according to https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf.

Contributor guide

Open the contributing guide

Research direction

Start in llvm/lib/Support/APFloat.cpp at APFloat::convert() and bitcastToAPInt(), then run the C++ reproducer with +infinity and Float4E2M1FN semantics. Compare the result with the OCP microscaling format specification; done means conversion no longer reaches UNREACHABLE and produces a valid signed finite result.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.