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

Last change on this file since 5174 was 5174, checked in by cameron, 3 years ago

Kernel/pipeline progress: sychronize with logicalSegmentNo

File size: 6.6 KB
RevLine 
[5044]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 <IDISA/idisa_builder.h>
10#include <llvm/IR/Type.h>
[5122]11#include <iostream>
[5044]12   
[5100]13using namespace parabix;
[5044]14
[5135]15enum SS_struct_index {iProducer_pos = 0, iConsumer_pos = 1, iEnd_of_input = 2, iBuffer_ptr = 3};
16
[5044]17llvm::Type * StreamSetType::getStreamSetBlockType(IDISA::IDISA_Builder * iBuilder) {
18    llvm::Type * streamType = mFieldWidth == 1 ? iBuilder->getBitBlockType() : ArrayType::get(iBuilder->getBitBlockType(), mFieldWidth);
19    return ArrayType::get(streamType, mStreamCount);
20}
[5076]21
[5123]22llvm::PointerType * StreamSetBuffer::getStreamBufferPointerType() {
23    return PointerType::get(mStreamSetType.getStreamSetBlockType(iBuilder), 0);
[5044]24}
25
[5135]26llvm::PointerType * StreamSetBuffer::getStreamSetStructPointerType() {
27    return PointerType::get(mStreamSetStructType, 0);
28}
29
30llvm::Value * StreamSetBuffer::getProducerPosPtr(Value * ptr) {
31    return iBuilder->CreateGEP(ptr, {iBuilder->getInt32(0), iBuilder->getInt32(iProducer_pos)});
32}
33
34void StreamSetBuffer::setProducerPos(Value * ptr, llvm::Value * pos){
35    iBuilder->CreateStore(pos, iBuilder->CreateGEP(ptr, {iBuilder->getInt32(0), iBuilder->getInt32(iProducer_pos)}));
36}
37
[5174]38llvm::Value * StreamSetBuffer::getConsumerPosPtr(Value * ptr) {
[5135]39    return iBuilder->CreateGEP(ptr, {iBuilder->getInt32(0), iBuilder->getInt32(iConsumer_pos)});
40}
41
42void StreamSetBuffer::setConsumerPos(Value * ptr, Value * pos){
43    iBuilder->CreateStore(pos, iBuilder->CreateGEP(ptr, {iBuilder->getInt32(0), iBuilder->getInt32(iConsumer_pos)}));
44}
45
46llvm::Value * StreamSetBuffer::hasEndOfInputPtr(Value * ptr) {
47    return iBuilder->CreateGEP(ptr, {iBuilder->getInt32(0), iBuilder->getInt32(iEnd_of_input)});
48}
49
50void StreamSetBuffer::setEndOfInput(Value * ptr){
51    iBuilder->CreateStore(ConstantInt::get(iBuilder->getInt8Ty(), 1), iBuilder->CreateGEP(ptr, {iBuilder->getInt32(0), iBuilder->getInt32(iEnd_of_input)}));
52}
53
54llvm::Value * StreamSetBuffer::getStreamSetStructPtr(){
55    return mStreamSetStructPtr;
56}
[5100]57// Single Block Buffer
58
[5106]59size_t SingleBlockBuffer::getBufferSize() {
[5100]60    return 1; //iBuilder->getBitBlockWidth();
[5076]61}
62
[5100]63llvm::Value * SingleBlockBuffer::allocateBuffer() {
[5139]64    Type * const size_ty = iBuilder->getSizeTy();
[5135]65    Type * const int8ty = iBuilder->getInt8Ty();
[5142]66    mStreamSetBufferPtr = iBuilder->CreateAlloca(mStreamSetType.getStreamSetBlockType(iBuilder));
[5135]67    mStreamSetStructPtr = iBuilder->CreateAlloca(mStreamSetStructType);
[5139]68    iBuilder->CreateStore(ConstantInt::get(size_ty, 0), iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iProducer_pos)}));
69    iBuilder->CreateStore(ConstantInt::get(size_ty, 0), iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iConsumer_pos)}));
[5135]70    iBuilder->CreateStore(ConstantInt::get(int8ty, 0), iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iEnd_of_input)}));
[5142]71    iBuilder->CreateStore(mStreamSetBufferPtr, iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iBuffer_ptr)}));
[5044]72    return mStreamSetBufferPtr;
73}
74
[5100]75// For a single block buffer, the block pointer is always the buffer base pointer.
[5135]76llvm::Value * SingleBlockBuffer::getStreamSetBlockPointer(llvm::Value * basePtr, llvm::Value * blockNo) {
[5142]77    Value * handle = iBuilder->CreateGEP(basePtr, {iBuilder->getInt32(0), iBuilder->getInt32(iBuffer_ptr)});
78    return iBuilder->CreateLoad(handle);
[5044]79}
80
[5100]81
82// External Unbounded Buffer
83
[5142]84size_t ExternalFileBuffer::getBufferSize() {
[5100]85    return 0;
86}
87
[5142]88void ExternalFileBuffer::setStreamSetBuffer(llvm::Value * ptr, Value * fileSize) {
[5135]89
[5139]90    Type * const size_ty = iBuilder->getSizeTy();
[5135]91    Type * const int8ty = iBuilder->getInt8Ty();
92
93    PointerType * t = getStreamBufferPointerType();   
[5122]94    mStreamSetBufferPtr = iBuilder->CreatePointerBitCastOrAddrSpaceCast(ptr, t);
[5135]95
96    mStreamSetStructPtr = iBuilder->CreateAlloca(mStreamSetStructType);
[5142]97    iBuilder->CreateStore(fileSize, iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iProducer_pos)}));
[5139]98    iBuilder->CreateStore(ConstantInt::get(size_ty, 0), iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iConsumer_pos)}));
[5135]99    iBuilder->CreateStore(ConstantInt::get(int8ty, 1), iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iEnd_of_input)}));
100    iBuilder->CreateStore(mStreamSetBufferPtr, iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iBuffer_ptr)}));
[5122]101}
102
[5142]103llvm::PointerType * ExternalFileBuffer::getStreamBufferPointerType() {
[5123]104    return PointerType::get(mStreamSetType.getStreamSetBlockType(iBuilder), mAddrSpace);
105}
106
[5142]107llvm::Value * ExternalFileBuffer::allocateBuffer() {
[5100]108    throw std::runtime_error("External buffers cannot be allocated.");
109}
110
[5142]111llvm::Value * ExternalFileBuffer::getStreamSetBlockPointer(llvm::Value * basePtr, llvm::Value * blockNo) {
[5135]112    Value * handle = iBuilder->CreateGEP(basePtr, {iBuilder->getInt32(0), iBuilder->getInt32(iBuffer_ptr)});
113    return iBuilder->CreateGEP(iBuilder->CreateLoad(handle), {blockNo});
[5100]114}
115
116
117// Circular Stack Allocated Buffer
118
[5106]119size_t CircularBuffer::getBufferSize() {
[5100]120    return mBufferBlocks; // * iBuilder->getBitBlockWidth();
121}
122
123llvm::Value * CircularBuffer::allocateBuffer() {
[5139]124    Type * const size_ty = iBuilder->getSizeTy();
[5135]125    Type * const int8ty = iBuilder->getInt8Ty();
[5123]126    mStreamSetBufferPtr = iBuilder->CreateAlloca(mStreamSetType.getStreamSetBlockType(iBuilder), ConstantInt::get(iBuilder->getSizeTy(), mBufferBlocks));
[5135]127    mStreamSetStructPtr = iBuilder->CreateAlloca(mStreamSetStructType);
[5139]128    iBuilder->CreateStore(ConstantInt::get(size_ty, 0), iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iProducer_pos)}));
129    iBuilder->CreateStore(ConstantInt::get(size_ty, 0), iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iConsumer_pos)}));
[5135]130    iBuilder->CreateStore(ConstantInt::get(int8ty, 0), iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iEnd_of_input)}));
131    iBuilder->CreateStore(mStreamSetBufferPtr, iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iBuffer_ptr)}));
132
[5100]133    return mStreamSetBufferPtr;
134}
135
[5135]136llvm::Value * CircularBuffer::getStreamSetBlockPointer(llvm::Value * basePtr, llvm::Value * blockNo) {
137    Value * handle = iBuilder->CreateGEP(basePtr, {iBuilder->getInt32(0), iBuilder->getInt32(iBuffer_ptr)});
138    return iBuilder->CreateGEP(iBuilder->CreateLoad(handle), {iBuilder->CreateAnd(blockNo, ConstantInt::get(iBuilder->getSizeTy(), mBufferBlocks-1))});
[5100]139}
140
Note: See TracBrowser for help on using the repository browser.