diff --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp --- a/llvm/lib/IR/AsmWriter.cpp +++ b/llvm/lib/IR/AsmWriter.cpp @@ -4008,6 +4008,10 @@ static void maybePrintCallAddrSpace(const Value *Operand, const Instruction *I, raw_ostream &Out) { // We print the address space of the call if it is non-zero. + if (Operand == nullptr) { + Out << " "; + return; + } unsigned CallAddrSpace = Operand->getType()->getPointerAddressSpace(); bool PrintAddrSpace = CallAddrSpace != 0; if (!PrintAddrSpace) { diff --git a/llvm/unittests/IR/AsmWriterTest.cpp b/llvm/unittests/IR/AsmWriterTest.cpp --- a/llvm/unittests/IR/AsmWriterTest.cpp +++ b/llvm/unittests/IR/AsmWriterTest.cpp @@ -62,4 +62,23 @@ OS.str()); } +TEST(AsmWriterTest, PrintAddrspaceWithNullOperand) { + LLVMContext Ctx; + std::unique_ptr M; + SmallVector FArgTypes; + FArgTypes.push_back(Type::getInt64Ty(Ctx)); + FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx), FArgTypes, false); + Function *F = Function::Create(FTy, Function::ExternalLinkage, "", M.get()); + Argument *Arg0 = F->getArg(0); + Value *Args[] = {Arg0}; + std::unique_ptr Call(CallInst::Create(F, Args)); + // This will make Call's operand null. + Call->dropAllReferences(); + + std::string S; + raw_string_ostream OS(S); + Call->print(OS); + std::size_t r = OS.str().find(""); + EXPECT_TRUE(r != std::string::npos); +} }