Skip to content

[FIX][LLVM] Use isCPUStringValid for mcpu validation instead of enumerating processor descriptions#18884

Open
gnguralnick wants to merge 1 commit intoapache:mainfrom
gnguralnick:fix-llvm22-cpu-alias-validation
Open

[FIX][LLVM] Use isCPUStringValid for mcpu validation instead of enumerating processor descriptions#18884
gnguralnick wants to merge 1 commit intoapache:mainfrom
gnguralnick:fix-llvm22-cpu-alias-validation

Conversation

@gnguralnick
Copy link

Summary

Fix false rejection of apple-m1, apple-m2, and apple-m3 as LLVM CPU names when building TVM with LLVM 22+.

Behavior

After following the installation from source instructions and building against LLVM 22, every import tvm produces spurious error messages:

Error: Using LLVM 22.1.0 with `-mcpu=apple-m1` is not valid in `-mtriple=arm64-apple-macos`, using default `-mcpu=generic`
Error: Using LLVM 22.1.0 with `-mcpu=apple-m2` is not valid in `-mtriple=arm64-apple-macos`, using default `-mcpu=generic`

These are triggered by the Metal target tag registrations in python/tvm/target/tag_registry/metal.py, which use apple-m1 and apple-m2 as the host -mcpu. The CPUs are silently downgraded to generic.

Root cause

LLVM 22 reorganized its AArch64 processor table. apple-m1 through apple-m3 are now CPU aliases — fully valid and accepted by createTargetMachine and isCPUStringValid(), but no longer returned by MCSubtargetInfo::getAllProcessorDescriptions().

TVM's LLVMTargetInfo constructor validates -mcpu by enumerating getAllProcessorDescriptions() and checking membership, so it misses alias-only names.

Fix

Replace the enumeration-based check with a new IsValidCPU() method that uses MCSubtargetInfo::isCPUStringValid(), which correctly handles both primary names and aliases. This API has been available since at least LLVM 7, well before TVM's minimum supported version.

Validation

  • Built and tested on macOS (Apple Silicon) with LLVM 22.1.0
  • python -c "import tvm; print(tvm.__file__)" produces clean output with no error messages

…rating processor descriptions

LLVM 22 reorganized AArch64 processor definitions, making apple-m1/m2/m3
into CPU aliases that don't appear in getAllProcessorDescriptions() but
are still valid. This caused spurious error logs and fallback to -mcpu=generic
on every import when Metal target tags are registered.

Replace the enumeration-based check with MCSubtargetInfo::isCPUStringValid(),
which correctly handles both primary CPU names and aliases.
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the robustness of TVM's LLVM integration by rectifying an issue that caused spurious error messages related to CPU validation, particularly for Apple Silicon processors with newer LLVM versions. The change ensures that CPU aliases are correctly recognized, preventing unnecessary downgrades to generic CPU targets and improving compatibility with modern LLVM toolchains.

Highlights

  • Fix for LLVM CPU Validation: Resolved an issue where TVM, when built with LLVM 22+, incorrectly rejected valid Apple Silicon CPU names such as apple-m1, apple-m2, and apple-m3.
  • Root Cause Identified: The problem was traced to TVM's LLVMTargetInfo constructor, which used an enumeration (getAllProcessorDescriptions()) for CPU validation. In LLVM 22+, this enumeration no longer includes CPU aliases, leading to false rejections.
  • Improved CPU Validation Logic: The validation mechanism has been updated to use a new IsValidCPU() method. This method internally calls LLVM's MCSubtargetInfo::isCPUStringValid(), which correctly handles both primary CPU names and their aliases.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/target/llvm/llvm_instance.cc
    • Modified the LLVMTargetInfo constructor to utilize the newly introduced IsValidCPU method for CPU validation.
    • Implemented the IsValidCPU method, which now uses MCSubtargetInfo::isCPUStringValid() to accurately check CPU validity, including aliases.
  • src/target/llvm/llvm_instance.h
    • Added the declaration for the IsValidCPU method to the LLVMTargetInfo class.
Activity
  • No human activity has been recorded on this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly fixes an issue where some valid LLVM CPU names (like apple-m1) were being rejected. The approach of using isCPUStringValid instead of enumerating processor descriptions is sound, as it correctly handles CPU aliases introduced in newer LLVM versions. My review includes a suggestion to optimize the new validation function for better performance by avoiding the unnecessary creation of a full TargetMachine instance.

Comment on lines +443 to +452
bool LLVMTargetInfo::IsValidCPU(const std::string& cpu) const {
auto llvm_instance = CreateLLVMTargetInstance(triple_, true);
if (!llvm_instance) return false;
auto tm = CreateLLVMTargetMachine(llvm_instance, triple_, "", "");
if (!tm) return false;
const auto* MCInfo = tm->getMCSubtargetInfo();
// Use isCPUStringValid which correctly handles CPU aliases (e.g. apple-m1
// in LLVM 22+) that don't appear in getAllProcessorDescriptions().
return MCInfo && MCInfo->isCPUStringValid(cpu);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While the current implementation is correct, it can be made more efficient. Creating a full llvm::TargetMachine via CreateLLVMTargetMachine is a relatively expensive operation. You can achieve the same result by directly creating an MCSubtargetInfo object using llvm::Target::createMCSubtargetInfo, which avoids the overhead of instantiating an entire TargetMachine.

bool LLVMTargetInfo::IsValidCPU(const std::string& cpu) const {
  auto llvm_instance = CreateLLVMTargetInstance(triple_, true);
  if (!llvm_instance) return false;
  std::unique_ptr<llvm::MCSubtargetInfo> mc_info(
      llvm_instance->createMCSubtargetInfo(triple_, "", ""));
  // Use isCPUStringValid which correctly handles CPU aliases (e.g. apple-m1
  // in LLVM 22+) that don't appear in getAllProcessorDescriptions().
  return mc_info && mc_info->isCPUStringValid(cpu);
}

@tqchen
Copy link
Member

tqchen commented Mar 7, 2026

thanks, please check if the gemini suggestion makes sense

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants