Adding a scalar type when the hardware only converts vectors: how PowerPC GCC handles __bf16 and _Float16
Power10 can convert bfloat16 only a whole vector at a time. Here is how a GCC backend turns that into scalar `__bf16` support, and how it fixes an ABI at the same time.
In GCC, a C type becomes real through a machine mode plus the patterns that move, convert and operate on it. When the ISA lacks a scalar instruction, the backend has to build the operation from instructions it does have, and the register convention it picks for moves becomes the ABI.
Assumes you know what RTL is and that GCC lowers GIMPLE into it · the idea of a machine description (.md) file · IEEE 754 float layout (sign, exponent, mantissa) · basic SIMD lanes and vector registers · what a calling convention specifies
Background: a type is a mode plus patterns
The C front end decides what _Float16 and __bf16 mean in the language. The backend decides whether the program can actually run. In GCC’s RTL, every value has a machine mode, a name for a size and an interpretation. SFmode is a 32-bit float, DFmode a 64-bit float, and V4SFmode a vector of four floats. A backend declares its modes in a *-modes.def file and writes patterns in .md files that say which instructions can move, convert and compute values of each mode. If no pattern handles a mode, the type is unusable on that target.
This commit gives the little-endian 64-bit PowerPC backend (gcc/config/rs6000) two new 16-bit formats. rs6000-modes.def declares them:
FLOAT_MODE (HF, 2, ieee_half_format);
FLOAT_MODE (BF, 2, arm_bfloat_half_format);
ADJUST_FLOAT_FORMAT (BF, &arm_bfloat_half_format);
HFmode is IEEE binary16: 1 sign bit, 5 exponent bits, 10 mantissa bits. BFmode is bfloat16: 1 sign bit, 8 exponent bits, 7 mantissa bits. The commit message notes that a bfloat16 value is exactly the upper 16 bits of an IEEE float. That fact drives most of the code generation below. The same file adds V4BF and V4HF as half-vector modes, commented as “for internal use”. The 128-bit vector mode V8HF comes from the existing VECTOR_MODES (FLOAT, 16) line.
The two formats get very different hardware support. Per the commit message, ISA 3.0 (power9) has scalar instructions that convert between _Float16 and float. ISA 3.1 (power10 and power11) adds conversions for the even elements of 16-bit vectors, for both formats. __bf16 has no scalar conversion instruction on any generation.
The mechanism: iterators, a register slot, and conversions
The new float16.md starts with mode iterators, which let one pattern template expand into one copy per listed mode. It also uses mode attributes, which substitute a mode-specific string into each copy:
(define_mode_iterator FP16 [BF HF])
(define_mode_iterator VS_FP16 [BF HF V8BF V8HF])
(define_mode_iterator FP16_HW [(BF "TARGET_BFLOAT16_HW")
(HF "TARGET_FLOAT16_HW")])
(define_mode_attr cvt_fp16_to_v4sf [(BF "xvcvbf16spn")
(HF "xvcvhpsp") ...])
FP16_HW attaches a condition to each mode, so a pattern written once is enabled per mode according to which hardware conversions exist.
Moves come first, because nothing else works without them. The *mov<mode>_internal insn lists alternatives for every register class a 16-bit value can live in. Each alternative is a constraint pair matched to one output template:
xxlor %x0,%x1,%x1 ; VSR -> VSR
lxsihzx %x0,%y1 ; mem -> VSR
stxsihx %x1,%y0 ; VSR -> mem
lhz / sth / mr ; GPR <-> mem, GPR -> GPR
mfvsrwz / mtvsrwz ; VSR <-> GPR
The VSR alternatives use instructions that load, store and transfer a halfword at one fixed position in the register: bits 48-63, the position lxsihzx loads into. Moves never widen the value, so a __bf16 in a VSR keeps its raw 16-bit memory encoding. The j and eZ alternatives load the constants +0.0 and -0.0. On power10, *mov<mode>_xxspltiw loads arbitrary constants with prefixed xxspltiw. When that instruction is unavailable, a define_split builds -0.0 by shifting a vector of all-ones halfwords.
Scalar conversions show the asymmetry. _Float16 gets one instruction in each direction, gated on TARGET_FLOAT16_HW:
(define_insn "extendhf<mode>2" ... "xscvhpdp %x0,%x1")
(define_insn "trunc<mode>hf2" ... "xscvdphp %x0,%x1")
__bf16 has no such instruction, so extendbf<mode>2 builds the conversion from integer and float instructions. It takes the value in a GPR, shifts it left 48 bits, moves it to a VSR and runs xscvspdpn:
/* The shift left of 48 combines a shift left of 16 bits to
convert a BFmode to a SFmode with shift left of 32 bits
to do the xscvspdpn instruction. */
emit_insn (gen_ashldi3 (op3, op1_di, GEN_INT (48)));
emit_move_insn (op2_di, op3);
The 16 bits turn a bfloat16 into the matching float bit pattern. The other 32 bits put that pattern in the word that xscvspdpn reads. The comment on xscvspdpn_sf explains why the output is not a plain float: scalar SFmode on PowerPC “uses the DFmode encoding” inside a register. The pattern is a define_insn_and_split that outputs # and splits only && reload_completed, so the register allocator treats it as one 12-byte instruction and its two scratch registers are allocated together.
The change: doing __bf16 arithmetic in float vector lanes
With conversions in place, a = b + c on __bf16 already compiles correctly. The problem is cost. The commit message shows the old sequence: two lhz, two sldi, two mtvsrd, two xscvspdpn, an fadds, then xscvdpsp and xvcvspbf16 to get back to bfloat16, and finally a store. Every operand goes through the GPRs and back, and the result has to be narrowed twice.
A new addbf3 pattern would be the obvious fix, but the commit rules it out: “GCC will not generate these patterns unless the user uses -Ofast. Without -Ofast, it will always convert BFmode into SFmode.” The middle end widens the operation to float before the backend sees it, so an arithmetic pattern in BFmode would only be used under -Ofast. The backend instead adds combiner patterns. They recognize the widen-operate-narrow sequence and rewrite it as vector work in the VSX unit:
lxsihzx 12,4,2 // load b into the bit 48-63 slot
lxsihzx 0,5,2
xxspltw 12,12,1 // move b into bits 16..31
xxspltw 0,0,1
xvcvbf16spn 12,12 // even BF elements -> V4SF
xvcvbf16spn 0,0
xvaddsp 0,0,12 // add in V4SF
xvcvspbf16 0,0 // back to BF memory format
stxsihx 0,3,2
The GPR round trip is gone. Values stay in VSRs from load to store, and the arithmetic is the vector instruction xvaddsp instead of the scalar fadds. The power10 vector conversions become the scalar conversions. The backend computes four lanes and uses one.
True vectors work the same way, through fp16_vectorization in float16.cc. It handles plus, minus, mult, smax, smin and four FMA variants. For each operand it calls rs6000_expand_interleave twice to split the eight 16-bit lanes into two halves. It widens each half to V4SFmode with xvcvhpsp (for V8HF) or xvcvbf16spn (for V8BF), runs the operation twice in V4SF, and packs the two halves back with vec_pack_trunc_v4sf_v8hf or _v8bf. FMS-style variants wrap the addend in NEG, and the NFMA-style variants wrap the whole FMA in NEG. The function starts with gcc_assert (can_create_pseudo_p ()), so it runs only during RTL expansion, while fresh pseudo registers can still be allocated.
Consequences for developers outside the backend
An ABI now exists. Per the commit message, scalar _Float16 and __bf16 are passed and returned in the traditional floating-point registers. They stay in their 16-bit memory encoding, in bits 48-63, and “the other bits in the register are unspecified”. A callee must not assume the upper bits are zero or sign-extended. Hand-written assembly, other compilers and FFI layers that pass these types must use the same slot and must not widen to float. Vector types (V8HF, V8BF) are passed in Altivec registers like any other 128-bit vector. The research notes report that the call-lowering code was not checked against this description, so the commit message is the stated contract.
Feature tests have two levels. rs6000_target_modify_macros defines __FLOAT16__ and __BFLOAT16__ whenever OPTION_MASK_FLOAT16 is set. The _HW macros also depend on the ISA level:
if ((flags & OPTION_MASK_P9_VECTOR) != 0)
rs6000_define_or_undefine_macro (define_p, "__FLOAT16_HW__");
if ((flags & OPTION_MASK_POWER10) != 0)
rs6000_define_or_undefine_macro (define_p, "__BFLOAT16_HW__");
So __BFLOAT16__ without __BFLOAT16_HW__ means the type exists but the compiler will not use power10’s hardware bfloat16 conversions. Because OPTION_MASK_FLOAT16 is in POWERPC_MASKS, it belongs to the set of flags whose defaults -mcpu= manages. The commit message adds -mfloat16/-mno-float16, a --with-powerpc-float16 configure switch, and says the feature is on by default on little-endian 64-bit Linux. The dossier did not reach the option declarations, so treat those details as the commit’s description.
The optimization level changes the code. Per the commit message, auto-vectorized __bf16 loops appear at -O3, but _Float16 needs -Ofast for the same vectorization. When comparing assembly across builds, check the flags first.
Seeing it yourself
Check the macros on a little-endian 64-bit PowerPC compiler:
echo | gcc -mcpu=power10 -dM -E - | grep -i float16
echo | gcc -mcpu=power9 -dM -E - | grep -i float16
On the power9 build, __BFLOAT16_HW__ should be missing.
To see the combiner rewrite, compile void f(__bf16 *a, __bf16 *b, __bf16 *c) { *a = *b + *c; } with -O2 -S, once with -mcpu=power10 and once with -mcpu=power9. Look for xvcvbf16spn and xvaddsp in the power10 output, and the sldi/mtvsrd/xscvspdpn sequence for the conversion to float in the power9 output. Add -fdump-rtl-combine to watch the patterns match.
For vectorization, write a loop over __bf16 and _Float16 arrays and compile it at -O3 and -Ofast. Compare which loops get vector instructions. The commit’s tests live in gcc/testsuite/gcc.target/powerpc/bf16-{1..5}.c and float16-{1..3}.c. Runtime helpers that the compiler calls instead of emitting inline code, such as float16-fixbfdi.c and float16-extendbfdf2.c, are in libgcc/config/rs6000/.
Files this lesson was written against, at commit
8264fc847f3e.
-
Declares HF and BF as 2-byte float modes and adds the internal V4HF/V4BF half-vector modes.
- gcc/config/rs6000/float16.md L21-92
Mode iterators gated on TARGET_FLOAT16_HW / TARGET_BFLOAT16_HW, and the mode attributes that map each mode to its vector conversion instruction.
-
The move alternatives (lxsihzx, stxsihx, mtvsrwz, mfvsrwz) that fix where a 16-bit scalar sits in a VSR, plus constant loading via xxspltiw and the -0.0 split.
-
_Float16 widens and narrows with one power9 instruction each way: xscvhpdp and xscvdphp.
-
__bf16 has no scalar conversion, so this splits after reload into a 48-bit shift, a move to a VSR, and xscvspdpn.
-
Splits eight 16-bit lanes into two V4SF halves, runs the operation or FMA variant on each, and packs the result back.
-
Shows that the _HW feature macros need both OPTION_MASK_FLOAT16 and an ISA-level mask.
-
OPTION_MASK_FLOAT16 joins the set of flags whose defaults -mcpu= controls.
- A C type works on a target only once its machine mode has move, convert and operate patterns, and moves come first.
- The register slot a backend picks for moving a small scalar effectively becomes its calling convention. Here, any bits outside the 16-bit slot are unspecified.
- If the ISA only has vector conversions, use them for scalar work: compute four lanes and keep one.
- If the middle end always widens an operation, an arithmetic pattern in the narrow mode will rarely be used. Combiner patterns over the widened sequence are what actually fire.
- Feature macros like __BFLOAT16__ and __BFLOAT16_HW__ mean different things: the type exists, versus the compiler will use hardware conversions for it.
In Details
The work lives in the new config/rs6000/float16.md and float16.cc, plus HF/BF entries in rs6000-modes.def. BF conversions are built from vector operations: extendbf<mode>2 uses sldi 48, a move to a VSR and xscvspdpn, and combiner patterns rewrite widened SFmode arithmetic into xvcvbf16spn/xv*sp/xvcvspbf16. Watch two interactions. The scalar ABI slot is bits 48-63 of the FPR/VSR, unwidened, with the upper bits unspecified. The _HW macros are keyed on P9_VECTOR/POWER10 as well as OPTION_MASK_FLOAT16.
- machine mode
- GCC's RTL name for the size and interpretation of a value, such as SFmode (32-bit float) or V4SFmode (four 32-bit floats). Backends declare their modes in a *-modes.def file.
- HFmode
- The GCC machine mode for IEEE 754 binary16 (half precision), the mode behind the C type _Float16.
- BFmode
- The GCC machine mode for bfloat16, a 16-bit float with float32's 8-bit exponent and a 7-bit mantissa. It is the mode behind __bf16.
- SFmode
- GCC's 32-bit single-precision float mode. On PowerPC, a scalar SFmode value in a register is held in double-precision encoding.
- V4SFmode
- A 128-bit vector mode holding four single-precision floats.
- machine description
- The .md files in a GCC backend that list RTL patterns and the assembly each one produces. Instruction selection, splitting and combining are all driven by them.
- mode iterator
- An .md construct that expands one pattern template into one copy per listed mode, optionally with a per-mode enabling condition.
- mode attribute
- An .md mapping from mode to a string, such as an instruction mnemonic, substituted into each copy of a pattern made by an iterator.
- define_expand
- A pattern that runs C code while GIMPLE is being lowered to RTL, to emit one or more RTL instructions for a named operation such as a move.
- define_split
- A pattern that rewrites one RTL instruction into a different sequence, often only after register allocation.