[llvm] [SimplifyLibCalls] Prevent orphaned global string literals (PR #189502)

Daniel Thornburgh via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 30 15:49:06 PDT 2026


https://github.com/mysterymath created https://github.com/llvm/llvm-project/pull/189502

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.)

>From f1b8313317a435b2769a3e4748d30204ec5f0b47 Mon Sep 17 00:00:00 2001
From: Daniel Thornburgh <dthorn at google.com>
Date: Mon, 30 Mar 2026 15:30:43 -0700
Subject: [PATCH] [SimplifyLibCalls] Prevent orphaned global string literals

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.)
---
 llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp | 4 ++++
 1 file changed, 4 insertions(+)

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();



More information about the llvm-commits mailing list