Skip to content

Commit 67ae38a

Browse files
committed
Expand the LLVM coverage of --print target-cpus
We've been relying on a custom patch to add `MCSubtargetInfo::getCPUTable` for `rustc --print target-cpus`, and just printing that it's not supported on external LLVM builds. LLVM `main` now has `getAllProcessorDescriptions` that can replace ours, so now we try to use that. In addition, the fallback path can at least print the native and default cpu options. There were also some mismatches in the function signatures here between `LLVM_RUSTLLVM` and otherwise; this is now mitigated by sharing these functions and only using cpp to adjust the function bodies.
1 parent f9a6b71 commit 67ae38a

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

+16-14
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,6 @@ static Reloc::Model fromRust(LLVMRustRelocModel RustReloc) {
297297
report_fatal_error("Bad RelocModel.");
298298
}
299299

300-
#ifdef LLVM_RUSTLLVM
301300
/// getLongestEntryLength - Return the length of the longest entry in the table.
302301
template<typename KV>
303302
static size_t getLongestEntryLength(ArrayRef<KV> Table) {
@@ -312,13 +311,23 @@ extern "C" void LLVMRustPrintTargetCPUs(LLVMTargetMachineRef TM, const char* Tar
312311
const MCSubtargetInfo *MCInfo = Target->getMCSubtargetInfo();
313312
const Triple::ArchType HostArch = Triple(sys::getDefaultTargetTriple()).getArch();
314313
const Triple::ArchType TargetArch = Target->getTargetTriple().getArch();
314+
315+
#if LLVM_VERSION_GE(17, 0)
316+
const ArrayRef<SubtargetSubTypeKV> CPUTable = MCInfo->getAllProcessorDescriptions();
317+
#elif defined(LLVM_RUSTLLVM)
315318
const ArrayRef<SubtargetSubTypeKV> CPUTable = MCInfo->getCPUTable();
319+
#else
320+
printf("Full target CPU help is not supported by this LLVM version.\n\n");
321+
SubtargetSubTypeKV TargetCPUKV = { TargetCPU, {{}}, {{}} };
322+
const ArrayRef<SubtargetSubTypeKV> CPUTable = TargetCPUKV;
323+
#endif
316324
unsigned MaxCPULen = getLongestEntryLength(CPUTable);
317325

318326
printf("Available CPUs for this target:\n");
319327
// Don't print the "native" entry when the user specifies --target with a
320328
// different arch since that could be wrong or misleading.
321329
if (HostArch == TargetArch) {
330+
MaxCPULen = std::max(MaxCPULen, (unsigned) std::strlen("native"));
322331
const StringRef HostCPU = sys::getHostCPUName();
323332
printf(" %-*s - Select the CPU of the current host (currently %.*s).\n",
324333
MaxCPULen, "native", (int)HostCPU.size(), HostCPU.data());
@@ -338,34 +347,27 @@ extern "C" void LLVMRustPrintTargetCPUs(LLVMTargetMachineRef TM, const char* Tar
338347
}
339348

340349
extern "C" size_t LLVMRustGetTargetFeaturesCount(LLVMTargetMachineRef TM) {
350+
#ifdef LLVM_RUSTLLVM
341351
const TargetMachine *Target = unwrap(TM);
342352
const MCSubtargetInfo *MCInfo = Target->getMCSubtargetInfo();
343353
const ArrayRef<SubtargetFeatureKV> FeatTable = MCInfo->getFeatureTable();
344354
return FeatTable.size();
355+
#else
356+
return 0;
357+
#endif
345358
}
346359

347360
extern "C" void LLVMRustGetTargetFeature(LLVMTargetMachineRef TM, size_t Index,
348361
const char** Feature, const char** Desc) {
362+
#ifdef LLVM_RUSTLLVM
349363
const TargetMachine *Target = unwrap(TM);
350364
const MCSubtargetInfo *MCInfo = Target->getMCSubtargetInfo();
351365
const ArrayRef<SubtargetFeatureKV> FeatTable = MCInfo->getFeatureTable();
352366
const SubtargetFeatureKV Feat = FeatTable[Index];
353367
*Feature = Feat.Key;
354368
*Desc = Feat.Desc;
355-
}
356-
357-
#else
358-
359-
extern "C" void LLVMRustPrintTargetCPUs(LLVMTargetMachineRef) {
360-
printf("Target CPU help is not supported by this LLVM version.\n\n");
361-
}
362-
363-
extern "C" size_t LLVMRustGetTargetFeaturesCount(LLVMTargetMachineRef) {
364-
return 0;
365-
}
366-
367-
extern "C" void LLVMRustGetTargetFeature(LLVMTargetMachineRef, const char**, const char**) {}
368369
#endif
370+
}
369371

370372
extern "C" const char* LLVMRustGetHostCPUName(size_t *len) {
371373
StringRef Name = sys::getHostCPUName();

0 commit comments

Comments
 (0)