Rework CI setup variables (#12524)

`ci-stage` was a confusing concept that conflated a few different
things. I split those things out instead. We've had a few issues
related to this in the last week:
https://github.com/openxla/iree/pull/12482 and
https://github.com/openxla/iree/pull/12514, and posting benchmark
comments is still failing because that's triggered by `workflow_run`:
https://github.com/openxla/iree/actions/runs/4346904097.

This includes making booleans json rather than magic strings.
`write-caches` is still "0" or "1" because it's ultimately used by
bash.
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index c34cc8c..55b9974 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -65,7 +65,7 @@
       BASE_REF: HEAD^
     outputs:
       should-run: ${{ steps.configure.outputs.should-run }}
-      ci-stage: ${{ steps.configure.outputs.ci-stage }}
+      is-pr: ${{ steps.configure.outputs.is-pr }}
       runner-env: ${{ steps.configure.outputs.runner-env }}
       runner-group: ${{ steps.configure.outputs.runner-group }}
       write-caches: ${{ steps.configure.outputs.write-caches }}
@@ -126,7 +126,7 @@
   ##############################################################################
   build_all:
     needs: setup
-    if: needs.setup.outputs.should-run == 'true'
+    if: fromJson(needs.setup.outputs.should-run)
     runs-on:
       - self-hosted  # must come first
       - runner-group=${{ needs.setup.outputs.runner-group }}
@@ -191,7 +191,7 @@
 
   build_test_all_windows:
     needs: setup
-    if: needs.setup.outputs.should-run == 'true' && needs.setup.outputs.ci-stage == 'postsubmit'
+    if: fromJson(needs.setup.outputs.should-run) && ! fromJson(needs.setup.outputs.is-pr)
     runs-on: managed-windows-cpu
     defaults:
       run:
@@ -258,7 +258,7 @@
 
   build_test_all_macos:
     needs: setup
-    if: needs.setup.outputs.should-run == 'true' && needs.setup.outputs.ci-stage == 'postsubmit'
+    if: fromJson(needs.setup.outputs.should-run) && ! fromJson(needs.setup.outputs.is-pr)
     runs-on:
       - ${{ github.repository == 'openxla/iree' && 'self-hosted' || 'macos-11' }}  # must come first
       - runner-group=postsubmit
@@ -291,7 +291,7 @@
 
   build_test_all_bazel:
     needs: setup
-    if: needs.setup.outputs.should-run == 'true'
+    if: fromJson(needs.setup.outputs.should-run)
     runs-on:
       - self-hosted  # must come first
       - runner-group=${{ needs.setup.outputs.runner-group }}
@@ -314,7 +314,7 @@
 
   test_all:
     needs: [setup, build_all]
-    if: needs.setup.outputs.should-run == 'true'
+    if: fromJson(needs.setup.outputs.should-run)
     runs-on:
       - self-hosted  # must come first
       - runner-group=${{ needs.setup.outputs.runner-group }}
@@ -344,7 +344,7 @@
 
   test_gpu:
     needs: [setup, build_all]
-    if: needs.setup.outputs.should-run == 'true'
+    if: fromJson(needs.setup.outputs.should-run)
     runs-on:
       - self-hosted  # must come first
       - runner-group=${{ needs.setup.outputs.runner-group }}
@@ -387,7 +387,7 @@
   ##############################################################################
   build_test_runtime:
     needs: setup
-    if: needs.setup.outputs.should-run == 'true'
+    if: fromJson(needs.setup.outputs.should-run)
     runs-on: ubuntu-20.04-64core
     env:
       BUILD_DIR: build-runtime
@@ -415,7 +415,7 @@
 
   build_test_runtime_windows:
     needs: setup
-    if: needs.setup.outputs.should-run == 'true'
+    if: fromJson(needs.setup.outputs.should-run)
     runs-on: managed-windows-cpu
     defaults:
       run:
@@ -444,7 +444,7 @@
   ##############################################################################
   build_tf_integrations:
     needs: setup
-    if: needs.setup.outputs.should-run == 'true'
+    if: fromJson(needs.setup.outputs.should-run)
     runs-on:
       - self-hosted  # must come first
       - runner-group=${{ needs.setup.outputs.runner-group }}
@@ -491,7 +491,7 @@
 
   test_tf_integrations:
     needs: [setup, build_all, build_tf_integrations]
-    if: needs.setup.outputs.should-run == 'true'
+    if: fromJson(needs.setup.outputs.should-run)
     runs-on:
       - self-hosted  # must come first
       - runner-group=${{ needs.setup.outputs.runner-group }}
@@ -530,7 +530,7 @@
 
   test_tf_integrations_gpu:
     needs: [setup, build_all, build_tf_integrations]
-    if: needs.setup.outputs.should-run == 'true'
+    if: fromJson(needs.setup.outputs.should-run)
     runs-on:
       - self-hosted  # must come first
       - runner-group=${{ needs.setup.outputs.runner-group }}
@@ -578,7 +578,7 @@
   # TODO(#11263): Drop this job once the IREE_BUILD_BENCHMARKS is removed.
   test_build_benchmark_suites:
     needs: [setup, build_all, build_tf_integrations]
-    if: needs.setup.outputs.should-run == 'true'
+    if: fromJson(needs.setup.outputs.should-run)
     runs-on:
       - self-hosted  # must come first
       - runner-group=${{ needs.setup.outputs.runner-group }}
@@ -621,7 +621,7 @@
   ##############################################################################
   python_release_packages:
     needs: setup
-    if: needs.setup.outputs.should-run == 'true'
+    if: fromJson(needs.setup.outputs.should-run)
     runs-on:
       - self-hosted  # must come first
       - runner-group=${{ needs.setup.outputs.runner-group }}
@@ -662,7 +662,7 @@
 
   asan:
     needs: setup
-    if: needs.setup.outputs.should-run == 'true'
+    if: fromJson(needs.setup.outputs.should-run)
     runs-on:
       - self-hosted  # must come first
       - runner-group=${{ needs.setup.outputs.runner-group }}
@@ -690,7 +690,7 @@
 
   tsan:
     needs: setup
-    if: needs.setup.outputs.should-run == 'true'
+    if: fromJson(needs.setup.outputs.should-run)
     runs-on:
       - self-hosted  # must come first
       - runner-group=${{ needs.setup.outputs.runner-group }}
@@ -715,7 +715,7 @@
 
   small_runtime:
     needs: setup
-    if: needs.setup.outputs.should-run == 'true'
+    if: fromJson(needs.setup.outputs.should-run)
     runs-on: ubuntu-20.04-64core
     env:
       BUILD_DIR: build-runtime
@@ -742,7 +742,7 @@
 
   gcc:
     needs: setup
-    if: needs.setup.outputs.should-run == 'true'
+    if: fromJson(needs.setup.outputs.should-run)
     runs-on:
       - self-hosted  # must come first
       - runner-group=${{ needs.setup.outputs.runner-group }}
@@ -774,7 +774,7 @@
 
   tracing:
     needs: setup
-    if: needs.setup.outputs.should-run == 'true'
+    if: fromJson(needs.setup.outputs.should-run)
     runs-on:
       - self-hosted  # must come first
       - runner-group=${{ needs.setup.outputs.runner-group }}
@@ -803,7 +803,7 @@
 
   debug:
     needs: setup
-    if: needs.setup.outputs.should-run == 'true'
+    if: fromJson(needs.setup.outputs.should-run)
     runs-on:
       - self-hosted  # must come first
       - runner-group=${{ needs.setup.outputs.runner-group }}
@@ -836,7 +836,7 @@
 
   build_benchmark_tools:
     needs: [setup, build_all]
-    if: needs.setup.outputs.should-run == 'true'
+    if: fromJson(needs.setup.outputs.should-run)
     runs-on:
       - self-hosted  # must come first
       - runner-group=${{ needs.setup.outputs.runner-group }}
@@ -919,7 +919,7 @@
 
   build_e2e_test_artifacts:
     needs: [setup, build_all, build_tf_integrations]
-    if: needs.setup.outputs.should-run == 'true'
+    if: fromJson(needs.setup.outputs.should-run)
     runs-on:
       - self-hosted  # must come first
       - runner-group=${{ needs.setup.outputs.runner-group }}
@@ -987,7 +987,7 @@
 
   compilation_benchmarks:
     needs: [setup, build_e2e_test_artifacts]
-    if: needs.setup.outputs.should-run == 'true' && needs.setup.outputs.benchmark-presets != ''
+    if: fromJson(needs.setup.outputs.should-run) && needs.setup.outputs.benchmark-presets != ''
     runs-on:
       - self-hosted  # must come first
       - runner-group=${{ needs.setup.outputs.runner-group }}
@@ -1061,7 +1061,7 @@
 
   execution_benchmarks:
     needs: [setup, build_benchmark_tools, build_e2e_test_artifacts]
-    if: needs.setup.outputs.should-run == 'true' && needs.setup.outputs.benchmark-presets != ''
+    if: fromJson(needs.setup.outputs.should-run) && needs.setup.outputs.benchmark-presets != ''
     uses: ./.github/workflows/benchmark_execution.yml
     with:
       # env.GCS_DIR is also duplicated in this workflow. See the note there on
@@ -1075,7 +1075,7 @@
 
   process_benchmark_results:
     needs: [setup, compilation_benchmarks, execution_benchmarks]
-    if: needs.setup.outputs.should-run == 'true' && needs.setup.outputs.benchmark-presets != ''
+    if: fromJson(needs.setup.outputs.should-run) && needs.setup.outputs.benchmark-presets != ''
     runs-on:
       - self-hosted  # must come first
       - runner-group=${{ needs.setup.outputs.runner-group }}
@@ -1112,7 +1112,7 @@
             "${EXECUTION_BENCHMARK_RESULTS_DIR}"
           echo "execution-benchmark-results-pattern=${EXECUTION_BENCHMARK_RESULTS_DIR}/benchmark-results-*.json" >> "${GITHUB_OUTPUT}"
       - name: Generating comment
-        if: needs.setup.outputs.ci-stage == 'presubmit'
+        if: fromJson(needs.setup.outputs.is-pr)
         id: generate-comment
         env:
           # Wildcard pattern to match all execution benchmark results. Empty if
@@ -1139,7 +1139,7 @@
         # Due to security reasons, instead of posting the comment to PR, we only
         # upload the comment data in presubmit workflow and trigger the posting
         # workflow on the main branch. See post_benchmark_comment.yaml
-        if: needs.setup.outputs.ci-stage == 'presubmit'
+        if: fromJson(needs.setup.outputs.is-pr)
         env:
           BENCHMARK_COMMENT_ARTIFACT: ${{ steps.generate-comment.outputs.benchmark-comment-artifact }}
           BENCHMARK_COMMENT_GCS_ARTIFACT: ${{ env.GCS_DIR }}/${{ steps.generate-comment.outputs.benchmark-comment-artifact }}
@@ -1148,7 +1148,7 @@
             "${BENCHMARK_COMMENT_ARTIFACT}" \
             "${BENCHMARK_COMMENT_GCS_ARTIFACT}"
       - name: Uploading results to dashboard
-        if: needs.setup.outputs.ci-stage == 'postsubmit'
+        if: github.ref_name == 'main'
         env:
           EXECUTION_BENCHMARK_RESULTS_PATTERN: ${{ steps.download-execution-results.outputs.execution-benchmark-results-pattern }}
           IREE_DASHBOARD_API_TOKEN: ${{ secrets.IREE_DASHBOARD_API_TOKEN }}
@@ -1167,7 +1167,7 @@
 
   cross_compile_and_test:
     needs: [setup, build_all]
-    if: needs.setup.outputs.should-run == 'true'
+    if: fromJson(needs.setup.outputs.should-run)
     runs-on:
       - self-hosted  # must come first
       - runner-group=${{ needs.setup.outputs.runner-group }}
@@ -1256,7 +1256,7 @@
 
   test_benchmark_suites:
     needs: [setup, build_all, build_e2e_test_artifacts]
-    if: needs.setup.outputs.should-run == 'true'
+    if: fromJson(needs.setup.outputs.should-run)
     runs-on:
       - self-hosted  # must come first
       - runner-group=${{ needs.setup.outputs.runner-group }}
@@ -1393,7 +1393,7 @@
           fi
       - name: Posting to Discord
         uses: sarisia/actions-status-discord@c193626e5ce172002b8161e116aa897de7ab5383 # v1.10.2
-        if: failure() && needs.setup.outputs.ci-stage == 'postsubmit'
+        if: failure() && github.ref_name == 'main'
         with:
           webhook: ${{ secrets.DISCORD_WEBHOOK }}
           description: "The following jobs failed: ${{ steps.failed_jobs.outputs.failed-jobs }}"
diff --git a/.github/workflows/run_shark_tank.yml b/.github/workflows/run_shark_tank.yml
index d1a4dc6..6c8f768 100644
--- a/.github/workflows/run_shark_tank.yml
+++ b/.github/workflows/run_shark_tank.yml
@@ -20,7 +20,6 @@
       PR_BODY: ${{ github.event.pull_request.body }}
     outputs:
       should-run: ${{ steps.configure.outputs.should-run }}
-      ci-stage: ${{ steps.configure.outputs.ci-stage }}
       runner-env: ${{ steps.configure.outputs.runner-env }}
       runner-group: ${{ steps.configure.outputs.runner-group }}
       write-caches: ${{ steps.configure.outputs.write-caches }}
diff --git a/build_tools/github_actions/configure_ci.py b/build_tools/github_actions/configure_ci.py
index 49ac80d..04309a2 100755
--- a/build_tools/github_actions/configure_ci.py
+++ b/build_tools/github_actions/configure_ci.py
@@ -29,15 +29,12 @@
 
 import difflib
 import fnmatch
+import json
 import os
 import subprocess
 import textwrap
 from typing import Iterable, Mapping, MutableMapping
 
-PULL_REQUEST_EVENT_NAME = "pull_request"
-PUSH_EVENT_NAME = "push"
-SCHEDULE_EVENT_NAME = "schedule"
-WORKFLOW_DISPATCH_EVENT_NAME = "workflow_dispatch"
 SKIP_CI_KEY = "skip-ci"
 RUNNER_ENV_KEY = "runner-env"
 BENCHMARK_PRESET_KEY = "benchmarks"
@@ -86,7 +83,7 @@
   print(f"Setting outputs: {d}")
   step_output_file = os.environ["GITHUB_OUTPUT"]
   with open(step_output_file, "a") as f:
-    f.writelines(f"{k}={v}" "\n" for k, v in d.items())
+    f.writelines(f"{k}={v}" + "\n" for k, v in d.items())
 
 
 def write_job_summary(summary: str):
@@ -121,7 +118,9 @@
   </details>""").format("".join(diffs)))
 
 
-def get_trailers() -> Mapping[str, str]:
+def get_trailers(is_pr: bool) -> Mapping[str, str]:
+  if not is_pr:
+    return {}
   title = os.environ["PR_TITLE"]
   body = os.environ.get("PR_BODY", "")
   original_title = os.environ.get("ORIGINAL_PR_TITLE")
@@ -167,10 +166,10 @@
   return any(not skip_path(p) for p in get_modified_paths(base_ref))
 
 
-def should_run_ci(event_name, trailers) -> bool:
-  if event_name != PULL_REQUEST_EVENT_NAME:
-    print(f"Running CI independent of diff because run was not triggered by a"
-          f" pull request event (event name is '{event_name}')")
+def should_run_ci(is_pr: bool, trailers: Mapping[str, str]) -> bool:
+  if not is_pr:
+    print("Running CI independent of diff because run was not triggered by a"
+          " pull request event.")
     return True
 
   if SKIP_CI_KEY in trailers:
@@ -204,19 +203,7 @@
   return runner_env
 
 
-def get_ci_stage(event_name):
-  if event_name == PULL_REQUEST_EVENT_NAME:
-    return "presubmit"
-  elif event_name == PUSH_EVENT_NAME:
-    return "postsubmit"
-  elif event_name == SCHEDULE_EVENT_NAME:
-    return "postsubmit"
-  elif event_name == WORKFLOW_DISPATCH_EVENT_NAME:
-    return "unknown"
-  raise ValueError(f"Unrecognized event name '{event_name}'")
-
-
-def get_benchmark_presets(ci_stage: str, trailers: Mapping[str, str]) -> str:
+def get_benchmark_presets(is_pr: bool, trailers: Mapping[str, str]) -> str:
   """Parses and validates the benchmark presets from trailers.
 
   Args:
@@ -226,7 +213,7 @@
     A comma separated preset string, which later will be parsed by
     build_tools/benchmarks/export_benchmark_config.py.
   """
-  if ci_stage == "postsubmit":
+  if not is_pr:
     preset_options = ["all"]
   else:
     trailer = trailers.get(BENCHMARK_PRESET_KEY)
@@ -248,22 +235,16 @@
 
 
 def main():
-  output: MutableMapping[str, str] = {}
-  event_name = os.environ["GITHUB_EVENT_NAME"]
-  trailers = get_trailers() if event_name == PULL_REQUEST_EVENT_NAME else {}
-  if should_run_ci(event_name, trailers):
-    output["should-run"] = "true"
-  else:
-    output["should-run"] = "false"
-  output[RUNNER_ENV_KEY] = get_runner_env(trailers)
-  ci_stage = get_ci_stage(event_name)
-  output["ci-stage"] = ci_stage
-  output["runner-group"] = ci_stage
-  write_caches = "0"
-  if ci_stage == "postsubmit":
-    write_caches = "1"
-  output["write-caches"] = write_caches
-  output["benchmark-presets"] = get_benchmark_presets(ci_stage, trailers)
+  is_pr = os.environ["GITHUB_EVENT_NAME"] == "pull_request"
+  trailers = get_trailers(is_pr)
+  output = {
+      "should-run": json.dumps(should_run_ci(is_pr, trailers)),
+      "is-pr": json.dumps(is_pr),
+      "runner-env": get_runner_env(trailers),
+      "runner-group": "presubmit" if is_pr else "postsubmit",
+      "write-caches": "0" if is_pr else "1",
+      "benchmark-presets": get_benchmark_presets(is_pr, trailers),
+  }
 
   set_output(output)