[llvm] [ThinLTOBitcodeWriter] Do not crash on a typed declaration (PR #69564)

Igor Kudrin via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 23 14:28:00 PDT 2023


https://github.com/igorkudrin updated https://github.com/llvm/llvm-project/pull/69564

>From 2221e59c3df7c1050bf4cc2c2542de9e5713732c Mon Sep 17 00:00:00 2001
From: Igor Kudrin <ikudrin at accesssoftek.com>
Date: Wed, 18 Oct 2023 23:30:24 -0700
Subject: [PATCH] [ThinLTOBitcodeWriter] Do not crash on a typed declaration

This fixes a crash when `splitAndWriteThinLTOBitcode()` hits a
declaration with type metadata. For example, such declarations can be
generated by the `EliminateAvailableExternally` pass.
---
 llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp     |  2 +-
 .../ThinLTOBitcodeWriter/split-typed-decl.ll         | 12 ++++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/Transforms/ThinLTOBitcodeWriter/split-typed-decl.ll

diff --git a/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp b/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp
index fc1e70b1b3d3d81..98ef2065d6d9492 100644
--- a/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp
+++ b/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp
@@ -329,7 +329,7 @@ void splitAndWriteThinLTOBitcode(
   // comdat in MergedM to keep the comdat together.
   DenseSet<const Comdat *> MergedMComdats;
   for (GlobalVariable &GV : M.globals())
-    if (HasTypeMetadata(&GV)) {
+    if (!GV.isDeclaration() && HasTypeMetadata(&GV)) {
       if (const auto *C = GV.getComdat())
         MergedMComdats.insert(C);
       forEachVirtualFunction(GV.getInitializer(), [&](Function *F) {
diff --git a/llvm/test/Transforms/ThinLTOBitcodeWriter/split-typed-decl.ll b/llvm/test/Transforms/ThinLTOBitcodeWriter/split-typed-decl.ll
new file mode 100644
index 000000000000000..ec96c1632586fbf
--- /dev/null
+++ b/llvm/test/Transforms/ThinLTOBitcodeWriter/split-typed-decl.ll
@@ -0,0 +1,12 @@
+;; Generating bitcode files with split LTO modules should not crash if there are
+;; typed declarations in sources.
+
+; RUN: opt --thinlto-bc --thinlto-split-lto-unit -o - %s
+
+ at _ZTV3Foo = external constant { [3 x ptr] }, !type !0
+
+define void @Bar() {
+  ret void
+}
+
+!0 = !{i64 16, !"_ZTS3Foo"}



More information about the llvm-commits mailing list