[all-commits] [llvm/llvm-project] 634484: [TableGen] Add new operator !exists

Wang Pengcheng via All-commits all-commits at lists.llvm.org
Wed Jun 22 20:13:21 PDT 2022


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 634484885ccf89de0c3f296ac8380b26f4c6763d
      https://github.com/llvm/llvm-project/commit/634484885ccf89de0c3f296ac8380b26f4c6763d
  Author: wangpc <pc.wang at linux.alibaba.com>
  Date:   2022-06-23 (Thu, 23 Jun 2022)

  Changed paths:
    M llvm/docs/TableGen/ProgRef.rst
    M llvm/include/llvm/TableGen/Record.h
    M llvm/lib/TableGen/Record.cpp
    M llvm/lib/TableGen/TGLexer.cpp
    M llvm/lib/TableGen/TGLexer.h
    M llvm/lib/TableGen/TGParser.cpp
    A llvm/test/TableGen/exists-error-non-string.td
    A llvm/test/TableGen/exists-error-record.td
    A llvm/test/TableGen/exists-error-uninitialized.td
    A llvm/test/TableGen/exists.td

  Log Message:
  -----------
  [TableGen] Add new operator !exists

We can cast a string to a record via !cast, but we have no mechanism
to check if it is valid and TableGen will raise an error if failed to
cast. Besides, we have no semantic null in TableGen (we have `?` but
different backends handle uninitialized value differently), so operator
like `dyn_cast<>` is hard to implement.

In this patch, we add a new operator `!exists<T>(s)` to check whether
a record with type `T` and name `s` exists. Self-references are allowed
just like `!cast`.

By doing these, we can write code like:
```
class dyn_cast_to_record<string name> {
  R value = !if(!exists<R>(name), !cast<R>(name), default_value);
}
defvar v = dyn_cast_to_record<"R0">.value; // R0 or default_value.
```

Reviewed By: tra, nhaehnle

Differential Revision: https://reviews.llvm.org/D127948




More information about the All-commits mailing list