llvm.org GIT mirror llvm / 3471174
Provide a fast "get me the target triple from the module" API. This can drastically reduce the linking time during LTO. Patch by Shantonu Sen! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@115728 91177308-0d34-0410-b5e6-96231b3b80d8 Bill Wendling 10 years ago
4 changed file(s) with 143 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
3131 Module *getLazyBitcodeModule(MemoryBuffer *Buffer,
3232 LLVMContext& Context,
3333 std::string *ErrMsg = 0);
34
35 /// getBitcodeTargetTriple - Read the header of the specified bitcode
36 /// buffer and extract just the triple information. If successful,
37 /// this returns a string and *does not* take ownership
38 /// of 'buffer'. On error, this returns "", and fills in *ErrMsg
39 /// if ErrMsg is non-null.
40 std::string getBitcodeTargetTriple(MemoryBuffer *Buffer,
41 LLVMContext& Context,
42 std::string *ErrMsg = 0);
3443
3544 /// ParseBitcodeFile - Read the specified bitcode file, returning the module.
3645 /// If an error occurs, this returns null and fills in *ErrMsg if it is
15991599 return false;
16001600 }
16011601
1602 bool BitcodeReader::ParseModuleTriple(std::string &Triple) {
1603 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
1604 return Error("Malformed block record");
1605
1606 SmallVector Record;
1607
1608 // Read all the records for this module.
1609 while (!Stream.AtEndOfStream()) {
1610 unsigned Code = Stream.ReadCode();
1611 if (Code == bitc::END_BLOCK) {
1612 if (Stream.ReadBlockEnd())
1613 return Error("Error at end of module block");
1614
1615 return false;
1616 }
1617
1618 if (Code == bitc::ENTER_SUBBLOCK) {
1619 switch (Stream.ReadSubBlockID()) {
1620 default: // Skip unknown content.
1621 if (Stream.SkipBlock())
1622 return Error("Malformed block record");
1623 break;
1624 }
1625 continue;
1626 }
1627
1628 if (Code == bitc::DEFINE_ABBREV) {
1629 Stream.ReadAbbrevRecord();
1630 continue;
1631 }
1632
1633 // Read a record.
1634 switch (Stream.ReadRecord(Code, Record)) {
1635 default: break; // Default behavior, ignore unknown content.
1636 case bitc::MODULE_CODE_VERSION: // VERSION: [version#]
1637 if (Record.size() < 1)
1638 return Error("Malformed MODULE_CODE_VERSION");
1639 // Only version #0 is supported so far.
1640 if (Record[0] != 0)
1641 return Error("Unknown bitstream version!");
1642 break;
1643 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
1644 std::string S;
1645 if (ConvertToString(Record, 0, S))
1646 return Error("Invalid MODULE_CODE_TRIPLE record");
1647 Triple = S;
1648 break;
1649 }
1650 }
1651 Record.clear();
1652 }
1653
1654 return Error("Premature end of bitstream");
1655 }
1656
1657 bool BitcodeReader::ParseTriple(std::string &Triple) {
1658 if (Buffer->getBufferSize() & 3)
1659 return Error("Bitcode stream should be a multiple of 4 bytes in length");
1660
1661 unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart();
1662 unsigned char *BufEnd = BufPtr+Buffer->getBufferSize();
1663
1664 // If we have a wrapper header, parse it and ignore the non-bc file contents.
1665 // The magic number is 0x0B17C0DE stored in little endian.
1666 if (isBitcodeWrapper(BufPtr, BufEnd))
1667 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd))
1668 return Error("Invalid bitcode wrapper header");
1669
1670 StreamFile.init(BufPtr, BufEnd);
1671 Stream.init(StreamFile);
1672
1673 // Sniff for the signature.
1674 if (Stream.Read(8) != 'B' ||
1675 Stream.Read(8) != 'C' ||
1676 Stream.Read(4) != 0x0 ||
1677 Stream.Read(4) != 0xC ||
1678 Stream.Read(4) != 0xE ||
1679 Stream.Read(4) != 0xD)
1680 return Error("Invalid bitcode signature");
1681
1682 // We expect a number of well-defined blocks, though we don't necessarily
1683 // need to understand them all.
1684 while (!Stream.AtEndOfStream()) {
1685 unsigned Code = Stream.ReadCode();
1686
1687 if (Code != bitc::ENTER_SUBBLOCK)
1688 return Error("Invalid record at top-level");
1689
1690 unsigned BlockID = Stream.ReadSubBlockID();
1691
1692 // We only know the MODULE subblock ID.
1693 switch (BlockID) {
1694 case bitc::MODULE_BLOCK_ID:
1695 if (ParseModuleTriple(Triple))
1696 return true;
1697 break;
1698 default:
1699 if (Stream.SkipBlock())
1700 return Error("Malformed block record");
1701 break;
1702 }
1703 }
1704
1705 return false;
1706 }
1707
16021708 /// ParseMetadataAttachment - Parse metadata attachments.
16031709 bool BitcodeReader::ParseMetadataAttachment() {
16041710 if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID))
25362642 // Read in the entire module, and destroy the BitcodeReader.
25372643 if (M->MaterializeAllPermanently(ErrMsg)) {
25382644 delete M;
2539 return NULL;
2540 }
2645 return 0;
2646 }
2647
25412648 return M;
25422649 }
2650
2651 std::string llvm::getBitcodeTargetTriple(MemoryBuffer *Buffer,
2652 LLVMContext& Context,
2653 std::string *ErrMsg) {
2654 BitcodeReader *R = new BitcodeReader(Buffer, Context);
2655 // Don't let the BitcodeReader dtor delete 'Buffer'.
2656 R->setBufferOwned(false);
2657
2658 std::string Triple("");
2659 if (R->ParseTriple(Triple))
2660 if (ErrMsg)
2661 *ErrMsg = R->getErrorString();
2662
2663 delete R;
2664 return Triple;
2665 }
211211 /// @brief Main interface to parsing a bitcode buffer.
212212 /// @returns true if an error occurred.
213213 bool ParseBitcodeInto(Module *M);
214
215 /// @brief Cheap mechanism to just extract module triple
216 /// @returns true if an error occurred.
217 bool ParseTriple(std::string &Triple);
214218 private:
215219 const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
216220 Value *getFnValueByID(unsigned ID, const Type *Ty) {
269273 bool ResolveGlobalAndAliasInits();
270274 bool ParseMetadata();
271275 bool ParseMetadataAttachment();
276 bool ParseModuleTriple(std::string &Triple);
272277 };
273278
274279 } // End llvm namespace
6363
6464 // Takes ownership of buffer.
6565 bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) {
66 OwningPtr m(getLazyBitcodeModule(buffer, getGlobalContext()));
67 // On success, m owns buffer and both are deleted at end of this method.
68 if (!m) {
69 delete buffer;
70 return false;
71 }
72 std::string actualTarget = m->getTargetTriple();
73 return (strncmp(actualTarget.c_str(), triplePrefix,
74 strlen(triplePrefix)) == 0);
66 std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext());
67 delete buffer;
68 return (strncmp(Triple.c_str(), triplePrefix,
69 strlen(triplePrefix)) == 0);
7570 }
7671
7772