-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lower polygeist.subindex through memref.reinterpret_cast
This should be a (hopefully) foolproof method of performing indexing into a memref. A reintrepret_cast is inserted with a dynamic index calculated from the subindex index operand + the product of the sizes of the target type. This has been added as a separate conversion pass instead of through the canonicalization drivers. When added as a canonicalization, the conversion may preemptively apply, resulting in sub-par IR. Nevertheless, i think it has its merits to have a polygeist op lowering pass which can be used as a fallback to convert the dialect operations, if canonicalization fails. For now, just added support for statically shaped memrefs (enough to fix the regression on my side) but should be possible for dynamically shaped as well.
- Loading branch information
Showing
7 changed files
with
114 additions
and
72 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
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,88 @@ | ||
//===- TrivialUse.cpp - Remove trivial use instruction ---------------- -*-===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file implements a pass to lower gpu kernels in NVVM/gpu dialects into | ||
// a generic parallel for representation | ||
//===----------------------------------------------------------------------===// | ||
#include "PassDetails.h" | ||
|
||
#include "mlir/Dialect/Arithmetic/IR/Arithmetic.h" | ||
#include "mlir/Dialect/MemRef/IR/MemRef.h" | ||
#include "mlir/Dialect/StandardOps/IR/Ops.h" | ||
#include "mlir/Dialect/StandardOps/Transforms/Passes.h" | ||
#include "mlir/Rewrite/FrozenRewritePatternSet.h" | ||
#include "mlir/Transforms/DialectConversion.h" | ||
#include "polygeist/Dialect.h" | ||
#include "polygeist/Ops.h" | ||
|
||
using namespace mlir; | ||
using namespace polygeist; | ||
using namespace mlir::arith; | ||
|
||
namespace { | ||
|
||
struct SubIndexToReinterpretCast | ||
: public OpConversionPattern<polygeist::SubIndexOp> { | ||
using OpConversionPattern::OpConversionPattern; | ||
|
||
LogicalResult | ||
matchAndRewrite(polygeist::SubIndexOp op, OpAdaptor adaptor, | ||
ConversionPatternRewriter &rewriter) const override { | ||
auto srcMemRefType = op.source().getType().cast<MemRefType>(); | ||
auto resMemRefType = op.result().getType().cast<MemRefType>(); | ||
auto shape = srcMemRefType.getShape(); | ||
|
||
if (!resMemRefType.hasStaticShape()) | ||
return failure(); | ||
|
||
int64_t innerSize = resMemRefType.getNumElements(); | ||
auto offset = rewriter.create<arith::MulIOp>( | ||
op.getLoc(), op.index(), | ||
rewriter.create<ConstantIndexOp>(op.getLoc(), innerSize)); | ||
|
||
llvm::SmallVector<OpFoldResult> sizes, strides; | ||
for (auto dim : shape.drop_front()) { | ||
sizes.push_back(rewriter.getIndexAttr(dim)); | ||
strides.push_back(rewriter.getIndexAttr(1)); | ||
} | ||
|
||
rewriter.replaceOpWithNewOp<memref::ReinterpretCastOp>( | ||
op, resMemRefType, op.source(), offset.getResult(), sizes, strides); | ||
|
||
return success(); | ||
} | ||
}; | ||
|
||
struct LowerPolygeistOpsPass | ||
: public LowerPolygeistOpsBase<LowerPolygeistOpsPass> { | ||
|
||
void runOnFunction() override { | ||
auto op = getOperation(); | ||
auto ctx = op.getContext(); | ||
RewritePatternSet patterns(ctx); | ||
patterns.insert<SubIndexToReinterpretCast>(ctx); | ||
|
||
ConversionTarget target(*ctx); | ||
target.addIllegalDialect<polygeist::PolygeistDialect>(); | ||
target.addLegalDialect<arith::ArithmeticDialect, mlir::StandardOpsDialect, | ||
memref::MemRefDialect>(); | ||
|
||
if (failed(applyPartialConversion(op, target, std::move(patterns)))) | ||
return signalPassFailure(); | ||
} | ||
}; | ||
} // namespace | ||
|
||
namespace mlir { | ||
namespace polygeist { | ||
std::unique_ptr<Pass> createLowerPolygeistOpsPass() { | ||
return std::make_unique<LowerPolygeistOpsPass>(); | ||
} | ||
|
||
} // namespace polygeist | ||
} // namespace mlir |
This file was deleted.
Oops, something went wrong.
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,17 @@ | ||
// RUN: polygeist-opt --lower-polygeist-ops --split-input-file %s | FileCheck %s | ||
|
||
// CHECK-LABEL: func @main( | ||
// CHECK-SAME: %[[VAL_0:.*]]: index) -> memref<30xi32> { | ||
// CHECK: %[[VAL_1:.*]] = memref.alloca() : memref<30x30xi32> | ||
// CHECK: %[[VAL_2:.*]] = arith.constant 30 : index | ||
// CHECK: %[[VAL_3:.*]] = arith.muli %[[VAL_0]], %[[VAL_2]] : index | ||
// CHECK: %[[VAL_4:.*]] = memref.reinterpret_cast %[[VAL_1]] to offset: {{\[}}%[[VAL_3]]], sizes: [30], strides: [1] : memref<30x30xi32> to memref<30xi32> | ||
// CHECK: return %[[VAL_4]] : memref<30xi32> | ||
// CHECK: } | ||
module { | ||
func @main(%arg0 : index) -> memref<30xi32> { | ||
%0 = memref.alloca() : memref<30x30xi32> | ||
%1 = "polygeist.subindex"(%0, %arg0) : (memref<30x30xi32>, index) -> memref<30xi32> | ||
return %1 : memref<30xi32> | ||
} | ||
} |