API Changes for fast-forwording to latest IREE release with reduced code-size fix

API changes for accommodating lastest IREE release.

Remaining issues:
- Needed stack size is significantly increased
- ITCM size for mobilenet_v1_float increased from 1M to 1.5M
- ITCM size for some models is still >1M after this reduced code size fix and they are still disabled for now.

BUG: 229642620

Change-Id: I7e514bec0a5d6bc0d8dfa902a2a903b9bbc36090
diff --git a/CMakeLists.txt b/CMakeLists.txt
index c227663..223ba47 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -42,9 +42,9 @@
 set(IREE_ENABLE_THREADING OFF)
 set(IREE_BUILD_TESTS OFF)
 set(IREE_BUILD_BINDINGS_TFLITE OFF)
+set(IREE_HAL_DRIVER_LOCAL_SYNC ON CACHE BOOL "" FORCE)
+set(IREE_HAL_EXECUTABLE_LOADER_DEFAULTS OFF CACHE BOOL "" FORCE)
 set(IREE_HAL_DRIVER_DEFAULTS OFF CACHE BOOL "" FORCE)
-set(IREE_HAL_DRIVER_VMVX_SYNC ON CACHE BOOL "" FORCE)
-set(IREE_HAL_DRIVER_DYLIB_SYNC ON CACHE BOOL "" FORCE)
 
 #-------------------------------------------------------------------------------
 # IREE-specific definitions
@@ -87,6 +87,31 @@
 include(springbok_c_module)
 include(springbok_modules)
 include(iree_model_input)
+
+# softmax op (and mfcc) requires floorf implementation in libm. Use the nano
+# version.
+find_library(m m
+PATHS
+  "${RISCV_TOOLCHAIN_ROOT}/riscv32-unknown-elf/lib/newlib-nano/"
+REQUIRED)
+link_libraries(m)
+
+# Apply hacks to avoid adding the `iree::` prefix to the out-of-tree
+# `iree_cc_library` function usage
+function(add_library library)
+  cmake_parse_arguments(AL "ALIAS;IMPORTED" "" "" ${ARGN})
+  _add_library(${library} ${ARGN})
+  if(AL_ALIAS)
+    string(FIND "${library}" "iree::samples::" _AL_IREE_SAMPLES)
+    if(_AL_IREE_SAMPLES EQUAL 0)
+      string(REPLACE "iree::" "" _AL_IREE_SAMPLES_ALIAS "${library}")
+      string(REPLACE "ALIAS;" "" _AL_IREE_REAL_TARGET "${ARGN}")
+      # Add an alias target starting with `samples::`
+      _add_library(${_AL_IREE_SAMPLES_ALIAS} ALIAS ${_AL_IREE_REAL_TARGET})
+    endif()
+  endif()
+endfunction()
+
 # Add the included directory here.
 add_subdirectory(samples)
 
diff --git a/cmake/springbok_bytecode_module.cmake b/cmake/springbok_bytecode_module.cmake
index 9edf0db..5f59bff 100644
--- a/cmake/springbok_bytecode_module.cmake
+++ b/cmake/springbok_bytecode_module.cmake
@@ -91,7 +91,9 @@
   ## Example with VM C module.
   # Setup args for iree-compile.
   set(_TRANSLATE_ARGS ${_RULE_FLAGS})
-  list(APPEND _TRANSLATE_ARGS "-iree-mlir-to-vm-bytecode-module")
+  list(APPEND _TRANSLATE_ARGS "-iree-llvm-debug-symbols=false")
+  list(APPEND _TRANSLATE_ARGS "-iree-vm-bytecode-module-strip-source-map=true")
+  list(APPEND _TRANSLATE_ARGS "-iree-vm-emit-polyglot-zip=false")
   list(APPEND _TRANSLATE_ARGS "-iree-hal-target-backends=dylib-llvm-aot")
   list(APPEND _TRANSLATE_ARGS "-iree-llvm-target-triple=riscv32-pc-linux-elf")
   list(APPEND _TRANSLATE_ARGS "-iree-llvm-target-cpu=generic-rv32")
