forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from Xilinx/tina.FXML-4224-add-arith-to-emitc
[MLIR][EmitC] Add arith to emitc conversion
- Loading branch information
Showing
8 changed files
with
193 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//===- ArithToEmitC.h - Convert Arith to EmitC ----------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
#ifndef MLIR_CONVERSION_ARITHTOEMITC_ARITHTOEMITC_H | ||
#define MLIR_CONVERSION_ARITHTOEMITC_ARITHTOEMITC_H | ||
|
||
#include "mlir/Pass/Pass.h" | ||
|
||
namespace mlir { | ||
class RewritePatternSet; | ||
|
||
#define GEN_PASS_DECL_ARITHTOEMITCCONVERSIONPASS | ||
#include "mlir/Conversion/Passes.h.inc" | ||
|
||
void populateArithToEmitCConversionPatterns(RewritePatternSet &patterns); | ||
} // namespace mlir | ||
|
||
#endif // MLIR_CONVERSION_ARITHTOEMITC_ARITHTOEMITC_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
//===- ArithToEmitC.cpp - Arith to EmitC conversion -----------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file implements a pass to convert arith ops into emitc ops. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "mlir/Conversion/ArithToEmitC/ArithToEmitC.h" | ||
|
||
#include "mlir/Dialect/Arith/IR/Arith.h" | ||
#include "mlir/Dialect/EmitC/IR/EmitC.h" | ||
#include "mlir/IR/BuiltinTypes.h" | ||
#include "mlir/Support/LogicalResult.h" | ||
#include "mlir/Transforms/DialectConversion.h" | ||
|
||
namespace mlir { | ||
#define GEN_PASS_DEF_ARITHTOEMITCCONVERSIONPASS | ||
#include "mlir/Conversion/Passes.h.inc" | ||
} // namespace mlir | ||
|
||
using namespace mlir; | ||
|
||
namespace { | ||
|
||
static bool isConvertibleToEmitC(Type type) { | ||
Type baseType = type; | ||
if (auto tensorType = dyn_cast<TensorType>(type)) { | ||
if (!tensorType.hasRank() || !tensorType.hasStaticShape()) { | ||
return false; | ||
} | ||
baseType = tensorType.getElementType(); | ||
} | ||
|
||
if (isa<IndexType>(baseType)) { | ||
return true; | ||
} | ||
|
||
if (auto intType = dyn_cast<IntegerType>(baseType)) { | ||
switch (intType.getWidth()) { | ||
case 1: | ||
case 8: | ||
case 16: | ||
case 32: | ||
case 64: | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
if (auto floatType = dyn_cast<FloatType>(baseType)) { | ||
return floatType.isF32() || floatType.isF64(); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
class ArithConstantOpConversionPattern | ||
: public OpRewritePattern<arith::ConstantOp> { | ||
public: | ||
using OpRewritePattern::OpRewritePattern; | ||
|
||
LogicalResult matchAndRewrite(arith::ConstantOp arithConst, | ||
PatternRewriter &rewriter) const override { | ||
|
||
auto constantType = arithConst.getType(); | ||
if (!isConvertibleToEmitC(constantType)) { | ||
return rewriter.notifyMatchFailure(arithConst.getLoc(), | ||
"Type cannot be converted to emitc"); | ||
} | ||
|
||
rewriter.replaceOpWithNewOp<emitc::ConstantOp>(arithConst, constantType, | ||
arithConst.getValue()); | ||
return success(); | ||
} | ||
}; | ||
|
||
struct ConvertArithToEmitCPass | ||
: public impl::ArithToEmitCConversionPassBase<ConvertArithToEmitCPass> { | ||
public: | ||
void runOnOperation() override { | ||
|
||
ConversionTarget target(getContext()); | ||
target.addIllegalDialect<arith::ArithDialect>(); | ||
target.addLegalDialect<emitc::EmitCDialect>(); | ||
RewritePatternSet patterns(&getContext()); | ||
populateArithToEmitCConversionPatterns(patterns); | ||
|
||
if (failed(applyPartialConversion(getOperation(), target, | ||
std::move(patterns)))) { | ||
signalPassFailure(); | ||
} | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
void mlir::populateArithToEmitCConversionPatterns(RewritePatternSet &patterns) { | ||
patterns.add<ArithConstantOpConversionPattern>(patterns.getContext()); | ||
} |
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,17 @@ | ||
add_mlir_conversion_library(ArithToEmitC | ||
ArithToEmitC.cpp | ||
|
||
ADDITIONAL_HEADER_DIRS | ||
${MLIR_MAIN_INCLUDE_DIR}/mlir/Conversion/ArithToEmitC | ||
|
||
DEPENDS | ||
MLIRConversionPassIncGen | ||
|
||
LINK_COMPONENTS | ||
Core | ||
|
||
LINK_LIBS PUBLIC | ||
MLIREmitCDialect | ||
MLIRArithDialect | ||
MLIRTransforms | ||
) |
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
15 changes: 15 additions & 0 deletions
15
mlir/test/Conversion/ArithToEmitC/arith-to-emit-c-failed.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,15 @@ | ||
// RUN: mlir-opt -split-input-file -convert-arith-to-emitc -verify-diagnostics %s | ||
|
||
func.func @arith_constant_complex_tensor() -> (tensor<complex<i32>>) { | ||
// expected-error @+1 {{failed to legalize operation 'arith.constant' that was explicitly marked illegal}} | ||
%c = arith.constant dense<(2, 2)> : tensor<complex<i32>> | ||
return %c : tensor<complex<i32>> | ||
} | ||
|
||
// ----- | ||
|
||
func.func @arith_constant_invalid_int_type() -> (i10) { | ||
// expected-error @+1 {{failed to legalize operation 'arith.constant' that was explicitly marked illegal}} | ||
%c = arith.constant 0 : i10 | ||
return %c : i10 | ||
} |
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,21 @@ | ||
// RUN: mlir-opt -split-input-file -convert-arith-to-emitc %s | FileCheck %s | ||
|
||
// CHECK-LABEL: arith_constants | ||
func.func @arith_constants() { | ||
// CHECK: emitc.constant | ||
// CHECK-SAME: value = 0 : index | ||
%c_index = arith.constant 0 : index | ||
// CHECK: emitc.constant | ||
// CHECK-SAME: value = 0 : i32 | ||
%c_signless_int_32 = arith.constant 0 : i32 | ||
// CHECK: emitc.constant | ||
// CHECK-SAME: value = 0.{{0+}}e+00 : f32 | ||
%c_float_32 = arith.constant 0.0 : f32 | ||
// CHECK: emitc.constant | ||
// CHECK-SAME: value = dense<0> : tensor<i32> | ||
%c_tensor_single_value = arith.constant dense<0> : tensor<i32> | ||
// CHECK: emitc.constant | ||
// CHECK-SAME: value{{.*}}[1, 2], [-3, 9], [0, 0], [2, -1]{{.*}}tensor<4x2xi64> | ||
%c_tensor_value = arith.constant dense<[[1, 2], [-3, 9], [0, 0], [2, -1]]> : tensor<4x2xi64> | ||
return | ||
} |