Skip to content

Commit 3e0cdb7

Browse files
committed
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.
1 parent 4325b34 commit 3e0cdb7

File tree

7 files changed

+114
-72
lines changed

7 files changed

+114
-72
lines changed

include/polygeist/Passes/Passes.h

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ std::unique_ptr<Pass> createParallelLowerPass();
1919
std::unique_ptr<Pass>
2020
createConvertPolygeistToLLVMPass(const LowerToLLVMOptions &options);
2121
std::unique_ptr<Pass> createConvertPolygeistToLLVMPass();
22+
std::unique_ptr<Pass> createLowerPolygeistOpsPass();
2223

2324
} // namespace polygeist
2425
} // namespace mlir

include/polygeist/Passes/Passes.td

+6
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ def RemoveTrivialUse : FunctionPass<"trivialuse"> {
6161
let constructor = "mlir::polygeist::createRemoveTrivialUsePass()";
6262
}
6363

64+
def LowerPolygeistOps : FunctionPass<"lower-polygeist-ops"> {
65+
let summary = "Lower polygeist ops to memref operations";
66+
let constructor = "mlir::polygeist::createLowerPolygeistOpsPass()";
67+
let dependentDialects = ["::mlir::memref::MemRefDialect"];
68+
}
69+
6470
def ConvertPolygeistToLLVM : Pass<"convert-polygeist-to-llvm", "mlir::ModuleOp"> {
6571
let summary = "Convert scalar and vector operations from the Standard to the "
6672
"LLVM dialect";

lib/polygeist/Ops.cpp

+1-43
Original file line numberDiff line numberDiff line change
@@ -176,48 +176,6 @@ class SubToCast final : public OpRewritePattern<SubIndexOp> {
176176
}
177177
};
178178

179-
// Simplify polygeist.subindex to memref.subview.
180-
class SubToSubView final : public OpRewritePattern<SubIndexOp> {
181-
public:
182-
using OpRewritePattern<SubIndexOp>::OpRewritePattern;
183-
184-
LogicalResult matchAndRewrite(SubIndexOp op,
185-
PatternRewriter &rewriter) const override {
186-
auto srcMemRefType = op.source().getType().cast<MemRefType>();
187-
auto resMemRefType = op.result().getType().cast<MemRefType>();
188-
auto dims = srcMemRefType.getShape().size();
189-
190-
// For now, restrict subview lowering to statically defined memref's
191-
if (!srcMemRefType.hasStaticShape() | !resMemRefType.hasStaticShape())
192-
return failure();
193-
194-
// For now, restrict to simple rank-reducing indexing
195-
if (srcMemRefType.getShape().size() <= resMemRefType.getShape().size())
196-
return failure();
197-
198-
// Build offset, sizes and strides
199-
SmallVector<OpFoldResult> sizes(dims, rewriter.getIndexAttr(0));
200-
sizes[0] = op.index();
201-
SmallVector<OpFoldResult> offsets(dims);
202-
for (auto dim : llvm::enumerate(srcMemRefType.getShape())) {
203-
if (dim.index() == 0)
204-
offsets[0] = rewriter.getIndexAttr(1);
205-
else
206-
offsets[dim.index()] = rewriter.getIndexAttr(dim.value());
207-
}
208-
SmallVector<OpFoldResult> strides(dims, rewriter.getIndexAttr(1));
209-
210-
// Generate the appropriate return type:
211-
auto subMemRefType = MemRefType::get(srcMemRefType.getShape().drop_front(),
212-
srcMemRefType.getElementType());
213-
214-
rewriter.replaceOpWithNewOp<memref::SubViewOp>(
215-
op, subMemRefType, op.source(), sizes, offsets, strides);
216-
217-
return success();
218-
}
219-
};
220-
221179
// Simplify redundant dynamic subindex patterns which tries to represent
222180
// rank-reducing indexing:
223181
// %3 = "polygeist.subindex"(%1, %arg0) : (memref<2x1000xi32>, index) ->
@@ -678,7 +636,7 @@ void SubIndexOp::getCanonicalizationPatterns(OwningRewritePatternList &results,
678636
results.insert<CastOfSubIndex, SubIndex2, SubToCast, SimplifySubViewUsers,
679637
SimplifySubIndexUsers, SelectOfCast, SelectOfSubIndex,
680638
RedundantDynSubIndex>(context);
681-
// Disabled: SubToSubView
639+
// Disabled:
682640
}
683641

684642
/// Simplify pointer2memref(memref2pointer(x)) to cast(x)

