Adding a size-optimized build config. (#7783)

Adding a size-optimized build config and IREE_ATTRIBUTE_UNUSED to potentially unused debug values to quiet the warnings about unused values when status/etc are disabled.
diff --git a/build_tools/buildkite/cmake/build_configurations.yml b/build_tools/buildkite/cmake/build_configurations.yml
index 7b3acec..ddb1d9c 100644
--- a/build_tools/buildkite/cmake/build_configurations.yml
+++ b/build_tools/buildkite/cmake/build_configurations.yml
@@ -23,6 +23,15 @@
     agents:
       - "queue=build"
 
+  - label: ":pinching_hand: Build the size-optimized runtime only"
+    commands:
+      - "./scripts/git/submodule_versions.py init"
+      - "docker run --user=$(id -u):$(id -g) --volume=\\$PWD:\\$IREE_DOCKER_WORKDIR --workdir=\\$IREE_DOCKER_WORKDIR --rm gcr.io/iree-oss/base@sha256:b8d9863c6ac913f167c6fab319d7cd883ab099312488709ee30b29976d63eb22 ./build_tools/cmake/build_runtime_small.sh"
+    env:
+      IREE_DOCKER_WORKDIR: "/usr/src/github/iree"
+    agents:
+      - "queue=build"
+
   - label: ":gnu: Build with GCC"
     key: "build-gcc"
     commands:
diff --git a/build_tools/cmake/build_runtime_small.sh b/build_tools/cmake/build_runtime_small.sh
new file mode 100755
index 0000000..287131c
--- /dev/null
+++ b/build_tools/cmake/build_runtime_small.sh
@@ -0,0 +1,35 @@
+#!/bin/bash
+# Copyright 2021 The IREE Authors
+#
+# Licensed under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+# Build IREE's runtime using CMake. Designed for CI, but can be run manually.
+# This uses previously cached build results and does not clear build
+# directories.
+
+set -e
+set -x
+
+ROOT_DIR=$(git rev-parse --show-toplevel)
+cd ${ROOT_DIR?}
+
+CMAKE_BIN=${CMAKE_BIN:-$(which cmake)}
+"${CMAKE_BIN?}" --version
+ninja --version
+
+if [ -d "build-runtime-small" ]
+then
+  echo "build-runtime-small directory already exists. Will use cached results there."
+else
+  echo "build-runtime-small directory does not already exist. Creating a new one."
+  mkdir build-runtime-small
+fi
+cd build-runtime-small
+
+"${CMAKE_BIN?}" -G Ninja .. \
+  -DCMAKE_BUILD_TYPE=MinSizeRel \
+  -DIREE_SIZE_OPTIMIZED=ON \
+  -DIREE_BUILD_COMPILER=OFF
+"${CMAKE_BIN?}" --build .
diff --git a/iree/base/attributes.h b/iree/base/attributes.h
index d419927..6077351 100644
--- a/iree/base/attributes.h
+++ b/iree/base/attributes.h
@@ -172,4 +172,24 @@
 #define IREE_ATTRIBUTE_PACKED
 #endif  // IREE_HAVE_ATTRIBUTE(packed)
 
+//===----------------------------------------------------------------------===//
+// IREE_ATTRIBUTE_UNUSED
+//===----------------------------------------------------------------------===//
+
+// Hints that a variable is _maybe_ unused. This is primarily to quiet
+// diagnostic messages about unused variables that crop up around variables
+// passed to assert/logging/etc that gets stripped in certain configurations.
+//
+// Example:
+//   int some_info IREE_ATTRIBUTE_UNUSED = compute_debug_info();
+//   assert(some_info > 0);  // stripped in NDEBUG
+#if IREE_HAVE_ATTRIBUTE(maybe_unused) || \
+    (defined(__GNUC__) && !defined(__clang__))
+#define IREE_ATTRIBUTE_UNUSED __attribute__((maybe_unused))
+#elif IREE_HAVE_ATTRIBUTE(unused) || (defined(__GNUC__) && !defined(__clang__))
+#define IREE_ATTRIBUTE_UNUSED __attribute__((unused))
+#else
+#define IREE_ATTRIBUTE_UNUSED
+#endif  // IREE_HAVE_ATTRIBUTE(maybe_unused / unused)
+
 #endif  // IREE_BASE_ATTRIBUTES_H_
diff --git a/iree/base/status.c b/iree/base/status.c
index a65a400..4942eb6 100644
--- a/iree/base/status.c
+++ b/iree/base/status.c
@@ -645,7 +645,8 @@
   *out_buffer_length = 0;
 
   // Grab storage which may have a message and zero or more payloads.
-  iree_status_storage_t* storage = iree_status_storage(status);
+  iree_status_storage_t* storage IREE_ATTRIBUTE_UNUSED =
+      iree_status_storage(status);
 
   // Prefix with source location and status code string (may be 'OK').
   iree_host_size_t buffer_length = 0;
diff --git a/iree/hal/allocator_heap.c b/iree/hal/allocator_heap.c
index 0259d6c..dc5d4cc 100644
--- a/iree/hal/allocator_heap.c
+++ b/iree/hal/allocator_heap.c
@@ -84,9 +84,9 @@
 static void iree_hal_heap_allocator_query_statistics(
     iree_hal_allocator_t* base_allocator,
     iree_hal_allocator_statistics_t* out_statistics) {
-  iree_hal_heap_allocator_t* allocator =
-      iree_hal_heap_allocator_cast(base_allocator);
   IREE_STATISTICS({
+    iree_hal_heap_allocator_t* allocator =
+        iree_hal_heap_allocator_cast(base_allocator);
     iree_slim_mutex_lock(&allocator->statistics.mutex);
     memcpy(out_statistics, &allocator->statistics.base,
            sizeof(*out_statistics));
diff --git a/iree/hal/buffer.c b/iree/hal/buffer.c
index a47b234..ca15374 100644
--- a/iree/hal/buffer.c
+++ b/iree/hal/buffer.c
@@ -170,9 +170,9 @@
           !iree_all_bits_set(actual_memory_type, expected_memory_type))) {
     // Missing one or more bits.
     iree_bitfield_string_temp_t temp0, temp1;
-    iree_string_view_t actual_memory_type_str =
+    iree_string_view_t actual_memory_type_str IREE_ATTRIBUTE_UNUSED =
         iree_hal_memory_type_format(actual_memory_type, &temp0);
-    iree_string_view_t expected_memory_type_str =
+    iree_string_view_t expected_memory_type_str IREE_ATTRIBUTE_UNUSED =
         iree_hal_memory_type_format(expected_memory_type, &temp1);
     return iree_make_status(
         IREE_STATUS_PERMISSION_DENIED,
@@ -201,9 +201,9 @@
                                               required_memory_access))) {
     // Bits must match exactly.
     iree_bitfield_string_temp_t temp0, temp1;
-    iree_string_view_t allowed_memory_access_str =
+    iree_string_view_t allowed_memory_access_str IREE_ATTRIBUTE_UNUSED =
         iree_hal_memory_access_format(allowed_memory_access, &temp0);
-    iree_string_view_t required_memory_access_str =
+    iree_string_view_t required_memory_access_str IREE_ATTRIBUTE_UNUSED =
         iree_hal_memory_access_format(required_memory_access, &temp1);
     return iree_make_status(
         IREE_STATUS_PERMISSION_DENIED,
@@ -221,9 +221,9 @@
   if (IREE_UNLIKELY(!iree_all_bits_set(allowed_usage, required_usage))) {
     // Missing one or more bits.
     iree_bitfield_string_temp_t temp0, temp1;
-    iree_string_view_t allowed_usage_str =
+    iree_string_view_t allowed_usage_str IREE_ATTRIBUTE_UNUSED =
         iree_hal_buffer_usage_format(allowed_usage, &temp0);
-    iree_string_view_t required_usage_str =
+    iree_string_view_t required_usage_str IREE_ATTRIBUTE_UNUSED =
         iree_hal_buffer_usage_format(required_usage, &temp1);
     return iree_make_status(
         IREE_STATUS_PERMISSION_DENIED,
diff --git a/iree/hal/command_buffer_validation.c b/iree/hal/command_buffer_validation.c
index 1f49a91..f8dffa9 100644
--- a/iree/hal/command_buffer_validation.c
+++ b/iree/hal/command_buffer_validation.c
@@ -57,9 +57,9 @@
   if (!iree_all_bits_set(command_buffer->allowed_categories,
                          required_categories)) {
     iree_bitfield_string_temp_t temp0, temp1;
-    iree_string_view_t required_categories_str =
+    iree_string_view_t required_categories_str IREE_ATTRIBUTE_UNUSED =
         iree_hal_command_category_format(required_categories, &temp0);
-    iree_string_view_t allowed_categories_str =
+    iree_string_view_t allowed_categories_str IREE_ATTRIBUTE_UNUSED =
         iree_hal_command_category_format(command_buffer->allowed_categories,
                                          &temp1);
     return iree_make_status(
@@ -87,9 +87,10 @@
   if (!iree_all_bits_set(allowed_compatibility, required_compatibility)) {
     // 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_hal_buffer_usage_format(
-        iree_hal_buffer_allowed_usage(buffer), &temp0);
-    iree_string_view_t intended_usage_str =
+    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_hal_buffer_usage_format(intended_usage, &temp1);
     return iree_make_status(
         IREE_STATUS_PERMISSION_DENIED,
@@ -454,10 +455,12 @@
       !iree_any_bit_set(iree_hal_buffer_memory_type(target_buffer),
                         IREE_HAL_MEMORY_TYPE_DEVICE_VISIBLE)) {
     iree_bitfield_string_temp_t temp0, 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);
+    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);
     return iree_make_status(
         IREE_STATUS_PERMISSION_DENIED,
         "at least one buffer must be device-visible for a copy; "
diff --git a/iree/hal/local/elf/elf_module.c b/iree/hal/local/elf/elf_module.c
index 3cffeb7..61f68e9 100644
--- a/iree/hal/local/elf/elf_module.c
+++ b/iree/hal/local/elf/elf_module.c
@@ -474,7 +474,8 @@
   for (iree_host_size_t i = 1; i < module->dynsym_count; ++i) {
     const iree_elf_sym_t* sym = &module->dynsym[i];
     if (sym->st_shndx == IREE_ELF_SHN_UNDEF) {
-      const char* symname = sym->st_name ? module->dynstr + sym->st_name : NULL;
+      const char* symname IREE_ATTRIBUTE_UNUSED =
+          sym->st_name ? module->dynstr + sym->st_name : NULL;
       return iree_make_status(IREE_STATUS_UNAVAILABLE,
                               "ELF imports one or more symbols (trying "
                               "'%s'); imports are not supported in the "
diff --git a/iree/hal/local/sync_semaphore.c b/iree/hal/local/sync_semaphore.c
index f982f22..c990ec1 100644
--- a/iree/hal/local/sync_semaphore.c
+++ b/iree/hal/local/sync_semaphore.c
@@ -131,7 +131,7 @@
 static iree_status_t iree_hal_sync_semaphore_signal_unsafe(
     iree_hal_sync_semaphore_t* semaphore, uint64_t new_value) {
   if (new_value <= semaphore->current_value) {
-    uint64_t current_value = semaphore->current_value;
+    uint64_t current_value IREE_ATTRIBUTE_UNUSED = semaphore->current_value;
     iree_slim_mutex_unlock(&semaphore->mutex);
     return iree_make_status(IREE_STATUS_OUT_OF_RANGE,
                             "semaphore values must be monotonically "
diff --git a/iree/hal/local/task_semaphore.c b/iree/hal/local/task_semaphore.c
index b4c6289..6aad400 100644
--- a/iree/hal/local/task_semaphore.c
+++ b/iree/hal/local/task_semaphore.c
@@ -238,7 +238,7 @@
   iree_slim_mutex_lock(&semaphore->mutex);
 
   if (new_value <= semaphore->current_value) {
-    uint64_t current_value = semaphore->current_value;
+    uint64_t current_value IREE_ATTRIBUTE_UNUSED = semaphore->current_value;
     iree_slim_mutex_unlock(&semaphore->mutex);
     return iree_make_status(IREE_STATUS_OUT_OF_RANGE,
                             "semaphore values must be monotonically "
diff --git a/iree/modules/hal/module.c b/iree/modules/hal/module.c
index 1faa691..72a0bae 100644
--- a/iree/modules/hal/module.c
+++ b/iree/modules/hal/module.c
@@ -414,7 +414,8 @@
   IREE_RETURN_IF_ERROR(iree_hal_buffer_check_deref(args->r0, &buffer));
   iree_vm_buffer_t* message = NULL;
   IREE_RETURN_IF_ERROR(iree_vm_buffer_check_deref(args->r1, &message));
-  iree_string_view_t message_str = iree_vm_buffer_as_string(message);
+  iree_string_view_t message_str IREE_ATTRIBUTE_UNUSED =
+      iree_vm_buffer_as_string(message);
   iree_hal_allocator_t* allocator = NULL;
   IREE_RETURN_IF_ERROR(iree_hal_allocator_check_deref(args->r2, &allocator));
   iree_vm_size_t minimum_length = (iree_vm_size_t)args->i3;
@@ -621,7 +622,8 @@
       iree_hal_buffer_view_check_deref(args->r0, &buffer_view));
   iree_vm_buffer_t* message = NULL;
   IREE_RETURN_IF_ERROR(iree_vm_buffer_check_deref(args->r1, &message));
-  iree_string_view_t message_str = iree_vm_buffer_as_string(message);
+  iree_string_view_t message_str IREE_ATTRIBUTE_UNUSED =
+      iree_vm_buffer_as_string(message);
   iree_hal_element_type_t expected_element_type =
       (iree_hal_element_type_t)args->i2;
   iree_hal_encoding_type_t expected_encoding_type =
diff --git a/iree/tools/utils/yaml_util.c b/iree/tools/utils/yaml_util.c
index f721d00..a3092a5 100644
--- a/iree/tools/utils/yaml_util.c
+++ b/iree/tools/utils/yaml_util.c
@@ -99,7 +99,7 @@
   size_t decoded_length = 0;
   size_t i = 0;
   while (i < source.size) {
-    uint8_t c = iree_yaml_base64_decode_table[source.data[i++]];
+    uint8_t c = iree_yaml_base64_decode_table[(uint8_t)source.data[i++]];
     if (c == IREE_YAML_BASE64_WHITESPACE) {
       // Skip whitespace.
       continue;
@@ -138,7 +138,7 @@
   size_t i = 0;
   uint8_t* p = target.data;
   while (i < source.size) {
-    uint8_t c = iree_yaml_base64_decode_table[source.data[i++]];
+    uint8_t c = iree_yaml_base64_decode_table[(uint8_t)source.data[i++]];
     if (c == IREE_YAML_BASE64_WHITESPACE) {
       // Skip whitespace.
       continue;
diff --git a/iree/vm/native_module.c b/iree/vm/native_module.c
index 7e90813..e941089 100644
--- a/iree/vm/native_module.c
+++ b/iree/vm/native_module.c
@@ -304,14 +304,20 @@
   iree_status_t status = function_ptr->shim(stack, call, function_ptr->target,
                                             module, module_state, out_result);
   if (IREE_UNLIKELY(!iree_status_is_ok(status))) {
-    iree_string_view_t module_name = iree_vm_native_module_name(module);
-    iree_string_view_t function_name = iree_string_view_empty();
+#if IREE_STATUS_FEATURES & IREE_STATUS_FEATURE_ANNOTATIONS
+    iree_string_view_t module_name IREE_ATTRIBUTE_UNUSED =
+        iree_vm_native_module_name(module);
+    iree_string_view_t function_name IREE_ATTRIBUTE_UNUSED =
+        iree_string_view_empty();
     iree_status_ignore(iree_vm_native_module_get_export_function(
         module, call->function.ordinal, NULL, &function_name, NULL));
     return iree_status_annotate_f(status,
                                   "while invoking native function %.*s.%.*s",
                                   (int)module_name.size, module_name.data,
                                   (int)function_name.size, function_name.data);
+#else
+    return status;
+#endif  // IREE_STATUS_FEATURES & IREE_STATUS_FEATURE_ANNOTATIONS
   }
 
   return iree_vm_stack_function_leave(stack);