Skip to content

Commit 0aed724

Browse files
j2kuncopybara-github
authored andcommitted
misc changes to support optimal bufferization
PiperOrigin-RevId: 931403381
1 parent 4df75b8 commit 0aed724

11 files changed

Lines changed: 37 additions & 36 deletions

File tree

lib/Dialect/Lattigo/IR/LattigoBGVOps.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Lattigo_BGVOp<string mnemonic, list<Trait> traits = []> :
1313
// This operation cannot be marked as Pure because it represents a buffer allocation.
1414
// If marked as Pure, CSE will deduplicate them, causing multiple in-place operations
1515
// (like encode) to share the same buffer and overwrite each other.
16-
def Lattigo_BGVNewPlaintextOp : Lattigo_BGVOp<"new_plaintext", []> {
16+
def Lattigo_BGVNewPlaintextOp : Lattigo_BGVOp<"new_plaintext"> {
1717
let summary = "Create a new plaintext in the Lattigo BGV dialect";
1818
let description = [{
1919
This operation creates a new plaintext value in the Lattigo BGV dialect.

lib/Dialect/Lattigo/IR/LattigoCKKSOps.td

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Lattigo_CKKSOp<string mnemonic, list<Trait> traits = []> :
1515
// This operation cannot be marked as Pure because it represents a buffer allocation.
1616
// If marked as Pure, CSE will deduplicate them, causing multiple in-place operations
1717
// (like encode) to share the same buffer and overwrite each other.
18-
def Lattigo_CKKSNewPlaintextOp : Lattigo_CKKSOp<"new_plaintext", []> {
18+
def Lattigo_CKKSNewPlaintextOp : Lattigo_CKKSOp<"new_plaintext"> {
1919
let summary = "Create a new plaintext in the Lattigo CKKS dialect";
2020
let description = [{
2121
This operation creates a new plaintext value in the Lattigo CKKS dialect.
@@ -48,7 +48,7 @@ def Lattigo_CKKSNewEncoderOp : Lattigo_CKKSOp<"new_encoder", [Pure]> {
4848
let results = (outs Lattigo_CKKSEncoder:$encoder);
4949
}
5050

51-
def Lattigo_CKKSEncodeOp : Lattigo_CKKSOp<"encode", [InPlaceOpInterface, Pure, PlaintextEncodeOpInterface]> {
51+
def Lattigo_CKKSEncodeOp : Lattigo_CKKSOp<"encode", [InPlaceOpInterface, PlaintextEncodeOpInterface]> {
5252
let summary = "Encode a plaintext value in the Lattigo CKKS dialect";
5353
let description = [{
5454
This operation encodes a plaintext value using the specified encoder in the Lattigo CKKS dialect.
@@ -68,7 +68,7 @@ def Lattigo_CKKSEncodeOp : Lattigo_CKKSOp<"encode", [InPlaceOpInterface, Pure, P
6868
let extraClassDeclaration = "int getInPlaceOperandIndex() { return 2; }";
6969
}
7070

71-
def Lattigo_CKKSDecodeOp : Lattigo_CKKSOp<"decode", [AllTypesMatch<["value", "decoded"]>, Pure]> {
71+
def Lattigo_CKKSDecodeOp : Lattigo_CKKSOp<"decode", [AllTypesMatch<["value", "decoded"]>]> {
7272
let summary = "Decode a plaintext value in the Lattigo CKKS dialect";
7373
let description = [{
7474
This operation decodes a plaintext value using the specified encoder in the Lattigo CKKS dialect.

lib/Dialect/TensorExt/Conversions/TensorExtToTensor/TensorExtToTensor.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,8 @@ struct ConvertRotateOp : public OpRewritePattern<RotateOp> {
155155
leftSliceOffsets[leftSliceOffsets.size() - 1] = dimMinusShift;
156156
rightSliceOffsets[rightSliceOffsets.size() - 1] = b.getIndexAttr(0);
157157

158-
auto empty =
159-
tensor::EmptyOp::create(rewriter, op.getLoc(), tensorType.getShape(),
160-
tensorType.getElementType());
161158
auto insertedLeftSlice = tensor::InsertSliceOp::create(
162-
b, left.getResult(), empty, leftSliceOffsets, leftSliceSizes,
159+
b, left.getResult(), op.getTensor(), leftSliceOffsets, leftSliceSizes,
163160
allOneStrides);
164161
auto insertedRightSlice = tensor::InsertSliceOp::create(
165162
b, right.getResult(), insertedLeftSlice.getResult(), rightSliceOffsets,

lib/Kernel/BUILD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ cc_test(
134134
":AbstractValue",
135135
":ArithmeticDag",
136136
":EvalVisitor",
137+
":IRMaterializingVisitor",
137138
":Kernel",
138139
":KernelImplementation",
139140
":RotationCountVisitor",
@@ -142,8 +143,11 @@ cc_test(
142143
"@heir//lib/Utils/Layout:Convolution",
143144
"@heir//lib/Utils/Layout:Evaluate",
144145
"@heir//lib/Utils/Layout:Utils",
146+
"@llvm-project//mlir:ArithDialect",
145147
"@llvm-project//mlir:IR",
148+
"@llvm-project//mlir:SCFDialect",
146149
"@llvm-project//mlir:Support",
150+
"@llvm-project//mlir:TensorDialect",
147151
],
148152
)
149153

lib/Kernel/IRMaterializingVisitor.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <cmath>
55
#include <cstddef>
66
#include <cstdint>
7+
#include <utility>
78
#include <variant>
89
#include <vector>
910

lib/Pipelines/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ cc_library(
109109
"@heir//lib/Dialect/LWE/Conversions/LWEToPolynomial",
110110
"@heir//lib/Dialect/LWE/Transforms:AddDebugPort",
111111
"@heir//lib/Dialect/LWE/Transforms:ImplementTrivialEncryptionAsAddition",
112-
"@heir//lib/Dialect/Lattigo/Transforms:AllocToInPlace",
113112
"@heir//lib/Dialect/Lattigo/Transforms:ConfigureCryptoContext",
114113
"@heir//lib/Dialect/Openfhe/Transforms:AllocToInPlace",
115114
"@heir//lib/Dialect/Openfhe/Transforms:ConfigureCryptoContext",

lib/Target/Lattigo/LattigoEmitter.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "mlir/include/mlir/Dialect/Affine/IR/AffineOps.h" // from @llvm-project
3535
#include "mlir/include/mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project
3636
#include "mlir/include/mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project
37+
#include "mlir/include/mlir/Dialect/MemRef/IR/MemRef.h" // from @llvm-project
3738
#include "mlir/include/mlir/Dialect/SCF/IR/SCF.h" // from @llvm-project
3839
#include "mlir/include/mlir/Dialect/Tensor/IR/Tensor.h" // from @llvm-project
3940
#include "mlir/include/mlir/Dialect/Utils/StaticValueUtils.h" // from @llvm-project
@@ -119,8 +120,9 @@ LogicalResult LattigoEmitter::translate(Operation& op) {
119120
.Case<memref::AllocOp, memref::LoadOp, memref::StoreOp,
120121
memref::CopyOp, memref::GlobalOp, memref::GetGlobalOp,
121122
memref::ExpandShapeOp, memref::CollapseShapeOp, memref::CastOp,
122-
memref::SubViewOp, memref::ExtractStridedMetadataOp,
123-
memref::DimOp>([&](auto op) { return printOperation(op); })
123+
memref::ReinterpretCastOp, memref::SubViewOp,
124+
memref::ExtractStridedMetadataOp, memref::DimOp>(
125+
[&](auto op) { return printOperation(op); })
124126

125127
// Lattigo ops
126128
.Case<
@@ -1197,6 +1199,17 @@ LogicalResult LattigoEmitter::printOperation(memref::CastOp op) {
11971199
return success();
11981200
}
11991201

1202+
LogicalResult LattigoEmitter::printOperation(memref::ReinterpretCastOp op) {
1203+
if (!op.getType().getLayout().isIdentity()) {
1204+
return op.emitOpError(
1205+
"requires result type to have identity layout (contiguous and 0 "
1206+
"offset)");
1207+
}
1208+
std::string name = getName(op.getResult());
1209+
emitAssignment(name, getName(op.getSource()));
1210+
return success();
1211+
}
1212+
12001213
LogicalResult LattigoEmitter::printOperation(memref::SubViewOp op) {
12011214
MemRefType srcType = op.getSourceType();
12021215
MemRefType resultType = op.getType();

lib/Target/Lattigo/LattigoEmitter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ class LattigoEmitter {
149149
LogicalResult printOperation(::mlir::memref::ExpandShapeOp op);
150150
LogicalResult printOperation(::mlir::memref::CollapseShapeOp op);
151151
LogicalResult printOperation(::mlir::memref::CastOp op);
152+
LogicalResult printOperation(::mlir::memref::ReinterpretCastOp op);
152153
LogicalResult printOperation(::mlir::memref::SubViewOp op);
153154
LogicalResult printOperation(::mlir::memref::ExtractStridedMetadataOp op);
154155
LogicalResult printOperation(::mlir::memref::DimOp op);

tests/Dialect/TensorExt/Conversions/tensor_ext_to_tensor/rotate_dynamic.mlir

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,15 @@ func.func @test_rotate_dynamic(%0: tensor<16xi32>, %shift: index) -> tensor<16xi
1313
// CHECK: tensor.extract_slice
1414
// CHECK-SAME: [%[[v2]]] [%[[v3]]] [1]
1515

16-
// CHECK: tensor.empty
17-
18-
// CHECK: tensor.insert_slice
19-
// CHECK-SAME: [%[[v3]]] [%[[v2]]] [1]
20-
// CHECK: tensor.insert_slice
21-
// CHECK-SAME: [0] [%[[v3]]] [1]
16+
// CHECK: %[[INSERTED:.*]] = tensor.insert_slice %{{.*}} into %[[arg0]][%[[v3]]] [%[[v2]]] [1]
17+
// CHECK: tensor.insert_slice %{{.*}} into %[[INSERTED]][0] [%[[v3]]] [1]
2218
%1 = tensor_ext.rotate %0, %shift : tensor<16xi32>, index
2319
return %1 : tensor<16xi32>
2420
}
2521

2622

2723
// CHECK: @test_rotate_dynamic_multidim
24+
// CHECK-SAME: (%[[arg0:.*]]: tensor<3x4x16xi32>, %{{.*}}: index) -> tensor<3x4x16xi32>
2825
func.func @test_rotate_dynamic_multidim(%0: tensor<3x4x16xi32>, %shift: index) -> tensor<3x4x16xi32> {
2926
// CHECK: %[[c16:.*]] = arith.constant 16
3027
// CHECK: %[[v0:.*]] = arith.remsi %[[shift]], %[[c16]]
@@ -37,12 +34,8 @@ func.func @test_rotate_dynamic_multidim(%0: tensor<3x4x16xi32>, %shift: index) -
3734
// CHECK: tensor.extract_slice
3835
// CHECK-SAME: [0, 0, %[[v2]]] [3, 4, %[[v3]]] [1, 1, 1]
3936

40-
// CHECK: tensor.empty
41-
42-
// CHECK: tensor.insert_slice
43-
// CHECK-SAME: [0, 0, %[[v3]]] [3, 4, %[[v2]]] [1, 1, 1]
44-
// CHECK: tensor.insert_slice
45-
// CHECK-SAME: [0, 0, 0] [3, 4, %[[v3]]] [1, 1, 1]
37+
// CHECK: %[[INSERTED:.*]] = tensor.insert_slice %{{.*}} into %[[arg0]][0, 0, %[[v3]]] [3, 4, %[[v2]]] [1, 1, 1]
38+
// CHECK: tensor.insert_slice %{{.*}} into %[[INSERTED]][0, 0, 0] [3, 4, %[[v3]]] [1, 1, 1]
4639
%1 = tensor_ext.rotate %0, %shift : tensor<3x4x16xi32>, index
4740
return %1 : tensor<3x4x16xi32>
4841
}

tests/Dialect/TensorExt/Conversions/tensor_ext_to_tensor/rotate_static.mlir

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,31 @@
11
// RUN: heir-opt --tensor-ext-to-tensor %s | FileCheck %s
22

33
// CHECK: @test_rotate
4+
// CHECK-SAME: (%[[arg0:.*]]: tensor<16xi32>) -> tensor<16xi32>
45
func.func @test_rotate(%0: tensor<16xi32>) -> tensor<16xi32> {
56
// CHECK: tensor.extract_slice
67
// CHECK-SAME: [0] [1] [1]
78
// CHECK: tensor.extract_slice
89
// CHECK-SAME: [1] [15] [1]
910

10-
// CHECK: tensor.empty
11-
12-
// CHECK: tensor.insert_slice
13-
// CHECK-SAME: [15] [1] [1]
14-
// CHECK: tensor.insert_slice
15-
// CHECK-SAME: [0] [15] [1]
11+
// CHECK: %[[INSERTED:.*]] = tensor.insert_slice %{{.*}} into %[[arg0]][15] [1] [1]
12+
// CHECK: tensor.insert_slice %{{.*}} into %[[INSERTED]][0] [15] [1]
1613
%c1 = arith.constant 1 : i32
1714
%1 = tensor_ext.rotate %0, %c1 : tensor<16xi32>, i32
1815
return %1 : tensor<16xi32>
1916
}
2017

2118

2219
// CHECK: @test_rotate_multidim
20+
// CHECK-SAME: (%[[arg0:.*]]: tensor<3x4x16xi32>) -> tensor<3x4x16xi32>
2321
func.func @test_rotate_multidim(%0: tensor<3x4x16xi32>) -> tensor<3x4x16xi32> {
2422
// CHECK: tensor.extract_slice
2523
// CHECK-SAME: [0, 0, 0] [3, 4, 3] [1, 1, 1]
2624
// CHECK: tensor.extract_slice
2725
// CHECK-SAME: [0, 0, 3] [3, 4, 13] [1, 1, 1]
2826

29-
// CHECK: tensor.empty
30-
31-
// CHECK: tensor.insert_slice
32-
// CHECK-SAME: [0, 0, 13] [3, 4, 3] [1, 1, 1]
33-
// CHECK: tensor.insert_slice
34-
// CHECK-SAME: [0, 0, 0] [3, 4, 13] [1, 1, 1]
27+
// CHECK: %[[INSERTED:.*]] = tensor.insert_slice %{{.*}} into %[[arg0]][0, 0, 13] [3, 4, 3] [1, 1, 1]
28+
// CHECK: tensor.insert_slice %{{.*}} into %[[INSERTED]][0, 0, 0] [3, 4, 13] [1, 1, 1]
3529
%c3 = arith.constant 3 : i32
3630
%1 = tensor_ext.rotate %0, %c3 : tensor<3x4x16xi32>, i32
3731
return %1 : tensor<3x4x16xi32>

0 commit comments

Comments
 (0)