lib/polygeist/Passes/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ add_mlir_dialect_library(MLIRPolygeistTransforms
1010
ParallelLower.cpp
1111
TrivialUse.cpp
1212
ConvertPolygeistToLLVM.cpp
13+
LowerPolygeistOps.cpp
1314

1415
ADDITIONAL_HEADER_DIRS
1516
${MLIR_MAIN_INCLUDE_DIR}/mlir/Dialect/Affine
+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//===- TrivialUse.cpp - Remove trivial use instruction ---------------- -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file implements a pass to lower gpu kernels in NVVM/gpu dialects into
10+
// a generic parallel for representation
11+
//===----------------------------------------------------------------------===//
12+
#include "PassDetails.h"
13+
14+
#include "mlir/Dialect/Arithmetic/IR/Arithmetic.h"
15+
#include "mlir/Dialect/MemRef/IR/MemRef.h"
16+
#include "mlir/Dialect/StandardOps/IR/Ops.h"
17+
#include "mlir/Dialect/StandardOps/Transforms/Passes.h"
18+
#include "mlir/Rewrite/FrozenRewritePatternSet.h"
19+
#include "mlir/Transforms/DialectConversion.h"
20+
#include "polygeist/Dialect.h"
21+
#include "polygeist/Ops.h"
22+
23+
using namespace mlir;
24+
using namespace polygeist;
25+
using namespace mlir::arith;
26+
27+
namespace {
28+
29+
struct SubIndexToReinterpretCast
30+
: public OpConversionPattern<polygeist::SubIndexOp> {
31+
using OpConversionPattern::OpConversionPattern;
32+
33+
LogicalResult
34+
matchAndRewrite(polygeist::SubIndexOp op, OpAdaptor adaptor,
35+
ConversionPatternRewriter &rewriter) const override {
36+
auto srcMemRefType = op.source().getType().cast<MemRefType>();
37+
auto resMemRefType = op.result().getType().cast<MemRefType>();
38+
auto shape = srcMemRefType.getShape();
39+
40+
if (!resMemRefType.hasStaticShape())
41+
return failure();
42+
43+
int64_t innerSize = resMemRefType.getNumElements();
44+
auto offset = rewriter.create<arith::MulIOp>(
45+
op.getLoc(), op.index(),
46+
rewriter.create<ConstantIndexOp>(op.getLoc(), innerSize));
47+
48+
llvm::SmallVector<OpFoldResult> sizes, strides;
49+
for (auto dim : shape.drop_front()) {
50+
sizes.push_back(rewriter.getIndexAttr(dim));
51+
strides.push_back(rewriter.getIndexAttr(1));
52+
}
53+
54+
rewriter.replaceOpWithNewOp<memref::ReinterpretCastOp>(
55+
op, resMemRefType, op.source(), offset.getResult(), sizes, strides);
56+
57+
return success();
58+
}
59+
};
60+
61+
struct LowerPolygeistOpsPass
62+
: public LowerPolygeistOpsBase<LowerPolygeistOpsPass> {
63+
64+
void runOnFunction() override {
65+
auto op = getOperation();
66+
auto ctx = op.getContext();
67+
RewritePatternSet patterns(ctx);
68+
patterns.insert<SubIndexToReinterpretCast>(ctx);
69+
70+
ConversionTarget target(*ctx);
71+
target.addIllegalDialect<polygeist::PolygeistDialect>();
72+
target.addLegalDialect<arith::ArithmeticDialect, mlir::StandardOpsDialect,
73+
memref::MemRefDialect>();
74+
75+
if (failed(applyPartialConversion(op, target, std::move(patterns))))
76+
return signalPassFailure();
77+
}
78+
};
79+
} // namespace
80+
81+
namespace mlir {
82+
namespace polygeist {
83+
std::unique_ptr<Pass> createLowerPolygeistOpsPass() {
84+
return std::make_unique<LowerPolygeistOpsPass>();
85+
}
86+
87+
} // namespace polygeist
88+
} // namespace mlir

test/polygeist-opt/canonicalization.mlir

-29
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: polygeist-opt --lower-polygeist-ops --split-input-file %s | FileCheck %s
2+
3+
// CHECK-LABEL: func @main(
4+
// CHECK-SAME: %[[VAL_0:.*]]: index) -> memref<30xi32> {
5+
// CHECK: %[[VAL_1:.*]] = memref.alloca() : memref<30x30xi32>
6+
// CHECK: %[[VAL_2:.*]] = arith.constant 30 : index
7+
// CHECK: %[[VAL_3:.*]] = arith.muli %[[VAL_0]], %[[VAL_2]] : index
8+
// CHECK: %[[VAL_4:.*]] = memref.reinterpret_cast %[[VAL_1]] to offset: {{\[}}%[[VAL_3]]], sizes: [30], strides: [1] : memref<30x30xi32> to memref<30xi32>
9+
// CHECK: return %[[VAL_4]] : memref<30xi32>
10+
// CHECK: }
11+
module {
12+
func @main(%arg0 : index) -> memref<30xi32> {
13+
%0 = memref.alloca() : memref<30x30xi32>
14+
%1 = "polygeist.subindex"(%0, %arg0) : (memref<30x30xi32>, index) -> memref<30xi32>
15+
return %1 : memref<30xi32>
16+
}
17+
}

0 commit comments

Comments
 (0)