From 75a20a4b4b4485f76158719bcdb48310cf2552d4 Mon Sep 17 00:00:00 2001 From: Daniel Gustafsson Date: Fri, 17 Nov 2023 10:25:37 +0100 Subject: llvmjit: Use explicit LLVMContextRef for inlining When performing inlining LLVM unfortunately "leaks" types (the types survive and are usable, but a new round of inlining will recreate new structurally equivalent types). This accumulation will over time amount to a memory leak which for some queries can be large enough to trigger the OOM process killer. To avoid accumulation of types, all IR related data is stored in an LLVMContextRef which is dropped and recreated in order to release all types. Dropping and recreating incurs overhead, so it will be done only after 100 queries. This is a heuristic which might be revisited, but until we can get the size of the context from LLVM we are flying a bit blind. This issue has been reported several times, there may be more references to it in the archives on top of the threads linked below. This is a backpatch of 9dce22033d5 to all supported branches. Reported-By: Justin Pryzby Reported-By: Kurt Roeckx Reported-By: Jaime Casanova Reported-By: Lauri Laanmets Author: Andres Freund and Daniel Gustafsson Discussion: https://postgr.es/m/7acc8678-df5f-4923-9cf6-e843131ae89d@www.fastmail.com Discussion: https://postgr.es/m/20201218235607.GC30237@telsasoft.com Discussion: https://postgr.es/m/CAPH-tTxLf44s3CvUUtQpkDr1D8Hxqc2NGDzGXS1ODsfiJ6WSqA@mail.gmail.com Backpatch-through: v12 --- src/backend/jit/llvm/llvmjit_inline.cpp | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) (limited to 'src/backend/jit/llvm/llvmjit_inline.cpp') diff --git a/src/backend/jit/llvm/llvmjit_inline.cpp b/src/backend/jit/llvm/llvmjit_inline.cpp index 774d9e8b66d..2121e5b83f1 100644 --- a/src/backend/jit/llvm/llvmjit_inline.cpp +++ b/src/backend/jit/llvm/llvmjit_inline.cpp @@ -114,12 +114,12 @@ typedef llvm::StringMap > SummaryCache llvm::ManagedStatic summary_cache; -static std::unique_ptr llvm_build_inline_plan(llvm::Module *mod); +static std::unique_ptr llvm_build_inline_plan(LLVMContextRef lc, llvm::Module *mod); static void llvm_execute_inline_plan(llvm::Module *mod, ImportMapTy *globalsToInline); -static llvm::Module* load_module_cached(llvm::StringRef modPath); -static std::unique_ptr load_module(llvm::StringRef Identifier); +static llvm::Module* load_module_cached(LLVMContextRef c, llvm::StringRef modPath); +static std::unique_ptr load_module(LLVMContextRef c, llvm::StringRef Identifier); static std::unique_ptr llvm_load_summary(llvm::StringRef path); @@ -152,6 +152,18 @@ summaries_for_guid(const InlineSearchPath& path, llvm::GlobalValue::GUID guid); #define ilog(...) (void) 0 #endif +/* + * Reset inlining related state. This needs to be called before the currently + * used LLVMContextRef is disposed (and a new one create), otherwise we would + * have dangling references to deleted modules. + */ +void +llvm_inline_reset_caches(void) +{ + module_cache->clear(); + summary_cache->clear(); +} + /* * Perform inlining of external function references in M based on a simple * cost based analysis. @@ -159,9 +171,10 @@ summaries_for_guid(const InlineSearchPath& path, llvm::GlobalValue::GUID guid); void llvm_inline(LLVMModuleRef M) { + LLVMContextRef lc = LLVMGetModuleContext(M); llvm::Module *mod = llvm::unwrap(M); - std::unique_ptr globalsToInline = llvm_build_inline_plan(mod); + std::unique_ptr globalsToInline = llvm_build_inline_plan(lc, mod); if (!globalsToInline) return; llvm_execute_inline_plan(mod, globalsToInline.get()); @@ -172,7 +185,7 @@ llvm_inline(LLVMModuleRef M) * mod. */ static std::unique_ptr -llvm_build_inline_plan(llvm::Module *mod) +llvm_build_inline_plan(LLVMContextRef lc, llvm::Module *mod) { std::unique_ptr globalsToInline(new ImportMapTy()); FunctionInlineStates functionStates; @@ -271,7 +284,7 @@ llvm_build_inline_plan(llvm::Module *mod) continue; } - defMod = load_module_cached(modPath); + defMod = load_module_cached(lc, modPath); if (defMod->materializeMetadata()) elog(FATAL, "failed to materialize metadata"); @@ -466,20 +479,20 @@ llvm_execute_inline_plan(llvm::Module *mod, ImportMapTy *globalsToInline) * the cache state would get corrupted. */ static llvm::Module* -load_module_cached(llvm::StringRef modPath) +load_module_cached(LLVMContextRef lc, llvm::StringRef modPath) { auto it = module_cache->find(modPath); if (it == module_cache->end()) { it = module_cache->insert( - std::make_pair(modPath, load_module(modPath))).first; + std::make_pair(modPath, load_module(lc, modPath))).first; } return it->second.get(); } static std::unique_ptr -load_module(llvm::StringRef Identifier) +load_module(LLVMContextRef lc, llvm::StringRef Identifier) { LLVMMemoryBufferRef buf; LLVMModuleRef mod; @@ -491,7 +504,7 @@ load_module(llvm::StringRef Identifier) if (LLVMCreateMemoryBufferWithContentsOfFile(path, &buf, &msg)) elog(FATAL, "failed to open bitcode file \"%s\": %s", path, msg); - if (LLVMGetBitcodeModuleInContext2(LLVMGetGlobalContext(), buf, &mod)) + if (LLVMGetBitcodeModuleInContext2(lc, buf, &mod)) elog(FATAL, "failed to parse bitcode in file \"%s\"", path); /* -- cgit v1.2.3