Merge pull request #8418 from google/benvanik-status-unused

Gardening binary size/memory consumption 🎍.
diff --git a/iree/base/status.c b/iree/base/status.c
index ceebea4..341630e 100644
--- a/iree/base/status.c
+++ b/iree/base/status.c
@@ -786,7 +786,7 @@
     fprintf(file, "%.*s\n", (int)status_buffer_length, status_buffer);
     iree_allocator_free(allocator, status_buffer);
   } else {
-    fprintf(file, "(failed to format status)\n");
+    fprintf(file, "(?)\n");
   }
   fflush(file);
 }
diff --git a/iree/compiler/Codegen/Common/RemoveTrivialLoops.cpp b/iree/compiler/Codegen/Common/RemoveTrivialLoops.cpp
index 9fe746b..680a625 100644
--- a/iree/compiler/Codegen/Common/RemoveTrivialLoops.cpp
+++ b/iree/compiler/Codegen/Common/RemoveTrivialLoops.cpp
@@ -30,6 +30,9 @@
       return 1;
     case gpu::Dimension::z:
       return 2;
+    default:
+      llvm_unreachable("invalid dimension");
+      return 0;
   }
 }
 
diff --git a/iree/compiler/Codegen/Interfaces/ProcessorOpInterfaces.cpp b/iree/compiler/Codegen/Interfaces/ProcessorOpInterfaces.cpp
index 2a4a478..4eb962b 100644
--- a/iree/compiler/Codegen/Interfaces/ProcessorOpInterfaces.cpp
+++ b/iree/compiler/Codegen/Interfaces/ProcessorOpInterfaces.cpp
@@ -23,6 +23,9 @@
       return 1;
     case gpu::Dimension::z:
       return 2;
+    default:
+      llvm_unreachable("invalid dimension");
+      return 0;
   }
 }
 
diff --git a/iree/hal/buffer.c b/iree/hal/buffer.c
index e006306..cb2b4ab 100644
--- a/iree/hal/buffer.c
+++ b/iree/hal/buffer.c
@@ -219,11 +219,12 @@
     iree_hal_memory_type_t expected_memory_type) {
   if (IREE_UNLIKELY(
           !iree_all_bits_set(actual_memory_type, expected_memory_type))) {
+#if IREE_STATUS_MODE
     // Missing one or more bits.
     iree_bitfield_string_temp_t temp0, temp1;
-    iree_string_view_t actual_memory_type_str IREE_ATTRIBUTE_UNUSED =
+    iree_string_view_t actual_memory_type_str =
         iree_hal_memory_type_format(actual_memory_type, &temp0);
-    iree_string_view_t expected_memory_type_str IREE_ATTRIBUTE_UNUSED =
+    iree_string_view_t expected_memory_type_str =
         iree_hal_memory_type_format(expected_memory_type, &temp1);
     return iree_make_status(
         IREE_STATUS_PERMISSION_DENIED,
@@ -231,6 +232,9 @@
         "buffer has %.*s, operation requires %.*s",
         (int)actual_memory_type_str.size, actual_memory_type_str.data,
         (int)expected_memory_type_str.size, expected_memory_type_str.data);
+#else
+    return iree_status_from_code(IREE_STATUS_PERMISSION_DENIED);
+#endif  // IREE_STATUS_MODE
   }
   return iree_ok_status();
 }
@@ -250,11 +254,12 @@
         "memory access must specify one or more of _READ or _WRITE");
   } else if (IREE_UNLIKELY(!iree_all_bits_set(allowed_memory_access,
                                               required_memory_access))) {
+#if IREE_STATUS_MODE
     // Bits must match exactly.
     iree_bitfield_string_temp_t temp0, temp1;
-    iree_string_view_t allowed_memory_access_str IREE_ATTRIBUTE_UNUSED =
+    iree_string_view_t allowed_memory_access_str =
         iree_hal_memory_access_format(allowed_memory_access, &temp0);
-    iree_string_view_t required_memory_access_str IREE_ATTRIBUTE_UNUSED =
+    iree_string_view_t required_memory_access_str =
         iree_hal_memory_access_format(required_memory_access, &temp1);
     return iree_make_status(
         IREE_STATUS_PERMISSION_DENIED,
@@ -262,6 +267,9 @@
         "type; buffer allows %.*s, operation requires %.*s",
         (int)allowed_memory_access_str.size, allowed_memory_access_str.data,
         (int)required_memory_access_str.size, required_memory_access_str.data);
+#else
+    return iree_status_from_code(IREE_STATUS_PERMISSION_DENIED);
+#endif  // IREE_STATUS_MODE
   }
   return iree_ok_status();
 }
