heir icon indicating copy to clipboard operation
heir copied to clipboard

Migrate dialect conversion passes to `allowPatternRollback = false`

Open j2kun opened this issue 4 months ago • 1 comments

Most of them migrate smoothly. The following fail

  • lib/Dialect/Arith/Conversions/ArithToCGGI/ArithToCGGI.cpp
  • lib/Transforms/MemrefToArith/MemrefGlobalReplace.cpp

MemrefGlobalReplace will be deleted I think, so it can stay opted out.

ArithToCGGI has tests that... don't run secret-wrap-generic at the start? (e.g., tests/Examples/tfhe_rust_hl/cpu/fully_connected.mlir starts with --mlir-to-cggi so the {secret.secret} annotation is never touched). That test crashes at materialization because it's trying to materialize an unrealized conversion cast of a memref function argument, but with allowPatternRollback=true those materializations are never needed or run in the first place. So I suspect this pass could be fixed by instead properly converting the input IR to use secret.generic, secret types, and split-generic before converting to CGGI.

IR dump after failure:

// -----// IR Dump After CGGIToTfheRust Failed (cggi-to-tfhe-rust) //----- //
#constant_coefficient_encoding = #lwe.constant_coefficient_encoding<scaling_factor = 16777216>
#constant_coefficient_encoding1 = #lwe.constant_coefficient_encoding<scaling_factor = 1>
#key = #lwe.key<>
#ring_i32_1 = #polynomial.ring<coefficientType = i32, polynomialModulus = <1>>
#ring_i8_1 = #polynomial.ring<coefficientType = i8, polynomialModulus = <1>>
#ciphertext_space__D742 = #lwe.ciphertext_space<ring = #ring_i32_1, encryption_type = msb, size = 742>
!ct__D742 = !lwe.lwe_ciphertext<application_data = <message_type = i8, overflow = #lwe.preserve_overflow<>>, plaintext_space = <ring = #ring_i8_1, encoding = #constant_coefficient_encoding>, ciphertext_space = #ciphertext_space__D742, key = #key>
!ct__D742_1 = !lwe.lwe_ciphertext<application_data = <message_type = i32, overflow = #lwe.preserve_overflow<>>, plaintext_space = <ring = #ring_i32_1, encoding = #constant_coefficient_encoding1>, ciphertext_space = #ciphertext_space__D742, key = #key>
module {
  func.func @fn_under_test(%arg0: memref<1x1x!tfhe_rust.eui8> {secret.secret}) -> memref<1x1x!tfhe_rust.eui32> {
    %0 = builtin.unrealized_conversion_cast %arg0 : memref<1x1x!tfhe_rust.eui8> to memref<1x1x!ct__D742, strided<[?, ?], offset: ?>>
    %c0 = arith.constant 0 : index
    %c2_i16 = arith.constant 2 : i16
    %c1_i16 = arith.constant 1 : i16
    %1 = builtin.unrealized_conversion_cast %0 : memref<1x1x!ct__D742, strided<[?, ?], offset: ?>> to memref<1x1xi8, strided<[?, ?], offset: ?>>
    %2 = builtin.unrealized_conversion_cast %1 : memref<1x1xi8, strided<[?, ?], offset: ?>> to memref<1x1xi8>
    %alloc = memref.alloc() {alignment = 64 : i64} : memref<1x1xi32>
    %3 = builtin.unrealized_conversion_cast %alloc : memref<1x1xi32> to memref<1x1x!ct__D742_1>
    %4 = builtin.unrealized_conversion_cast %3 : memref<1x1x!ct__D742_1> to memref<1x1x!tfhe_rust.eui32>
    %5 = memref.load %2[%c0, %c0] : memref<1x1xi8>
    %6 = arith.extsi %5 : i8 to i16
    %7 = arith.muli %6, %c2_i16 : i16
    %8 = arith.addi %7, %c1_i16 : i16
    %9 = arith.extsi %8 : i16 to i32
    memref.store %9, %alloc[%c0, %c0] : memref<1x1xi32>
    return %4 : memref<1x1x!tfhe_rust.eui32>
  }
}

j2kun avatar Sep 10 '25 22:09 j2kun

This issue has 2 outstanding TODOs:

This comment was autogenerated by todo-backlinks

github-actions[bot] avatar Sep 12 '25 14:09 github-actions[bot]