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


##########
src/target/llvm/codegen_llvm.cc:
##########
@@ -2323,22 +2323,27 @@ void CodeGenLLVM::AddDebugInformation(llvm::Value* 
llvm_value, const Var& tir_va
 
   auto* di_loc = llvm::DILocation::get(*llvm_target_->GetContext(), 0, 0, 
di_subprogram_);
 
-  if (insert_before) {
+#if TVM_LLVM_VERSION >= 150
+  // LLVM 15+ requires dbg_declare to reference pointer or integer types only.
+  // For non-pointer types (floats, vectors), use dbg_value instead to track
+  // the SSA value directly rather than a memory location.
+  if (!llvm_value->getType()->isPointerTy()) {
+    if (insert_before) {
+      // LLVM 20+ changed insertDbgValueIntrinsic to take BasicBlock::iterator
+      // instead of Instruction* for the insertion point.
 #if TVM_LLVM_VERSION >= 200
-    dbg_info_->di_builder_->insertDeclare(
-        llvm_value, local_var, dbg_info_->di_builder_->createExpression(), 
llvm::DebugLoc(di_loc),
-        llvm::BasicBlock::iterator(insert_before));
-#else
-    dbg_info_->di_builder_->insertDeclare(llvm_value, local_var,
-                                          
dbg_info_->di_builder_->createExpression(),
-                                          llvm::DebugLoc(di_loc), 
insert_before);
-#endif
-  } else {
-    dbg_info_->di_builder_->insertDeclare(llvm_value, local_var,
-                                          
dbg_info_->di_builder_->createExpression(),
-                                          llvm::DebugLoc(di_loc), 
builder_->GetInsertBlock());
+      dbg_info_->di_builder_->insertDbgValueIntrinsic(
+          llvm_value, local_var, dbg_info_->di_builder_->createExpression(), 
llvm::DebugLoc(di_loc),
+          llvm::BasicBlock::iterator(insert_before));
+    } else {
+      dbg_info_->di_builder_->insertDbgValueIntrinsic(
+          llvm_value, local_var, dbg_info_->di_builder_->createExpression(), 
llvm::DebugLoc(di_loc),
+          builder_->GetInsertBlock());
+    }
+    return;
   }
 #endif
+#endif

Review Comment:
   ![critical](https://www.gstatic.com/codereviewagent/critical.svg)
   
   This change introduces two issues:
   
   1.  **Regression**: The original code block that calls `insertDeclare` has 
been completely removed. This will prevent debug information from being 
generated for pointer types on LLVM >= 150 and for all types on LLVM versions 
older than 150, which is a significant regression.
   
   2.  **Bug**: The new logic for `insertDbgValueIntrinsic` is incomplete. For 
LLVM versions between 15.0 and 19.x, `insertDbgValueIntrinsic` expects an 
`Instruction*` as its last argument when `insert_before` is true. The current 
code is missing the `else` branch for `#if TVM_LLVM_VERSION >= 200`, so no code 
will be generated in this case.
   
   The fix should be to use `insertDbgValueIntrinsic` for non-pointer types on 
LLVM 15+ and fall back to the original `insertDeclare` logic for pointer types 
and older LLVM versions. Also, the missing `else` branch needs to be added.
   
   ```c
   #if TVM_LLVM_VERSION >= 150
     // LLVM 15+ requires dbg_declare to reference pointer or integer types 
only.
     // For non-pointer types (floats, vectors), use dbg_value instead to track
     // the SSA value directly rather than a memory location.
     if (!llvm_value->getType()->isPointerTy()) {
       if (insert_before) {
         // LLVM 20+ changed insertDbgValueIntrinsic to take 
BasicBlock::iterator
         // instead of Instruction* for the insertion point.
   #if TVM_LLVM_VERSION >= 200
         dbg_info_->di_builder_->insertDbgValueIntrinsic(
             llvm_value, local_var, dbg_info_->di_builder_->createExpression(),
             llvm::DebugLoc(di_loc), llvm::BasicBlock::iterator(insert_before));
   #else
         dbg_info_->di_builder_->insertDbgValueIntrinsic(
             llvm_value, local_var, dbg_info_->di_builder_->createExpression(),
             llvm::DebugLoc(di_loc), insert_before);
   #endif
       } else {
         dbg_info_->di_builder_->insertDbgValueIntrinsic(
             llvm_value, local_var, dbg_info_->di_builder_->createExpression(),
             llvm::DebugLoc(di_loc), builder_->GetInsertBlock());
       }
       return;
     }
   #endif
   
     if (insert_before) {
   #if TVM_LLVM_VERSION >= 200
       dbg_info_->di_builder_->insertDeclare(
           llvm_value, local_var, dbg_info_->di_builder_->createExpression(), 
llvm::DebugLoc(di_loc),
           llvm::BasicBlock::iterator(insert_before));
   #else
       dbg_info_->di_builder_->insertDeclare(llvm_value, local_var,
                                             
dbg_info_->di_builder_->createExpression(),
                                             llvm::DebugLoc(di_loc), 
insert_before);
   #endif
     } else {
       dbg_info_->di_builder_->insertDeclare(llvm_value, local_var,
                                             
dbg_info_->di_builder_->createExpression(),
                                             llvm::DebugLoc(di_loc), 
builder_->GetInsertBlock());
     }
   #endif
   ```



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