@@ -270,11 +278,12 @@
 iree_hal_buffer_validate_usage(iree_hal_buffer_usage_t allowed_usage,
                                iree_hal_buffer_usage_t required_usage) {
   if (IREE_UNLIKELY(!iree_all_bits_set(allowed_usage, required_usage))) {
+#if IREE_STATUS_MODE
     // Missing one or more bits.
     iree_bitfield_string_temp_t temp0, temp1;
-    iree_string_view_t allowed_usage_str IREE_ATTRIBUTE_UNUSED =
+    iree_string_view_t allowed_usage_str =
         iree_hal_buffer_usage_format(allowed_usage, &temp0);
-    iree_string_view_t required_usage_str IREE_ATTRIBUTE_UNUSED =
+    iree_string_view_t required_usage_str =
         iree_hal_buffer_usage_format(required_usage, &temp1);
     return iree_make_status(
         IREE_STATUS_PERMISSION_DENIED,
@@ -282,6 +291,9 @@
         "buffer allows %.*s, operation requires %.*s",
         (int)allowed_usage_str.size, allowed_usage_str.data,
         (int)required_usage_str.size, required_usage_str.data);
+#else
+    return iree_status_from_code(IREE_STATUS_PERMISSION_DENIED);
+#endif  // IREE_STATUS_MODE
   }
   return iree_ok_status();
 }
diff --git a/iree/hal/command_buffer_validation.c b/iree/hal/command_buffer_validation.c
index 4bd1538..8ccf775 100644
--- a/iree/hal/command_buffer_validation.c
+++ b/iree/hal/command_buffer_validation.c
@@ -35,10 +35,11 @@
     iree_hal_command_category_t required_categories) {
   if (!iree_all_bits_set(command_buffer->allowed_categories,
                          required_categories)) {
+#if IREE_STATUS_MODE
     iree_bitfield_string_temp_t temp0, temp1;
-    iree_string_view_t required_categories_str IREE_ATTRIBUTE_UNUSED =
+    iree_string_view_t required_categories_str =
         iree_hal_command_category_format(required_categories, &temp0);
-    iree_string_view_t allowed_categories_str IREE_ATTRIBUTE_UNUSED =
+    iree_string_view_t allowed_categories_str =
         iree_hal_command_category_format(command_buffer->allowed_categories,
                                          &temp1);
     return iree_make_status(
@@ -47,6 +48,9 @@
         "%.*s",
         (int)required_categories_str.size, required_categories_str.data,
         (int)allowed_categories_str.size, allowed_categories_str.data);
+#else
+    return iree_status_from_code(IREE_STATUS_FAILED_PRECONDITION);
+#endif  // IREE_STATUS_MODE
   }
   return iree_ok_status();
 }
@@ -65,12 +69,12 @@
           },
           iree_hal_buffer_allocation_size(buffer));
   if (!iree_all_bits_set(allowed_compatibility, required_compatibility)) {
+#if IREE_STATUS_MODE
     // Buffer cannot be used on the queue for the given usage.
     iree_bitfield_string_temp_t temp0, temp1;
-    iree_string_view_t allowed_usage_str IREE_ATTRIBUTE_UNUSED =
-        iree_hal_buffer_usage_format(iree_hal_buffer_allowed_usage(buffer),
-                                     &temp0);
-    iree_string_view_t intended_usage_str IREE_ATTRIBUTE_UNUSED =
+    iree_string_view_t allowed_usage_str = iree_hal_buffer_usage_format(
+        iree_hal_buffer_allowed_usage(buffer), &temp0);
+    iree_string_view_t intended_usage_str =
         iree_hal_buffer_usage_format(intended_usage, &temp1);
     return iree_make_status(
         IREE_STATUS_PERMISSION_DENIED,
@@ -79,6 +83,9 @@
         "mismatch)",
         (int)allowed_usage_str.size, allowed_usage_str.data,
         (int)intended_usage_str.size, intended_usage_str.data);
+#else
+    return iree_status_from_code(IREE_STATUS_PERMISSION_DENIED);
+#endif  // IREE_STATUS_MODE
   }
   return iree_ok_status();
 }
