Skip to content

Commit

Permalink
Add lit test for control flow conversion
Browse files Browse the repository at this point in the history
Additionally fix several broken conversion tests
  • Loading branch information
simon-camp committed Mar 24, 2021
1 parent 18f685c commit a9f17ff
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 8 deletions.
2 changes: 2 additions & 0 deletions iree/compiler/Dialect/VM/Target/C/CModuleTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ static LogicalResult translateReturnOpToC(
static LogicalResult translateOpToC(Operation &op,
mlir::emitc::CppEmitter &emitter,
SmallVector<std::string, 4> resultNames) {
if (auto branchOp = dyn_cast<IREE::VM::BranchOp>(op))
return translateBranchOp(branchOp, emitter);
if (auto callOp = dyn_cast<IREE::VM::CallOp>(op))
return translateCallOpToC(callOp, emitter);
if (auto condBranchOp = dyn_cast<IREE::VM::CondBranchOp>(op))
Expand Down
6 changes: 4 additions & 2 deletions iree/compiler/Dialect/VM/Target/C/test/add.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
vm.module @add_module {
// CHECK: iree_status_t add_module_add_1_impl(int32_t v1, int32_t v2, int32_t *out0, int32_t *out1, add_module_state_t* state) {
vm.func @add_1(%arg0 : i32, %arg1 : i32) -> (i32, i32) {
// CHECK-NEXT: int32_t v3 = vm_add_i32(v1, v2);
// CHECK-NEXT: int32_t v3;
// CHECK-NEXT: int32_t v4;
// CHECK-NEXT: v3 = vm_add_i32(v1, v2);
%0 = vm.add.i32 %arg0, %arg1 : i32
// CHECK-NEXT: int32_t v4 = vm_add_i32(v3, v3);
// CHECK-NEXT: v4 = vm_add_i32(v3, v3);
%1 = vm.add.i32 %0, %0 : i32
// CHECK-NEXT: *out0 = v3;
// CHECK-NEXT: *out1 = v4;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ vm.module @calling_convention_test {

// CHECK: iree_status_t calling_convention_test_no_in_i32_return_impl(int32_t *out0, calling_convention_test_state_t* state) {
vm.func @no_in_i32_return() -> (i32) {
// CHECK-NEXT: int32_t v1 = vm_const_i32(32);
// CHECK-NEXT: int32_t v1;
// CHECK-NEXT: v1 = 32;
%0 = vm.const.i32 32 : i32
// CHECK-NEXT: *out0 = v1;
// CHECK-NEXT: return iree_ok_status();
Expand All @@ -25,7 +26,8 @@ vm.module @calling_convention_test {

// CHECK: iree_status_t calling_convention_test_i32_in_i32_return_impl(int32_t v1, int32_t *out0, calling_convention_test_state_t* state) {
vm.func @i32_in_i32_return(%arg0 : i32) -> (i32) {
// CHECK-NEXT: int32_t v2 = vm_const_i32(32);
// CHECK-NEXT: int32_t v2;
// CHECK-NEXT: v2 = 32;
%0 = vm.const.i32 32 : i32
// CHECK-NEXT: *out0 = v2;
// CHECK-NEXT: return iree_ok_status();
Expand Down
44 changes: 44 additions & 0 deletions iree/compiler/Dialect/VM/Target/C/test/control_flow.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// RUN: iree-translate -iree-vm-ir-to-c-module -iree-vm-c-module-optimize=false %s | IreeFileCheck %s

vm.module @control_flow_module {
vm.func @control_flow_test(%a: i32, %cond: i32) -> i32 {
vm.cond_br %cond, ^bb1, ^bb2
^bb1:
vm.br ^bb3(%a: i32)
^bb2:
%b = vm.add.i32 %a, %a : i32
vm.br ^bb3(%b: i32)
^bb3(%c: i32):
vm.br ^bb4(%c, %a : i32, i32)
^bb4(%d : i32, %e : i32):
%0 = vm.add.i32 %d, %e : i32
vm.return %0 : i32
}
}
// CHECK: iree_status_t control_flow_module_control_flow_test_impl(int32_t [[A:[^ ]*]], int32_t [[COND:[^ ]*]], int32_t *[[RESULT:[^ ]*]], control_flow_module_state_t* [[STATE:[^ ]*]]) {
// CHECK-NEXT: int32_t [[B:[^ ]*]];
// CHECK-NEXT: int32_t [[V0:[^ ]*]];
// CHECK-NEXT: int32_t [[C:[^ ]*]];
// CHECK-NEXT: int32_t [[D:[^ ]*]];
// CHECK-NEXT: int32_t [[E:[^ ]*]];
// CHECK-NEXT: [[BB0:[^ ]*]]:
// CHECK-NEXT: if ([[COND]]) {
// CHECK-NEXT: goto [[BB1:[^ ]*]];
// CHECK-NEXT: } else {
// CHECK-NEXT: goto [[BB2:[^ ]*]];
// CHECK-NEXT: }
// CHECK-NEXT: [[BB1]]:
// CHECK-NEXT: [[C]] = [[A]];
// CHECK-NEXT: goto [[BB3:[^ ]*]];
// CHECK-NEXT: [[BB2]]:
// CHECK-NEXT: [[B]] = vm_add_i32([[A]], [[A]]);
// CHECK-NEXT: [[C]] = [[B]];
// CHECK-NEXT: goto [[BB3]];
// CHECK-NEXT: [[BB3]]:
// CHECK-NEXT: [[D]] = [[C]];
// CHECK-NEXT: [[E]] = [[A]];
// CHECK-NEXT: goto [[BB4:[^ ]*]];
// CHECK-NEXT: [[BB4]]:
// CHECK-NEXT: [[V0]] = vm_add_i32([[D]], [[E]]);
// CHECK-NEXT: *[[RESULT]] = [[V0]];
// CHECK-NEXT: return iree_ok_status();
12 changes: 8 additions & 4 deletions iree/compiler/Dialect/VM/Target/C/test/global_ops.mlir
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// RUN: iree-translate -iree-vm-ir-to-c-module %s | IreeFileCheck %s
// RUN: iree-translate -iree-vm-ir-to-c-module -iree-vm-c-module-optimize=false %s | IreeFileCheck %s

vm.module @global_ops {
// check the generated state struct
// CHECK-LABEL: struct global_ops_state_s {
// CHECK-NEXT: iree_allocator_t allocator;
// CHECK-NEXT: uint8_t rwdata[8];
// CHECK-NEXT: iree_vm_ref_t refs[0];
// CHECK-NEXT: };

vm.global.i32 @c42 42 : i32
Expand All @@ -13,19 +14,22 @@ vm.module @global_ops {
vm.export @test_global_load_i32
// CHECK-LABEL: iree_status_t global_ops_test_global_load_i32_impl(
vm.func @test_global_load_i32() -> i32 {
// CHECK-NEXT: int32_t v1 = vm_global_load_i32(state->rwdata, 0);
// CHECK-NEXT: int32_t v1;
// CHECK-NEXT: v1 = vm_global_load_i32(state->rwdata, 0);
%value = vm.global.load.i32 @c42 : i32
vm.return %value : i32
}

vm.export @test_global_store_i32
// CHECK-LABEL: iree_status_t global_ops_test_global_store_i32_impl(
vm.func @test_global_store_i32() -> i32 {
// CHECK-NEXT: int32_t v1 = vm_const_i32(17);
// CHECK-NEXT: int32_t v1;
// CHECK-NEXT: int32_t v2;
// CHECK-NEXT: v1 = 17;
%c17 = vm.const.i32 17 : i32
// CHECK-NEXT: vm_global_store_i32(state->rwdata, 4, v1);
vm.global.store.i32 %c17, @c107_mut : i32
// CHECK-NEXT: int32_t v2 = vm_global_load_i32(state->rwdata, 4);
// CHECK-NEXT: v2 = vm_global_load_i32(state->rwdata, 4);
%value = vm.global.load.i32 @c107_mut : i32
vm.return %value : i32
}
Expand Down

0 comments on commit a9f17ff

Please sign in to comment.