From d5a9fe34b4a203c1fbb5925a6c06f7717654d7f8 Mon Sep 17 00:00:00 2001 From: LekkalaSravya3 Date: Mon, 22 Sep 2025 23:12:10 +0530 Subject: [PATCH 1/2] Handled UnrealizedConversionCast for C code generation and validated tests Signed-off-by: LekkalaSravya3 --- mlir/lib/Target/Cpp/TranslateToCpp.cpp | 84 ++++++++++++++++++- .../Cpp/unrealized_conversion_cast.mlir | 15 ++++ 2 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 mlir/test/Target/Cpp/unrealized_conversion_cast.mlir diff --git a/mlir/lib/Target/Cpp/TranslateToCpp.cpp b/mlir/lib/Target/Cpp/TranslateToCpp.cpp index a5bd80e9d6b8b..80d0c83049ac7 100644 --- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp +++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp @@ -782,6 +782,64 @@ static LogicalResult printOperation(CppEmitter &emitter, return success(); } +static LogicalResult printOperation(CppEmitter &emitter, + mlir::UnrealizedConversionCastOp castOp) { + raw_ostream &os = emitter.ostream(); + Operation &op = *castOp.getOperation(); + + if (castOp.getResults().size() != 1 || castOp.getOperands().size() != 1) { + return castOp.emitOpError( + "expected single result and single operand for conversion cast"); + } + + Type destType = castOp.getResult(0).getType(); + + auto srcPtrType = + mlir::dyn_cast(castOp.getOperand(0).getType()); + auto destArrayType = mlir::dyn_cast(destType); + + if (srcPtrType && destArrayType) { + + // Emit declaration: (*v13)[dims] = + if (failed(emitter.emitType(op.getLoc(), destArrayType.getElementType()))) + return failure(); + os << " (*" << emitter.getOrCreateName(op.getResult(0)) << ")"; + for (int64_t dim : destArrayType.getShape()) + os << "[" << dim << "]"; + os << " = "; + + os << "("; + + // Emit the C++ type for "datatype (*)[dim1][dim2]..." + if (failed(emitter.emitType(op.getLoc(), destArrayType.getElementType()))) + return failure(); + + os << "(*)"; // Pointer to array + + for (int64_t dim : destArrayType.getShape()) { + os << "[" << dim << "]"; + } + os << ")"; + if (failed(emitter.emitOperand(castOp.getOperand(0)))) + return failure(); + + return success(); + } + + // Fallback to generic C-style cast for other cases + if (failed(emitter.emitAssignPrefix(op))) + return failure(); + + os << "("; + if (failed(emitter.emitType(op.getLoc(), destType))) + return failure(); + os << ")"; + if (failed(emitter.emitOperand(castOp.getOperand(0)))) + return failure(); + + return success(); +} + static LogicalResult printOperation(CppEmitter &emitter, emitc::ApplyOp applyOp) { raw_ostream &os = emitter.ostream(); @@ -1291,7 +1349,29 @@ CppEmitter::CppEmitter(raw_ostream &os, bool declareVariablesAtTop, std::string CppEmitter::getSubscriptName(emitc::SubscriptOp op) { std::string out; llvm::raw_string_ostream ss(out); - ss << getOrCreateName(op.getValue()); + Value baseValue = op.getValue(); + + // Check if the baseValue (%arg1) is a result of UnrealizedConversionCastOp + // that converts a pointer to an array type. + if (auto castOp = dyn_cast_or_null( + baseValue.getDefiningOp())) { + auto destArrayType = + mlir::dyn_cast(castOp.getResult(0).getType()); + auto srcPtrType = + mlir::dyn_cast(castOp.getOperand(0).getType()); + + // If it's a pointer being cast to an array, emit (*varName) + if (srcPtrType && destArrayType) { + ss << "(*" << getOrCreateName(baseValue) << ")"; + } else { + // Fallback if the cast is not our specific pointer-to-array case + ss << getOrCreateName(baseValue); + } + } else { + // Default behavior for a regular array or other base types + ss << getOrCreateName(baseValue); + } + for (auto index : op.getIndices()) { ss << "[" << getOrCreateName(index) << "]"; } @@ -1747,6 +1827,8 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) { cacheDeferredOpResult(op.getResult(), getSubscriptName(op)); return success(); }) + .Case( + [&](auto op) { return printOperation(*this, op); }) .Default([&](Operation *) { return op.emitOpError("unable to find printer for op"); }); diff --git a/mlir/test/Target/Cpp/unrealized_conversion_cast.mlir b/mlir/test/Target/Cpp/unrealized_conversion_cast.mlir new file mode 100644 index 0000000000000..075a268821fa1 --- /dev/null +++ b/mlir/test/Target/Cpp/unrealized_conversion_cast.mlir @@ -0,0 +1,15 @@ +// RUN: mlir-translate -mlir-to-cpp %s | FileCheck %s + +// CHECK-LABEL: void builtin_cast +func.func @builtin_cast(%arg0: !emitc.ptr){ + // CHECK : float (*v2)[1][3][4][4] = (float(*)[1][3][4][4])v1 + %1 = builtin.unrealized_conversion_cast %arg0 : !emitc.ptr to !emitc.array<1x3x4x4xf32> +return +} + +// CHECK-LABEL: void builtin_cast_index +func.func @builtin_cast_index(%arg0: !emitc.size_t){ + // CHECK : size_t v2 = (size_t)v1 + %1 = builtin.unrealized_conversion_cast %arg0 : !emitc.size_t to index +return +} \ No newline at end of file From ec47d015e05d2c2992df0037ffd9be32dbbe866a Mon Sep 17 00:00:00 2001 From: LekkalaSravya3 Date: Thu, 25 Sep 2025 15:45:26 +0530 Subject: [PATCH 2/2] added the new line and included space in array to pointer conversion Signed-off-by: LekkalaSravya3 --- mlir/lib/Target/Cpp/TranslateToCpp.cpp | 2 +- mlir/test/Target/Cpp/unrealized_conversion_cast.mlir | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/mlir/lib/Target/Cpp/TranslateToCpp.cpp b/mlir/lib/Target/Cpp/TranslateToCpp.cpp index 80d0c83049ac7..4726656869d62 100644 --- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp +++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp @@ -814,7 +814,7 @@ static LogicalResult printOperation(CppEmitter &emitter, if (failed(emitter.emitType(op.getLoc(), destArrayType.getElementType()))) return failure(); - os << "(*)"; // Pointer to array + os << " (*)"; // Pointer to array for (int64_t dim : destArrayType.getShape()) { os << "[" << dim << "]"; diff --git a/mlir/test/Target/Cpp/unrealized_conversion_cast.mlir b/mlir/test/Target/Cpp/unrealized_conversion_cast.mlir index 075a268821fa1..3971189218c39 100644 --- a/mlir/test/Target/Cpp/unrealized_conversion_cast.mlir +++ b/mlir/test/Target/Cpp/unrealized_conversion_cast.mlir @@ -2,7 +2,7 @@ // CHECK-LABEL: void builtin_cast func.func @builtin_cast(%arg0: !emitc.ptr){ - // CHECK : float (*v2)[1][3][4][4] = (float(*)[1][3][4][4])v1 + // CHECK : float (*v2)[1][3][4][4] = (float (*)[1][3][4][4])v1 %1 = builtin.unrealized_conversion_cast %arg0 : !emitc.ptr to !emitc.array<1x3x4x4xf32> return } @@ -12,4 +12,4 @@ func.func @builtin_cast_index(%arg0: !emitc.size_t){ // CHECK : size_t v2 = (size_t)v1 %1 = builtin.unrealized_conversion_cast %arg0 : !emitc.size_t to index return -} \ No newline at end of file +}