From e48815fdb767a6129664b244a84bbfd2f0ef7b10 Mon Sep 17 00:00:00 2001 From: Corentin Ferry Date: Tue, 1 Oct 2024 08:27:13 +0100 Subject: [PATCH 1/2] Add option to initialize UB.poison variables --- mlir/include/mlir/Conversion/Passes.td | 5 +++ .../mlir/Conversion/UBToEmitC/UBToEmitC.h | 3 +- mlir/lib/Conversion/UBToEmitC/UBToEmitC.cpp | 34 +++++++++++++++---- .../convert-ub-to-emitc-no-init.mlir | 12 +++++++ .../UBToEmitC/convert-ub-to-emitc.mlir | 6 ++-- 5 files changed, 50 insertions(+), 10 deletions(-) create mode 100644 mlir/test/Conversion/UBToEmitC/convert-ub-to-emitc-no-init.mlir diff --git a/mlir/include/mlir/Conversion/Passes.td b/mlir/include/mlir/Conversion/Passes.td index 022e2b470ce700..e60060502173e9 100644 --- a/mlir/include/mlir/Conversion/Passes.td +++ b/mlir/include/mlir/Conversion/Passes.td @@ -1220,6 +1220,11 @@ def ConvertUBToEmitC : Pass<"convert-ub-to-emitc"> { This pass converts supported UB ops to EmitC dialect. }]; let dependentDialects = ["emitc::EmitCDialect"]; + let options = [ + Option<"noInitialization", "no-initialization", "bool", + /*default=*/"false", + "Do not initialize the generated variables">, + ]; } //===----------------------------------------------------------------------===// diff --git a/mlir/include/mlir/Conversion/UBToEmitC/UBToEmitC.h b/mlir/include/mlir/Conversion/UBToEmitC/UBToEmitC.h index 9d208a0275e904..64a37c9304afc1 100644 --- a/mlir/include/mlir/Conversion/UBToEmitC/UBToEmitC.h +++ b/mlir/include/mlir/Conversion/UBToEmitC/UBToEmitC.h @@ -18,7 +18,8 @@ namespace mlir { namespace ub { void populateUBToEmitCConversionPatterns(TypeConverter &converter, - RewritePatternSet &patterns); + RewritePatternSet &patterns, + bool noInitialization); } // namespace ub } // namespace mlir diff --git a/mlir/lib/Conversion/UBToEmitC/UBToEmitC.cpp b/mlir/lib/Conversion/UBToEmitC/UBToEmitC.cpp index 29f073322cdf2c..4d0f3359a2500f 100644 --- a/mlir/lib/Conversion/UBToEmitC/UBToEmitC.cpp +++ b/mlir/lib/Conversion/UBToEmitC/UBToEmitC.cpp @@ -26,7 +26,13 @@ using namespace mlir; namespace { struct PoisonOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; + bool noInitialization; + +public: + PoisonOpLowering(const TypeConverter &converter, MLIRContext *context, + bool noInitialization) + : OpConversionPattern(converter, context), + noInitialization(noInitialization) {} LogicalResult matchAndRewrite(ub::PoisonOp op, OpAdaptor adaptor, @@ -43,18 +49,33 @@ struct PoisonOpLowering : public OpConversionPattern { op.getLoc(), "only scalar poison values can be lowered"); } + Attribute value; + + if (noInitialization) { + value = emitc::OpaqueAttr::get(op->getContext(), ""); + } + if (!noInitialization && emitc::isIntegerIndexOrOpaqueType(convertedType)) { + value = IntegerAttr::get((emitc::isPointerWideType(convertedType)) + ? IndexType::get(op.getContext()) + : convertedType, + 42); + } + if (!noInitialization && emitc::isSupportedFloatType(convertedType)) { + value = FloatAttr::get(convertedType, 42.0f); + } + // Any constant will be fine to lower a poison op - rewriter.replaceOpWithNewOp( - op, convertedType, emitc::OpaqueAttr::get(op->getContext(), "")); + rewriter.replaceOpWithNewOp(op, convertedType, value); return success(); } }; } // namespace void ub::populateUBToEmitCConversionPatterns(TypeConverter &converter, - RewritePatternSet &patterns) { + RewritePatternSet &patterns, + bool noInitialization) { MLIRContext *ctx = patterns.getContext(); - patterns.add(converter, ctx); + patterns.add(converter, ctx, noInitialization); } struct ConvertUBToEmitC : public impl::ConvertUBToEmitCBase { @@ -70,7 +91,8 @@ struct ConvertUBToEmitC : public impl::ConvertUBToEmitCBase { target.addLegalDialect(); target.addIllegalDialect(); - mlir::ub::populateUBToEmitCConversionPatterns(converter, patterns); + mlir::ub::populateUBToEmitCConversionPatterns(converter, patterns, + noInitialization); if (failed(applyPartialConversion(getOperation(), target, std::move(patterns)))) diff --git a/mlir/test/Conversion/UBToEmitC/convert-ub-to-emitc-no-init.mlir b/mlir/test/Conversion/UBToEmitC/convert-ub-to-emitc-no-init.mlir new file mode 100644 index 00000000000000..24582254ee332f --- /dev/null +++ b/mlir/test/Conversion/UBToEmitC/convert-ub-to-emitc-no-init.mlir @@ -0,0 +1,12 @@ +// RUN: mlir-opt -p 'builtin.module(convert-ub-to-emitc{no-initialization})' %s | FileCheck %s + +// CHECK-LABEL: func.func @poison +func.func @poison() { + // CHECK: "emitc.variable"() <{value = #emitc.opaque<"">}> : () -> i32 + %0 = ub.poison : i32 + // CHECK: "emitc.variable"() <{value = #emitc.opaque<"">}> : () -> f32 + %1 = ub.poison : f32 + // CHECK: "emitc.variable"() <{value = #emitc.opaque<"">}> : () -> !emitc.size_t + %2 = ub.poison : index + return +} diff --git a/mlir/test/Conversion/UBToEmitC/convert-ub-to-emitc.mlir b/mlir/test/Conversion/UBToEmitC/convert-ub-to-emitc.mlir index b57f984354e53f..105fc9ddc18994 100644 --- a/mlir/test/Conversion/UBToEmitC/convert-ub-to-emitc.mlir +++ b/mlir/test/Conversion/UBToEmitC/convert-ub-to-emitc.mlir @@ -2,11 +2,11 @@ // CHECK-LABEL: func.func @poison func.func @poison() { - // CHECK: "emitc.variable"{{.*}} -> i32 + // CHECK: "emitc.variable"() <{value = 42 : i32}> : () -> i32 %0 = ub.poison : i32 - // CHECK: "emitc.variable"{{.*}} -> f32 + // CHECK: "emitc.variable"() <{value = 4.200000e+01 : f32}> : () -> f32 %1 = ub.poison : f32 - // CHECK: "emitc.variable"{{.*}} -> !emitc.size_t + // CHECK: "emitc.variable"() <{value = 42 : index}> : () -> !emitc.size_t %2 = ub.poison : index return } From 3bc24d38cef557e4c18cffc228a644bff517d4ae Mon Sep 17 00:00:00 2001 From: Corentin Ferry Date: Tue, 1 Oct 2024 09:30:03 +0100 Subject: [PATCH 2/2] Use else-if --- mlir/lib/Conversion/UBToEmitC/UBToEmitC.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/mlir/lib/Conversion/UBToEmitC/UBToEmitC.cpp b/mlir/lib/Conversion/UBToEmitC/UBToEmitC.cpp index 4d0f3359a2500f..5d7439eec2f14c 100644 --- a/mlir/lib/Conversion/UBToEmitC/UBToEmitC.cpp +++ b/mlir/lib/Conversion/UBToEmitC/UBToEmitC.cpp @@ -50,17 +50,14 @@ struct PoisonOpLowering : public OpConversionPattern { } Attribute value; - if (noInitialization) { value = emitc::OpaqueAttr::get(op->getContext(), ""); - } - if (!noInitialization && emitc::isIntegerIndexOrOpaqueType(convertedType)) { + } else if (emitc::isIntegerIndexOrOpaqueType(convertedType)) { value = IntegerAttr::get((emitc::isPointerWideType(convertedType)) ? IndexType::get(op.getContext()) : convertedType, 42); - } - if (!noInitialization && emitc::isSupportedFloatType(convertedType)) { + } else if (emitc::isSupportedFloatType(convertedType)) { value = FloatAttr::get(convertedType, 42.0f); }