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


##########
src/target/llvm/codegen_aarch64.cc:
##########
@@ -58,9 +57,18 @@ void CodeGenAArch64::AddFunction(const GlobalVar& gvar, 
const PrimFunc& f) {
 void CodeGenAArch64::SetTargetAttributes(llvm::Function* func) {
   // Add vscale_range() function attribute when appropriate.
   if (llvm_target_->TargetHasCPUFeature("sve") || 
llvm_target_->TargetHasCPUFeature("sme")) {
-    auto kVScaleValues = arith::GetVScaleValues(Target::Current());
-    if (!kVScaleValues.empty()) {
-      unsigned int max_val = *std::max_element(kVScaleValues.begin(), 
kVScaleValues.end());
+    // Compute max_val = largest power-of-two <= vector_width/8.
+    static auto llvm_get_vector_width_fn =
+        tvm::ffi::Function::GetGlobalRequired("target.llvm_get_vector_width");
+    unsigned int vector_width =
+        static_cast<unsigned 
int>(llvm_get_vector_width_fn(Target::Current()).cast<int>());
+    unsigned int max_val = 0;
+    for (unsigned int i = 0;; ++i) {
+      unsigned int power = 1u << i;
+      if (power > (vector_width / 8)) break;
+      max_val = power;
+    }
+    if (max_val > 0) {

Review Comment:
   ![high](https://www.gstatic.com/codereviewagent/high-priority.svg)
   
   Defensive programming check: `Target::Current()` can return an undefined 
(null) `Target` if called outside of an active target context. Passing an 
undefined `Target` to `llvm_get_vector_width_fn` will cause a runtime crash or 
FFI error. We should check if the target is defined before querying the vector 
width.
   
   ```suggestion
       // Compute max_val = largest power-of-two <= vector_width/8.
       static auto llvm_get_vector_width_fn =
           
tvm::ffi::Function::GetGlobalRequired("target.llvm_get_vector_width");
       unsigned int max_val = 0;
       if (auto target = Target::Current(); target.defined()) {
         unsigned int vector_width =
             static_cast<unsigned 
int>(llvm_get_vector_width_fn(target).cast<int>());
         for (unsigned int i = 0;; ++i) {
           unsigned int power = 1u << i;
           if (power > (vector_width / 8)) break;
           max_val = power;
         }
       }
       if (max_val > 0) {
   ```



-- 
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