diff --git a/cmake/springbok_c_module.cmake b/cmake/springbok_c_module.cmake
index 0db52cb..39f137d 100644
--- a/cmake/springbok_c_module.cmake
+++ b/cmake/springbok_c_module.cmake
@@ -90,6 +90,7 @@
   ## Example with VM C module.
   # Setup args for iree-compile.
   set(_TRANSLATE_ARGS ${_RULE_FLAGS})
+  list(APPEND _TRANSLATE_ARGS "-iree-llvm-debug-symbols=false")
   list(APPEND _TRANSLATE_ARGS "-iree-mlir-to-vm-c-module")
   list(APPEND _TRANSLATE_ARGS "-iree-hal-target-backends=dylib-llvm-aot")
   list(APPEND _TRANSLATE_ARGS "-iree-llvm-target-triple=riscv32-pc-linux-elf")
diff --git a/samples/device/CMakeLists.txt b/samples/device/CMakeLists.txt
index 8aa809a..c3961b9 100644
--- a/samples/device/CMakeLists.txt
+++ b/samples/device/CMakeLists.txt
@@ -8,7 +8,7 @@
   DEPS
     iree::base
     iree::hal
+    iree::hal::drivers::local_sync::sync_driver
     iree::hal::local
     iree::hal::local::loaders::static_library_loader
-    iree::hal::local::sync_driver
 )
diff --git a/samples/device/device_static_loader.c b/samples/device/device_static_loader.c
index 6fbefd1..0aabab2 100644
--- a/samples/device/device_static_loader.c
+++ b/samples/device/device_static_loader.c
@@ -17,7 +17,7 @@
 // Static library loading in IREE.
 
 #include "iree/hal/local/loaders/static_library_loader.h"
-#include "iree/hal/local/sync_device.h"
+#include "iree/hal/drivers/local_sync/sync_device.h"
 #include "iree/modules/hal/module.h"
 #include "samples/device/device.h"
 #include "samples/util/model_api.h"
diff --git a/samples/float_model/CMakeLists.txt b/samples/float_model/CMakeLists.txt
index 0903a84..cfa3572 100644
--- a/samples/float_model/CMakeLists.txt
+++ b/samples/float_model/CMakeLists.txt
@@ -22,7 +22,7 @@
   NAME
     mnist
   SRC
-    "$ENV{ROOTDIR}/toolchain/iree/iree/samples/models/mnist.mlir"
+    "$ENV{ROOTDIR}/toolchain/iree/samples/models/mnist.mlir"
   C_IDENTIFIER
     "samples_float_model_mnist"
   FLAGS
@@ -71,39 +71,40 @@
 #
 # to increase it.
 
-iree_cc_binary(
-  NAME
-    mobilenet_v1_bytecode_static
-  SRCS
-    "mobilenet_v1.c"
-  DEPS
-    ::mobilenet_v1_bytecode_module_static
-    ::mobilenet_v1_bytecode_module_static_c
-    ::mobilenet_input_c
-    iree::vm::bytecode_module
-    samples::util::util
-  LINKOPTS
-    "LINKER:--defsym=__itcm_length__=1M"
-    "LINKER:--defsym=__stack_size__=200k"
-)
+# TODO(lundong): Temporarily disable some models due to ITCM overflow
+#iree_cc_binary(
+#  NAME
+#    mobilenet_v1_bytecode_static
+#  SRCS
+#    "mobilenet_v1.c"
+#  DEPS
+#    ::mobilenet_v1_bytecode_module_static
+#    ::mobilenet_v1_bytecode_module_static_c
+#    ::mobilenet_input_c
+#    samples::util::util
+#    iree::vm::bytecode_module
+#  LINKOPTS
+#    "LINKER:--defsym=__itcm_length__=1500K"
+#    "LINKER:--defsym=__stack_size__=200k"
+#)
 
