Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(324)

Side by Side Diff: lib/Bitcode/Reader/BitcodeReader.cpp

Issue 8437024: Bitcode parsing modifications for streaming (Closed)
Patch Set: Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 //===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===// 1 //===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===//
2 // 2 //
3 // The LLVM Compiler Infrastructure 3 // The LLVM Compiler Infrastructure
4 // 4 //
5 // This file is distributed under the University of Illinois Open Source 5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 // 9 //
10 // This header defines the BitcodeReader class. 10 // This header defines the BitcodeReader class.
(...skipping 1510 matching lines...) Expand 10 before | Expand all | Expand 10 after
1521 /// RememberAndSkipFunctionBody - When we see the block for a function body, 1521 /// RememberAndSkipFunctionBody - When we see the block for a function body,
1522 /// remember where it is and then skip it. This lets us lazily deserialize the 1522 /// remember where it is and then skip it. This lets us lazily deserialize the
1523 /// functions. 1523 /// functions.
1524 bool BitcodeReader::RememberAndSkipFunctionBody() { 1524 bool BitcodeReader::RememberAndSkipFunctionBody() {
1525 // Get the function we are talking about. 1525 // Get the function we are talking about.
1526 if (FunctionsWithBodies.empty()) 1526 if (FunctionsWithBodies.empty())
1527 return Error("Insufficient function protos"); 1527 return Error("Insufficient function protos");
1528 1528
1529 Function *Fn = FunctionsWithBodies.back(); 1529 Function *Fn = FunctionsWithBodies.back();
1530 FunctionsWithBodies.pop_back(); 1530 FunctionsWithBodies.pop_back();
1531 fprintf(stderr, "skipping %p\n", Fn);
1531 1532
1532 // Save the current stream state. 1533 // Save the current stream state.
1533 uint64_t CurBit = Stream.GetCurrentBitNo(); 1534 uint64_t CurBit = Stream.GetCurrentBitNo();
1534 DeferredFunctionInfo[Fn] = CurBit; 1535 DeferredFunctionInfo[Fn] = CurBit;
1535 1536
1536 // Skip over the function block for now. 1537 // Skip over the function block for now.
1537 if (Stream.SkipBlock()) 1538 if (Stream.SkipBlock())
1538 return Error("Malformed block record"); 1539 return Error("Malformed block record");
1539 return false; 1540 return false;
1540 } 1541 }
1541 1542
1542 bool BitcodeReader::ParseModule() { 1543 bool BitcodeReader::SuspendModuleParse() {
1543 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 1544 // save our current position
1545 NextUnreadBit = Stream.GetCurrentBitNo();
1546 // Patch the initializers for globals and aliases up.
1547 // TODO(dschuff) this probably only needs to be done once as well
1548 ResolveGlobalAndAliasInits();
1549 if (!GlobalInits.empty() || !AliasInits.empty())
1550 return Error("Malformed global initializer set");
1551
1552 // Look for intrinsic functions which need to be upgraded at some point
1553 // TODO(dschuff): do these intrinsics have bodies? if not, this can be
1554 // done only once, before we start dealing with function bodies. if so,
1555 // this can probably still be done incrementally instead of this copy/paste
1556 // hack
1557 for (Module::iterator FI = TheModule->begin(), FE = TheModule->end();
1558 FI != FE; ++FI) {
1559 Function* NewFn;
1560 if (UpgradeIntrinsicFunction(FI, NewFn))
1561 UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn));
1562 }
1563
1564 // TODO(dschuff) this probably only needs to be done once (currently it's a
jasonwkim 2011/11/03 17:54:09 I think upstream guys dont like TODO(username) -
(google.com) Derek Schuff 2011/11/03 18:01:27 These will go away before I upstream it. On 2011/
1565 // noop anyway). find the right place to put it
1566 // Look for global variables which need to be renamed.
1567 for (Module::global_iterator
1568 GI = TheModule->global_begin(), GE = TheModule->global_end();
1569 GI != GE; ++GI)
1570 UpgradeGlobalVariable(GI);
1571 // Force deallocation of memory for these vectors to favor the client that
1572 // want lazy deserialization.
1573 std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits);
1574 std::vector<std::pair<GlobalAlias*, unsigned> >().swap(AliasInits);
1575 return false;
1576 }
1577
1578 bool BitcodeReader::ParseModule(bool Resume) {
1579 if (Resume)
1580 Stream.JumpToBit(NextUnreadBit);
1581 else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
1544 return Error("Malformed block record"); 1582 return Error("Malformed block record");
1545 fprintf(stderr, "module at byte %d\n", Stream.GetCurrentBitNo()*8); 1583 fprintf(stderr, "module at byte %lu\n", Stream.GetCurrentBitNo()/8);
1546 SmallVector<uint64_t, 64> Record; 1584 SmallVector<uint64_t, 64> Record;
1547 std::vector<std::string> SectionTable; 1585 std::vector<std::string> SectionTable;
1548 std::vector<std::string> GCTable; 1586 std::vector<std::string> GCTable;
1549 1587
1550 // Read all the records for this module. 1588 // Read all the records for this module.
1551 while (!Stream.AtEndOfStream()) { 1589 while (!Stream.AtEndOfStream()) {
1552 unsigned Code = Stream.ReadCode(); 1590 unsigned Code = Stream.ReadCode();
1553 if (Code == bitc::END_BLOCK) { 1591 if (Code == bitc::END_BLOCK) {
1554 if (Stream.ReadBlockEnd()) 1592 if (Stream.ReadBlockEnd())
1555 return Error("Error at end of module block"); 1593 return Error("Error at end of module block");
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
1624 case bitc::FUNCTION_BLOCK_ID: 1662 case bitc::FUNCTION_BLOCK_ID:
1625 // If this is the first function body we've seen, reverse the 1663 // If this is the first function body we've seen, reverse the
1626 // FunctionsWithBodies list. 1664 // FunctionsWithBodies list.
1627 if (!HasReversedFunctionsWithBodies) { 1665 if (!HasReversedFunctionsWithBodies) {
1628 std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end()); 1666 std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end());
1629 HasReversedFunctionsWithBodies = true; 1667 HasReversedFunctionsWithBodies = true;
1630 } 1668 }
1631 1669
1632 if (RememberAndSkipFunctionBody()) 1670 if (RememberAndSkipFunctionBody())
1633 return true; 1671 return true;
1672 if (LazyStreamer) return SuspendModuleParse();
1634 break; 1673 break;
1635 } 1674 }
1636 continue; 1675 continue;
1637 } 1676 }
1638 1677
1639 if (Code == bitc::DEFINE_ABBREV) { 1678 if (Code == bitc::DEFINE_ABBREV) {
1640 Stream.ReadAbbrevRecord(); 1679 Stream.ReadAbbrevRecord();
1641 continue; 1680 continue;
1642 } 1681 }
1643 1682
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
1768 FunctionType *FTy = 1807 FunctionType *FTy =
1769 dyn_cast<FunctionType>(cast<PointerType>(Ty)->getElementType()); 1808 dyn_cast<FunctionType>(cast<PointerType>(Ty)->getElementType());
1770 if (!FTy) 1809 if (!FTy)
1771 return Error("Function not a pointer to function type!"); 1810 return Error("Function not a pointer to function type!");
1772 1811
1773 Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage, 1812 Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage,
1774 "", TheModule); 1813 "", TheModule);
1775 1814
1776 Func->setCallingConv(static_cast<CallingConv::ID>(Record[1])); 1815 Func->setCallingConv(static_cast<CallingConv::ID>(Record[1]));
1777 bool isProto = Record[2]; 1816 bool isProto = Record[2];
1817 fprintf(stderr, "func record %p isProto %d\n", Func, isProto);
1778 Func->setLinkage(GetDecodedLinkage(Record[3])); 1818 Func->setLinkage(GetDecodedLinkage(Record[3]));
1779 Func->setAttributes(getAttributes(Record[4])); 1819 Func->setAttributes(getAttributes(Record[4]));
1780 1820
1781 Func->setAlignment((1 << Record[5]) >> 1); 1821 Func->setAlignment((1 << Record[5]) >> 1);
1782 if (Record[6]) { 1822 if (Record[6]) {
1783 if (Record[6]-1 >= SectionTable.size()) 1823 if (Record[6]-1 >= SectionTable.size())
1784 return Error("Invalid section ID"); 1824 return Error("Invalid section ID");
1785 Func->setSection(SectionTable[Record[6]-1]); 1825 Func->setSection(SectionTable[Record[6]-1]);
1786 } 1826 }
1787 Func->setVisibility(GetDecodedVisibility(Record[7])); 1827 Func->setVisibility(GetDecodedVisibility(Record[7]));
1788 if (Record.size() > 8 && Record[8]) { 1828 if (Record.size() > 8 && Record[8]) {
1789 if (Record[8]-1 > GCTable.size()) 1829 if (Record[8]-1 > GCTable.size())
1790 return Error("Invalid GC ID"); 1830 return Error("Invalid GC ID");
1791 Func->setGC(GCTable[Record[8]-1].c_str()); 1831 Func->setGC(GCTable[Record[8]-1].c_str());
1792 } 1832 }
1793 bool UnnamedAddr = false; 1833 bool UnnamedAddr = false;
1794 if (Record.size() > 9) 1834 if (Record.size() > 9)
1795 UnnamedAddr = Record[9]; 1835 UnnamedAddr = Record[9];
1796 Func->setUnnamedAddr(UnnamedAddr); 1836 Func->setUnnamedAddr(UnnamedAddr);
1797 ValueList.push_back(Func); 1837 ValueList.push_back(Func);
1798 1838
1799 // If this is a function with a body, remember the prototype we are 1839 // If this is a function with a body, remember the prototype we are
1800 // creating now, so that we can match up the body with them later. 1840 // creating now, so that we can match up the body with them later.
1801 if (!isProto) 1841 if (!isProto) {
1802 FunctionsWithBodies.push_back(Func); 1842 FunctionsWithBodies.push_back(Func);
1843 if (LazyStreamer) DeferredFunctionInfo[Func] = 0;
1844 }
1803 break; 1845 break;
1804 } 1846 }
1805 // ALIAS: [alias type, aliasee val#, linkage] 1847 // ALIAS: [alias type, aliasee val#, linkage]
1806 // ALIAS: [alias type, aliasee val#, linkage, visibility] 1848 // ALIAS: [alias type, aliasee val#, linkage, visibility]
1807 case bitc::MODULE_CODE_ALIAS: { 1849 case bitc::MODULE_CODE_ALIAS: {
1808 if (Record.size() < 3) 1850 if (Record.size() < 3)
1809 return Error("Invalid MODULE_ALIAS record"); 1851 return Error("Invalid MODULE_ALIAS record");
1810 Type *Ty = getTypeByID(Record[0]); 1852 Type *Ty = getTypeByID(Record[0]);
1811 if (!Ty) return Error("Invalid MODULE_ALIAS record"); 1853 if (!Ty) return Error("Invalid MODULE_ALIAS record");
1812 if (!Ty->isPointerTy()) 1854 if (!Ty->isPointerTy())
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
1873 switch (BlockID) { 1915 switch (BlockID) {
1874 case bitc::BLOCKINFO_BLOCK_ID: 1916 case bitc::BLOCKINFO_BLOCK_ID:
1875 if (Stream.ReadBlockInfoBlock()) 1917 if (Stream.ReadBlockInfoBlock())
1876 return Error("Malformed BlockInfoBlock"); 1918 return Error("Malformed BlockInfoBlock");
1877 break; 1919 break;
1878 case bitc::MODULE_BLOCK_ID: 1920 case bitc::MODULE_BLOCK_ID:
1879 // Reject multiple MODULE_BLOCK's in a single bitstream. 1921 // Reject multiple MODULE_BLOCK's in a single bitstream.
1880 if (TheModule) 1922 if (TheModule)
1881 return Error("Multiple MODULE_BLOCKs in same stream"); 1923 return Error("Multiple MODULE_BLOCKs in same stream");
1882 TheModule = M; 1924 TheModule = M;
1883 if (ParseModule()) 1925 if (ParseModule(false))
1884 return true; 1926 return true;
1927 if (LazyStreamer) return false;
1885 break; 1928 break;
1886 default: 1929 default:
1887 if (Stream.SkipBlock()) 1930 if (Stream.SkipBlock())
1888 return Error("Malformed block record"); 1931 return Error("Malformed block record");
1889 break; 1932 break;
1890 } 1933 }
1891 } 1934 }
1892 1935
1893 return false; 1936 return false;
1894 } 1937 }
(...skipping 941 matching lines...) Expand 10 before | Expand all | Expand 10 after
2836 2879
2837 2880
2838 bool BitcodeReader::isMaterializable(const GlobalValue *GV) const { 2881 bool BitcodeReader::isMaterializable(const GlobalValue *GV) const {
2839 if (const Function *F = dyn_cast<Function>(GV)) { 2882 if (const Function *F = dyn_cast<Function>(GV)) {
2840 return F->isDeclaration() && 2883 return F->isDeclaration() &&
2841 DeferredFunctionInfo.count(const_cast<Function*>(F)); 2884 DeferredFunctionInfo.count(const_cast<Function*>(F));
2842 } 2885 }
2843 return false; 2886 return false;
2844 } 2887 }
2845 2888
2889 bool BitcodeReader::FindFunctionInStream(Function *F,
2890 DenseMap<Function*, uint64_t>::iterator DFII) {
2891 //Stream.JumpToBit(NextUnreadBit);
2892 while (DFII->second == 0) {
2893 if (Stream.AtEndOfStream())
2894 return Error("Could not find Function in stream");
2895 if(ParseModule(true)) return true;
2896 }
2897 return false;
2898 }
2899
2846 bool BitcodeReader::Materialize(GlobalValue *GV, std::string *ErrInfo) { 2900 bool BitcodeReader::Materialize(GlobalValue *GV, std::string *ErrInfo) {
2847 Function *F = dyn_cast<Function>(GV); 2901 Function *F = dyn_cast<Function>(GV);
2902 fprintf(stderr, "Materialize %p (%s)\n", F, F->getName().str().c_str());
2848 // If it's not a function or is already material, ignore the request. 2903 // If it's not a function or is already material, ignore the request.
2849 if (!F || !F->isMaterializable()) return false; 2904 if (!F || !F->isMaterializable()) return false;
2850 2905
2851 DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F); 2906 DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F);
2852 assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!"); 2907 assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
2908 if(DFII->second == 0) {
2909 if (LazyStreamer && FindFunctionInStream(F, DFII)) return true;
2910 }
2853 2911
2854 // Move the bit stream to the saved position of the deferred function body. 2912 // Move the bit stream to the saved position of the deferred function body.
2855 Stream.JumpToBit(DFII->second); 2913 Stream.JumpToBit(DFII->second);
2856 fprintf(stderr, "Materialize %s\n", F->getName().str().c_str()); 2914
2857 if (ParseFunctionBody(F)) { 2915 if (ParseFunctionBody(F)) {
2858 if (ErrInfo) *ErrInfo = ErrorString; 2916 if (ErrInfo) *ErrInfo = ErrorString;
2859 return true; 2917 return true;
2860 } 2918 }
2861 2919
2862 // Upgrade any old intrinsic calls in the function. 2920 // Upgrade any old intrinsic calls in the function.
2863 for (UpgradedIntrinsicMap::iterator I = UpgradedIntrinsics.begin(), 2921 for (UpgradedIntrinsicMap::iterator I = UpgradedIntrinsics.begin(),
2864 E = UpgradedIntrinsics.end(); I != E; ++I) { 2922 E = UpgradedIntrinsics.end(); I != E; ++I) {
2865 if (I->first != I->second) { 2923 if (I->first != I->second) {
2866 for (Value::use_iterator UI = I->first->use_begin(), 2924 for (Value::use_iterator UI = I->first->use_begin(),
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
3052 R->setBufferOwned(false); 3110 R->setBufferOwned(false);
3053 3111
3054 std::string Triple(""); 3112 std::string Triple("");
3055 if (R->ParseTriple(Triple)) 3113 if (R->ParseTriple(Triple))
3056 if (ErrMsg) 3114 if (ErrMsg)
3057 *ErrMsg = R->getErrorString(); 3115 *ErrMsg = R->getErrorString();
3058 3116
3059 delete R; 3117 delete R;
3060 return Triple; 3118 return Triple;
3061 } 3119 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698