Issue 235429
Summary [ExecutionEngine] Interpreter cannot represent `half`/`bfloat` (clang's `_Float16`/`__bf16`): `GenericValue` has no 16-bit FP slot, so FP↔int casts silently use `double` — wrong value, empty stderr; 9 more sites abort or write nothing
Labels clang
Assignees
Reporter leidian977
    Follow-up on #235388 (opaque byte types `b8`/`b16`/…/`b128` in the interpreter). This report is about a **different type family with a different and deeper root cause**, and I am filing it separately for one reason: #235388 carries the caveat "clang does not emit `bN`", so it is reachable only from hand-written IR. **`half` and `bfloat` are what clang emits for `_Float16` and `__bf16`**, so the same class of defect is reachable from ordinary C. Cross-disclosure with #235388 is at the bottom; three sites are shared between the two reports and are marked as such.

### The headline: a wrong answer with completely empty stderr

```llvm
; %argc == 1 when run under lli, so %n == 4101.
; half has an 11-bit significand: near 4096 the representable step is 4,
; so 4101 MUST round to 4100. 4100 & 255 == 4. A double keeps 4101 => 5.
define i32 @main(i32 %argc, ptr %argv) {
  %n = add i32 %argc, 4100
  %h = sitofp i32 %n to half
  %r = fptosi half %h to i32
  %m = and i32 %r, 255
  ret i32 %m
}
```

```console
$ lli t.ll ; echo rc=$?                       # ORC
rc=4
$ lli --jit-kind=mcjit t.ll ; echo rc=$?      # MCJIT
rc=4
$ lli --force-interpreter t.ll ; echo rc=$?   # 5/5 runs
rc=5
$ lli --force-interpreter t.ll 2>&1 >/dev/null | wc -c
0
```

`4` is correct (4101 rounded to the nearest `half` is 4100). The interpreter returns `5`, i.e. it performed **no `half` rounding at all**, and it prints **nothing** — no `LLVM ERROR`, no assertion, no debug-ostream note, exit status is a normal `5`. This is deterministic: 5/5 identical runs on each engine.

The type control is the same program with `float` instead of `half`. `float` has a 24-bit significand and represents 4101 exactly, so all three engines must and do agree:

```console
$ for e in "" "--jit-kind=mcjit" "--force-interpreter"; do lli $e t_float.ll >/dev/null 2>&1; echo "$e -> $?"; done
 -> 5
--jit-kind=mcjit -> 5
--force-interpreter -> 5
```

`uitofp`/`fptoui` behaves identically (`orc=4 mcjit=4 interp=5`, empty stderr).

### Root cause: `GenericValue` has no slot for a 16-bit FP value

`llvm/include/llvm/ExecutionEngine/GenericValue.h:23`:

```cpp
struct GenericValue {
  struct IntPair { unsigned int first; unsigned int second; };
  union {
    double DoubleVal;
    float FloatVal;
    PointerTy PointerVal;
    struct IntPair UIntPairVal;
    unsigned char Untyped[8];
  };
  APInt IntVal; // also used for long doubles.
  std::vector<GenericValue> AggregateVal;
```

There is no `half`/`bfloat` member, and `Untyped` is 8 bytes. So the interpreter cannot hold a 16-bit FP value in its own representation, and every FP dispatch that needs one falls into an `else` that assumes `double`:

`Interpreter/Execution.cpp:1462-1468` (`executeSIToFPInst`, scalar; quoted verbatim, blank line at `:1463` included):

```cpp
    assert(DstTy->isFloatingPointTy() && "Invalid SIToFP instruction");

    if (DstTy->getTypeID() == Type::FloatTyID)
      Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(Src.IntVal);
    else {
      Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(Src.IntVal);
    }
```

`half` satisfies `isFloatingPointTy()`, so the assert passes, and it is not `FloatTyID`, so the `else` runs: the value is computed and stored **at double precision in `DoubleVal`**. `executeFPToSIInst:1397-1403` does the mirror image (`if (SrcTy->getTypeID() == Type::FloatTyID) … else … Src.DoubleVal`). The write and the read are self-consistent, so nothing crashes — the `half` rounding is simply never performed. Same shape at `executeUIToFPInst:1430-1435` and `executeFPToUIInst:1359-1364`, and in the vector branches at `:1450`, `:1418`, `:1384`, `:1346`.

A direct confirmation that no real `ppc_fp128`/`half` value is ever built: `2^53+1` needs 54 bits, which `ppc_fp128` (106-bit significand) represents exactly and `double` cannot.

```llvm
define i32 @main(i32 %argc, ptr %argv) {
  %z = zext i32 %argc to i64
  %n = add i64 %z, 9007199254740992   ; 2^53, so %n == 2^53+1
  %h = sitofp i64 %n to ppc_fp128
  %r = fptosi ppc_fp128 %h to i64
  %k = and i64 %r, 15
  %m = trunc i64 %k to i32
  ret i32 %m
}
```

Correct answer `1`; `double`-rounded answer `0`. All three engines return `0`, and the `double` twin of this program also returns `0` — i.e. the interpreter's `ppc_fp128` result is bit-identical to its `double` result, so it never widened. (For `ppc_fp128` specifically the JITs are separately unreliable on x86-64 — see "Not included here" below.)

### Second silent class: a scalar `store half` writes nothing

`ExecutionEngine.cpp:970-982`:

```cpp
void ExecutionEngine::StoreValueToMemory(const GenericValue &Val,
                                         GenericValue *Ptr, Type *Ty) {
  ...
  const unsigned StoreBytes = getDataLayout().getTypeStoreSize(Ty);   // computed, then unused below

  switch (Ty->getTypeID()) {
  default:
    dbgs() << "Cannot store value of type " << *Ty << "!\n";
    break;
```

`HalfTyID` hits `default`: one line to the **debug stream** (not a diagnostic), `StoreBytes` is discarded, and the function returns having written nothing. The program continues and exits normally with a wrong value.

```llvm
define i32 @main(i32 %argc, ptr %argv) {
  %h = sitofp i32 %argc to half        ; half 1.0 == 0x3C00
  %p = alloca i16, align 16
  store i16 -86, ptr %p, align 16      ; 0xFFAA pre-fill, low byte 0xAA == 170
  store half %h, ptr %p, align 16      ; the store under test
  %y = load i16, ptr %p, align 16
  %z = trunc i16 %y to i8
  %m = zext i8 %z to i32
  ret i32 %m
}
```

```console
$ lli s.ll ; echo rc=$?                      # ORC   (0x3C00 low byte == 0)
rc=0
$ lli --jit-kind=mcjit s.ll ; echo rc=$?     # MCJIT
rc=0
$ lli --force-interpreter s.ll ; echo rc=$?  # 0xAA survived => nothing was written
Cannot store value of type half!
rc=170
```

10/10 runs: ORC `0`, MCJIT `0`, interpreter `170`. The pre-fill is what makes this admissible — a `zeroinitializer` destination cannot distinguish "wrote zero" from "wrote nothing" (that is the trap documented in #235388). The `float` twin returns `63` on all three engines when the exit code reads the high byte.

Drop the pre-fill and the same shape reads uninitialized stack instead, differently every run:

```console
$ for i in 1 2 3 4 5 6 7 8; do lli --force-interpreter s_noprefill.ll >/dev/null 2>&1; printf '%s ' $?; done; echo
170 92 181 96 190 237 96 33
$ for i in 1 2 3 4 5; do lli s_noprefill.ll >/dev/null 2>&1; printf '%s ' $?; done; echo     # ORC, stable
60 60 60 60 60
$ for i in 1 2 3 4 5; do lli --jit-kind=mcjit s_noprefill.ll >/dev/null 2>&1; printf '%s ' $?; done; echo
60 60 60 60 60
```

### Third class: the aborts

`half` cannot even be *created* in the interpreter — every producer aborts, so all downstream `half` operations are unreachable in practice:

| how you make a `half` | site | behaviour |
|---|---|---|
| `half` constant (global initializer or inline operand) | `ExecutionEngine.cpp:963` | `LLVM ERROR: ERROR: Constant unimplemented for type: half` |
| `load half` | `ExecutionEngine.cpp:1087` | `LLVM ERROR: Cannot load value of type half!` |
| `bitcast i16 → half` | `Interpreter/Execution.cpp:1655` | `llvm_unreachable("Invalid Bitcast")` |
| `bitcast half → i16` | `Interpreter/Execution.cpp:1640` | `llvm_unreachable("Invalid BitCast")` |
| `fptrunc float → half` | `Interpreter/Execution.cpp:1303` | `assert(SrcVal->getType()->isDoubleTy() && DstTy->isFloatTy() && "Invalid FPTrunc instruction")` |
| `sitofp`/`uitofp iN → half` | — | **does not abort**: silently produces a `double` (see above) |

The `fptrunc` assert is the whole story in one line — `:1293-1306` hardcodes `double → float` as the only supported truncation, and `executeFPExtInst:1316-1327` hardcodes `float → double` as the only supported extension:

```cpp
  } else {
    assert(SrcVal->getType()->isDoubleTy() && DstTy->isFloatTy() &&
           "Invalid FPTrunc instruction");
    Dest.FloatVal = (float)Src.DoubleVal;
  }
```

The clang-shaped reproducer — a scalar `half` global, no vectors, no aggregates:

```llvm
; the IR clang emits for:  _Float16 g = 1.5h;
@g = global half 0xH3E00, align 16
define i32 @main() {
  %v = load half, ptr @g, align 16
  %b = bitcast half %v to i16
  %s = lshr i16 %b, 8          ; 0x3E == 62
  %z = trunc i16 %s to i8
  %m = zext i8 %z to i32
  ret i32 %m
}
```

```console
$ lli g.ll ; echo rc=$?                      # ORC
rc=62
$ lli --jit-kind=mcjit g.ll ; echo rc=$?     # MCJIT
rc=62
$ lli --force-interpreter g.ll ; echo rc=$?
LLVM ERROR: ERROR: Constant unimplemented for type: half
rc=134
```

`bfloat` is identical (`@g = global bfloat 0xR3FC0` → ORC/MCJIT `63`, interpreter `134`). The `float` twin (`@g = global float 1.5`, high byte `0x3F`) returns `63` on all three.

Also reachable, and shared with #235388:

| site | trigger |
|---|---|
| `ExecutionEngine.cpp:934` | `@g = internal constant <2 x half> splat (half …)` → `llvm_unreachable("Unknown constant pointer type!")`. 15 rows in the matrix below. |
| `Interpreter/Execution.cpp:1843` | `shufflevector <2 x half>` → `llvm_unreachable("Unhandled dest type for insertelement instruction")` (the message names the wrong instruction) |
| `Interpreter/Execution.cpp:1804` | `insertelement <2 x half>` → `llvm_unreachable("Invalid index in insertelement instruction")`. This one is a *downstream symptom*, not a dispatch gap: the `<2 x half>` load left `AggregateVal` empty (see the `:1059-1083` row at the end of this table), so the index check fails. A backtrace points here but the defect is in `LoadValueFromMemory`. |
| `Interpreter/Execution.cpp:389` | `fcmp oeq half` → the `default` arm at `:387-389` writes `dbgs() << "Unhandled type for FCmp EQ instruction: half"` at `:388`, then `llvm_unreachable(nullptr)` at `:389` — a null message |
| `ExecutionEngine.cpp:1002-1015` + `:1059-1083` | `<2 x half>` load+store → vector silent no-op / uninitialized read (same mechanism as #235388 root cause 5 and 8) |

Note that `:389` is only reachable for a `half` that came from `sitofp`/`uitofp`, since every other producer aborts first; the `IMPLEMENT_FCMP` macro (`:358`) is instantiated only for `Float` (`:384`) and `Double` (`:385`) inside `executeFCMP_OEQ` (`:380-392`), and `IMPLEMENT_VECTOR_FCMP` (`:371-378`, instantiated at `:386`) is `if (element isFloatTy()) Float else Double` — so `<2 x half>` comparisons silently compare `DoubleVal` fields.

### Matrix

Generated, not hand-picked: 10 shapes × 12 types + 1 control shape = 121 programs. Every program is checked with `opt -passes=verify -disable-output` (this build has no `llvm-as`; `opt` rejects syntax errors too), and every program is run **twice per engine**. Shapes: splat-global-unused, scalar-prefill, vec2-nonsplat-prefill, vec2-splat-prefill, vec2-nonsplat-uninit, vec2-bitcast-icmp, vec2-shuffle, runtime-roundtrip, cast-roundtrip, store-runtime-prefill. Types: `half`, `bfloat`, `x86_fp80`, `fp128`, `ppc_fp128` (family under test), `float`, `double`, `i16`, `i32` (controls), `b8`, `b32`, `b128` (#235388's family, re-run here as a second control).

Adjudication rule: the expected value is **never** presupposed. Truth = ORC and MCJIT agreeing. Any row where a control engine crashes, where the two controls disagree, where either control is non-deterministic across its two runs, or where the control value equals the pre-fill byte (making "wrote zero" indistinguishable from "wrote nothing") is sent to a gate file and **excluded** from the counts below.

**Controls: 36 of 36 legal control rows are correct on all three engines** (`float`/`double`/`i16`/`i32` × the shapes that are legal for them). 4 control rows are illegal by construction (`sitofp` to an integer type) and 1 control shape is gated (below). So the shapes are sound; only the type differs.

Family under test, 50 rows:

| type | OK | ABORT | silent no-op | silent wrong | interp nondet | gated (control crashed) |
|---|---|---|---|---|---|---|
| `half` | 0 | 6 | 2 | 1 | 1 | 0 |
| `bfloat` | 0 | 6 | 1 | 0 | 1 | 2 |
| `x86_fp80` | 3 | 5 | 1 | 1 | 0 | 0 |
| `fp128` | 1 | 6 | 2 | 1 | 0 | 0 |
| `ppc_fp128` | 0 | 6 | 2 | 1* | 0 | 0 |

44 candidate rows for the FP family. Per-site tally: `ExecutionEngine.cpp:934` ×15, `Interpreter/Execution.cpp:1655` ×5, `:1843` ×5, `:963` ×2 (`half`, `bfloat`), `:1087` ×2 (`fp128`, `ppc_fp128`), silent no-op ×8, silent wrong ×5, interpreter-nondeterministic ×2.

The `x86_fp80`/`fp128` OK cells are informative, not counterexamples: `LoadValueFromMemory` and `StoreValueToMemory` *do* have an `X86_FP80TyID` case, and `getConstantValue` has `X86_FP80TyID`/`FP128TyID`/`PPC_FP128TyID` cases at `:907-910` — those are exactly the shapes that pass, and they are the positive control proving that a type *with* an arm works in the same shape where a type without one fails.

`*` The `ppc_fp128` `cast-roundtrip` row is **excluded** from this report: ORC and MCJIT both return `0` there while the interpreter returns the arithmetically correct `5`, so the two controls are themselves wrong and the differential cannot adjudicate. See "Not included here" below.

The byte family re-run in the same matrix (24 admissible rows: 20 ABORT, 2 silent no-op, 2 nondeterministic) reproduces #235388 exactly, which is the point of running it here: **the same shapes fail for two unrelated type families**, so #235388's defects are structural, not byte-specific.

### Grep-level statement of the gap

```console
$ grep -rn "HalfTyID\|BFloatTyID\|ByteTyID\|isHalfTy\|isBFloatTy\|isByteTy" llvm/lib/ExecutionEngine/ | wc -l
0
```

Zero. No file under `llvm/lib/ExecutionEngine/` mentions any of these type predicates in any form.

The test side is equally bare, and the numbers are small enough to state exactly: **16** lit tests in the entire tree pass `--force-interpreter`, all of them under `llvm/test/ExecutionEngine/`, and not one of the 16 contains a `half` or `bfloat` value. The only 16-bit-plus FP string anywhere in those files is a comment in `test/ExecutionEngine/Interpreter/intrinsics.ll:3` recording that `fp128` is deliberately not tested (`; libffi does not support fp128 so we don’t test it` — the apostrophe is U+2019 in the file, quoted exactly). There is no `llvm/test/tools/lli/` directory at all.

### Suggested fix

The `else ⇒ double` arms are the priority, because they are the only ones that produce a **wrong value with no diagnostic at all**:

1. `executeSIToFPInst:1462`, `executeUIToFPInst:1430`, `executeFPToSIInst:1399`, `executeFPToUIInst:1361` (and their vector branches) — turn `if (FloatTyID) … else …` into an explicit three-way dispatch with a `report_fatal_error` (or a real `half`/`bfloat` arm using `APIntOps::RoundSignedAPIntToFloat` on a widened `APFloat`) for anything that is neither `float` nor `double`. Silently computing at double precision is worse than aborting.
2. `StoreValueToMemory:980-982` — the `default` currently writes a line to `dbgs()` and returns success. `StoreBytes` is already computed on `:977` and unused; either use it for a `memcpy` from `V<truncated>Please see the issue for the entire body.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to