From 7cc79984979014c86b6b4672ed0df93a74b2f218 Mon Sep 17 00:00:00 2001 From: Vladislav Vinogradov Date: Tue, 2 Feb 2021 18:26:31 +0000 Subject: [PATCH] [mlir] Allow to use constant lambda as callbacks for `TypeConverter` Reviewed By: rriddle Differential Revision: https://reviews.llvm.org/D95787 --- mlir/include/mlir/Transforms/DialectConversion.h | 16 ++++++++-------- mlir/test/lib/Dialect/Test/TestPatterns.cpp | 21 ++++++++++----------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/mlir/include/mlir/Transforms/DialectConversion.h b/mlir/include/mlir/Transforms/DialectConversion.h index ae2e2d7..a67c993 100644 --- a/mlir/include/mlir/Transforms/DialectConversion.h +++ b/mlir/include/mlir/Transforms/DialectConversion.h @@ -103,8 +103,8 @@ public: /// conversion function to perform the conversion. /// Note: When attempting to convert a type, e.g. via 'convertType', the /// mostly recently added conversions will be invoked first. - template ::template arg_t<0>> + template >::template arg_t<0>> void addConversion(FnT &&callback) { registerConversion(wrapCallback(std::forward(callback))); } @@ -124,8 +124,8 @@ public: /// /// This method registers a materialization that will be called when /// converting an illegal block argument type, to a legal type. - template ::template arg_t<1>> + template >::template arg_t<1>> void addArgumentMaterialization(FnT &&callback) { argumentMaterializations.emplace_back( wrapMaterialization(std::forward(callback))); @@ -133,16 +133,16 @@ public: /// This method registers a materialization that will be called when /// converting a legal type to an illegal source type. This is used when /// conversions to an illegal type must persist beyond the main conversion. - template ::template arg_t<1>> + template >::template arg_t<1>> void addSourceMaterialization(FnT &&callback) { sourceMaterializations.emplace_back( wrapMaterialization(std::forward(callback))); } /// This method registers a materialization that will be called when /// converting type from an illegal, or source, type to a legal type. - template ::template arg_t<1>> + template >::template arg_t<1>> void addTargetMaterialization(FnT &&callback) { targetMaterializations.emplace_back( wrapMaterialization(std::forward(callback))); diff --git a/mlir/test/lib/Dialect/Test/TestPatterns.cpp b/mlir/test/lib/Dialect/Test/TestPatterns.cpp index 7da02ed..1b2fab1 100644 --- a/mlir/test/lib/Dialect/Test/TestPatterns.cpp +++ b/mlir/test/lib/Dialect/Test/TestPatterns.cpp @@ -492,8 +492,17 @@ struct TestTypeConverter : public TypeConverter { TestTypeConverter() { addConversion(convertType); addArgumentMaterialization(materializeCast); - addArgumentMaterialization(materializeOneToOneCast); addSourceMaterialization(materializeCast); + + /// Materialize the cast for one-to-one conversion from i64 to f64. + const auto materializeOneToOneCast = + [](OpBuilder &builder, IntegerType resultType, ValueRange inputs, + Location loc) -> Optional { + if (resultType.getWidth() == 42 && inputs.size() == 1) + return builder.create(loc, resultType, inputs).getResult(); + return llvm::None; + }; + addArgumentMaterialization(materializeOneToOneCast); } static LogicalResult convertType(Type t, SmallVectorImpl &results) { @@ -532,16 +541,6 @@ struct TestTypeConverter : public TypeConverter { return inputs[0]; return builder.create(loc, resultType, inputs).getResult(); } - - /// Materialize the cast for one-to-one conversion from i64 to f64. - static Optional materializeOneToOneCast(OpBuilder &builder, - IntegerType resultType, - ValueRange inputs, - Location loc) { - if (resultType.getWidth() == 42 && inputs.size() == 1) - return builder.create(loc, resultType, inputs).getResult(); - return llvm::None; - } }; struct TestLegalizePatternDriver -- 2.7.4