[llvm] 3531c41 - [AsmWriter] Don't crash when printing addrspace and the operand is null

Vasileios Porpodas via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 10 15:15:28 PST 2023


Author: Vasileios Porpodas
Date: 2023-01-10T15:14:25-08:00
New Revision: 3531c41168e01bf1750b436036c9b9db7e8ee5d5

URL: https://github.com/llvm/llvm-project/commit/3531c41168e01bf1750b436036c9b9db7e8ee5d5
DIFF: https://github.com/llvm/llvm-project/commit/3531c41168e01bf1750b436036c9b9db7e8ee5d5.diff

LOG: [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

Added: 
    

Modified: 
    llvm/lib/IR/AsmWriter.cpp
    llvm/unittests/IR/AsmWriterTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp
index b1ec4848076e1..234f4d88bcee1 100644
--- a/llvm/lib/IR/AsmWriter.cpp
+++ b/llvm/lib/IR/AsmWriter.cpp
@@ -4008,6 +4008,10 @@ void AssemblyWriter::printInfoComment(const Value &V) {
 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 << " <cannot get addrspace!>";
+    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
index c8b5379342de3..9fc7ce410fe09 100644
--- a/llvm/unittests/IR/AsmWriterTest.cpp
+++ b/llvm/unittests/IR/AsmWriterTest.cpp
@@ -62,4 +62,23 @@ TEST(AsmWriterTest, DumpDIExpression) {
             OS.str());
 }
 
+TEST(AsmWriterTest, PrintAddrspaceWithNullOperand) {
+  LLVMContext Ctx;
+  std::unique_ptr<Module> M;
+  SmallVector<Type *, 3> 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<CallInst> 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("<cannot get addrspace!>");
+  EXPECT_TRUE(r != std::string::npos);
+}
 }


        


More information about the llvm-commits mailing list