Skip to content

Commit 0df1895

Browse files
asraacopybara-github
authored andcommitted
fix: fix LUT input pin orderings to be MSB to LSB in comb truth table operations
Previously, Yosys Optimizer's RTLIL importer collected alphabetical pins A, B, C, (...) for truth table operations when importing the optimized RTLIL. A is the LSB and C is the MSB top bit. When converting to comb truth table ops, we place A, B, C as the inputs, while comb's truth table op expects inputs ordered from MSB to LSB. This resulted in correctness issues later in lowerings to CGGI (whose LUT op also expects MSB to LSB) and then tfhe_rust. PiperOrigin-RevId: 603044828
1 parent 6293a83 commit 0df1895

File tree

19 files changed

+207
-66
lines changed

19 files changed

+207
-66
lines changed

include/Dialect/BUILD

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Built-in common HEIR definitions
2+
3+
load("@llvm-project//mlir:tblgen.bzl", "gentbl_cc_library", "td_library")
4+
5+
package(
6+
default_applicable_licenses = ["@heir//:license"],
7+
default_visibility = ["//visibility:public"],
8+
)
9+
10+
exports_files(
11+
[
12+
"HEIRInterfaces.h",
13+
],
14+
)
15+
16+
td_library(
17+
name = "td_files",
18+
srcs = [
19+
"HEIRInterfaces.td",
20+
],
21+
# include from the heir-root to enable fully-qualified include-paths
22+
includes = ["../.."],
23+
deps = [
24+
"@llvm-project//mlir:OpBaseTdFiles",
25+
],
26+
)
27+
28+
gentbl_cc_library(
29+
name = "interfaces_inc_gen",
30+
tbl_outs = [
31+
(
32+
["-gen-op-interface-decls"],
33+
"HEIRInterfaces.h.inc",
34+
),
35+
(
36+
["-gen-op-interface-defs"],
37+
"HEIRInterfaces.cpp.inc",
38+
),
39+
],
40+
tblgen = "@llvm-project//mlir:mlir-tblgen",
41+
td_file = "HEIRInterfaces.td",
42+
deps = [
43+
":td_files",
44+
],
45+
)

