jasmin-lang / jasmin

Language for high-assurance and high-speed cryptography
MIT License
268 stars 55 forks source link

Unexpected failure of register allocation #340

Closed vbgl closed 1 year ago

vbgl commented 1 year ago

The Jasmin compiler cannot compile the following program.

inline fn xor(reg u256 a b) -> reg u256 {
  reg u256 c;
  c = a ^ b;
  return c;
}

export
fn main(reg u256 x) -> reg u256 {
  reg u256[1] a;
  a[0] = x;
  x = xor(a[0], a[0]);
  return x;
}

The computation of conflicts during register allocation is not precise enough.

When all u256 are changed into u64, the error goes away (but a warning remains): this discrepancy is suspicious.

vbgl commented 1 year ago

This particular example has been fixed in #341.

The difference in behavior when using general purpose registers (when values are 64-bit wide) instead of vector registers (when values are 256-bit wide) can be explained by the difference in calling conventions: with 64-bit registers, the argument x to the export function comes in a different register than the returned value and the result of the xor must be at the same place as its first argument; a copy is necessary. With 256-bit registers however, arguments and returned values use the same register (and even if it were not the case, the wide xor instruction can put its result in any desired wide register).

The weakness of register-allocation might still be present, but without a test-case allowing to reproduce it, I suggest to close this issue.