-
Notifications
You must be signed in to change notification settings - Fork 78
[LDE: Project] 'Codegen-only compilation pipeline for LA operations' #935
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
WangYuyao
wants to merge
34
commits into
daphne-project:main
Choose a base branch
from
WangYuyao:LDEProject
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
04fc40e
build daphne-opt
WangYuyao 03cb04a
add SliceRowOpLowering
WangYuyao 537f655
add SliceColOpLowering
WangYuyao 2621af4
exclude linalg.generic op, add memreftollvm pass
WangYuyao 3a9eafe
Revert "build daphne-opt"
WangYuyao 55d8a76
remove improper test case
WangYuyao 204a511
combine slice row and column
WangYuyao f218f30
add ExtractOpLowering
WangYuyao 99125d4
add unfinished ExtractOpLowering
WangYuyao 77938fb
Merge remote-tracking branch 'upstream/main' into LDEProject
WangYuyao f234567
add EwUnaryOpsLowering for Sparse Matrix
WangYuyao e68919a
comment out extract op lowering
WangYuyao 7cb9c3f
add untested kernel for converting memref to CSR
WangYuyao edc5546
add EwUnaryMat for CSR Matrix
WangYuyao 196b8e1
add a new constructor for CSRMatrix
WangYuyao 6212dc6
adapt MemRefToCSR kernel with new constructor
WangYuyao 5679eea
add CSR support to EwBinaryObjSca
WangYuyao a2f9c90
add EwOpsLowering for Op between CSR and Dense
WangYuyao 90dabd6
add EwBinaryMat kernel for Dense <- (CSR + Dense)
WangYuyao b171392
update kernels.json
WangYuyao 960a4c2
add CSR +/* CSR
WangYuyao 4752fdb
add EwBinaryMat CSR <- (CSR, CSR)
WangYuyao c18516c
fix bug
WangYuyao a8c3268
fix bug
WangYuyao 4a8fd6a
add Matmul Lowering for (CSR, Dense)
WangYuyao a3bbd9e
add Matmul for (CSR, CSR), correct EwOpsLowering
WangYuyao d4a7cdb
add a script level test case for gemm codegen
WangYuyao e2476a2
clean comment-outs
WangYuyao d5361d4
add comments and fix a bug
WangYuyao 1cbc9f8
edit the comments
WangYuyao ee3959a
Delete src/compiler/lowering/ExtractOpLowering.cpp
WangYuyao f743c81
remove ExtractOp lowering related
WangYuyao df2796d
optimize CSR Matrix Index in MatMulOpLowering
WangYuyao 23921b8
add tests
WangYuyao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # bench.daph | ||
| size=$size; | ||
| sparsity=$sparsity; | ||
|
|
||
| alpha = 2; | ||
| beta = 3; | ||
| A = rand(size, size, 1.0, 1.0, sparsity, -1); | ||
| B = rand(size, size, 1.0, 1.0, sparsity, -1); | ||
| C = rand(size, size, 1.0, 1.0, sparsity, -1); | ||
| start = now(); | ||
| D = beta * C + alpha * A @ B ; | ||
| end = now(); | ||
| print((end-start) / 1000000000.0); | ||
| x = aggMax(D); | ||
| print(x); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| /* | ||
| * Copyright 2024 The DAPHNE Consortium | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include "compiler/utils/LoweringUtils.h" | ||
| #include "ir/daphneir/Daphne.h" | ||
| #include "ir/daphneir/Passes.h" | ||
|
|
||
| #include "mlir/Conversion/AffineToStandard/AffineToStandard.h" | ||
| #include "mlir/Conversion/ArithToLLVM/ArithToLLVM.h" | ||
| #include "mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h" | ||
| #include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVM.h" | ||
| #include "mlir/Conversion/LLVMCommon/LoweringOptions.h" | ||
| #include "mlir/Conversion/LLVMCommon/TypeConverter.h" | ||
| #include "mlir/Dialect/Arith/IR/Arith.h" | ||
| #include "mlir/Dialect/Func/Transforms/FuncConversions.h" | ||
| #include "mlir/Dialect/LLVMIR/LLVMDialect.h" | ||
| #include "mlir/Dialect/Linalg/IR/Linalg.h" | ||
| #include "mlir/Dialect/MemRef/IR/MemRef.h" | ||
| #include "mlir/Dialect/Utils/StructuredOpsUtils.h" | ||
| #include "mlir/Dialect/Vector/IR/VectorOps.h" | ||
| #include "mlir/IR/AffineExpr.h" | ||
| #include "mlir/IR/AffineMap.h" | ||
| #include "mlir/IR/Builders.h" | ||
| #include "mlir/IR/BuiltinAttributes.h" | ||
| #include "mlir/IR/BuiltinDialect.h" | ||
| #include "mlir/IR/BuiltinTypes.h" | ||
| #include "mlir/IR/Location.h" | ||
| #include "mlir/IR/MLIRContext.h" | ||
| #include "mlir/IR/OpDefinition.h" | ||
| #include "mlir/IR/Operation.h" | ||
| #include "mlir/IR/PatternMatch.h" | ||
| #include "mlir/IR/UseDefLists.h" | ||
| #include "mlir/IR/Value.h" | ||
| #include "mlir/IR/ValueRange.h" | ||
| #include "mlir/Pass/Pass.h" | ||
| #include "mlir/Support/LLVM.h" | ||
| #include "mlir/Support/LogicalResult.h" | ||
| #include "mlir/Transforms/DialectConversion.h" | ||
|
|
||
|
|
||
| using namespace mlir; | ||
| using namespace std; | ||
|
|
||
| //template<class SliceOp> | ||
| class SliceColOpLowering : public OpConversionPattern<daphne::SliceColOp> { | ||
| public: | ||
| using OpConversionPattern::OpConversionPattern; | ||
|
|
||
| explicit SliceColOpLowering(TypeConverter &typeConverter, MLIRContext *ctx) | ||
| : mlir::OpConversionPattern<daphne::SliceColOp>(typeConverter, ctx, PatternBenefit(1)) { | ||
| this->setDebugName("SliceColOpLowering"); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Replaces a Transpose operation with a Linalg TransposeOp if possible. | ||
| * | ||
| * @return mlir::success if Transpose has been replaced, else mlir::failure. | ||
| */ | ||
| LogicalResult matchAndRewrite(daphne::SliceColOp op, OpAdaptor adaptor, | ||
| ConversionPatternRewriter &rewriter) const override { | ||
|
|
||
| daphne::MatrixType matrixType = adaptor.getSource().getType().dyn_cast<daphne::MatrixType>(); | ||
| if (!matrixType) { | ||
| return failure(); | ||
| } | ||
|
|
||
| Location loc = op->getLoc(); | ||
|
|
||
| Type matrixElementType = matrixType.getElementType(); | ||
| ssize_t numRows = matrixType.getNumRows(); | ||
| ssize_t numCols = matrixType.getNumCols(); | ||
|
|
||
| if (numRows < 0 || numCols < 0) { | ||
| return rewriter.notifyMatchFailure( | ||
| op, "sliceColOp codegen currently only works with matrix dimensions that are known at compile time"); | ||
| } | ||
|
|
||
| Value argMemref = rewriter.create<daphne::ConvertDenseMatrixToMemRef>( | ||
| loc, MemRefType::get({numRows, numCols}, matrixElementType), adaptor.getSource()); | ||
|
|
||
| auto lowerIncl = adaptor.getLowerIncl().getDefiningOp<daphne::ConstantOp>().getValue().dyn_cast<mlir::IntegerAttr>().getSInt(); | ||
| auto upperExcl = adaptor.getUpperExcl().getDefiningOp<daphne::ConstantOp>().getValue().dyn_cast<mlir::IntegerAttr>().getSInt(); | ||
|
|
||
| Value resMemref = rewriter.create<memref::AllocOp>(loc, MemRefType::get({numRows, (upperExcl-lowerIncl)}, matrixElementType)); | ||
|
|
||
| DenseI64ArrayAttr offset = rewriter.getDenseI64ArrayAttr({0, lowerIncl}); | ||
| DenseI64ArrayAttr sizes = rewriter.getDenseI64ArrayAttr({numRows, (upperExcl-lowerIncl)}); | ||
| DenseI64ArrayAttr strides = rewriter.getDenseI64ArrayAttr({1, 1}); | ||
|
|
||
| Value selMemref = rewriter.create<memref::SubViewOp>(loc, argMemref, offset, sizes, strides); | ||
|
|
||
| SmallVector<AffineMap, 2> indexMaps{AffineMap::getMultiDimIdentityMap(2, rewriter.getContext()), | ||
| AffineMap::getMultiDimIdentityMap(2, rewriter.getContext())}; | ||
|
|
||
| SmallVector<utils::IteratorType, 2> iterTypes{utils::IteratorType::parallel, | ||
| utils::IteratorType::parallel}; | ||
|
|
||
| rewriter.create<linalg::GenericOp>(loc, TypeRange{}, ValueRange{selMemref}, ValueRange{resMemref}, | ||
| indexMaps, iterTypes, | ||
| [&](OpBuilder &OpBuilderNested, Location locNested, ValueRange arg) { | ||
| OpBuilderNested.create<linalg::YieldOp>(locNested, arg[0]); | ||
| }); | ||
|
|
||
| Value resDenseMatrix = convertMemRefToDenseMatrix(loc, rewriter, resMemref, op.getType()); | ||
|
|
||
| rewriter.replaceOp(op, resDenseMatrix); | ||
|
|
||
| return success(); | ||
| } | ||
| }; | ||
|
|
||
| namespace { | ||
| /** | ||
| * @brief Lowers the daphne::Transpose operator to a Linalg TransposeOp. | ||
| * | ||
| * This rewrite may enable loop fusion on the affine loops TransposeOp is | ||
| * lowered to by running the loop fusion pass. | ||
| */ | ||
| struct SliceColLoweringPass : public mlir::PassWrapper<SliceColLoweringPass, mlir::OperationPass<mlir::ModuleOp>> { | ||
| explicit SliceColLoweringPass() {} | ||
|
|
||
| StringRef getArgument() const final { return "lower-slice-col"; } | ||
| StringRef getDescription() const final { return "Lowers SliceCol operators to a Memref SubViewOp."; } | ||
|
|
||
| void getDependentDialects(mlir::DialectRegistry ®istry) const override { | ||
| registry.insert<mlir::LLVM::LLVMDialect, mlir::linalg::LinalgDialect, mlir::memref::MemRefDialect>(); | ||
| } | ||
| void runOnOperation() final; | ||
| }; | ||
| } // end anonymous namespace | ||
|
|
||
| void SliceColLoweringPass::runOnOperation() { | ||
| mlir::ConversionTarget target(getContext()); | ||
| mlir::RewritePatternSet patterns(&getContext()); | ||
| LowerToLLVMOptions llvmOptions(&getContext()); | ||
| LLVMTypeConverter typeConverter(&getContext(), llvmOptions); | ||
|
|
||
| typeConverter.addConversion(convertInteger); | ||
| typeConverter.addConversion(convertFloat); | ||
| typeConverter.addConversion([](Type type) { return type; }); | ||
| typeConverter.addArgumentMaterialization(materializeCastFromIllegal); | ||
| typeConverter.addSourceMaterialization(materializeCastToIllegal); | ||
| typeConverter.addTargetMaterialization(materializeCastFromIllegal); | ||
|
|
||
| target.addLegalDialect<BuiltinDialect, daphne::DaphneDialect, linalg::LinalgDialect, memref::MemRefDialect>(); | ||
|
|
||
| target.addDynamicallyLegalOp<daphne::SliceColOp>([](Operation *op) { | ||
| Type operand = op->getOperand(0).getType(); | ||
| daphne::MatrixType matType = operand.dyn_cast<daphne::MatrixType>(); | ||
| if (matType && matType.getRepresentation() == daphne::MatrixRepresentation::Dense) { | ||
| return false; | ||
| } | ||
| return true; | ||
| }); | ||
|
|
||
| patterns.insert<SliceColOpLowering>(typeConverter, &getContext()); | ||
| auto module = getOperation(); | ||
| if (failed(applyPartialConversion(module, target, std::move(patterns)))) { | ||
| signalPassFailure(); | ||
| } | ||
| } | ||
|
|
||
| std::unique_ptr<mlir::Pass> daphne::createSliceColOpLoweringPass() { | ||
| return std::make_unique<SliceColLoweringPass>(); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This shouldn't be needed, as we want to create a view of a matrix we don't want to copy the data. The
memref::SubViewOpshould be enough. You'll need to properly lower it afterwards by adding the following passes to the pipeline:mlir::memref::createExpandStridedMetadataPass;andmlir::createFinalizeMemRefToLLVMConversionPass.