-
Notifications
You must be signed in to change notification settings - Fork 182
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
[midend/lib/Conversion/LowerLinalgToGemmini] Add pass support for gemmini to run E2E LeNet and some tests. #463
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// RUN: buddy-opt %s \ | ||
// RUN: --convert-linalg-to-gemmini | \ | ||
// RUN: FileCheck %s | ||
|
||
memref.global "private" @input : memref<1x7x7x1xi8> = dense<[[[[1],[1],[1],[1],[1],[1],[1]], | ||
[[1],[1],[1],[1],[1],[1],[1]], | ||
[[1],[1],[1],[1],[1],[1],[1]], | ||
[[1],[1],[1],[1],[1],[1],[1]], | ||
[[1],[1],[1],[1],[1],[1],[1]], | ||
[[1],[1],[1],[1],[1],[1],[1]], | ||
[[1],[1],[1],[1],[1],[1],[1]]]]> | ||
|
||
memref.global "private" @kernel : memref<1x5x5x1xi8> = dense<[[[[1], [1], [1], [1], [1]], | ||
[[1], [1], [1], [1], [1]], | ||
[[1], [1], [1], [1], [1]], | ||
[[1], [1], [1], [1], [1]], | ||
[[1], [1], [1], [1], [1]]]]> | ||
|
||
func.func @main() -> i8 { | ||
%0 = arith.constant 0 : i8 | ||
%input = memref.get_global @input : memref<1x7x7x1xi8> | ||
%kernel = memref.get_global @kernel : memref<1x5x5x1xi8> | ||
%output = memref.alloc() : memref<1x3x3x1xi8> | ||
|
||
// CHECK: gemmini.tile_conv %{{[0-9]+}} %alloc_{{[0-9]+}} %alloc_{{[0-9]+}} %alloc_{{[0-9]+}} %{{.+}} %{{.+}} : | ||
// CHECK-SAME: memref<1x7x7x1xi8> memref<25x1xi8> memref<1xi32> memref<9x1xi8> i64 i64 | ||
linalg.conv_2d_nhwc_fhwc | ||
ins(%input, %kernel : memref<1x7x7x1xi8>, memref<1x5x5x1xi8>) | ||
outs(%output : memref<1x3x3x1xi8>) | ||
gemmini.print %output : memref<1x3x3x1xi8> | ||
return %0 : i8 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// RUN: buddy-opt %s \ | ||
// RUN: --convert-linalg-to-gemmini="acc_t=f32" | \ | ||
// RUN: FileCheck %s | ||
|
||
memref.global "private" @input : memref<1x5x5x1xf32> = dense<[[[[1.],[2.],[3.],[4.],[5.]], | ||
[[6.],[7.],[8.],[9.],[10.]], | ||
[[11.],[12.],[13.],[14.],[15.]], | ||
[[16.],[17.],[18.],[19.],[20.]], | ||
[[21.],[22.],[23.],[24.],[25.]]]]> | ||
|
||
memref.global "private" @kernel : memref<1x3x3x1xf32> = dense<[[[[1.], [1.], [1.]], | ||
[[1.], [1.], [1.]], | ||
[[1.], [1.], [1.]]]]> | ||
|
||
|
||
func.func @main() -> i8 { | ||
%0 = arith.constant 0 : i8 | ||
// batchsize = 2 inputchannel = 2 | ||
%input = memref.get_global @input : memref<1x5x5x1xf32> | ||
// outputchannel = 3 | ||
%kernel = memref.get_global @kernel : memref<1x3x3x1xf32> | ||
// batchsize h w outputchannel | ||
%output = memref.alloc() : memref<1x3x3x1xf32> | ||
// CHECK: gemmini.tile_conv %{{.+}} %alloc_{{[0-9]+}} %alloc_{{[0-9]+}} %alloc_{{[0-9]+}} %{{.+}} %{{.+}} : | ||
// CHECK: memref<1x5x5x1xf32> memref<9x1xf32> memref<1xf32> memref<9x1xf32> i64 i64 | ||
linalg.conv_2d_nhwc_fhwc | ||
ins(%input, %kernel : memref<1x5x5x1xf32>, memref<1x3x3x1xf32>) | ||
outs(%output : memref<1x3x3x1xf32>) | ||
gemmini.print %output : memref<1x3x3x1xf32> | ||
return %0 : i8 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// RUN: buddy-opt %s \ | ||
// RUN: --convert-linalg-to-gemmini | \ | ||
// RUN: FileCheck %s | ||
|
||
memref.global "private" @input : memref<1x7x7x1xi8> = dense<[[[[1],[1],[1],[1],[1],[1],[1]], | ||
[[1],[1],[1],[1],[1],[1],[1]], | ||
[[1],[1],[1],[1],[1],[1],[1]], | ||
[[1],[1],[1],[1],[1],[1],[1]], | ||
[[1],[1],[1],[1],[1],[1],[1]], | ||
[[1],[1],[1],[1],[1],[1],[1]], | ||
[[1],[1],[1],[1],[1],[1],[1]]]]> | ||
|
||
memref.global "private" @kernel : memref<1x3x3x1xi8> = dense<[[[[1], [1], [1]], | ||
[[1], [1], [1]], | ||
[[1], [1], [1]]]]> | ||
|
||
func.func @main() -> i8 { | ||
%0 = arith.constant 0 : i8 | ||
%input = memref.get_global @input : memref<1x7x7x1xi8> | ||
%kernel = memref.get_global @kernel : memref<1x3x3x1xi8> | ||
%output = memref.alloc() : memref<1x5x5x1xi8> | ||
|
||
// CHECK: gemmini.tile_conv %{{[0-9]+}} %alloc_{{[0-9]+}} %alloc_{{[0-9]+}} %alloc_{{[0-9]+}} %{{.+}} %{{.+}} : | ||
// CHECK-SAME: memref<1x7x7x1xi8> memref<9x1xi8> memref<1xi32> memref<25x1xi8> i64 i64 | ||
linalg.conv_2d_nhwc_fhwc | ||
ins(%input, %kernel : memref<1x7x7x1xi8>, memref<1x3x3x1xi8>) | ||
outs(%output : memref<1x5x5x1xi8>) | ||
gemmini.print %output : memref<1x5x5x1xi8> | ||
return %0 : i8 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// RUN: buddy-opt %s \ | ||
// RUN: --convert-linalg-to-gemmini \ | ||
// --convert-linalg-to-loops \ | ||
// --lower-gemmini | \ | ||
// RUN: FileCheck %s | ||
|
||
func.func @main() -> i8 { | ||
%c0 = arith.constant 0 : i8 | ||
|
||
%scalar = arith.constant 42 : i8 | ||
// CHECK: gemmini.print_scalar %{{.*}} : i8 | ||
gemmini.print_scalar %scalar : i8 | ||
|
||
%vector = memref.alloc() : memref<4xi8> // 1D向量 | ||
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. use English. |
||
%matrix = memref.alloc() : memref<2x3xi8> // 2D矩阵 | ||
%tensor = memref.alloc() : memref<1x2x3xi8> // 3D张量 | ||
%c1 = arith.constant 1 : i8 | ||
linalg.fill ins(%c1 : i8) outs(%vector : memref<4xi8>) | ||
linalg.fill ins(%c1 : i8) outs(%matrix : memref<2x3xi8>) | ||
// CHECK: gemmini.print %{{.*}} : memref<4xi8> | ||
gemmini.print %vector : memref<4xi8> | ||
// CHECK: gemmini.print %{{.*}} : memref<2x3xi8> | ||
gemmini.print %matrix : memref<2x3xi8> | ||
// CHECK: gemmini.print %{{.*}} : memref<1x2x3xi8> | ||
gemmini.print %tensor : memref<1x2x3xi8> | ||
memref.dealloc %vector : memref<4xi8> | ||
memref.dealloc %matrix : memref<2x3xi8> | ||
memref.dealloc %tensor : memref<1x2x3xi8> | ||
|
||
return %c0 : i8 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -152,6 +152,95 @@ class PrintOpLowering : public ConversionPattern { | |
} | ||
}; | ||
|
||
class PrintScalarOpLowering : public ConversionPattern { | ||
public: | ||
explicit PrintScalarOpLowering(MLIRContext *context) | ||
: ConversionPattern(gemmini::PrintScalarOp::getOperationName(), 1, | ||
context) {} | ||
|
||
LogicalResult | ||
matchAndRewrite(Operation *op, ArrayRef<Value> operands, | ||
ConversionPatternRewriter &rewriter) const override { | ||
auto context = rewriter.getContext(); | ||
auto loc = op->getLoc(); | ||
|
||
ModuleOp parentModule = op->getParentOfType<ModuleOp>(); | ||
|
||
auto printfRef = getOrInsertPrintf(rewriter, parentModule); | ||
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. Do not use |
||
|
||
Type elementType = op->getOperand(0).getType(); | ||
Value formatSpecifierCst; | ||
|
||
if (elementType == rewriter.getF32Type() || | ||
elementType == rewriter.getF64Type()) { | ||
formatSpecifierCst = getOrCreateGlobalString( | ||
loc, rewriter, "scalar_fmt", StringRef("%f\n\0", 5), parentModule); | ||
} else if (elementType == rewriter.getI8Type() || | ||
elementType == rewriter.getI32Type()) { | ||
formatSpecifierCst = getOrCreateGlobalString( | ||
loc, rewriter, "scalar_fmt", StringRef("%d\n\0", 5), parentModule); | ||
} | ||
|
||
Value valueToPrint = op->getOperand(0); | ||
if (elementType == rewriter.getF32Type()) { | ||
valueToPrint = rewriter.create<LLVM::FPExtOp>(loc, rewriter.getF64Type(), | ||
valueToPrint); | ||
} else if (elementType == rewriter.getI8Type()) { | ||
valueToPrint = rewriter.create<LLVM::SExtOp>(loc, rewriter.getI32Type(), | ||
valueToPrint); | ||
} | ||
|
||
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. For unsupported data types, you should return |
||
rewriter.create<LLVM::CallOp>( | ||
loc, getPrintfType(context), printfRef, | ||
ArrayRef<Value>({formatSpecifierCst, valueToPrint})); | ||
|
||
rewriter.eraseOp(op); | ||
return success(); | ||
} | ||
|
||
private: | ||
static LLVM::LLVMFunctionType getPrintfType(MLIRContext *context) { | ||
auto llvmI32Ty = IntegerType::get(context, 32); | ||
auto llvmPtr = LLVM::LLVMPointerType::get(context); | ||
return LLVM::LLVMFunctionType::get(llvmI32Ty, llvmPtr, true); | ||
} | ||
|
||
static FlatSymbolRefAttr getOrInsertPrintf(PatternRewriter &rewriter, | ||
ModuleOp module) { | ||
auto *context = module.getContext(); | ||
if (module.lookupSymbol<LLVM::LLVMFuncOp>("printf")) | ||
return SymbolRefAttr::get(context, "printf"); | ||
|
||
PatternRewriter::InsertionGuard insertGuard(rewriter); | ||
rewriter.setInsertionPointToStart(module.getBody()); | ||
rewriter.create<LLVM::LLVMFuncOp>(module.getLoc(), "printf", | ||
getPrintfType(context)); | ||
return SymbolRefAttr::get(context, "printf"); | ||
} | ||
|
||
static Value getOrCreateGlobalString(Location loc, OpBuilder &builder, | ||
StringRef name, StringRef value, | ||
ModuleOp module) { | ||
LLVM::GlobalOp global; | ||
if (!(global = module.lookupSymbol<LLVM::GlobalOp>(name))) { | ||
OpBuilder::InsertionGuard insertGuard(builder); | ||
builder.setInsertionPointToStart(module.getBody()); | ||
auto type = LLVM::LLVMArrayType::get( | ||
IntegerType::get(builder.getContext(), 8), value.size()); | ||
global = builder.create<LLVM::GlobalOp>(loc, type, true, | ||
LLVM::Linkage::Internal, name, | ||
builder.getStringAttr(value), 0); | ||
} | ||
|
||
Value globalPtr = builder.create<LLVM::AddressOfOp>(loc, global); | ||
Value cst0 = builder.create<LLVM::ConstantOp>(loc, builder.getI64Type(), | ||
builder.getIndexAttr(0)); | ||
return builder.create<LLVM::GEPOp>( | ||
loc, LLVM::LLVMPointerType::get(builder.getContext()), global.getType(), | ||
globalPtr, ArrayRef<Value>({cst0, cst0})); | ||
} | ||
}; | ||
|
||
namespace { | ||
class LowerGemminiToLLVMPass | ||
: public PassWrapper<LowerGemminiToLLVMPass, OperationPass<ModuleOp>> { | ||
|
@@ -222,6 +311,7 @@ void LowerGemminiToLLVMPass::runOnOperation() { | |
cf::populateControlFlowToLLVMConversionPatterns(converter, patterns); | ||
populateFuncToLLVMConversionPatterns(converter, patterns); | ||
patterns.add<PrintOpLowering>(&getContext()); | ||
patterns.add<PrintScalarOpLowering>(&getContext()); | ||
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. ... context = &getContext(); |
||
if (failed(applyPartialConversion(module, target, std::move(patterns)))) | ||
signalPassFailure(); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you don't need
check-same
, and feel that the content ofcheck-smae
is not important. The same is true for the following example.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gemmini.tile_conv
({{.*}}
({{.*}}
..., Maybe better since you're not capturing the variable and then using it.