Skip to content

Commit 659d996

Browse files
committed
Changed intrinsic generation
1 parent 82160c4 commit 659d996

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/intrinsic/llvm.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1548,10 +1548,11 @@ pub fn intrinsic<'gcc, 'tcx>(name: &str, cx: &CodegenCx<'gcc, 'tcx>) -> Function
15481548
"llvm.x86.tcmmrlfp16ps" => "__builtin_trap",
15491549

15501550
// NOTE: this file is generated by https://github.com/GuillaumeGomez/llvmint/blob/master/generate_list.py
1551-
_ => include!("archs.rs"),
1551+
_ => map_arch_intrinsic(name),
15521552
};
15531553

15541554
let func = cx.context.get_target_builtin_function(gcc_name);
15551555
cx.functions.borrow_mut().insert(gcc_name.to_string(), func);
15561556
func
15571557
}
1558+
include!("archs.rs");

tools/generate_intrinsics.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,25 +168,39 @@ def update_intrinsics(llvm_path, llvmint, llvmint2):
168168
os.path.dirname(os.path.abspath(__file__)),
169169
"../src/intrinsic/archs.rs",
170170
)
171+
# A hashmap of all architectures. This allows us to first match on the architecture, and then on the intrisnics.
172+
# This speeds up the comparison, and makes our code considerably smaller.
173+
# Since all intrinsic names start with ".llvm", we skip that prefix.
171174
print("Updating content of `{}`...".format(output_file))
172175
with open(output_file, "w", encoding="utf8") as out:
173176
out.write("// File generated by `rustc_codegen_gcc/tools/generate_intrinsics.py`\n")
174177
out.write("// DO NOT EDIT IT!\n")
175-
out.write("match name {\n")
178+
out.write("/// Translate a given LLVM intrinsic name to an equivalent GCC one.\n")
179+
out.write("fn map_arch_intrinsic(name:&str)->&str{\n")
180+
out.write('let Some(name) = name.strip_prefix("llvm.") else {unimplemented!("***** unsupported LLVM intrinsic {}", name)};\n')
181+
out.write('let Some((arch, name)) = name.split_once(\'.\') else {unimplemented!("***** unsupported LLVM intrinsic {}", name)};\n')
182+
out.write("match arch {\n")
176183
for arch in archs:
177184
if len(intrinsics[arch]) == 0:
178185
continue
186+
out.write("\"{}\" => {{ #[allow(non_snake_case)] fn {}(name:&str)->&str{{ match name{{".format(arch,arch))
179187
intrinsics[arch].sort(key=lambda x: (x[0], x[2]))
180188
out.write(' // {}\n'.format(arch))
181189
for entry in intrinsics[arch]:
190+
llvm_name = entry[0].removeprefix("llvm.");
191+
llvm_name = llvm_name.removeprefix(arch);
192+
llvm_name = llvm_name.removeprefix(".");
182193
if entry[2] is True: # if it is a duplicate
183-
out.write(' // [DUPLICATE]: "{}" => "{}",\n'.format(entry[0], entry[1]))
194+
out.write(' // [DUPLICATE]: "{}" => "{}",\n'.format(llvm_name, entry[1]))
184195
elif "_round_mask" in entry[1]:
185-
out.write(' // [INVALID CONVERSION]: "{}" => "{}",\n'.format(entry[0], entry[1]))
196+
out.write(' // [INVALID CONVERSION]: "{}" => "{}",\n'.format(llvm_name, entry[1]))
186197
else:
187-
out.write(' "{}" => "{}",\n'.format(entry[0], entry[1]))
198+
out.write(' "{}" => "{}",\n'.format(llvm_name, entry[1]))
199+
out.write(' _ => unimplemented!("***** unsupported LLVM intrinsic {}", name),\n')
200+
out.write("}} }} {}(name) }}\n,".format(arch))
188201
out.write(' _ => unimplemented!("***** unsupported LLVM intrinsic {}", name),\n')
189-
out.write("}\n")
202+
out.write("}\n}")
203+
subprocess.call(["rustfmt", output_file])
190204
print("Done!")
191205

192206

0 commit comments

Comments
 (0)