source: icGREP/icgrep-devel/icgrep/kernels/streamset.cpp @ 5377

Last change on this file since 5377 was 5377, checked in by nmedfort, 2 years ago

Support for stdin. Needs more testing.

File size: 27.4 KB
Line 
1/*
2 *  Copyright (c) 2016 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 */
5
6#include "streamset.h"
7#include <IR_Gen/idisa_builder.h>  // for IDISA_Builder
8#include <llvm/IR/BasicBlock.h>    // for BasicBlock
9#include <llvm/IR/Constants.h>     // for ConstantInt
10#include <llvm/IR/DataLayout.h>    // for DataLayout
11#include <llvm/IR/DerivedTypes.h>  // for IntegerType (ptr only), PointerType
12#include <llvm/IR/Module.h>        // for Module
13#include <llvm/IR/Value.h>         // for Value
14#include <llvm/Support/raw_ostream.h>
15#include <llvm/IR/CFG.h>
16
17namespace llvm { class Constant; }
18namespace llvm { class Function; }
19
20using namespace parabix;
21using namespace llvm;
22using namespace IDISA;
23
24ArrayType * resolveStreamSetType(IDISA_Builder * const b, Type * type);
25
26StructType * resolveExpandableStreamSetType(IDISA_Builder * const b, Type * type);
27
28void StreamSetBuffer::allocateBuffer() {
29    Type * const ty = getType();
30    ConstantInt * blocks = iBuilder->getSize(mBufferBlocks);
31    mStreamSetBufferPtr = iBuilder->CreateCacheAlignedAlloca(ty, iBuilder->getSize(mBufferBlocks));
32    Constant * width = ConstantExpr::getMul(ConstantExpr::getSizeOf(ty), blocks);
33    iBuilder->CreateMemZero(mStreamSetBufferPtr, width, iBuilder->getCacheAlignment());
34}
35
36Value * StreamSetBuffer::getStreamBlockPtr(Value * self, Value * streamIndex, Value * blockIndex, const bool /* readOnly */) const {
37    iBuilder->CreateAssert(iBuilder->CreateICmpULT(streamIndex, getStreamSetCount(self)), "StreamSetBuffer: out-of-bounds stream access");
38    return iBuilder->CreateGEP(getStreamSetBlockPtr(getBaseAddress(self), blockIndex), {iBuilder->getInt32(0), streamIndex});
39}
40
41Value * StreamSetBuffer::getStreamPackPtr(Value * self, Value * streamIndex, Value * blockIndex, Value * packIndex, const bool /* readOnly */) const {
42    iBuilder->CreateAssert(iBuilder->CreateICmpULT(streamIndex, getStreamSetCount(self)), "StreamSetBuffer: out-of-bounds stream access");
43    return iBuilder->CreateGEP(getStreamSetBlockPtr(getBaseAddress(self), blockIndex), {iBuilder->getInt32(0), streamIndex, packIndex});
44}
45
46inline bool StreamSetBuffer::isCapacityGuaranteed(const Value * const index, const size_t capacity) const {
47    if (LLVM_UNLIKELY(isa<ConstantInt>(index))) {
48        if (LLVM_LIKELY(cast<ConstantInt>(index)->getLimitedValue() < capacity)) {
49            return true;
50        }
51    }
52    return false;
53}
54
55Value * StreamSetBuffer::getStreamSetCount(Value *) const {
56    uint64_t count = 1;
57    if (isa<ArrayType>(mBaseType)) {
58        count = mBaseType->getArrayNumElements();
59    }
60    return iBuilder->getSize(count);
61}
62
63inline Value * StreamSetBuffer::modByBufferBlocks(Value * const offset) const {
64    assert (offset->getType()->isIntegerTy());
65    if (isCapacityGuaranteed(offset, mBufferBlocks)) {
66        return offset;
67    } else if (mBufferBlocks == 1) {
68        return ConstantInt::getNullValue(iBuilder->getSizeTy());
69    } else if ((mBufferBlocks & (mBufferBlocks - 1)) == 0) { // is power of 2
70        return iBuilder->CreateAnd(offset, ConstantInt::get(offset->getType(), mBufferBlocks - 1));
71    } else {
72        return iBuilder->CreateURem(offset, ConstantInt::get(offset->getType(), mBufferBlocks));
73    }
74}
75
76/**
77 * @brief getRawItemPointer
78 *
79 * get a raw pointer the iN field at position absoluteItemPosition of the stream number streamIndex of the stream set.
80 * In the case of a stream whose fields are less than one byte (8 bits) in size, the pointer is to the containing byte.
81 * The type of the pointer is i8* for fields of 8 bits or less, otherwise iN* for N-bit fields.
82 */
83Value * StreamSetBuffer::getRawItemPointer(Value * self, Value * streamIndex, Value * absolutePosition) const {
84    Value * ptr = getBaseAddress(self);
85    if (isa<ConstantInt>(streamIndex) && cast<ConstantInt>(streamIndex)->isZero()) {
86        ptr = iBuilder->CreateGEP(ptr, {iBuilder->getInt32(0), streamIndex});
87    }
88    IntegerType * const ty = cast<IntegerType>(mBaseType->getArrayElementType()->getVectorElementType());
89    ptr = iBuilder->CreatePointerCast(ptr, ty->getPointerTo());
90    if (LLVM_UNLIKELY(ty->getBitWidth() < 8)) {
91        const auto bw = ty->getBitWidth();
92        if (LLVM_LIKELY((bw & (bw - 1)) == 0)) { // is power of 2
93            absolutePosition = iBuilder->CreateUDiv(absolutePosition, ConstantInt::get(absolutePosition->getType(), 8 / bw));
94        } else {
95            absolutePosition = iBuilder->CreateMul(absolutePosition, ConstantInt::get(absolutePosition->getType(), bw));
96            absolutePosition = iBuilder->CreateUDiv(absolutePosition, ConstantInt::get(absolutePosition->getType(), 8));
97        }
98    }
99    return iBuilder->CreateGEP(ptr, absolutePosition);
100}
101
102Value * StreamSetBuffer::getLinearlyAccessibleItems(Value * self, Value * fromPosition) const {
103    if (isa<ArrayType>(mType) && dyn_cast<ArrayType>(mType)->getNumElements() > 1) {
104        Constant * stride = iBuilder->getSize(iBuilder->getStride());
105        return iBuilder->CreateSub(stride, iBuilder->CreateURem(fromPosition, stride));
106    } else {
107        Constant * bufSize = iBuilder->getSize(mBufferBlocks * iBuilder->getStride());
108        return iBuilder->CreateSub(bufSize, iBuilder->CreateURem(fromPosition, bufSize));
109    }
110}
111
112Value * StreamSetBuffer::getLinearlyAccessibleBlocks(Value * self, Value * fromBlock) const {
113    Constant * bufBlocks = iBuilder->getSize(mBufferBlocks);
114    return iBuilder->CreateSub(bufBlocks, iBuilder->CreateURem(fromBlock, bufBlocks));
115}
116
117Value * StreamSetBuffer::reserveItemCount(Value * self, llvm::Value * position, llvm::Value *requested) const {
118    report_fatal_error("reserve() can only be used with ExtensibleBuffers");
119}
120
121Value * StreamSetBuffer::getBaseAddress(Value * self) const {
122    return self;
123}
124
125// Single Block Buffer
126
127// For a single block buffer, the block pointer is always the buffer base pointer.
128Value * SingleBlockBuffer::getStreamSetBlockPtr(Value * self, Value *) const {
129    return self;
130}
131
132// External File Buffer
133void ExternalFileBuffer::setStreamSetBuffer(Value * ptr) {
134    mStreamSetBufferPtr = iBuilder->CreatePointerBitCastOrAddrSpaceCast(ptr, getPointerType());
135}
136
137void ExternalFileBuffer::allocateBuffer() {
138    report_fatal_error("External buffers cannot be allocated.");
139}
140
141Value * ExternalFileBuffer::getStreamSetBlockPtr(Value * self, Value * blockIndex) const {
142    return iBuilder->CreateGEP(self, blockIndex);
143}
144
145Value * ExternalFileBuffer::getLinearlyAccessibleItems(Value * self, Value *) const {
146    report_fatal_error("External buffers: getLinearlyAccessibleItems is not supported.");
147}
148
149// ExtensibleBuffer
150Value * ExtensibleBuffer::getLinearlyAccessibleItems(Value * self, Value * fromPosition) const {
151    Value * capacityPtr = iBuilder->CreateGEP(self, {iBuilder->getInt32(0), iBuilder->getInt32(0)});
152    Value * capacity = iBuilder->CreateLoad(capacityPtr);
153    return iBuilder->CreateSub(capacity, fromPosition);
154}
155
156void ExtensibleBuffer::allocateBuffer() {
157    Type * ty = getType();
158    Value * instance = iBuilder->CreateCacheAlignedAlloca(ty);
159    ConstantInt * const capacity = iBuilder->getSize(mBufferBlocks);
160    Value * const capacityPtr = iBuilder->CreateGEP(instance, {iBuilder->getInt32(0), iBuilder->getInt32(0)});
161    iBuilder->CreateStore(capacity, capacityPtr);
162    Constant * const size = ConstantExpr::getMul(ConstantExpr::getSizeOf(ty), capacity);
163    Value * addr = iBuilder->CreateAlignedMalloc(iBuilder->CreateMul(iBuilder->CreateShl(size, 1), size), iBuilder->getCacheAlignment());
164    iBuilder->CreateMemZero(addr, size, iBuilder->getCacheAlignment());
165    Value * const addrPtr = iBuilder->CreateGEP(instance, {iBuilder->getInt32(0), iBuilder->getInt32(1)});
166    addr = iBuilder->CreatePointerCast(addr, addrPtr->getType()->getPointerElementType());
167    iBuilder->CreateStore(addr, addrPtr);
168    mStreamSetBufferPtr = instance;
169}
170
171Value * ExtensibleBuffer::getStreamSetBlockPtr(Value * self, Value * blockIndex) const {
172    return iBuilder->CreateGEP(self, blockIndex);
173}
174
175Value * ExtensibleBuffer::reserveItemCount(Value * self, llvm::Value * position, llvm::Value * requested) const {
176
177    Value * const capacityPtr = iBuilder->CreateGEP(self, {iBuilder->getInt32(0), iBuilder->getInt32(0)});
178    Value * const capacity = iBuilder->CreateLoad(capacityPtr);
179    Type * const intTy = capacity->getType();
180    Constant * const blockSize = ConstantExpr::getIntegerCast(ConstantExpr::getSizeOf(getType()->getStructElementType(1)), intTy, false);
181    Constant * const blockSize2 = ConstantExpr::getMul(blockSize, ConstantInt::get(intTy, 2));
182
183    BasicBlock * const entry = iBuilder->GetInsertBlock();
184    BasicBlock * const expand = BasicBlock::Create(iBuilder->getContext(), "expand", entry->getParent());
185    BasicBlock * const resume = BasicBlock::Create(iBuilder->getContext(), "resume", entry->getParent());
186
187    Value * const reserved = iBuilder->CreateAdd(position, requested);
188
189    iBuilder->CreateLikelyCondBr(iBuilder->CreateICmpULT(reserved, capacity), resume, expand);
190
191    iBuilder->SetInsertPoint(expand);
192
193    Value * const currentSize = iBuilder->CreateMul(capacity, blockSize);
194    Value * const reservedSize = iBuilder->CreateMul(reserved, blockSize2);
195
196    Value * newAddr = iBuilder->CreateAlignedMalloc(reservedSize, iBuilder->getCacheAlignment());
197    Value * const baseAddrPtr = iBuilder->CreateGEP(self, {iBuilder->getInt32(0), iBuilder->getInt32(1)});
198    Value * const baseAddr = iBuilder->CreateLoad(baseAddrPtr);
199
200    iBuilder->CreateMemCpy(newAddr, baseAddr, currentSize, iBuilder->getCacheAlignment());
201    iBuilder->CreateAlignedFree(baseAddr);
202    iBuilder->CreateMemZero(iBuilder->CreateGEP(newAddr, currentSize), iBuilder->CreateSub(reservedSize, currentSize), iBuilder->getCacheAlignment());
203
204    newAddr = iBuilder->CreatePointerCast(newAddr, baseAddr->getType());
205
206    iBuilder->CreateStore(newAddr, baseAddrPtr);
207    iBuilder->CreateStore(iBuilder->CreateShl(capacity, 1), capacityPtr);
208
209    iBuilder->CreateBr(resume);
210
211    iBuilder->SetInsertPoint(resume);
212
213    return iBuilder->CreateMul(requested, blockSize);
214}
215
216Value * ExtensibleBuffer::getBaseAddress(Value * self) const {
217    return iBuilder->CreateLoad(iBuilder->CreateGEP(self, {iBuilder->getInt32(0), iBuilder->getInt32(1)}));
218}
219
220// Circular Buffer
221
222Value * CircularBuffer::getStreamSetBlockPtr(Value * self, Value * blockIndex) const {
223    return iBuilder->CreateGEP(self, modByBufferBlocks(blockIndex));
224}
225
226// CircularCopybackBuffer Buffer
227
228void CircularCopybackBuffer::allocateBuffer() {
229    mStreamSetBufferPtr = iBuilder->CreateCacheAlignedAlloca(getType(), iBuilder->getSize(mBufferBlocks + mOverflowBlocks));
230}
231
232void CircularCopybackBuffer::createCopyBack(Value * self, Value * overFlowItems) const {
233    Type * size_ty = iBuilder->getSizeTy();
234    Type * i8ptr = iBuilder->getInt8PtrTy();
235    Constant * blockSize = iBuilder->getSize(iBuilder->getBitBlockWidth());
236    Function * f = iBuilder->GetInsertBlock()->getParent();
237    BasicBlock * wholeBlockCopy = BasicBlock::Create(iBuilder->getContext(), "wholeBlockCopy", f, 0);
238    BasicBlock * partialBlockCopy = BasicBlock::Create(iBuilder->getContext(), "partialBlockCopy", f, 0);
239    BasicBlock * copyBackDone = BasicBlock::Create(iBuilder->getContext(), "copyBackDone", f, 0);
240    unsigned numStreams = getType()->getArrayNumElements();
241    auto elemTy = getType()->getArrayElementType();
242    unsigned fieldWidth = isa<ArrayType>(elemTy) ? elemTy->getArrayNumElements() : 1;
243    Value * overFlowAreaPtr = iBuilder->CreateGEP(self, iBuilder->getSize(mBufferBlocks));
244    Value * overFlowBlocks = iBuilder->CreateUDiv(overFlowItems, blockSize);
245    Value * partialItems = iBuilder->CreateURem(overFlowItems, blockSize);
246    Value * partialBlockTargetPtr = iBuilder->CreateGEP(self, overFlowBlocks);
247    Value * partialBlockSourcePtr = iBuilder->CreateGEP(overFlowAreaPtr, overFlowBlocks);
248    iBuilder->CreateCondBr(iBuilder->CreateICmpUGT(overFlowBlocks, iBuilder->getSize(0)), wholeBlockCopy, partialBlockCopy);
249    iBuilder->SetInsertPoint(wholeBlockCopy);
250    unsigned alignment = iBuilder->getBitBlockWidth() / 8;
251    Value * copyLength = iBuilder->CreateSub(iBuilder->CreatePtrToInt(partialBlockTargetPtr, size_ty), iBuilder->CreatePtrToInt(self, size_ty));
252    iBuilder->CreateMemMove(iBuilder->CreateBitCast(self, i8ptr), iBuilder->CreateBitCast(overFlowAreaPtr, i8ptr), copyLength, alignment);
253    iBuilder->CreateCondBr(iBuilder->CreateICmpUGT(partialItems, iBuilder->getSize(0)), partialBlockCopy, copyBackDone);
254    iBuilder->SetInsertPoint(partialBlockCopy);
255    Value * copyBits = iBuilder->CreateMul(overFlowItems, iBuilder->getSize(fieldWidth));
256    Value * copyBytes = iBuilder->CreateLShr(iBuilder->CreateAdd(copyBits, iBuilder->getSize(7)), iBuilder->getSize(3));
257    for (unsigned strm = 0; strm < numStreams; strm++) {
258        Value * strmTargetPtr = iBuilder->CreateGEP(partialBlockTargetPtr, {iBuilder->getInt32(0), iBuilder->getInt32(strm)});
259        Value * strmSourcePtr = iBuilder->CreateGEP(partialBlockSourcePtr, {iBuilder->getInt32(0), iBuilder->getInt32(strm)});
260        iBuilder->CreateMemMove(iBuilder->CreateBitCast(strmTargetPtr, i8ptr), iBuilder->CreateBitCast(strmSourcePtr, i8ptr), copyBytes, alignment);
261    }
262    iBuilder->CreateBr(copyBackDone);
263    iBuilder->SetInsertPoint(copyBackDone);
264}
265
266Value * CircularCopybackBuffer::getStreamSetBlockPtr(Value * self, Value * blockIndex) const {
267    return iBuilder->CreateGEP(self, modByBufferBlocks(blockIndex));
268}
269
270// SwizzledCopybackBuffer Buffer
271
272void SwizzledCopybackBuffer::allocateBuffer() {
273    mStreamSetBufferPtr = iBuilder->CreateCacheAlignedAlloca(getType(), iBuilder->getSize(mBufferBlocks + mOverflowBlocks));
274}
275
276void SwizzledCopybackBuffer::createCopyBack(Value * self, Value * overFlowItems) const {
277    Type * size_ty = iBuilder->getSizeTy();
278    Type * i8ptr = iBuilder->getInt8PtrTy();
279    Constant * blockSize = iBuilder->getSize(iBuilder->getBitBlockWidth());
280    Function * f = iBuilder->GetInsertBlock()->getParent();
281    BasicBlock * wholeBlockCopy = BasicBlock::Create(iBuilder->getContext(), "wholeBlockCopy", f, 0);
282    BasicBlock * partialBlockCopy = BasicBlock::Create(iBuilder->getContext(), "partialBlockCopy", f, 0);
283    BasicBlock * copyBackDone = BasicBlock::Create(iBuilder->getContext(), "copyBackDone", f, 0);
284    unsigned numStreams = getType()->getArrayNumElements();
285    unsigned swizzleFactor = iBuilder->getBitBlockWidth()/mFieldWidth;
286    auto elemTy = getType()->getArrayElementType();
287    unsigned fieldWidth = isa<ArrayType>(elemTy) ? elemTy->getArrayNumElements() : 1;
288    Value * overFlowAreaPtr = iBuilder->CreateGEP(self, iBuilder->getSize(mBufferBlocks));
289    Value * overFlowBlocks = iBuilder->CreateUDiv(overFlowItems, blockSize);
290    Value * partialItems = iBuilder->CreateURem(overFlowItems, blockSize);
291    Value * partialBlockTargetPtr = iBuilder->CreateGEP(self, overFlowBlocks);
292    Value * partialBlockSourcePtr = iBuilder->CreateGEP(overFlowAreaPtr, overFlowBlocks);
293    iBuilder->CreateCondBr(iBuilder->CreateICmpUGT(overFlowBlocks, iBuilder->getSize(0)), wholeBlockCopy, partialBlockCopy);
294    iBuilder->SetInsertPoint(wholeBlockCopy);
295    unsigned alignment = iBuilder->getBitBlockWidth() / 8;
296    Value * copyLength = iBuilder->CreateSub(iBuilder->CreatePtrToInt(partialBlockTargetPtr, size_ty), iBuilder->CreatePtrToInt(self, size_ty));
297    iBuilder->CreateMemMove(iBuilder->CreateBitCast(self, i8ptr), iBuilder->CreateBitCast(overFlowAreaPtr, i8ptr), copyLength, alignment);
298    iBuilder->CreateCondBr(iBuilder->CreateICmpUGT(partialItems, iBuilder->getSize(0)), partialBlockCopy, copyBackDone);
299    iBuilder->SetInsertPoint(partialBlockCopy);
300    Value * copyBits = iBuilder->CreateMul(overFlowItems, iBuilder->getSize(fieldWidth * swizzleFactor));
301    Value * copyBytes = iBuilder->CreateLShr(iBuilder->CreateAdd(copyBits, iBuilder->getSize(7)), iBuilder->getSize(3));
302    for (unsigned strm = 0; strm < numStreams; strm += swizzleFactor) {
303        Value * strmTargetPtr = iBuilder->CreateGEP(partialBlockTargetPtr, {iBuilder->getInt32(0), iBuilder->getInt32(strm)});
304        Value * strmSourcePtr = iBuilder->CreateGEP(partialBlockSourcePtr, {iBuilder->getInt32(0), iBuilder->getInt32(strm)});
305        iBuilder->CreateMemMove(iBuilder->CreateBitCast(strmTargetPtr, i8ptr), iBuilder->CreateBitCast(strmSourcePtr, i8ptr), copyBytes, alignment);
306    }
307    iBuilder->CreateBr(copyBackDone);
308    iBuilder->SetInsertPoint(copyBackDone);
309}
310
311Value * SwizzledCopybackBuffer::getStreamSetBlockPtr(Value * self, Value * blockIndex) const {
312    return iBuilder->CreateGEP(self, modByBufferBlocks(blockIndex));
313}
314
315SwizzledCopybackBuffer::SwizzledCopybackBuffer(IDISA::IDISA_Builder * b, Type * type, size_t bufferBlocks, size_t overflowBlocks, unsigned fieldwidth, unsigned AddressSpace)
316: StreamSetBuffer(BufferKind::SwizzledCopybackBuffer, b, type, resolveStreamSetType(b, type), bufferBlocks, AddressSpace), mOverflowBlocks(overflowBlocks), mFieldWidth(fieldwidth) {
317   
318}
319
320// Expandable Buffer
321
322void ExpandableBuffer::allocateBuffer() {
323    mStreamSetBufferPtr = iBuilder->CreateCacheAlignedAlloca(getType());
324    Value * const capacityPtr = iBuilder->CreateGEP(mStreamSetBufferPtr, {iBuilder->getInt32(0), iBuilder->getInt32(0)});
325    iBuilder->CreateStore(iBuilder->getSize(mInitialCapacity), capacityPtr);
326    Type * const bufferType = getType()->getStructElementType(1)->getPointerElementType();
327    Constant * const bufferWidth = ConstantExpr::getIntegerCast(ConstantExpr::getSizeOf(bufferType), iBuilder->getSizeTy(), false);
328    Constant * const size = ConstantExpr::getMul(iBuilder->getSize(mBufferBlocks * mInitialCapacity), bufferWidth);
329    Value * const ptr = iBuilder->CreateAlignedMalloc(size, iBuilder->getCacheAlignment());
330    iBuilder->CreateMemZero(ptr, size, bufferType->getPrimitiveSizeInBits() / 8);
331    Value * const streamSetPtr = iBuilder->CreateGEP(mStreamSetBufferPtr, {iBuilder->getInt32(0), iBuilder->getInt32(1)});
332    iBuilder->CreateStore(iBuilder->CreatePointerCast(ptr, bufferType->getPointerTo()), streamSetPtr);
333}
334
335std::pair<Value *, Value *> ExpandableBuffer::getInternalStreamBuffer(Value * self, Value * streamIndex, Value * blockIndex, const bool readOnly) const {
336
337    // ENTRY
338    Value * const capacityPtr = iBuilder->CreateGEP(self, {iBuilder->getInt32(0), iBuilder->getInt32(0)});
339    Value * const capacity = iBuilder->CreateLoad(capacityPtr);
340    Value * const streamSetPtr = iBuilder->CreateGEP(self, {iBuilder->getInt32(0), iBuilder->getInt32(1)});
341    Value * const streamSet = iBuilder->CreateLoad(streamSetPtr);
342    blockIndex = modByBufferBlocks(blockIndex);
343
344    assert (streamIndex->getType() == capacity->getType());
345    Value * const cond = iBuilder->CreateICmpULT(streamIndex, capacity);
346
347    // Are we guaranteed that we can access this stream?
348    if (readOnly || isCapacityGuaranteed(streamIndex, mInitialCapacity)) {
349        iBuilder->CreateAssert(cond, "ExpandableBuffer: out-of-bounds stream access");
350        Value * offset = iBuilder->CreateAdd(iBuilder->CreateMul(blockIndex, capacity), streamIndex);
351        return {streamSet, offset};
352    }
353
354    BasicBlock * const entry = iBuilder->GetInsertBlock();
355    BasicBlock * const expand = BasicBlock::Create(iBuilder->getContext(), "expand", entry->getParent());
356    BasicBlock * const resume = BasicBlock::Create(iBuilder->getContext(), "resume", entry->getParent());
357
358    iBuilder->CreateLikelyCondBr(cond, resume, expand);
359
360    // EXPAND
361    iBuilder->SetInsertPoint(expand);
362
363    Type * elementType = getType()->getStructElementType(1)->getPointerElementType();
364    Constant * const vectorWidth = ConstantExpr::getIntegerCast(ConstantExpr::getSizeOf(elementType), capacity->getType(), false);
365
366    Value * newCapacity = iBuilder->CreateAdd(streamIndex, iBuilder->getSize(1));
367    newCapacity = iBuilder->CreateCeilLog2(newCapacity);
368    newCapacity = iBuilder->CreateShl(iBuilder->getSize(1), newCapacity, "newCapacity");
369
370    std::string tmp;
371    raw_string_ostream out(tmp);
372    out << "__expand";
373    elementType->print(out);
374    std::string name = out.str();
375
376    Module * const m = iBuilder->getModule();
377    Function * expandFunction = m->getFunction(name);
378
379    if (expandFunction == nullptr) {
380
381        const auto ip = iBuilder->saveIP();
382
383        FunctionType * fty = FunctionType::get(elementType->getPointerTo(), {elementType->getPointerTo(), iBuilder->getSizeTy(), iBuilder->getSizeTy()}, false);
384        expandFunction = Function::Create(fty, GlobalValue::PrivateLinkage, name, m);
385
386        auto args = expandFunction->arg_begin();
387        Value * streamSet = &*args++;
388        Value * capacity = &*args++;
389        Value * newCapacity = &*args;
390
391        BasicBlock * entry = BasicBlock::Create(iBuilder->getContext(), "entry", expandFunction);
392        iBuilder->SetInsertPoint(entry);
393
394        Value * size = iBuilder->CreateMul(newCapacity, iBuilder->getSize(mBufferBlocks));
395        Value * newStreamSet = iBuilder->CreatePointerCast(iBuilder->CreateAlignedMalloc(iBuilder->CreateMul(size, vectorWidth), iBuilder->getCacheAlignment()), elementType->getPointerTo());
396        Value * const diffCapacity = iBuilder->CreateMul(iBuilder->CreateSub(newCapacity, capacity), vectorWidth);
397
398        const auto alignment = elementType->getPrimitiveSizeInBits() / 8;
399        for (unsigned i = 0; i < mBufferBlocks; ++i) {
400            ConstantInt * const offset = iBuilder->getSize(i);
401            Value * srcOffset = iBuilder->CreateMul(capacity, offset);
402            Value * srcPtr = iBuilder->CreateGEP(streamSet, srcOffset);
403            Value * destOffset = iBuilder->CreateMul(newCapacity, offset);
404            Value * destPtr = iBuilder->CreateGEP(newStreamSet, destOffset);
405            iBuilder->CreateMemCpy(destPtr, srcPtr, iBuilder->CreateMul(capacity, vectorWidth), alignment);
406            Value * destZeroOffset = iBuilder->CreateAdd(destOffset, capacity);
407            Value * destZeroPtr = iBuilder->CreateGEP(newStreamSet, destZeroOffset);
408            iBuilder->CreateMemZero(destZeroPtr, diffCapacity, alignment);
409        }
410
411        iBuilder->CreateAlignedFree(streamSet);
412
413        iBuilder->CreateRet(newStreamSet);
414
415        iBuilder->restoreIP(ip);
416    }
417
418    Value * newStreamSet = iBuilder->CreateCall(expandFunction, {streamSet, capacity, newCapacity});
419    iBuilder->CreateStore(newStreamSet, streamSetPtr);
420    iBuilder->CreateStore(newCapacity, capacityPtr);
421
422    iBuilder->CreateBr(resume);
423
424    // RESUME
425    iBuilder->SetInsertPoint(resume);
426
427    PHINode * phiStreamSet = iBuilder->CreatePHI(streamSet->getType(), 2);
428    phiStreamSet->addIncoming(streamSet, entry);
429    phiStreamSet->addIncoming(newStreamSet, expand);
430
431    PHINode * phiCapacity = iBuilder->CreatePHI(capacity->getType(), 2);
432    phiCapacity->addIncoming(capacity, entry);
433    phiCapacity->addIncoming(newCapacity, expand);
434
435    Value * offset = iBuilder->CreateAdd(iBuilder->CreateMul(blockIndex, phiCapacity), streamIndex);
436
437    return {phiStreamSet, offset};
438}
439
440Value * ExpandableBuffer::getStreamBlockPtr(Value * self, Value * streamIndex, Value * blockIndex, const bool readOnly) const {
441    Value * ptr, * offset;
442    std::tie(ptr, offset) = getInternalStreamBuffer(self, streamIndex, blockIndex, readOnly);
443    return iBuilder->CreateGEP(ptr, offset);
444}
445
446Value * ExpandableBuffer::getStreamPackPtr(Value * self, Value * streamIndex, Value * blockIndex, Value * packIndex, const bool readOnly) const {
447    Value * ptr, * offset;
448    std::tie(ptr, offset) = getInternalStreamBuffer(self, streamIndex, blockIndex, readOnly);
449    return iBuilder->CreateGEP(ptr, {offset, packIndex});
450}
451
452Value * ExpandableBuffer::getStreamSetCount(Value * self) const {
453    return iBuilder->CreateLoad(iBuilder->CreateGEP(self, {iBuilder->getInt32(0), iBuilder->getInt32(0)}));
454}
455
456Value * ExpandableBuffer::getBaseAddress(Value * self) const {
457    return iBuilder->CreateLoad(iBuilder->CreateGEP(self, {iBuilder->getInt32(0), iBuilder->getInt32(1)}));
458}
459
460Value * ExpandableBuffer::getStreamSetBlockPtr(Value *, Value *) const {
461    report_fatal_error("Expandable buffers: getStreamSetBlockPtr is not supported.");
462}
463
464Value * ExpandableBuffer::getLinearlyAccessibleItems(Value * self, Value *) const {
465    report_fatal_error("Expandable buffers: getLinearlyAccessibleItems is not supported.");
466}
467
468// Constructors
469SingleBlockBuffer::SingleBlockBuffer(IDISA::IDISA_Builder * b, Type * type)
470: StreamSetBuffer(BufferKind::BlockBuffer, b, type, resolveStreamSetType(b, type), 1, 0) {
471
472}
473
474ExternalFileBuffer::ExternalFileBuffer(IDISA::IDISA_Builder * b, Type * type, unsigned AddressSpace)
475: StreamSetBuffer(BufferKind::ExternalFileBuffer, b, type, resolveStreamSetType(b, type), 0, AddressSpace) {
476
477}
478
479ExtensibleBuffer::ExtensibleBuffer(IDISA::IDISA_Builder * b, Type * type, size_t bufferBlocks, unsigned AddressSpace)
480: StreamSetBuffer(BufferKind::ExtensibleBuffer, b, type, StructType::get(b->getSizeTy(), resolveStreamSetType(b, type)->getPointerTo(), nullptr), bufferBlocks, AddressSpace) {
481
482}
483
484CircularBuffer::CircularBuffer(IDISA::IDISA_Builder * b, Type * type, size_t bufferBlocks, unsigned AddressSpace)
485: StreamSetBuffer(BufferKind::CircularBuffer, b, type, resolveStreamSetType(b, type), bufferBlocks, AddressSpace) {
486
487}
488
489CircularCopybackBuffer::CircularCopybackBuffer(IDISA::IDISA_Builder * b, Type * type, size_t bufferBlocks, size_t overflowBlocks, unsigned AddressSpace)
490: StreamSetBuffer(BufferKind::CircularCopybackBuffer, b, type, resolveStreamSetType(b, type), bufferBlocks, AddressSpace), mOverflowBlocks(overflowBlocks) {
491
492}
493
494ExpandableBuffer::ExpandableBuffer(IDISA::IDISA_Builder * b, Type * type, size_t bufferBlocks, unsigned AddressSpace)
495: StreamSetBuffer(BufferKind::ExpandableBuffer, b, type, resolveExpandableStreamSetType(b, type), bufferBlocks, AddressSpace)
496, mInitialCapacity(type->getArrayNumElements()) {
497
498}
499
500inline StreamSetBuffer::StreamSetBuffer(BufferKind k, IDISA::IDISA_Builder * b, Type * baseType, Type * resolvedType, unsigned blocks, unsigned AddressSpace)
501: mBufferKind(k)
502, iBuilder(b)
503, mType(resolvedType)
504, mBufferBlocks(blocks)
505, mAddressSpace(AddressSpace)
506, mStreamSetBufferPtr(nullptr)
507, mBaseType(baseType) {
508
509}
510
511StreamSetBuffer::~StreamSetBuffer() { }
512
513// Helper routines
514ArrayType * resolveStreamSetType(IDISA_Builder * const b, Type * type) {
515    unsigned numElements = 1;
516    if (LLVM_LIKELY(type->isArrayTy())) {
517        numElements = type->getArrayNumElements();
518        type = type->getArrayElementType();
519    }
520    if (LLVM_LIKELY(type->isVectorTy() && type->getVectorNumElements() == 0)) {
521        type = type->getVectorElementType();
522        if (LLVM_LIKELY(type->isIntegerTy())) {
523            const auto fieldWidth = cast<IntegerType>(type)->getBitWidth();
524            type = b->getBitBlockType();
525            if (fieldWidth != 1) {
526                type = ArrayType::get(type, fieldWidth);
527            }
528            return ArrayType::get(type, numElements);
529        }
530    }
531    std::string tmp;
532    raw_string_ostream out(tmp);
533    type->print(out);
534    out << " is an unvalid stream set buffer type.";
535    report_fatal_error(out.str());
536}
537
538StructType * resolveExpandableStreamSetType(IDISA_Builder * const b, Type * type) {
539    if (LLVM_LIKELY(type->isArrayTy())) {
540        type = type->getArrayElementType();
541    }
542    if (LLVM_LIKELY(type->isVectorTy() && type->getVectorNumElements() == 0)) {
543        type = type->getVectorElementType();
544        if (LLVM_LIKELY(type->isIntegerTy())) {
545            const auto fieldWidth = cast<IntegerType>(type)->getBitWidth();
546            type = b->getBitBlockType();
547            if (fieldWidth != 1) {
548                type = ArrayType::get(type, fieldWidth);
549            }
550            return StructType::get(b->getSizeTy(), type->getPointerTo(), nullptr);
551        }
552    }
553    std::string tmp;
554    raw_string_ostream out(tmp);
555    type->print(out);
556    out << " is an unvalid stream set buffer type.";
557    report_fatal_error(out.str());
558}
Note: See TracBrowser for help on using the repository browser.