From 69a47ca153f85af8bfcf53a5d03551e14933a561 Mon Sep 17 00:00:00 2001 From: Antonio Maiorano Date: Mon, 16 Dec 2024 14:10:01 -0500 Subject: [PATCH 1/2] Fix crash when printing instructions that have a metadata attached but no parent. Applying patch from upstream LLVM: https://github.com/llvm/llvm-project/commit/b9b50aaffddf9b3d7b22f42a332811dddb6b9440#diff-69d28d03f6d27b8c800ff1dafb34ede6999a3165c109263e7da48dbbdf964f0aR9 --- lib/IR/AsmWriter.cpp | 2 +- unittests/IR/AsmWriterTest.cpp | 37 ++++++++++++++++++++++++++++++++++ unittests/IR/CMakeLists.txt | 1 + 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 unittests/IR/AsmWriterTest.cpp diff --git a/lib/IR/AsmWriter.cpp b/lib/IR/AsmWriter.cpp index 005907f3b1..109bbc47a0 100644 --- a/lib/IR/AsmWriter.cpp +++ b/lib/IR/AsmWriter.cpp @@ -3049,7 +3049,7 @@ void AssemblyWriter::printMetadataAttachments( return; if (MDNames.empty()) - TheModule->getMDKindNames(MDNames); + MDs[0].second->getContext().getMDKindNames(MDNames); for (const auto &I : MDs) { unsigned Kind = I.first; diff --git a/unittests/IR/AsmWriterTest.cpp b/unittests/IR/AsmWriterTest.cpp new file mode 100644 index 0000000000..c7e7bb5c9f --- /dev/null +++ b/unittests/IR/AsmWriterTest.cpp @@ -0,0 +1,37 @@ +//===- llvm/unittest/IR/AsmWriter.cpp - AsmWriter tests -------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +#include "llvm/IR/IRBuilder.h" +#include "llvm/IR/Function.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/MDBuilder.h" +#include "llvm/IR/Module.h" +#include "gtest/gtest.h" + +using namespace llvm; + +namespace { + +TEST(AsmWriterTest, DebugPrintDetachedInstruction) { + + // PR24852: Ensure that an instruction can be printed even when it + // has metadata attached but no parent. + LLVMContext Ctx; + auto Ty = Type::getInt32Ty(Ctx); + auto Undef = UndefValue::get(Ty); + std::unique_ptr Add(BinaryOperator::CreateAdd(Undef, Undef)); + Add->setMetadata( + "", MDNode::get(Ctx, {ConstantAsMetadata::get(ConstantInt::get(Ty, 1))})); + std::string S; + raw_string_ostream OS(S); + Add->print(OS); + std::size_t r = OS.str().find(" = add i32 undef, undef, ! Date: Mon, 16 Dec 2024 19:18:57 +0000 Subject: [PATCH 2/2] chore: autopublish 2024-12-16T19:18:57Z --- unittests/IR/AsmWriterTest.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/unittests/IR/AsmWriterTest.cpp b/unittests/IR/AsmWriterTest.cpp index c7e7bb5c9f..4cbfce4721 100644 --- a/unittests/IR/AsmWriterTest.cpp +++ b/unittests/IR/AsmWriterTest.cpp @@ -6,8 +6,8 @@ // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// -#include "llvm/IR/IRBuilder.h" #include "llvm/IR/Function.h" +#include "llvm/IR/IRBuilder.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/MDBuilder.h" #include "llvm/IR/Module.h" @@ -34,4 +34,4 @@ TEST(AsmWriterTest, DebugPrintDetachedInstruction) { EXPECT_TRUE(r != std::string::npos); } -} +} // namespace