Skip to content

[MLIR] MemRefToEmitC: Lower memref.alloca to emitc.variable #120

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions mlir/lib/Conversion/MemRefToEmitC/MemRefToEmitC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,32 @@ struct ConvertStore final : public OpConversionPattern<memref::StoreOp> {
}
};

struct ConvertAlloca final : public OpConversionPattern<memref::AllocaOp> {
using OpConversionPattern::OpConversionPattern;

LogicalResult
matchAndRewrite(memref::AllocaOp op, OpAdaptor operands,
ConversionPatternRewriter &rewriter) const override {

if (!op.getType().hasStaticShape()) {
return rewriter.notifyMatchFailure(
op.getLoc(), "cannot transform alloca with dynamic shape");
}

if (op.getAlignment().value_or(1) > 1) {
// TODO: Allow alignment if it is not more than the natural alignment
// of the C array.
return rewriter.notifyMatchFailure(
op.getLoc(), "cannot transform alloca with alignment requirement");
}

auto resultTy = getTypeConverter()->convertType(op.getType());
auto noInit = emitc::OpaqueAttr::get(getContext(), "");
rewriter.replaceOpWithNewOp<emitc::VariableOp>(op, resultTy, noInit);
return success();
}
};

struct ConvertMemRefToEmitCPass
: public impl::ConvertMemRefToEmitCBase<ConvertMemRefToEmitCPass> {
void runOnOperation() override {
Expand Down Expand Up @@ -135,8 +161,8 @@ void mlir::populateMemRefToEmitCConversionPatterns(RewritePatternSet &patterns,
converter);
populateCallOpTypeConversionPattern(patterns, converter);
populateReturnOpTypeConversionPattern(patterns, converter);
patterns.add<ConvertLoad>(converter, patterns.getContext());
patterns.add<ConvertStore>(converter, patterns.getContext());
patterns.add<ConvertLoad, ConvertStore, ConvertAlloca>(converter,
patterns.getContext());
}

std::unique_ptr<OperationPass<>> mlir::createConvertMemRefToEmitCPass() {
Expand Down
17 changes: 17 additions & 0 deletions mlir/test/Conversion/MemRefToEmitC/memref-to-emit-failed.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,20 @@ func.func @memref_op(%arg0 : memref<2x4xf32>) {
memref.copy %arg0, %arg0 : memref<2x4xf32> to memref<2x4xf32>
return
}

// -----

func.func @alloca_with_dynamic_shape() {
%0 = index.constant 1
// expected-error@+1 {{failed to legalize operation 'memref.alloca' that was explicitly marked illegal}}
%1 = memref.alloca(%0) : memref<4x?xf32>
return
}

// -----

func.func @alloca_with_alignment() {
// expected-error@+1 {{failed to legalize operation 'memref.alloca' that was explicitly marked illegal}}
%1 = memref.alloca() {alignment = 64 : i64}: memref<4xf32>
return
}
9 changes: 9 additions & 0 deletions mlir/test/Conversion/MemRefToEmitC/memref-to-emitc.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,12 @@ func.func @memref_load_store(%in: memref<4x8xf32>, %out: memref<3x5xf32>, %i: in
memref.store %0, %out[%i, %j] : memref<3x5xf32>
return
}

// -----

// CHECK-LABEL: alloca
func.func @alloca() {
// CHECK "emitc.variable"() <{value = #emitc.opaque<"">}> : () -> !emitc.array<4x8xf32>
%0 = memref.alloca() : memref<4x8xf32>
return
}