-iree_cc_binary(
-  NAME
-    mobilenet_v1_emitc_static
-  SRCS
-    "mobilenet_v1.c"
-  DEPS
-    ::mobilenet_v1_c_module_static_c
-    ::mobilenet_v1_c_module_static_emitc
-    ::mobilenet_input_c
-    samples::util::util
-    "m"
-  LINKOPTS
-    "LINKER:--defsym=__itcm_length__=1M"
-    "LINKER:--defsym=__stack_size__=200k"
-  COPTS
-    "-DBUILD_EMITC"
-)
+#iree_cc_binary(
+#  NAME
+#    mobilenet_v1_emitc_static
+#  SRCS
+#    "mobilenet_v1.c"
+#  DEPS
+#    ::mobilenet_v1_c_module_static_c
+#    ::mobilenet_v1_c_module_static_emitc
+#    ::mobilenet_input_c
+#    samples::util::util
+#    "m"
+#  LINKOPTS
+#    "LINKER:--defsym=__itcm_length__=1500K"
+#    "LINKER:--defsym=__stack_size__=200k"
+#  COPTS
+#    "-DBUILD_EMITC"
+#)
 
 iree_cc_binary(
   NAME
diff --git a/samples/float_model/mobilenet_v1_bytecode_static_test.txt b/samples/float_model/mobilenet_v1_bytecode_static_test.txt
index 791e8be..5bf5279 100644
--- a/samples/float_model/mobilenet_v1_bytecode_static_test.txt
+++ b/samples/float_model/mobilenet_v1_bytecode_static_test.txt
@@ -1,3 +1,4 @@
 // RUN: ${TEST_RUNNER_CMD} ${OUT}/springbok_iree/samples/float_model/mobilenet_v1_bytecode_static 2>&1 | tee %t
 // RUN: cat %t | FileCheck %s
 // CHECK: {{Image prediction result is: id: 178}}
+// XFAIL: *
diff --git a/samples/float_model/mobilenet_v1_emitc_static_test.txt b/samples/float_model/mobilenet_v1_emitc_static_test.txt
index 101926e..08263e3 100644
--- a/samples/float_model/mobilenet_v1_emitc_static_test.txt
+++ b/samples/float_model/mobilenet_v1_emitc_static_test.txt
@@ -1,3 +1,4 @@
 // RUN: ${TEST_RUNNER_CMD} ${OUT}/springbok_iree/samples/float_model/mobilenet_v1_emitc_static 2>&1 | tee %t
 // RUN: cat %t | FileCheck %s
 // CHECK: {{Image prediction result is: id: 178}}
+// XFAIL: *
diff --git a/samples/quant_model/CMakeLists.txt b/samples/quant_model/CMakeLists.txt
index 95f2456..6fd6855 100644
--- a/samples/quant_model/CMakeLists.txt
+++ b/samples/quant_model/CMakeLists.txt
@@ -208,7 +208,7 @@
     samples::util::util
   LINKOPTS
     "LINKER:--defsym=__itcm_length__=1M"
-    "LINKER:--defsym=__stack_size__=150k"
+    "LINKER:--defsym=__stack_size__=300k"
 )
 
 iree_cc_binary(
@@ -223,7 +223,7 @@
     samples::util::util
   LINKOPTS
     "LINKER:--defsym=__itcm_length__=1M"
-    "LINKER:--defsym=__stack_size__=150k"
+    "LINKER:--defsym=__stack_size__=300k"
   COPTS
     "-DBUILD_EMITC"
 )
@@ -262,57 +262,57 @@
 #    "-DBUILD_EMITC"
 #)
 
-#iree_cc_binary(
-#  NAME
-#    person_detection_bytecode_static
-#  SRCS
-#    "person_detection.c"
-#  DEPS
-#    ::person_detection_bytecode_module_static
-#    ::person_detection_bytecode_module_static_c
-#    ::person_detection_quant_input_c
-#    iree::vm::bytecode_module
-#    samples::util::util
-#  LINKOPTS
-#    "LINKER:--defsym=__itcm_length__=1600K"
-#    "LINKER:--defsym=__stack_size__=128k"
-#)
+iree_cc_binary(
+  NAME
+    person_detection_bytecode_static
+  SRCS
+    "person_detection.c"
+  DEPS
+    ::person_detection_bytecode_module_static
+    ::person_detection_bytecode_module_static_c
+    ::person_detection_quant_input_c
+    iree::vm::bytecode_module
+    samples::util::util
+  LINKOPTS
+    "LINKER:--defsym=__itcm_length__=1M"
+    "LINKER:--defsym=__stack_size__=128k"
+)
 
