Build e2e test artifacts in CI (#11217)

diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 0100396..528aefe 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -579,6 +579,64 @@
       tf-binaries-archive: ${{ needs.build_tf_integrations.outputs.binaries-archive }}
       tf-binaries-gcs-artifact: ${{ needs.build_tf_integrations.outputs.binaries-gcs-artifact }}
 
+  build_e2e_test_artifacts:
+    needs: [setup, build_all, build_tf_integrations]
+    if: needs.setup.outputs.should-run == 'true'
+    runs-on:
+      - self-hosted  # must come first
+      - runner-group=${{ needs.setup.outputs.runner-group }}
+      - environment=${{ needs.setup.outputs.runner-env }}
+      - cpu
+      - os-family=Linux
+    env:
+      BUILD_DIR: ${{ needs.build_all.outputs.build-dir }}
+      BUILD_DIR_ARCHIVE: ${{ needs.build_all.outputs.build-dir-archive }}
+      BUILD_DIR_GCS_ARTIFACT: ${{ needs.build_all.outputs.build-dir-gcs-artifact }}
+      TF_BINARIES_DIR: ${{ needs.build_tf_integrations.outputs.binaries-dir }}
+      TF_BINARIES_ARCHIVE: ${{ needs.build_tf_integrations.outputs.binaries-archive }}
+      TF_BINARIES_GCS_ARTIFACT: ${{ needs.build_tf_integrations.outputs.binaries-gcs-artifact }}
+    outputs:
+      e2e-test-artifacts-dir: ${{ steps.build.outputs.e2e-test-artifacts-dir }}
+      e2e-test-artifacts-gcs-artifact-dir: ${{ steps.upload.outputs.e2e-test-artifacts-gcs-artifact-dir }}
+    steps:
+      - name: "Checking out repository"
+        uses: actions/checkout@7884fcad6b5d53d10323aee724dc68d8b9096a2e # v2
+      - name: "Checking out runtime submodules"
+        run: ./build_tools/scripts/git/update_runtime_submodules.sh
+      - name: "Downloading build dir archive"
+        run: gcloud alpha storage cp "${BUILD_DIR_GCS_ARTIFACT}" "${BUILD_DIR_ARCHIVE}"
+      - name: "Extracting install from build dir archive"
+        run: tar -xf "${BUILD_DIR_ARCHIVE}" "${BUILD_DIR}/install"
+      - name: "Downloading TF binaries archive"
+        run: gcloud alpha storage cp "${TF_BINARIES_GCS_ARTIFACT}" "${TF_BINARIES_ARCHIVE}"
+      - name: "Extracting TF binaries archive"
+        run: tar -xf "${TF_BINARIES_ARCHIVE}"
+      - name: "Building e2e test artifacts"
+        id: build
+        env:
+          BUILD_E2E_TEST_ARTIFACTS_DIR: build-e2e-test-artifacts
+        run: |
+          build_tools/github_actions/docker_run.sh \
+            --env "IREE_TF_BINARIES_DIR=${TF_BINARIES_DIR}" \
+            --env "IREE_HOST_BINARY_ROOT=${BUILD_DIR}/install" \
+            --env "BUILD_E2E_TEST_ARTIFACTS_DIR=${BUILD_E2E_TEST_ARTIFACTS_DIR}" \
+            gcr.io/iree-oss/base@sha256:7c3027c48b94fc38e64488987fc7893c100526c57308d25cef0c6b76a2dfe117 \
+            build_tools/cmake/build_e2e_test_artifacts.sh
+          echo "::set-output name=e2e-test-artifacts-dir::${BUILD_E2E_TEST_ARTIFACTS_DIR}/e2e_test_artifacts"
+      - name: "Uploading e2e test artifacts"
+        id: upload
+        env:
+          E2E_TEST_ARTIFACTS_DIR: ${{ steps.build.outputs.e2e-test-artifacts-dir }}
+          E2E_TEST_ARTIFACTS_GCS_ARTIFACT_DIR: ${{ env.GCS_DIR }}/e2e-test-artifacts
+        run: |
+          # Not archiving the directory to allow fetching each file as needed
+          # separately. The option `--gzip-in-flight-all` compresses the files
+          # during uploading to save traffic. The files are still uncompressed
+          # on GCS.
+          # More details: https://cloud.google.com/sdk/gcloud/reference/storage/cp#--gzip-in-flight-all
+          gcloud alpha storage cp -r --gzip-in-flight-all "${E2E_TEST_ARTIFACTS_DIR}/iree" "${E2E_TEST_ARTIFACTS_GCS_ARTIFACT_DIR}/iree"
+          echo "::set-output name=e2e-test-artifacts-gcs-artifact-dir::${E2E_TEST_ARTIFACTS_GCS_ARTIFACT_DIR}"
+
   ############################## Crosscompilation ##############################
   # Jobs that cross-compile IREE for other platforms
   ##############################################################################
@@ -797,6 +855,9 @@
       - cross_compile_and_test
       - emscripten
 
+      # Artifacts for e2e testing and benchmarking
+      - build_e2e_test_artifacts
+
       # Benchmark pipeline
       - benchmarks
 
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 59a5788..d469847 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -731,6 +731,19 @@
   add_custom_target(iree-benchmark-suites)
 endif()
 
+# TODO(#11263): This conditional block should be merged with the block above
+# once we remove IREE_BUILD_BENCHMARKS.
+if(IREE_BUILD_EXPERIMENTAL_E2E_TEST_ARTIFACTS)
+  # iree-e2e-test-artifacts builds all e2e test artifacts, including benchmarks
+  # and model tests.
+  add_custom_target(iree-e2e-test-artifacts)
+
+  add_dependencies(iree-e2e-test-artifacts
+    iree-benchmark-import-models
+    iree-benchmark-suites
+  )
+endif()
+
 if(IREE_BUILD_MICROBENCHMARKS)
   # Add top-level custom targets to drive generating microbenchmark suites.
   add_custom_target(iree-microbenchmark-suites)
diff --git a/build_tools/cmake/build_e2e_test_artifacts.sh b/build_tools/cmake/build_e2e_test_artifacts.sh
new file mode 100755
index 0000000..0c0839a
--- /dev/null
+++ b/build_tools/cmake/build_e2e_test_artifacts.sh
@@ -0,0 +1,51 @@
+#!/bin/bash
+
+# Copyright 2022 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 e2e test artifacts using a host tools directory.
+# Designed for CI, but can be run locally.
+#
+# This is copied and modified from build_tools/cmake/build_benchmarks.sh.
+# We will remove build_tools/cmake/build_benchmarks.sh once everything has been
+# migrated.
+
+set -xeuo pipefail
+
+ROOT_DIR="${ROOT_DIR:-$(git rev-parse --show-toplevel)}"
+cd "${ROOT_DIR}"
+
+CMAKE_BIN=${CMAKE_BIN:-$(which cmake)}
+IREE_HOST_BINARY_ROOT="$(realpath ${IREE_HOST_BINARY_ROOT})"
+IREE_TF_BINARIES_DIR="${IREE_TF_BINARIES_DIR:-integrations/tensorflow/bazel-bin/iree_tf_compiler}"
+BUILD_E2E_TEST_ARTIFACTS_DIR="${BUILD_E2E_TEST_ARTIFACTS_DIR:-$ROOT_DIR/build-e2e-test-artifacts}"
+
+"$CMAKE_BIN" --version
+ninja --version
+
+if [[ -d "${BUILD_E2E_TEST_ARTIFACTS_DIR}" ]]; then
+  echo "${BUILD_E2E_TEST_ARTIFACTS_DIR} directory already exists. Will use cached results there."
+else
+  echo "${BUILD_E2E_TEST_ARTIFACTS_DIR} directory does not already exist. Creating a new one."
+  mkdir "${BUILD_E2E_TEST_ARTIFACTS_DIR}"
+fi
+
+echo "Configuring to build e2e test artifacts"
+"${CMAKE_BIN}" -B "${BUILD_E2E_TEST_ARTIFACTS_DIR}" \
+  -G Ninja \
+  -DIREE_HOST_BINARY_ROOT="${IREE_HOST_BINARY_ROOT}" \
+  -DIREE_BUILD_EXPERIMENTAL_E2E_TEST_ARTIFACTS=ON \
+  -DIREE_BUILD_COMPILER=OFF \
+  -DIREE_BUILD_SAMPLES=OFF \
+  -DIREE_BUILD_TESTS=OFF \
+  -DIREE_IMPORT_TFLITE_PATH="${IREE_TF_BINARIES_DIR}/iree-import-tflite" \
+  -DIREE_IMPORT_TF_PATH="${IREE_TF_BINARIES_DIR}/iree-import-tf"
+
+echo "Building e2e test artifacts"
+"${CMAKE_BIN}" \
+  --build "${BUILD_E2E_TEST_ARTIFACTS_DIR}" \
+  --target iree-e2e-test-artifacts \
+  -- -k 0