Skip to content

Commit 3531c41

Browse files
committed
[AsmWriter] Don't crash when printing addrspace and the operand is null
This helps print instructions with detached operands in the debugger. Differential Revision: https://reviews.llvm.org/D141343
1 parent 4089c28 commit 3531c41

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

llvm/lib/IR/AsmWriter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4008,6 +4008,10 @@ void AssemblyWriter::printInfoComment(const Value &V) {
40084008
static void maybePrintCallAddrSpace(const Value *Operand, const Instruction *I,
40094009
raw_ostream &Out) {
40104010
// We print the address space of the call if it is non-zero.
4011+
if (Operand == nullptr) {
4012+
Out << " <cannot get addrspace!>";
4013+
return;
4014+
}
40114015
unsigned CallAddrSpace = Operand->getType()->getPointerAddressSpace();
40124016
bool PrintAddrSpace = CallAddrSpace != 0;
40134017
if (!PrintAddrSpace) {

llvm/unittests/IR/AsmWriterTest.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,23 @@ TEST(AsmWriterTest, DumpDIExpression) {
6262
OS.str());
6363
}
6464

65+
TEST(AsmWriterTest, PrintAddrspaceWithNullOperand) {
66+
LLVMContext Ctx;
67+
std::unique_ptr<Module> M;
68+
SmallVector<Type *, 3> FArgTypes;
69+
FArgTypes.push_back(Type::getInt64Ty(Ctx));
70+
FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx), FArgTypes, false);
71+
Function *F = Function::Create(FTy, Function::ExternalLinkage, "", M.get());
72+
Argument *Arg0 = F->getArg(0);
73+
Value *Args[] = {Arg0};
74+
std::unique_ptr<CallInst> Call(CallInst::Create(F, Args));
75+
// This will make Call's operand null.
76+
Call->dropAllReferences();
77+
78+
std::string S;
79+
raw_string_ostream OS(S);
80+
Call->print(OS);
81+
std::size_t r = OS.str().find("<cannot get addrspace!>");
82+
EXPECT_TRUE(r != std::string::npos);
83+
}
6584
}

0 commit comments

Comments
 (0)