From 9b970eb5daa3d774ae8fdad07491853979f935db Mon Sep 17 00:00:00 2001 From: Vasileios Porpodas Date: Mon, 9 Jan 2023 21:25:18 -0800 Subject: [PATCH] [AsmWriter] Don't crash when printing a null operand bundle. Differential Revision: https://reviews.llvm.org/D141415 --- llvm/lib/IR/AsmWriter.cpp | 10 +++++++--- llvm/unittests/IR/AsmWriterTest.cpp | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp index 234f4d8..6108ce0 100644 --- a/llvm/lib/IR/AsmWriter.cpp +++ b/llvm/lib/IR/AsmWriter.cpp @@ -2765,9 +2765,13 @@ void AssemblyWriter::writeOperandBundles(const CallBase *Call) { Out << ", "; FirstInput = false; - TypePrinter.print(Input->getType(), Out); - Out << " "; - WriteAsOperandInternal(Out, Input, WriterCtx); + if (Input == nullptr) + Out << ""; + else { + TypePrinter.print(Input->getType(), Out); + Out << " "; + WriteAsOperandInternal(Out, Input, WriterCtx); + } } Out << ')'; diff --git a/llvm/unittests/IR/AsmWriterTest.cpp b/llvm/unittests/IR/AsmWriterTest.cpp index 9fc7ce4..a3967f7 100644 --- a/llvm/unittests/IR/AsmWriterTest.cpp +++ b/llvm/unittests/IR/AsmWriterTest.cpp @@ -81,4 +81,27 @@ TEST(AsmWriterTest, PrintAddrspaceWithNullOperand) { std::size_t r = OS.str().find(""); EXPECT_TRUE(r != std::string::npos); } + +TEST(AsmWriterTest, PrintNullOperandBundle) { + LLVMContext C; + Type *Int32Ty = Type::getInt32Ty(C); + FunctionType *FnTy = FunctionType::get(Int32Ty, Int32Ty, /*isVarArg=*/false); + Value *Callee = Constant::getNullValue(FnTy->getPointerTo()); + Value *Args[] = {ConstantInt::get(Int32Ty, 42)}; + std::unique_ptr NormalDest(BasicBlock::Create(C)); + std::unique_ptr UnwindDest(BasicBlock::Create(C)); + OperandBundleDef Bundle("bundle", UndefValue::get(Int32Ty)); + std::unique_ptr Invoke( + InvokeInst::Create(FnTy, Callee, NormalDest.get(), UnwindDest.get(), Args, + Bundle, "result")); + // Makes the operand bundle null. + Invoke->dropAllReferences(); + Invoke->setNormalDest(NormalDest.get()); + Invoke->setUnwindDest(UnwindDest.get()); + + std::string S; + raw_string_ostream OS(S); + Invoke->print(OS); + EXPECT_TRUE(OS.str().find("") != std::string::npos); +} } -- 2.7.4