onnx / onnx-mlir

Representation and Reference Lowering of ONNX Models in MLIR Compiler Infrastructure
Apache License 2.0
760 stars 320 forks source link

Possible memory leak in getDataLayout(const Location &loc) #1389

Open etiotto opened 2 years ago

etiotto commented 2 years ago

Potential memory leaks reported by valgrind:

==95294== 25 bytes in 1 blocks are possibly lost in loss record 4 of 91
==95294==    at 0x51AEE63: operator new(unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==95294==    by 0x268868C: llvm::TargetMachine::TargetMachine(llvm::Target const&, llvm::StringRef, llvm::Triple const&, llvm::StringRef, llvm::StringRef, llvm::TargetOptions const&) (in /workdir/onnx-mlir/build/Debug/bin/TestMatMul2D)
==95294==    by 0x2113CCC: llvm::LLVMTargetMachine::LLVMTargetMachine(llvm::Target const&, llvm::StringRef, llvm::Triple const&, llvm::StringRef, llvm::StringRef, llvm::TargetOptions const&, llvm::Reloc::Model, llvm::CodeModel::Model, llvm::CodeGenOpt::Level) (in /workdir/onnx-mlir/build/Debug/bin/TestMatMul2D)
==95294==    by 0x17EAAF6: llvm::X86TargetMachine::X86TargetMachine(llvm::Target const&, llvm::Triple const&, llvm::StringRef, llvm::StringRef, llvm::TargetOptions const&, llvm::Optional<llvm::Reloc::Model>, llvm::Optional<llvm::CodeModel::Model>, llvm::CodeGenOpt::Level, bool) (in /workdir/onnx-mlir/build/Debug/bin/TestMatMul2D)
==95294==    by 0x17EAF35: llvm::RegisterTargetMachine<llvm::X86TargetMachine>::Allocator(llvm::Target const&, llvm::Triple const&, llvm::StringRef, llvm::StringRef, llvm::TargetOptions const&, llvm::Optional<llvm::Reloc::Model>, llvm::Optional<llvm::CodeModel::Model>, llvm::CodeGenOpt::Level, bool) (in /workdir/onnx-mlir/build/Debug/bin/TestMatMul2D)
==95294==    by 0x4530E5: llvm::Target::createTargetMachine(llvm::StringRef, llvm::StringRef, llvm::StringRef, llvm::TargetOptions const&, llvm::Optional<llvm::Reloc::Model>, llvm::Optional<llvm::CodeModel::Model>, llvm::CodeGenOpt::Level, bool) const (TargetRegistry.h:455)
==95294==    by 0x45175C: getDataLayout(mlir::Location const&) (CompilerUtils.cpp:714)
==95294==    by 0x451A8D: setupModule(mlir::OwningOpRef<mlir::ModuleOp>&, mlir::MLIRContext&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) (CompilerUtils.cpp:742)
==95294==    by 0x451E33: compileModule(mlir::OwningOpRef<mlir::ModuleOp>&, mlir::MLIRContext&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, onnx_mlir::EmissionTargetType) (CompilerUtils.cpp:766)
==95294==    by 0x44604E: onnx_mlir::test::ModelLibBuilder::compileAndLoad() (ModelLib.cpp:44)
==95294==    by 0x3F89E8: onnx_mlir::test::isOMMatmulTheSameAsNaiveImplFor(int, int, int) (TestMatMul2D.cpp:26)
==95294==    by 0x3F8B81: main::{lambda()#1}::operator()() const (TestMatMul2D.cpp:51)

It seems to be located in the static std::string getDataLayout(const Location &loc) function, where target machines are being created… and maybe they should be freed?

Similar leak:


==95294== 3,567,700 (682,096 direct, 2,885,604 indirect) bytes in 479 blocks are definitely lost in loss record 91 of 91
==95294==    at 0x51AEE63: operator new(unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==95294==    by 0x17EAEF8: llvm::RegisterTargetMachine<llvm::X86TargetMachine>::Allocator(llvm::Target const&, llvm::Triple const&, llvm::StringRef, llvm::StringRef, llvm::TargetOptions const&, llvm::Optional<llvm::Reloc::Model>, llvm::Optional<llvm::CodeModel::Model>, llvm::CodeGenOpt::Level, bool) (in /workdir/onnx-mlir/build/Debug/bin/TestMatMul2D)
==95294==    by 0x4530E5: llvm::Target::createTargetMachine(llvm::StringRef, llvm::StringRef, llvm::StringRef, llvm::TargetOptions const&, llvm::Optional<llvm::Reloc::Model>, llvm::Optional<llvm::CodeModel::Model>, llvm::CodeGenOpt::Level, bool) const (TargetRegistry.h:455)
==95294==    by 0x45175C: getDataLayout(mlir::Location const&) (CompilerUtils.cpp:714)
==95294==    by 0x451A8D: setupModule(mlir::OwningOpRef<mlir::ModuleOp>&, mlir::MLIRContext&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) (CompilerUtils.cpp:742)
==95294==    by 0x451E33: compileModule(mlir::OwningOpRef<mlir::ModuleOp>&, mlir::MLIRContext&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, onnx_mlir::EmissionTargetType) (CompilerUtils.cpp:766)
==95294==    by 0x44604E: onnx_mlir::test::ModelLibBuilder::compileAndLoad() (ModelLib.cpp:44)
==95294==    by 0x3F89E8: onnx_mlir::test::isOMMatmulTheSameAsNaiveImplFor(int, int, int) (TestMatMul2D.cpp:26)
==95294==    by 0x3F923C: main (TestMatMul2D.cpp:71)
AlexandreEichenberger commented 2 years ago

@etiotto let me know if you need the full traces. Or you may simply run valgrind Debug/bin/TestMatMul2D.