[llvm] r229960 - Bitcode: Stop assuming non-null fields

Duncan P. N. Exon Smith dexonsmith at apple.com
Thu Feb 19 19:17:58 PST 2015


Author: dexonsmith
Date: Thu Feb 19 21:17:58 2015
New Revision: 229960

URL: http://llvm.org/viewvc/llvm-project?rev=229960&view=rev
Log:
Bitcode: Stop assuming non-null fields

When writing the bitcode serialization for the new debug info hierarchy,
I assumed two fields would never be null.

Drop that assumption, since it's brittle (and crashes the
`BitcodeWriter` if wrong), and is a check better left for the verifier
anyway.  (No need for a bitcode upgrade here, since the new hierarchy is
still not in place.)

The fields in question are `MDCompileUnit::getFile()` and
`MDDerivedType::getBaseType()`, the latter of which isn't null in
test/Transforms/Mem2Reg/ConvertDebugInfo2.ll (see !14, a pointer to
nothing).  While the testcase might have bitrotted, there's no reason
for the bitcode format to rely on non-null for metadata operands.

This also fixes a bug in `AsmWriter` where if the `file:` is null it
isn't emitted (caught by the double-round trip in the testcase I'm
adding) -- this is a required field in `LLParser`.

I'll circle back to ConvertDebugInfo2.  Once the specialized nodes are
in place, I'll be trying to turn the debug info verifier back on by
default (in the newer module pass form committed r206300) and throwing
more logic in there.  If the testcase has bitrotted (as opposed to me
not understanding the schema correctly) I'll fix it then.

Added:
    llvm/trunk/test/Assembler/metadata-null-operands.ll
Modified:
    llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp
    llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp
    llvm/trunk/lib/IR/AsmWriter.cpp

Modified: llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp?rev=229960&r1=229959&r2=229960&view=diff
==============================================================================
--- llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp (original)
+++ llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp Thu Feb 19 21:17:58 2015
@@ -1407,8 +1407,8 @@ std::error_code BitcodeReader::ParseMeta
           GET_OR_DISTINCT(MDDerivedType, Record[0],
                           (Context, Record[1], getMDString(Record[2]),
                            getMDOrNull(Record[3]), Record[4],
-                           getMDOrNull(Record[5]), getMD(Record[6]), Record[7],
-                           Record[8], Record[9], Record[10],
+                           getMDOrNull(Record[5]), getMDOrNull(Record[6]),
+                           Record[7], Record[8], Record[9], Record[10],
                            getMDOrNull(Record[11]))),
           NextMDValueNo++);
       break;
@@ -1454,13 +1454,14 @@ std::error_code BitcodeReader::ParseMeta
         return Error("Invalid record");
 
       MDValueList.AssignValue(
-          GET_OR_DISTINCT(
-              MDCompileUnit, Record[0],
-              (Context, Record[1], getMD(Record[2]), getMDString(Record[3]),
-               Record[4], getMDString(Record[5]), Record[6],
-               getMDString(Record[7]), Record[8], getMDOrNull(Record[9]),
-               getMDOrNull(Record[10]), getMDOrNull(Record[11]),
-               getMDOrNull(Record[12]), getMDOrNull(Record[13]))),
+          GET_OR_DISTINCT(MDCompileUnit, Record[0],
+                          (Context, Record[1], getMDOrNull(Record[2]),
+                           getMDString(Record[3]), Record[4],
+                           getMDString(Record[5]), Record[6],
+                           getMDString(Record[7]), Record[8],
+                           getMDOrNull(Record[9]), getMDOrNull(Record[10]),
+                           getMDOrNull(Record[11]), getMDOrNull(Record[12]),
+                           getMDOrNull(Record[13]))),
           NextMDValueNo++);
       break;
     }

Modified: llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp?rev=229960&r1=229959&r2=229960&view=diff
==============================================================================
--- llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp (original)
+++ llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp Thu Feb 19 21:17:58 2015
@@ -864,7 +864,7 @@ static void WriteMDDerivedType(const MDD
   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
   Record.push_back(N->getLine());
   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
-  Record.push_back(VE.getMetadataID(N->getBaseType()));
+  Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
   Record.push_back(N->getSizeInBits());
   Record.push_back(N->getAlignInBits());
   Record.push_back(N->getOffsetInBits());
@@ -932,7 +932,7 @@ static void WriteMDCompileUnit(const MDC
                                unsigned Abbrev) {
   Record.push_back(N->isDistinct());
   Record.push_back(N->getSourceLanguage());
-  Record.push_back(VE.getMetadataID(N->getFile()));
+  Record.push_back(VE.getMetadataOrNullID(N->getFile()));
   Record.push_back(VE.getMetadataOrNullID(N->getRawProducer()));
   Record.push_back(N->isOptimized());
   Record.push_back(VE.getMetadataOrNullID(N->getRawFlags()));

Modified: llvm/trunk/lib/IR/AsmWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/AsmWriter.cpp?rev=229960&r1=229959&r2=229960&view=diff
==============================================================================
--- llvm/trunk/lib/IR/AsmWriter.cpp (original)
+++ llvm/trunk/lib/IR/AsmWriter.cpp Thu Feb 19 21:17:58 2015
@@ -1515,11 +1515,8 @@ static void writeMDCompileUnit(raw_ostre
     Out << Lang;
   else
     Out << N->getSourceLanguage();
-  if (N->getFile()) {
-    Out << FS << "file: ";
-    writeMetadataAsOperand(Out, N->getFile(), TypePrinter, Machine,
-                           Context);
-  }
+  Out << FS << "file: ";
+  writeMetadataAsOperand(Out, N->getFile(), TypePrinter, Machine, Context);
   if (!N->getProducer().empty())
     Out << FS << "producer: \"" << N->getProducer() << "\"";
   Out << FS << "isOptimized: " << (N->isOptimized() ? "true" : "false");

Added: llvm/trunk/test/Assembler/metadata-null-operands.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Assembler/metadata-null-operands.ll?rev=229960&view=auto
==============================================================================
--- llvm/trunk/test/Assembler/metadata-null-operands.ll (added)
+++ llvm/trunk/test/Assembler/metadata-null-operands.ll Thu Feb 19 21:17:58 2015
@@ -0,0 +1,13 @@
+; RUN: llvm-as < %s | llvm-dis | llvm-as | llvm-dis | FileCheck %s
+; RUN: verify-uselistorder %s
+
+; Don't crash on null operands.  (If/when we add a verify check for these, we
+; should disable the verifier for this test and remove this comment; the test
+; is still important.)
+!named = !{!0, !1}
+!0 = !MDDerivedType(tag: DW_TAG_pointer_type, baseType: null)
+!1 = !MDCompileUnit(language: DW_LANG_C, file: null)
+
+; CHECK: !named = !{!0, !1}
+; CHECK: !0 = !MDDerivedType({{.*}}baseType: null{{.*}})
+; CHECK: !1 = !MDCompileUnit({{.*}}file: null{{.*}})





More information about the llvm-commits mailing list