Fix segfaults when printing unlinked statements, instructions and blocks. Fancy printing requires a pointer to the function since SSA values get function-specific names. This CL adds checks to ensure that we don't dereference null pointers in unliked objects. Unlinked statements, instructions and blocks are printed as <<UNLINKED STATEMENT>> etc.
PiperOrigin-RevId: 207293992
diff --git a/lib/IR/AsmPrinter.cpp b/lib/IR/AsmPrinter.cpp
index c53c333..d71fb92 100644
--- a/lib/IR/AsmPrinter.cpp
+++ b/lib/IR/AsmPrinter.cpp
@@ -582,6 +582,7 @@
}
void printOperand(const SSAValue *value) { printValueID(value); }
+
void printOptionalAttrDict(ArrayRef<NamedAttribute> attrs,
ArrayRef<const char *> elidedAttrs = {}) override;
@@ -1182,6 +1183,10 @@
void SSAValue::dump() const { print(llvm::errs()); }
void Instruction::print(raw_ostream &os) const {
+ if (!getFunction()) {
+ os << "<<UNLINKED INSTRUCTION>>\n";
+ return;
+ }
ModuleState state(getFunction()->getContext());
ModulePrinter modulePrinter(os, state);
CFGFunctionPrinter(getFunction(), modulePrinter).print(this);
@@ -1193,6 +1198,10 @@
}
void BasicBlock::print(raw_ostream &os) const {
+ if (!getFunction()) {
+ os << "<<UNLINKED BLOCK>>\n";
+ return;
+ }
ModuleState state(getFunction()->getContext());
ModulePrinter modulePrinter(os, state);
CFGFunctionPrinter(getFunction(), modulePrinter).print(this);
@@ -1202,6 +1211,11 @@
void Statement::print(raw_ostream &os) const {
MLFunction *function = findFunction();
+ if (!function) {
+ os << "<<UNLINKED STATEMENT>>\n";
+ return;
+ }
+
ModuleState state(function->getContext());
ModulePrinter modulePrinter(os, state);
MLFunctionPrinter(function, modulePrinter).print(this);