Skip to content

Commit 8f40544

Browse files
authored
[LLVM] Fix insertDbgValueIntrinsic for Metal backend (#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 #18585 (comment). Unit tests are updated, though they are not enabled in CI for now.
1 parent c575973 commit 8f40544

3 files changed

Lines changed: 31 additions & 2 deletions

File tree

src/target/llvm/codegen_llvm.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2323,6 +2323,32 @@ void CodeGenLLVM::AddDebugInformation(llvm::Value* llvm_value, const Var& tir_va
23232323

23242324
auto* di_loc = llvm::DILocation::get(*llvm_target_->GetContext(), 0, 0, di_subprogram_);
23252325

2326+
#if TVM_LLVM_VERSION >= 150
2327+
// LLVM 15+ requires dbg_declare to reference pointer or integer types only.
2328+
// For non-pointer types (floats, vectors), use dbg_value instead to track
2329+
// the SSA value directly rather than a memory location.
2330+
if (!llvm_value->getType()->isPointerTy()) {
2331+
if (insert_before) {
2332+
// LLVM 20+ changed insertDbgValueIntrinsic to take BasicBlock::iterator
2333+
// instead of Instruction* for the insertion point.
2334+
#if TVM_LLVM_VERSION >= 200
2335+
dbg_info_->di_builder_->insertDbgValueIntrinsic(
2336+
llvm_value, local_var, dbg_info_->di_builder_->createExpression(), llvm::DebugLoc(di_loc),
2337+
llvm::BasicBlock::iterator(insert_before));
2338+
#else
2339+
dbg_info_->di_builder_->insertDbgValueIntrinsic(llvm_value, local_var,
2340+
dbg_info_->di_builder_->createExpression(),
2341+
llvm::DebugLoc(di_loc), insert_before);
2342+
#endif
2343+
} else {
2344+
dbg_info_->di_builder_->insertDbgValueIntrinsic(
2345+
llvm_value, local_var, dbg_info_->di_builder_->createExpression(), llvm::DebugLoc(di_loc),
2346+
builder_->GetInsertBlock());
2347+
}
2348+
return;
2349+
}
2350+
#endif
2351+
23262352
if (insert_before) {
23272353
#if TVM_LLVM_VERSION >= 200
23282354
dbg_info_->di_builder_->insertDeclare(

tests/python/codegen/test_gpu_codegen_allreduce.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717
import tvm
18+
import tvm_ffi
1819
import tvm.testing
1920
import numpy as np
2021
from tvm.script import tir as T
@@ -96,7 +97,9 @@ def optional_metal_compile_callback(define_metal_compile_callback):
9697

9798
@tvm.register_global_func(name, override=True)
9899
def compile_metal(src, target):
99-
return tvm.contrib.xcode.compile_metal(src, sdk="macosx")
100+
from tvm.contrib.xcode import compile_metal # pylint: disable=import-outside-toplevel
101+
102+
return compile_metal(src, sdk="macosx")
100103

101104
yield
102105

tests/python/codegen/test_target_codegen_metal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def compile_metal(src, target):
186186

187187
mod = tvm.IRModule({"main": func})
188188

189-
f = tvm.compile(mod, target="metal")
189+
f = tvm.tir.build(mod, target="metal")
190190
src: str = f.imports[0].inspect_source()
191191
occurrences = src.count("struct func_kernel_args_t")
192192
assert occurrences == 1, occurrences

0 commit comments

Comments
 (0)