Skip to content

Commit 995b14c

Browse files
authored
[MLIR] Target/Cpp: Translate emitc.array (#29)
Reviewers: TinaAMD Reviewed By: TinaAMD Pull Request: #115
1 parent 17dab4e commit 995b14c

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

mlir/lib/Target/Cpp/TranslateToCpp.cpp

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ struct CppEmitter {
9696
LogicalResult emitVariableDeclaration(OpResult result,
9797
bool trailingSemicolon);
9898

99+
/// Emits a declaration of a variable with the given type and name.
100+
LogicalResult emitVariableDeclaration(Location loc, Type type,
101+
StringRef name);
102+
99103
/// Emits the variable declaration and assignment prefix for 'op'.
100104
/// - emits separate variable followed by std::tie for multi-valued operation;
101105
/// - emits single type followed by variable for single result;
@@ -623,14 +627,12 @@ static LogicalResult printOperation(CppEmitter &emitter,
623627
os << " " << functionOp.getName();
624628

625629
os << "(";
626-
if (failed(interleaveCommaWithError(
627-
functionOp.getArguments(), os,
628-
[&](BlockArgument arg) -> LogicalResult {
629-
if (failed(emitter.emitType(functionOp.getLoc(), arg.getType())))
630-
return failure();
631-
os << " " << emitter.getOrCreateName(arg);
632-
return success();
633-
})))
630+
if (failed(interleaveCommaWithError(functionOp.getArguments(), os,
631+
[&](BlockArgument arg) -> LogicalResult {
632+
return emitter.emitVariableDeclaration(
633+
functionOp.getLoc(), arg.getType(),
634+
emitter.getOrCreateName(arg));
635+
})))
634636
return failure();
635637
os << ") {\n";
636638
os.indent();
@@ -893,9 +895,10 @@ LogicalResult CppEmitter::emitVariableDeclaration(OpResult result,
893895
return result.getDefiningOp()->emitError(
894896
"result variable for the operation already declared");
895897
}
896-
if (failed(emitType(result.getOwner()->getLoc(), result.getType())))
898+
if (failed(emitVariableDeclaration(result.getOwner()->getLoc(),
899+
result.getType(),
900+
getOrCreateName(result))))
897901
return failure();
898-
os << " " << getOrCreateName(result);
899902
if (trailingSemicolon)
900903
os << ";\n";
901904
return success();
@@ -977,6 +980,23 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
977980
return success();
978981
}
979982

983+
LogicalResult CppEmitter::emitVariableDeclaration(Location loc, Type type,
984+
StringRef name) {
985+
if (auto arrType = dyn_cast<emitc::ArrayType>(type)) {
986+
if (failed(emitType(loc, arrType.getElementType())))
987+
return failure();
988+
os << " " << name;
989+
for (auto dim : arrType.getShape()) {
990+
os << "[" << dim << "]";
991+
}
992+
return success();
993+
}
994+
if (failed(emitType(loc, type)))
995+
return failure();
996+
os << " " << name;
997+
return success();
998+
}
999+
9801000
LogicalResult CppEmitter::emitType(Location loc, Type type) {
9811001
if (auto iType = dyn_cast<IntegerType>(type)) {
9821002
switch (iType.getWidth()) {

mlir/test/Target/Cpp/common-cpp.mlir

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,8 @@ func.func @apply(%arg0: i32) -> !emitc.ptr<i32> {
8989
%1 = emitc.apply "*"(%0) : (!emitc.ptr<i32>) -> (i32)
9090
return %0 : !emitc.ptr<i32>
9191
}
92+
93+
// CHECK: void array_type(int32_t v1[3], float v2[10][20])
94+
func.func @array_type(%arg0: !emitc.array<3xi32>, %arg1: !emitc.array<10x20xf32>) {
95+
return
96+
}

mlir/test/Target/Cpp/variable.mlir

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ func.func @emitc_variable() {
99
%c4 = "emitc.variable"(){value = 255 : ui8} : () -> ui8
1010
%c5 = "emitc.variable"(){value = #emitc.opaque<"">} : () -> !emitc.ptr<i32>
1111
%c6 = "emitc.variable"(){value = #emitc.opaque<"NULL">} : () -> !emitc.ptr<i32>
12+
%c7 = "emitc.variable"(){value = #emitc.opaque<"">} : () -> !emitc.array<3x7xi32>
1213
return
1314
}
1415
// CPP-DEFAULT: void emitc_variable() {
@@ -19,6 +20,7 @@ func.func @emitc_variable() {
1920
// CPP-DEFAULT-NEXT: uint8_t [[V4:[^ ]*]] = 255;
2021
// CPP-DEFAULT-NEXT: int32_t* [[V5:[^ ]*]];
2122
// CPP-DEFAULT-NEXT: int32_t* [[V6:[^ ]*]] = NULL;
23+
// CPP-DEFAULT-NEXT: int32_t [[V7:[^ ]*]][3][7];
2224

2325
// CPP-DECLTOP: void emitc_variable() {
2426
// CPP-DECLTOP-NEXT: int32_t [[V0:[^ ]*]];
@@ -28,6 +30,7 @@ func.func @emitc_variable() {
2830
// CPP-DECLTOP-NEXT: uint8_t [[V4:[^ ]*]];
2931
// CPP-DECLTOP-NEXT: int32_t* [[V5:[^ ]*]];
3032
// CPP-DECLTOP-NEXT: int32_t* [[V6:[^ ]*]];
33+
// CPP-DECLTOP-NEXT: int32_t [[V7:[^ ]*]][3][7];
3134
// CPP-DECLTOP-NEXT: ;
3235
// CPP-DECLTOP-NEXT: [[V1]] = 42;
3336
// CPP-DECLTOP-NEXT: [[V2]] = -1;

0 commit comments

Comments
 (0)