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

Side by Side Diff: tools/pnacl-llc/ThreadedStreamingCache.cpp

Issue 940243003: PNaCl localmod mods in LLVM to 223109 (local files only) (Closed)
Patch Set: Created 5 years, 10 months 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 //=- ThreadedStreamingCache.cpp - Cache for StreamingMemoryObject -*- C++ -*-=// 1 //=- ThreadedStreamingCache.cpp - 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 #include "ThreadedStreamingCache.h" 10 #include "ThreadedStreamingCache.h"
11 #include "llvm/Support/Compiler.h" 11 #include "llvm/Support/Compiler.h"
12 #include "llvm/Support/Mutex.h" 12 #include "llvm/Support/Mutex.h"
13 #include <cstring> 13 #include <cstring>
14 14
15 using namespace llvm; 15 using namespace llvm;
16 using llvm::sys::ScopedLock; 16 using llvm::sys::ScopedLock;
17 17
18 ThreadedStreamingCache::ThreadedStreamingCache( 18 ThreadedStreamingCache::ThreadedStreamingCache(
19 llvm::StreamingMemoryObject *S) : Streamer(S), 19 llvm::StreamingMemoryObject *S) : Streamer(S),
20 Cache(kCacheSize), 20 Cache(kCacheSize),
21 MinObjectSize(0), 21 MinObjectSize(0),
22 CacheBase(-1) { 22 CacheBase(-1) {
23 static_assert((kCacheSize & (kCacheSize - 1)) == 0, 23 static_assert((kCacheSize & (kCacheSize - 1)) == 0,
24 "kCacheSize must be a power of 2"); 24 "kCacheSize must be a power of 2");
25 } 25 }
26 26
27 int ThreadedStreamingCache::fetchCacheLine(uint64_t address) const { 27 bool ThreadedStreamingCache::fetchCacheLine(uint64_t address) const {
28 uint64_t Base = address & kCacheSizeMask; 28 uint64_t Base = address & kCacheSizeMask;
29 int Ret; 29 uint64_t Ret;
30 ScopedLock L(StreamerLock); 30 ScopedLock L(StreamerLock);
31 if (Streamer->isValidAddress(Base + kCacheSize - 1)) { 31 if (Streamer->isValidAddress(Base + kCacheSize - 1)) {
32 Ret = Streamer->readBytes(Base, kCacheSize, &Cache[0]); 32 Ret = Streamer->readBytes(&Cache[0], kCacheSize, Base);
33 assert(Ret == 0); 33 if (Ret != kCacheSize)
34 return true;
34 MinObjectSize = Base + kCacheSize; 35 MinObjectSize = Base + kCacheSize;
35 } else { 36 } else {
36 uint64_t End = Streamer->getExtent(); 37 uint64_t End = Streamer->getExtent();
37 assert(End > address && End <= Base + kCacheSize); 38 assert(End > address && End <= Base + kCacheSize);
38 Ret = Streamer->readBytes(Base, End - Base, &Cache[0]); 39 Ret = Streamer->readBytes(&Cache[0], End - Base, Base);
39 assert(Ret == 0); 40 if (Ret != (End - Base))
41 return true;
40 MinObjectSize = End; 42 MinObjectSize = End;
41 } 43 }
42 CacheBase = Base; 44 CacheBase = Base;
43 return Ret; 45 return false;
44 } 46 }
45 47
46 int ThreadedStreamingCache::readByte( 48 uint64_t ThreadedStreamingCache::readBytes(uint8_t* Buf, uint64_t Size,
47 uint64_t address, uint8_t* ptr) const { 49 uint64_t Address) const {
48 if (address < CacheBase || address >= CacheBase + kCacheSize) {
49 if(fetchCacheLine(address))
50 return -1;
51 }
52 *ptr = Cache[address - CacheBase];
53 return 0;
54 }
55
56 int ThreadedStreamingCache::readBytes(
57 uint64_t address, uint64_t size, uint8_t* buf) const {
58 // To keep the cache fetch simple, we currently require that no request cross 50 // To keep the cache fetch simple, we currently require that no request cross
59 // the cache line. This isn't a problem for the bitcode reader because it only 51 // the cache line. This isn't a problem for the bitcode reader because it only
60 // fetches a byte or a word at a time. 52 // fetches a byte or a word at a time.
61 if (address < CacheBase || (address + size) > CacheBase + kCacheSize) { 53 if (Address < CacheBase || (Address + Size) > CacheBase + kCacheSize) {
62 if ((address & kCacheSizeMask) != ((address + size - 1) & kCacheSizeMask)) 54 if ((Address & kCacheSizeMask) != ((Address + Size - 1) & kCacheSizeMask))
63 llvm::report_fatal_error("readBytes request spans cache lines"); 55 llvm::report_fatal_error("readBytes request spans cache lines");
64 if(fetchCacheLine(address)) 56 if (fetchCacheLine(Address))
65 return -1; 57 llvm::report_fatal_error("readBytes failed to fetch a full cache line");
66 } 58 }
67 memcpy(buf, &Cache[address - CacheBase], size); 59 memcpy(Buf, &Cache[Address - CacheBase], Size);
68 return 0; 60 return Size;
69 } 61 }
70 62
71 uint64_t ThreadedStreamingCache::getExtent() const { 63 uint64_t ThreadedStreamingCache::getExtent() const {
72 llvm::report_fatal_error( 64 llvm::report_fatal_error(
73 "getExtent should not be called for pnacl streaming bitcode"); 65 "getExtent should not be called for pnacl streaming bitcode");
74 return 0; 66 return 0;
75 } 67 }
76 68
77 bool ThreadedStreamingCache::isValidAddress(uint64_t address) const { 69 bool ThreadedStreamingCache::isValidAddress(uint64_t address) const {
78 if (address < MinObjectSize) 70 if (address < MinObjectSize)
79 return true; 71 return true;
80 ScopedLock L(StreamerLock); 72 ScopedLock L(StreamerLock);
81 bool Valid = Streamer->isValidAddress(address); 73 bool Valid = Streamer->isValidAddress(address);
82 if (Valid) 74 if (Valid)
83 MinObjectSize = address; 75 MinObjectSize = address;
84 return Valid; 76 return Valid;
85 } 77 }
86 78
87 bool ThreadedStreamingCache::isObjectEnd(uint64_t address) const {
88 if (address < MinObjectSize)
89 return false;
90 ScopedLock L(StreamerLock);
91 if (Streamer->isValidAddress(address)) {
92 MinObjectSize = address;
93 return false;
94 }
95 return Streamer->isObjectEnd(address);
96 }
97
98 bool ThreadedStreamingCache::dropLeadingBytes(size_t s) { 79 bool ThreadedStreamingCache::dropLeadingBytes(size_t s) {
99 ScopedLock L(StreamerLock); 80 ScopedLock L(StreamerLock);
100 return Streamer->dropLeadingBytes(s); 81 return Streamer->dropLeadingBytes(s);
101 } 82 }
102 83
103 void ThreadedStreamingCache::setKnownObjectSize(size_t size) { 84 void ThreadedStreamingCache::setKnownObjectSize(size_t size) {
104 MinObjectSize = size; 85 MinObjectSize = size;
105 ScopedLock L(StreamerLock); 86 ScopedLock L(StreamerLock);
106 Streamer->setKnownObjectSize(size); 87 Streamer->setKnownObjectSize(size);
107 } 88 }
108 89
109 const uint64_t ThreadedStreamingCache::kCacheSize; 90 const uint64_t ThreadedStreamingCache::kCacheSize;
110 const uint64_t ThreadedStreamingCache::kCacheSizeMask; 91 const uint64_t ThreadedStreamingCache::kCacheSizeMask;
111 llvm::sys::SmartMutex<false> ThreadedStreamingCache::StreamerLock; 92 llvm::sys::SmartMutex<false> ThreadedStreamingCache::StreamerLock;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698