[clang] 4a44f03 - [Clang][LTO] Fix use of funified-lto and save-temps flags together (#162763)

via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 15 13:46:43 PDT 2025


Author: Alexey Bader
Date: 2025-10-15T13:46:39-07:00
New Revision: 4a44f03271246d64bee4edd8bcaf47f06db76475

URL: https://github.com/llvm/llvm-project/commit/4a44f03271246d64bee4edd8bcaf47f06db76475
DIFF: https://github.com/llvm/llvm-project/commit/4a44f03271246d64bee4edd8bcaf47f06db76475.diff

LOG: [Clang][LTO] Fix use of funified-lto and save-temps flags together (#162763)

> clang -flto -funified-lto -save-temps test.c

Fails with the following error message:

```bash
module flag identifiers must be unique (or of 'require' type)
!"UnifiedLTO"
fatal error: error in backend: Broken module found, compilation aborted!
clang: error: clang frontend command failed with exit code 70 (use -v to see invocation)
```

Here is what the driver does when `-save-temps` flag is set:

> clang -flto -funified-lto -save-temps test.c -ccc-print-phases
>          +- 0: input, "test.c", c
>       +- 1: preprocessor, {0}, cpp-output
>    +- 2: compiler, {1}, ir
> +- 3: backend, {2}, lto-bc
> 4: linker, {3}, image

The IR output of "compiler" step has "UnifiedLTO" module flag. "backend"
step adds another module flag with "UnifiedLTO" identifier, which
invalidates the LLVM IR module.

Added: 
    clang/test/CodeGen/unified-lto-module-flag.ll

Modified: 
    clang/lib/CodeGen/BackendUtil.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp
index f8e8086afc36f..602068436101b 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -1200,7 +1200,8 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
       }
     }
 
-    if (shouldEmitUnifiedLTOModueFlag())
+    if (shouldEmitUnifiedLTOModueFlag() &&
+        !TheModule->getModuleFlag("UnifiedLTO"))
       TheModule->addModuleFlag(llvm::Module::Error, "UnifiedLTO", uint32_t(1));
   }
 

diff  --git a/clang/test/CodeGen/unified-lto-module-flag.ll b/clang/test/CodeGen/unified-lto-module-flag.ll
new file mode 100644
index 0000000000000..deefe826d1566
--- /dev/null
+++ b/clang/test/CodeGen/unified-lto-module-flag.ll
@@ -0,0 +1,11 @@
+; Test that we do not duplicate the UnifiedLTO module flag.
+;
+; RUN: %clang_cc1 -emit-llvm -flto=full -funified-lto -o - %s | FileCheck %s
+
+; CHECK: !llvm.module.flags = !{!0, !1, !2, !3}
+!llvm.module.flags = !{!0, !1, !2, !3}
+
+!0 = !{i32 1, !"wchar_size", i32 2}
+!1 = !{i32 7, !"frame-pointer", i32 2}
+!2 = !{i32 1, !"EnableSplitLTOUnit", i32 1}
+!3 = !{i32 1, !"UnifiedLTO", i32 1}


        


More information about the cfe-commits mailing list