You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by "MasterJH5574 (via GitHub)" <gi...@apache.org> on 2026/02/02 15:18:05 UTC

[PR] [LLVM] Fix insertDbgValueIntrinsic for Metal backend [tvm]

MasterJH5574 opened a new pull request, #18706:
URL: https://github.com/apache/tvm/pull/18706

   Some TIR code could not be compiled to Metal due to an LLVM issue, as described in #18585.  This PR fixes the issue according to the Option 2 in
   https://github.com/apache/tvm/issues/18585#issuecomment-3649857591.
   
   Unit tests are updated, though they are not enabled in CI for now.


-- 
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: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [LLVM] Fix insertDbgValueIntrinsic for Metal backend [tvm]

Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
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: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [LLVM] Fix insertDbgValueIntrinsic for Metal backend [tvm]

Posted by "tqchen (via GitHub)" <gi...@apache.org>.
tqchen merged PR #18706:
URL: https://github.com/apache/tvm/pull/18706


-- 
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: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [LLVM] Fix insertDbgValueIntrinsic for Metal backend [tvm]

Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on code in PR #18706:
URL: https://github.com/apache/tvm/pull/18706#discussion_r2754944997


##########
src/target/llvm/codegen_llvm.cc:
##########
@@ -2323,6 +2323,32 @@ 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 TVM_LLVM_VERSION >= 150
+  // LLVM 15+ requires dbg_declare to reference pointer or integer types only.

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   The comment is slightly inaccurate. According to the LLVM 15 source code for `DIBuilder::insertDeclare`, it asserts that the storage must be a pointer type. The mention of "integer types" seems incorrect. It should be just "pointer types".
   
   ```c
     // LLVM 15+ requires dbg_declare to reference pointer types only.
   ```



-- 
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: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [LLVM] Fix insertDbgValueIntrinsic for Metal backend [tvm]

Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on PR #18706:
URL: https://github.com/apache/tvm/pull/18706#issuecomment-3835786876

   ## Summary of Changes
   
   Hello @MasterJH5574, I'm Gemini Code Assist[^1]! 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 resolves a critical compilation issue affecting the Metal backend when using specific LLVM versions. It refines how debug information is handled for different data types, ensuring compatibility with LLVM's stricter requirements for debug intrinsics. The changes enable successful compilation of previously failing TIR code for Metal.
   
   ### Highlights
   
   * **LLVM Debug Intrinsic Fix**: Implemented a conditional fix in `CodeGenLLVM::AddDebugInformation` to correctly use `insertDbgValueIntrinsic` for non-pointer types (floats, vectors) when compiling with LLVM 15 or newer, addressing a Metal backend compilation issue.
   * **LLVM Version Compatibility**: Adapted the `insertDbgValueIntrinsic` call signature for LLVM 20+ to use `BasicBlock::iterator` as the insertion point, ensuring forward compatibility.
   * **Test Updates**: Updated `test_gpu_codegen_allreduce.py` to include `tvm_ffi` import and refactor `compile_metal` import, and `test_target_codegen_metal.py` to use `tvm.tir.build` instead of `tvm.compile`.
   
   🧠 **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](https://codeassist.google/code-review/login) to enable Memory in your admin console.**
   
   
   
   
   
   
   
   <details>
   <summary><b>Using Gemini Code Assist</b></summary>
   <br>
   
   The full guide for Gemini Code Assist can be found on our [documentation page](https://developers.google.com/gemini-code-assist/docs/review-github-code), here are some quick tips.
   
   <b>Invoking Gemini</b>
   
   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.
   
   <b>Customization</b>
   
   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](https://developers.google.com/gemini-code-assist/docs/customize-gemini-behavior-github).
   
   <b>Limitations & Feedback</b>
   
   Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with :thumbsup: and :thumbsdown: 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](https://google.qualtrics.com/jfe/form/SV_2cyuGuTWsEw84yG).
   
   <b>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](https://cloud.google.com/products/gemini/code-assist).</b>
   </details>
   
   
   
   
   [^1]: Review the [Privacy Notices](https://policies.google.com/privacy), [Generative AI Prohibited Use Policy](https://policies.google.com/terms/generative-ai/use-policy), [Terms of Service](https://policies.google.com/terms), and learn how to configure Gemini Code Assist in GitHub [here](https://developers.google.com/gemini-code-assist/docs/customize-gemini-behavior-github). Gemini can make mistakes, so double check it and [use code with caution](https://support.google.com/legal/answer/13505487).
   


-- 
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: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [LLVM] Fix insertDbgValueIntrinsic for Metal backend [tvm]

Posted by "MasterJH5574 (via GitHub)" <gi...@apache.org>.
MasterJH5574 commented on PR #18706:
URL: https://github.com/apache/tvm/pull/18706#issuecomment-3835910518

   /gemini review


-- 
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: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org