From 7b766f924420e304ab643fb6bf6c0ef139760097 Mon Sep 17 00:00:00 2001 From: Balint Cristian Date: Mon, 25 Aug 2025 13:44:46 +0300 Subject: [PATCH] [LLVM][Fix] Do not emit debuginfo on vscale or other unknown types --- include/tvm/runtime/data_type.h | 4 ++- src/target/llvm/codegen_llvm.cc | 8 ++++-- .../codegen/test_target_codegen_riscv.py | 27 +++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/include/tvm/runtime/data_type.h b/include/tvm/runtime/data_type.h index e24768bde2f8..7236a9e3a2e0 100644 --- a/include/tvm/runtime/data_type.h +++ b/include/tvm/runtime/data_type.h @@ -206,7 +206,9 @@ class DataType { /*! \return whether type is a bool vector type. */ bool is_vector_bool() const { return is_scalable_or_fixed_length_vector() && bits() == 1; } /*! \return whether type is a Void type. */ - bool is_void() const { return code() == DataType::kHandle && bits() == 0 && lanes() == 0; } + bool is_void() const { + return code() == DataType::kHandle && bits() == 0 && static_cast(data_.lanes) == 0; + } /*! * \brief Create a new data type by change lanes to a specified value. * \param lanes The target number of lanes. diff --git a/src/target/llvm/codegen_llvm.cc b/src/target/llvm/codegen_llvm.cc index 5b2cb5cc95e3..ac73c9c3fccb 100644 --- a/src/target/llvm/codegen_llvm.cc +++ b/src/target/llvm/codegen_llvm.cc @@ -2274,9 +2274,11 @@ void CodeGenLLVM::AddDebugInformation(llvm::Value* llvm_value, const Var& tir_va #if TVM_LLVM_VERSION >= 50 if (!di_subprogram_) return; + auto dbg_dtype = GetDebugType(GetType(tir_var)); + // no invalid dtypes + if (!dbg_dtype) return; auto local_var = dbg_info_->di_builder_->createAutoVariable( - di_subprogram_, std::string(tir_var->name_hint), dbg_info_->file_, 0, - GetDebugType(GetType(tir_var))); + di_subprogram_, std::string(tir_var->name_hint), dbg_info_->file_, 0, dbg_dtype); auto* di_loc = llvm::DILocation::get(*llvm_target_->GetContext(), 0, 0, di_subprogram_); @@ -2330,6 +2332,8 @@ llvm::DIType* CodeGenLLVM::GetDebugType(const Type& ty_tir, llvm::Type* ty_llvm) return nullptr; } + if (dtype.is_scalable_vector()) return nullptr; + return dbg_info_->di_builder_->createBasicType(DLDataTypeToString(dtype).operator std::string(), dtype.bits() * dtype.lanes(), dwarf_type); diff --git a/tests/python/codegen/test_target_codegen_riscv.py b/tests/python/codegen/test_target_codegen_riscv.py index 1a30ab203f04..9e2d18e109f9 100644 --- a/tests/python/codegen/test_target_codegen_riscv.py +++ b/tests/python/codegen/test_target_codegen_riscv.py @@ -14,6 +14,7 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. +import pytest import tvm import tvm.testing from tvm.script import tir as T @@ -46,5 +47,31 @@ def load_vec(A: T.Buffer((N,), "int8")): check_rvv_presence(16, 32) +@tvm.testing.requires_llvm_minimum_version(14) +@tvm.testing.parametrize_targets( + "llvm -device=riscv_cpu -mtriple=riscv32-linux-gnu -mcpu=generic-rv32 -mattr=+i,+m,+v", + "llvm -device=riscv_cpu -mtriple=riscv64-linux-gnu -mcpu=generic-rv64 -mattr=+64bit,+a,+c,+d,+f,+m,+v", +) +def test_rvv_vscale_llvm_dbginfo(target): + # fmt: off + @T.prim_func + def rvv_with_vscale(A_handle: T.handle, B_handle: T.handle, C_handle: T.handle): + A = T.match_buffer(A_handle, (8,), dtype="float32", align=4, offset_factor=1) + B = T.match_buffer(B_handle, (4, 8), dtype="float32", align=4, offset_factor=1, strides=[8, 1]) + C = T.match_buffer(C_handle, (4,), dtype="float32", align=4, offset_factor=1) + with T.block("root"): + T.reads(A[0:8], B[0:4, 0:8]) + zero = T.call_llvm_intrin("float32xvscalex2", "llvm.riscv.vfmv.v.f", T.Broadcast(T.float32(0.0), T.vscale() * 2), C[0], T.uint64(1)) + vec_A = T.call_llvm_intrin("float32xvscalex4", "llvm.riscv.vle", T.Broadcast(T.float32(0.0), T.vscale() * 4), T.tvm_access_ptr(T.type_annotation("float32"), A.data, 0, 8, 1), T.int64(8)) + vec_B = T.call_llvm_intrin("float32xvscalex4", "llvm.riscv.vle", T.Broadcast(T.float32(0.0), T.vscale() * 4), T.tvm_access_ptr(T.type_annotation("float32"), B.data, 0 * 8, 8, 1), T.int64(8)) + prod = T.call_llvm_intrin("float32xvscalex4", "llvm.riscv.vfmul", T.Broadcast(T.float32(0.0), T.vscale() * 4), vec_A, vec_B, T.uint64(7), T.uint64(8)) + redsum = T.call_llvm_intrin("float32xvscalex2", "llvm.riscv.vfredusum", T.Broadcast(T.float32(0.0), T.vscale() * 2), prod, zero, T.uint64(7), T.uint64(8)) + # fmt: on + + # tvm.error.InternalError: Can't fetch the lanes of a scalable vector at a compile time. + with tvm.target.Target(target): + f = tvm.tir.build(rvv_with_vscale, target) + + if __name__ == "__main__": tvm.testing.main()