Skip to content

Commit

Permalink
Sort descriptors as expected by the native module (iree-org#5207)
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-camp authored Mar 24, 2021
1 parent 47183fb commit c8a7b2f
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 40 deletions.
34 changes: 29 additions & 5 deletions iree/compiler/Dialect/VM/Target/C/CModuleTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,18 @@ static LogicalResult buildModuleDescriptors(IREE::VM::ModuleOp &moduleOp,
};

// exports
// TODO: Add sorting the module export table by name. This is already
// supported in the ByteCodeModuleTarget using `llvm::sort`.
std::string exportName = moduleName + "_exports_";
output << "static const iree_vm_native_export_descriptor_t " << exportName
<< "[] = {\n";
for (auto exportOp : moduleOp.getOps<IREE::VM::ExportOp>()) {

// sort export ops
SmallVector<IREE::VM::ExportOp, 4> exportOps(
moduleOp.getOps<IREE::VM::ExportOp>());
llvm::sort(exportOps, [](auto &lhs, auto &rhs) {
return lhs.export_name().compare(rhs.export_name()) < 0;
});

for (auto exportOp : exportOps) {
auto funcOp = symbolTable.lookup<IREE::VM::FuncOp>(exportOp.function_ref());
if (!funcOp) {
return exportOp.emitError("Couldn't find referenced FuncOp");
Expand All @@ -327,7 +333,15 @@ static LogicalResult buildModuleDescriptors(IREE::VM::ModuleOp &moduleOp,
std::string importName = moduleName + "_imports_";
output << "static const iree_vm_native_import_descriptor_t " << importName
<< "[] = {\n";
for (auto importOp : moduleOp.getOps<IREE::VM::ImportOp>()) {

// sort import ops
SmallVector<IREE::VM::ImportOp, 4> importOps(
moduleOp.getOps<IREE::VM::ImportOp>());
llvm::sort(importOps, [](auto &lhs, auto &rhs) {
return lhs.getName().compare(rhs.getName()) < 0;
});

for (auto importOp : importOps) {
output << "{" << printCStringView(importOp.getName().str()) << "},\n";
}
output << "};\n";
Expand All @@ -337,7 +351,17 @@ static LogicalResult buildModuleDescriptors(IREE::VM::ModuleOp &moduleOp,
std::string functionName = moduleName + "_funcs_";
output << "static const iree_vm_native_function_ptr_t " << functionName
<< "[] = {\n";
for (auto funcOp : moduleOp.getOps<IREE::VM::FuncOp>()) {

// sort func ops
SmallVector<IREE::VM::FuncOp, 4> funcOps(moduleOp.getOps<IREE::VM::FuncOp>());
llvm::sort(funcOps, [&moduleOp](auto &lhs, auto &rhs) {
std::string lhsStr =
buildFunctionName(moduleOp, lhs, /*implSufffix=*/false);
std::string rhsStr =
buildFunctionName(moduleOp, rhs, /*implSufffix=*/false);
return lhsStr.compare(rhsStr) < 0;
});
for (auto funcOp : funcOps) {
output << "{"
<< "(iree_vm_native_function_shim_t)";

Expand Down
22 changes: 10 additions & 12 deletions iree/vm/test/arithmetic_ops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,7 @@ vm.module @arithmetic_ops {
// Native integer arithmetic
//===--------------------------------------------------------------------===//

// TODO: The CModuleTarget enforces exports to be ordered.
vm.export @test_add_i32
vm.export @test_and_i32
vm.export @test_div_i32s
vm.export @test_div_i32u
vm.export @test_mul_i32
vm.export @test_not_i32
vm.export @test_or_i32
vm.export @test_rem_i32s
vm.export @test_rem_i32u
vm.export @test_sub_i32
vm.export @test_xor_i32

vm.func @test_add_i32() {
%c1 = vm.const.i32 1 : i32
%c1dno = iree.do_not_optimize(%c1) : i32
Expand All @@ -26,6 +14,7 @@ vm.module @arithmetic_ops {
vm.return
}

vm.export @test_sub_i32
vm.func @test_sub_i32() {
%c1 = vm.const.i32 3 : i32
%c1dno = iree.do_not_optimize(%c1) : i32
Expand All @@ -37,6 +26,7 @@ vm.module @arithmetic_ops {
vm.return
}

vm.export @test_mul_i32
vm.func @test_mul_i32() {
%c1 = vm.const.i32 2 : i32
%c1dno = iree.do_not_optimize(%c1) : i32
Expand All @@ -46,6 +36,7 @@ vm.module @arithmetic_ops {
vm.return
}

vm.export @test_div_i32s
vm.func @test_div_i32s() {
%c1 = vm.const.i32 4 : i32
%c1dno = iree.do_not_optimize(%c1) : i32
Expand All @@ -57,6 +48,7 @@ vm.module @arithmetic_ops {
vm.return
}

vm.export @test_div_i32u
vm.func @test_div_i32u() {
%c1 = vm.const.i32 4 : i32
%c1dno = iree.do_not_optimize(%c1) : i32
Expand All @@ -68,6 +60,7 @@ vm.module @arithmetic_ops {
vm.return
}

vm.export @test_rem_i32s
vm.func @test_rem_i32s() {
%c1 = vm.const.i32 -3 : i32
%c1dno = iree.do_not_optimize(%c1) : i32
Expand All @@ -79,6 +72,7 @@ vm.module @arithmetic_ops {
vm.return
}

vm.export @test_rem_i32u
vm.func @test_rem_i32u() {
%c1 = vm.const.i32 3 : i32
%c1dno = iree.do_not_optimize(%c1) : i32
Expand All @@ -90,6 +84,7 @@ vm.module @arithmetic_ops {
vm.return
}

vm.export @test_not_i32
vm.func @test_not_i32() {
%c1 = vm.const.i32 0 : i32
%c1dno = iree.do_not_optimize(%c1) : i32
Expand All @@ -99,6 +94,7 @@ vm.module @arithmetic_ops {
vm.return
}

vm.export @test_and_i32
vm.func @test_and_i32() {
%c1 = vm.const.i32 5 : i32
%c1dno = iree.do_not_optimize(%c1) : i32
Expand All @@ -110,6 +106,7 @@ vm.module @arithmetic_ops {
vm.return
}

vm.export @test_or_i32
vm.func @test_or_i32() {
%c1 = vm.const.i32 5 : i32
%c1dno = iree.do_not_optimize(%c1) : i32
Expand All @@ -121,6 +118,7 @@ vm.module @arithmetic_ops {
vm.return
}

vm.export @test_xor_i32
vm.func @test_xor_i32() {
%c1 = vm.const.i32 5 : i32
%c1dno = iree.do_not_optimize(%c1) : i32
Expand Down
22 changes: 10 additions & 12 deletions iree/vm/test/arithmetic_ops_i64.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,7 @@ vm.module @arithmetic_ops_i64 {
// ExtI64: Native integer arithmetic
//===--------------------------------------------------------------------===//

// TODO: The CModuleTarget enforces exports to be ordered.
vm.export @test_add_i64
vm.export @test_and_i64
vm.export @test_div_i64s
vm.export @test_div_i64u
vm.export @test_mul_i64
vm.export @test_not_i64
vm.export @test_or_i64
vm.export @test_rem_i64s
vm.export @test_rem_i64u
vm.export @test_sub_i64
vm.export @test_xor_i64

vm.func @test_add_i64() {
%c1 = vm.const.i64 1 : i64
%c1dno = iree.do_not_optimize(%c1) : i64
Expand All @@ -26,6 +14,7 @@ vm.module @arithmetic_ops_i64 {
vm.return
}

vm.export @test_sub_i64
vm.func @test_sub_i64() {
%c1 = vm.const.i64 3 : i64
%c1dno = iree.do_not_optimize(%c1) : i64
Expand All @@ -37,6 +26,7 @@ vm.module @arithmetic_ops_i64 {
vm.return
}

vm.export @test_mul_i64
vm.func @test_mul_i64() {
%c1 = vm.const.i64 2 : i64
%c1dno = iree.do_not_optimize(%c1) : i64
Expand All @@ -46,6 +36,7 @@ vm.module @arithmetic_ops_i64 {
vm.return
}

vm.export @test_div_i64s
vm.func @test_div_i64s() {
%c1 = vm.const.i64 4 : i64
%c1dno = iree.do_not_optimize(%c1) : i64
Expand All @@ -57,6 +48,7 @@ vm.module @arithmetic_ops_i64 {
vm.return
}

vm.export @test_div_i64u
vm.func @test_div_i64u() {
%c1 = vm.const.i64 4 : i64
%c1dno = iree.do_not_optimize(%c1) : i64
Expand All @@ -68,6 +60,7 @@ vm.module @arithmetic_ops_i64 {
vm.return
}

vm.export @test_rem_i64s
vm.func @test_rem_i64s() {
%c1 = vm.const.i64 -3 : i64
%c1dno = iree.do_not_optimize(%c1) : i64
Expand All @@ -79,6 +72,7 @@ vm.module @arithmetic_ops_i64 {
vm.return
}

vm.export @test_rem_i64u
vm.func @test_rem_i64u() {
%c1 = vm.const.i64 3 : i64
%c1dno = iree.do_not_optimize(%c1) : i64
Expand All @@ -90,6 +84,7 @@ vm.module @arithmetic_ops_i64 {
vm.return
}

vm.export @test_not_i64
vm.func @test_not_i64() {
%c1 = vm.const.i64 0 : i64
%c1dno = iree.do_not_optimize(%c1) : i64
Expand All @@ -99,6 +94,7 @@ vm.module @arithmetic_ops_i64 {
vm.return
}

vm.export @test_and_i64
vm.func @test_and_i64() {
%c1 = vm.const.i64 5 : i64
%c1dno = iree.do_not_optimize(%c1) : i64
Expand All @@ -110,6 +106,7 @@ vm.module @arithmetic_ops_i64 {
vm.return
}

vm.export @test_or_i64
vm.func @test_or_i64() {
%c1 = vm.const.i64 5 : i64
%c1dno = iree.do_not_optimize(%c1) : i64
Expand All @@ -121,6 +118,7 @@ vm.module @arithmetic_ops_i64 {
vm.return
}

vm.export @test_xor_i64
vm.func @test_xor_i64() {
%c1 = vm.const.i64 5 : i64
%c1dno = iree.do_not_optimize(%c1) : i64
Expand Down
2 changes: 0 additions & 2 deletions iree/vm/test/assignment_ops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ vm.module @assignment_ops {
// Conditional assignment
//===--------------------------------------------------------------------===//

// TODO: The CModuleTarget enforces exports to be ordered.
vm.export @test_select_i32

vm.func @test_select_i32() {
%c0 = vm.const.i32 0 : i32
%c0dno = iree.do_not_optimize(%c0) : i32
Expand Down
2 changes: 0 additions & 2 deletions iree/vm/test/assignment_ops_i64.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ vm.module @assignment_ops_i64 {
// ExtI64: Conditional assignment
//===--------------------------------------------------------------------===//

// TODO: The CModuleTarget enforces exports to be ordered.
vm.export @test_select_i64

vm.func @test_select_i64() {
%c0 = vm.const.i32 0 : i32
%c0dno = iree.do_not_optimize(%c0) : i32
Expand Down
6 changes: 2 additions & 4 deletions iree/vm/test/conversion_ops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ vm.module @conversion_ops {
// Casting and type conversion/emulation
//===----------------------------------------------------------------------===//

// TODO: The CModuleTarget enforces exports to be ordered.
vm.export @test_trunc_i32_i16
vm.export @test_trunc_i32_i8

vm.func @test_trunc_i32_i8() {
%c1 = vm.const.i32 2147483647 : i32
%c1dno = iree.do_not_optimize(%c1) : i32
Expand All @@ -17,7 +14,8 @@ vm.module @conversion_ops {
vm.return
}

vm.func @test_trunc_i32_i16() {
vm.export @test_trunc_i32_i16
vm.func @test_trunc_i32_i16() {
%c1 = vm.const.i32 2147483647 : i32
%c1dno = iree.do_not_optimize(%c1) : i32
%v = vm.trunc.i32.i16 %c1dno : i32 -> i32
Expand Down
1 change: 0 additions & 1 deletion iree/vm/test/conversion_ops_i64.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ vm.module @conversion_ops_i64 {
//===----------------------------------------------------------------------===//

vm.export @test_trunc_i64_i32

vm.func @test_trunc_i64_i32() {
%c1 = vm.const.i64 9223372036854775807 : i64
%c1dno = iree.do_not_optimize(%c1) : i64
Expand Down
2 changes: 1 addition & 1 deletion iree/vm/test/shift_ops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ vm.module @shift_ops {
%c1dno = iree.do_not_optimize(%c1) : i32
%v = vm.shr.i32.s %c1dno, 2 : i32
%c2 = vm.const.i32 -1 : i32
vm.check.eq %v, %c2, "-1>>-1=-1" : i32
vm.check.eq %v, %c2, "-1>>2=-1" : i32
vm.return
}

Expand Down
2 changes: 1 addition & 1 deletion iree/vm/test/shift_ops_i64.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ vm.module @shift_ops_i64 {
%c1dno = iree.do_not_optimize(%c1) : i64
%v = vm.shr.i64.s %c1dno, 2 : i64
%c2 = vm.const.i64 -1 : i64
vm.check.eq %v, %c2, "-1>>-1=-1" : i64
vm.check.eq %v, %c2, "-1>>2=-1" : i64
vm.return
}

Expand Down

0 comments on commit c8a7b2f

Please sign in to comment.