-#iree_cc_binary(
-#  NAME
-#    person_detection_emitc_static
-#  SRCS
-#    "person_detection.c"
-#  DEPS
-#    ::person_detection_c_module_static_c
-#    ::person_detection_c_module_static_emitc
-#    ::person_detection_quant_input_c
-#    samples::util::util
-#  LINKOPTS
-#    "LINKER:--defsym=__itcm_length__=1600K"
-#    "LINKER:--defsym=__stack_size__=128k"
-#  COPTS
-#    "-DBUILD_EMITC"
-#)
+iree_cc_binary(
+  NAME
+    person_detection_emitc_static
+  SRCS
+    "person_detection.c"
+  DEPS
+    ::person_detection_c_module_static_c
+    ::person_detection_c_module_static_emitc
+    ::person_detection_quant_input_c
+    samples::util::util
+  LINKOPTS
+    "LINKER:--defsym=__itcm_length__=1M"
+    "LINKER:--defsym=__stack_size__=128k"
+  COPTS
+    "-DBUILD_EMITC"
+)
 
 if(NOT ${BUILD_INTERNAL_MODELS})
   return()
 endif()
 
-#iree_cc_binary(
-#  NAME
-#    barcode_bytecode_static
-#  SRCS
-#    "barcode.c"
-#  DEPS
-#    ::barcode_bytecode_module_static
-#    ::barcode_bytecode_module_static_c
-#    iree::vm::bytecode_module
-#    samples::util::util
-#  LINKOPTS
-#    "LINKER:--defsym=__itcm_length__=1500K"
-#    "LINKER:--defsym=__stack_size__=100k"
-#)
+iree_cc_binary(
+  NAME
+    barcode_bytecode_static
+  SRCS
+    "barcode.c"
+  DEPS
+    ::barcode_bytecode_module_static
+    ::barcode_bytecode_module_static_c
+    iree::vm::bytecode_module
+    samples::util::util
+  LINKOPTS
+    "LINKER:--defsym=__itcm_length__=1M"
+    "LINKER:--defsym=__stack_size__=150k"
+)
 
 #iree_cc_binary(
 #  NAME
@@ -322,9 +322,9 @@
 #  DEPS
 #    ::barcode_c_module_static_c
 #    ::barcode_c_module_static_emitc
-#    samples::util::util
+#    iree::samples::util::util
 #  LINKOPTS
-#    "LINKER:--defsym=__itcm_length__=1500K"
+#    "LINKER:--defsym=__itcm_length__=1100K"
 #    "LINKER:--defsym=__stack_size__=100k"
 #  COPTS
 #    "-DBUILD_EMITC"
@@ -343,7 +343,7 @@
     samples::util::util
   LINKOPTS
     "LINKER:--defsym=__itcm_length__=1M"
-    "LINKER:--defsym=__stack_size__=100k"
+    "LINKER:--defsym=__stack_size__=300k"
 )
 
 iree_cc_binary(
@@ -358,45 +358,45 @@
     samples::util::util
   LINKOPTS
     "LINKER:--defsym=__itcm_length__=1M"
-    "LINKER:--defsym=__stack_size__=100k"
+    "LINKER:--defsym=__stack_size__=300k"
   COPTS
     "-DBUILD_EMITC"
 )
 
-#iree_cc_binary(
-#  NAME
-#    fssd_25_8bit_v2_bytecode_static
-#  SRCS
-#    "fssd_25_8bit_v2.c"
-#  DEPS
-#    ::fssd_quant_input_c
-#    ::fssd_25_8bit_v2_bytecode_module_static
-#    ::fssd_25_8bit_v2_bytecode_module_static_c
-#    iree::vm::bytecode_module
-#    samples::risp4ml::pipeline::pipeline
-#    samples::util::util
-#  LINKOPTS
-#    "LINKER:--defsym=__itcm_length__=1200K"
-#    "LINKER:--defsym=__stack_size__=100k"
-#)
+iree_cc_binary(
+  NAME
+    fssd_25_8bit_v2_bytecode_static
+  SRCS
+    "fssd_25_8bit_v2.c"
+  DEPS
+    ::fssd_quant_input_c
+    ::fssd_25_8bit_v2_bytecode_module_static
+    ::fssd_25_8bit_v2_bytecode_module_static_c
+    iree::vm::bytecode_module
+    samples::risp4ml::pipeline::pipeline
+    samples::util::util
+  LINKOPTS
+    "LINKER:--defsym=__itcm_length__=1M"
+    "LINKER:--defsym=__stack_size__=300k"
+)
 
