-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[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
Changes from 8 commits
5a54bdf
b5b7637
c17cf73
a7a2d01
12e967e
5870e6b
3663062
ec252fc
de3c63d
ee8ab84
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,8 @@ | |
| #include "mlir/IR/Attributes.h" | ||
| #include "mlir/Pass/Pass.h" | ||
| #include "mlir/Transforms/DialectConversion.h" | ||
| #include "llvm/ADT/SmallSet.h" | ||
| #include "llvm/ADT/StringRef.h" | ||
|
|
||
| namespace mlir { | ||
| #define GEN_PASS_DEF_CONVERTMEMREFTOEMITC | ||
|
|
@@ -27,6 +29,15 @@ 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()); | ||
| } | ||
|
|
||
| struct ConvertMemRefToEmitCPass | ||
| : public impl::ConvertMemRefToEmitCBase<ConvertMemRefToEmitCPass> { | ||
| using Base::Base; | ||
|
|
@@ -55,34 +66,29 @@ struct ConvertMemRefToEmitCPass | |
| return signalPassFailure(); | ||
|
|
||
| mlir::ModuleOp module = getOperation(); | ||
| llvm::SmallSet<StringRef, 4> existingHeaders; | ||
| mlir::OpBuilder builder(module.getBody(), module.getBody()->begin()); | ||
| module.walk([&](mlir::emitc::IncludeOp includeOp) { | ||
| if (includeOp.getIsStandardInclude()) | ||
| existingHeaders.insert(includeOp.getInclude()); | ||
| }); | ||
|
|
||
| module.walk([&](mlir::emitc::CallOpaqueOp callOp) { | ||
| if (callOp.getCallee() != alignedAllocFunctionName && | ||
| callOp.getCallee() != mallocFunctionName) { | ||
| StringRef expectedHeader; | ||
| if (callOp.getCallee() == alignedAllocFunctionName || | ||
| callOp.getCallee() == mallocFunctionName) | ||
| expectedHeader = options.lowerToCpp ? cppStandardLibraryHeader | ||
| : cStandardLibraryHeader; | ||
| else if (callOp.getCallee() == memcpyFunctionName) | ||
| expectedHeader = | ||
| options.lowerToCpp ? cppStringLibraryHeader : cStringLibraryHeader; | ||
| else | ||
| return mlir::WalkResult::advance(); | ||
| if (!existingHeaders.contains(expectedHeader)) { | ||
| addStandardHeader(builder, module, expectedHeader); | ||
| existingHeaders.insert(expectedHeader); | ||
| } | ||
|
|
||
| for (auto &op : *module.getBody()) { | ||
| emitc::IncludeOp includeOp = llvm::dyn_cast<mlir::emitc::IncludeOp>(op); | ||
| if (!includeOp) { | ||
|
||
| continue; | ||
| } | ||
| if (includeOp.getIsStandardInclude() && | ||
| ((options.lowerToCpp && | ||
| includeOp.getInclude() == cppStandardLibraryHeader) || | ||
| (!options.lowerToCpp && | ||
| includeOp.getInclude() == cStandardLibraryHeader))) { | ||
|
||
| 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()); | ||
| return mlir::WalkResult::interrupt(); | ||
| return mlir::WalkResult::advance(); | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // 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 @alloc_copy(%arg0: memref<999xi32>) { | ||
| %alloc = memref.alloc() : memref<999xi32> | ||
| memref.copy %arg0, %alloc : memref<999xi32> to memref<999xi32> | ||
| %alloc_1 = memref.alloc() : memref<999xi32> | ||
| memref.copy %arg0, %alloc_1 : memref<999xi32> to memref<999xi32> | ||
| return | ||
| } | ||
|
|
||
| // CHECK: module { | ||
| // NOCPP: emitc.include <"stdlib.h"> | ||
| // NOCPP-NEXT: emitc.include <"string.h"> | ||
|
|
||
| // CPP: emitc.include <"cstdlib"> | ||
| // CPP-NEXT: emitc.include <"cstring"> | ||
|
|
||
| // CHECK-LABEL: alloc_copy | ||
| // CHECK-SAME: %[[arg0:.*]]: memref<999xi32> | ||
| // CHECK-NEXT: builtin.unrealized_conversion_cast %arg0 : memref<999xi32> to !emitc.array<999xi32> | ||
| // CHECK-NEXT: emitc.call_opaque "sizeof"() {args = [i32]} : () -> !emitc.size_t | ||
| // CHECK-NEXT: "emitc.constant"() <{value = 999 : index}> : () -> index | ||
| // CHECK-NEXT: emitc.mul %1, %2 : (!emitc.size_t, index) -> !emitc.size_t | ||
| // CHECK-NEXT: emitc.call_opaque "malloc"(%3) : (!emitc.size_t) -> !emitc.ptr<!emitc.opaque<"void">> | ||
| // CHECK-NEXT: emitc.cast %4 : !emitc.ptr<!emitc.opaque<"void">> to !emitc.ptr<i32> | ||
| // CHECK-NEXT: builtin.unrealized_conversion_cast %5 : !emitc.ptr<i32> to !emitc.array<999xi32> | ||
| // CHECK-NEXT: "emitc.constant"() <{value = 0 : index}> : () -> index | ||
| // CHECK-NEXT: emitc.subscript %0[%7] : (!emitc.array<999xi32>, index) -> !emitc.lvalue<i32> | ||
| // CHECK-NEXT: emitc.apply "&"(%8) : (!emitc.lvalue<i32>) -> !emitc.ptr<i32> | ||
| // CHECK-NEXT: emitc.call_opaque "sizeof"() {args = [i32]} : () -> !emitc.size_t | ||
| // CHECK-NEXT: "emitc.constant"() <{value = 999 : index}> : () -> index | ||
| // CHECK-NEXT: emitc.mul %12, %13 : (!emitc.size_t, index) -> !emitc.size_t | ||
| // CHECK-NEXT: emitc.call_opaque "memcpy"(%11, %9, %14) : (!emitc.ptr<i32>, !emitc.ptr<i32>, !emitc.size_t) -> () | ||
| // CHECK-NEXT: emitc.call_opaque "sizeof"() {args = [i32]} : () -> !emitc.size_t | ||
| // CHECK-NEXT: "emitc.constant"() <{value = 999 : index}> : () -> index | ||
| // CHECK-NEXT: emitc.mul %15, %16 : (!emitc.size_t, index) -> !emitc.size_t | ||
| // CHECK-NEXT: emitc.call_opaque "malloc"(%17) : (!emitc.size_t) -> !emitc.ptr<!emitc.opaque<"void">> | ||
| // CHECK-NEXT: emitc.cast %18 : !emitc.ptr<!emitc.opaque<"void">> to !emitc.ptr<i32> | ||
| // CHECK-NEXT: builtin.unrealized_conversion_cast %19 : !emitc.ptr<i32> to !emitc.array<999xi32> | ||
| // CHECK-NEXT: "emitc.constant"() <{value = 0 : index}> : () -> index | ||
| // CHECK-NEXT: emitc.subscript %0[%21] : (!emitc.array<999xi32>, index) -> !emitc.lvalue<i32> | ||
| // CHECK-NEXT: emitc.apply "&"(%22) : (!emitc.lvalue<i32>) -> !emitc.ptr<i32> | ||
| // CHECK-NEXT: emitc.subscript %20[%21] : (!emitc.array<999xi32>, index) -> !emitc.lvalue<i32> | ||
| // CHECK-NEXT: emitc.apply "&"(%24) : (!emitc.lvalue<i32>) -> !emitc.ptr<i32> | ||
| // CHECK-NEXT: emitc.call_opaque "sizeof"() {args = [i32]} : () -> !emitc.size_t | ||
| // CHECK-NEXT: "emitc.constant"() <{value = 999 : index}> : () -> index | ||
| // CHECK-NEXT: emitc.mul %26, %27 : (!emitc.size_t, index) -> !emitc.size_t | ||
| // CHECK-NEXT: emitc.call_opaque "memcpy"(%25, %23, %28) : (!emitc.ptr<i32>, !emitc.ptr<i32>, !emitc.size_t) -> () | ||
| // CHECK-NEXT: return |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,30 @@ | ||||||||||||||
| // 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 | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // CHECK: module { | ||||||||||||||
| // NOCPP: emitc.include <"string.h"> | ||||||||||||||
|
|
||||||||||||||
| // CPP: emitc.include <"cstring"> | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think this a more readable formatting, since its makes it a bit obvious that these are variants of the same line. This is a rather minor nit though, so up to you.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i've addressed in new changes. |
||||||||||||||
|
|
||||||||||||||
| // CHECK-LABEL: copying | ||||||||||||||
| // CHECK-SAME: %[[arg0:.*]]: memref<9x4x5x7xf32>, %[[arg1:.*]]: memref<9x4x5x7xf32> | ||||||||||||||
| // CHECK-NEXT: %0 = builtin.unrealized_conversion_cast %arg1 : memref<9x4x5x7xf32> to !emitc.array<9x4x5x7xf32> | ||||||||||||||
| // CHECK-NEXT: %1 = builtin.unrealized_conversion_cast %arg0 : memref<9x4x5x7xf32> to !emitc.array<9x4x5x7xf32> | ||||||||||||||
| // CHECK-NEXT: %2 = "emitc.constant"() <{value = 0 : index}> : () -> index | ||||||||||||||
| // CHECK-NEXT: %3 = emitc.subscript %1[%2, %2, %2, %2] : (!emitc.array<9x4x5x7xf32>, index, index, index, index) -> !emitc.lvalue<f32> | ||||||||||||||
| // CHECK-NEXT: %4 = emitc.apply "&"(%3) : (!emitc.lvalue<f32>) -> !emitc.ptr<f32> | ||||||||||||||
| // CHECK-NEXT: %5 = emitc.subscript %0[%2, %2, %2, %2] : (!emitc.array<9x4x5x7xf32>, index, index, index, index) -> !emitc.lvalue<f32> | ||||||||||||||
| // CHECK-NEXT: %6 = emitc.apply "&"(%5) : (!emitc.lvalue<f32>) -> !emitc.ptr<f32> | ||||||||||||||
| // CHECK-NEXT: %7 = emitc.call_opaque "sizeof"() {args = [f32]} : () -> !emitc.size_t | ||||||||||||||
| // CHECK-NEXT: %8 = "emitc.constant"() <{value = 1260 : index}> : () -> index | ||||||||||||||
| // CHECK-NEXT: %9 = emitc.mul %7, %8 : (!emitc.size_t, index) -> !emitc.size_t | ||||||||||||||
| // CHECK-NEXT: emitc.call_opaque "memcpy"(%6, %4, %9) : (!emitc.ptr<f32>, !emitc.ptr<f32>, !emitc.size_t) -> () | ||||||||||||||
| // CHECK-NEXT: return | ||||||||||||||
| // CHECK-NEXT: } | ||||||||||||||
| // CHECK-NEXT:} | ||||||||||||||
|
|
||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.