[Mlir-commits] [mlir] Handled UnrealizedConversionCast for C code generation and validated tests (PR #160159)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Thu Oct 23 01:57:38 PDT 2025
https://github.com/LekkalaSravya3 updated https://github.com/llvm/llvm-project/pull/160159
>From a226b5bd4fbc8c92712e06bb54cc43cf12dbb796 Mon Sep 17 00:00:00 2001
From: LekkalaSravya3 <lekkala.sravya at multicorewareinc.com>
Date: Mon, 22 Sep 2025 23:12:10 +0530
Subject: [PATCH 1/3] Handled UnrealizedConversionCast for C code generation
and validated tests
Signed-off-by: LekkalaSravya3 <lekkala.sravya at multicorewareinc.com>
---
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 12435119b98a1..2d6d94af93270 100644
--- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp
+++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
@@ -830,6 +830,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<emitc::PointerType>(castOp.getOperand(0).getType());
+ auto destArrayType = mlir::dyn_cast<emitc::ArrayType>(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();
@@ -1339,7 +1397,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<mlir::UnrealizedConversionCastOp>(
+ baseValue.getDefiningOp())) {
+ auto destArrayType =
+ mlir::dyn_cast<emitc::ArrayType>(castOp.getResult(0).getType());
+ auto srcPtrType =
+ mlir::dyn_cast<emitc::PointerType>(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) << "]";
}
@@ -1796,6 +1876,8 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
cacheDeferredOpResult(op.getResult(), getSubscriptName(op));
return success();
})
+ .Case<mlir::UnrealizedConversionCastOp>(
+ [&](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<f32>){
+ // CHECK : float (*v2)[1][3][4][4] = (float(*)[1][3][4][4])v1
+ %1 = builtin.unrealized_conversion_cast %arg0 : !emitc.ptr<f32> 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 37db2407478fae6d2705c03c327bd94b6aa65055 Mon Sep 17 00:00:00 2001
From: LekkalaSravya3 <lekkala.sravya at multicorewareinc.com>
Date: Thu, 25 Sep 2025 15:45:26 +0530
Subject: [PATCH 2/3] added the new line and included space in array to pointer
conversion
Signed-off-by: LekkalaSravya3 <lekkala.sravya at multicorewareinc.com>
---
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 2d6d94af93270..ed455535911d0 100644
--- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp
+++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
@@ -862,7 +862,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<f32>){
- // 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<f32> 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
+}
>From a76c5a4218cd96c7b3c2bdc33ec6f9a0ae241235 Mon Sep 17 00:00:00 2001
From: LekkalaSravya3 <lekkala.sravya at multicorewareinc.com>
Date: Thu, 23 Oct 2025 08:01:43 +0000
Subject: [PATCH 3/3] Added Support pointer-array types in type
converter,modified alloc/load/store and C-emitter
Signed-off-by: LekkalaSravya3 <lekkala.sravya at multicorewareinc.com>
---
.../MemRefToEmitC/MemRefToEmitC.cpp | 44 +++--
mlir/lib/Target/Cpp/TranslateToCpp.cpp | 177 ++++++++----------
.../MemRefToEmitC/memref-to-emitc-alloc.mlir | 12 +-
.../MemRefToEmitC/memref-to-emitc.mlir | 10 +-
.../Cpp/unrealized_conversion_cast.mlir | 15 --
5 files changed, 122 insertions(+), 136 deletions(-)
delete mode 100644 mlir/test/Target/Cpp/unrealized_conversion_cast.mlir
diff --git a/mlir/lib/Conversion/MemRefToEmitC/MemRefToEmitC.cpp b/mlir/lib/Conversion/MemRefToEmitC/MemRefToEmitC.cpp
index 11f866c103639..3f24b5b407f62 100644
--- a/mlir/lib/Conversion/MemRefToEmitC/MemRefToEmitC.cpp
+++ b/mlir/lib/Conversion/MemRefToEmitC/MemRefToEmitC.cpp
@@ -194,8 +194,9 @@ struct ConvertAlloc final : public OpConversionPattern<memref::AllocOp> {
emitc::PointerType::get(
emitc::OpaqueType::get(rewriter.getContext(), "void")),
allocFunctionName, args);
-
- emitc::PointerType targetPointerType = emitc::PointerType::get(elementType);
+ emitc::ArrayType arrayType =
+ emitc::ArrayType::get(memrefType.getShape(), elementType);
+ emitc::PointerType targetPointerType = emitc::PointerType::get(arrayType);
emitc::CastOp castOp = emitc::CastOp::create(
rewriter, loc, targetPointerType, allocCall.getResult(0));
@@ -340,13 +341,17 @@ struct ConvertLoad final : public OpConversionPattern<memref::LoadOp> {
if (!resultTy) {
return rewriter.notifyMatchFailure(op.getLoc(), "cannot convert type");
}
-
- auto arrayValue =
- dyn_cast<TypedValue<emitc::ArrayType>>(operands.getMemref());
- if (!arrayValue) {
- return rewriter.notifyMatchFailure(op.getLoc(), "expected array type");
+ ImplicitLocOpBuilder b(op.getLoc(), rewriter);
+ Value memrefVal = operands.getMemref();
+ Value deref;
+ if (auto ptrVal = dyn_cast<TypedValue<emitc::PointerType>>(memrefVal)) {
+ auto arrayTy = dyn_cast<emitc::ArrayType>(ptrVal.getType().getPointee());
+ if (!arrayTy)
+ return failure();
+ deref = emitc::ApplyOp::create(b, arrayTy, b.getStringAttr("*"), ptrVal);
}
+ auto arrayValue = dyn_cast<TypedValue<emitc::ArrayType>>(deref);
auto subscript = emitc::SubscriptOp::create(
rewriter, op.getLoc(), arrayValue, operands.getIndices());
@@ -361,16 +366,21 @@ struct ConvertStore final : public OpConversionPattern<memref::StoreOp> {
LogicalResult
matchAndRewrite(memref::StoreOp op, OpAdaptor operands,
ConversionPatternRewriter &rewriter) const override {
- auto arrayValue =
- dyn_cast<TypedValue<emitc::ArrayType>>(operands.getMemref());
- if (!arrayValue) {
- return rewriter.notifyMatchFailure(op.getLoc(), "expected array type");
+ ImplicitLocOpBuilder b(op.getLoc(), rewriter);
+ Value memrefVal = operands.getMemref();
+ Value deref;
+ if (auto ptrVal = dyn_cast<TypedValue<emitc::PointerType>>(memrefVal)) {
+ auto arrayTy = dyn_cast<emitc::ArrayType>(ptrVal.getType().getPointee());
+ if (!arrayTy)
+ return failure();
+ deref = emitc::ApplyOp::create(b, arrayTy, b.getStringAttr("*"), ptrVal);
}
-
+ auto arrayValue = dyn_cast<TypedValue<emitc::ArrayType>>(deref);
auto subscript = emitc::SubscriptOp::create(
rewriter, op.getLoc(), arrayValue, operands.getIndices());
- rewriter.replaceOpWithNewOp<emitc::AssignOp>(op, subscript,
- operands.getValue());
+ Value valueToStore = operands.getOperands()[0];
+
+ rewriter.replaceOpWithNewOp<emitc::AssignOp>(op, subscript, valueToStore);
return success();
}
};
@@ -386,8 +396,10 @@ void mlir::populateMemRefToEmitCTypeConversion(TypeConverter &typeConverter) {
typeConverter.convertType(memRefType.getElementType());
if (!convertedElementType)
return {};
- return emitc::ArrayType::get(memRefType.getShape(),
- convertedElementType);
+ Type innerArrayType =
+ emitc::ArrayType::get(memRefType.getShape(), convertedElementType);
+ return emitc::PointerType::get(innerArrayType);
+
});
auto materializeAsUnrealizedCast = [](OpBuilder &builder, Type resultType,
diff --git a/mlir/lib/Target/Cpp/TranslateToCpp.cpp b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
index ed455535911d0..1f58e20572650 100644
--- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp
+++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
@@ -192,6 +192,9 @@ struct CppEmitter {
// Returns the textual representation of a subscript operation.
std::string getSubscriptName(emitc::SubscriptOp op);
+ // Returns a string representing the expression of an emitc.apply operation.
+ std::string getApplyName(emitc::ApplyOp op);
+
// Returns the textual representation of a member (of object) operation.
std::string createMemberAccess(emitc::MemberOp op);
@@ -830,64 +833,6 @@ 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<emitc::PointerType>(castOp.getOperand(0).getType());
- auto destArrayType = mlir::dyn_cast<emitc::ArrayType>(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();
@@ -1399,24 +1344,12 @@ std::string CppEmitter::getSubscriptName(emitc::SubscriptOp op) {
llvm::raw_string_ostream ss(out);
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<mlir::UnrealizedConversionCastOp>(
- baseValue.getDefiningOp())) {
- auto destArrayType =
- mlir::dyn_cast<emitc::ArrayType>(castOp.getResult(0).getType());
- auto srcPtrType =
- mlir::dyn_cast<emitc::PointerType>(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
+ if (auto applyOp = baseValue.getDefiningOp<emitc::ApplyOp>()) {
+ if (applyOp.getApplicableOperator() == "*")
+ ss << "(*" << getOrCreateName(applyOp.getOperand()) << ")";
+ else
ss << getOrCreateName(baseValue);
- }
} else {
- // Default behavior for a regular array or other base types
ss << getOrCreateName(baseValue);
}
@@ -1426,6 +1359,26 @@ std::string CppEmitter::getSubscriptName(emitc::SubscriptOp op) {
return out;
}
+std::string CppEmitter::getApplyName(emitc::ApplyOp op) {
+ std::string expr;
+ llvm::raw_string_ostream ss(expr);
+
+ StringRef opStr = op.getApplicableOperator();
+ Value operand = op.getOperand();
+
+ // Handle dereference and address-of operators specially
+ if (opStr == "*") {
+ ss << "(*" << getOrCreateName(operand) << ")";
+ } else if (opStr == "&") {
+ ss << "&" << getOrCreateName(operand);
+ } else {
+ // Generic operator form: operand followed by operator
+ ss << getOrCreateName(operand) << opStr;
+ }
+
+ return ss.str();
+}
+
std::string CppEmitter::createMemberAccess(emitc::MemberOp op) {
std::string out;
llvm::raw_string_ostream ss(out);
@@ -1833,20 +1786,19 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
.Case<cf::BranchOp, cf::CondBranchOp>(
[&](auto op) { return printOperation(*this, op); })
// EmitC ops.
- .Case<emitc::AddOp, emitc::ApplyOp, emitc::AssignOp,
- emitc::BitwiseAndOp, emitc::BitwiseLeftShiftOp,
- emitc::BitwiseNotOp, emitc::BitwiseOrOp,
- emitc::BitwiseRightShiftOp, emitc::BitwiseXorOp, emitc::CallOp,
- emitc::CallOpaqueOp, emitc::CastOp, emitc::ClassOp,
- emitc::CmpOp, emitc::ConditionalOp, emitc::ConstantOp,
- emitc::DeclareFuncOp, emitc::DivOp, emitc::DoOp,
- emitc::ExpressionOp, emitc::FieldOp, emitc::FileOp,
- emitc::ForOp, emitc::FuncOp, emitc::GlobalOp, emitc::IfOp,
- emitc::IncludeOp, emitc::LoadOp, emitc::LogicalAndOp,
- emitc::LogicalNotOp, emitc::LogicalOrOp, emitc::MulOp,
- emitc::RemOp, emitc::ReturnOp, emitc::SubOp, emitc::SwitchOp,
- emitc::UnaryMinusOp, emitc::UnaryPlusOp, emitc::VariableOp,
- emitc::VerbatimOp>(
+ .Case<emitc::AddOp, emitc::AssignOp, emitc::BitwiseAndOp,
+ emitc::BitwiseLeftShiftOp, emitc::BitwiseNotOp,
+ emitc::BitwiseOrOp, emitc::BitwiseRightShiftOp,
+ emitc::BitwiseXorOp, emitc::CallOp, emitc::CallOpaqueOp,
+ emitc::CastOp, emitc::ClassOp, emitc::CmpOp,
+ emitc::ConditionalOp, emitc::ConstantOp, emitc::DeclareFuncOp,
+ emitc::DivOp, emitc::DoOp, emitc::ExpressionOp, emitc::FieldOp,
+ emitc::FileOp, emitc::ForOp, emitc::FuncOp, emitc::GlobalOp,
+ emitc::IfOp, emitc::IncludeOp, emitc::LoadOp,
+ emitc::LogicalAndOp, emitc::LogicalNotOp, emitc::LogicalOrOp,
+ emitc::MulOp, emitc::RemOp, emitc::ReturnOp, emitc::SubOp,
+ emitc::SwitchOp, emitc::UnaryMinusOp, emitc::UnaryPlusOp,
+ emitc::VariableOp, emitc::VerbatimOp>(
[&](auto op) { return printOperation(*this, op); })
// Func ops.
@@ -1876,8 +1828,19 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
cacheDeferredOpResult(op.getResult(), getSubscriptName(op));
return success();
})
- .Case<mlir::UnrealizedConversionCastOp>(
- [&](auto op) { return printOperation(*this, op); })
+ .Case<emitc::ApplyOp>([&](emitc::ApplyOp op) {
+ std::string expr = getApplyName(op);
+ cacheDeferredOpResult(op.getResult(), expr);
+ // If the result is unused, emit it as a standalone statement.
+ if (op->use_empty()) {
+ std::string tmpName = "tmp" + std::to_string(++valueCount);
+ if (failed(emitType(op->getLoc(), op.getResult().getType())))
+ return failure();
+ os << " " << tmpName << " = " << expr << ";\n";
+ }
+ return success();
+ })
+
.Default([&](Operation *) {
return op.emitOpError("unable to find printer for op");
});
@@ -1896,9 +1859,9 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
// Never emit a semicolon for some operations, especially if endening with
// `}`.
trailingSemicolon &=
- !isa<cf::CondBranchOp, emitc::DeclareFuncOp, emitc::DoOp, emitc::FileOp,
- emitc::ForOp, emitc::IfOp, emitc::IncludeOp, emitc::SwitchOp,
- emitc::VerbatimOp>(op);
+ !isa<cf::CondBranchOp, emitc::DeclareFuncOp, emitc::FileOp, emitc::ForOp,
+ emitc::IfOp, emitc::IncludeOp, emitc::SwitchOp, emitc::VerbatimOp,
+ emitc::ApplyOp>(op);
os << (trailingSemicolon ? ";\n" : "\n");
@@ -1916,6 +1879,17 @@ LogicalResult CppEmitter::emitVariableDeclaration(Location loc, Type type,
}
return success();
}
+ // Handle pointer-to-array types: e.g., int (*ptr)[4][8];
+ if (auto pType = dyn_cast<emitc::PointerType>(type)) {
+ if (auto arrayPointee = dyn_cast<emitc::ArrayType>(pType.getPointee())) {
+ if (failed(emitType(loc, arrayPointee.getElementType())))
+ return failure();
+ os << " (*" << name << ")";
+ for (auto dim : arrayPointee.getShape())
+ os << "[" << dim << "]";
+ return success();
+ }
+ }
if (failed(emitType(loc, type)))
return failure();
os << " " << name;
@@ -1999,8 +1973,21 @@ LogicalResult CppEmitter::emitType(Location loc, Type type) {
if (auto lType = dyn_cast<emitc::LValueType>(type))
return emitType(loc, lType.getValueType());
if (auto pType = dyn_cast<emitc::PointerType>(type)) {
- if (isa<ArrayType>(pType.getPointee()))
- return emitError(loc, "cannot emit pointer to array type ") << type;
+ if (auto arrayPointee = dyn_cast<emitc::ArrayType>(pType.getPointee())) {
+ if (!arrayPointee.hasStaticShape())
+ return emitError(
+ loc, "cannot emit pointer to array type with non-static shape");
+ if (arrayPointee.getShape().empty())
+ return emitError(loc, "cannot emit pointer to empty array type");
+
+ if (failed(emitType(loc, arrayPointee.getElementType())))
+ return failure();
+
+ os << " (*)";
+ for (int64_t dim : arrayPointee.getShape())
+ os << "[" << dim << "]";
+ return success();
+ }
if (failed(emitType(loc, pType.getPointee())))
return failure();
os << "*";
diff --git a/mlir/test/Conversion/MemRefToEmitC/memref-to-emitc-alloc.mlir b/mlir/test/Conversion/MemRefToEmitC/memref-to-emitc-alloc.mlir
index e391a893bc44a..9336faafb0dfd 100644
--- a/mlir/test/Conversion/MemRefToEmitC/memref-to-emitc-alloc.mlir
+++ b/mlir/test/Conversion/MemRefToEmitC/memref-to-emitc-alloc.mlir
@@ -13,7 +13,7 @@ func.func @alloc() {
// CPP-NEXT: %[[ALLOC_SIZE:.*]] = "emitc.constant"() <{value = 999 : index}> : () -> index
// CPP-NEXT: %[[ALLOC_TOTAL_SIZE:.*]] = emitc.mul %[[ALLOC]], %[[ALLOC_SIZE]] : (!emitc.size_t, index) -> !emitc.size_t
// CPP-NEXT: %[[ALLOC_PTR:.*]] = emitc.call_opaque "malloc"(%[[ALLOC_TOTAL_SIZE]]) : (!emitc.size_t) -> !emitc.ptr<!emitc.opaque<"void">>
-// CPP-NEXT: %[[ALLOC_CAST:.*]] = emitc.cast %[[ALLOC_PTR]] : !emitc.ptr<!emitc.opaque<"void">> to !emitc.ptr<i32>
+// CPP-NEXT: %[[ALLOC_CAST:.*]] = emitc.cast %[[ALLOC_PTR]] : !emitc.ptr<!emitc.opaque<"void">> to !emitc.ptr<!emitc.array<999xi32>>
// CPP-NEXT: return
// NOCPP: module {
@@ -23,7 +23,7 @@ func.func @alloc() {
// NOCPP-NEXT: %[[ALLOC_SIZE:.*]] = "emitc.constant"() <{value = 999 : index}> : () -> index
// NOCPP-NEXT: %[[ALLOC_TOTAL_SIZE:.*]] = emitc.mul %[[ALLOC]], %[[ALLOC_SIZE]] : (!emitc.size_t, index) -> !emitc.size_t
// NOCPP-NEXT: %[[ALLOC_PTR:.*]] = emitc.call_opaque "malloc"(%[[ALLOC_TOTAL_SIZE]]) : (!emitc.size_t) -> !emitc.ptr<!emitc.opaque<"void">>
-// NOCPP-NEXT: %[[ALLOC_CAST:.*]] = emitc.cast %[[ALLOC_PTR]] : !emitc.ptr<!emitc.opaque<"void">> to !emitc.ptr<i32>
+// NOCPP-NEXT: %[[ALLOC_CAST:.*]] = emitc.cast %[[ALLOC_PTR]] : !emitc.ptr<!emitc.opaque<"void">> to !emitc.ptr<!emitc.array<999xi32>>
// NOCPP-NEXT: return
func.func @alloc_aligned() {
@@ -37,7 +37,7 @@ func.func @alloc_aligned() {
// CPP-NEXT: %[[ALLOC_TOTAL_SIZE:.*]] = emitc.mul %[[ALLOC]], %[[ALLOC_SIZE]] : (!emitc.size_t, index) -> !emitc.size_t
// CPP-NEXT: %[[ALIGNMENT:.*]] = "emitc.constant"() <{value = 64 : index}> : () -> !emitc.size_t
// CPP-NEXT: %[[ALLOC_PTR:.*]] = emitc.call_opaque "aligned_alloc"(%[[ALIGNMENT]], %[[ALLOC_TOTAL_SIZE]]) : (!emitc.size_t, !emitc.size_t) -> !emitc.ptr<!emitc.opaque<"void">>
-// CPP-NEXT: %[[ALLOC_CAST:.*]] = emitc.cast %[[ALLOC_PTR]] : !emitc.ptr<!emitc.opaque<"void">> to !emitc.ptr<f32>
+// CPP-NEXT: %[[ALLOC_CAST:.*]] = emitc.cast %[[ALLOC_PTR]] : !emitc.ptr<!emitc.opaque<"void">> to !emitc.ptr<!emitc.array<999xf32>>
// CPP-NEXT: return
// NOCPP-LABEL: alloc_aligned
@@ -46,7 +46,7 @@ func.func @alloc_aligned() {
// NOCPP-NEXT: %[[ALLOC_TOTAL_SIZE:.*]] = emitc.mul %[[ALLOC]], %[[ALLOC_SIZE]] : (!emitc.size_t, index) -> !emitc.size_t
// NOCPP-NEXT: %[[ALIGNMENT:.*]] = "emitc.constant"() <{value = 64 : index}> : () -> !emitc.size_t
// NOCPP-NEXT: %[[ALLOC_PTR:.*]] = emitc.call_opaque "aligned_alloc"(%[[ALIGNMENT]], %[[ALLOC_TOTAL_SIZE]]) : (!emitc.size_t, !emitc.size_t) -> !emitc.ptr<!emitc.opaque<"void">>
-// NOCPP-NEXT: %[[ALLOC_CAST:.*]] = emitc.cast %[[ALLOC_PTR]] : !emitc.ptr<!emitc.opaque<"void">> to !emitc.ptr<f32>
+// NOCPP-NEXT: %[[ALLOC_CAST:.*]] = emitc.cast %[[ALLOC_PTR]] : !emitc.ptr<!emitc.opaque<"void">> to !emitc.ptr<!emitc.array<999xf32>>
// NOCPP-NEXT: return
func.func @allocating_multi() {
@@ -59,7 +59,7 @@ func.func @allocating_multi() {
// CPP-NEXT: %[[ALLOC_SIZE:.*]] = "emitc.constant"() <{value = 6993 : index}> : () -> index
// CPP-NEXT: %[[ALLOC_TOTAL_SIZE:.*]] = emitc.mul %[[ALLOC]], %[[ALLOC_SIZE]] : (!emitc.size_t, index) -> !emitc.size_t
// CPP-NEXT: %[[ALLOC_PTR:.*]] = emitc.call_opaque "malloc"(%[[ALLOC_TOTAL_SIZE]]) : (!emitc.size_t) -> !emitc.ptr<!emitc.opaque<"void">
-// CPP-NEXT: %[[ALLOC_CAST:.*]] = emitc.cast %[[ALLOC_PTR]] : !emitc.ptr<!emitc.opaque<"void">> to !emitc.ptr<i32>
+// CPP-NEXT: %[[ALLOC_CAST:.*]] = emitc.cast %[[ALLOC_PTR]] : !emitc.ptr<!emitc.opaque<"void">> to !emitc.ptr<!emitc.array<7x999xi32>>
// CPP-NEXT: return
// NOCPP-LABEL: allocating_multi
@@ -67,6 +67,6 @@ func.func @allocating_multi() {
// NOCPP-NEXT: %[[ALLOC_SIZE:.*]] = "emitc.constant"() <{value = 6993 : index}> : () -> index
// NOCPP-NEXT: %[[ALLOC_TOTAL_SIZE:.*]] = emitc.mul %[[ALLOC]], %[[ALLOC_SIZE]] : (!emitc.size_t, index) -> !emitc.size_t
// NOCPP-NEXT: %[[ALLOC_PTR:.*]] = emitc.call_opaque "malloc"(%[[ALLOC_TOTAL_SIZE]]) : (!emitc.size_t) -> !emitc.ptr<!emitc.opaque<"void">>
-// NOCPP-NEXT: %[[ALLOC_CAST:.*]] = emitc.cast %[[ALLOC_PTR]] : !emitc.ptr<!emitc.opaque<"void">> to !emitc.ptr<i32>
+// NOCPP-NEXT: %[[ALLOC_CAST:.*]] = emitc.cast %[[ALLOC_PTR]] : !emitc.ptr<!emitc.opaque<"void">> to !emitc.ptr<!emitc.array<7x999xi32>>
// NOCPP-NEXT: return
diff --git a/mlir/test/Conversion/MemRefToEmitC/memref-to-emitc.mlir b/mlir/test/Conversion/MemRefToEmitC/memref-to-emitc.mlir
index 2b4eda37903d4..4b02e3fe744f0 100644
--- a/mlir/test/Conversion/MemRefToEmitC/memref-to-emitc.mlir
+++ b/mlir/test/Conversion/MemRefToEmitC/memref-to-emitc.mlir
@@ -13,9 +13,10 @@ func.func @alloca() {
// CHECK-LABEL: memref_store
// CHECK-SAME: %[[buff:.*]]: memref<4x8xf32>, %[[v:.*]]: f32, %[[i:.*]]: index, %[[j:.*]]: index
func.func @memref_store(%buff : memref<4x8xf32>, %v : f32, %i: index, %j: index) {
- // CHECK-NEXT: %[[BUFFER:.*]] = builtin.unrealized_conversion_cast %[[buff]] : memref<4x8xf32> to !emitc.array<4x8xf32>
+ // CHECK-NEXT: %[[BUFFER:.*]] = builtin.unrealized_conversion_cast %[[buff]] : memref<4x8xf32> to !emitc.ptr<!emitc.array<4x8xf32>>
- // CHECK-NEXT: %[[SUBSCRIPT:.*]] = emitc.subscript %[[BUFFER]][%[[i]], %[[j]]] : (!emitc.array<4x8xf32>, index, index) -> !emitc.lvalue<f32>
+ // CHECK-NEXT: %[[APPLY:.+]] = emitc.apply "*"(%[[BUFFER]]) : (!emitc.ptr<!emitc.array<4x8xf32>>) -> !emitc.array<4x8xf32>
+ // CHECK-NEXT: %[[SUBSCRIPT:.*]] = emitc.subscript %[[APPLY]][%[[i]], %[[j]]] : (!emitc.array<4x8xf32>, index, index) -> !emitc.lvalue<f32>
// CHECK-NEXT: emitc.assign %[[v]] : f32 to %[[SUBSCRIPT]] : <f32>
memref.store %v, %buff[%i, %j] : memref<4x8xf32>
return
@@ -26,9 +27,10 @@ func.func @memref_store(%buff : memref<4x8xf32>, %v : f32, %i: index, %j: index)
// CHECK-LABEL: memref_load
// CHECK-SAME: %[[buff:.*]]: memref<4x8xf32>, %[[i:.*]]: index, %[[j:.*]]: index
func.func @memref_load(%buff : memref<4x8xf32>, %i: index, %j: index) -> f32 {
- // CHECK-NEXT: %[[BUFFER:.*]] = builtin.unrealized_conversion_cast %[[buff]] : memref<4x8xf32> to !emitc.array<4x8xf32>
+ // CHECK-NEXT: %[[BUFFER:.*]] = builtin.unrealized_conversion_cast %[[buff]] : memref<4x8xf32> to !emitc.ptr<!emitc.array<4x8xf32>>
- // CHECK-NEXT: %[[SUBSCRIPT:.*]] = emitc.subscript %[[BUFFER]][%[[i]], %[[j]]] : (!emitc.array<4x8xf32>, index, index) -> !emitc.lvalue<f32>
+ // CHECK-NEXT: %[[APPLY:.+]] = emitc.apply "*"(%[[BUFFER]]) : (!emitc.ptr<!emitc.array<4x8xf32>>) -> !emitc.array<4x8xf32>
+ // CHECK-NEXT: %[[SUBSCRIPT:.*]] = emitc.subscript %[[APPLY]][%[[i]], %[[j]]] : (!emitc.array<4x8xf32>, index, index) -> !emitc.lvalue<f32>
// CHECK-NEXT: %[[LOAD:.*]] = emitc.load %[[SUBSCRIPT]] : <f32>
%1 = memref.load %buff[%i, %j] : memref<4x8xf32>
// CHECK-NEXT: return %[[LOAD]] : f32
diff --git a/mlir/test/Target/Cpp/unrealized_conversion_cast.mlir b/mlir/test/Target/Cpp/unrealized_conversion_cast.mlir
deleted file mode 100644
index 3971189218c39..0000000000000
--- a/mlir/test/Target/Cpp/unrealized_conversion_cast.mlir
+++ /dev/null
@@ -1,15 +0,0 @@
-// RUN: mlir-translate -mlir-to-cpp %s | FileCheck %s
-
-// CHECK-LABEL: void builtin_cast
-func.func @builtin_cast(%arg0: !emitc.ptr<f32>){
- // CHECK : float (*v2)[1][3][4][4] = (float (*)[1][3][4][4])v1
- %1 = builtin.unrealized_conversion_cast %arg0 : !emitc.ptr<f32> 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
-}
More information about the Mlir-commits
mailing list