Skip to content

Commit

Permalink
Merge pull request #381 from Xilinx/corentin.ub_initialize
Browse files Browse the repository at this point in the history
[FXML-5071] Add option to initialize UB.poison variables
  • Loading branch information
cferry-AMD authored Oct 1, 2024
2 parents 69d08b3 + 3bc24d3 commit daa3383
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 10 deletions.
5 changes: 5 additions & 0 deletions mlir/include/mlir/Conversion/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -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">,
];
}

//===----------------------------------------------------------------------===//
Expand Down
3 changes: 2 additions & 1 deletion mlir/include/mlir/Conversion/UBToEmitC/UBToEmitC.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ namespace mlir {

namespace ub {
void populateUBToEmitCConversionPatterns(TypeConverter &converter,
RewritePatternSet &patterns);
RewritePatternSet &patterns,
bool noInitialization);
} // namespace ub
} // namespace mlir

Expand Down
31 changes: 25 additions & 6 deletions mlir/lib/Conversion/UBToEmitC/UBToEmitC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ using namespace mlir;

namespace {
struct PoisonOpLowering : public OpConversionPattern<ub::PoisonOp> {
using OpConversionPattern::OpConversionPattern;
bool noInitialization;

public:
PoisonOpLowering(const TypeConverter &converter, MLIRContext *context,
bool noInitialization)
: OpConversionPattern<ub::PoisonOp>(converter, context),
noInitialization(noInitialization) {}

LogicalResult
matchAndRewrite(ub::PoisonOp op, OpAdaptor adaptor,
Expand All @@ -43,18 +49,30 @@ struct PoisonOpLowering : public OpConversionPattern<ub::PoisonOp> {
op.getLoc(), "only scalar poison values can be lowered");
}

Attribute value;
if (noInitialization) {
value = emitc::OpaqueAttr::get(op->getContext(), "");
} else if (emitc::isIntegerIndexOrOpaqueType(convertedType)) {
value = IntegerAttr::get((emitc::isPointerWideType(convertedType))
? IndexType::get(op.getContext())
: convertedType,
42);
} else if (emitc::isSupportedFloatType(convertedType)) {
value = FloatAttr::get(convertedType, 42.0f);
}

// Any constant will be fine to lower a poison op
rewriter.replaceOpWithNewOp<emitc::VariableOp>(
op, convertedType, emitc::OpaqueAttr::get(op->getContext(), ""));
rewriter.replaceOpWithNewOp<emitc::VariableOp>(op, convertedType, value);
return success();
}
};
} // namespace

void ub::populateUBToEmitCConversionPatterns(TypeConverter &converter,
RewritePatternSet &patterns) {
RewritePatternSet &patterns,
bool noInitialization) {
MLIRContext *ctx = patterns.getContext();
patterns.add<PoisonOpLowering>(converter, ctx);
patterns.add<PoisonOpLowering>(converter, ctx, noInitialization);
}

struct ConvertUBToEmitC : public impl::ConvertUBToEmitCBase<ConvertUBToEmitC> {
Expand All @@ -70,7 +88,8 @@ struct ConvertUBToEmitC : public impl::ConvertUBToEmitCBase<ConvertUBToEmitC> {
target.addLegalDialect<emitc::EmitCDialect>();
target.addIllegalDialect<ub::UBDialect>();

mlir::ub::populateUBToEmitCConversionPatterns(converter, patterns);
mlir::ub::populateUBToEmitCConversionPatterns(converter, patterns,
noInitialization);

if (failed(applyPartialConversion(getOperation(), target,
std::move(patterns))))
Expand Down
12 changes: 12 additions & 0 deletions mlir/test/Conversion/UBToEmitC/convert-ub-to-emitc-no-init.mlir
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 3 additions & 3 deletions mlir/test/Conversion/UBToEmitC/convert-ub-to-emitc.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit daa3383

Please sign in to comment.