[llvm] [SimplifyLibCalls] Prevent orphaned global string literals (PR #189502)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Mar 30 15:49:39 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Daniel Thornburgh (mysterymath)
<details>
<summary>Changes</summary>
When `printf` is simplified to `puts`, `SimplifyLibCalls` would eagerly create a global string for the argument before checking if `puts` is emittable. If `puts` is not emittable (e.g. because it's an unextracted bitcode libfunc), the optimization aborts, leaving an orphaned global string in the module. Under expensive checks, this triggers a fatal error because the function pass modified the module without reporting it.
This change defers the creation of the global string until after checking if `puts` is emittable.
(This PR was created with the help of Gemini CLI.)
---
Full diff: https://github.com/llvm/llvm-project/pull/189502.diff
1 Files Affected:
- (modified) llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp (+4)
``````````diff
diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index 3b68afe8700dd..80d354ccaa826 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -3378,6 +3378,8 @@ Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilderBase &B) {
}
// printf("%s", str"\n") --> puts(str)
if (OperandStr.back() == '\n') {
+ if (!isLibFuncEmittable(CI->getModule(), TLI, LibFunc_puts))
+ return nullptr;
OperandStr = OperandStr.drop_back();
Value *GV = B.CreateGlobalString(OperandStr, "str");
return copyFlags(*CI, emitPutS(GV, B, TLI));
@@ -3388,6 +3390,8 @@ Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilderBase &B) {
// printf("foo\n") --> puts("foo")
if (FormatStr.back() == '\n' &&
!FormatStr.contains('%')) { // No format characters.
+ if (!isLibFuncEmittable(CI->getModule(), TLI, LibFunc_puts))
+ return nullptr;
// Create a string literal with no \n on it. We expect the constant merge
// pass to be run after this pass, to merge duplicate strings.
FormatStr = FormatStr.drop_back();
``````````
</details>
https://github.com/llvm/llvm-project/pull/189502
More information about the llvm-commits
mailing list