[clang] [llvm] [UpdateTestChecks][Clang][CIR] Add support for CIR in update_cc_test_checks.py (PR #241654)
Vicky Nguyen via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 11 19:54:40 PST 2026
https://github.com/iamvickynguyen created https://github.com/llvm/llvm-project/pull/241654
## Motivation
- Clang tests for builtins SVE now run through two paths: classic CodeGen and ClangIR.
- Today the CIR checks are written by hand, and the LLVM IR checks are often copied once per path.
- This patch lets `update_cc_test_checks.py` write both, and share one set of checks between the paths.
## What the script does now
- **Understands CIR RUN lines.** A RUN line with `-emit-cir` gets `cir.func` checks. Debug locations (`loc(...)`) are removed.
- **Reads more lit syntax.**
- `%if <feature> %{ ... %} [%else %{ ... %}]`
- `// DEFINE: %{name} = value` (and `REDEFINE`)
- `%clang_cc1` shortcuts from the test's `lit.local.cfg` (e.g. `%clang_cc1_cg_arm64_sve`).
- **Shares checks across paths.** Two kinds of prefixes:
- *Label prefixes* (e.g. `C`, `CPP`): only a `-LABEL` line with the function name. C and C++ mangle names differently, so each gets its own.
- *Body prefixes* (e.g. `LLVM`, `CIR`): the function body, written once and used by every RUN line with that prefix.
- **Hides small naming differences.** Drops the `entry:` label and renumbers local values, so classic and CIR LLVM IR look the same.
## New options
| Option | What it does |
|---|---|
| `--lit-features a,b` | Features treated as available when resolving `%if`. Others are off. |
| `--body-prefixes LLVM,CIR` | Prefixes that hold shared bodies. All others get only a `-LABEL`. |
| `--normalize-local-names` | Drop `entry:` and renumber locals before making checks. |
Existing options still work. `--filter-out` is useful to hide CIR noise like `cir.alloca` / `cir.store` / `cir.load`.
## How to run
Input: `clang/test/utils/update_cc_test_checks/Inputs/cir-sve-recpe.c` (SVE **n`recpe`** test with no checks). Its RUN lines use `%clang_cc1_cg_arm64_sve`, so
the script needs the AArch64 `lit.local.cfg` next to the file. Same steps as
`cir-sve-recpe.test`:
```bash
mkdir -p /tmp/utc && cd /tmp/utc
cp $LLVM/clang/test/CodeGen/AArch64/lit.local.cfg .
cp $LLVM/clang/test/utils/update_cc_test_checks/Inputs/cir-sve-recpe.c test.c
$LLVM/llvm/utils/update_cc_test_checks.py \
--clang $LLVM/build/bin/clang --opt $LLVM/build/bin/opt \
--version 6 \
--lit-features cir-enabled \
--body-prefixes LLVM,CIR \
--normalize-local-names \
--filter-out "cir\.(alloca|store|load|return)" \
test.c
diff -u $LLVM/clang/test/utils/update_cc_test_checks/Inputs/cir-sve-recpe.c.expected test.c
```
- Options are saved in the `UTC_ARGS` note, so later runs need only the file name: `update_cc_test_checks.py --clang ... test.c`.
- Or run the test: `build/bin/llvm-lit -v clang/test/utils/update_cc_test_checks/cir-sve-recpe.test`
## Expected output
>From `Inputs/cir-sve-recpe.c.expected`. First line is the saved options:
```c
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --lit-features cir-enabled --body-prefixes LLVM,CIR --normalize-local-names --filter-out "cir\.(alloca|store|load|return)" --version 6
```
For each function, one short block that focuses on the intrinsic call:
```c
// C-LABEL: @test_svrecpe_f16(
// CPP-LABEL: @_Z16test_svrecpe_f16u13__SVFloat16_t(
// CIR: [[TMP3:%.*]] = cir.call_llvm_intrinsic "aarch64.sve.frecpe.x" [[TMP2:%.*]] : (!cir.vector<[8] x !cir.f16>) -> !cir.vector<[8] x !cir.f16>
// LLVM-SAME: <vscale x 8 x half> [[TMP0:%.*]])
// LLVM: [[TMP1:%.*]] = call <vscale x 8 x half> @llvm.aarch64.sve.frecpe.x.nxv8f16(<vscale x 8 x half> [[TMP0]])
// LLVM: ret <vscale x 8 x half> [[TMP1]]
//
svfloat16_t test_svrecpe_f16(svfloat16_t op) MODE_ATTR
```
## Limitation
- Tested on SVE, not with others yet.
## Relates to
- Discussion https://github.com/llvm/llvm-project/pull/236314#discussion_r4424220790
- Part of https://github.com/llvm/llvm-project/issues/223963.
>From 34ca9f379e1fc467ae8945e7fccc4f18dea28eb1 Mon Sep 17 00:00:00 2001
From: Vicky Nguyen <vicky.trucviennguyen at gmail.com>
Date: Mon, 9 Nov 2026 20:46:41 -0700
Subject: [PATCH] [UpdateTestChecks][Clang][CIR] Generate CIR checks in
update_cc_test_checks.py
Let update_cc_test_checks.py write CIR checks next to LLVM IR checks,
and share one set of checks between the classic and CIR paths.
- New file UpdateTestChecks/cir.py for the CIR parts.
- Read %if and DEFINE lines, and the %clang_cc1 shortcuts from the
test's lit.local.cfg.
- Option to hide small naming differences between the two paths.
- Add one test, based on the SVE recpe test.
Co-Authored-By: Claude Opus 5.5 <noreply at anthropic.com>
---
.../Inputs/cir-sve-recpe.c | 48 +++++
.../Inputs/cir-sve-recpe.c.expected | 63 ++++++
.../update_cc_test_checks/cir-sve-recpe.test | 20 ++
llvm/utils/UpdateTestChecks/cir.py | 194 ++++++++++++++++++
llvm/utils/UpdateTestChecks/common.py | 111 +++++++++-
llvm/utils/update_cc_test_checks.py | 187 ++++++++++++++---
6 files changed, 592 insertions(+), 31 deletions(-)
create mode 100644 clang/test/utils/update_cc_test_checks/Inputs/cir-sve-recpe.c
create mode 100644 clang/test/utils/update_cc_test_checks/Inputs/cir-sve-recpe.c.expected
create mode 100644 clang/test/utils/update_cc_test_checks/cir-sve-recpe.test
create mode 100644 llvm/utils/UpdateTestChecks/cir.py
diff --git a/clang/test/utils/update_cc_test_checks/Inputs/cir-sve-recpe.c b/clang/test/utils/update_cc_test_checks/Inputs/cir-sve-recpe.c
new file mode 100644
index 00000000000000..b655abf007e567
--- /dev/null
+++ b/clang/test/utils/update_cc_test_checks/Inputs/cir-sve-recpe.c
@@ -0,0 +1,48 @@
+// REQUIRES: aarch64-registered-target
+
+// DEFINE: %{optimize} = opt -passes=mem2reg -S
+
+// RUN: %if cir-enabled %{%clang_cc1_cg_arm64_sve -fclangir -emit-cir -disable-O0-optnone -o - %s | FileCheck %s --check-prefixes=C,CIR %}
+// RUN: %if cir-enabled %{%clang_cc1_cg_arm64_sve -DSVE_OVERLOADED_FORMS -fclangir -emit-cir -disable-O0-optnone -o - %s | FileCheck %s --check-prefixes=C,CIR %}
+
+// RUN: %if cir-enabled %{%clang_cc1_cg_arm64_sve -fclangir -emit-llvm -disable-O0-optnone -o - %s | %{optimize} | FileCheck %s --check-prefixes=C,LLVM %}
+// RUN: %if cir-enabled %{%clang_cc1_cg_arm64_sve -DSVE_OVERLOADED_FORMS -fclangir -emit-llvm -disable-O0-optnone -o - %s | %{optimize} | FileCheck %s --check-prefixes=C,LLVM %}
+// RUN: %if cir-enabled %{%clang_cc1_cg_arm64_sve -DSVE_OVERLOADED_FORMS -fclangir -emit-llvm -disable-O0-optnone -o - -x c++ %s | %{optimize} | FileCheck %s --check-prefixes=CPP,LLVM %}
+
+// RUN: %clang_cc1_cg_arm64_sve -emit-llvm -disable-O0-optnone -o - %s | %{optimize} | FileCheck %s --check-prefixes=C,LLVM
+// RUN: %clang_cc1_cg_arm64_sve -DSVE_OVERLOADED_FORMS -emit-llvm -disable-O0-optnone -o - %s | %{optimize} | FileCheck %s --check-prefixes=C,LLVM
+// RUN: %clang_cc1_cg_arm64_sve -DSVE_OVERLOADED_FORMS -emit-llvm -disable-O0-optnone -o - -x c++ %s | %{optimize} | FileCheck %s --check-prefixes=CPP,LLVM
+
+// RUN: %clang_cc1_cg_arm64_sme -S -disable-O0-optnone -Werror -Wall -o /dev/null %s
+
+//=============================================================================
+// NOTES
+// Tests for SVE RECPE intrinsics
+//=============================================================================
+
+#include <arm_sve.h>
+
+#if defined __ARM_FEATURE_SME
+#define MODE_ATTR __arm_streaming
+#else
+#define MODE_ATTR
+#endif
+
+#ifdef SVE_OVERLOADED_FORMS
+// A simple used,unused... macro, long enough to represent any SVE builtin.
+#define SVE_ACLE_FUNC(A1,A2_UNUSED,A3,A4_UNUSED) A1##A3
+#else
+#define SVE_ACLE_FUNC(A1,A2,A3,A4) A1##A2##A3##A4
+#endif
+
+svfloat16_t test_svrecpe_f16(svfloat16_t op) MODE_ATTR
+{
+
+ return SVE_ACLE_FUNC(svrecpe,_f16,,)(op);
+}
+
+svfloat32_t test_svrecpe_f32(svfloat32_t op) MODE_ATTR
+{
+
+ return SVE_ACLE_FUNC(svrecpe,_f32,,)(op);
+}
diff --git a/clang/test/utils/update_cc_test_checks/Inputs/cir-sve-recpe.c.expected b/clang/test/utils/update_cc_test_checks/Inputs/cir-sve-recpe.c.expected
new file mode 100644
index 00000000000000..d9097b547f0dde
--- /dev/null
+++ b/clang/test/utils/update_cc_test_checks/Inputs/cir-sve-recpe.c.expected
@@ -0,0 +1,63 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --lit-features cir-enabled --body-prefixes LLVM,CIR --normalize-local-names --filter-out "cir\.(alloca|store|load|return)" --version 6
+// REQUIRES: aarch64-registered-target
+
+// DEFINE: %{optimize} = opt -passes=mem2reg -S
+
+// RUN: %if cir-enabled %{%clang_cc1_cg_arm64_sve -fclangir -emit-cir -disable-O0-optnone -o - %s | FileCheck %s --check-prefixes=C,CIR %}
+// RUN: %if cir-enabled %{%clang_cc1_cg_arm64_sve -DSVE_OVERLOADED_FORMS -fclangir -emit-cir -disable-O0-optnone -o - %s | FileCheck %s --check-prefixes=C,CIR %}
+
+// RUN: %if cir-enabled %{%clang_cc1_cg_arm64_sve -fclangir -emit-llvm -disable-O0-optnone -o - %s | %{optimize} | FileCheck %s --check-prefixes=C,LLVM %}
+// RUN: %if cir-enabled %{%clang_cc1_cg_arm64_sve -DSVE_OVERLOADED_FORMS -fclangir -emit-llvm -disable-O0-optnone -o - %s | %{optimize} | FileCheck %s --check-prefixes=C,LLVM %}
+// RUN: %if cir-enabled %{%clang_cc1_cg_arm64_sve -DSVE_OVERLOADED_FORMS -fclangir -emit-llvm -disable-O0-optnone -o - -x c++ %s | %{optimize} | FileCheck %s --check-prefixes=CPP,LLVM %}
+
+// RUN: %clang_cc1_cg_arm64_sve -emit-llvm -disable-O0-optnone -o - %s | %{optimize} | FileCheck %s --check-prefixes=C,LLVM
+// RUN: %clang_cc1_cg_arm64_sve -DSVE_OVERLOADED_FORMS -emit-llvm -disable-O0-optnone -o - %s | %{optimize} | FileCheck %s --check-prefixes=C,LLVM
+// RUN: %clang_cc1_cg_arm64_sve -DSVE_OVERLOADED_FORMS -emit-llvm -disable-O0-optnone -o - -x c++ %s | %{optimize} | FileCheck %s --check-prefixes=CPP,LLVM
+
+// RUN: %clang_cc1_cg_arm64_sme -S -disable-O0-optnone -Werror -Wall -o /dev/null %s
+
+//=============================================================================
+// NOTES
+// Tests for SVE RECPE intrinsics
+//=============================================================================
+
+#include <arm_sve.h>
+
+#if defined __ARM_FEATURE_SME
+#define MODE_ATTR __arm_streaming
+#else
+#define MODE_ATTR
+#endif
+
+#ifdef SVE_OVERLOADED_FORMS
+// A simple used,unused... macro, long enough to represent any SVE builtin.
+#define SVE_ACLE_FUNC(A1,A2_UNUSED,A3,A4_UNUSED) A1##A3
+#else
+#define SVE_ACLE_FUNC(A1,A2,A3,A4) A1##A2##A3##A4
+#endif
+
+// C-LABEL: @test_svrecpe_f16(
+// CPP-LABEL: @_Z16test_svrecpe_f16u13__SVFloat16_t(
+// CIR: [[TMP3:%.*]] = cir.call_llvm_intrinsic "aarch64.sve.frecpe.x" [[TMP2:%.*]] : (!cir.vector<[8] x !cir.f16>) -> !cir.vector<[8] x !cir.f16>
+// LLVM-SAME: <vscale x 8 x half> [[TMP0:%.*]])
+// LLVM: [[TMP1:%.*]] = call <vscale x 8 x half> @llvm.aarch64.sve.frecpe.x.nxv8f16(<vscale x 8 x half> [[TMP0]])
+// LLVM: ret <vscale x 8 x half> [[TMP1]]
+//
+svfloat16_t test_svrecpe_f16(svfloat16_t op) MODE_ATTR
+{
+
+ return SVE_ACLE_FUNC(svrecpe,_f16,,)(op);
+}
+
+// C-LABEL: @test_svrecpe_f32(
+// CPP-LABEL: @_Z16test_svrecpe_f32u13__SVFloat32_t(
+// CIR: [[TMP3:%.*]] = cir.call_llvm_intrinsic "aarch64.sve.frecpe.x" [[TMP2:%.*]] : (!cir.vector<[4] x !cir.float>) -> !cir.vector<[4] x !cir.float>
+// LLVM-SAME: <vscale x 4 x float> [[TMP0:%.*]])
+// LLVM: [[TMP1:%.*]] = call <vscale x 4 x float> @llvm.aarch64.sve.frecpe.x.nxv4f32(<vscale x 4 x float> [[TMP0]])
+// LLVM: ret <vscale x 4 x float> [[TMP1]]
+//
+svfloat32_t test_svrecpe_f32(svfloat32_t op) MODE_ATTR
+{
+
+ return SVE_ACLE_FUNC(svrecpe,_f32,,)(op);
+}
diff --git a/clang/test/utils/update_cc_test_checks/cir-sve-recpe.test b/clang/test/utils/update_cc_test_checks/cir-sve-recpe.test
new file mode 100644
index 00000000000000..086f2a4cabedce
--- /dev/null
+++ b/clang/test/utils/update_cc_test_checks/cir-sve-recpe.test
@@ -0,0 +1,20 @@
+# REQUIRES: cir-enabled, aarch64-registered-target
+## LLVM IR and CIR RUN lines of an SVE test share one set of checks, focused on
+## the intrinsic call. The test uses the real AArch64 lit.local.cfg, so this
+## also fails when its substitutions change.
+
+# RUN: rm -rf %t && mkdir %t
+# RUN: cp %S/../../CodeGen/AArch64/lit.local.cfg %t/lit.local.cfg
+# RUN: cp %S/Inputs/cir-sve-recpe.c %t/test.c
+# RUN: %update_cc_test_checks --version 6 --lit-features cir-enabled --body-prefixes LLVM,CIR --normalize-local-names --filter-out "cir\.(alloca|store|load|return)" %t/test.c
+# RUN: diff -u %S/Inputs/cir-sve-recpe.c.expected %t/test.c
+
+## The generated checks must hold against the real output of every pipeline.
+# RUN: %clang_cc1 -triple arm64-none-linux-gnu -target-feature +sve -fclangir -emit-cir -disable-O0-optnone -o - %t/test.c | FileCheck %t/test.c --check-prefixes=C,CIR
+# RUN: %clang_cc1 -triple arm64-none-linux-gnu -target-feature +sve -fclangir -emit-llvm -disable-O0-optnone -o - %t/test.c | opt -passes=mem2reg -S | FileCheck %t/test.c --check-prefixes=C,LLVM
+# RUN: %clang_cc1 -triple arm64-none-linux-gnu -target-feature +sve -emit-llvm -disable-O0-optnone -o - %t/test.c | opt -passes=mem2reg -S | FileCheck %t/test.c --check-prefixes=C,LLVM
+# RUN: %clang_cc1 -triple arm64-none-linux-gnu -target-feature +sve -DSVE_OVERLOADED_FORMS -emit-llvm -disable-O0-optnone -o - -x c++ %t/test.c | opt -passes=mem2reg -S | FileCheck %t/test.c --check-prefixes=CPP,LLVM
+
+## Check that re-running update_cc_test_checks doesn't change the output
+# RUN: %update_cc_test_checks %t/test.c
+# RUN: diff -u %S/Inputs/cir-sve-recpe.c.expected %t/test.c
diff --git a/llvm/utils/UpdateTestChecks/cir.py b/llvm/utils/UpdateTestChecks/cir.py
new file mode 100644
index 00000000000000..f086afd0badab2
--- /dev/null
+++ b/llvm/utils/UpdateTestChecks/cir.py
@@ -0,0 +1,194 @@
+"""CIR check generation for update_cc_test_checks.py.
+
+Generates checks for -emit-cir RUN lines, and lets LLVM IR and CIR RUN lines
+share function bodies under common -LABEL prefixes (--body-prefixes).
+
+Shared bodies are checked less strictly than normal checks: they use plain
+CHECK lines instead of -NEXT, skip global checks, and keep only the function
+arguments, not the return type or attributes. The RUN lines sharing a body can
+differ in exactly those parts, and they are not what these tests are about.
+"""
+
+from __future__ import print_function
+
+import collections
+import re
+import sys
+
+from . import common
+
+# The signature is deliberately not captured: it mixes in attributes and debug
+# locations that can't be stripped from a check line matching the raw output.
+CIR_FUNCTION_RE = re.compile(
+ r"^(?P<indent> *)cir\.func\s+(?P<attrs>)(?P<funcdef_attrs_and_ret>[^@\n]*)"
+ r"@(?P<func>[\w.$-]+)(?P<args_and_sig>\()[^\n]*\{\n(?P<body>.*?)^(?P=indent)\}",
+ flags=(re.M | re.S),
+)
+
+SCRUB_CIR_LOC_RE = re.compile(r"[ \t]+loc\((?:[^()\n]|\([^()\n]*\))*\)")
+
+
+def scrub_cir_body(body):
+ # Debug locations are noise, and they trail the op so the check still matches.
+ body = SCRUB_CIR_LOC_RE.sub("", body)
+ # "[%0" would turn into "[[[TMP0:%.*]]", which FileCheck can't parse.
+ body = body.replace("[%", "{{\\[}}%")
+ return common.scrub_body(body)
+
+
+def make_cir_generalizer(version):
+ # CIR text is MLIR: only SSA values are unstable. Types (!cir.ptr) and
+ # attributes (#cir.int) look like LLVM IR metadata and attribute groups but
+ # must be matched literally.
+ values = [common.NamelessValue(r"TMP", "%", r"%", r"[\w$.-]+?", None)]
+ prefix = r"(\s*)"
+ suffix = r"([,:\s\(\)\[\]\{\}]|\Z)"
+
+ return common.GeneralizerInfo(
+ version,
+ common.GeneralizerInfo.MODE_IR,
+ values,
+ prefix,
+ suffix,
+ func_keyword="cir.func",
+ )
+
+
+def split_list(value):
+ return [v for v in value.split(",") if v]
+
+
+def emits_cir(clang_args):
+ return "-emit-cir" in clang_args
+
+
+def add_arguments(parser):
+ parser.add_argument(
+ "--body-prefixes",
+ default="",
+ help="Comma-separated check prefixes that hold function bodies shared "
+ "across RUN lines (e.g. LLVM,CIR). Every other prefix only gets a -LABEL "
+ "line with the function name, so that C and C++ can share one body. "
+ "Shared bodies use plain CHECK lines, no -NEXT, no global checks, and "
+ "no return type or attributes.",
+ )
+
+
+def select_run_lines(args, path, run_list):
+ """Apply --body-prefixes to the RUN lines of a test.
+
+ Returns the RUN lines to process and the body prefixes in effect. May change
+ `args`, which holds the options of this one test.
+ """
+ filecheck_run_list = [i for i in run_list if i[0]]
+ body_prefixes = split_list(args.body_prefixes)
+ if body_prefixes:
+ # A prefix can be shared between formats, but its globals can't.
+ args.check_globals = "none"
+ if any(all(p in body_prefixes for p in i[0]) for i in filecheck_run_list):
+ # Without a label prefix there is nothing to share bodies under. Generate
+ # LLVM IR checks only. CIR prefixes then stay out of the prefix set, which
+ # leaves any existing CIR checks in the test untouched.
+ common.warn(
+ "A RUN line has no label prefix for --body-prefixes: "
+ "generating LLVM IR checks only and skipping CIR RUN lines",
+ test_file=path,
+ )
+ run_list = [i for i in run_list if not emits_cir(i[1])]
+ args.body_prefixes = ""
+ body_prefixes = []
+ return run_list, body_prefixes
+
+
+def validate_output_formats(ir_run_list, cir_run_list, body_prefixes):
+ """A prefix can't hold both LLVM IR and CIR bodies: they never match."""
+ ir_prefixes = {p for run in ir_run_list for p in run[0]}
+ cir_prefixes = {p for run in cir_run_list for p in run[0]}
+ if not cir_run_list:
+ return
+ message = None
+ shared = ir_prefixes & cir_prefixes
+ if not body_prefixes and shared:
+ message = "prefixes {} are used for both LLVM IR and CIR; use separate "
+ message += "prefixes or --body-prefixes"
+ message = message.format(sorted(shared))
+ elif shared & set(body_prefixes):
+ message = "body prefixes {} are used for both LLVM IR and CIR".format(
+ sorted(shared & set(body_prefixes))
+ )
+ if message:
+ common.error(message)
+ sys.exit(1)
+
+
+def add_split_checks(
+ output_lines, run_list, names, body_prefixes, formats, global_vars_seen_dict
+):
+ """Emit one -LABEL per non-body prefix, followed by the shared bodies.
+
+ `names` are the mangled names of one source function, and `formats` maps a
+ RUN line's clang args to its (func_dict, ginfo). Checks can't use -NEXT, the
+ RUN lines sharing a body may differ in lines that are not checked.
+ """
+ label_names = collections.defaultdict(set)
+ body_owners = {}
+ for run in run_list:
+ func_dict, ginfo = formats(run[1])
+ for prefix in run[0]:
+ present = names & func_dict[prefix].keys()
+ if prefix in body_prefixes:
+ body_owners[prefix] = (func_dict, ginfo)
+ else:
+ label_names[prefix] |= present
+
+ if any(len(found) > 1 for found in label_names.values()):
+ common.warn("Skipping functions with several names per prefix: " + str(names))
+ return []
+
+ printed = []
+ for prefix, found in label_names.items():
+ for name in found:
+ output_lines.append("// {}-LABEL: @{}(".format(prefix, name))
+ printed.append(prefix)
+
+ for prefix, (func_dict, ginfo) in body_owners.items():
+ bodies = [func_dict[prefix][n] for n in names if n in func_dict[prefix]]
+ if not bodies:
+ continue
+ first = bodies[0]
+ if not all(
+ b and str(b) == str(first) and b.args_and_sig == first.args_and_sig
+ for b in bodies
+ ):
+ common.warn(
+ "Prefix {} has conflicting bodies for {}".format(prefix, sorted(names))
+ )
+ continue
+
+ vars_seen = {}
+ global_vars_seen = global_vars_seen_dict.setdefault(prefix, {})
+
+ def generalize(lines):
+ return common.generalize_check_lines(
+ lines,
+ ginfo,
+ vars_seen,
+ global_vars_seen,
+ original_check_lines=[],
+ no_meta_details=ginfo.no_meta_details(),
+ )
+
+ # Keep the arguments, they are the operands of the checked operations.
+ # Everything after them is not what these checks are about.
+ signature = first.args_and_sig
+ arguments = signature[1 : signature.rfind(")") + 1]
+ if arguments not in ("", ")"):
+ line = generalize([arguments])[0]
+ output_lines.append("// {}-SAME: {}".format(prefix, line))
+ for line in generalize(str(first).splitlines()):
+ if line.strip():
+ output_lines.append("// {}: {}".format(prefix, line))
+ printed.append(prefix)
+
+ output_lines.append("//")
+ return printed
diff --git a/llvm/utils/UpdateTestChecks/common.py b/llvm/utils/UpdateTestChecks/common.py
index 2afa6072e1d379..ade9756e49ff8e 100644
--- a/llvm/utils/UpdateTestChecks/common.py
+++ b/llvm/utils/UpdateTestChecks/common.py
@@ -685,6 +685,67 @@ def find_run_lines(test, lines):
return run_lines
+LIT_DEFINE_RE = re.compile(
+ r"^\s*(?://|[;#])\s*(?:RE)?DEFINE:\s*(%\{[^}]+\})\s*=\s*(.*?)\s*$"
+)
+LIT_IF_RE = re.compile(
+ r"%if\s+(.*?)\s*%\{(.*?)%\}(?:\s*%else\s*%\{(.*?)%\})?"
+)
+# Expansion rounds before giving up, in case a definition refers to itself.
+MAX_LIT_DEFINE_ROUNDS = 10
+
+
+def expand_lit_defines(run_lines, input_lines):
+ """Expand lit "DEFINE: %{name} = value" substitutions in RUN lines.
+
+ REDEFINE is treated like DEFINE: the last value of a name is used for every
+ RUN line, not just the ones after it.
+ """
+ defs = {}
+ for line in input_lines:
+ m = LIT_DEFINE_RE.match(line)
+ if m:
+ defs[m.group(1)] = m.group(2)
+ out = []
+ for l in run_lines:
+ # Definitions may refer to other definitions, so expand until stable.
+ for _ in range(MAX_LIT_DEFINE_ROUNDS):
+ new = l
+ for name, value in defs.items():
+ new = new.replace(name, value)
+ if new == l:
+ break
+ l = new
+ else:
+ warn("DEFINE substitutions did not settle, check for a cycle: " + l)
+ out.append(l)
+ return out
+
+
+def expand_lit_ifs(run_lines, features):
+ """Resolve "%if <cond> %{ A %} [%else %{ B %}]" in RUN lines.
+
+ `features` are the available lit features. RUN lines that become empty are
+ dropped.
+ """
+ lit_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "lit")
+ if lit_dir not in sys.path:
+ sys.path.insert(0, lit_dir)
+ from lit.BooleanExpression import BooleanExpression
+
+ def pick(m):
+ enabled = BooleanExpression.evaluate(m.group(1), features)
+ taken = m.group(2) if enabled else m.group(3)
+ return (taken or "").strip()
+
+ out = []
+ for l in run_lines:
+ l = LIT_IF_RE.sub(pick, l)
+ if l.strip():
+ out.append(l)
+ return out
+
+
def get_triple_from_march(march):
triples = {
"amdgcn": "amdgcn",
@@ -827,6 +888,47 @@ def scrub_body(body):
return body
+IR_FUNC_DEF_RE = re.compile(r"^define .*?^}$", re.M | re.S)
+IR_TYPE_DEF_RE = re.compile(r"^(%[\w.$-]+) = type\b", re.M)
+IR_LOCAL_RE = re.compile(r"%(?:[\w.$-]+|\"[^\"]*\")")
+IR_ATTR_GROUP_RE = re.compile(r"^attributes #\d+ = \{.*\}\n", re.M)
+IR_METADATA_RE = re.compile(r"^!(?:\d+|llvm\.[\w.]+) = .*\n", re.M)
+IR_LABEL_RE = re.compile(r"^([\w.$-]+):", re.M)
+
+
+def normalize_local_names(ir):
+ """Make the local value names of every function body canonical.
+
+ Drops a leading "entry:" label and renames local values (arguments and
+ instructions) to %0, %1, ... in order of first use. IR produced by
+ different pipelines (e.g. classic CodeGen vs. CIR) can then be compared
+ and share CHECK lines. Basic block labels and named types are kept. The
+ attribute group and module metadata definitions are removed as well: they
+ legitimately differ between pipelines and are not what these checks are
+ about.
+ """
+ ir = IR_METADATA_RE.sub("", IR_ATTR_GROUP_RE.sub("", ir))
+ types = set(IR_TYPE_DEF_RE.findall(ir))
+
+ def normalize(m):
+ body = m.group(0)
+ labels = {"%" + l for l in IR_LABEL_RE.findall(body)}
+ if "%entry" in labels and body.count("%entry") == 0:
+ body = re.sub(r"^entry:.*\n", "", body, count=1, flags=re.M)
+ labels.discard("%entry")
+ names = {}
+
+ def rename(n):
+ name = n.group(0)
+ if name in labels or name in types:
+ return name
+ return "%" + str(names.setdefault(name, len(names)))
+
+ return IR_LOCAL_RE.sub(rename, body)
+
+ return IR_FUNC_DEF_RE.sub(normalize, ir)
+
+
def do_scrub(body, scrubber, scrubber_args, extra):
if scrubber_args:
local_args = copy.deepcopy(scrubber_args)
@@ -1263,8 +1365,10 @@ def __init__(
regexp_prefix,
regexp_suffix,
no_meta_details=False,
+ func_keyword="define",
):
self._version = version
+ self._func_keyword = func_keyword
self._mode = mode
self._nameless_values = nameless_values
@@ -1306,6 +1410,9 @@ def _build_regexp(self, globals_only, unstable_only):
def get_version(self):
return self._version
+ def get_func_keyword(self):
+ return self._func_keyword
+
def is_ir(self):
return self._mode == GeneralizerInfo.MODE_IR
@@ -2411,9 +2518,9 @@ def add_ir_checks(
assert ginfo.is_ir()
# Label format is based on IR string.
if function_sig and ginfo.get_version() > 1:
- function_def_regex = "define %s"
+ function_def_regex = ginfo.get_func_keyword() + " %s"
elif function_sig:
- function_def_regex = "define {{[^@]+}}%s"
+ function_def_regex = ginfo.get_func_keyword() + " {{[^@]+}}%s"
else:
function_def_regex = "%s"
check_label_format = "{} %s-LABEL: {}@%s%s%s".format(
diff --git a/llvm/utils/update_cc_test_checks.py b/llvm/utils/update_cc_test_checks.py
index 97b446d5659734..6552704b661597 100755
--- a/llvm/utils/update_cc_test_checks.py
+++ b/llvm/utils/update_cc_test_checks.py
@@ -27,7 +27,7 @@
import sys
import tempfile
-from UpdateTestChecks import common
+from UpdateTestChecks import cir, common
SUBST = {
"%clang": [],
@@ -36,6 +36,42 @@
}
+# A lit.local.cfg substitution built on %clang_cc1, e.g. in AArch64/lit.local.cfg:
+# config.substitutions.append(("%name", clang_cc1 + " -triple ... -o -"))
+LIT_CFG_CLANG_CC1_SUBST_RE = re.compile(
+ r"""config\.substitutions\.append\(\s*\(\s*["'](%\w+)["']\s*,"""
+ r"""\s*clang_cc1\s*\+\s*["']([^"']*)["']\s*\)\s*\)"""
+)
+LIT_SUITE_CONFIGS = ("lit.cfg.py", "lit.site.cfg.py")
+
+
+def get_local_substitutions(test_path):
+ """Return the %clang_cc1 based substitutions of the test's lit.local.cfg files.
+
+ Reads every lit.local.cfg from the test's directory up to the root of its
+ test suite, the nearest file winning. Reading them, rather than copying the
+ substitutions here, keeps both in sync. Other forms of substitution are
+ ignored.
+ """
+ found = {}
+ path = os.path.dirname(os.path.abspath(test_path))
+ while True:
+ cfg = os.path.join(path, "lit.local.cfg")
+ if os.path.isfile(cfg):
+ with open(cfg) as f:
+ for name, extra in LIT_CFG_CLANG_CC1_SUBST_RE.findall(f.read()):
+ found.setdefault(name, extra)
+ parent = os.path.dirname(path)
+ if parent == path or any(
+ os.path.isfile(os.path.join(path, c)) for c in LIT_SUITE_CONFIGS
+ ):
+ break
+ path = parent
+ return {
+ name: SUBST["%clang_cc1"] + shlex.split(extra) for name, extra in found.items()
+ }
+
+
def get_line2func_list(args, clang_args, globals_name_prefix):
ret = collections.defaultdict(list)
# Use clang's JSON AST dump to get the mangled name
@@ -221,6 +257,20 @@ def config():
choices=["none", "smart", "all"],
help="Check global entries (global variables, metadata, attribute sets, ...) for functions",
)
+ parser.add_argument(
+ "--lit-features",
+ default="",
+ help="Comma-separated lit features to treat as available when resolving "
+ "%%if in RUN lines (e.g. cir-enabled). Others are unavailable.",
+ )
+ # Keep this order: it is the order of the options in UTC_ARGS notes.
+ cir.add_arguments(parser)
+ parser.add_argument(
+ "--normalize-local-names",
+ action="store_true",
+ help="Drop the entry label and renumber local values before generating "
+ "checks, so that IR from different pipelines can share CHECK lines",
+ )
parser.add_argument("tests", nargs="+")
args = common.parse_commandline_args(parser)
infer_dependent_args(args)
@@ -277,15 +327,30 @@ def get_function_body(
)
sys.exit(1)
extra_args[0] = args.opt
+ elif extra_args[0] == "cir-opt":
+ # Prefer the cir-opt built with this clang, then fall back to PATH.
+ cir_opt = os.path.join(os.path.dirname(args.clang), "cir-opt")
+ if find_executable(cir_opt):
+ extra_args[0] = cir_opt
raw_tool_output = common.invoke_tool(extra_args[0], extra_args[1:], f.name)
- if "-emit-llvm" in clang_args:
+ # A body prefix legitimately sees new names: C and C++ mangle differently.
+ body_prefixes = cir.split_list(args.body_prefixes)
+ label_prefixes = [p for p in prefixes if p not in body_prefixes]
+ if cir.emits_cir(clang_args):
+ builder.process_run_line(
+ cir.CIR_FUNCTION_RE, cir.scrub_cir_body, raw_tool_output, prefixes
+ )
+ builder.processed_prefixes(label_prefixes)
+ elif "-emit-llvm" in clang_args:
+ if args.normalize_local_names:
+ raw_tool_output = common.normalize_local_names(raw_tool_output)
builder.process_run_line(
common.OPT_FUNCTION_RE, common.scrub_body, raw_tool_output, prefixes
)
- builder.processed_prefixes(prefixes)
+ builder.processed_prefixes(label_prefixes)
else:
print(
- "The clang command line should include -emit-llvm as asm tests "
+ "The clang command line should include -emit-llvm or -emit-cir as asm tests "
"are discouraged in Clang testsuite.",
file=sys.stderr,
)
@@ -315,7 +380,12 @@ def update_test(ti: common.TestInfo):
"%S": os.path.dirname(ti.path),
}
- for l in ti.run_lines:
+ run_lines = common.expand_lit_defines(ti.run_lines, ti.input_lines)
+ features = [f for f in ti.args.lit_features.split(",") if f]
+ run_lines = common.expand_lit_ifs(run_lines, features)
+ subst = dict(SUBST)
+ subst.update(get_local_substitutions(ti.path))
+ for l in run_lines:
commands = [cmd.strip() for cmd in l.split("|")]
triple_in_cmd = None
@@ -326,7 +396,14 @@ def update_test(ti: common.TestInfo):
# Parse executable args.
exec_args = shlex.split(commands[0])
# Execute non-clang runline.
- if exec_args[0] not in SUBST:
+ if exec_args[0] not in subst and exec_args[0].startswith("%clang"):
+ common.error(
+ "unknown substitution {}: it is not defined by a lit.local.cfg "
+ "of this test".format(exec_args[0]),
+ test_file=ti.path,
+ )
+ sys.exit(1)
+ if exec_args[0] not in subst:
# Do lit-like substitutions.
for s in subs:
exec_args = [i.replace(s, subs[s]) if s in i else i for i in exec_args]
@@ -335,7 +412,7 @@ def update_test(ti: common.TestInfo):
# This is a clang runline, apply %clang substitution rule, do lit-like substitutions,
# and append args.clang_args
clang_args = exec_args
- clang_args[0:1] = SUBST[clang_args[0]]
+ clang_args[0:1] = subst[clang_args[0]]
for s in subs:
clang_args = [i.replace(s, subs[s]) if s in i else i for i in clang_args]
clang_args += ti.args.clang_args
@@ -354,16 +431,33 @@ def update_test(ti: common.TestInfo):
# Execute clang, generate LLVM IR, and extract functions.
+ run_list, body_prefixes = cir.select_run_lines(ti.args, ti.path, run_list)
# Store only filechecked runlines.
filecheck_run_list = [i for i in run_list if i[0]]
+ # LLVM IR and CIR need different generalizers, so each gets its own builder.
+ ir_run_list = [i for i in filecheck_run_list if not cir.emits_cir(i[1])]
+ cir_run_list = [i for i in filecheck_run_list if cir.emits_cir(i[1])]
+ cir.validate_output_formats(ir_run_list, cir_run_list, body_prefixes)
+ if cir_run_list and ti.args.include_generated_funcs:
+ common.error("--include-generated-funcs doesn't support -emit-cir")
+ sys.exit(1)
+
ginfo = common.make_ir_generalizer(ti.args.version, ti.args.check_globals == "none")
builder = common.FunctionTestBuilder(
- run_list=filecheck_run_list,
+ run_list=ir_run_list,
flags=ti.args,
scrubber_args=[],
path=ti.path,
ginfo=ginfo,
)
+ cir_ginfo = cir.make_cir_generalizer(ti.args.version)
+ cir_builder = common.FunctionTestBuilder(
+ run_list=cir_run_list,
+ flags=ti.args,
+ scrubber_args=[],
+ path=ti.path,
+ ginfo=cir_ginfo,
+ )
global_tbaa_records_for_prefixes = {}
for prefixes, args, extra_commands, triple_in_cmd in run_list:
@@ -382,8 +476,9 @@ def update_test(ti: common.TestInfo):
# Invoke external tool and extract function bodies.
raw_tool_output = common.invoke_tool(ti.args.clang, clang_args, ti.path)
+ is_cir = cir.emits_cir(clang_args)
get_function_body(
- builder,
+ cir_builder if is_cir else builder,
ti.args,
ti.path,
clang_args,
@@ -393,8 +488,9 @@ def update_test(ti: common.TestInfo):
)
# Extract TBAA metadata for later usage in check lines.
- tbaa_map = common.get_tbaa_records(ti.args.version, raw_tool_output)
- global_tbaa_records_for_prefixes[tuple(prefixes)] = tbaa_map
+ if not is_cir:
+ tbaa_map = common.get_tbaa_records(ti.args.version, raw_tool_output)
+ global_tbaa_records_for_prefixes[tuple(prefixes)] = tbaa_map
# Invoke clang -Xclang -ast-dump=json to get mapping from start lines to
# mangled names. Forward all clang args for now.
@@ -404,6 +500,17 @@ def update_test(ti: common.TestInfo):
line2func_list[k].extend(v)
func_dict = builder.finish_and_get_func_dict()
+ cir_func_dict = cir_builder.finish_and_get_func_dict()
+
+ def split_formats(clang_args):
+ if cir.emits_cir(clang_args):
+ return cir_func_dict, cir_ginfo
+ return func_dict, ginfo
+
+ # Normalized IR drops lines from the real output, which rules out -NEXT.
+ is_filtered = builder.is_filtered() or ti.args.normalize_local_names
+ # Global checks only exist for LLVM IR. Keeps non-FileChecked RUN lines.
+ global_run_list = [i for i in run_list if not cir.emits_cir(i[1])]
global_vars_seen_dict = {}
prefix_set = set([prefix for p in filecheck_run_list for prefix in p[0]])
output_lines = []
@@ -442,7 +549,7 @@ def check_generator(my_output_lines, prefixes, func):
ginfo,
global_vars_seen_dict,
global_tbaa_records_for_prefixes,
- is_filtered=builder.is_filtered(),
+ is_filtered=is_filtered,
)
if ti.args.check_globals != "none":
@@ -450,7 +557,7 @@ def check_generator(my_output_lines, prefixes, func):
common.add_global_checks(
builder.global_var_dict(),
"//",
- run_list,
+ global_run_list,
output_lines,
ginfo,
global_vars_seen_dict,
@@ -498,6 +605,9 @@ def check_generator(my_output_lines, prefixes, func):
if args.functions is None or any(
re.search(regex, spell) for regex in args.functions
):
+ if body_prefixes:
+ added.add(mangled)
+ continue
last_line = output_lines[-1].strip()
while last_line == "//":
# Remove the comment line since we will generate a new comment
@@ -512,7 +622,7 @@ def check_generator(my_output_lines, prefixes, func):
common.add_global_checks(
builder.global_var_dict(),
"//",
- run_list,
+ global_run_list,
output_lines,
ginfo,
global_vars_seen_dict,
@@ -526,23 +636,42 @@ def check_generator(my_output_lines, prefixes, func):
if added:
output_lines.append("//")
added.add(mangled)
- generated_prefixes.extend(
- common.add_ir_checks(
- output_lines,
- "//",
- filecheck_run_list,
- func_dict,
- mangled,
- False,
- args.function_signature,
- ginfo,
- global_vars_seen_dict,
- global_tbaa_records_for_prefixes,
- is_filtered=builder.is_filtered(),
+ for group_runs, group_dict, group_ginfo in (
+ (ir_run_list, func_dict, ginfo),
+ (cir_run_list, cir_func_dict, cir_ginfo),
+ ):
+ generated_prefixes.extend(
+ common.add_ir_checks(
+ output_lines,
+ "//",
+ group_runs,
+ group_dict,
+ mangled,
+ False,
+ args.function_signature,
+ group_ginfo,
+ global_vars_seen_dict,
+ global_tbaa_records_for_prefixes,
+ is_filtered=is_filtered,
+ )
)
- )
if line.rstrip("\n") == "//":
include_line = False
+ if body_prefixes and added:
+ while output_lines[-1].strip() == "//":
+ output_lines.pop()
+ generated_prefixes.extend(
+ cir.add_split_checks(
+ output_lines,
+ filecheck_run_list,
+ added,
+ body_prefixes,
+ split_formats,
+ global_vars_seen_dict,
+ )
+ )
+ if line.rstrip("\n") == "//":
+ include_line = False
if include_line:
output_lines.append(line.rstrip("\n"))
@@ -552,7 +681,7 @@ def check_generator(my_output_lines, prefixes, func):
common.add_global_checks(
builder.global_var_dict(),
"//",
- run_list,
+ global_run_list,
output_lines,
ginfo,
global_vars_seen_dict,
More information about the cfe-commits
mailing list