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

Last change on this file since 5697 was 5694, checked in by cameron, 2 years ago

Update scanmatch callback protocol to use line start/end pointers; add finalize callback for buffer end

File size: 21.9 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
30void MMapSourceKernel::linkExternalMethods(const std::unique_ptr<kernel::KernelBuilder> & kb) {
31    mFileSizeFunction = kb->LinkFunction("file_size", &file_size);
32}
33
34void MMapSourceKernel::generateInitializeMethod(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    assert (kb->getKernel() == this);
40    Value * const fd = kb->getScalarField("fileDescriptor");
41    assert (mFileSizeFunction);
42    Value * fileSize = kb->CreateCall(mFileSizeFunction, fd);
43    fileSize = kb->CreateZExtOrTrunc(fileSize, sizeTy);
44    if (mCodeUnitWidth > 8) {
45        fileSize = kb->CreateUDiv(fileSize, kb->getSize(mCodeUnitWidth / 8));
46    }
47    Value * const isEmpty = kb->CreateICmpEQ(fileSize, ConstantInt::getNullValue(fileSize->getType()));
48    kb->CreateUnlikelyCondBr(isEmpty, emptyFile, nonEmptyFile);
49    // we cannot mmap a 0 length file; just create a 1-page sized fake file buffer for simplicity
50    kb->SetInsertPoint(emptyFile);
51    Constant * pageSize = kb->getSize(getpagesize());
52    Value * fakeFileBuffer = kb->CreateAnonymousMMap(pageSize);
53    kb->CreateBr(exit);
54
55    kb->SetInsertPoint(nonEmptyFile);
56    Value * fileBackedBuffer = kb->CreateFileSourceMMap(fd, fileSize);
57    kb->CreateBr(exit);
58
59    kb->SetInsertPoint(exit);
60    PHINode * buffer = kb->CreatePHI(fileBackedBuffer->getType(), 2);
61    buffer->addIncoming(fakeFileBuffer, emptyFile);
62    buffer->addIncoming(fileBackedBuffer, nonEmptyFile);
63    PHINode * size = kb->CreatePHI(sizeTy, 2);
64    size->addIncoming(pageSize, emptyFile);
65    size->addIncoming(fileSize, nonEmptyFile);
66    Value * bufferPtr = kb->CreatePointerCast(buffer, kb->getInt8PtrTy());
67    kb->setBaseAddress("sourceBuffer", bufferPtr);
68    kb->setBufferedSize("sourceBuffer", size);
69    kb->setScalarField("readableBuffer", bufferPtr);
70    kb->setScalarField("fileSize", fileSize);
71    kb->setCapacity("sourceBuffer", fileSize);
72    kb->CreateMAdvise(buffer, fileSize, CBuilder::ADVICE_WILLNEED);
73
74}
75
76void MMapSourceKernel::generateDoSegmentMethod(const std::unique_ptr<KernelBuilder> & kb) {
77
78    BasicBlock * dropPages = kb->CreateBasicBlock("dropPages");
79    BasicBlock * processSegment = kb->CreateBasicBlock("produceData");
80    BasicBlock * setTermination = kb->CreateBasicBlock("setTermination");
81    BasicBlock * mmapSourceExit = kb->CreateBasicBlock("mmapSourceExit");
82
83    // instruct the OS that it can safely drop any fully consumed pages
84    Value * consumed = kb->getConsumedItemCount("sourceBuffer");
85    IntegerType * const consumedTy = cast<IntegerType>(consumed->getType());
86    Type * const int8PtrTy = kb->getInt8PtrTy();
87
88    DataLayout DL(kb->getModule());
89    IntegerType * const intAddrTy = kb->getIntPtrTy(DL);
90
91    // multiply the consumed count by the code unit size then mask off any partial pages
92    if (mCodeUnitWidth > 8) {
93        consumed = kb->CreateMul(consumed, ConstantInt::get(consumedTy, mCodeUnitWidth / 8));
94    }
95    const auto pageSize = getpagesize();
96    if (LLVM_LIKELY((pageSize & (pageSize - 1)) == 0)) {
97        consumed = kb->CreateAnd(consumed, ConstantExpr::getNeg(ConstantInt::get(consumedTy, pageSize)));
98    } else {
99        consumed = kb->CreateSub(consumed, kb->CreateURem(consumed, ConstantInt::get(consumedTy, pageSize)));
100    }
101
102    Value * sourceBuffer = kb->getBaseAddress("sourceBuffer");
103    sourceBuffer = kb->CreatePtrToInt(sourceBuffer, intAddrTy);
104    if (LLVM_UNLIKELY(intAddrTy->getBitWidth() > consumedTy->getBitWidth())) {
105        consumed = kb->CreateZExt(consumed, intAddrTy);
106    } else if (LLVM_UNLIKELY(intAddrTy->getBitWidth() < consumedTy->getBitWidth())) {
107        sourceBuffer = kb->CreateZExt(sourceBuffer, consumedTy);
108    }
109    Value * consumedBuffer = kb->CreateAdd(sourceBuffer, consumed);
110    Value * readableBuffer = kb->getScalarField("readableBuffer");
111    readableBuffer = kb->CreatePtrToInt(readableBuffer, consumedBuffer->getType());
112    Value * unnecessaryBytes = kb->CreateSub(consumedBuffer, readableBuffer);
113
114    // avoid calling madvise unless an actual page table change could occur
115    Value * hasPagesToDrop = kb->CreateICmpEQ(unnecessaryBytes, ConstantInt::getNullValue(intAddrTy));
116    kb->CreateLikelyCondBr(hasPagesToDrop, processSegment, dropPages);
117
118    kb->SetInsertPoint(dropPages);
119    kb->CreateMAdvise(kb->CreateIntToPtr(readableBuffer, int8PtrTy), unnecessaryBytes, CBuilder::ADVICE_DONTNEED);
120    readableBuffer = kb->CreateIntToPtr(kb->CreateAdd(readableBuffer, unnecessaryBytes), int8PtrTy);
121    kb->setScalarField("readableBuffer", readableBuffer);
122    kb->CreateBr(processSegment);
123
124    // determine whether or not we've exhausted the file buffer
125    kb->SetInsertPoint(processSegment);
126    ConstantInt * segmentItems = kb->getSize(mSegmentBlocks * kb->getBitBlockWidth());
127    Value * const fileSize = kb->getScalarField("fileSize");
128    Value * const produced = kb->CreateAdd(kb->getProducedItemCount("sourceBuffer"), segmentItems);
129    Value * const lessThanFullSegment = kb->CreateICmpULT(fileSize, produced);
130    kb->CreateUnlikelyCondBr(lessThanFullSegment, setTermination, mmapSourceExit);
131    kb->SetInsertPoint(setTermination);
132
133    kb->setTerminationSignal();
134    kb->CreateBr(mmapSourceExit);
135
136    // finally, set the "produced" count to reflect current position in the file
137    kb->SetInsertPoint(mmapSourceExit);
138    PHINode * itemsRead = kb->CreatePHI(produced->getType(), 2);
139    itemsRead->addIncoming(produced, processSegment);
140    itemsRead->addIncoming(fileSize, setTermination);
141    kb->setProducedItemCount("sourceBuffer", itemsRead);
142}
143
144void MMapSourceKernel::generateFinalizeMethod(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 std::unique_ptr<KernelBuilder> & kb) {
163    const size_t initialBufferSize = 8 * getpagesize() * mCodeUnitWidth;
164    ConstantInt * const bufferBytes = kb->getSize(initialBufferSize * mCodeUnitWidth/8);
165    PointerType * const codeUnitPtrTy = IntegerType::get(kb->getContext(), mCodeUnitWidth)->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 std::unique_ptr<KernelBuilder> & kb) {
175
176    ConstantInt * const readSize = kb->getSize(getpagesize() * 8/mCodeUnitWidth);
177    PointerType * const codeUnitPtrTy = IntegerType::get(kb->getContext(), mCodeUnitWidth)->getPointerTo();
178    PointerType * const i8PtrTy = IntegerType::get(kb->getContext(), 8)->getPointerTo();
179    ConstantInt * const codeUnitBytes = kb->getSize(mCodeUnitWidth/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    assert(kb->getKernel() == this);
187
188    // Check whether we need to read another page of data
189    ConstantInt * const segmentSize = kb->getSize(mSegmentBlocks * kb->getBitBlockWidth());
190    Value * bufferedSize = kb->getBufferedSize("sourceBuffer");
191    Value * const produced = kb->getProducedItemCount("sourceBuffer");
192    Value * unreadSize = kb->CreateSub(bufferedSize, produced);
193    kb->CreateUnlikelyCondBr(kb->CreateICmpULT(unreadSize, segmentSize), exhaustedBuffer, stdInExit);
194
195    // If so, it checks whether it can simply append another page to the existing buffer or whether
196    // we need to perform a copyback.
197
198    kb->SetInsertPoint(exhaustedBuffer);
199
200    // Otherwise, we're going to have to perform a copy back...
201
202    // Let L be the logical buffer address (i.e., the position of the "first code unit" of the input stream)
203    // and B be the address pointing to the beginning of our actual buffer. Check whether:
204
205    //     L + produced + readSize < B + capacity
206
207    // If so, we can append to our existing buffer without impacting any subsequent kernel.
208
209    Value * inputStream = kb->getRawOutputPointer("sourceBuffer", kb->getInt32(0), kb->getInt32(0));
210    Value * const originalPtr = kb->CreateGEP(inputStream, produced);
211
212    Value * const buffer = kb->getScalarField("buffer");
213    Value * const capacity = kb->getScalarField("capacity");
214
215    Value * L = kb->CreateGEP(originalPtr, readSize);
216    Value * B = kb->CreateGEP(buffer, capacity);
217    Value * const canAppend = kb->CreateICmpULT(L, B);
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 / mCodeUnitWidth));
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 (mCodeUnitWidth != 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 (mCodeUnitWidth != 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 = mCodeUnitWidth == 8 ? expandedCapacity : kb->CreateMul(expandedCapacity, codeUnitBytes);
259    Value * const expandedBuffer = kb->CreatePointerCast(kb->CreateCacheAlignedMalloc(expandedBytes), codeUnitPtrTy);
260    target = mCodeUnitWidth == 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    assert(kb->getKernel() == this);
287    Value * const fd = kb->getScalarField("fileDescriptor");
288    Value * toRead = readSize;
289    if (mCodeUnitWidth != 8) {
290        toRead = kb->CreateMul(toRead, codeUnitBytes);
291    }
292    Value * bytesRead = kb->CreateReadCall(fd, addr, toRead);
293    Value * itemsRead = bytesRead;
294    if (mCodeUnitWidth != 8) {
295        itemsRead = kb->CreateUDiv(bytesRead, codeUnitBytes);
296    }
297    unreadSize = kb->CreateAdd(unreadSize, itemsRead);
298    bufferedSize = kb->CreateAdd(bufferedSize, itemsRead);
299    kb->setBufferedSize("sourceBuffer", bufferedSize);
300    Value * const exhaustedInputSource = kb->CreateICmpULT(unreadSize, segmentSize);
301    BasicBlock * const setTermination = kb->CreateBasicBlock("SetTermination");
302    kb->CreateUnlikelyCondBr(exhaustedInputSource, setTermination, stdInExit);
303
304    // ... zero out the remaining bytes and set the termination signal.
305    kb->SetInsertPoint(setTermination);
306    Value * bytesToZero = kb->CreateSub(segmentSize, unreadSize);
307    Value * unreadPtr = kb->CreateGEP(addr, unreadSize);
308    bytesToZero = mCodeUnitWidth == 8 ? bytesToZero : kb->CreateMul(bytesToZero, codeUnitBytes);
309    if (mCodeUnitWidth != 8) {
310        bytesToZero = kb->CreateMul(bytesToZero, codeUnitBytes);
311        unreadPtr = kb->CreatePointerCast(unreadPtr, i8PtrTy);
312    }
313    kb->CreateMemZero(unreadPtr, bytesToZero);
314    kb->setTerminationSignal();
315    kb->CreateBr(stdInExit);
316
317    // finally add the segment item count to the produced item count to inform the subsequent kernels how
318    // much data is available for processing
319    kb->SetInsertPoint(stdInExit);
320    stdInExit->moveAfter(setTermination);
321    PHINode * const items = kb->CreatePHI(produced->getType(), 3);
322    items->addIncoming(segmentSize, entryBlock);
323    items->addIncoming(segmentSize, readData);
324    items->addIncoming(unreadSize, setTermination);
325    kb->setProducedItemCount("sourceBuffer", kb->CreateAdd(produced, items));
326}
327
328void ReadSourceKernel::generateFinalizeMethod(const std::unique_ptr<KernelBuilder> & kb) {
329    kb->CreateFree(kb->getScalarField("buffer"));
330}
331
332ReadSourceKernel::ReadSourceKernel(const std::unique_ptr<kernel::KernelBuilder> & kb, unsigned blocksPerSegment, unsigned codeUnitWidth)
333: SegmentOrientedKernel("read_source"  + std::to_string(blocksPerSegment) + "@" + std::to_string(codeUnitWidth)
334, {}
335, {Binding{kb->getStreamSetTy(1, codeUnitWidth), "sourceBuffer"}}
336, {Binding{kb->getInt32Ty(), "fileDescriptor"}}
337, {}
338, {Binding{IntegerType::get(kb->getContext(), codeUnitWidth)->getPointerTo(), "buffer"}, Binding{kb->getSizeTy(), "capacity"}})
339, mSegmentBlocks(blocksPerSegment)
340, mCodeUnitWidth(codeUnitWidth) {
341
342}
343
344// Hybrid MMap/Read source kernel
345   
346FDSourceKernel::FDSourceKernel(const std::unique_ptr<kernel::KernelBuilder> & kb, unsigned blocksPerSegment, unsigned codeUnitWidth)
347: SegmentOrientedKernel("FD_source" + std::to_string(blocksPerSegment) + "@" + std::to_string(codeUnitWidth)
348, {}
349, {Binding{kb->getStreamSetTy(1, codeUnitWidth), "sourceBuffer"}}
350, {Binding{kb->getInt32Ty(), "fileDescriptor"}}
351, {}
352, {Binding{IntegerType::get(kb->getContext(), codeUnitWidth)->getPointerTo(), "buffer"}, Binding{kb->getSizeTy(), "capacity"},
353    Binding{kb->getSizeTy(), "fileSize"}, Binding{kb->getInt8PtrTy(), "readableBuffer"}})
354, mSegmentBlocks(blocksPerSegment)
355, mCodeUnitWidth(codeUnitWidth)
356, mFileSizeFunction(nullptr) {
357   
358}
359
360void FDSourceKernel::generateFinalizeMethod(const std::unique_ptr<KernelBuilder> & kb) {
361    BasicBlock * finalizeRead = kb->CreateBasicBlock("finalizeRead");
362    BasicBlock * finalizeMMap = kb->CreateBasicBlock("finalizeMMap");
363    BasicBlock * finalizeDone = kb->CreateBasicBlock("finalizeDone");
364    // if the fileDescriptor is 0, the file is stdin, use readSource kernel logic, otherwise use mmap logic.
365    kb->CreateCondBr(kb->CreateICmpEQ(kb->getScalarField("fileDescriptor"), kb->getInt32(STDIN_FILENO)), finalizeRead, finalizeMMap);
366    kb->SetInsertPoint(finalizeRead);
367    reinterpret_cast<ReadSourceKernel *>(this)->ReadSourceKernel::generateFinalizeMethod(kb);
368    kb->CreateBr(finalizeDone);
369    kb->SetInsertPoint(finalizeMMap);
370    reinterpret_cast<MMapSourceKernel *>(this)->MMapSourceKernel::generateFinalizeMethod(kb);
371    kb->CreateBr(finalizeDone);
372    kb->SetInsertPoint(finalizeDone);
373}
374
375void FDSourceKernel::generateInitializeMethod(const std::unique_ptr<KernelBuilder> & kb) {
376    BasicBlock * initializeRead = kb->CreateBasicBlock("initializeRead");
377    BasicBlock * initializeMMap = kb->CreateBasicBlock("initializeMMap");
378    BasicBlock * initializeDone = kb->CreateBasicBlock("initializeDone");
379    // if the fileDescriptor is 0, the file is stdin, use readSource kernel logic, otherwise use MMap logic.
380    kb->CreateCondBr(kb->CreateICmpEQ(kb->getScalarField("fileDescriptor"), kb->getInt32(STDIN_FILENO)), initializeRead, initializeMMap);
381    kb->SetInsertPoint(initializeRead);
382    reinterpret_cast<ReadSourceKernel *>(this)->ReadSourceKernel::generateInitializeMethod(kb);
383    kb->CreateBr(initializeDone);
384    kb->SetInsertPoint(initializeMMap);
385    reinterpret_cast<MMapSourceKernel *>(this)->MMapSourceKernel::generateInitializeMethod(kb);
386    kb->CreateBr(initializeDone);
387    kb->SetInsertPoint(initializeDone);
388}
389
390void FDSourceKernel::generateDoSegmentMethod(const std::unique_ptr<KernelBuilder> & kb) {
391    BasicBlock * DoSegmentRead = kb->CreateBasicBlock("DoSegmentRead");
392    BasicBlock * DoSegmentMMap = kb->CreateBasicBlock("DoSegmentMMap");
393    BasicBlock * DoSegmentDone = kb->CreateBasicBlock("DoSegmentDone");
394    // if the fileDescriptor is 0, the file is stdin, use readSource kernel logic, otherwise use MMap logic.
395    kb->CreateCondBr(kb->CreateICmpEQ(kb->getScalarField("fileDescriptor"), kb->getInt32(STDIN_FILENO)), DoSegmentRead, DoSegmentMMap);
396    kb->SetInsertPoint(DoSegmentRead);
397    reinterpret_cast<ReadSourceKernel *>(this)->ReadSourceKernel::generateDoSegmentMethod(kb);
398    kb->CreateBr(DoSegmentDone);
399    kb->SetInsertPoint(DoSegmentMMap);
400    reinterpret_cast<MMapSourceKernel *>(this)->MMapSourceKernel::generateDoSegmentMethod(kb);
401    kb->CreateBr(DoSegmentDone);
402    kb->SetInsertPoint(DoSegmentDone);
403}
404
405
406void FDSourceKernel::linkExternalMethods(const std::unique_ptr<kernel::KernelBuilder> & kb) {
407    mFileSizeFunction = kb->LinkFunction("file_size", &file_size);
408}
409   
410   
411/// MEMORY SOURCE KERNEL
412
413void MemorySourceKernel::generateInitializeMethod(const std::unique_ptr<KernelBuilder> & kb) {
414    kb->setBaseAddress("sourceBuffer", kb->CreatePointerCast(kb->getScalarField("fileSource"), kb->getVoidPtrTy()));
415    kb->setBufferedSize("sourceBuffer", kb->getScalarField("fileSize"));
416    kb->setCapacity("sourceBuffer", kb->getScalarField("fileSize"));
417}
418
419void MemorySourceKernel::generateDoSegmentMethod(const std::unique_ptr<KernelBuilder> & kb) {
420
421    BasicBlock * entryBlock = kb->GetInsertBlock();
422    BasicBlock * setTermination = kb->CreateBasicBlock("setTermination");
423    BasicBlock * mmapSourceExit = kb->CreateBasicBlock("sourceExit");
424    ConstantInt * segmentItems = kb->getSize(mSegmentBlocks * kb->getBitBlockWidth());
425    Value * fileItems = kb->getScalarField("fileSize");
426    if (mCodeUnitWidth > 8) {
427        fileItems = kb->CreateUDiv(fileItems, kb->getSize(mCodeUnitWidth / 8));
428    }
429    Value * produced = kb->getProducedItemCount("sourceBuffer");
430    produced = kb->CreateAdd(produced, segmentItems);
431    Value * lessThanFullSegment = kb->CreateICmpULT(fileItems, produced);
432    kb->CreateCondBr(lessThanFullSegment, setTermination, mmapSourceExit);
433    kb->SetInsertPoint(setTermination);
434    kb->setTerminationSignal();
435    kb->CreateBr(mmapSourceExit);
436
437    kb->SetInsertPoint(mmapSourceExit);
438
439    PHINode * itemsRead = kb->CreatePHI(produced->getType(), 2);
440    itemsRead->addIncoming(produced, entryBlock);
441    itemsRead->addIncoming(fileItems, setTermination);
442    kb->setProducedItemCount("sourceBuffer", itemsRead);
443}
444
445MemorySourceKernel::MemorySourceKernel(const std::unique_ptr<kernel::KernelBuilder> & kb, Type * type, unsigned blocksPerSegment, unsigned codeUnitWidth)
446: SegmentOrientedKernel("memory_source",
447    {},
448    {Binding{kb->getStreamSetTy(1, codeUnitWidth), "sourceBuffer"}},
449    {Binding{cast<PointerType>(type), "fileSource"}, Binding{kb->getSizeTy(), "fileSize"}}, {}, {})
450, mSegmentBlocks(blocksPerSegment)
451, mCodeUnitWidth(codeUnitWidth) {
452
453}
454
455}
Note: See TracBrowser for help on using the repository browser.