-
Notifications
You must be signed in to change notification settings - Fork 128
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
Lower polygeist.subindex through memref.reinterpret_cast #147
Open
mortbopet
wants to merge
6
commits into
llvm:main
Choose a base branch
from
circt-hls:subindex_lowering2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
81c33c3
Lower polygeist.subindex through memref.reinterpret_cast
mortbopet 0769f00
Extend polygeist subindex lowering to multidim memrefs
mortbopet 8c565b2
rebase
mortbopet c070c5e
Rename pass
mortbopet 52f6117
Also handle non-rank reducing offset operations
mortbopet 524ccc8
rebase to polygeist upstream
mortbopet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,90 @@ | ||
//===- LowerPolygeistOps.cpp - Lower polygeist ops to upstream MLIR ops -*-===// | ||
// | ||
// 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 which lowers any remaining Polygeist dialect | ||
// operations (after canonicalization) to operations found in upstream MLIR | ||
// dialects. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
#include "PassDetails.h" | ||
|
||
#include "mlir/Dialect/Arithmetic/IR/Arithmetic.h" | ||
#include "mlir/Dialect/MemRef/IR/MemRef.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 inShape = srcMemRefType.getShape(); | ||
auto outShape = resMemRefType.getShape(); | ||
|
||
if (!resMemRefType.hasStaticShape()) | ||
return failure(); | ||
|
||
llvm::SmallVector<OpFoldResult> strides, sizes; | ||
int64_t innerSize = resMemRefType.getNumElements(); | ||
auto offset = rewriter.create<arith::MulIOp>( | ||
op.getLoc(), op.index(), | ||
rewriter.create<ConstantIndexOp>(op.getLoc(), innerSize)); | ||
|
||
int64_t strideAcc = 1; | ||
for (auto dim : llvm::reverse(outShape)) { | ||
sizes.insert(sizes.begin(), rewriter.getIndexAttr(dim)); | ||
strides.insert(strides.begin(), rewriter.getIndexAttr(strideAcc)); | ||
strideAcc *= dim; | ||
} | ||
|
||
rewriter.replaceOpWithNewOp<memref::ReinterpretCastOp>( | ||
op, resMemRefType, op.source(), offset.getResult(), sizes, strides); | ||
|
||
return success(); | ||
} | ||
}; | ||
|
||
struct LowerPolygeistOpsPass | ||
: public LowerPolygeistOpsBase<LowerPolygeistOpsPass> { | ||
|
||
void runOnOperation() 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, 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 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,48 @@ | ||
// 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: } | ||
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> | ||
} | ||
|
||
// ----- | ||
|
||
// CHECK-LABEL: func @main( | ||
// CHECK-SAME: %[[VAL_0:.*]]: index) -> memref<42x43x44x45xi32> { | ||
// CHECK: %[[VAL_1:.*]] = memref.alloca() : memref<41x42x43x44x45xi32> | ||
// CHECK: %[[VAL_2:.*]] = arith.constant 3575880 : index | ||
// CHECK: %[[VAL_3:.*]] = arith.muli %[[VAL_0]], %[[VAL_2]] : index | ||
// CHECK: %[[VAL_4:.*]] = memref.reinterpret_cast %[[VAL_1]] to offset: {{\[}}%[[VAL_3]]], sizes: [42, 43, 44, 45], strides: [85140, 1980, 45, 1] : memref<41x42x43x44x45xi32> to memref<42x43x44x45xi32> | ||
// CHECK: return %[[VAL_4]] : memref<42x43x44x45xi32> | ||
// CHECK: } | ||
func @main(%arg0 : index) -> memref<42x43x44x45xi32> { | ||
%0 = memref.alloca() : memref<41x42x43x44x45xi32> | ||
%1 = "polygeist.subindex"(%0, %arg0) : (memref<41x42x43x44x45xi32>, index) -> memref<42x43x44x45xi32> | ||
return %1 : memref<42x43x44x45xi32> | ||
} | ||
|
||
// ----- | ||
|
||
// CHECK-LABEL: func @main( | ||
// CHECK-SAME: %[[VAL_0:.*]]: index) -> memref<29x30xi32> { | ||
// CHECK: %[[VAL_1:.*]] = memref.alloca() : memref<30x30xi32> | ||
// CHECK: %[[VAL_2:.*]] = arith.constant 870 : index | ||
// CHECK: %[[VAL_3:.*]] = arith.muli %[[VAL_0]], %[[VAL_2]] : index | ||
// CHECK: %[[VAL_4:.*]] = memref.reinterpret_cast %[[VAL_1]] to offset: {{\[}}%[[VAL_3]]], sizes: [29, 30], strides: [30, 1] : memref<30x30xi32> to memref<29x30xi32> | ||
// CHECK: return %[[VAL_4]] : memref<29x30xi32> | ||
// CHECK: } | ||
|
||
func @main(%arg0 : index) -> memref<29x30xi32> { | ||
%0 = memref.alloca() : memref<30x30xi32> | ||
%1 = "polygeist.subindex"(%0, %arg0) : (memref<30x30xi32>, index) -> memref<29x30xi32> | ||
return %1 : memref<29x30xi32> | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you may need to extract the previous stride and then add this new value. Lest this not work if you have two such operations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should have been done in the updated commit (see the new test for reference). Let me know what you think!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps I'm not understanding, or I'm still not seeing it.
Can you add a subindex of subindex test (where both subindices are just offsets, rather than rank reducing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Additionally, could you have the memref argument be passed in as an argument?
In other words I would've expected
%[[VAL_3:.*]]
to set the new offset = oldoffset + index * dimsize, whereas it is currently just index * dimsizeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added an extra test for the case of a non-rank reducing subindex operation. Again, this initial PR only covers the case of statically sized memrefs (which takes care of the most pressing issues on my sides). subindex to subindex of statically sized memories should therefore hold transitively.
Not sure i understand what you mean by this. Could you show a snippet of the IR that you expect here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess my worry is that you don't appear to be adding to the offset (e.g. you are setting the new offset). Suppose you have two subindex's in a row, the total offset should include terms from both subindex operations. Is that the case presently?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless i am misunderstanding the reinterpret_cast operation, the offset is included in the value that the reinterpret_cast returns. So when two subindex operations are in a row, the first one will be converted to a reinterpret cast that has the same semantics as the initial subindex operation. Lowering the second is therefore independent of this.
But again an IR example of expected behaviour here might clarify any confusion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Such cases are not covered by the PR due to the dynamically sized memrefs.
Again, this PR intends to lower
polygeist.subindex
operations with static memrefs. By having this, even if it is only a subset of all possiblepolygeist.subindex
operations that are converted, we still expand the set of C programs that Polygeist can emit using upstream MLIR dialect operations. It is vital that the Polygeist dialect operations are converted for any downstream tools to be able to consume the output IR.I am not excluding that I'll in the future look more closely into how
polygeist.subindex
operations with dynamically shaped memrefs can be lowered into something meaningful, but for now, our usecase requires statically shaped memrefs and as such that is the most pressing issue to have resolved in Polygeist.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh apologies, let me make that a statically sized subindex in that case (also regardless running the pass on the above crashes, which shuoldn't happen...)
Regardless, my concern here is (perhaps because of unfamiliarity with reinterpret_cast) that one of the offsets will be lost.