Skip to content

Commit eff40b7

Browse files
Added Support pointer-array types in type converter,modified alloc/load/store and C-emitter
Signed-off-by: LekkalaSravya3 <[email protected]>
1 parent 1534193 commit eff40b7

File tree

5 files changed

+122
-136
lines changed

5 files changed

+122
-136
lines changed

mlir/lib/Conversion/MemRefToEmitC/MemRefToEmitC.cpp

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,9 @@ struct ConvertAlloc final : public OpConversionPattern<memref::AllocOp> {
194194
emitc::PointerType::get(
195195
emitc::OpaqueType::get(rewriter.getContext(), "void")),
196196
allocFunctionName, args);
197-
198-
emitc::PointerType targetPointerType = emitc::PointerType::get(elementType);
197+
emitc::ArrayType arrayType =
198+
emitc::ArrayType::get(memrefType.getShape(), elementType);
199+
emitc::PointerType targetPointerType = emitc::PointerType::get(arrayType);
199200
emitc::CastOp castOp = emitc::CastOp::create(
200201
rewriter, loc, targetPointerType, allocCall.getResult(0));
201202

@@ -340,13 +341,17 @@ struct ConvertLoad final : public OpConversionPattern<memref::LoadOp> {
340341
if (!resultTy) {
341342
return rewriter.notifyMatchFailure(op.getLoc(), "cannot convert type");
342343
}
343-
344-
auto arrayValue =
345-
dyn_cast<TypedValue<emitc::ArrayType>>(operands.getMemref());
346-
if (!arrayValue) {
347-
return rewriter.notifyMatchFailure(op.getLoc(), "expected array type");
344+
ImplicitLocOpBuilder b(op.getLoc(), rewriter);
345+
Value memrefVal = operands.getMemref();
346+
Value deref;
347+
if (auto ptrVal = dyn_cast<TypedValue<emitc::PointerType>>(memrefVal)) {
348+
auto arrayTy = dyn_cast<emitc::ArrayType>(ptrVal.getType().getPointee());
349+
if (!arrayTy)
350+
return failure();
351+
deref = emitc::ApplyOp::create(b, arrayTy, b.getStringAttr("*"), ptrVal);
348352
}
349353

354+
auto arrayValue = dyn_cast<TypedValue<emitc::ArrayType>>(deref);
350355
auto subscript = emitc::SubscriptOp::create(
351356
rewriter, op.getLoc(), arrayValue, operands.getIndices());
352357

@@ -361,16 +366,21 @@ struct ConvertStore final : public OpConversionPattern<memref::StoreOp> {
361366
LogicalResult
362367
matchAndRewrite(memref::StoreOp op, OpAdaptor operands,
363368
ConversionPatternRewriter &rewriter) const override {
364-
auto arrayValue =
365-
dyn_cast<TypedValue<emitc::ArrayType>>(operands.getMemref());
366-
if (!arrayValue) {
367-
return rewriter.notifyMatchFailure(op.getLoc(), "expected array type");
369+
ImplicitLocOpBuilder b(op.getLoc(), rewriter);
370+
Value memrefVal = operands.getMemref();
371+
Value deref;
372+
if (auto ptrVal = dyn_cast<TypedValue<emitc::PointerType>>(memrefVal)) {
373+
auto arrayTy = dyn_cast<emitc::ArrayType>(ptrVal.getType().getPointee());
374+
if (!arrayTy)
375+
return failure();
376+
deref = emitc::ApplyOp::create(b, arrayTy, b.getStringAttr("*"), ptrVal);
368377
}
369-
378+
auto arrayValue = dyn_cast<TypedValue<emitc::ArrayType>>(deref);
370379
auto subscript = emitc::SubscriptOp::create(
371380
rewriter, op.getLoc(), arrayValue, operands.getIndices());
372-
rewriter.replaceOpWithNewOp<emitc::AssignOp>(op, subscript,
373-
operands.getValue());
381+
Value valueToStore = operands.getOperands()[0];
382+
383+
rewriter.replaceOpWithNewOp<emitc::AssignOp>(op, subscript, valueToStore);
374384
return success();
375385
}
376386
};
@@ -386,8 +396,10 @@ void mlir::populateMemRefToEmitCTypeConversion(TypeConverter &typeConverter) {
386396
typeConverter.convertType(memRefType.getElementType());
387397
if (!convertedElementType)
388398
return {};
389-
return emitc::ArrayType::get(memRefType.getShape(),
390-
convertedElementType);
399+
Type innerArrayType =
400+
emitc::ArrayType::get(memRefType.getShape(), convertedElementType);
401+
return emitc::PointerType::get(innerArrayType);
402+
391403
});
392404

393405
auto materializeAsUnrealizedCast = [](OpBuilder &builder, Type resultType,

mlir/lib/Target/Cpp/TranslateToCpp.cpp

Lines changed: 82 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ struct CppEmitter {
192192
// Returns the textual representation of a subscript operation.
193193
std::string getSubscriptName(emitc::SubscriptOp op);
194194

195+
// Returns a string representing the expression of an emitc.apply operation.
196+
std::string getApplyName(emitc::ApplyOp op);
197+
195198
// Returns the textual representation of a member (of object) operation.
196199
std::string createMemberAccess(emitc::MemberOp op);
197200

@@ -830,64 +833,6 @@ static LogicalResult printOperation(CppEmitter &emitter,
830833
return success();
831834
}
832835

833-
static LogicalResult printOperation(CppEmitter &emitter,
834-
mlir::UnrealizedConversionCastOp castOp) {
835-
raw_ostream &os = emitter.ostream();
836-
Operation &op = *castOp.getOperation();
837-
838-
if (castOp.getResults().size() != 1 || castOp.getOperands().size() != 1) {
839-
return castOp.emitOpError(
840-
"expected single result and single operand for conversion cast");
841-
}
842-
843-
Type destType = castOp.getResult(0).getType();
844-
845-
auto srcPtrType =
846-
mlir::dyn_cast<emitc::PointerType>(castOp.getOperand(0).getType());
847-
auto destArrayType = mlir::dyn_cast<emitc::ArrayType>(destType);
848-
849-
if (srcPtrType && destArrayType) {
850-
851-
// Emit declaration: (*v13)[dims] =
852-
if (failed(emitter.emitType(op.getLoc(), destArrayType.getElementType())))
853-
return failure();
854-
os << " (*" << emitter.getOrCreateName(op.getResult(0)) << ")";
855-
for (int64_t dim : destArrayType.getShape())
856-
os << "[" << dim << "]";
857-
os << " = ";
858-
859-
os << "(";
860-
861-
// Emit the C++ type for "datatype (*)[dim1][dim2]..."
862-
if (failed(emitter.emitType(op.getLoc(), destArrayType.getElementType())))
863-
return failure();
864-
865-
os << " (*)"; // Pointer to array
866-
867-
for (int64_t dim : destArrayType.getShape()) {
868-
os << "[" << dim << "]";
869-
}
870-
os << ")";
871-
if (failed(emitter.emitOperand(castOp.getOperand(0))))
872-
return failure();
873-
874-
return success();
875-
}
876-
877-
// Fallback to generic C-style cast for other cases
878-
if (failed(emitter.emitAssignPrefix(op)))
879-
return failure();
880-
881-
os << "(";
882-
if (failed(emitter.emitType(op.getLoc(), destType)))
883-
return failure();
884-
os << ")";
885-
if (failed(emitter.emitOperand(castOp.getOperand(0))))
886-
return failure();
887-
888-
return success();
889-
}
890-
891836
static LogicalResult printOperation(CppEmitter &emitter,
892837
emitc::ApplyOp applyOp) {
893838
raw_ostream &os = emitter.ostream();
@@ -1399,24 +1344,12 @@ std::string CppEmitter::getSubscriptName(emitc::SubscriptOp op) {
13991344
llvm::raw_string_ostream ss(out);
14001345
Value baseValue = op.getValue();
14011346

1402-
// Check if the baseValue (%arg1) is a result of UnrealizedConversionCastOp
1403-
// that converts a pointer to an array type.
1404-
if (auto castOp = dyn_cast_or_null<mlir::UnrealizedConversionCastOp>(
1405-
baseValue.getDefiningOp())) {
1406-
auto destArrayType =
1407-
mlir::dyn_cast<emitc::ArrayType>(castOp.getResult(0).getType());
1408-
auto srcPtrType =
1409-
mlir::dyn_cast<emitc::PointerType>(castOp.getOperand(0).getType());
1410-
1411-
// If it's a pointer being cast to an array, emit (*varName)
1412-
if (srcPtrType && destArrayType) {
1413-
ss << "(*" << getOrCreateName(baseValue) << ")";
1414-
} else {
1415-
// Fallback if the cast is not our specific pointer-to-array case
1347+
if (auto applyOp = baseValue.getDefiningOp<emitc::ApplyOp>()) {
1348+
if (applyOp.getApplicableOperator() == "*")
1349+
ss << "(*" << getOrCreateName(applyOp.getOperand()) << ")";
1350+
else
14161351
ss << getOrCreateName(baseValue);
1417-
}
14181352
} else {
1419-
// Default behavior for a regular array or other base types
14201353
ss << getOrCreateName(baseValue);
14211354
}
14221355

@@ -1426,6 +1359,26 @@ std::string CppEmitter::getSubscriptName(emitc::SubscriptOp op) {
14261359
return out;
14271360
}
14281361

1362+
std::string CppEmitter::getApplyName(emitc::ApplyOp op) {
1363+
std::string expr;
1364+
llvm::raw_string_ostream ss(expr);
1365+
1366+
StringRef opStr = op.getApplicableOperator();
1367+
Value operand = op.getOperand();
1368+
1369+
// Handle dereference and address-of operators specially
1370+
if (opStr == "*") {
1371+
ss << "(*" << getOrCreateName(operand) << ")";
1372+
} else if (opStr == "&") {
1373+
ss << "&" << getOrCreateName(operand);
1374+
} else {
1375+
// Generic operator form: operand followed by operator
1376+
ss << getOrCreateName(operand) << opStr;
1377+
}
1378+
1379+
return ss.str();
1380+
}
1381+
14291382
std::string CppEmitter::createMemberAccess(emitc::MemberOp op) {
14301383
std::string out;
14311384
llvm::raw_string_ostream ss(out);
@@ -1833,20 +1786,19 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
18331786
.Case<cf::BranchOp, cf::CondBranchOp>(
18341787
[&](auto op) { return printOperation(*this, op); })
18351788
// EmitC ops.
1836-
.Case<emitc::AddOp, emitc::ApplyOp, emitc::AssignOp,
1837-
emitc::BitwiseAndOp, emitc::BitwiseLeftShiftOp,
1838-
emitc::BitwiseNotOp, emitc::BitwiseOrOp,
1839-
emitc::BitwiseRightShiftOp, emitc::BitwiseXorOp, emitc::CallOp,
1840-
emitc::CallOpaqueOp, emitc::CastOp, emitc::ClassOp,
1841-
emitc::CmpOp, emitc::ConditionalOp, emitc::ConstantOp,
1842-
emitc::DeclareFuncOp, emitc::DivOp, emitc::DoOp,
1843-
emitc::ExpressionOp, emitc::FieldOp, emitc::FileOp,
1844-
emitc::ForOp, emitc::FuncOp, emitc::GlobalOp, emitc::IfOp,
1845-
emitc::IncludeOp, emitc::LoadOp, emitc::LogicalAndOp,
1846-
emitc::LogicalNotOp, emitc::LogicalOrOp, emitc::MulOp,
1847-
emitc::RemOp, emitc::ReturnOp, emitc::SubOp, emitc::SwitchOp,
1848-
emitc::UnaryMinusOp, emitc::UnaryPlusOp, emitc::VariableOp,
1849-
emitc::VerbatimOp>(
1789+
.Case<emitc::AddOp, emitc::AssignOp, emitc::BitwiseAndOp,
1790+
emitc::BitwiseLeftShiftOp, emitc::BitwiseNotOp,
1791+
emitc::BitwiseOrOp, emitc::BitwiseRightShiftOp,
1792+
emitc::BitwiseXorOp, emitc::CallOp, emitc::CallOpaqueOp,
1793+
emitc::CastOp, emitc::ClassOp, emitc::CmpOp,
1794+
emitc::ConditionalOp, emitc::ConstantOp, emitc::DeclareFuncOp,
1795+
emitc::DivOp, emitc::DoOp, emitc::ExpressionOp, emitc::FieldOp,
1796+
emitc::FileOp, emitc::ForOp, emitc::FuncOp, emitc::GlobalOp,
1797+
emitc::IfOp, emitc::IncludeOp, emitc::LoadOp,
1798+
emitc::LogicalAndOp, emitc::LogicalNotOp, emitc::LogicalOrOp,
1799+
emitc::MulOp, emitc::RemOp, emitc::ReturnOp, emitc::SubOp,
1800+
emitc::SwitchOp, emitc::UnaryMinusOp, emitc::UnaryPlusOp,
1801+
emitc::VariableOp, emitc::VerbatimOp>(
18501802

18511803
[&](auto op) { return printOperation(*this, op); })
18521804
// Func ops.
@@ -1876,8 +1828,19 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
18761828
cacheDeferredOpResult(op.getResult(), getSubscriptName(op));
18771829
return success();
18781830
})
1879-
.Case<mlir::UnrealizedConversionCastOp>(
1880-
[&](auto op) { return printOperation(*this, op); })
1831+
.Case<emitc::ApplyOp>([&](emitc::ApplyOp op) {
1832+
std::string expr = getApplyName(op);
1833+
cacheDeferredOpResult(op.getResult(), expr);
1834+
// If the result is unused, emit it as a standalone statement.
1835+
if (op->use_empty()) {
1836+
std::string tmpName = "tmp" + std::to_string(++valueCount);
1837+
if (failed(emitType(op->getLoc(), op.getResult().getType())))
1838+
return failure();
1839+
os << " " << tmpName << " = " << expr << ";\n";
1840+
}
1841+
return success();
1842+
})
1843+
18811844
.Default([&](Operation *) {
18821845
return op.emitOpError("unable to find printer for op");
18831846
});
@@ -1896,9 +1859,9 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
18961859
// Never emit a semicolon for some operations, especially if endening with
18971860
// `}`.
18981861
trailingSemicolon &=
1899-
!isa<cf::CondBranchOp, emitc::DeclareFuncOp, emitc::DoOp, emitc::FileOp,
1900-
emitc::ForOp, emitc::IfOp, emitc::IncludeOp, emitc::SwitchOp,
1901-
emitc::VerbatimOp>(op);
1862+
!isa<cf::CondBranchOp, emitc::DeclareFuncOp, emitc::FileOp, emitc::ForOp,
1863+
emitc::IfOp, emitc::IncludeOp, emitc::SwitchOp, emitc::VerbatimOp,
1864+
emitc::ApplyOp>(op);
19021865

19031866
os << (trailingSemicolon ? ";\n" : "\n");
19041867

@@ -1916,6 +1879,17 @@ LogicalResult CppEmitter::emitVariableDeclaration(Location loc, Type type,
19161879
}
19171880
return success();
19181881
}
1882+
// Handle pointer-to-array types: e.g., int (*ptr)[4][8];
1883+
if (auto pType = dyn_cast<emitc::PointerType>(type)) {
1884+
if (auto arrayPointee = dyn_cast<emitc::ArrayType>(pType.getPointee())) {
1885+
if (failed(emitType(loc, arrayPointee.getElementType())))
1886+
return failure();
1887+
os << " (*" << name << ")";
1888+
for (auto dim : arrayPointee.getShape())
1889+
os << "[" << dim << "]";
1890+
return success();
1891+
}
1892+
}
19191893
if (failed(emitType(loc, type)))
19201894
return failure();
19211895
os << " " << name;
@@ -1999,8 +1973,21 @@ LogicalResult CppEmitter::emitType(Location loc, Type type) {
19991973
if (auto lType = dyn_cast<emitc::LValueType>(type))
20001974
return emitType(loc, lType.getValueType());
20011975
if (auto pType = dyn_cast<emitc::PointerType>(type)) {
2002-
if (isa<ArrayType>(pType.getPointee()))
2003-
return emitError(loc, "cannot emit pointer to array type ") << type;
1976+
if (auto arrayPointee = dyn_cast<emitc::ArrayType>(pType.getPointee())) {
1977+
if (!arrayPointee.hasStaticShape())
1978+
return emitError(
1979+
loc, "cannot emit pointer to array type with non-static shape");
1980+
if (arrayPointee.getShape().empty())
1981+
return emitError(loc, "cannot emit pointer to empty array type");
1982+
1983+
if (failed(emitType(loc, arrayPointee.getElementType())))
1984+
return failure();
1985+
1986+
os << " (*)";
1987+
for (int64_t dim : arrayPointee.getShape())
1988+
os << "[" << dim << "]";
1989+
return success();
1990+
}
20041991
if (failed(emitType(loc, pType.getPointee())))
20051992
return failure();
20061993
os << "*";

mlir/test/Conversion/MemRefToEmitC/memref-to-emitc-alloc.mlir

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func.func @alloc() {
1313
// CPP-NEXT: %[[ALLOC_SIZE:.*]] = "emitc.constant"() <{value = 999 : index}> : () -> index
1414
// CPP-NEXT: %[[ALLOC_TOTAL_SIZE:.*]] = emitc.mul %[[ALLOC]], %[[ALLOC_SIZE]] : (!emitc.size_t, index) -> !emitc.size_t
1515
// CPP-NEXT: %[[ALLOC_PTR:.*]] = emitc.call_opaque "malloc"(%[[ALLOC_TOTAL_SIZE]]) : (!emitc.size_t) -> !emitc.ptr<!emitc.opaque<"void">>
16-
// CPP-NEXT: %[[ALLOC_CAST:.*]] = emitc.cast %[[ALLOC_PTR]] : !emitc.ptr<!emitc.opaque<"void">> to !emitc.ptr<i32>
16+
// CPP-NEXT: %[[ALLOC_CAST:.*]] = emitc.cast %[[ALLOC_PTR]] : !emitc.ptr<!emitc.opaque<"void">> to !emitc.ptr<!emitc.array<999xi32>>
1717
// CPP-NEXT: return
1818

1919
// NOCPP: module {
@@ -23,7 +23,7 @@ func.func @alloc() {
2323
// NOCPP-NEXT: %[[ALLOC_SIZE:.*]] = "emitc.constant"() <{value = 999 : index}> : () -> index
2424
// NOCPP-NEXT: %[[ALLOC_TOTAL_SIZE:.*]] = emitc.mul %[[ALLOC]], %[[ALLOC_SIZE]] : (!emitc.size_t, index) -> !emitc.size_t
2525
// NOCPP-NEXT: %[[ALLOC_PTR:.*]] = emitc.call_opaque "malloc"(%[[ALLOC_TOTAL_SIZE]]) : (!emitc.size_t) -> !emitc.ptr<!emitc.opaque<"void">>
26-
// NOCPP-NEXT: %[[ALLOC_CAST:.*]] = emitc.cast %[[ALLOC_PTR]] : !emitc.ptr<!emitc.opaque<"void">> to !emitc.ptr<i32>
26+
// NOCPP-NEXT: %[[ALLOC_CAST:.*]] = emitc.cast %[[ALLOC_PTR]] : !emitc.ptr<!emitc.opaque<"void">> to !emitc.ptr<!emitc.array<999xi32>>
2727
// NOCPP-NEXT: return
2828

2929
func.func @alloc_aligned() {
@@ -37,7 +37,7 @@ func.func @alloc_aligned() {
3737
// CPP-NEXT: %[[ALLOC_TOTAL_SIZE:.*]] = emitc.mul %[[ALLOC]], %[[ALLOC_SIZE]] : (!emitc.size_t, index) -> !emitc.size_t
3838
// CPP-NEXT: %[[ALIGNMENT:.*]] = "emitc.constant"() <{value = 64 : index}> : () -> !emitc.size_t
3939
// CPP-NEXT: %[[ALLOC_PTR:.*]] = emitc.call_opaque "aligned_alloc"(%[[ALIGNMENT]], %[[ALLOC_TOTAL_SIZE]]) : (!emitc.size_t, !emitc.size_t) -> !emitc.ptr<!emitc.opaque<"void">>
40-
// CPP-NEXT: %[[ALLOC_CAST:.*]] = emitc.cast %[[ALLOC_PTR]] : !emitc.ptr<!emitc.opaque<"void">> to !emitc.ptr<f32>
40+
// CPP-NEXT: %[[ALLOC_CAST:.*]] = emitc.cast %[[ALLOC_PTR]] : !emitc.ptr<!emitc.opaque<"void">> to !emitc.ptr<!emitc.array<999xf32>>
4141
// CPP-NEXT: return
4242

4343
// NOCPP-LABEL: alloc_aligned
@@ -46,7 +46,7 @@ func.func @alloc_aligned() {
4646
// NOCPP-NEXT: %[[ALLOC_TOTAL_SIZE:.*]] = emitc.mul %[[ALLOC]], %[[ALLOC_SIZE]] : (!emitc.size_t, index) -> !emitc.size_t
4747
// NOCPP-NEXT: %[[ALIGNMENT:.*]] = "emitc.constant"() <{value = 64 : index}> : () -> !emitc.size_t
4848
// NOCPP-NEXT: %[[ALLOC_PTR:.*]] = emitc.call_opaque "aligned_alloc"(%[[ALIGNMENT]], %[[ALLOC_TOTAL_SIZE]]) : (!emitc.size_t, !emitc.size_t) -> !emitc.ptr<!emitc.opaque<"void">>
49-
// NOCPP-NEXT: %[[ALLOC_CAST:.*]] = emitc.cast %[[ALLOC_PTR]] : !emitc.ptr<!emitc.opaque<"void">> to !emitc.ptr<f32>
49+
// NOCPP-NEXT: %[[ALLOC_CAST:.*]] = emitc.cast %[[ALLOC_PTR]] : !emitc.ptr<!emitc.opaque<"void">> to !emitc.ptr<!emitc.array<999xf32>>
5050
// NOCPP-NEXT: return
5151

5252
func.func @allocating_multi() {
@@ -59,14 +59,14 @@ func.func @allocating_multi() {
5959
// CPP-NEXT: %[[ALLOC_SIZE:.*]] = "emitc.constant"() <{value = 6993 : index}> : () -> index
6060
// CPP-NEXT: %[[ALLOC_TOTAL_SIZE:.*]] = emitc.mul %[[ALLOC]], %[[ALLOC_SIZE]] : (!emitc.size_t, index) -> !emitc.size_t
6161
// CPP-NEXT: %[[ALLOC_PTR:.*]] = emitc.call_opaque "malloc"(%[[ALLOC_TOTAL_SIZE]]) : (!emitc.size_t) -> !emitc.ptr<!emitc.opaque<"void">
62-
// CPP-NEXT: %[[ALLOC_CAST:.*]] = emitc.cast %[[ALLOC_PTR]] : !emitc.ptr<!emitc.opaque<"void">> to !emitc.ptr<i32>
62+
// CPP-NEXT: %[[ALLOC_CAST:.*]] = emitc.cast %[[ALLOC_PTR]] : !emitc.ptr<!emitc.opaque<"void">> to !emitc.ptr<!emitc.array<7x999xi32>>
6363
// CPP-NEXT: return
6464

6565
// NOCPP-LABEL: allocating_multi
6666
// NOCPP-NEXT: %[[ALLOC:.*]] = emitc.call_opaque "sizeof"() {args = [i32]} : () -> !emitc.size_t
6767
// NOCPP-NEXT: %[[ALLOC_SIZE:.*]] = "emitc.constant"() <{value = 6993 : index}> : () -> index
6868
// NOCPP-NEXT: %[[ALLOC_TOTAL_SIZE:.*]] = emitc.mul %[[ALLOC]], %[[ALLOC_SIZE]] : (!emitc.size_t, index) -> !emitc.size_t
6969
// NOCPP-NEXT: %[[ALLOC_PTR:.*]] = emitc.call_opaque "malloc"(%[[ALLOC_TOTAL_SIZE]]) : (!emitc.size_t) -> !emitc.ptr<!emitc.opaque<"void">>
70-
// NOCPP-NEXT: %[[ALLOC_CAST:.*]] = emitc.cast %[[ALLOC_PTR]] : !emitc.ptr<!emitc.opaque<"void">> to !emitc.ptr<i32>
70+
// NOCPP-NEXT: %[[ALLOC_CAST:.*]] = emitc.cast %[[ALLOC_PTR]] : !emitc.ptr<!emitc.opaque<"void">> to !emitc.ptr<!emitc.array<7x999xi32>>
7171
// NOCPP-NEXT: return
7272

mlir/test/Conversion/MemRefToEmitC/memref-to-emitc.mlir

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ func.func @alloca() {
1313
// CHECK-LABEL: memref_store
1414
// CHECK-SAME: %[[buff:.*]]: memref<4x8xf32>, %[[v:.*]]: f32, %[[i:.*]]: index, %[[j:.*]]: index
1515
func.func @memref_store(%buff : memref<4x8xf32>, %v : f32, %i: index, %j: index) {
16-
// CHECK-NEXT: %[[BUFFER:.*]] = builtin.unrealized_conversion_cast %[[buff]] : memref<4x8xf32> to !emitc.array<4x8xf32>
16+
// CHECK-NEXT: %[[BUFFER:.*]] = builtin.unrealized_conversion_cast %[[buff]] : memref<4x8xf32> to !emitc.ptr<!emitc.array<4x8xf32>>
1717

18-
// CHECK-NEXT: %[[SUBSCRIPT:.*]] = emitc.subscript %[[BUFFER]][%[[i]], %[[j]]] : (!emitc.array<4x8xf32>, index, index) -> !emitc.lvalue<f32>
18+
// CHECK-NEXT: %[[APPLY:.+]] = emitc.apply "*"(%[[BUFFER]]) : (!emitc.ptr<!emitc.array<4x8xf32>>) -> !emitc.array<4x8xf32>
19+
// CHECK-NEXT: %[[SUBSCRIPT:.*]] = emitc.subscript %[[APPLY]][%[[i]], %[[j]]] : (!emitc.array<4x8xf32>, index, index) -> !emitc.lvalue<f32>
1920
// CHECK-NEXT: emitc.assign %[[v]] : f32 to %[[SUBSCRIPT]] : <f32>
2021
memref.store %v, %buff[%i, %j] : memref<4x8xf32>
2122
return
@@ -26,9 +27,10 @@ func.func @memref_store(%buff : memref<4x8xf32>, %v : f32, %i: index, %j: index)
2627
// CHECK-LABEL: memref_load
2728
// CHECK-SAME: %[[buff:.*]]: memref<4x8xf32>, %[[i:.*]]: index, %[[j:.*]]: index
2829
func.func @memref_load(%buff : memref<4x8xf32>, %i: index, %j: index) -> f32 {
29-
// CHECK-NEXT: %[[BUFFER:.*]] = builtin.unrealized_conversion_cast %[[buff]] : memref<4x8xf32> to !emitc.array<4x8xf32>
30+
// CHECK-NEXT: %[[BUFFER:.*]] = builtin.unrealized_conversion_cast %[[buff]] : memref<4x8xf32> to !emitc.ptr<!emitc.array<4x8xf32>>
3031

31-
// CHECK-NEXT: %[[SUBSCRIPT:.*]] = emitc.subscript %[[BUFFER]][%[[i]], %[[j]]] : (!emitc.array<4x8xf32>, index, index) -> !emitc.lvalue<f32>
32+
// CHECK-NEXT: %[[APPLY:.+]] = emitc.apply "*"(%[[BUFFER]]) : (!emitc.ptr<!emitc.array<4x8xf32>>) -> !emitc.array<4x8xf32>
33+
// CHECK-NEXT: %[[SUBSCRIPT:.*]] = emitc.subscript %[[APPLY]][%[[i]], %[[j]]] : (!emitc.array<4x8xf32>, index, index) -> !emitc.lvalue<f32>
3234
// CHECK-NEXT: %[[LOAD:.*]] = emitc.load %[[SUBSCRIPT]] : <f32>
3335
%1 = memref.load %buff[%i, %j] : memref<4x8xf32>
3436
// CHECK-NEXT: return %[[LOAD]] : f32

0 commit comments

Comments
 (0)