gemini-code-assist[bot] commented on code in PR #18884:
URL: https://github.com/apache/tvm/pull/18884#discussion_r2897097911


##########
src/target/llvm/llvm_instance.cc:
##########
@@ -442,6 +440,17 @@ llvm::TargetMachine* 
LLVMTargetInfo::GetOrCreateTargetMachine(bool allow_missing
   return target_machine_.get();
 }
 
+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);
+}

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   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`.
   
   ```c
   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);
   }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to