google / heir

A compiler for homomorphic encryption
https://heir.dev/
Apache License 2.0
292 stars 44 forks source link

types: simplify assembly formats by using type constraints #106

Closed asraa closed 1 year ago

asraa commented 1 year ago

Most of our ops do not utilize Constrains which can be used to verify operations.

For example, it may enforce that for BGV mul ops, that their ring parameter must be the same across inputs and outputs, or that the inputs size must be the same, while their output size is predicatable one greater.

These also allow simplification of the assembly format.

mr0re1 commented 1 year ago

149

mr0re1 commented 1 year ago

In regard of assembly format for SameOperandsAndResultType ops. @asraa , @j2kun could you please comment on the options, propose new ones ?

// Current:
%z = bgv.mul(%x, %y) : !bgv.ciphertext<rings=#rings> -> !bgv.ciphertext<rings=#rings, dim=3>
%q = bgv.modulus_switch(%x) {from_level = 1, to_level=0} : (!bgv.ciphertext<rings=#rings>) -> !bgv.ciphertext<rings=#rings, dim=2, level=0>

// Option 1. Render `rings` spec before the ciphertext first operand, 
// render all ciphertext operands without `rings` attribute
%z = bgv.mul(%x, %y) : <rings=#rings> !bgv.ciphertext -> !bgv.ciphertext<dim=3>
%q = bgv.modulus_switch(%x) {from_level = 1, to_level=0} : <rings=#rings> !bgv.ciphertext -> !bgv.ciphertext<dim=2, level=0>

// Option 2. Do not render `rings` at all, 
// assuming that in most cases within block all operation will be done on the same rings, so no disambiguation is needed.
%z = bgv.mul(%x, %y) : !bgv.ciphertext -> !bgv.ciphertext<dim=3>
%q = bgv.modulus_switch(%x) {from_level = 1, to_level=0} : !bgv.ciphertext -> !bgv.ciphertext<dim=2, level=0>
j2kun commented 1 year ago

In all of these cases the type actually legitimately changes, though, right? So we can't use a type constraint that asserts equality to simplify the assembly format. In that case I don't mind keeping the more verbose syntax, at least to promote consistency.

github-actions[bot] commented 8 months ago

[!NOTE] Re-commenting because this issue was closed with unresolved TODOs.

This issue has 1 outstanding TODOs:

This comment was autogenerated by todo-backlinks