[Chapel Merge] Global unique strings

Branch: refs/heads/main
Revision: f975ee8
Author: aconsroe-hpe
Link: Unavailable
Log Message:

Merge pull request #18830 from aconsroe-hpe/global-unique-strings

Global unique strings

reviewed by mppf and comments from arezaii

Introduce global unique strings

This introduces a macro, USTR, that expands to a template
specialization that references a field in the struct GlobalStrings.

The macro is limited to a fixed max-string-length but can be easily
doubled by adding the appropriate EXPAND{N} macro.

The list of known strings the macro will work with is defined in
all-global-strings.h and covers the commonly used ones.

You can add a string to all-global-strings.h as needed

There will be compile time errors if either

  1. string is too long : USTR("this string is way too long")
  2. typo in string : USTR("algin")

In order for this to work, we introduce a root context that
initializes the fields in the GlobalStrings struct and for those that
exceed the small size optimization, adds them to its unique string
table. All subsequent contexts add each entry from the root context so
that the UniqueString stays unique (no duplicate copy between tables).

Additionally, we add a do-not-collect bit for these strings that is
set in the string metadata (next to the gc mark) so that Context
doesn't GC these global strings.

The benefit to all of this is that

  1. It is easier to write code like:

    • if (op == GSTR("?")) than
    • if (op == globalStrings.questionMark)
  2. We still get compile time checking of typos

  3. Comparison is very fast because it is the same as accessing

    Modified Files:
    A compiler/next/include/chpl/queries/all-global-strings.h
    A compiler/next/include/chpl/queries/global-strings.h
    M compiler/next/include/chpl/queries/Context.h
    M compiler/next/include/chpl/types/BasicClassType.h
    M compiler/next/lib/queries/Context.cpp
    M compiler/next/lib/resolution/resolution-queries.cpp
    M compiler/next/lib/types/PrimitiveType.cpp
    M compiler/next/test/queries/testUniqueString.cpp
    M compiler/passes/convert-uast.cpp

    Compare: Comparing ee3e15c18896...f975ee82992a · chapel-lang/chapel · GitHub