-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add boilerplate for poly.constant * add folder for poly.constant * add failing tests demonstrating folding * Add binary op folders * add a folder for FromTensorOp * add constant materializer to poly dialect
- Loading branch information
Showing
8 changed files
with
149 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include "lib/Dialect/Poly/PolyOps.h" | ||
|
||
#include "mlir/Dialect/CommonFolders.h" | ||
#include "llvm/Support/Debug.h" | ||
|
||
namespace mlir { | ||
namespace tutorial { | ||
namespace poly { | ||
|
||
OpFoldResult ConstantOp::fold(ConstantOp::FoldAdaptor adaptor) { | ||
return adaptor.getCoefficients(); | ||
} | ||
|
||
OpFoldResult AddOp::fold(AddOp::FoldAdaptor adaptor) { | ||
return constFoldBinaryOp<IntegerAttr, APInt>( | ||
adaptor.getOperands(), [&](APInt a, APInt b) { return a + b; }); | ||
} | ||
|
||
OpFoldResult SubOp::fold(SubOp::FoldAdaptor adaptor) { | ||
return constFoldBinaryOp<IntegerAttr, APInt>( | ||
adaptor.getOperands(), [&](APInt a, APInt b) { return a - b; }); | ||
} | ||
|
||
OpFoldResult MulOp::fold(MulOp::FoldAdaptor adaptor) { | ||
auto lhs = dyn_cast<DenseIntElementsAttr>(adaptor.getOperands()[0]); | ||
auto rhs = dyn_cast<DenseIntElementsAttr>(adaptor.getOperands()[1]); | ||
|
||
if (!lhs || !rhs) | ||
return nullptr; | ||
|
||
auto degree = getResult().getType().cast<PolynomialType>().getDegreeBound(); | ||
auto maxIndex = lhs.size() + rhs.size() - 1; | ||
|
||
SmallVector<APInt, 8> result; | ||
result.reserve(maxIndex); | ||
for (int i = 0; i < maxIndex; ++i) { | ||
result.push_back(APInt((*lhs.begin()).getBitWidth(), 0)); | ||
} | ||
|
||
int i = 0; | ||
for (auto lhsIt = lhs.value_begin<APInt>(); lhsIt != lhs.value_end<APInt>(); | ||
++lhsIt) { | ||
int j = 0; | ||
for (auto rhsIt = rhs.value_begin<APInt>(); rhsIt != rhs.value_end<APInt>(); | ||
++rhsIt) { | ||
// index is modulo degree because poly's semantics are defined modulo x^N = 1. | ||
result[(i + j) % degree] += *rhsIt * (*lhsIt); | ||
++j; | ||
} | ||
++i; | ||
} | ||
|
||
return DenseIntElementsAttr::get( | ||
RankedTensorType::get(static_cast<int64_t>(result.size()), | ||
IntegerType::get(getContext(), 32)), | ||
result); | ||
} | ||
|
||
OpFoldResult FromTensorOp::fold(FromTensorOp::FoldAdaptor adaptor) { | ||
// Returns null if the cast failed, which corresponds to a failed fold. | ||
return dyn_cast<DenseIntElementsAttr>(adaptor.getInput()); | ||
} | ||
|
||
} // namespace poly | ||
} // namespace tutorial | ||
} // namespace mlir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// RUN: tutorial-opt --canonicalize %s | FileCheck %s | ||
|
||
// CHECK-LABEL: @test_simple | ||
func.func @test_simple() -> !poly.poly<10> { | ||
// CHECK: poly.constant dense<[2, 4, 6]> | ||
// CHECK-NEXT: return | ||
%0 = arith.constant dense<[1, 2, 3]> : tensor<3xi32> | ||
%p0 = poly.from_tensor %0 : tensor<3xi32> -> !poly.poly<10> | ||
%2 = poly.add %p0, %p0 : (!poly.poly<10>, !poly.poly<10>) -> !poly.poly<10> | ||
%3 = poly.mul %p0, %p0 : (!poly.poly<10>, !poly.poly<10>) -> !poly.poly<10> | ||
%4 = poly.add %2, %3 : (!poly.poly<10>, !poly.poly<10>) -> !poly.poly<10> | ||
return %2 : !poly.poly<10> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// RUN: tutorial-opt -pass-pipeline="builtin.module(func.func(sccp))" %s | FileCheck %s | ||
|
||
// Note how sscp creates new constants for the computed values, | ||
// though it does not remove the dead code. | ||
|
||
// CHECK-LABEL: @test_arith_sccp | ||
// CHECK-NEXT: %[[v0:.*]] = arith.constant 63 : i32 | ||
// CHECK-NEXT: %[[v1:.*]] = arith.constant 49 : i32 | ||
// CHECK-NEXT: %[[v2:.*]] = arith.constant 14 : i32 | ||
// CHECK-NEXT: %[[v3:.*]] = arith.constant 8 : i32 | ||
// CHECK-NEXT: %[[v4:.*]] = arith.constant 7 : i32 | ||
// CHECK-NEXT: return %[[v2]] : i32 | ||
func.func @test_arith_sccp() -> i32 { | ||
%0 = arith.constant 7 : i32 | ||
%1 = arith.constant 8 : i32 | ||
%2 = arith.addi %0, %0 : i32 | ||
%3 = arith.muli %0, %0 : i32 | ||
%4 = arith.addi %2, %3 : i32 | ||
return %2 : i32 | ||
} | ||
|
||
// CHECK-LABEL: @test_poly_sccp | ||
func.func @test_poly_sccp() -> !poly.poly<10> { | ||
%0 = arith.constant dense<[1, 2, 3]> : tensor<3xi32> | ||
%p0 = poly.from_tensor %0 : tensor<3xi32> -> !poly.poly<10> | ||
// CHECK: poly.constant dense<[2, 8, 20, 24, 18]> | ||
// CHECK: poly.constant dense<[1, 4, 10, 12, 9]> | ||
// CHECK: poly.constant dense<[1, 2, 3]> | ||
// CHECK-NOT: poly.mul | ||
// CHECK-NOT: poly.add | ||
%2 = poly.mul %p0, %p0 : (!poly.poly<10>, !poly.poly<10>) -> !poly.poly<10> | ||
%3 = poly.mul %p0, %p0 : (!poly.poly<10>, !poly.poly<10>) -> !poly.poly<10> | ||
%4 = poly.add %2, %3 : (!poly.poly<10>, !poly.poly<10>) -> !poly.poly<10> | ||
return %2 : !poly.poly<10> | ||
} |