cbalint13 commented on PR #16425:
URL: https://github.com/apache/tvm/pull/16425#issuecomment-2019031916

   > It seems CI was failing due to a memory leak observed when calling 
`GetAllLLVMCpuFeatures()` and `GetAllLLVMTargetArches()`. The following is a 
valgrind report for an executable that creates a new target `tvm::Target("llvm 
-mtriple=aarch64-linux-gnu")` with this PR's changes:
   > 
   > ```
   > ...
   > ==356347== 8,040 (1,560 direct, 6,480 indirect) bytes in 1 blocks are 
definitely lost in loss record 42,621 of 42,667
   > ==356347==    at 0x4849013: operator new(unsigned long) (in 
/usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
   > ==356347==    by 0x1136B1B9: ??? (in 
/usr/lib/x86_64-linux-gnu/libLLVM-17.so.1)
   > ==356347==    by 0xBC44347: 
llvm::Target::createTargetMachine(llvm::StringRef, llvm::StringRef, 
llvm::StringRef, llvm::TargetOptions const&, std::optional<llvm::Reloc::Model>, 
std::optional<llvm::CodeModel::Model>, llvm::CodeGenOpt::Level, bool) const 
(TargetRegistry.h:488)
   > ==356347==    by 0xBC3EF2D: 
tvm::codegen::CreateLLVMTargetMachine(llvm::Target const*, 
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > 
const&, std::__cxx11::basic_string<char, std::char_traits<char>, 
std::allocator<char> > const&, std::__cxx11::basic_string<char, 
std::char_traits<char>, std::allocator<char> > const&, llvm::TargetOptions 
const&, llvm::Reloc::Model const&, llvm::CodeModel::Model const&, 
llvm::CodeGenOpt::Level const&) (llvm_instance.cc:398)
   > ==356347==    by 0xBC3F0B2: 
tvm::codegen::GetLLVMSubtargetInfo(std::__cxx11::basic_string<char, 
std::char_traits<char>, std::allocator<char> > const&, 
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > 
const&, std::__cxx11::basic_string<char, std::char_traits<char>, 
std::allocator<char> > const&) (llvm_instance.cc:413)
   > ==356347==    by 0xBC41E09: 
tvm::codegen::LLVMTargetInfo::GetAllLLVMTargetArches() (llvm_instance.cc:843)
   > ==356347==    by 0xBC3D5CA: 
tvm::codegen::LLVMTargetInfo::LLVMTargetInfo(tvm::codegen::LLVMInstance&, 
tvm::runtime::Map<tvm::runtime::String, tvm::runtime::ObjectRef, void, void> 
const&) (llvm_instance.cc:220)
   > ==356347==    by 0xAD6FCE8: 
tvm::target::parsers::aprofile::GetFeatures(tvm::runtime::Map<tvm::runtime::String,
 tvm::runtime::ObjectRef, void, void>) (aprofile.cc:101)
   > ==356347==    by 0xAD70A5E: 
tvm::target::parsers::aprofile::ParseTarget(tvm::runtime::Map<tvm::runtime::String,
 tvm::runtime::ObjectRef, void, void>) (aprofile.cc:137)
   > ==356347==    by 0xAD71C25: 
tvm::target::parsers::cpu::ParseTarget(tvm::runtime::Map<tvm::runtime::String, 
tvm::runtime::ObjectRef, void, void>) (cpu.cc:55)
   > ==356347==    by 0xAEAB127: 
tvm::runtime::TypedPackedFunc<tvm::runtime::Map<tvm::runtime::String, 
tvm::runtime::ObjectRef, void, void> (tvm::runtime::Map<tvm::runtime::String, 
tvm::runtime::ObjectRef, void, 
void>)>::AssignTypedLambda<tvm::runtime::Map<tvm::runtime::String, 
tvm::runtime::ObjectRef, void, void> 
(*)(tvm::runtime::Map<tvm::runtime::String, tvm::runtime::ObjectRef, void, 
void>)>(tvm::runtime::Map<tvm::runtime::String, tvm::runtime::ObjectRef, void, 
void> (*)(tvm::runtime::Map<tvm::runtime::String, tvm::runtime::ObjectRef, 
void, void>))::{lambda(tvm::runtime::TVMArgs const&, 
tvm::runtime::TVMRetValue*)#1}::operator()(tvm::runtime::TVMArgs const&, 
tvm::runtime::TVMRetValue*) const (packed_func.h:1826)
   > ==356347==    by 0xAEB3382: 
tvm::runtime::PackedFuncObj::Extractor<tvm::runtime::PackedFuncSubObj<tvm::runtime::TypedPackedFunc<tvm::runtime::Map<tvm::runtime::String,
 tvm::runtime::ObjectRef, void, void> (tvm::runtime::Map<tvm::runtime::String, 
tvm::runtime::ObjectRef, void, 
void>)>::AssignTypedLambda<tvm::runtime::Map<tvm::runtime::String, 
tvm::runtime::ObjectRef, void, void> 
(*)(tvm::runtime::Map<tvm::runtime::String, tvm::runtime::ObjectRef, void, 
void>)>(tvm::runtime::Map<tvm::runtime::String, tvm::runtime::ObjectRef, void, 
void> (*)(tvm::runtime::Map<tvm::runtime::String, tvm::runtime::ObjectRef, 
void, void>))::{lambda(tvm::runtime::TVMArgs const&, 
tvm::runtime::TVMRetValue*)#1}> >::Call(tvm::runtime::PackedFuncObj const*, 
tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*) (packed_func.h:1252)
   > ==356347== 
   > ==356347== 8,040 (1,560 direct, 6,480 indirect) bytes in 1 blocks are 
definitely lost in loss record 42,622 of 42,667
   > ==356347==    at 0x4849013: operator new(unsigned long) (in 
/usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
   > ==356347==    by 0x1136B1B9: ??? (in 
/usr/lib/x86_64-linux-gnu/libLLVM-17.so.1)
   > ==356347==    by 0xBC44347: 
llvm::Target::createTargetMachine(llvm::StringRef, llvm::StringRef, 
llvm::StringRef, llvm::TargetOptions const&, std::optional<llvm::Reloc::Model>, 
std::optional<llvm::CodeModel::Model>, llvm::CodeGenOpt::Level, bool) const 
(TargetRegistry.h:488)
   > ==356347==    by 0xBC3EF2D: 
tvm::codegen::CreateLLVMTargetMachine(llvm::Target const*, 
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > 
const&, std::__cxx11::basic_string<char, std::char_traits<char>, 
std::allocator<char> > const&, std::__cxx11::basic_string<char, 
std::char_traits<char>, std::allocator<char> > const&, llvm::TargetOptions 
const&, llvm::Reloc::Model const&, llvm::CodeModel::Model const&, 
llvm::CodeGenOpt::Level const&) (llvm_instance.cc:398)
   > ==356347==    by 0xBC3F0B2: 
tvm::codegen::GetLLVMSubtargetInfo(std::__cxx11::basic_string<char, 
std::char_traits<char>, std::allocator<char> > const&, 
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > 
const&, std::__cxx11::basic_string<char, std::char_traits<char>, 
std::allocator<char> > const&) (llvm_instance.cc:413)
   > ==356347==    by 0xBC4217B: 
tvm::codegen::LLVMTargetInfo::GetAllLLVMCpuFeatures() (llvm_instance.cc:868)
   > ==356347==    by 0xAD6FD01: 
tvm::target::parsers::aprofile::GetFeatures(tvm::runtime::Map<tvm::runtime::String,
 tvm::runtime::ObjectRef, void, void>) (aprofile.cc:102)
   > ==356347==    by 0xAD70A5E: 
tvm::target::parsers::aprofile::ParseTarget(tvm::runtime::Map<tvm::runtime::String,
 tvm::runtime::ObjectRef, void, void>) (aprofile.cc:137)
   > ==356347==    by 0xAD71C25: 
tvm::target::parsers::cpu::ParseTarget(tvm::runtime::Map<tvm::runtime::String, 
tvm::runtime::ObjectRef, void, void>) (cpu.cc:55)
   > ==356347==    by 0xAEAB127: 
tvm::runtime::TypedPackedFunc<tvm::runtime::Map<tvm::runtime::String, 
tvm::runtime::ObjectRef, void, void> (tvm::runtime::Map<tvm::runtime::String, 
tvm::runtime::ObjectRef, void, 
void>)>::AssignTypedLambda<tvm::runtime::Map<tvm::runtime::String, 
tvm::runtime::ObjectRef, void, void> 
(*)(tvm::runtime::Map<tvm::runtime::String, tvm::runtime::ObjectRef, void, 
void>)>(tvm::runtime::Map<tvm::runtime::String, tvm::runtime::ObjectRef, void, 
void> (*)(tvm::runtime::Map<tvm::runtime::String, tvm::runtime::ObjectRef, 
void, void>))::{lambda(tvm::runtime::TVMArgs const&, 
tvm::runtime::TVMRetValue*)#1}::operator()(tvm::runtime::TVMArgs const&, 
tvm::runtime::TVMRetValue*) const (packed_func.h:1826)
   > ==356347==    by 0xAEB3382: 
tvm::runtime::PackedFuncObj::Extractor<tvm::runtime::PackedFuncSubObj<tvm::runtime::TypedPackedFunc<tvm::runtime::Map<tvm::runtime::String,
 tvm::runtime::ObjectRef, void, void> (tvm::runtime::Map<tvm::runtime::String, 
tvm::runtime::ObjectRef, void, 
void>)>::AssignTypedLambda<tvm::runtime::Map<tvm::runtime::String, 
tvm::runtime::ObjectRef, void, void> 
(*)(tvm::runtime::Map<tvm::runtime::String, tvm::runtime::ObjectRef, void, 
void>)>(tvm::runtime::Map<tvm::runtime::String, tvm::runtime::ObjectRef, void, 
void> (*)(tvm::runtime::Map<tvm::runtime::String, tvm::runtime::ObjectRef, 
void, void>))::{lambda(tvm::runtime::TVMArgs const&, 
tvm::runtime::TVMRetValue*)#1}> >::Call(tvm::runtime::PackedFuncObj const*, 
tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*) (packed_func.h:1252)
   > ==356347==    by 0xAE8D453: CallPacked (packed_func.h:1256)
   > ==356347==    by 0xAE8D453: tvm::runtime::TVMRetValue 
tvm::runtime::PackedFunc::operator()<tvm::runtime::Map<tvm::runtime::String, 
tvm::runtime::ObjectRef, void, void> >(tvm::runtime::Map<tvm::runtime::String, 
tvm::runtime::ObjectRef, void, void>&&) const (packed_func.h:1784)
   > ...
   > ```
   > 
   > The problem seems to come from `GetLLVMSubtargetInfo(...)` which creates a 
target machine instance to get a pointer to an `MCSubtargetInfo` object. The 
reference to the created target machine is lost and the memory is not freed. 
I've tried to naively rework the code to remove the GetLLVMSubtargetInfo(...) 
function and therefore avoid the leak, but happy to hear any better ideas (cc 
@cbalint13, @kparzysz)
   
   * Is this happening with llvm18 too ?
   * I look into this (across multiple llvm versions), could help me with a 
simple testcase ?


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

Reply via email to