compiler/types: Use a string table for builtin type names
authorCaio Oliveira <caio.oliveira@intel.com>
Tue, 12 Sep 2023 20:43:09 +0000 (13:43 -0700)
committerMarge Bot <emma+marge@anholt.net>
Sat, 16 Sep 2023 15:17:01 +0000 (15:17 +0000)
This avoids the relocations for each of the builtin type names, allowing
all the builtin data to be loaded in read-only memory.

Reviewed-by: Adam Jackson <ajax@redhat.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/25191>

src/compiler/builtin_types_c.py
src/compiler/glsl_types.h
src/compiler/nir_types.cpp

index cc90228..0386bf5 100644 (file)
@@ -17,13 +17,20 @@ template = """\
 #include "glsl_types.h"
 #include "util/glheader.h"
 
+const char glsl_type_builtin_names[] =
+%for n in NAME_ARRAY:
+   "${n}"
+%endfor
+;
+
 %for t in BUILTIN_TYPES:
 const struct glsl_type glsl_type_builtin_${t["name"]} = {
    %for k, v in t.items():
-       %if v is None:
+       %if v is None or k == "name":
           <% continue %>
-       %elif k == "name":
-          .name_id = (uintptr_t) "${v}",
+       %elif k == "name_id":
+          .name_id = ${v},
+          .has_builtin_name = 1,
        %else:
           .${k} = ${v},
        %endif
@@ -38,5 +45,16 @@ if len(sys.argv) < 2:
 
 output = sys.argv[1]
 
+# Add padding to make sure zero is an invalid name.
+invalid = "INVALID"
+NAME_ARRAY = [invalid + "\\0"]
+id = len(invalid) + 1
+
+for t in BUILTIN_TYPES:
+    name = t["name"]
+    NAME_ARRAY.append(name + "\\0")
+    t["name_id"] = id
+    id += len(name) + 1
+
 with open(output, 'w') as f:
-    f.write(Template(template).render(BUILTIN_TYPES=BUILTIN_TYPES))
+    f.write(Template(template).render(BUILTIN_TYPES=BUILTIN_TYPES, NAME_ARRAY=NAME_ARRAY))
index dfcd0c6..938dc78 100644 (file)
@@ -304,6 +304,8 @@ struct glsl_type {
     */
    unsigned packed:1;
 
+   unsigned has_builtin_name:1;
+
    /**
     * \name Vector and matrix element counts
     *
index 1f396cb..e167105 100644 (file)
 #include "nir_types.h"
 #include "nir_gl_types.h"
 
+extern "C" const char glsl_type_builtin_names[];
+
 const char *
 glsl_get_type_name(const glsl_type *type)
 {
-   return (const char *)type->name_id;
+   if (type->has_builtin_name) {
+      return &glsl_type_builtin_names[type->name_id];
+   } else {
+      return (const char *) type->name_id;
+   }
 }
 
 int