-#iree_cc_binary(
-#  NAME
-#    fssd_25_8bit_v2_emitc_static
-#  SRCS
-#    "fssd_25_8bit_v2.c"
-#  DEPS
-#    ::fssd_quant_input_c
-#    ::fssd_25_8bit_v2_c_module_static_c
-#    ::fssd_25_8bit_v2_c_module_static_emitc
-#    samples::risp4ml::pipeline::pipeline
-#    samples::util::util
-#  LINKOPTS
-#    "LINKER:--defsym=__itcm_length__=1200K"
-#    "LINKER:--defsym=__stack_size__=100k"
-#  COPTS
-#    "-DBUILD_EMITC"
-#)
+iree_cc_binary(
+  NAME
+    fssd_25_8bit_v2_emitc_static
+  SRCS
+    "fssd_25_8bit_v2.c"
+  DEPS
+    ::fssd_quant_input_c
+    ::fssd_25_8bit_v2_c_module_static_c
+    ::fssd_25_8bit_v2_c_module_static_emitc
+    samples::risp4ml::pipeline::pipeline
+    samples::util::util
+  LINKOPTS
+    "LINKER:--defsym=__itcm_length__=1M"
+    "LINKER:--defsym=__stack_size__=300k"
+  COPTS
+    "-DBUILD_EMITC"
+)
 
 #iree_cc_binary(
 #  NAME
@@ -441,7 +441,7 @@
     samples::util::util
   LINKOPTS
     "LINKER:--defsym=__itcm_length__=1M"
-    "LINKER:--defsym=__stack_size__=100k"
+    "LINKER:--defsym=__stack_size__=300k"
 )
 
 iree_cc_binary(
@@ -455,7 +455,7 @@
     samples::util::util
   LINKOPTS
     "LINKER:--defsym=__itcm_length__=1M"
-    "LINKER:--defsym=__stack_size__=100k"
+    "LINKER:--defsym=__stack_size__=300k"
   COPTS
     "-DBUILD_EMITC"
 )
diff --git a/samples/quant_model/fssd_test.txt b/samples/quant_model/fssd_test.txt
index 90ca377..42f6a81 100644
--- a/samples/quant_model/fssd_test.txt
+++ b/samples/quant_model/fssd_test.txt
@@ -1,4 +1,3 @@
 // RUN: ${TEST_RUNNER_CMD} ${OUT}/springbok_iree/samples/quant_model/fssd_25_8bit_v2_bytecode_static
 // RUN: ${TEST_RUNNER_CMD} ${OUT}/springbok_iree/samples/quant_model/fssd_25_8bit_v2_emitc_static
 // REQUIRES: internal
-// XFAIL: *
diff --git a/samples/quant_model/person_detection_bytecode_static_test.txt b/samples/quant_model/person_detection_bytecode_static_test.txt
index 31e94ef..f278d1d 100644
--- a/samples/quant_model/person_detection_bytecode_static_test.txt
+++ b/samples/quant_model/person_detection_bytecode_static_test.txt
@@ -1,4 +1,3 @@
 // RUN: ${TEST_RUNNER_CMD} ${OUT}/springbok_iree/samples/quant_model/person_detection_bytecode_static 2>&1 | tee %t
 // RUN: cat %t | FileCheck %s
 // CHECK: {{Non-person Score: -113; Person Score: 113}}
-// XFAIL: *
diff --git a/samples/quant_model/person_detection_emitc_static_test.txt b/samples/quant_model/person_detection_emitc_static_test.txt
index 28c8946..38b4bc4 100644
--- a/samples/quant_model/person_detection_emitc_static_test.txt
+++ b/samples/quant_model/person_detection_emitc_static_test.txt
@@ -1,4 +1,3 @@
 // RUN: ${TEST_RUNNER_CMD} ${OUT}/springbok_iree/samples/quant_model/person_detection_emitc_static 2>&1 | tee %t
 // RUN: cat %t | FileCheck %s
 // CHECK: {{Non-person Score: -113; Person Score: 113}}
