Skip to content

Commit 81c33c3

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 3976ff9 commit 81c33c3

File tree

6 files changed

+113
-42
lines changed

6 files changed

+113
-42
lines changed

include/polygeist/Passes/Passes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ std::unique_ptr<Pass> createParallelLowerPass();
2424
std::unique_ptr<Pass>
2525
createConvertPolygeistToLLVMPass(const LowerToLLVMOptions &options);
2626
std::unique_ptr<Pass> createConvertPolygeistToLLVMPass();
27+
std::unique_ptr<Pass> createLowerPolygeistOpsPass();
2728

2829
} // namespace polygeist
2930
} // namespace mlir

include/polygeist/Passes/Passes.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ def RemoveTrivialUse : Pass<"trivialuse"> {
7878
let constructor = "mlir::polygeist::createRemoveTrivialUsePass()";
7979
}
8080

81+
def LowerPolygeistOps : FunctionPass<"lower-polygeist-ops"> {
82+
let summary = "Lower polygeist ops to memref operations";
83+
let constructor = "mlir::polygeist::createLowerPolygeistOpsPass()";
84+
let dependentDialects = ["::mlir::memref::MemRefDialect"];
85+
}
86+
8187
def ConvertPolygeistToLLVM : Pass<"convert-polygeist-to-llvm", "mlir::ModuleOp"> {
8288
let summary = "Convert scalar and vector operations from the Standard to the "
8389
"LLVM dialect";

lib/polygeist/Ops.cpp

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -529,48 +529,6 @@ class SubToCast final : public OpRewritePattern<SubIndexOp> {
529529
}
530530
};
531531

532-
// Simplify polygeist.subindex to memref.subview.
533-
class SubToSubView final : public OpRewritePattern<SubIndexOp> {
534-
public:
535-
using OpRewritePattern<SubIndexOp>::OpRewritePattern;
536-
537-
LogicalResult matchAndRewrite(SubIndexOp op,
538-
PatternRewriter &rewriter) const override {
539-
auto srcMemRefType = op.source().getType().cast<MemRefType>();
540-
auto resMemRefType = op.result().getType().cast<MemRefType>();
541-
auto dims = srcMemRefType.getShape().size();
542-
543-
// For now, restrict subview lowering to statically defined memref's
544-
if (!srcMemRefType.hasStaticShape() | !resMemRefType.hasStaticShape())
545-
return failure();
546-
547-
// For now, restrict to simple rank-reducing indexing
548-
if (srcMemRefType.getShape().size() <= resMemRefType.getShape().size())
549-
return failure();
550-
551-
// Build offset, sizes and strides
552-
SmallVector<OpFoldResult> sizes(dims, rewriter.getIndexAttr(0));
553-
sizes[0] = op.index();
554-
SmallVector<OpFoldResult> offsets(dims);
555-
for (auto dim : llvm::enumerate(srcMemRefType.getShape())) {
556-
if (dim.index() == 0)
557-
offsets[0] = rewriter.getIndexAttr(1);
558-
else
559-
offsets[dim.index()] = rewriter.getIndexAttr(dim.value());
560-
}
561-
SmallVector<OpFoldResult> strides(dims, rewriter.getIndexAttr(1));
562-
563-
// Generate the appropriate return type:
564-
auto subMemRefType = MemRefType::get(srcMemRefType.getShape().drop_front(),
565-
srcMemRefType.getElementType());
566-
567-
rewriter.replaceOpWithNewOp<memref::SubViewOp>(
568-
op, subMemRefType, op.source(), sizes, offsets, strides);
569-
570-
return success();
571-
}
572-
};
573-
574532
// Simplify redundant dynamic subindex patterns which tries to represent
575533
// rank-reducing indexing:
576534
// %3 = "polygeist.subindex"(%1, %arg0) : (memref<2x1000xi32>, index) ->

lib/polygeist/Passes/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ add_mlir_dialect_library(MLIRPolygeistTransforms
1313
TrivialUse.cpp
1414
ConvertPolygeistToLLVM.cpp
1515
InnerSerialization.cpp
16+
LowerPolygeistOps.cpp
1617

1718
ADDITIONAL_HEADER_DIRS
1819
${MLIR_MAIN_INCLUDE_DIR}/mlir/Dialect/Affine
Lines changed: 88 additions & 0 deletions
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
Lines changed: 17 additions & 0 deletions
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)