Add CI job to build benchmark tools (#10233)

Co-authored-by: Geoffrey Martin-Noble <gcmn@google.com>
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 11b0342..b121d08 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -561,7 +561,7 @@
       - os-family=Linux
     env:
       BUILD_RISCV_DIR: "build-riscv32-baremetal"
-      RISCV_CONFIG: "rv32-baremetal"
+      BUILD_ARCH: "rv32-baremetal"
       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 }}
@@ -577,8 +577,9 @@
       - name: "Cross-compiling and testing riscv32"
         run: |
           ./build_tools/github_actions/docker_run.sh \
-            --env "RISCV_CONFIG=${RISCV_CONFIG}" \
+            --env "RISCV_ARCH=${BUILD_ARCH}" \
             --env "BUILD_RISCV_DIR=${BUILD_RISCV_DIR}" \
+            --env "BUILD_PRESET=test" \
             --env "IREE_HOST_BINARY_ROOT=${BUILD_DIR}/install" \
             gcr.io/iree-oss/riscv@sha256:720bc0215d8462ea14352edc22710a6ce4c0c1daff581d179dd173885f1d8a35 \
             bash -euo pipefail -c \
@@ -595,7 +596,7 @@
       - os-family=Linux
     env:
       BUILD_RISCV_DIR: "build-riscv64"
-      RISCV_CONFIG: "rv64"
+      BUILD_ARCH: "rv64"
       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 }}
@@ -618,8 +619,9 @@
       - name: "Cross-compiling and testing riscv64"
         run: |
           ./build_tools/github_actions/docker_run.sh \
-            --env "RISCV_CONFIG=${RISCV_CONFIG}" \
+            --env "RISCV_ARCH=${BUILD_ARCH}" \
             --env "BUILD_RISCV_DIR=${BUILD_RISCV_DIR}" \
+            --env "BUILD_PRESET=test" \
             --env "IREE_HOST_BINARY_ROOT=${BUILD_DIR}/install" \
             --env "IREE_IMPORT_TFLITE_BIN=${TF_BINARIES_DIR}/iree-import-tflite" \
             --env "LLVM_BIN_DIR=${BUILD_DIR}/third_party/llvm-project/llvm/bin" \
@@ -627,6 +629,77 @@
             bash -euo pipefail -c \
               "./build_tools/cmake/build_riscv.sh && ./tests/riscv64/smoke.sh"
 