@@ -315,19 +322,21 @@
                         IREE_HAL_MEMORY_TYPE_DEVICE_VISIBLE) &&
       !iree_any_bit_set(iree_hal_buffer_memory_type(target_buffer),
                         IREE_HAL_MEMORY_TYPE_DEVICE_VISIBLE)) {
+#if IREE_STATUS_MODE
     iree_bitfield_string_temp_t temp0, temp1;
-    iree_string_view_t source_memory_type_str IREE_ATTRIBUTE_UNUSED =
-        iree_hal_memory_type_format(iree_hal_buffer_memory_type(source_buffer),
-                                    &temp0);
-    iree_string_view_t target_memory_type_str IREE_ATTRIBUTE_UNUSED =
-        iree_hal_memory_type_format(iree_hal_buffer_memory_type(target_buffer),
-                                    &temp1);
+    iree_string_view_t source_memory_type_str = iree_hal_memory_type_format(
+        iree_hal_buffer_memory_type(source_buffer), &temp0);
+    iree_string_view_t target_memory_type_str = iree_hal_memory_type_format(
+        iree_hal_buffer_memory_type(target_buffer), &temp1);
     return iree_make_status(
         IREE_STATUS_PERMISSION_DENIED,
         "at least one buffer must be device-visible for a copy; "
         "source_buffer=%.*s, target_buffer=%.*s",
         (int)source_memory_type_str.size, source_memory_type_str.data,
         (int)target_memory_type_str.size, target_memory_type_str.data);
+#else
+    return iree_status_from_code(IREE_STATUS_PERMISSION_DENIED);
+#endif  // IREE_STATUS_MODE
   }
 
   // Check for overlap - just like memcpy we don't handle that.
diff --git a/iree/runtime/demo/BUILD b/iree/runtime/demo/BUILD
index a5e7b0e..c465e1e 100644
--- a/iree/runtime/demo/BUILD
+++ b/iree/runtime/demo/BUILD
@@ -17,6 +17,22 @@
 # Hello World!
 #===------------------------------------------------------------------------===#
 
