about summary refs log tree commit diff
path: root/compiler/rustc_codegen_gcc/tools/generate_intrinsics.py
diff options
context:
space:
mode:
authorTshepang Mbambo <hopsi@tuta.io>2025-06-19 22:14:30 +0200
committerGitHub <noreply@github.com>2025-06-19 22:14:30 +0200
commit6ad42bf4e7ab81ec3d66a5e47dfe01dc51603c0b (patch)
tree59d2cbb81c6cf1bb95feb51963d037fd67fc51bf /compiler/rustc_codegen_gcc/tools/generate_intrinsics.py
parentf25cfe83f2a13d96731861caae7236372f6445d5 (diff)
parentd854d563445082268ca8e7dd76b6ccde0342c2b3 (diff)
downloadrust-6ad42bf4e7ab81ec3d66a5e47dfe01dc51603c0b.tar.gz
rust-6ad42bf4e7ab81ec3d66a5e47dfe01dc51603c0b.zip
Merge pull request #2477 from rust-lang/rustc-pull
Rustc pull update
Diffstat (limited to 'compiler/rustc_codegen_gcc/tools/generate_intrinsics.py')
-rw-r--r--compiler/rustc_codegen_gcc/tools/generate_intrinsics.py26
1 files changed, 20 insertions, 6 deletions
diff --git a/compiler/rustc_codegen_gcc/tools/generate_intrinsics.py b/compiler/rustc_codegen_gcc/tools/generate_intrinsics.py
index 181f1e501a4..ed0ebf00719 100644
--- a/compiler/rustc_codegen_gcc/tools/generate_intrinsics.py
+++ b/compiler/rustc_codegen_gcc/tools/generate_intrinsics.py
@@ -168,25 +168,39 @@ def update_intrinsics(llvm_path, llvmint, llvmint2):
         os.path.dirname(os.path.abspath(__file__)),
         "../src/intrinsic/archs.rs",
     )
+    # A hashmap of all architectures. This allows us to first match on the architecture, and then on the intrinsics.
+    # This speeds up the comparison, and makes our code considerably smaller.
+    # Since all intrinsic names start with "llvm.", we skip that prefix.
     print("Updating content of `{}`...".format(output_file))
     with open(output_file, "w", encoding="utf8") as out:
         out.write("// File generated by `rustc_codegen_gcc/tools/generate_intrinsics.py`\n")
         out.write("// DO NOT EDIT IT!\n")
-        out.write("match name {\n")
+        out.write("/// Translate a given LLVM intrinsic name to an equivalent GCC one.\n")
+        out.write("fn map_arch_intrinsic(name:&str)->&str{\n")
+        out.write('let Some(name) = name.strip_prefix("llvm.") else { unimplemented!("***** unsupported LLVM intrinsic {}", name) };\n')
+        out.write('let Some((arch, name)) = name.split_once(\'.\') else { unimplemented!("***** unsupported LLVM intrinsic {}", name) };\n')
+        out.write("match arch {\n")
         for arch in archs:
             if len(intrinsics[arch]) == 0:
                 continue
+            out.write("\"{}\" => {{ #[allow(non_snake_case)] fn {}(name: &str) -> &str {{ match name {{".format(arch,arch))
             intrinsics[arch].sort(key=lambda x: (x[0], x[2]))
             out.write('    // {}\n'.format(arch))
             for entry in intrinsics[arch]:
+                llvm_name = entry[0].removeprefix("llvm.");
+                llvm_name = llvm_name.removeprefix(arch);
+                llvm_name = llvm_name.removeprefix(".");
                 if entry[2] is True: # if it is a duplicate
-                    out.write('    // [DUPLICATE]: "{}" => "{}",\n'.format(entry[0], entry[1]))
+                    out.write('    // [DUPLICATE]: "{}" => "{}",\n'.format(llvm_name, entry[1]))
                 elif "_round_mask" in entry[1]:
-                    out.write('    // [INVALID CONVERSION]: "{}" => "{}",\n'.format(entry[0], entry[1]))
+                    out.write('    // [INVALID CONVERSION]: "{}" => "{}",\n'.format(llvm_name, entry[1]))
                 else:
-                    out.write('    "{}" => "{}",\n'.format(entry[0], entry[1]))
-        out.write('    _ => unimplemented!("***** unsupported LLVM intrinsic {}", name),\n')
-        out.write("}\n")
+                    out.write('    "{}" => "{}",\n'.format(llvm_name, entry[1]))
+            out.write('    _ => unimplemented!("***** unsupported LLVM intrinsic {}", name),\n')
+            out.write("}} }} {}(name) }}\n,".format(arch))
+        out.write('    _ => unimplemented!("***** unsupported LLVM architecture {}", name),\n')
+        out.write("}\n}")
+    subprocess.call(["rustfmt", output_file])
     print("Done!")