+  build_benchmark_tools:
+    needs: [should_run, build_all]
+    if: needs.should_run.outputs.should-run == 'true'
+    runs-on:
+      # Pseudo-ternary hack and order matters. See comment at top of file.
+      - self-hosted
+      - runner-group=${{ github.event_name == 'pull_request' && 'presubmit' || 'postsubmit' }}
+      - cpu
+      - os-family=Linux
+    strategy:
+      matrix:
+        target:
+          - platform: "riscv"
+            architecture: "rv64"
+            docker_image: "gcr.io/iree-oss/riscv@sha256:720bc0215d8462ea14352edc22710a6ce4c0c1daff581d179dd173885f1d8a35"
+    outputs:
+      benchmark-tools-gcs-artifacts: ${{ toJSON(steps.upload.outputs) }}
+    env:
+      PLATFORM: ${{ matrix.target.platform }}
+      ARCHITECTURE: ${{ matrix.target.architecture }}
+      DOCKER_IMAGE: ${{ matrix.target.docker_image }}
+      BUILD_TOOLS_DIR: ${{ matrix.target.platform }}-${{ matrix.target.architecture }}-benchmark-tools-dir
+      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 }}
+    steps:
+      - name: "Checking out repository"
+        uses: actions/checkout@7884fcad6b5d53d10323aee724dc68d8b9096a2e # v2
+        with:
+          submodules: true
+      - name: "Downloading build dir archive"
+        run: gcloud alpha storage cp "${BUILD_DIR_GCS_ARTIFACT}" "${BUILD_DIR_ARCHIVE}"
+      - name: "Extracting host binaries"
+        run: tar -xf "${BUILD_DIR_ARCHIVE}" "${BUILD_DIR}/install"
+      - name: "Compiling the benchmark tools"
+        id: build
+        run: |
+          ./build_tools/github_actions/docker_run.sh \
+            --env "${PLATFORM^^}_ARCH=${ARCHITECTURE}" \
+            --env "BUILD_${PLATFORM^^}_DIR=${BUILD_TOOLS_DIR}/build" \
+            --env "BUILD_PRESET=benchmark" \
+            --env "IREE_HOST_BINARY_ROOT=${BUILD_DIR}/install" \
+            "${DOCKER_IMAGE}" \
+            bash -euo pipefail -c "./build_tools/cmake/build_${PLATFORM}.sh"
+      - name: "Compiling the benchmark tools with tracing"
+        id: build-with-tracing
+        run: |
+          ./build_tools/github_actions/docker_run.sh \
+            --env "${PLATFORM^^}_ARCH=${ARCHITECTURE}" \
+            --env "BUILD_${PLATFORM^^}_DIR=${BUILD_TOOLS_DIR}/build-traced" \
+            --env "BUILD_PRESET=benchmark-with-tracing" \
+            --env "IREE_HOST_BINARY_ROOT=${BUILD_DIR}/install" \
+            "${DOCKER_IMAGE}" \
+            bash -euo pipefail -c "./build_tools/cmake/build_${PLATFORM}.sh"
+      - name: "Creating the benchmark tools archive"
+        id: archive
+        env:
+          BENCHMARK_TOOLS_ARCHIVE: ${{ matrix.target.platform }}-${{ matrix.target.architecture }}-benchmarks-tools.tar
+        run: |
+          tar -cf ${BENCHMARK_TOOLS_ARCHIVE} \
+            ${BUILD_TOOLS_DIR}/*/tools/iree-benchmark-module
+          echo "::set-output name=benchmark-tools-archive::${BENCHMARK_TOOLS_ARCHIVE}"
+      - name: "Uploading the benchmark tools archive"
+        id: upload
+        env:
+          BENCHMARK_TOOLS_ARCHIVE: ${{ steps.archive.outputs.benchmark-tools-archive }}
+          BENCHMARK_TOOLS_GCS_ARTIFACT: ${{ env.GCS_DIR }}/${{ steps.archive.outputs.benchmark-tools-archive }}
+        run: |
+          gcloud alpha storage cp "${BENCHMARK_TOOLS_ARCHIVE}" "${BENCHMARK_TOOLS_GCS_ARTIFACT}"
+          echo "::set-output name=${PLATFORM}-${ARCHITECTURE}-benchmark-tools-gcs-artifact::${BENCHMARK_TOOLS_GCS_ARTIFACT}"
+
   ##############################################################################
 
   # Depends on all the other jobs to provide a single anchor that indicates the
@@ -660,6 +733,9 @@
       - android_arm64
       - riscv32
       - riscv64
+
+      # Benchmark tools
+      - build_benchmark_tools
     steps:
       - name: Getting combined job status
         run: |
diff --git a/build_tools/cmake/build_riscv.sh b/build_tools/cmake/build_riscv.sh
index 7c57f37..a399d94 100755
--- a/build_tools/cmake/build_riscv.sh
+++ b/build_tools/cmake/build_riscv.sh
@@ -12,6 +12,9 @@
 #
 # Host binaries (e.g. compiler tools) will be built and installed in build-host/
 # RISCV binaries (e.g. tests) will be built in build-riscv/.
+#
+# BUILD_PRESET can be: test, benchmark, benchmark-with-tracing to build with
+# different flags.
 
 set -xeuo pipefail
 
@@ -19,10 +22,11 @@
 cd "${ROOT_DIR}"
 
 CMAKE_BIN="${CMAKE_BIN:-$(which cmake)}"
-RISCV_CONFIG="${RISCV_CONFIG:-rv64}"
+RISCV_ARCH="${RISCV_ARCH:-rv64}"
 RISCV_COMPILER_FLAGS="${RISCV_COMPILER_FLAGS:--O3}"
 IREE_HOST_BINARY_ROOT="$(realpath ${IREE_HOST_BINARY_ROOT})"
 BUILD_RISCV_DIR="${BUILD_RISCV_DIR:-$ROOT_DIR/build-riscv}"
+BUILD_PRESET="${BUILD_PRESET:-test}"
 
 # --------------------------------------------------------------------------- #
 # Build for the target (riscv).
@@ -33,7 +37,7 @@
   mkdir -p "${BUILD_RISCV_DIR}"
 fi
 
-echo "Build riscv target with the config of ${RISCV_CONFIG}"
+echo "Build riscv target with the config of ${RISCV_ARCH}"
 TOOLCHAIN_FILE="$(realpath ${ROOT_DIR}/build_tools/cmake/riscv.toolchain.cmake)"
 declare -a args
 args=(
@@ -41,20 +45,18 @@
   "-B" "${BUILD_RISCV_DIR}"
   -DCMAKE_TOOLCHAIN_FILE="${TOOLCHAIN_FILE}"
   -DIREE_HOST_BINARY_ROOT="${IREE_HOST_BINARY_ROOT}"
-  -DRISCV_CPU="${RISCV_CONFIG}"
+  -DRISCV_CPU="${RISCV_ARCH}"
   -DRISCV_COMPILER_FLAGS="${RISCV_COMPILER_FLAGS}"
-  -DIREE_ENABLE_ASSERTIONS=ON
   -DIREE_BUILD_COMPILER=OFF
   # CPU info doesn't work on RISCV
   -DIREE_ENABLE_CPUINFO=OFF
-  -DIREE_BUILD_SAMPLES=ON
 )
 
-if [[ "${RISCV_CONFIG}" == "rv64" ]]; then
+if [[ "${RISCV_ARCH}" == "rv64" ]]; then
   args+=(
     -DRISCV_TOOLCHAIN_ROOT="${RISCV_RV64_LINUX_TOOLCHAIN_ROOT}"
   )
-elif [[ "${RISCV_CONFIG}" == "rv32-baremetal" ]]; then
+elif [[ "${RISCV_ARCH}" == "rv32-baremetal" ]]; then
   args+=(
     # TODO(#6353): Off until tools/ are refactored to support threadless config.
     -DIREE_BUILD_TESTS=OFF
@@ -65,6 +67,34 @@
   return -1
 fi
 
+case "${BUILD_PRESET}" in
+  test)
+    args+=(
+      -DIREE_ENABLE_ASSERTIONS=ON
+      -DIREE_BUILD_SAMPLES=ON
+    )
+    ;;
+  benchmark)
+    args+=(
+      -DIREE_ENABLE_ASSERTIONS=OFF
+      -DIREE_BUILD_SAMPLES=OFF
+      -DIREE_BUILD_TESTS=OFF
+    )
+    ;;
+  benchmark-with-tracing)
+    args+=(
+      -DIREE_ENABLE_ASSERTIONS=OFF
+      -DIREE_BUILD_SAMPLES=OFF
+      -DIREE_BUILD_TESTS=OFF
+      -DIREE_ENABLE_RUNTIME_TRACING=ON
+    )
+    ;;
+  *)
+    echo "Unknown build preset: ${BUILD_PRESET}"
+    exit 1
+    ;;
+esac
+
 args_str=$(IFS=' ' ; echo "${args[*]}")
 "${CMAKE_BIN}" ${args_str} "${ROOT_DIR}"
 "${CMAKE_BIN}" --build "${BUILD_RISCV_DIR}" -- -k 0