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

Igor Kudrin via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 18 23:59:08 PDT 2023


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

This fixes a crash when `splitAndWriteThinLTOBitcode()` hits a declaration with type metadata. For example, such declarations can be generated by the `EliminateAvailableExternally` pass.

>From 03a44537b2d0394450c1a5e07e41dc4ff9795bc2 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..033d2d0cbd50b39
--- /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 -passes=elim-avail-extern --thinlto-bc --thinlto-split-lto-unit -o - %s
+
+ at _ZTV3Foo = available_externally constant { [3 x ptr] } zeroinitializer, align 8, !type !0
+
+define void @Bar() {
+  ret void
+}
+
+!0 = !{i64 16, !"_ZTS3Foo"}



More information about the llvm-commits mailing list