| OLD | NEW |
| 1 //==- ThreadedStreamingCache.h - Cache for StreamingMemoryObject -*- C++ -*-==// | 1 //==- ThreadedStreamingCache.h - Cache for StreamingMemoryObject -*- C++ -*-==// |
| 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 #ifndef THREADEDSTREAMINGCACHE_H | 10 #ifndef THREADEDSTREAMINGCACHE_H |
| 11 #define THREADEDSTREAMINGCACHE_H | 11 #define THREADEDSTREAMINGCACHE_H |
| 12 | 12 |
| 13 #include "llvm/Support/ErrorHandling.h" | 13 #include "llvm/Support/ErrorHandling.h" |
| 14 #include "llvm/Support/Mutex.h" | 14 #include "llvm/Support/Mutex.h" |
| 15 #include "llvm/Support/StreamableMemoryObject.h" | 15 #include "llvm/Support/StreamingMemoryObject.h" |
| 16 | 16 |
| 17 namespace llvm { | 17 namespace llvm { |
| 18 | 18 |
| 19 // An implementation of StreamingMemoryObject for use in multithreaded | 19 // An implementation of StreamingMemoryObject for use in multithreaded |
| 20 // translation. Each thread has one of these objects, each of which has a | 20 // translation. Each thread has one of these objects, each of which has a |
| 21 // pointer to a shared StreamingMemoryObject. This object is effectively | 21 // pointer to a shared StreamingMemoryObject. This object is effectively |
| 22 // a thread-local cache for the bitcode streamer to avoid contention, since | 22 // a thread-local cache for the bitcode streamer to avoid contention, since |
| 23 // bits are only read from the bitcode stream one word at a time. | 23 // bits are only read from the bitcode stream one word at a time. |
| 24 | 24 |
| 25 class ThreadedStreamingCache : public llvm::StreamingMemoryObject { | 25 class ThreadedStreamingCache : public llvm::StreamingMemoryObject { |
| 26 public: | 26 public: |
| 27 explicit ThreadedStreamingCache(llvm::StreamingMemoryObject *S); | 27 explicit ThreadedStreamingCache(llvm::StreamingMemoryObject *S); |
| 28 uint64_t getBase() const override { return 0; } | |
| 29 uint64_t getExtent() const override; | 28 uint64_t getExtent() const override; |
| 30 int readByte(uint64_t address, uint8_t* ptr) const override; | 29 uint64_t readBytes(uint8_t *Buf, uint64_t Size, |
| 31 int readBytes(uint64_t address, uint64_t size, | 30 uint64_t Address) const override; |
| 32 uint8_t *buf) const override; | 31 const uint8_t *getPointer(uint64_t Address, |
| 33 const uint8_t *getPointer(uint64_t address, | 32 uint64_t Size) const override { |
| 34 uint64_t size) const override { | |
| 35 // This could be fixed by ensuring the bytes are fetched and making a copy, | 33 // This could be fixed by ensuring the bytes are fetched and making a copy, |
| 36 // requiring that the bitcode size be known, or otherwise ensuring that | 34 // requiring that the bitcode size be known, or otherwise ensuring that |
| 37 // the memory doesn't go away/get reallocated, but it's | 35 // the memory doesn't go away/get reallocated, but it's |
| 38 // not currently necessary. Users that need the pointer don't stream. | 36 // not currently necessary. Users that need the pointer don't stream. |
| 39 llvm_unreachable("getPointer in streaming memory objects not allowed"); | 37 llvm_unreachable("getPointer in streaming memory objects not allowed"); |
| 40 return NULL; | 38 return NULL; |
| 41 } | 39 } |
| 42 bool isValidAddress(uint64_t address) const override; | 40 bool isValidAddress(uint64_t Address) const override; |
| 43 bool isObjectEnd(uint64_t address) const override; | |
| 44 | 41 |
| 45 /// Drop s bytes from the front of the stream, pushing the positions of the | 42 /// Drop s bytes from the front of the stream, pushing the positions of the |
| 46 /// remaining bytes down by s. This is used to skip past the bitcode header, | 43 /// remaining bytes down by s. This is used to skip past the bitcode header, |
| 47 /// since we don't know a priori if it's present, and we can't put bytes | 44 /// since we don't know a priori if it's present, and we can't put bytes |
| 48 /// back into the stream once we've read them. | 45 /// back into the stream once we've read them. |
| 49 bool dropLeadingBytes(size_t s) override; | 46 bool dropLeadingBytes(size_t S) override; |
| 50 | 47 |
| 51 /// If the data object size is known in advance, many of the operations can | 48 /// If the data object size is known in advance, many of the operations can |
| 52 /// be made more efficient, so this method should be called before reading | 49 /// be made more efficient, so this method should be called before reading |
| 53 /// starts (although it can be called anytime). | 50 /// starts (although it can be called anytime). |
| 54 void setKnownObjectSize(size_t size) override; | 51 void setKnownObjectSize(size_t Size) override; |
| 55 private: | 52 private: |
| 56 const static uint64_t kCacheSize = 4 * 4096; | 53 const static uint64_t kCacheSize = 4 * 4096; |
| 57 const static uint64_t kCacheSizeMask = ~(kCacheSize - 1); | 54 const static uint64_t kCacheSizeMask = ~(kCacheSize - 1); |
| 58 static llvm::sys::SmartMutex<false> StreamerLock; | 55 static llvm::sys::SmartMutex<false> StreamerLock; |
| 59 | 56 |
| 60 int fetchCacheLine(uint64_t address) const; | 57 // Fetch up to kCacheSize worth of data starting from Address, into the |
| 58 // CacheBase, and set MinObjectSize to the new known edge. |
| 59 // If at EOF, MinObjectSize reflects the final size. |
| 60 void fetchCacheLine(uint64_t Address) const; |
| 61 | 61 |
| 62 llvm::StreamingMemoryObject *Streamer; | 62 llvm::StreamingMemoryObject *Streamer; |
| 63 // Cached data for addresses [CacheBase, CacheBase + kCacheSize) | 63 // Cached data for addresses [CacheBase, CacheBase + kCacheSize) |
| 64 mutable std::vector<unsigned char> Cache; | 64 mutable std::vector<unsigned char> Cache; |
| 65 // The MemoryObject is at least this size. Used as a cache for isObjectEnd and | 65 // The MemoryObject is at least this size. Used as a cache for isValidAddress. |
| 66 // isValidAddress | |
| 67 mutable uint64_t MinObjectSize; | 66 mutable uint64_t MinObjectSize; |
| 68 // Current base address for the cache. | 67 // Current base address for the cache. |
| 69 mutable uint64_t CacheBase; | 68 mutable uint64_t CacheBase; |
| 70 | 69 |
| 71 ThreadedStreamingCache( | 70 ThreadedStreamingCache( |
| 72 const ThreadedStreamingCache&) LLVM_DELETED_FUNCTION; | 71 const ThreadedStreamingCache&) LLVM_DELETED_FUNCTION; |
| 73 void operator=(const ThreadedStreamingCache&) LLVM_DELETED_FUNCTION; | 72 void operator=(const ThreadedStreamingCache&) LLVM_DELETED_FUNCTION; |
| 74 }; | 73 }; |
| 75 | 74 |
| 76 } // namespace llvm | 75 } // namespace llvm |
| 77 | 76 |
| 78 #endif // THREADEDSTREAMINGCACHE_H | 77 #endif // THREADEDSTREAMINGCACHE_H |
| OLD | NEW |