-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add conversion pass for Arith ConstantOp (#953)
* Add pass to convert arith Dialect ops to stablehloDialect ops starting with ConstantOp. Add test for arith.constant to ttir.constant conversion.
- Loading branch information
Showing
9 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
include/ttmlir/Conversion/ArithToStableHLO/ArithToStableHLO.h
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,19 @@ | ||
// SPDX-FileCopyrightText: (c) 2024 Tenstorrent AI ULC | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#ifndef TTMLIR_CONVERSION_ARITHTOSTABLEHLO_ARITHTOSTABLEHLO_H | ||
#define TTMLIR_CONVERSION_ARITHTOSTABLEHLO_ARITHTOSTABLEHLO_H | ||
|
||
#include "mlir/Pass/Pass.h" | ||
#include "mlir/Transforms/DialectConversion.h" | ||
|
||
namespace mlir::tt { | ||
|
||
#ifdef TTMLIR_ENABLE_STABLEHLO | ||
std::unique_ptr<OperationPass<ModuleOp>> createConvertArithToStableHLOPass(); | ||
#endif | ||
|
||
} // namespace mlir::tt | ||
|
||
#endif // TTMLIR_CONVERSION_STABLEHLOTOTTIR_STABLEHLOTOTTIR_H |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// SPDX-FileCopyrightText: (c) 2024 Tenstorrent AI ULC | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "ttmlir/Conversion/ArithToStableHLO/ArithToStableHLO.h" | ||
|
||
#include <mlir/Dialect/Arith/IR/Arith.h> | ||
#include <mlir/Dialect/Func/IR/FuncOps.h> | ||
#include <mlir/Dialect/Func/Transforms/FuncConversions.h> | ||
#include <mlir/Dialect/Tensor/IR/Tensor.h> | ||
#include <mlir/IR/BuiltinOps.h> | ||
#include <mlir/IR/Dialect.h> | ||
#include <mlir/IR/PatternMatch.h> | ||
#include <mlir/Pass/Pass.h> | ||
|
||
#include <stablehlo/dialect/StablehloOps.h> | ||
|
||
#include "ttmlir/Dialect/TT/IR/TT.h" | ||
#include "ttmlir/Dialect/TTIR/IR/TTIR.h" | ||
|
||
using namespace mlir; | ||
using namespace mlir::tt; | ||
|
||
namespace mlir::tt::ttir { | ||
|
||
#define GEN_PASS_DEF_CONVERTARITHTOSTABLEHLO | ||
#include "ttmlir/Conversion/Passes.h.inc" | ||
|
||
} // namespace mlir::tt::ttir | ||
|
||
namespace { | ||
|
||
class ArithToStableHLOConstantOpConversionPattern | ||
: public OpConversionPattern<mlir::arith::ConstantOp> { | ||
|
||
using OpConversionPattern<mlir::arith::ConstantOp>::OpConversionPattern; | ||
|
||
public: | ||
LogicalResult | ||
matchAndRewrite(mlir::arith::ConstantOp srcOp, | ||
mlir::arith::ConstantOp::Adaptor adaptor, | ||
ConversionPatternRewriter &rewriter) const override { | ||
|
||
rewriter.replaceOpWithNewOp<mlir::stablehlo::ConstantOp>(srcOp, | ||
srcOp.getValue()); | ||
return success(); | ||
} | ||
}; | ||
|
||
struct ConvertArithToStableHLOPass | ||
: public ttir::impl::ConvertArithToStableHLOBase< | ||
ConvertArithToStableHLOPass> { | ||
void runOnOperation() final { | ||
mlir::ConversionTarget target(getContext()); | ||
|
||
target.addIllegalDialect<mlir::arith::ArithDialect>(); | ||
target.addLegalDialect<mlir::stablehlo::StablehloDialect>(); | ||
target.addLegalOp<mlir::tensor::EmptyOp>(); | ||
target.addLegalOp<mlir::ModuleOp>(); | ||
target.addLegalOp<mlir::func::FuncOp>(); | ||
target.addLegalOp<mlir::func::ReturnOp>(); | ||
|
||
// For now keep the same type assuming StableHLO ops operate on builtin | ||
// tensor. | ||
TypeConverter typeConverter; | ||
typeConverter.addConversion([](Type type) { | ||
assert(isa<RankedTensorType>(type) && | ||
"only ranked tensor type supported"); | ||
return type; | ||
}); | ||
RewritePatternSet patterns(&getContext()); | ||
|
||
// Convert Arith ConstantOp to StableHLO ConstantOp | ||
patterns.add<ArithToStableHLOConstantOpConversionPattern>(typeConverter, | ||
&getContext()); | ||
|
||
// Apply conversion. | ||
if (failed( | ||
applyFullConversion(getOperation(), target, std::move(patterns)))) { | ||
signalPassFailure(); | ||
return; | ||
} | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
namespace mlir::tt { | ||
|
||
std::unique_ptr<OperationPass<ModuleOp>> createConvertArithToStableHLOPass() { | ||
return std::make_unique<ConvertArithToStableHLOPass>(); | ||
} | ||
|
||
} // namespace mlir::tt |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// REQUIRES: stablehlo | ||
// RUN: ttmlir-opt --stablehlo-to-ttir-pipeline %s | FileCheck %s | ||
module @jit_constant attributes {} { | ||
func.func public @test_splat() -> tensor<64xf32> { | ||
%0 = arith.constant dense<0.3> : tensor<64xf32> | ||
// CHECK: %[[C:.*]] = "ttir.constant"[[C:.*]] | ||
return %0 : tensor<64xf32> | ||
} | ||
|
||
func.func public @test_multiple() -> tensor<2x2xf32> { | ||
%0 = arith.constant dense<[[0.0, 1.0], [2.0, 3.0]]> : tensor<2x2xf32> | ||
// CHECK: %[[C:.*]] = "ttir.constant"[[C:.*]] | ||
return %0 : tensor<2x2xf32> | ||
} | ||
} |