Integer execution¶
Integer code is most of what a compiler emits, and on LoongArch it is also the
easiest part of the core to reason about: the instructions are simple, the
register file is 32 entries with $r0 hardwired to zero, and most operations are
single-cycle. The interesting parts are the exceptions: multiply, divide, the
bit-field instructions, and the conditional-select sequences that replace
branches.
Execution resources¶
LA664 has four integer ALUs fed by a 48-entry integer scheduler. Two of the ALUs can execute branch instructions, and two have integer multiply capability. Rename is 6-wide and the ROB holds 256 entries, so the core can keep a lot of integer work in flight.
LA464 also has four ALUs, a big step up from the two ALUs of the GS464E it evolved from. Only one branch can resolve per cycle, and two ALUs can multiply. Rename is 4-wide and the ROB holds 128 entries. Three 32-entry queues hold memory, integer, and vector work, rather than the three 48-entry queues on LA664.
Simple operations such as add.d, and, or, xor, slt, and the shifts are
single-cycle on both cores. What differs is how many can start per cycle and how
much room the schedulers have to find independent work.
Multiply and divide¶
mul.d produces the low half of a 64-bit product, mulh.d and mulhu.d the
signed and unsigned high halves, with .w forms for 32-bit results. The two
multiply pipes mean the core can start two independent multiplies per cycle.
LoongPerf measures mul.d and mulh.d at 4 cycles on both cores.
Division is the slow case. div.d and mod.d produce quotient and remainder, and
the operation is iterative, so latency depends on the operand values. LoongPerf
fits the divider as 7 + floor(quotient_bits / 2): it retires two quotient bits per
cycle. div.w tops out at 22 cycles (23 unsigned), div.d at 38 (39 unsigned),
and a zero dividend is the 5-cycle minimum. Both cores implement the same model;
the only difference is in the remainder path, where LA464's mod can leave the
long path one digit earlier. Throughput is far below one instruction per cycle,
and the latency is data-dependent, so a single number does not describe it.
Code that divides by a constant should let the compiler turn it into a multiply and a shift. Code that divides inside a loop should be restructured, or replaced with a reciprocal estimate and a Newton step when the exact quotient is not required. The integer divider is one of the execution units where a small algorithmic change usually beats any instruction-level tweak.
Bit fields and conditional selects¶
LoongArch has a full set of bit-manipulation instructions and they are worth
using. bstrins.d and bstrpick.d insert or extract an arbitrary bit field in one
instruction, bytepick.d assembles a value from two registers at byte
granularity, revb.2h, revb.4h, revb.2w, and revb.d reverse byte order,
clz.w/clz.d and ctz.w/ctz.d count leading and trailing zeros,
crc.w.b.w and the wider crc.w.* forms accumulate CRC, and alsl.d combines a
shift with an add. Each of
these replaces a short sequence of shifts, masks, and adds.
Conditional assignment is the other place where instruction selection matters.
slt and sltu produce a 0 or 1, and maskeqz/masknez turn that into a
select. A short sequence can replace a branch when the branch is genuinely
unpredictable, and it avoids the front-end cost of a misprediction.
When the branch is predictable, the branch is usually cheaper, so this is a
trade-off to measure.
According to LoongPerf, the CRC unit is the largest single change between the
two cores. crc.w.b.w and its wider forms fall from 5, 7, 11, and 19 cycles on
LA464 to 1 cycle on LA664, and throughput rises from 0.10–0.29 IPC to 2 IPC.
bstrins, bytepick, and alsl also double from 2 to 4 IPC.
The $r0 register is hardwired to zero, which makes zeroing cheap in terms of
register pressure but not necessarily in terms of execution slots. LA664
recognizes zeroing at rename and eliminates it, so or rD, $r0, $r0 or
xor rD, rD, rD does not occupy an ALU. LA464 has no such optimization, and the
instruction takes a slot.
What the compiler does¶
Both compilers pattern-match the bit-field idioms. LLVM turns (x >> s) & mask
into bstrpick and (x & mask) | (y << s) into bstrins; GCC has equivalent
bstrins/bstrpick patterns and peepholes for the same shapes. Both use
bytepick when a value is assembled from bytes of two registers. Writing the
mask-and-shift form is therefore fine, and a hand-written multi-instruction
sequence is usually the pessimization.
LLVM also decomposes multiplication by 3, 5, 9, or 17, and by values of the form
2^k +/- 1, into alsl or a shift plus add/sub. Those sequences have 1-cycle
latency, against 4 cycles for mul. GCC has the same alsl patterns when the
RTL already contains a shift and add, so a hand-written mul for a small
constant is often the slower choice.
What this means for software¶
Keep dependency chains short and give the scheduler independent work. The ROB is twice as large on LA664 and the scheduler is split into three queues, so a loop that is latency-bound on LA464 may become throughput-bound on LA664, and unrolling that helps one may not help the other.
Spread work across the pipes. A loop that is all multiplies is limited by the two multiply pipes no matter how wide the rest of the core is; mixing multiplies with adds and loads keeps all four ALUs busy. The same applies to branches: only two of LA664's ALUs can execute them, and LA464 resolves one branch per cycle, so a branch-heavy loop can leave the other ALUs idle.
Use the bit-field instructions instead of hand-written shift-and-mask sequences. They reduce instruction count, which matters on cores without a micro-op cache, and they often have the same single-cycle latency as a plain ALU operation.
Avoid division in hot loops. If the divisor is a constant, the compiler can strength-reduce it. If it is not, consider a reciprocal or a different algorithm. The divider's data-dependent latency is the most persistent performance problem in the integer unit.