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/Instructions.cpp b/lib/IR/Instructions.cpp
index 847907b..8f64066 100644
--- a/lib/IR/Instructions.cpp
+++ b/lib/IR/Instructions.cpp
@@ -71,11 +71,13 @@
/// Return the context this operation is associated with.
MLIRContext *Instruction::getContext() const {
- return getFunction()->getContext();
+ auto *fn = getFunction();
+ return fn ? fn->getContext() : nullptr;
}
CFGFunction *Instruction::getFunction() const {
- return getBlock()->getFunction();
+ auto *block = getBlock();
+ return block ? block->getFunction() : nullptr;
}
unsigned Instruction::getNumOperands() const {