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 #378 from Xilinx/corentin.ub_lowering
[FXML-5071] Lower UB to EmitC
- Loading branch information
Showing
8 changed files
with
170 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//===- UBToEmitC.h - UB to EmitC dialect conversion -------------*- C++ -*-===// | ||
// | ||
// 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_UBTOEMITC_UBTOEMITC_H | ||
#define MLIR_CONVERSION_UBTOEMITC_UBTOEMITC_H | ||
|
||
#include "mlir/Pass/Pass.h" | ||
#include "mlir/Transforms/DialectConversion.h" | ||
|
||
namespace mlir { | ||
#define GEN_PASS_DECL_CONVERTUBTOEMITC | ||
#include "mlir/Conversion/Passes.h.inc" | ||
|
||
namespace ub { | ||
void populateUBToEmitCConversionPatterns(TypeConverter &converter, | ||
RewritePatternSet &patterns); | ||
} // namespace ub | ||
} // namespace mlir | ||
|
||
#endif // MLIR_CONVERSION_UBTOEMITC_UBTOEMITC_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
add_mlir_conversion_library(MLIRUBToEmitC | ||
UBToEmitC.cpp | ||
|
||
ADDITIONAL_HEADER_DIRS | ||
${MLIR_MAIN_INCLUDE_DIR}/mlir/Conversion/UBToEmitC | ||
|
||
DEPENDS | ||
MLIRConversionPassIncGen | ||
|
||
LINK_COMPONENTS | ||
Core | ||
|
||
LINK_LIBS PUBLIC | ||
MLIRLLVMCommonConversion | ||
MLIREmitCDialect | ||
MLIRUBDialect | ||
) |
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,79 @@ | ||
//===- UBToEmitC.cpp - UB to EmitC dialect 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "mlir/Conversion/UBToEmitC/UBToEmitC.h" | ||
|
||
#include "mlir/Dialect/EmitC/IR/EmitC.h" | ||
#include "mlir/Dialect/EmitC/Transforms/TypeConversions.h" | ||
#include "mlir/Dialect/UB/IR/UBOps.h" | ||
#include "mlir/IR/BuiltinAttributes.h" | ||
#include "mlir/IR/BuiltinTypes.h" | ||
#include "mlir/IR/TypeUtilities.h" | ||
#include "mlir/Pass/Pass.h" | ||
#include "mlir/Transforms/DialectConversion.h" | ||
|
||
namespace mlir { | ||
#define GEN_PASS_DEF_CONVERTUBTOEMITC | ||
#include "mlir/Conversion/Passes.h.inc" | ||
} // namespace mlir | ||
|
||
using namespace mlir; | ||
|
||
namespace { | ||
struct PoisonOpLowering : public OpConversionPattern<ub::PoisonOp> { | ||
using OpConversionPattern::OpConversionPattern; | ||
|
||
LogicalResult | ||
matchAndRewrite(ub::PoisonOp op, OpAdaptor adaptor, | ||
ConversionPatternRewriter &rewriter) const override { | ||
const TypeConverter *converter = getTypeConverter(); | ||
Type convertedType = converter->convertType(op.getType()); | ||
|
||
if (!convertedType) | ||
return rewriter.notifyMatchFailure(op.getLoc(), "type conversion failed"); | ||
|
||
if (!(emitc::isIntegerIndexOrOpaqueType(convertedType) || | ||
emitc::isSupportedFloatType(convertedType))) { | ||
return rewriter.notifyMatchFailure( | ||
op.getLoc(), "only scalar poison values can be lowered"); | ||
} | ||
|
||
// Any constant will be fine to lower a poison op | ||
rewriter.replaceOpWithNewOp<emitc::VariableOp>( | ||
op, convertedType, emitc::OpaqueAttr::get(op->getContext(), "")); | ||
return success(); | ||
} | ||
}; | ||
} // namespace | ||
|
||
void ub::populateUBToEmitCConversionPatterns(TypeConverter &converter, | ||
RewritePatternSet &patterns) { | ||
MLIRContext *ctx = patterns.getContext(); | ||
patterns.add<PoisonOpLowering>(converter, ctx); | ||
} | ||
|
||
struct ConvertUBToEmitC : public impl::ConvertUBToEmitCBase<ConvertUBToEmitC> { | ||
using Base::Base; | ||
|
||
void runOnOperation() override { | ||
RewritePatternSet patterns(&getContext()); | ||
TypeConverter converter; | ||
converter.addConversion([](Type t) { return t; }); | ||
populateEmitCSizeTTypeConversions(converter); | ||
|
||
ConversionTarget target(getContext()); | ||
target.addLegalDialect<emitc::EmitCDialect>(); | ||
target.addIllegalDialect<ub::UBDialect>(); | ||
|
||
mlir::ub::populateUBToEmitCConversionPatterns(converter, patterns); | ||
|
||
if (failed(applyPartialConversion(getOperation(), target, | ||
std::move(patterns)))) | ||
signalPassFailure(); | ||
} | ||
}; |
23 changes: 23 additions & 0 deletions
23
mlir/test/Conversion/UBToEmitC/convert-ub-to-emitc-unsupported.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,23 @@ | ||
// RUN: mlir-opt -convert-ub-to-emitc -split-input-file -verify-diagnostics %s | ||
|
||
func.func @poison_memref() { | ||
// expected-error @+1 {{failed to legalize operation 'ub.poison'}} | ||
%0 = ub.poison : memref<i32> | ||
return | ||
} | ||
|
||
// ----- | ||
|
||
func.func @poison_tensor() { | ||
// expected-error @+1 {{failed to legalize operation 'ub.poison'}} | ||
%1 = ub.poison : tensor<f32> | ||
return | ||
} | ||
|
||
// ----- | ||
|
||
func.func @poison_vector() { | ||
// expected-error @+1 {{failed to legalize operation 'ub.poison'}} | ||
%1 = "ub.poison"() {value = #ub.poison} : () -> vector<4xi64> | ||
return | ||
} |
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,12 @@ | ||
// RUN: mlir-opt -convert-ub-to-emitc %s | FileCheck %s | ||
|
||
// CHECK-LABEL: func.func @poison | ||
func.func @poison() { | ||
// CHECK: "emitc.variable"{{.*}} -> i32 | ||
%0 = ub.poison : i32 | ||
// CHECK: "emitc.variable"{{.*}} -> f32 | ||
%1 = ub.poison : f32 | ||
// CHECK: "emitc.variable"{{.*}} -> !emitc.size_t | ||
%2 = ub.poison : index | ||
return | ||
} |