forked from alibaba/BladeDISC
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[transform] add transform dialect interpreter pass (alibaba#765)
- Loading branch information
Showing
10 changed files
with
277 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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 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 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
5 changes: 5 additions & 0 deletions
5
...sc-transform/transforms/tests/metadata-only-transform-dialect-interpreter-standalone.mlir
This file contains 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,5 @@ | ||
transform.sequence failures(propagate) { | ||
^bb0(%arg1: !pdl.operation): | ||
%0 = transform.structured.match ops{["linalg.matmul"]} in %arg1 | ||
%1, %loops:3 = transform.structured.tile %0 [2, 3, 4] | ||
} |
41 changes: 41 additions & 0 deletions
41
...mpiler/mlir/disc/tools/disc-transform/transforms/tests/transform-dialect-interpreter.mlir
This file contains 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,41 @@ | ||
// RUN: disc-opt --disc-transform-dialect-interpreter -split-input-file %s | FileCheck %s --check-prefix=INLINE | ||
// RUN: disc-opt --disc-transform-dialect-interpreter=transform-file-name=%p/metadata-only-transform-dialect-interpreter-standalone.mlir -split-input-file %s | FileCheck %s --check-prefix=STANDALONE | ||
|
||
// INLINE-LABEL: @matmul_nn | ||
func.func @matmul_nn(%arg0: tensor<?x?xf32>, %arg1: tensor<?x?xf32>, %arg2: tensor<?x?xf32>) -> tensor<?x?xf32> { | ||
%cst = arith.constant 0.000000e+00 : f32 | ||
%0 = linalg.fill ins(%cst : f32) outs(%arg2 : tensor<?x?xf32>) -> tensor<?x?xf32> | ||
// INLINE: %{{.*}} = scf.for | ||
// INLINE-NEXT: %{{.*}} = scf.for | ||
// INLINE-NEXT: %{{.*}} = scf.for | ||
// INLINE: tensor.extract_slice | ||
// INLINE-NEXT: tensor.extract_slice | ||
// INLINE-NEXT: tensor.extract_slice | ||
// INLINE-NEXT: linalg.matmul | ||
%1 = linalg.matmul ins(%arg0, %arg1 : tensor<?x?xf32>, tensor<?x?xf32>) outs(%0 : tensor<?x?xf32>) -> tensor<?x?xf32> | ||
return %1 : tensor<?x?xf32> | ||
} | ||
|
||
|
||
transform.sequence failures(propagate) { | ||
^bb0(%arg1: !pdl.operation): | ||
%0 = transform.structured.match ops{["linalg.matmul"]} in %arg1 | ||
%1, %loops:3 = transform.structured.tile %0 [2, 3, 4] | ||
} | ||
|
||
// ----- | ||
|
||
// STANDALONE-LABEL: @matmul_nn | ||
func.func @matmul_nn(%arg0: tensor<?x?xf32>, %arg1: tensor<?x?xf32>, %arg2: tensor<?x?xf32>) -> tensor<?x?xf32> { | ||
%cst = arith.constant 0.000000e+00 : f32 | ||
%0 = linalg.fill ins(%cst : f32) outs(%arg2 : tensor<?x?xf32>) -> tensor<?x?xf32> | ||
// STANDALONE: %{{.*}} = scf.for | ||
// STANDALONE-NEXT: %{{.*}} = scf.for | ||
// STANDALONE-NEXT: %{{.*}} = scf.for | ||
// STANDALONE: tensor.extract_slice | ||
// STANDALONE-NEXT: tensor.extract_slice | ||
// STANDALONE-NEXT: tensor.extract_slice | ||
// STANDALONE-NEXT: linalg.matmul | ||
%1 = linalg.matmul ins(%arg0, %arg1 : tensor<?x?xf32>, tensor<?x?xf32>) outs(%0 : tensor<?x?xf32>) -> tensor<?x?xf32> | ||
return %1 : tensor<?x?xf32> | ||
} |
170 changes: 170 additions & 0 deletions
170
tao_compiler/mlir/disc/tools/disc-transform/transforms/transform_dialect_interpreter.cc
This file contains 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,170 @@ | ||
// Copyright 2022 The BladeDISC Authors. All rights reserved. | ||
// 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 "iree-dialects/Dialect/LinalgExt/IR/LinalgExtDialect.h" | ||
#include "iree-dialects/Dialect/LinalgExt/TransformOps/LinalgExtTransformOps.h" | ||
#include "iree-dialects/Dialect/LinalgTransform/LinalgTransformOps.h" | ||
#include "iree-dialects/Dialect/LinalgTransform/StructuredTransformOpsExt.h" | ||
#include "iree-dialects/Dialect/LinalgTransform/TransformInterpreterUtils.h" | ||
#include "llvm/Support/Debug.h" | ||
#include "llvm/Support/SourceMgr.h" | ||
#include "mlir/Dialect/Affine/IR/AffineOps.h" | ||
#include "mlir/Dialect/Arith/IR/Arith.h" | ||
#include "mlir/Dialect/Arith/Transforms/BufferizableOpInterfaceImpl.h" | ||
#include "mlir/Dialect/Bufferization/IR/Bufferization.h" | ||
#include "mlir/Dialect/Bufferization/Transforms/FuncBufferizableOpInterfaceImpl.h" | ||
#include "mlir/Dialect/Func/IR/FuncOps.h" | ||
#include "mlir/Dialect/LLVMIR/LLVMDialect.h" | ||
#include "mlir/Dialect/Linalg/IR/Linalg.h" | ||
#include "mlir/Dialect/Linalg/TransformOps/LinalgTransformOps.h" | ||
#include "mlir/Dialect/Linalg/Transforms/BufferizableOpInterfaceImpl.h" | ||
#include "mlir/Dialect/PDL/IR/PDL.h" | ||
#include "mlir/Dialect/PDLInterp/IR/PDLInterp.h" | ||
#include "mlir/Dialect/SCF/IR/SCF.h" | ||
#include "mlir/Dialect/SCF/Transforms/BufferizableOpInterfaceImpl.h" | ||
#include "mlir/Dialect/Tensor/IR/Tensor.h" | ||
#include "mlir/Dialect/Tensor/Transforms/BufferizableOpInterfaceImpl.h" | ||
#include "mlir/Dialect/Transform/IR/TransformInterfaces.h" | ||
#include "mlir/Dialect/Vector/IR/VectorOps.h" | ||
#include "mlir/Dialect/Vector/Transforms/BufferizableOpInterfaceImpl.h" | ||
#include "mlir/IR/MLIRContext.h" | ||
#include "mlir/Parser/Parser.h" | ||
#include "mlir/Pass/Pass.h" | ||
#include "mlir/Support/FileUtilities.h" | ||
#include "mlir/Transforms/Passes.h" | ||
#include "tensorflow/compiler/mlir/disc/tools/disc-transform/transforms/PassDetail.h" | ||
|
||
#define DEBUG_TYPE "disc-transform-dialect-interpreter" | ||
|
||
// This file implements the logic to apply transform dialect ops for codegen. | ||
|
||
namespace mlir { | ||
namespace disc_ral { | ||
namespace { | ||
|
||
LogicalResult parseTransformModuleFromFile( | ||
MLIRContext* context, llvm::StringRef transformFileName, | ||
OwningOpRef<ModuleOp>& transformModule) { | ||
// Parse transformFileName content into a ModuleOp. | ||
std::string errorMessage; | ||
auto memoryBuffer = openInputFile(transformFileName, &errorMessage); | ||
if (!memoryBuffer) { | ||
llvm::errs() << "failed to parse transform file: " << transformFileName | ||
<< "\n"; | ||
return failure(); | ||
} | ||
// Tell sourceMgr about this buffer, the parser will pick it up. | ||
llvm::SourceMgr sourceMgr; | ||
sourceMgr.AddNewSourceBuffer(std::move(memoryBuffer), llvm::SMLoc()); | ||
transformModule = | ||
OwningOpRef<ModuleOp>(parseSourceFile<ModuleOp>(sourceMgr, context)); | ||
return success(); | ||
} | ||
|
||
struct DiscTransformDialectInterpreterPass | ||
: public DiscTransformDialectInterpreterPassBase< | ||
DiscTransformDialectInterpreterPass> { | ||
explicit DiscTransformDialectInterpreterPass(const std::string& fileName, | ||
bool enableExpensiveChecks) | ||
: DiscTransformDialectInterpreterPassBase< | ||
DiscTransformDialectInterpreterPass>:: | ||
DiscTransformDialectInterpreterPassBase() { | ||
this->transformFileName_ = fileName; | ||
this->enableExpensiveChecks_ = enableExpensiveChecks; | ||
} | ||
|
||
void getDependentDialects(DialectRegistry& registry) const override { | ||
// TODO: this is only necessary to make registry subset happy when running | ||
// the lowering to LLVM. The lowering should be changed to stop using the | ||
// nested pass manager and this will go away. | ||
|
||
// clang-format off | ||
registry.insert<arith::ArithDialect, | ||
AffineDialect, | ||
bufferization::BufferizationDialect, | ||
iree_compiler::IREE::LinalgExt::IREELinalgExtDialect, | ||
func::FuncDialect, | ||
linalg::LinalgDialect, | ||
linalg::transform::LinalgTransformDialect, | ||
LLVM::LLVMDialect, | ||
pdl::PDLDialect, | ||
pdl_interp::PDLInterpDialect, | ||
scf::SCFDialect, | ||
tensor::TensorDialect, | ||
vector::VectorDialect | ||
// clang-format on | ||
>(); | ||
|
||
// TODO: these should be registered by the extension instead, but there is | ||
// no support for it in core currently. | ||
arith::registerBufferizableOpInterfaceExternalModels(registry); | ||
linalg::registerBufferizableOpInterfaceExternalModels(registry); | ||
scf::registerBufferizableOpInterfaceExternalModels(registry); | ||
bufferization::func_ext::registerBufferizableOpInterfaceExternalModels( | ||
registry); | ||
tensor::registerBufferizableOpInterfaceExternalModels(registry); | ||
vector::registerBufferizableOpInterfaceExternalModels(registry); | ||
|
||
registry.addExtensions< | ||
mlir::iree_compiler::IREE::LinalgExt::LinalgExtTransformOpsExtension, | ||
transform_ext::StructuredTransformOpsExtension>(); | ||
linalg::registerTransformDialectExtension(registry); | ||
} | ||
|
||
void runOnOperation() override; | ||
}; | ||
|
||
void DiscTransformDialectInterpreterPass::runOnOperation() { | ||
ModuleOp module = getOperation(); | ||
if (transformFileName_.empty()) { | ||
llvm::errs() << "no transform file name specified, assuming the transform " | ||
"module is embedded in the IR next to the top-level\n"; | ||
// parse transform ops from the module itself. | ||
for (auto op : | ||
module.getBody()->getOps<transform::TransformOpInterface>()) { | ||
if (failed(transform::applyTransforms( | ||
module, op, | ||
transform::TransformOptions().enableExpensiveChecks( | ||
enableExpensiveChecks_)))) | ||
return signalPassFailure(); | ||
} | ||
} else { | ||
// parse transform ops from a standalone file. | ||
OwningOpRef<ModuleOp> transformModule; | ||
if (failed(parseTransformModuleFromFile( | ||
module.getContext(), transformFileName_, transformModule))) { | ||
llvm::errs() << "failed to load transform ops from file " | ||
<< transformFileName_ << "\n"; | ||
return signalPassFailure(); | ||
} | ||
for (auto op : transformModule.get() | ||
.getBody() | ||
->getOps<transform::TransformOpInterface>()) { | ||
if (failed(transform::applyTransforms( | ||
module, op, | ||
transform::TransformOptions().enableExpensiveChecks( | ||
enableExpensiveChecks_)))) | ||
return signalPassFailure(); | ||
} | ||
} | ||
} | ||
|
||
} // namespace | ||
|
||
std::unique_ptr<OperationPass<ModuleOp>> | ||
createDiscTransformDialectInterpreterPass(const std::string& fileName, | ||
bool enableExpensiveChecks) { | ||
return std::make_unique<DiscTransformDialectInterpreterPass>( | ||
fileName, enableExpensiveChecks); | ||
} | ||
|
||
} // namespace disc_ral | ||
} // namespace mlir |
This file contains 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