source: icGREP/icgrep-devel/icgrep/kernels/source_kernel.cpp @ 5706

Last change on this file since 5706 was 5706, checked in by nmedfort, 20 months ago

First stage of MultiBlockKernel? and pipeline restructuring

File size: 21.7 KB
Line 
1/*
2 *  Copyright (c) 2017 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 */
5#include "source_kernel.h"
6#include <kernels/kernel_builder.h>
7#include <kernels/streamset.h>
8#include <llvm/IR/Module.h>
9#include <sys/stat.h>
10#include <fcntl.h>
11
12using namespace llvm;
13
14inline static size_t round_up_to_nearest(const size_t x, const size_t y) {
15    return (((x - 1) | (y - 1)) + 1);
16}
17
18uint64_t file_size(const uint32_t fd) {
19    struct stat st;
20    if (LLVM_UNLIKELY(fstat(fd, &st) != 0)) {
21        st.st_size = 0;
22    }
23    return st.st_size;
24}
25
26namespace kernel {
27
28/// MMAP SOURCE KERNEL
29
30Function * MMapSourceKernel::linkFileSizeMethod(const std::unique_ptr<kernel::KernelBuilder> & kb) {
31    return kb->LinkFunction("file_size", &file_size);
32}
33
34void MMapSourceKernel::generateInitializeMethod(Function * const fileSizeMethod, const unsigned codeUnitWidth, const std::unique_ptr<KernelBuilder> & kb) {
35    BasicBlock * const emptyFile = kb->CreateBasicBlock("EmptyFile");
36    BasicBlock * const nonEmptyFile = kb->CreateBasicBlock("NonEmptyFile");
37    BasicBlock * const exit = kb->CreateBasicBlock("Exit");
38    IntegerType * const sizeTy = kb->getSizeTy();
39    Value * const fd = kb->getScalarField("fileDescriptor");
40    assert (fileSizeMethod);
41    Value * fileSize = kb->CreateCall(fileSizeMethod, fd);
42    fileSize = kb->CreateZExtOrTrunc(fileSize, sizeTy);
43    if (codeUnitWidth > 8) {
44        fileSize = kb->CreateUDiv(fileSize, kb->getSize(codeUnitWidth / 8));
45    }
46    Value * const isEmpty = kb->CreateICmpEQ(fileSize, ConstantInt::getNullValue(fileSize->getType()));
47    kb->CreateUnlikelyCondBr(isEmpty, emptyFile, nonEmptyFile);
48    // we cannot mmap a 0 length file; just create a 1-page sized fake file buffer for simplicity
49    kb->SetInsertPoint(emptyFile);
50    Constant * pageSize = kb->getSize(getpagesize());
51    Value * fakeFileBuffer = kb->CreateAnonymousMMap(pageSize);
52    kb->CreateBr(exit);
53
54    kb->SetInsertPoint(nonEmptyFile);
55    Value * fileBackedBuffer = kb->CreateFileSourceMMap(fd, fileSize);
56    kb->CreateBr(exit);
57
58    kb->SetInsertPoint(exit);
59    PHINode * buffer = kb->CreatePHI(fileBackedBuffer->getType(), 2);
60    buffer->addIncoming(fakeFileBuffer, emptyFile);
61    buffer->addIncoming(fileBackedBuffer, nonEmptyFile);
62    PHINode * size = kb->CreatePHI(sizeTy, 2);
63    size->addIncoming(pageSize, emptyFile);
64    size->addIncoming(fileSize, nonEmptyFile);
65    Value * bufferPtr = kb->CreatePointerCast(buffer, kb->getInt8PtrTy());
66    kb->setBaseAddress("sourceBuffer", bufferPtr);
67    kb->setBufferedSize("sourceBuffer", size);
68    kb->setScalarField("readableBuffer", bufferPtr);
69    kb->setScalarField("fileSize", fileSize);
70    kb->setCapacity("sourceBuffer", fileSize);
71    kb->CreateMAdvise(buffer, fileSize, CBuilder::ADVICE_WILLNEED);
72
73}
74
75void MMapSourceKernel::generateDoSegmentMethod(const unsigned codeUnitWidth, const unsigned segmentBlocks, const std::unique_ptr<KernelBuilder> & kb) {
76
77    BasicBlock * dropPages = kb->CreateBasicBlock("dropPages");
78    BasicBlock * processSegment = kb->CreateBasicBlock("produceData");
79    BasicBlock * setTermination = kb->CreateBasicBlock("setTermination");
80    BasicBlock * mmapSourceExit = kb->CreateBasicBlock("mmapSourceExit");
81
82    // instruct the OS that it can safely drop any fully consumed pages
83    Value * consumed = kb->getConsumedItemCount("sourceBuffer");
84    IntegerType * const consumedTy = cast<IntegerType>(consumed->getType());
85    Type * const int8PtrTy = kb->getInt8PtrTy();
86
87    DataLayout DL(kb->getModule());
88    IntegerType * const intAddrTy = kb->getIntPtrTy(DL);
89
90    // multiply the consumed count by the code unit size then mask off any partial pages
91    if (codeUnitWidth > 8) {
92        consumed = kb->CreateMul(consumed, ConstantInt::get(consumedTy, codeUnitWidth / 8));
93    }
94    const auto pageSize = getpagesize();
95    if (LLVM_LIKELY((pageSize & (pageSize - 1)) == 0)) {
96        consumed = kb->CreateAnd(consumed, ConstantExpr::getNeg(ConstantInt::get(consumedTy, pageSize)));
97    } else {
98        consumed = kb->CreateSub(consumed, kb->CreateURem(consumed, ConstantInt::get(consumedTy, pageSize)));
99    }
100
101    Value * sourceBuffer = kb->getBaseAddress("sourceBuffer");
102    sourceBuffer = kb->CreatePtrToInt(sourceBuffer, intAddrTy);
103    if (LLVM_UNLIKELY(intAddrTy->getBitWidth() > consumedTy->getBitWidth())) {
104        consumed = kb->CreateZExt(consumed, intAddrTy);
105    } else if (LLVM_UNLIKELY(intAddrTy->getBitWidth() < consumedTy->getBitWidth())) {
106        sourceBuffer = kb->CreateZExt(sourceBuffer, consumedTy);
107    }
108    Value * consumedBuffer = kb->CreateAdd(sourceBuffer, consumed);
109    Value * readableBuffer = kb->getScalarField("readableBuffer");
110    readableBuffer = kb->CreatePtrToInt(readableBuffer, consumedBuffer->getType());
111    Value * unnecessaryBytes = kb->CreateSub(consumedBuffer, readableBuffer);
112
113    // avoid calling madvise unless an actual page table change could occur
114    Value * hasPagesToDrop = kb->CreateICmpEQ(unnecessaryBytes, ConstantInt::getNullValue(intAddrTy));
115    kb->CreateLikelyCondBr(hasPagesToDrop, processSegment, dropPages);
116
117    kb->SetInsertPoint(dropPages);
118    kb->CreateMAdvise(kb->CreateIntToPtr(readableBuffer, int8PtrTy), unnecessaryBytes, CBuilder::ADVICE_DONTNEED);
119    readableBuffer = kb->CreateIntToPtr(kb->CreateAdd(readableBuffer, unnecessaryBytes), int8PtrTy);
120    kb->setScalarField("readableBuffer", readableBuffer);
121    kb->CreateBr(processSegment);
122
123    // determine whether or not we've exhausted the file buffer
124    kb->SetInsertPoint(processSegment);
125    ConstantInt * segmentItems = kb->getSize(segmentBlocks * kb->getBitBlockWidth());
126    Value * const fileSize = kb->getScalarField("fileSize");
127    Value * const produced = kb->CreateAdd(kb->getProducedItemCount("sourceBuffer"), segmentItems);
128    Value * const lessThanFullSegment = kb->CreateICmpULT(fileSize, produced);
129    kb->CreateUnlikelyCondBr(lessThanFullSegment, setTermination, mmapSourceExit);
130
131    kb->SetInsertPoint(setTermination);
132    kb->setTerminationSignal();
133    kb->CreateBr(mmapSourceExit);
134
135    // finally, set the "produced" count to reflect current position in the file
136    kb->SetInsertPoint(mmapSourceExit);
137    PHINode * itemsRead = kb->CreatePHI(produced->getType(), 2);
138    itemsRead->addIncoming(produced, processSegment);
139    itemsRead->addIncoming(fileSize, setTermination);
140    kb->setProducedItemCount("sourceBuffer", itemsRead);
141
142}
143
144void MMapSourceKernel::unmapSourceBuffer(const std::unique_ptr<KernelBuilder> & kb) {
145    kb->CreateMUnmap(kb->getBaseAddress("sourceBuffer"), kb->getBufferedSize("sourceBuffer"));
146}
147
148MMapSourceKernel::MMapSourceKernel(const std::unique_ptr<kernel::KernelBuilder> & kb, unsigned blocksPerSegment, unsigned codeUnitWidth)
149: SegmentOrientedKernel("mmap_source" + std::to_string(blocksPerSegment) + "@" + std::to_string(codeUnitWidth),
150{},
151{Binding{kb->getStreamSetTy(1, codeUnitWidth), "sourceBuffer"}},
152{Binding{kb->getInt32Ty(), "fileDescriptor"}},
153{Binding{kb->getSizeTy(), "fileSize"}}, {Binding{kb->getInt8PtrTy(), "readableBuffer"}})
154, mSegmentBlocks(blocksPerSegment)
155, mCodeUnitWidth(codeUnitWidth)
156, mFileSizeFunction(nullptr) {
157
158}
159
160/// READ SOURCE KERNEL
161
162void ReadSourceKernel::generateInitializeMethod(const unsigned codeUnitWidth, const std::unique_ptr<KernelBuilder> & kb) {
163    const size_t initialBufferSize = 8 * getpagesize() * codeUnitWidth;
164    ConstantInt * const bufferBytes = kb->getSize(initialBufferSize * codeUnitWidth / 8);
165    PointerType * const codeUnitPtrTy = IntegerType::get(kb->getContext(), codeUnitWidth)->getPointerTo();
166    Value * const buffer = kb->CreatePointerCast(kb->CreateCacheAlignedMalloc(bufferBytes), codeUnitPtrTy);
167    kb->setScalarField("buffer", buffer);
168    kb->setScalarField("capacity", kb->getSize(initialBufferSize));
169    kb->setBaseAddress("sourceBuffer", buffer);
170    kb->setBufferedSize("sourceBuffer", kb->getSize(0));
171    kb->setCapacity("sourceBuffer", kb->getSize(initialBufferSize));
172}
173
174void ReadSourceKernel::generateDoSegmentMethod(const unsigned codeUnitWidth, const unsigned segmentBlocks, const std::unique_ptr<KernelBuilder> & kb) {
175
176    ConstantInt * const readSize = kb->getSize(getpagesize() * 8/codeUnitWidth);
177    PointerType * const codeUnitPtrTy = IntegerType::get(kb->getContext(), codeUnitWidth)->getPointerTo();
178    PointerType * const i8PtrTy = IntegerType::get(kb->getContext(), 8)->getPointerTo();
179    ConstantInt * const codeUnitBytes = kb->getSize(codeUnitWidth/8);
180    BasicBlock * const entryBlock = kb->GetInsertBlock();
181    BasicBlock * const exhaustedBuffer = kb->CreateBasicBlock("ExhaustedBuffer");
182    BasicBlock * const waitOnConsumers = kb->CreateBasicBlock("WaitOnConsumers");
183    BasicBlock * const readData = kb->CreateBasicBlock("ReadData");
184    BasicBlock * const stdInExit = kb->CreateBasicBlock("StdInExit");
185
186    // Check whether we need to read another page of data
187    ConstantInt * const segmentSize = kb->getSize(segmentBlocks * kb->getBitBlockWidth());
188    Value * bufferedSize = kb->getBufferedSize("sourceBuffer");
189    Value * const produced = kb->getProducedItemCount("sourceBuffer");
190    Value * unreadSize = kb->CreateSub(bufferedSize, produced);
191
192    kb->CreateUnlikelyCondBr(kb->CreateICmpULT(unreadSize, segmentSize), exhaustedBuffer, stdInExit);
193
194    // If so, it checks whether it can simply append another page to the existing buffer or whether
195    // we need to perform a copyback.
196
197    kb->SetInsertPoint(exhaustedBuffer);
198
199    // Otherwise, we're going to have to perform a copy back...
200
201    // Let L be the logical buffer address (i.e., the position of the "first code unit" of the input stream)
202    // and B be the address pointing to the beginning of our actual buffer. Check whether:
203
204    //     L + produced + readSize < B + capacity
205
206    // If so, we can append to our existing buffer without impacting any subsequent kernel.
207
208    Value * inputStream = kb->getRawOutputPointer("sourceBuffer", kb->getInt32(0));
209    Value * const originalPtr = kb->CreateGEP(inputStream, produced);
210
211    Value * const buffer = kb->getScalarField("buffer");
212    Value * const capacity = kb->getScalarField("capacity");
213
214    Value * L = kb->CreateGEP(originalPtr, readSize);
215    Value * B = kb->CreateGEP(buffer, capacity);
216    Value * const canAppend = kb->CreateICmpULT(L, B);
217
218    kb->CreateLikelyCondBr(canAppend, readData, waitOnConsumers);
219
220    // First wait on any consumers to finish processing then check how much data has been consumed.
221    kb->SetInsertPoint(waitOnConsumers);
222    kb->CreateConsumerWait();
223
224    // Then determine how much data has been consumed and how much needs to be copied back, noting
225    // that our "unproduced" data must be block aligned.
226    const size_t blockAlignment = kb->getBitBlockWidth() / 8;
227    Constant * const alignmentMask = kb->getSize(-(blockAlignment * 8 / codeUnitWidth));
228    Value * const consumed = kb->CreateAnd(kb->getConsumedItemCount("sourceBuffer"), alignmentMask);
229    Value * const remaining = kb->CreateSub(bufferedSize, consumed);
230    Value * const unconsumedPtr = kb->CreateGEP(inputStream, consumed);
231    Value * const consumedMajority = kb->CreateICmpULT(kb->CreateGEP(buffer, remaining), unconsumedPtr);
232    Value * target = buffer;
233    Value * source = unconsumedPtr;
234    Value * toCopy = remaining;
235    if (codeUnitWidth != 8) {
236        source = kb->CreatePointerCast(unconsumedPtr, i8PtrTy);
237        toCopy = kb->CreateMul(remaining, codeUnitBytes);
238    }
239
240    BasicBlock * const copyBack = kb->CreateBasicBlock("CopyBack");
241    BasicBlock * const expandAndCopyBack = kb->CreateBasicBlock("ExpandAndCopyBack");
242    BasicBlock * const calculateLogicalAddress = kb->CreateBasicBlock("CalculateLogicalAddress");
243
244    // Have we consumed enough data that we can safely copy back the unconsumed data without needing
245    // a temporary buffer? (i.e., B + remaining < L + consumed)
246    kb->CreateLikelyCondBr(consumedMajority, copyBack, expandAndCopyBack);
247    kb->SetInsertPoint(copyBack);
248    // If so, just copy the data ...
249    if (codeUnitWidth != 8) {
250        target = kb->CreatePointerCast(buffer, i8PtrTy);
251    }
252    kb->CreateMemCpy(target, source, toCopy, 1);
253    kb->CreateBr(calculateLogicalAddress);
254   
255    // Otherwise, allocate a buffer with twice the capacity and copy the unconsumed data back into it
256    kb->SetInsertPoint(expandAndCopyBack);
257    Value * const expandedCapacity = kb->CreateShl(capacity, 1);
258    Value * const expandedBytes = codeUnitWidth == 8 ? expandedCapacity : kb->CreateMul(expandedCapacity, codeUnitBytes);
259    Value * const expandedBuffer = kb->CreatePointerCast(kb->CreateCacheAlignedMalloc(expandedBytes), codeUnitPtrTy);
260    target = codeUnitWidth == 8 ? expandedBuffer : kb->CreatePointerCast(expandedBuffer, i8PtrTy);
261    kb->CreateMemCpy(target, source, toCopy, 1);
262    kb->CreateFree(buffer);
263    kb->setScalarField("buffer", expandedBuffer);
264    kb->setScalarField("capacity", expandedCapacity);
265    kb->setCapacity("sourceBuffer", expandedCapacity);
266    kb->CreateBr(calculateLogicalAddress);
267
268    // Update the logical address for this buffer....
269    kb->SetInsertPoint(calculateLogicalAddress);
270    PHINode * const baseAddress = kb->CreatePHI(codeUnitPtrTy, 2);
271    baseAddress->addIncoming(buffer, copyBack);
272    baseAddress->addIncoming(expandedBuffer, expandAndCopyBack);
273    Value * const logicalAddress = kb->CreateGEP(baseAddress, kb->CreateNeg(consumed));
274    Value * const modifiedPtr = kb->CreateGEP(baseAddress, remaining);
275    kb->setBaseAddress("sourceBuffer", logicalAddress);
276    kb->CreateBr(readData);
277
278    // Regardless of whether we're simply appending data or had to allocate a new buffer, read a new page
279    // of data into the input source buffer. If we fail to read a full segment ...
280    readData->moveAfter(calculateLogicalAddress);
281    kb->SetInsertPoint(readData);
282    calculateLogicalAddress->moveAfter(calculateLogicalAddress);
283    PHINode * const addr = kb->CreatePHI(codeUnitPtrTy, 2);
284    addr->addIncoming(originalPtr, exhaustedBuffer);
285    addr->addIncoming(modifiedPtr, calculateLogicalAddress);
286    Value * const fd = kb->getScalarField("fileDescriptor");
287
288    Value * itemsRead = kb->CreateReadCall(fd, addr, readSize);
289    if (codeUnitWidth != 8) {
290        itemsRead = kb->CreateUDiv(itemsRead, codeUnitBytes);
291    }
292    unreadSize = kb->CreateAdd(unreadSize, itemsRead);
293    bufferedSize = kb->CreateAdd(bufferedSize, itemsRead);
294    kb->setBufferedSize("sourceBuffer", bufferedSize);
295    Value * const exhaustedInputSource = kb->CreateICmpULT(unreadSize, segmentSize);
296    BasicBlock * const setTermination = kb->CreateBasicBlock("SetTermination");
297    kb->CreateUnlikelyCondBr(exhaustedInputSource, setTermination, stdInExit);
298
299    // ... zero out the remaining bytes and set the termination signal.
300    kb->SetInsertPoint(setTermination);
301    Value * bytesToZero = kb->CreateSub(segmentSize, unreadSize);
302    Value * unreadPtr = kb->CreateGEP(addr, unreadSize);
303    bytesToZero = codeUnitWidth == 8 ? bytesToZero : kb->CreateMul(bytesToZero, codeUnitBytes);
304    if (codeUnitWidth != 8) {
305        bytesToZero = kb->CreateMul(bytesToZero, codeUnitBytes);
306        unreadPtr = kb->CreatePointerCast(unreadPtr, i8PtrTy);
307    }
308    kb->CreateMemZero(unreadPtr, bytesToZero);
309    kb->setCapacity("sourceBuffer", bufferedSize);
310    kb->setTerminationSignal(kb->CreateICmpEQ(unreadSize, Constant::getNullValue(itemsRead->getType())));
311    kb->CreateBr(stdInExit);
312
313    // finally add the segment item count to the produced item count to inform the subsequent kernels how
314    // much data is available for processing
315    kb->SetInsertPoint(stdInExit);
316    stdInExit->moveAfter(setTermination);
317    PHINode * const items = kb->CreatePHI(produced->getType(), 3);
318    items->addIncoming(segmentSize, entryBlock);
319    items->addIncoming(segmentSize, readData);
320    items->addIncoming(unreadSize, setTermination);
321    kb->setProducedItemCount("sourceBuffer", kb->CreateAdd(produced, items));
322}
323
324void ReadSourceKernel::freeBuffer(const std::unique_ptr<KernelBuilder> & kb) {
325    kb->CreateFree(kb->getScalarField("buffer"));
326}
327
328ReadSourceKernel::ReadSourceKernel(const std::unique_ptr<kernel::KernelBuilder> & kb, unsigned blocksPerSegment, unsigned codeUnitWidth)
329: SegmentOrientedKernel("read_source"  + std::to_string(blocksPerSegment) + "@" + std::to_string(codeUnitWidth)
330, {}
331, {Binding{kb->getStreamSetTy(1, codeUnitWidth), "sourceBuffer"}}
332, {Binding{kb->getInt32Ty(), "fileDescriptor"}}
333, {}
334, {Binding{IntegerType::get(kb->getContext(), codeUnitWidth)->getPointerTo(), "buffer"}, Binding{kb->getSizeTy(), "capacity"}})
335, mSegmentBlocks(blocksPerSegment)
336, mCodeUnitWidth(codeUnitWidth) {
337
338}
339
340/// Hybrid MMap/Read source kernel
341
342void FDSourceKernel::linkExternalMethods(const std::unique_ptr<kernel::KernelBuilder> & kb) {
343    mFileSizeFunction = MMapSourceKernel::linkFileSizeMethod(kb);
344}
345
346void FDSourceKernel::generateFinalizeMethod(const std::unique_ptr<KernelBuilder> & kb) {
347    BasicBlock * finalizeRead = kb->CreateBasicBlock("finalizeRead");
348    BasicBlock * finalizeMMap = kb->CreateBasicBlock("finalizeMMap");
349    BasicBlock * finalizeDone = kb->CreateBasicBlock("finalizeDone");
350    // if the fileDescriptor is 0, the file is stdin, use readSource kernel logic, otherwise use mmap logic.
351    kb->CreateCondBr(kb->CreateICmpEQ(kb->getScalarField("fileDescriptor"), kb->getInt32(STDIN_FILENO)), finalizeRead, finalizeMMap);
352    kb->SetInsertPoint(finalizeRead);
353    ReadSourceKernel::freeBuffer(kb);
354    kb->CreateBr(finalizeDone);
355    kb->SetInsertPoint(finalizeMMap);
356    MMapSourceKernel::unmapSourceBuffer(kb);
357    kb->CreateBr(finalizeDone);
358    kb->SetInsertPoint(finalizeDone);
359}
360
361void FDSourceKernel::generateInitializeMethod(const std::unique_ptr<KernelBuilder> & kb) {
362    BasicBlock * initializeRead = kb->CreateBasicBlock("initializeRead");
363    BasicBlock * initializeMMap = kb->CreateBasicBlock("initializeMMap");
364    BasicBlock * initializeDone = kb->CreateBasicBlock("initializeDone");
365    // if the fileDescriptor is 0, the file is stdin, use readSource kernel logic, otherwise use MMap logic.
366    kb->CreateCondBr(kb->CreateICmpEQ(kb->getScalarField("fileDescriptor"), kb->getInt32(STDIN_FILENO)), initializeRead, initializeMMap);
367    kb->SetInsertPoint(initializeRead);
368    ReadSourceKernel::generateInitializeMethod(mCodeUnitWidth, kb);
369    kb->CreateBr(initializeDone);
370    kb->SetInsertPoint(initializeMMap);
371    MMapSourceKernel::generateInitializeMethod(mFileSizeFunction, mCodeUnitWidth, kb);
372    kb->CreateBr(initializeDone);
373    kb->SetInsertPoint(initializeDone);
374}
375
376void FDSourceKernel::generateDoSegmentMethod(const std::unique_ptr<KernelBuilder> & kb) {
377    BasicBlock * DoSegmentRead = kb->CreateBasicBlock("DoSegmentRead");
378    BasicBlock * DoSegmentMMap = kb->CreateBasicBlock("DoSegmentMMap");
379    BasicBlock * DoSegmentDone = kb->CreateBasicBlock("DoSegmentDone");
380    // if the fileDescriptor is 0, the file is stdin, use readSource kernel logic, otherwise use MMap logic.
381    kb->CreateCondBr(kb->CreateICmpEQ(kb->getScalarField("fileDescriptor"), kb->getInt32(STDIN_FILENO)), DoSegmentRead, DoSegmentMMap);
382    kb->SetInsertPoint(DoSegmentRead);
383    ReadSourceKernel::generateDoSegmentMethod(mCodeUnitWidth, mSegmentBlocks, kb);
384    kb->CreateBr(DoSegmentDone);
385    kb->SetInsertPoint(DoSegmentMMap);
386    MMapSourceKernel::generateDoSegmentMethod(mCodeUnitWidth, mSegmentBlocks, kb);
387    kb->CreateBr(DoSegmentDone);
388    kb->SetInsertPoint(DoSegmentDone);
389}
390
391FDSourceKernel::FDSourceKernel(const std::unique_ptr<kernel::KernelBuilder> & kb, unsigned blocksPerSegment, unsigned codeUnitWidth)
392: SegmentOrientedKernel("FD_source" + std::to_string(blocksPerSegment) + "@" + std::to_string(codeUnitWidth)
393, {}
394, {Binding{kb->getStreamSetTy(1, codeUnitWidth), "sourceBuffer"}}
395, {Binding{kb->getInt32Ty(), "fileDescriptor"}}
396, {}
397, {Binding{IntegerType::get(kb->getContext(), codeUnitWidth)->getPointerTo(), "buffer"}, Binding{kb->getSizeTy(), "capacity"},
398    Binding{kb->getSizeTy(), "fileSize"}, Binding{kb->getInt8PtrTy(), "readableBuffer"}})
399, mSegmentBlocks(blocksPerSegment)
400, mCodeUnitWidth(codeUnitWidth)
401, mFileSizeFunction(nullptr) {
402
403}
404
405/// MEMORY SOURCE KERNEL
406
407void MemorySourceKernel::generateInitializeMethod(const std::unique_ptr<KernelBuilder> & kb) {
408    kb->setBaseAddress("sourceBuffer", kb->CreatePointerCast(kb->getScalarField("fileSource"), kb->getVoidPtrTy()));
409    kb->setBufferedSize("sourceBuffer", kb->getScalarField("fileSize"));
410    kb->setCapacity("sourceBuffer", kb->getScalarField("fileSize"));
411}
412
413void MemorySourceKernel::generateDoSegmentMethod(const std::unique_ptr<KernelBuilder> & kb) {
414
415    BasicBlock * entryBlock = kb->GetInsertBlock();
416    BasicBlock * setTermination = kb->CreateBasicBlock("setTermination");
417    BasicBlock * mmapSourceExit = kb->CreateBasicBlock("sourceExit");
418    ConstantInt * segmentItems = kb->getSize(mSegmentBlocks * kb->getBitBlockWidth());
419    Value * fileItems = kb->getScalarField("fileSize");
420    if (mCodeUnitWidth > 8) {
421        fileItems = kb->CreateUDiv(fileItems, kb->getSize(mCodeUnitWidth / 8));
422    }
423    Value * produced = kb->getProducedItemCount("sourceBuffer");
424    produced = kb->CreateAdd(produced, segmentItems);
425    Value * lessThanFullSegment = kb->CreateICmpULT(fileItems, produced);
426    kb->CreateCondBr(lessThanFullSegment, setTermination, mmapSourceExit);
427    kb->SetInsertPoint(setTermination);
428    kb->setTerminationSignal();
429    kb->CreateBr(mmapSourceExit);
430
431    kb->SetInsertPoint(mmapSourceExit);
432
433    PHINode * itemsRead = kb->CreatePHI(produced->getType(), 2);
434    itemsRead->addIncoming(produced, entryBlock);
435    itemsRead->addIncoming(fileItems, setTermination);
436    kb->setProducedItemCount("sourceBuffer", itemsRead);
437}
438
439MemorySourceKernel::MemorySourceKernel(const std::unique_ptr<kernel::KernelBuilder> & kb, Type * type, unsigned blocksPerSegment, unsigned codeUnitWidth)
440: SegmentOrientedKernel("memory_source",
441    {},
442    {Binding{kb->getStreamSetTy(1, codeUnitWidth), "sourceBuffer"}},
443    {Binding{cast<PointerType>(type), "fileSource"}, Binding{kb->getSizeTy(), "fileSize"}}, {}, {})
444, mSegmentBlocks(blocksPerSegment)
445, mCodeUnitWidth(codeUnitWidth) {
446
447}
448
449}
Note: See TracBrowser for help on using the repository browser.