+cc_binary(
+    name = "hello_world_file",
+    srcs = ["hello_world_explained.c"],
+    defines = [
+        # Load data from a file passed on the command line.
+        "IREE_RUNTIME_DEMO_LOAD_FILE_FROM_COMMAND_LINE_ARG",
+    ],
+    deps = [
+        "//iree/runtime",
+    ],
+)
+
+# TODO(benvanik): native_test that passes the file as a flag. Right now we
+# can't specify data through native_test, though, so this isn't possible to
+# automate.
+
 iree_cmake_extra_content(
     content = """
 if (NOT ${IREE_HAL_DRIVER_VMVX} OR NOT ${IREE_TARGET_BACKEND_VMVX})
@@ -45,22 +61,6 @@
 )
 
 cc_binary(
-    name = "hello_world_file",
-    srcs = ["hello_world_explained.c"],
-    defines = [
-        # Load data from a file passed on the command line.
-        "IREE_RUNTIME_DEMO_LOAD_FILE_FROM_COMMAND_LINE_ARG",
-    ],
-    deps = [
-        "//iree/runtime",
-    ],
-)
-
-# TODO(benvanik): native_test that passes the file as a flag. Right now we
-# can't specify data through native_test, though, so this isn't possible to
-# automate.
-
-cc_binary(
     name = "hello_world_terse",
     srcs = ["hello_world_terse.c"],
     deps = [
diff --git a/iree/runtime/demo/CMakeLists.txt b/iree/runtime/demo/CMakeLists.txt
index 54c8c5d..c06f15a 100644
--- a/iree/runtime/demo/CMakeLists.txt
+++ b/iree/runtime/demo/CMakeLists.txt
@@ -1,3 +1,17 @@
+# NOTE: not using bazel-to-cmake here because of the runtime unified rule.
+# We should figure out how to make bazel/cmake consistent with that.
+
+iree_cc_binary(
+  NAME
+    hello_world_file
+  SRCS
+    "hello_world_explained.c"
+  DEFINES
+    "IREE_RUNTIME_DEMO_LOAD_FILE_FROM_COMMAND_LINE_ARG"
+  DEPS
+    iree::runtime::unified
+)
+
 if (NOT ${IREE_HAL_DRIVER_VMVX} OR NOT ${IREE_TARGET_BACKEND_VMVX})
   return()
 endif()
@@ -23,17 +37,6 @@
 
 iree_cc_binary(
   NAME
-    hello_world_file
-  SRCS
-    "hello_world_explained.c"
-  DEFINES
-    "IREE_RUNTIME_DEMO_LOAD_FILE_FROM_COMMAND_LINE_ARG"
-  DEPS
-    iree::runtime::unified
-)
-
-iree_cc_binary(
-  NAME
     hello_world_terse
   SRCS
     "hello_world_terse.c"
diff --git a/iree/vm/list.c b/iree/vm/list.c
index ca65044..e9a47b1 100644
--- a/iree/vm/list.c
+++ b/iree/vm/list.c
@@ -13,19 +13,19 @@
 
 #include "iree/base/tracing.h"
 
-// Size of each iree_vm_value_type_t in bytes.
-static const iree_host_size_t kValueTypeSizes[7] = {
-    0,  // IREE_VM_VALUE_TYPE_NONE
-    1,  // IREE_VM_VALUE_TYPE_I8
-    2,  // IREE_VM_VALUE_TYPE_I16
-    4,  // IREE_VM_VALUE_TYPE_I32
-    8,  // IREE_VM_VALUE_TYPE_I64
-    4,  // IREE_VM_VALUE_TYPE_F32
-    8,  // IREE_VM_VALUE_TYPE_F64
-};
-static_assert(IREE_VM_VALUE_TYPE_COUNT ==
-                  (sizeof(kValueTypeSizes) / sizeof(kValueTypeSizes[0])),
-              "Enum mismatch");
+static uint8_t iree_vm_value_type_size(iree_vm_value_type_t type) {
+  // Size of each iree_vm_value_type_t in bytes. We bitpack these so that we
+  // can do a simple shift and mask to get the size.
+  const uint32_t kValueTypeSizes = (0u << 0) |   // IREE_VM_VALUE_TYPE_NONE
+                                   (1u << 4) |   // IREE_VM_VALUE_TYPE_I8
+                                   (2u << 8) |   // IREE_VM_VALUE_TYPE_I16
+                                   (4u << 12) |  // IREE_VM_VALUE_TYPE_I32
+                                   (8u << 16) |  // IREE_VM_VALUE_TYPE_I64
+                                   (4u << 20) |  // IREE_VM_VALUE_TYPE_F32
+                                   (8u << 24) |  // IREE_VM_VALUE_TYPE_F64
+                                   (0u << 28);   // unused
+  return (kValueTypeSizes >> ((type & 0x7) * 4)) & 0xF;
+}
 
 // Defines how the iree_vm_list_t storage is allocated and what elements are
 // interpreted as.
@@ -102,7 +102,7 @@
   iree_host_size_t element_size = sizeof(iree_vm_variant_t);
   if (element_type) {
     if (iree_vm_type_def_is_value(element_type)) {
-      element_size = kValueTypeSizes[element_type->value_type];
+      element_size = iree_vm_value_type_size(element_type->value_type);
     } else if (iree_vm_type_def_is_ref(element_type)) {
       element_size = sizeof(iree_vm_ref_t);
     } else {
@@ -123,7 +123,7 @@
   if (element_type) {
     if (iree_vm_type_def_is_value(element_type)) {
       storage_mode = IREE_VM_LIST_STORAGE_MODE_VALUE;
-      element_size = kValueTypeSizes[element_type->value_type];
+      element_size = iree_vm_value_type_size(element_type->value_type);
     } else if (iree_vm_type_def_is_ref(element_type)) {
       storage_mode = IREE_VM_LIST_STORAGE_MODE_REF;
       element_size = sizeof(iree_vm_ref_t);
@@ -188,7 +188,7 @@
 
   if (iree_vm_type_def_is_value(&list->element_type) && element_type) {
     list->storage_mode = IREE_VM_LIST_STORAGE_MODE_VALUE;
-    list->element_size = kValueTypeSizes[element_type->value_type];
+    list->element_size = iree_vm_value_type_size(element_type->value_type);
   } else if (iree_vm_type_def_is_ref(&list->element_type)) {
     list->storage_mode = IREE_VM_LIST_STORAGE_MODE_REF;
     list->element_size = sizeof(iree_vm_ref_t);
diff --git a/iree/vm/stack.c b/iree/vm/stack.c
index acb4fb8..5ea5b9c 100644
--- a/iree/vm/stack.c
+++ b/iree/vm/stack.c
@@ -417,9 +417,9 @@
 
   iree_vm_stack_frame_t* callee_frame = &frame_header->frame;
   callee_frame->function = *function;
-  callee_frame->depth = caller_frame ? caller_frame->depth + 1 : 0;
   callee_frame->module_state = module_state;
   callee_frame->pc = 0;
+  callee_frame->depth = caller_frame ? caller_frame->depth + 1 : 0;
 
   stack->frame_storage_size = new_top;
   stack->top = frame_header;
diff --git a/iree/vm/stack.h b/iree/vm/stack.h
index 277b3c1..abd5b75 100644
--- a/iree/vm/stack.h
+++ b/iree/vm/stack.h
@@ -75,11 +75,6 @@
   // Function that the stack frame is within.
   iree_vm_function_t function;
 
-  // Depth of the frame within the stack.
-  // As stack frame pointers are not stable this can be used instead to detect
-  // stack enter/leave balance issues.
-  int32_t depth;
-
   // Cached module state pointer for the module containing |function|.
   // This removes the need to lookup the module state when control returns to
   // the function during continuation or from a return instruction.
@@ -91,6 +86,11 @@
   // code), etc.
   iree_vm_source_offset_t pc;
 
+  // Depth of the frame within the stack.
+  // As stack frame pointers are not stable this can be used instead to detect
+  // stack enter/leave balance issues.
+  int32_t depth;
+
   IREE_TRACE(iree_zone_id_t trace_zone;)
 } iree_vm_stack_frame_t;