Skip to content

[mlir][EmitC] Expand the MemRefToEmitC pass - Lowering CopyOp #151206

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions mlir/include/mlir/Conversion/MemRefToEmitC/MemRefToEmitC.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@

constexpr const char *alignedAllocFunctionName = "aligned_alloc";
constexpr const char *mallocFunctionName = "malloc";
constexpr const char *memcpyFunctionName = "memcpy";
constexpr const char *cppStandardLibraryHeader = "cstdlib";
constexpr const char *cStandardLibraryHeader = "stdlib.h";
constexpr const char *cppStringLibraryHeader = "cstring";
constexpr const char *cStringLibraryHeader = "string.h";

namespace mlir {
class DialectRegistry;
Expand Down
91 changes: 89 additions & 2 deletions mlir/lib/Conversion/MemRefToEmitC/MemRefToEmitC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/Diagnostics.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/IR/TypeRange.h"
#include "mlir/IR/Value.h"
Expand Down Expand Up @@ -97,6 +98,31 @@ Type convertMemRefType(MemRefType opTy, const TypeConverter *typeConverter) {
return resultTy;
}

Value calculateMemrefTotalSizeBytes(Location loc, MemRefType memrefType,
OpBuilder &builder) {
assert(isMemRefTypeLegalForEmitC(memrefType) &&
"incompatible memref type for EmitC conversion");
emitc::CallOpaqueOp elementSize = builder.create<emitc::CallOpaqueOp>(
loc, emitc::SizeTType::get(builder.getContext()),
builder.getStringAttr("sizeof"), ValueRange{},
ArrayAttr::get(builder.getContext(),
{TypeAttr::get(memrefType.getElementType())}));

IndexType indexType = builder.getIndexType();
int64_t numElements = 1;
for (int64_t dimSize : memrefType.getShape()) {
numElements *= dimSize;
}
emitc::ConstantOp numElementsValue = builder.create<emitc::ConstantOp>(
loc, indexType, builder.getIndexAttr(numElements));

Type sizeTType = emitc::SizeTType::get(builder.getContext());
emitc::MulOp totalSizeBytes = builder.create<emitc::MulOp>(
loc, sizeTType, elementSize.getResult(0), numElementsValue);

return totalSizeBytes.getResult();
}

struct ConvertAlloc final : public OpConversionPattern<memref::AllocOp> {
using OpConversionPattern::OpConversionPattern;
LogicalResult
Expand Down Expand Up @@ -159,6 +185,66 @@ struct ConvertAlloc final : public OpConversionPattern<memref::AllocOp> {
}
};

struct ConvertCopy final : public OpConversionPattern<memref::CopyOp> {
using OpConversionPattern::OpConversionPattern;

LogicalResult
matchAndRewrite(memref::CopyOp copyOp, OpAdaptor operands,
ConversionPatternRewriter &rewriter) const override {
Location loc = copyOp.getLoc();
MemRefType srcMemrefType = cast<MemRefType>(copyOp.getSource().getType());
MemRefType targetMemrefType =
cast<MemRefType>(copyOp.getTarget().getType());

if (!isMemRefTypeLegalForEmitC(srcMemrefType))
return rewriter.notifyMatchFailure(
loc, "incompatible source memref type for EmitC conversion");

if (!isMemRefTypeLegalForEmitC(targetMemrefType))
return rewriter.notifyMatchFailure(
loc, "incompatible target memref type for EmitC conversion");

emitc::ConstantOp zeroIndex = rewriter.create<emitc::ConstantOp>(
loc, rewriter.getIndexType(), rewriter.getIndexAttr(0));

auto createPointerFromEmitcArray =
[loc, &rewriter, &zeroIndex](
mlir::TypedValue<emitc::ArrayType> arrayValue) -> emitc::ApplyOp {
int64_t rank = arrayValue.getType().getRank();
llvm::SmallVector<mlir::Value> indices;
for (int i = 0; i < rank; ++i) {
indices.push_back(zeroIndex);
}

emitc::SubscriptOp subPtr = rewriter.create<emitc::SubscriptOp>(
loc, arrayValue, mlir::ValueRange(indices));
emitc::ApplyOp ptr = rewriter.create<emitc::ApplyOp>(
loc, emitc::PointerType::get(arrayValue.getType().getElementType()),
rewriter.getStringAttr("&"), subPtr);

return ptr;
};

auto srcArrayValue =
cast<TypedValue<emitc::ArrayType>>(operands.getSource());
emitc::ApplyOp srcPtr = createPointerFromEmitcArray(srcArrayValue);

auto targetArrayValue =
cast<TypedValue<emitc::ArrayType>>(operands.getTarget());
emitc::ApplyOp targetPtr = createPointerFromEmitcArray(targetArrayValue);

emitc::CallOpaqueOp memCpyCall = rewriter.create<emitc::CallOpaqueOp>(
loc, TypeRange{}, "memcpy",
ValueRange{
targetPtr.getResult(), srcPtr.getResult(),
calculateMemrefTotalSizeBytes(loc, srcMemrefType, rewriter)});

rewriter.replaceOp(copyOp, memCpyCall.getResults());

return success();
}
};

struct ConvertGlobal final : public OpConversionPattern<memref::GlobalOp> {
using OpConversionPattern::OpConversionPattern;

Expand Down Expand Up @@ -320,6 +406,7 @@ void mlir::populateMemRefToEmitCTypeConversion(TypeConverter &typeConverter) {

void mlir::populateMemRefToEmitCConversionPatterns(
RewritePatternSet &patterns, const TypeConverter &converter) {
patterns.add<ConvertAlloca, ConvertAlloc, ConvertGlobal, ConvertGetGlobal,
ConvertLoad, ConvertStore>(converter, patterns.getContext());
patterns.add<ConvertAlloca, ConvertAlloc, ConvertCopy, ConvertGlobal,
ConvertGetGlobal, ConvertLoad, ConvertStore>(
converter, patterns.getContext());
}
49 changes: 34 additions & 15 deletions mlir/lib/Conversion/MemRefToEmitC/MemRefToEmitCPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "mlir/IR/Attributes.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Transforms/DialectConversion.h"
#include "llvm/ADT/StringRef.h"

namespace mlir {
#define GEN_PASS_DEF_CONVERTMEMREFTOEMITC
Expand All @@ -27,6 +28,25 @@ namespace mlir {
using namespace mlir;

namespace {

emitc::IncludeOp addStandardHeader(OpBuilder &builder, ModuleOp module,
StringRef headerName) {
StringAttr includeAttr = builder.getStringAttr(headerName);
return builder.create<emitc::IncludeOp>(
module.getLoc(), includeAttr,
/*is_standard_include=*/builder.getUnitAttr());
}

bool isExpectedStandardInclude(ConvertMemRefToEmitCOptions options,
emitc::IncludeOp includeOp) {
return ((options.lowerToCpp &&
(includeOp.getInclude() == cppStandardLibraryHeader ||
includeOp.getInclude() == cppStringLibraryHeader)) ||
(!options.lowerToCpp &&
(includeOp.getInclude() == cStandardLibraryHeader ||
includeOp.getInclude() == cStringLibraryHeader)));
}

struct ConvertMemRefToEmitCPass
: public impl::ConvertMemRefToEmitCBase<ConvertMemRefToEmitCPass> {
using Base::Base;
Expand Down Expand Up @@ -57,31 +77,30 @@ struct ConvertMemRefToEmitCPass
mlir::ModuleOp module = getOperation();
module.walk([&](mlir::emitc::CallOpaqueOp callOp) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this works as expected: the same module may contain both malloc and memcpy calls. The code seems to settle for a single call that needs an include directive, and IINM not necessarily the right one (as isExpectedIncludeOp doesn't compare includeOp against the specific callOp.getCallee()). Am I missing something? (In any case please add such a test).

if (callOp.getCallee() != alignedAllocFunctionName &&
callOp.getCallee() != mallocFunctionName) {
callOp.getCallee() != mallocFunctionName &&
callOp.getCallee() != memcpyFunctionName)
return mlir::WalkResult::advance();
}

for (auto &op : *module.getBody()) {
emitc::IncludeOp includeOp = llvm::dyn_cast<mlir::emitc::IncludeOp>(op);
if (!includeOp) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change itself is good (removing braces from single-line blocks) but should be done on a separate PR to avoid cluttering this one with unrelated modifications.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally, yes.
but im already modifying this portion of code and this would be a single line change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally, yes. but im already modifying this portion of code and this would be a single line change.

Practically too. It's not about the number of lines or their proximity to other changes. LLVM's contribution policy requires patches to be minimal. More specifically:

* not contain any unrelated changes
* be an isolated change. Independent changes should be submitted as separate patches as this makes reviewing easier.

if (!includeOp)
continue;
}

if (includeOp.getIsStandardInclude() &&
((options.lowerToCpp &&
includeOp.getInclude() == cppStandardLibraryHeader) ||
(!options.lowerToCpp &&
includeOp.getInclude() == cStandardLibraryHeader))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.
Also, shouldn't the code here also check for c/cppStringLibraryHeader?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, i should!
thanks for the pointer!

isExpectedStandardInclude(options, includeOp))
return mlir::WalkResult::interrupt();
}
}

mlir::OpBuilder builder(module.getBody(), module.getBody()->begin());
StringAttr includeAttr =
builder.getStringAttr(options.lowerToCpp ? cppStandardLibraryHeader
: cStandardLibraryHeader);
builder.create<mlir::emitc::IncludeOp>(
module.getLoc(), includeAttr,
/*is_standard_include=*/builder.getUnitAttr());
StringRef headerName;
if (callOp.getCallee() == memcpyFunctionName)
headerName =
options.lowerToCpp ? cppStringLibraryHeader : cStringLibraryHeader;
else
headerName = options.lowerToCpp ? cppStandardLibraryHeader
: cStandardLibraryHeader;

addStandardHeader(builder, module, headerName);
return mlir::WalkResult::interrupt();
});
}
Expand Down
45 changes: 45 additions & 0 deletions mlir/test/Conversion/MemRefToEmitC/memref-to-emitc-copy.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// RUN: mlir-opt -convert-memref-to-emitc="lower-to-cpp=true" %s -split-input-file | FileCheck %s --check-prefix=CPP
// RUN: mlir-opt -convert-memref-to-emitc="lower-to-cpp=false" %s -split-input-file | FileCheck %s --check-prefix=NOCPP

func.func @copying(%arg0 : memref<9x4x5x7xf32>, %arg1 : memref<9x4x5x7xf32>) {
memref.copy %arg0, %arg1 : memref<9x4x5x7xf32> to memref<9x4x5x7xf32>
return
}

// NOCPP: module {
// NOCPP-NEXT: emitc.include <"string.h">
// NOCPP-LABEL: copying
// NOCPP-SAME: %[[arg0:.*]]: memref<9x4x5x7xf32>, %[[arg1:.*]]: memref<9x4x5x7xf32>
// NOCPP-NEXT: %0 = builtin.unrealized_conversion_cast %arg1 : memref<9x4x5x7xf32> to !emitc.array<9x4x5x7xf32>
// NOCPP-NEXT: %1 = builtin.unrealized_conversion_cast %arg0 : memref<9x4x5x7xf32> to !emitc.array<9x4x5x7xf32>
// NOCPP-NEXT: %2 = "emitc.constant"() <{value = 0 : index}> : () -> index
// NOCPP-NEXT: %3 = emitc.subscript %1[%2, %2, %2, %2] : (!emitc.array<9x4x5x7xf32>, index, index, index, index) -> !emitc.lvalue<f32>
// NOCPP-NEXT: %4 = emitc.apply "&"(%3) : (!emitc.lvalue<f32>) -> !emitc.ptr<f32>
// NOCPP-NEXT: %5 = emitc.subscript %0[%2, %2, %2, %2] : (!emitc.array<9x4x5x7xf32>, index, index, index, index) -> !emitc.lvalue<f32>
// NOCPP-NEXT: %6 = emitc.apply "&"(%5) : (!emitc.lvalue<f32>) -> !emitc.ptr<f32>
// NOCPP-NEXT: %7 = emitc.call_opaque "sizeof"() {args = [f32]} : () -> !emitc.size_t
// NOCPP-NEXT: %8 = "emitc.constant"() <{value = 1260 : index}> : () -> index
// NOCPP-NEXT: %9 = emitc.mul %7, %8 : (!emitc.size_t, index) -> !emitc.size_t
// NOCPP-NEXT: emitc.call_opaque "memcpy"(%6, %4, %9) : (!emitc.ptr<f32>, !emitc.ptr<f32>, !emitc.size_t) -> ()
// NOCPP-NEXT: return
// NOCPP-NEXT: }
// NOCPP-NEXT:}

// CPP: module {
// CPP-NEXT: emitc.include <"cstring">
// CPP-LABEL: copying
// CPP-SAME: %[[arg0:.*]]: memref<9x4x5x7xf32>, %[[arg1:.*]]: memref<9x4x5x7xf32>
// CPP-NEXT: %0 = builtin.unrealized_conversion_cast %arg1 : memref<9x4x5x7xf32> to !emitc.array<9x4x5x7xf32>
// CPP-NEXT: %1 = builtin.unrealized_conversion_cast %arg0 : memref<9x4x5x7xf32> to !emitc.array<9x4x5x7xf32>
// CPP-NEXT: %2 = "emitc.constant"() <{value = 0 : index}> : () -> index
// CPP-NEXT: %3 = emitc.subscript %1[%2, %2, %2, %2] : (!emitc.array<9x4x5x7xf32>, index, index, index, index) -> !emitc.lvalue<f32>
// CPP-NEXT: %4 = emitc.apply "&"(%3) : (!emitc.lvalue<f32>) -> !emitc.ptr<f32>
// CPP-NEXT: %5 = emitc.subscript %0[%2, %2, %2, %2] : (!emitc.array<9x4x5x7xf32>, index, index, index, index) -> !emitc.lvalue<f32>
// CPP-NEXT: %6 = emitc.apply "&"(%5) : (!emitc.lvalue<f32>) -> !emitc.ptr<f32>
// CPP-NEXT: %7 = emitc.call_opaque "sizeof"() {args = [f32]} : () -> !emitc.size_t
// CPP-NEXT: %8 = "emitc.constant"() <{value = 1260 : index}> : () -> index
// CPP-NEXT: %9 = emitc.mul %7, %8 : (!emitc.size_t, index) -> !emitc.size_t
// CPP-NEXT: emitc.call_opaque "memcpy"(%6, %4, %9) : (!emitc.ptr<f32>, !emitc.ptr<f32>, !emitc.size_t) -> ()
// CPP-NEXT: return
// CPP-NEXT: }
// CPP-NEXT:}
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
// RUN: mlir-opt -convert-memref-to-emitc %s -split-input-file -verify-diagnostics

func.func @memref_op(%arg0 : memref<2x4xf32>) {
// expected-error@+1 {{failed to legalize operation 'memref.copy'}}
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'}}
Expand Down