Skip to content

Commit 6538711

Browse files
committed
rustc_codegen_llvm: Reorder conversion cases
For maintainability, this commit reorders target feature conversion cases into four categories: 1. Non-default, relatively simple conversion (renames w/wo dependencies), 2. Simple filtering to ignore unsupported LLVM features, 3. Relatively complex conversion and 4. The default case. For each category, clauses are sorted by the architecture.
1 parent d3522d4 commit 6538711

File tree

1 file changed

+55
-50
lines changed

1 file changed

+55
-50
lines changed

compiler/rustc_codegen_llvm/src/llvm_util.rs

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,9 @@ pub(crate) fn to_llvm_features<'a>(sess: &Session, s: &'a str) -> Option<LLVMFea
226226
_ => raw_arch,
227227
};
228228
match (arch, s) {
229-
("x86", "sse4.2") => Some(LLVMFeature::with_dependencies(
230-
"sse4.2",
231-
smallvec![TargetFeatureFoldStrength::EnableOnly("crc32")],
232-
)),
233-
("x86", "pclmulqdq") => Some(LLVMFeature::new("pclmul")),
234-
("x86", "rdrand") => Some(LLVMFeature::new("rdrnd")),
235-
("x86", "bmi1") => Some(LLVMFeature::new("bmi")),
236-
("x86", "cmpxchg16b") => Some(LLVMFeature::new("cx16")),
237-
("x86", "lahfsahf") => Some(LLVMFeature::new("sahf")),
229+
/*
230+
Perform relatively simple feature conversion.
231+
*/
238232
("aarch64", "rcpc2") => Some(LLVMFeature::new("rcpc-immo")),
239233
("aarch64", "dpb") => Some(LLVMFeature::new("ccpp")),
240234
("aarch64", "dpb2") => Some(LLVMFeature::new("ccdp")),
@@ -243,10 +237,6 @@ pub(crate) fn to_llvm_features<'a>(sess: &Session, s: &'a str) -> Option<LLVMFea
243237
("aarch64", "pmuv3") => Some(LLVMFeature::new("perfmon")),
244238
("aarch64", "paca") => Some(LLVMFeature::new("pauth")),
245239
("aarch64", "pacg") => Some(LLVMFeature::new("pauth")),
246-
// Before LLVM 20 those two features were packaged together as b16b16
247-
("aarch64", "sve-b16b16" | "sme-b16b16") if get_version().0 < 20 => {
248-
Some(LLVMFeature::new("b16b16"))
249-
}
250240
("aarch64", "flagm2") => Some(LLVMFeature::new("altnzcv")),
251241
// Rust ties fp and neon together.
252242
("aarch64", "neon") => Some(LLVMFeature::with_dependencies(
@@ -257,22 +247,45 @@ pub(crate) fn to_llvm_features<'a>(sess: &Session, s: &'a str) -> Option<LLVMFea
257247
// neon when a feature only implicitly enables fp
258248
("aarch64", "fhm") => Some(LLVMFeature::new("fp16fml")),
259249
("aarch64", "fp16") => Some(LLVMFeature::new("fullfp16")),
260-
// Filter out features that are not supported by the current LLVM version
261-
("aarch64", "fpmr") => None, // only existed in 18
262250
("arm", "fp16") => Some(LLVMFeature::new("fullfp16")),
263-
// NVPTX targets added in LLVM 20
264-
(
265-
"nvptx64",
266-
"sm_100" | "sm_100a" | "sm_101" | "sm_101a" | "sm_120" | "sm_120a" | "ptx86" | "ptx87",
267-
) if get_version().0 < 20 => None,
268-
// Filter out features that are not supported by the current LLVM version
251+
("powerpc", "power8-crypto") => Some(LLVMFeature::new("crypto")),
252+
("sparc", "leoncasa") => Some(LLVMFeature::new("hasleoncasa")),
253+
("x86", "sse4.2") => Some(LLVMFeature::with_dependencies(
254+
"sse4.2",
255+
smallvec![TargetFeatureFoldStrength::EnableOnly("crc32")],
256+
)),
257+
("x86", "pclmulqdq") => Some(LLVMFeature::new("pclmul")),
258+
("x86", "rdrand") => Some(LLVMFeature::new("rdrnd")),
259+
("x86", "bmi1") => Some(LLVMFeature::new("bmi")),
260+
("x86", "cmpxchg16b") => Some(LLVMFeature::new("cx16")),
261+
("x86", "lahfsahf") => Some(LLVMFeature::new("sahf")),
262+
("x86", "avx10.1") => Some(LLVMFeature::new("avx10.1-512")),
263+
("x86", "apxf") => Some(LLVMFeature::with_dependencies(
264+
"egpr",
265+
smallvec![
266+
TargetFeatureFoldStrength::Both("push2pop2"),
267+
TargetFeatureFoldStrength::Both("ppx"),
268+
TargetFeatureFoldStrength::Both("ndd"),
269+
TargetFeatureFoldStrength::Both("ccmp"),
270+
TargetFeatureFoldStrength::Both("cf"),
271+
TargetFeatureFoldStrength::Both("nf"),
272+
TargetFeatureFoldStrength::Both("zu"),
273+
],
274+
)),
275+
/*
276+
Filter out features that are not supported by the current LLVM version.
277+
*/
278+
("aarch64", "fpmr") => None, // only existed in 18
279+
("loongarch32" | "loongarch64", "32s") if get_version().0 < 21 => None,
269280
("loongarch64", "div32" | "lam-bh" | "lamcas" | "ld-seq-sa" | "scq")
270281
if get_version().0 < 20 =>
271282
{
272283
None
273284
}
274-
("loongarch32" | "loongarch64", "32s") if get_version().0 < 21 => None,
275-
// Filter out features that are not supported by the current LLVM version
285+
(
286+
"nvptx64",
287+
"sm_100" | "sm_100a" | "sm_101" | "sm_101a" | "sm_120" | "sm_120a" | "ptx86" | "ptx87",
288+
) if get_version().0 < 20 => None,
276289
("riscv32" | "riscv64", "zacas" | "rva23u64" | "supm") if get_version().0 < 20 => None,
277290
(
278291
"s390x",
@@ -282,40 +295,32 @@ pub(crate) fn to_llvm_features<'a>(sess: &Session, s: &'a str) -> Option<LLVMFea
282295
| "vector-enhancements-3"
283296
| "vector-packed-decimal-enhancement-3",
284297
) if get_version().0 < 20 => None,
285-
// Enable the evex512 target feature if an avx512 target feature is enabled.
286-
("x86", s) if s.starts_with("avx512") => Some(LLVMFeature::with_dependencies(
287-
s,
288-
smallvec![TargetFeatureFoldStrength::EnableOnly("evex512")],
289-
)),
290-
// Support for `wide-arithmetic` will first land in LLVM 20 as part of
291-
// llvm/llvm-project#111598
292298
("wasm32" | "wasm64", "wide-arithmetic") if get_version().0 < 20 => None,
293-
("sparc", "leoncasa") => Some(LLVMFeature::new("hasleoncasa")),
299+
(
300+
"x86",
301+
"amx-avx512" | "amx-fp8" | "amx-movrs" | "amx-tf32" | "amx-transpose" | "movrs",
302+
) if get_version().0 < 20 => None,
303+
/*
304+
Perform relatively complex feature conversion.
305+
*/
306+
// Before LLVM 20 those two features were packaged together as b16b16
307+
("aarch64", "sve-b16b16" | "sme-b16b16") if get_version().0 < 20 => {
308+
Some(LLVMFeature::new("b16b16"))
309+
}
294310
// In LLVM 19, there is no `v8plus` feature and `v9` means "SPARC-V9 instruction available and SPARC-V8+ ABI used".
295311
// https://github.com/llvm/llvm-project/blob/llvmorg-19.1.0/llvm/lib/Target/Sparc/MCTargetDesc/SparcELFObjectWriter.cpp#L27-L28
296312
// Before LLVM 19, there was no `v8plus` feature and `v9` means "SPARC-V9 instruction available".
297313
// https://github.com/llvm/llvm-project/blob/llvmorg-18.1.0/llvm/lib/Target/Sparc/MCTargetDesc/SparcELFObjectWriter.cpp#L26
298314
("sparc", "v8plus") if get_version().0 == 19 => Some(LLVMFeature::new("v9")),
299-
("powerpc", "power8-crypto") => Some(LLVMFeature::new("crypto")),
300-
// These new `amx` variants and `movrs` were introduced in LLVM20
301-
(
302-
"x86",
303-
"amx-avx512" | "amx-fp8" | "amx-movrs" | "amx-tf32" | "amx-transpose" | "movrs",
304-
) if get_version().0 < 20 => None,
305-
("x86", "avx10.1") => Some(LLVMFeature::new("avx10.1-512")),
306-
("x86", "avx10.2") => (get_version().0 >= 20).then(|| LLVMFeature::new("avx10.2-512")),
307-
("x86", "apxf") => Some(LLVMFeature::with_dependencies(
308-
"egpr",
309-
smallvec![
310-
TargetFeatureFoldStrength::Both("push2pop2"),
311-
TargetFeatureFoldStrength::Both("ppx"),
312-
TargetFeatureFoldStrength::Both("ndd"),
313-
TargetFeatureFoldStrength::Both("ccmp"),
314-
TargetFeatureFoldStrength::Both("cf"),
315-
TargetFeatureFoldStrength::Both("nf"),
316-
TargetFeatureFoldStrength::Both("zu"),
317-
],
315+
// Enable the evex512 target feature if an avx512 target feature is enabled.
316+
("x86", s) if s.starts_with("avx512") => Some(LLVMFeature::with_dependencies(
317+
s,
318+
smallvec![TargetFeatureFoldStrength::EnableOnly("evex512")],
318319
)),
320+
("x86", "avx10.2") => (get_version().0 >= 20).then(|| LLVMFeature::new("avx10.2-512")),
321+
/*
322+
By default, use the Rust's target feature name as-is.
323+
*/
319324
(_, s) => Some(LLVMFeature::new(s)),
320325
}
321326
}

0 commit comments

Comments
 (0)