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

Last change on this file since 5257 was 5246, checked in by nmedfort, 3 years ago

Code clean up to enforce proper calling order of KernelBuilder? methods

File size: 9.7 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   
7#include <kernels/streamset.h>
8#include <vector>
9#include <IR_Gen/idisa_builder.h>
10#include <llvm/IR/Type.h>
11
12using namespace parabix;
13
14enum SS_struct_index {iProducer_pos = 0, iConsumer_pos = 1, iEnd_of_input = 2, iBuffer_ptr = 3};
15
16llvm::Value * StreamSetBuffer::getProducerPosPtr(Value * bufferStructPtr) {
17    return iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iProducer_pos)});
18}
19
20void StreamSetBuffer::setProducerPos(Value * bufferStructPtr, llvm::Value * pos) {
21    iBuilder->CreateStore(pos, getProducerPosPtr(bufferStructPtr));
22}
23
24llvm::Value * StreamSetBuffer::getConsumerPosPtr(Value * bufferStructPtr) {
25    return iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iConsumer_pos)});
26}
27
28void StreamSetBuffer::setConsumerPos(Value * bufferStructPtr, Value * pos) {
29    iBuilder->CreateStore(pos, getConsumerPosPtr(bufferStructPtr));
30}
31
32llvm::Value * StreamSetBuffer::getEndOfInputPtr(Value * bufferStructPtr) {
33    return iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iEnd_of_input)});
34}
35
36void StreamSetBuffer::setEndOfInput(Value * bufferStructPtr) {
37    iBuilder->CreateStore(ConstantInt::get(iBuilder->getInt1Ty(), 1), getEndOfInputPtr(bufferStructPtr));
38}
39
40void StreamSetBuffer::allocateBuffer() {
41    Type * const size_ty = iBuilder->getSizeTy();
42    Type * const int1ty = iBuilder->getInt1Ty();
43    mStreamSetBufferPtr = iBuilder->CreateCacheAlignedAlloca(mStreamSetType, iBuilder->getSize(mBufferBlocks));
44    mStreamSetStructPtr = iBuilder->CreateCacheAlignedAlloca(mStreamSetStructType);
45    iBuilder->CreateStore(ConstantInt::get(size_ty, 0), iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iProducer_pos)}));
46    iBuilder->CreateStore(ConstantInt::get(size_ty, 0), iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iConsumer_pos)}));
47    iBuilder->CreateStore(ConstantInt::get(int1ty, 0), iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iEnd_of_input)}));
48    iBuilder->CreateStore(mStreamSetBufferPtr, iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iBuffer_ptr)}));
49}
50
51// Single Block Buffer
52// For a single block buffer, the block pointer is always the buffer base pointer.
53llvm::Value * SingleBlockBuffer::getStreamSetBlockPointer(llvm::Value * bufferStructPtr, llvm::Value *) {
54    Value * handle = iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iBuffer_ptr)});
55    return iBuilder->CreateLoad(handle);
56}
57
58
59// External File Buffer
60
61void ExternalFileBuffer::setStreamSetBuffer(llvm::Value * ptr, Value * fileSize) {
62   
63    Type * const size_ty = iBuilder->getSizeTy();
64    Type * const int1ty = iBuilder->getInt1Ty();
65   
66    PointerType * t = getStreamBufferPointerType();   
67    mStreamSetBufferPtr = iBuilder->CreatePointerBitCastOrAddrSpaceCast(ptr, t);
68   
69    mStreamSetStructPtr = iBuilder->CreateCacheAlignedAlloca(mStreamSetStructType);
70    iBuilder->CreateStore(fileSize, iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iProducer_pos)}));
71    iBuilder->CreateStore(ConstantInt::get(size_ty, 0), iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iConsumer_pos)}));
72    iBuilder->CreateStore(ConstantInt::get(int1ty, 1), iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iEnd_of_input)}));
73    iBuilder->CreateStore(mStreamSetBufferPtr, iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iBuffer_ptr)}));
74}
75
76void ExternalFileBuffer::setEmptyBuffer(llvm::Value * ptr) {
77   
78    Type * const size_ty = iBuilder->getSizeTy();
79    Type * const int1ty = iBuilder->getInt1Ty();
80   
81    PointerType * t = getStreamBufferPointerType();   
82    mStreamSetBufferPtr = iBuilder->CreatePointerBitCastOrAddrSpaceCast(ptr, t);
83   
84    mStreamSetStructPtr = iBuilder->CreateCacheAlignedAlloca(mStreamSetStructType);
85    iBuilder->CreateStore(ConstantInt::get(size_ty, 0), iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iProducer_pos)}));
86    iBuilder->CreateStore(ConstantInt::get(size_ty, 0), iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iConsumer_pos)}));
87    iBuilder->CreateStore(ConstantInt::get(int1ty,0), iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iEnd_of_input)}));
88    iBuilder->CreateStore(mStreamSetBufferPtr, iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iBuffer_ptr)}));
89}
90
91void ExternalFileBuffer::allocateBuffer() {
92    throw std::runtime_error("External buffers cannot be allocated.");
93}
94
95llvm::Value * ExternalFileBuffer::getStreamSetBlockPointer(llvm::Value * bufferStructPtr, llvm::Value * blockNo) {
96    Value * handle = iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iBuffer_ptr)});
97    Value * bufPtr = iBuilder->CreateLoad(handle);
98    return iBuilder->CreateGEP(bufPtr, blockNo);
99}
100
101// Circular Stack Allocated Buffer
102
103llvm::Value * CircularBuffer::getStreamSetBlockPointer(llvm::Value * bufferStructPtr, llvm::Value * blockNo) {
104    assert (blockNo->getType()->isIntegerTy());
105
106    Value * handle = iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iBuffer_ptr)});
107    Value * bufPtr = iBuilder->CreateLoad(handle);
108    Value * offset = nullptr;
109    if (mBufferBlocks == 1) {
110        offset = ConstantInt::getNullValue(iBuilder->getSizeTy());
111    } else if ((mBufferBlocks & (mBufferBlocks - 1)) == 0) { // is power of 2
112        offset = iBuilder->CreateAnd(blockNo, ConstantInt::get(blockNo->getType(), mBufferBlocks - 1));
113    } else {
114        offset = iBuilder->CreateURem(blockNo, ConstantInt::get(blockNo->getType(), mBufferBlocks));
115    }
116    return iBuilder->CreateGEP(bufPtr, offset);
117}
118
119llvm::Value * LinearCopybackBuffer::getStreamSetBlockPointer(llvm::Value * bufferStructPtr, llvm::Value * blockNo) {
120    Constant * blockWidth = iBuilder->getSize(iBuilder->getStride());
121    Value * consumerPos_ptr = iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iConsumer_pos)});
122    Value * consumerPos = iBuilder->CreateLoad(consumerPos_ptr);
123    Value * consumerBlock = iBuilder->CreateUDiv(consumerPos, blockWidth);
124    Value * handle = iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iBuffer_ptr)});
125    Value * bufPtr = iBuilder->CreateLoad(handle);
126    return iBuilder->CreateGEP(bufPtr, iBuilder->CreateSub(blockNo, consumerBlock));
127}
128
129void LinearCopybackBuffer::setConsumerPos(Value * bufferStructPtr, Value * newConsumerPos) {
130    Type * const i8 = iBuilder->getInt8Ty();
131    Type * const i8_ptr = i8->getPointerTo(mAddrSpace);
132    IntegerType * const sizeTy = iBuilder->getSizeTy();
133
134    Module * const M = iBuilder->getModule();
135
136    Function * const current = iBuilder->GetInsertBlock()->getParent();
137    BasicBlock * const copyBackBody = BasicBlock::Create(M->getContext(), "copy_back", current, 0);
138    BasicBlock * const setConsumerPosExit = BasicBlock::Create(M->getContext(), "setConsumerPos_done", current, 0);
139    Constant * const blockWidth = ConstantInt::get(sizeTy, iBuilder->getStride());
140
141    Constant * const one = ConstantInt::get(sizeTy, 1);
142
143    Value * const consumerPosPtr = getConsumerPosPtr(bufferStructPtr);
144    Value * const consumerPos = iBuilder->CreateLoad(consumerPosPtr);
145
146    // Ensure that the new consumer position is no less than the current position.
147    newConsumerPos = iBuilder->CreateSelect(iBuilder->CreateICmpULT(newConsumerPos, consumerPos), consumerPos, newConsumerPos);
148    Value * producerPos = iBuilder->CreateLoad(iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iProducer_pos)}));
149
150    // Ensure that the new consumer position is no greater than the current producer position.
151    Value * new_pos_lt_producer_pos = iBuilder->CreateICmpULT(newConsumerPos, producerPos);
152    newConsumerPos = iBuilder->CreateSelect(new_pos_lt_producer_pos, newConsumerPos, producerPos);
153
154    // Now, the new_consumer_pos is at most = to the producer_pos; if =, we're done.
155    iBuilder->CreateCondBr(new_pos_lt_producer_pos, copyBackBody, setConsumerPosExit);
156    iBuilder->SetInsertPoint(copyBackBody);
157   
158    Value * new_consumer_block = iBuilder->CreateUDiv(newConsumerPos, blockWidth);
159    Value * lastProducerBlock = iBuilder->CreateUDiv(iBuilder->CreateSub(producerPos, one), blockWidth);
160    Value * copyBlocks = iBuilder->CreateAdd(iBuilder->CreateSub(lastProducerBlock, new_consumer_block), one);
161
162    DataLayout dl(iBuilder->getModule());
163
164    Constant * blockBytes = ConstantInt::get(sizeTy, dl.getTypeAllocSize(mStreamSetType) * iBuilder->getStride());
165
166    Value * copyLength = iBuilder->CreateMul(copyBlocks, blockBytes);
167
168    // Must copy back one full block for each of the streams in the stream set.
169    Value * handle = iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iBuffer_ptr)});
170    Value * bufferPtr = iBuilder->CreateLoad(handle);
171    Value * const consumerBlock = iBuilder->CreateUDiv(consumerPos, blockWidth);
172    Value * copyFrom = iBuilder->CreateGEP(bufferPtr, iBuilder->CreateSub(new_consumer_block, consumerBlock));
173    unsigned alignment = iBuilder->getBitBlockWidth() / 8;
174    iBuilder->CreateMemMove(iBuilder->CreateBitCast(bufferPtr, i8_ptr), iBuilder->CreateBitCast(copyFrom, i8_ptr), copyLength, alignment);
175    iBuilder->CreateBr(setConsumerPosExit);
176    // Copy back done, store the new consumer position.
177    iBuilder->SetInsertPoint(setConsumerPosExit);
178
179    iBuilder->CreateStore(newConsumerPos, consumerPosPtr);
180}   
Note: See TracBrowser for help on using the repository browser.