-// XFAIL: *
diff --git a/samples/simple_vec_mul/simple_float_mul.mlir b/samples/simple_vec_mul/simple_float_mul.mlir
index 7a928ff..0b2f7df 100644
--- a/samples/simple_vec_mul/simple_float_mul.mlir
+++ b/samples/simple_vec_mul/simple_float_mul.mlir
@@ -1,4 +1,4 @@
-func @simple_mul(%arg0: tensor<1024xf32>, %arg1: tensor<1024xf32>) -> tensor<1024xf32>
+func.func @simple_mul(%arg0: tensor<1024xf32>, %arg1: tensor<1024xf32>) -> tensor<1024xf32>
 {
   %0 = "mhlo.multiply"(%arg0, %arg1) : (tensor<1024xf32>, tensor<1024xf32>) -> tensor<1024xf32>
   return %0 : tensor<1024xf32>
diff --git a/samples/simple_vec_mul/simple_int_mul.mlir b/samples/simple_vec_mul/simple_int_mul.mlir
index 9aa9a22..ee2dac0 100644
--- a/samples/simple_vec_mul/simple_int_mul.mlir
+++ b/samples/simple_vec_mul/simple_int_mul.mlir
@@ -1,4 +1,4 @@
-func @simple_mul(%arg0: tensor<1024xi32>, %arg1: tensor<1024xi32>) -> tensor<1024xi32>
+func.func @simple_mul(%arg0: tensor<1024xi32>, %arg1: tensor<1024xi32>) -> tensor<1024xi32>
 {
   %0 = "mhlo.multiply"(%arg0, %arg1) : (tensor<1024xi32>, tensor<1024xi32>) -> tensor<1024xi32>
   return %0 : tensor<1024xi32>
diff --git a/samples/util/CMakeLists.txt b/samples/util/CMakeLists.txt
index 514cbf4..53ce441 100644
--- a/samples/util/CMakeLists.txt
+++ b/samples/util/CMakeLists.txt
@@ -11,7 +11,7 @@
     iree::hal
     iree::hal::local
     iree::hal::local::loaders::static_library_loader
-    iree::hal::local::sync_driver
+    iree::hal::drivers::local_sync::sync_driver
     iree::modules::hal
     iree::vm
     iree::vm::shims_emitc
diff --git a/samples/util/util.c b/samples/util/util.c
index 69337e4..3d2e9f9 100644
--- a/samples/util/util.c
+++ b/samples/util/util.c
@@ -50,12 +50,12 @@
       .type =
           IREE_HAL_MEMORY_TYPE_HOST_LOCAL | IREE_HAL_MEMORY_TYPE_DEVICE_VISIBLE,
       .access = IREE_HAL_MEMORY_ACCESS_READ,
-      .usage = IREE_HAL_BUFFER_USAGE_DISPATCH | IREE_HAL_BUFFER_USAGE_TRANSFER};
+      .usage = IREE_HAL_BUFFER_USAGE_DEFAULT};
   for (int i = 0; i < model->num_input; ++i) {
     if (iree_status_is_ok(result)) {
       result = iree_hal_buffer_view_allocate_buffer(
-          iree_hal_device_allocator(device), model->input_shape[i],
-          model->num_input_dim[i], model->hal_element_type,
+          iree_hal_device_allocator(device), model->num_input_dim[i],
+          model->input_shape[i], model->hal_element_type,
           IREE_HAL_ENCODING_TYPE_DENSE_ROW_MAJOR, buffer_params, *byte_span[i],
           &(arg_buffer_views[i]));
     }
@@ -93,8 +93,8 @@
   iree_vm_module_t *modules[] = {hal_module, module};
   if (iree_status_is_ok(result)) {
     result = iree_vm_context_create_with_modules(
-        instance, IREE_VM_CONTEXT_FLAG_NONE, &modules[0],
-        IREE_ARRAYSIZE(modules), iree_allocator_system(), &context);
+        instance, IREE_VM_CONTEXT_FLAG_NONE, IREE_ARRAYSIZE(modules),
+        &modules[0], iree_allocator_system(), &context);
   }
   iree_vm_module_release(hal_module);
   iree_vm_module_release(module);