Skip to content

Poly canonicalization #91410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions mlir/include/mlir/Dialect/Polynomial/IR/Polynomial.td
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ def Polynomial_SubOp : Polynomial_BinaryOp<"sub"> {
%2 = polynomial.sub %0, %1 : !polynomial.polynomial<#ring>
```
}];
let hasCanonicalizer = 1;
}

def Polynomial_MulOp : Polynomial_BinaryOp<"mul", [Commutative]> {
Expand Down Expand Up @@ -480,6 +481,7 @@ def Polynomial_NTTOp : Polynomial_Op<"ntt", [Pure]> {
let arguments = (ins Polynomial_PolynomialType:$input);
let results = (outs RankedTensorOf<[AnyInteger]>:$output);
let assemblyFormat = "$input attr-dict `:` qualified(type($input)) `->` type($output)";
let hasCanonicalizer = 1;
let hasVerifier = 1;
}

Expand All @@ -498,6 +500,7 @@ def Polynomial_INTTOp : Polynomial_Op<"intt", [Pure]> {
let arguments = (ins RankedTensorOf<[AnyInteger]>:$input);
let results = (outs Polynomial_PolynomialType:$output);
let assemblyFormat = "$input attr-dict `:` qualified(type($input)) `->` type($output)";
let hasCanonicalizer = 1;
let hasVerifier = 1;
}

Expand Down
5 changes: 5 additions & 0 deletions mlir/lib/Dialect/Polynomial/IR/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
set(LLVM_TARGET_DEFINITIONS PolynomialCanonicalization.td)
mlir_tablegen(PolynomialCanonicalization.inc -gen-rewriters)
add_public_tablegen_target(MLIRPolynomialCanonicalizationIncGen)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you need to depend on MLIRPolynomialCanonicalizationIncGen somewhere?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1. Bazel has such a dependency.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In principle I agree (added in 0e25a32).

In practice I am baffled by how CI and my local tests are passing without this dependency added. Excuse my build system naïveté, but maybe someone could help me understand why? Without that, I can't even be 100% sure I added the dependency in the right place.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most likely it would be concurrency and luck.


add_mlir_dialect_library(MLIRPolynomialDialect
Polynomial.cpp
PolynomialAttributes.cpp
Expand All @@ -10,6 +14,7 @@ add_mlir_dialect_library(MLIRPolynomialDialect
DEPENDS
MLIRPolynomialIncGen
MLIRPolynomialAttributesIncGen
MLIRPolynomialCanonicalizationIncGen
MLIRBuiltinAttributesIncGen

LINK_LIBS PUBLIC
Expand Down
42 changes: 42 additions & 0 deletions mlir/lib/Dialect/Polynomial/IR/PolynomialCanonicalization.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//===- PolynomialCanonicalization.td - Polynomial patterns -*- tablegen -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef POLYNOMIAL_CANONICALIZATION
#define POLYNOMIAL_CANONICALIZATION

include "mlir/Dialect/Polynomial/IR/Polynomial.td"
include "mlir/Dialect/Arith/IR/ArithOps.td"
include "mlir/IR/OpBase.td"
include "mlir/IR/PatternBase.td"

// Get a -1 integer attribute of the same type as the polynomial SSA value's
// ring coefficient type.
def getMinusOne
: NativeCodeCall<
"$_builder.getIntegerAttr("
"cast<PolynomialType>($0.getType()).getRing().getCoefficientType(), -1)">;

def SubAsAdd : Pat<
(Polynomial_SubOp $f, $g),
(Polynomial_AddOp $f,
(Polynomial_MulScalarOp $g,
(Arith_ConstantOp (getMinusOne $g))))>;

def INTTAfterNTT : Pat<
(Polynomial_INTTOp (Polynomial_NTTOp $poly)),
(replaceWithValue $poly),
[]
>;

def NTTAfterINTT : Pat<
(Polynomial_NTTOp (Polynomial_INTTOp $tensor)),
(replaceWithValue $tensor),
[]
>;

#endif // POLYNOMIAL_CANONICALIZATION
25 changes: 25 additions & 0 deletions mlir/lib/Dialect/Polynomial/IR/PolynomialOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
//===----------------------------------------------------------------------===//

#include "mlir/Dialect/Polynomial/IR/PolynomialOps.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/Polynomial/IR/Polynomial.h"
#include "mlir/Dialect/Polynomial/IR/PolynomialAttributes.h"
#include "mlir/Dialect/Polynomial/IR/PolynomialTypes.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/Dialect.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/Support/LogicalResult.h"
#include "llvm/ADT/APInt.h"

Expand Down Expand Up @@ -183,3 +185,26 @@ LogicalResult INTTOp::verify() {
auto ring = getOutput().getType().getRing();
return verifyNTTOp(this->getOperation(), ring, tensorType);
}

//===----------------------------------------------------------------------===//
// TableGen'd canonicalization patterns
//===----------------------------------------------------------------------===//

namespace {
#include "PolynomialCanonicalization.inc"
} // namespace

void SubOp::getCanonicalizationPatterns(RewritePatternSet &results,
MLIRContext *context) {
results.add<SubAsAdd>(context);
}

void NTTOp::getCanonicalizationPatterns(RewritePatternSet &results,
MLIRContext *context) {
results.add<NTTAfterINTT>(context);
}

void INTTOp::getCanonicalizationPatterns(RewritePatternSet &results,
MLIRContext *context) {
results.add<INTTAfterNTT>(context);
}
45 changes: 45 additions & 0 deletions mlir/test/Dialect/Polynomial/canonicalization.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// RUN: mlir-opt -canonicalize %s | FileCheck %s
#ntt_poly = #polynomial.int_polynomial<-1 + x**8>
#ntt_ring = #polynomial.ring<coefficientType=i32, coefficientModulus=256, polynomialModulus=#ntt_poly, primitiveRoot=31>
!ntt_poly_ty = !polynomial.polynomial<ring=#ntt_ring>
!tensor_ty = tensor<8xi32, #ntt_ring>

// CHECK-LABEL: @test_canonicalize_intt_after_ntt
// CHECK: (%[[P:.*]]: [[T:.*]]) -> [[T]]
func.func @test_canonicalize_intt_after_ntt(%p0 : !ntt_poly_ty) -> !ntt_poly_ty {
// CHECK-NOT: polynomial.ntt
// CHECK-NOT: polynomial.intt
// CHECK: %[[RESULT:.+]] = polynomial.add %[[P]], %[[P]] : [[T]]
%t0 = polynomial.ntt %p0 : !ntt_poly_ty -> !tensor_ty
%p1 = polynomial.intt %t0: !tensor_ty -> !ntt_poly_ty
%p2 = polynomial.add %p1, %p1 : !ntt_poly_ty
// CHECK: return %[[RESULT]] : [[T]]
return %p2 : !ntt_poly_ty
}

// CHECK-LABEL: @test_canonicalize_ntt_after_intt
// CHECK: (%[[X:.*]]: [[T:.*]]) -> [[T]]
func.func @test_canonicalize_ntt_after_intt(%t0 : !tensor_ty) -> !tensor_ty {
// CHECK-NOT: polynomial.intt
// CHECK-NOT: polynomial.ntt
// CHECK: %[[RESULT:.+]] = arith.addi %[[X]], %[[X]] : [[T]]
%p0 = polynomial.intt %t0 : !tensor_ty -> !ntt_poly_ty
%t1 = polynomial.ntt %p0 : !ntt_poly_ty -> !tensor_ty
%t2 = arith.addi %t1, %t1 : !tensor_ty
// CHECK: return %[[RESULT]] : [[T]]
return %t2 : !tensor_ty
}

#cycl_2048 = #polynomial.int_polynomial<1 + x**1024>
#ring = #polynomial.ring<coefficientType=i32, coefficientModulus=256:i32, polynomialModulus=#cycl_2048>
!sub_ty = !polynomial.polynomial<ring=#ring>

// CHECK-LABEL: test_canonicalize_sub
// CHECK-SAME: (%[[p0:.*]]: [[T:.*]], %[[p1:.*]]: [[T]]) -> [[T]] {
func.func @test_canonicalize_sub(%poly0 : !sub_ty, %poly1 : !sub_ty) -> !sub_ty {
%0 = polynomial.sub %poly0, %poly1 : !sub_ty
// CHECK: %[[minus_one:.+]] = arith.constant -1 : i32
// CHECK: %[[p1neg:.+]] = polynomial.mul_scalar %[[p1]], %[[minus_one]]
// CHECK: [[ADD:%.+]] = polynomial.add %[[p0]], %[[p1neg]]
return %0 : !sub_ty
}
18 changes: 18 additions & 0 deletions utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6727,6 +6727,7 @@ cc_library(
":IR",
":InferTypeOpInterface",
":PolynomialAttributesIncGen",
":PolynomialCanonicalizationIncGen",
":PolynomialIncGen",
":Support",
"//llvm:Support",
Expand Down Expand Up @@ -6817,6 +6818,23 @@ gentbl_cc_library(
deps = [":PolynomialTdFiles"],
)

gentbl_cc_library(
name = "PolynomialCanonicalizationIncGen",
strip_include_prefix = "include/mlir/Dialect/Polynomial/IR",
tbl_outs = [
(
["-gen-rewriters"],
"include/mlir/Dialect/Polynomial/IR/PolynomialCanonicalization.inc",
),
],
tblgen = ":mlir-tblgen",
td_file = "lib/Dialect/Polynomial/IR/PolynomialCanonicalization.td",
deps = [
":ArithOpsTdFiles",
":PolynomialTdFiles",
],
)

td_library(
name = "SPIRVOpsTdFiles",
srcs = glob(["include/mlir/Dialect/SPIRV/IR/*.td"]),
Expand Down
Loading