include/Dialect/CGGI/IR/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ gentbl_cc_library(
7373
deps = [
7474
":dialect_inc_gen",
7575
":td_files",
76+
"@heir//include/Dialect:td_files",
7677
"@heir//include/Dialect/LWE/IR:td_files",
7778
"@heir//include/Dialect/Polynomial/IR:td_files",
7879
],

include/Dialect/CGGI/IR/CGGIOps.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define HEIR_INCLUDE_DIALECT_CGGI_IR_CGGIOPS_H_
33

44
#include "include/Dialect/CGGI/IR/CGGIDialect.h"
5+
#include "include/Dialect/HEIRInterfaces.h"
56
#include "include/Dialect/LWE/IR/LWETypes.h"
67
#include "mlir/include/mlir/IR/BuiltinOps.h" // from @llvm-project
78
#include "mlir/include/mlir/Interfaces/InferTypeOpInterface.h" // from @llvm-project

include/Dialect/CGGI/IR/CGGIOps.td

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef HEIR_INCLUDE_DIALECT_CGGI_IR_CGGIOPS_TD_
22
#define HEIR_INCLUDE_DIALECT_CGGI_IR_CGGIOPS_TD_
33

4+
include "include/Dialect/HEIRInterfaces.td"
45
include "include/Dialect/CGGI/IR/CGGIDialect.td"
56

67
include "include/Dialect/Polynomial/IR/PolynomialAttributes.td"
@@ -53,7 +54,9 @@ class CGGI_LutOp<string mnemonic, list<Trait> traits = []>
5354
Pure,
5455
Commutative,
5556
Elementwise,
56-
Scalarizable
57+
Scalarizable,
58+
DeclareOpInterfaceMethods<LUTOpInterface>
59+
5760
]> {
5861
let results = (outs LWECiphertextLike:$output);
5962
let assemblyFormat = "`(` operands `)` attr-dict `:` qualified(type($output))" ;

include/Dialect/Comb/IR/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ td_library(
1717
],
1818
includes = ["../../../.."],
1919
deps = [
20+
"@heir//include/Dialect:td_files",
2021
"@llvm-project//mlir:BuiltinDialectTdFiles",
2122
"@llvm-project//mlir:ControlFlowInterfacesTdFiles",
2223
"@llvm-project//mlir:FunctionInterfacesTdFiles",

include/Dialect/Comb/IR/CombOps.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define HEIR_INCLUDE_DIALECT_COMB_COMBOPS_H
1515

1616
#include "include/Dialect/Comb/IR/CombDialect.h"
17+
#include "include/Dialect/HEIRInterfaces.h"
1718
#include "mlir/include/mlir/Bytecode/BytecodeOpInterface.h" // from @llvm-project
1819
#include "mlir/include/mlir/IR/BuiltinOps.h" // from @llvm-project
1920
#include "mlir/include/mlir/IR/OpImplementation.h" // from @llvm-project

include/Dialect/Comb/IR/Combinational.td

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#ifndef HEIR_INCLUDE_DIALECT_COMB_COMBINATIONAL_TD
1818
#define HEIR_INCLUDE_DIALECT_COMB_COMBINATIONAL_TD
1919

20+
include "include/Dialect/HEIRInterfaces.td"
21+
2022
include "mlir/Interfaces/InferTypeOpInterface.td"
2123
include "mlir/IR/BuiltinAttributes.td"
2224
include "mlir/IR/EnumAttr.td"
@@ -270,7 +272,7 @@ def MuxOp : CombOp<"mux",
270272

271273
}
272274

273-
def TruthTableOp : CombOp<"truth_table", [Pure]> {
275+
def TruthTableOp : CombOp<"truth_table", [Pure, DeclareOpInterfaceMethods<LUTOpInterface>]> {
274276
let summary = "Return a true/false based on a lookup table";
275277
let description = [{
276278
```

include/Dialect/HEIRInterfaces.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef HEIR_INCLUDE_DIALECT_HEIRINTERFACES_H_
2+
#define HEIR_INCLUDE_DIALECT_HEIRINTERFACES_H_
3+
4+
#include "mlir/include/mlir/IR/Builders.h" // from @llvm-project
5+
#include "mlir/include/mlir/IR/BuiltinTypes.h" // from @llvm-project
6+
#include "mlir/include/mlir/IR/Dialect.h" // from @llvm-project
7+
#include "mlir/include/mlir/IR/DialectImplementation.h" // from @llvm-project
8+
9+
// Pull in HEIR interfaces
10+
#include "include/Dialect/HEIRInterfaces.h.inc"
11+
12+
#endif // HEIR_INCLUDE_DIALECT_HEIRINTERFACES_H_

include/Dialect/HEIRInterfaces.td

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
include "mlir/IR/OpBase.td"
2+
3+
def LUTOpInterface : OpInterface<"LUTOpInterface"> {
4+
let description = [{
5+
This is an example interface definition.
6+
}];
7+
8+
let methods = [
9+
InterfaceMethod<
10+
"Gets lookup table inputs from most significant bit to least.",
11+
"mlir::ValueRange", "getLookupTableInputs"
12+
>,
13+
];
14+
}

lib/Dialect/BUILD

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Built in HEIR declarations
2+
3+
package(
4+
default_applicable_licenses = ["@heir//:license"],
5+
default_visibility = ["//visibility:public"],
6+
)
7+
8+
cc_library(
9+
name = "HEIRInterfaces",
10+
srcs = [
11+
"HEIRInterfaces.cpp",
12+
],
13+
hdrs = [
14+
"@heir//include/Dialect:HEIRInterfaces.h",
15+
],
16+
deps = [
17+
"@heir//include/Dialect:interfaces_inc_gen",
18+
"@llvm-project//mlir:IR",
19+
],
20+
)

0 commit comments

Comments
 (0)