[all-commits] [llvm/llvm-project] 025b9a: [TableGen] Add !switch operator (#199659)

Tomer Shafir via All-commits all-commits at lists.llvm.org
Sun May 31 05:32:54 PDT 2026


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 025b9a27cf74c16e1b1467aa8d7423abaac8363b
      https://github.com/llvm/llvm-project/commit/025b9a27cf74c16e1b1467aa8d7423abaac8363b
  Author: Tomer Shafir <tomer.shafir8 at gmail.com>
  Date:   2026-05-31 (Sun, 31 May 2026)

  Changed paths:
    M llvm/docs/TableGen/ProgRef.rst
    M llvm/lib/TableGen/TGLexer.cpp
    M llvm/lib/TableGen/TGLexer.h
    M llvm/lib/TableGen/TGParser.cpp
    M llvm/lib/TableGen/TGParser.h
    A llvm/test/TableGen/switch.td

  Log Message:
  -----------
  [TableGen] Add !switch operator (#199659)

This patch add a syntactic sugar operator to TableGen named `!switch`,
to simplify use cases where a user needs to conditionally use a value
based on exact key match. It supports variadic case arguments (0 or
more). It requires a default value - which creates a stricter grammar
that is simpler to parse, and I think the flexibility cost is not real -
it is considered a best practice in SW design for switch expressions (or
statements) on arbitrary types to always provide a default.

At parse time, after key and value type-checking, we reduce the
`!switch` expression to `!cond`, as they effectively entirely share the
downstream logic. The impl also extracts a shared pre-reduction
type-checking for `!switch` and `!cond` called
`TGParser::resolveInitTypes`.

Motivation: switch-behaving `!cond` value selection in `llvm/lib/Target`
e.g. from `llvm/lib/Target/AArch64/AArch64InstrFormats.td`:
```
// Helper class to convert vector element types to integers.
class ChangeElementTypeToInteger<ValueType InVT> {
  ValueType VT = !cond(
    !eq(InVT, v2f32): v2i32,
    !eq(InVT, v4f32): v4i32,
    // TODO: Other types.
    true : untyped);
}
```
It seems like the patch can possibly have a pretty wide impact following
up, and make many parts of TableGen code a bit more compact. I intend to
migrate actual TableGen code in followup patches.



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list