[mlir][AsmPrinter] Gracefully handle empty symbol
authorBruno Schmitt <bruno@oschmitt.com>
Mon, 30 Jan 2023 20:48:07 +0000 (12:48 -0800)
committerMin-Yih Hsu <minyihh@uci.edu>
Mon, 30 Jan 2023 20:48:07 +0000 (12:48 -0800)
The GenericOp printer should support malformed IR without crashing

GitHub issue #59529

Differential Revision: https://reviews.llvm.org/D142818

mlir/lib/IR/AsmPrinter.cpp

index af5cb6f..1ce617c 100644 (file)
@@ -2042,7 +2042,10 @@ static void printKeywordOrString(StringRef keyword, raw_ostream &os) {
 /// represented as a string prefixed with '@'. The reference is surrounded with
 /// ""'s and escaped if it has any special or non-printable characters in it.
 static void printSymbolReference(StringRef symbolRef, raw_ostream &os) {
-  assert(!symbolRef.empty() && "expected valid symbol reference");
+  if (symbolRef.empty()) {
+    os << "@<<INVALID EMPTY SYMBOL>>";
+    return;
+  }
   os << '@';
   printKeywordOrString(symbolRef, os);
 }