This is an automated email from the ASF dual-hosted git repository. lukhut pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/tvm.git
The following commit(s) were added to refs/heads/main by this push: new c67a055386 [BugFix][Target] Added null check to fix segfault at ->defined() in cpu.cc DetectSystemTriple() (#16766) c67a055386 is described below commit c67a05538642d24c75555b02103800d7f6a1ceaf Author: Otto Rasmussen <34154515+ottow...@users.noreply.github.com> AuthorDate: Thu Apr 11 10:12:20 2024 +0200 [BugFix][Target] Added null check to fix segfault at ->defined() in cpu.cc DetectSystemTriple() (#16766) I ran into a problem running a very simple ONNX compile, i would get a segfault at a FoldConstantExpr() call from TVMC. **This only happens if the compile flag `set(USE_LLVM OFF)` is OFF.** ``` Thread 1 "python3" received signal SIGSEGV, Segmentation fault. 0x00007fffc94ac78c in tvm::runtime::ObjectPtr<tvm::runtime::Object>::operator!=(decltype(nullptr)) const (this=0x0, null=<error reading variable: Attempt to dereference a generic pointer.>) at /home/otto/tvm/include/tvm/runtime/object.h:470 470 bool operator!=(std::nullptr_t null) const { return data_ != null; } ``` I had compiled TVM Using GCC: ``` COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa OFFLOAD_TARGET_DEFAULT=1 Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 11.4.0-1ubuntu1~22.04' --with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-11 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu - [...] Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04) ``` This was caused by a call to defined() from DetectSystemTriple() in cpu.cc that was added in #16513. When the previous call ``` auto pf = tvm::runtime::Registry::Get("target.llvm_get_system_triple"); ``` would fail, and return null. The consecutive call to defined() would segfault after trying to dereference the null value. This commit adds a check to see if the function pointer is null. This might not be the best solution, but it worked for me, so it might also help someone else struggling with this. Please suggest a better solution, if you know one. Co-authored-by: Luke Hutton <luke.hut...@arm.com> --- src/target/parsers/cpu.cc | 8 +++++--- tests/cpp/target_test.cc | 35 ++++++++++++++++++++--------------- tests/cpp/tir_scalable_datatype.cc | 5 ++++- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/target/parsers/cpu.cc b/src/target/parsers/cpu.cc index 13f41e0e1c..5fd5fdeccc 100644 --- a/src/target/parsers/cpu.cc +++ b/src/target/parsers/cpu.cc @@ -29,10 +29,12 @@ namespace parsers { namespace cpu { Optional<String> DetectSystemTriple() { +#ifdef TVM_LLVM_VERSION auto pf = tvm::runtime::Registry::Get("target.llvm_get_system_triple"); - if (pf->defined()) { - return (*pf)(); - } + ICHECK(pf != nullptr) << "The target llvm_get_system_triple was not found, " + "please compile with USE_LLVM = ON"; + return (*pf)(); +#endif return {}; } diff --git a/tests/cpp/target_test.cc b/tests/cpp/target_test.cc index b32af0e9c7..2db4b572bf 100644 --- a/tests/cpp/target_test.cc +++ b/tests/cpp/target_test.cc @@ -290,6 +290,7 @@ TEST(TargetCreation, ProcessStrings) { ASSERT_EQ(array7[1][1][0], "fred"); } +#ifdef TVM_LLVM_VERSION // Checks that malformed options cause an assertion. TEST(TargetCreation, LLVMCommandLineParseFatalDashDashDash) { tvm::codegen::LLVMInstance inst; @@ -448,6 +449,25 @@ TEST(TargetCreation, LLVMCommandLineSaveRestore) { ASSERT_FALSE(info.MatchesGlobalState()); } +TEST(TargetCreation, DetectSystemTriple) { + Map<String, ObjectRef> config = { + {"kind", String("llvm")}, + }; + + Target target = Target(config); + ICHECK_EQ(target->kind, TargetKind::Get("llvm").value()); + + auto pf = tvm::runtime::Registry::Get("target.llvm_get_system_triple"); + if (pf == nullptr) { + GTEST_SKIP() << "LLVM is not available, skipping test"; + } + + Optional<String> mtriple = target->GetAttr<String>("mtriple"); + ASSERT_TRUE(mtriple.value() == String((*pf)())); +} + +#endif + TVM_REGISTER_TARGET_KIND("test_external_codegen_0", kDLCUDA) .set_attr<Bool>(tvm::attr::kIsExternalCodegen, Bool(true)); @@ -498,21 +518,6 @@ TEST(TargetCreation, DeduplicateKeys) { ICHECK_EQ(target->GetAttr<String>("device"), "arm_cpu"); } -TEST(TargetCreation, DetectSystemTriple) { - Map<String, ObjectRef> config = { - {"kind", String("llvm")}, - }; - - Target target = Target(config); - ICHECK_EQ(target->kind, TargetKind::Get("llvm").value()); - - Optional<String> mtriple = target->GetAttr<String>("mtriple"); - auto pf = tvm::runtime::Registry::Get("target.llvm_get_system_triple"); - if (!pf->defined()) { - GTEST_SKIP() << "LLVM is not available, skipping test"; - } -} - TEST(TargetKindRegistry, ListTargetKinds) { Array<String> names = TargetKindRegEntry::ListTargetKinds(); ICHECK_EQ(names.empty(), false); diff --git a/tests/cpp/tir_scalable_datatype.cc b/tests/cpp/tir_scalable_datatype.cc index 4b4764555f..da30706e13 100644 --- a/tests/cpp/tir_scalable_datatype.cc +++ b/tests/cpp/tir_scalable_datatype.cc @@ -19,11 +19,14 @@ #include <gmock/gmock.h> #include <gtest/gtest.h> -#include <llvm/IR/Intrinsics.h> #include <tvm/runtime/data_type.h> #include <tvm/tir/builtin.h> #include <tvm/tir/expr.h> +#ifdef TVM_LLVM_VERSION +#include <llvm/IR/Intrinsics.h> +#endif + #include "../../src/script/printer/utils.h" using ::testing::HasSubstr;