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

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

Kernel/pipeline progress: sychronize with logicalSegmentNo

File size: 6.6 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 <IDISA/idisa_builder.h>
10#include <llvm/IR/Type.h>
11#include <iostream>
12   
13using namespace parabix;
14
15enum SS_struct_index {iProducer_pos = 0, iConsumer_pos = 1, iEnd_of_input = 2, iBuffer_ptr = 3};
16
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}
21
22llvm::PointerType * StreamSetBuffer::getStreamBufferPointerType() {
23    return PointerType::get(mStreamSetType.getStreamSetBlockType(iBuilder), 0);
24}
25
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
38llvm::Value * StreamSetBuffer::getConsumerPosPtr(Value * ptr) {
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}
57// Single Block Buffer
58
59size_t SingleBlockBuffer::getBufferSize() {
60    return 1; //iBuilder->getBitBlockWidth();
61}
62
63llvm::Value * SingleBlockBuffer::allocateBuffer() {
64    Type * const size_ty = iBuilder->getSizeTy();
65    Type * const int8ty = iBuilder->getInt8Ty();
66    mStreamSetBufferPtr = iBuilder->CreateAlloca(mStreamSetType.getStreamSetBlockType(iBuilder));
67    mStreamSetStructPtr = iBuilder->CreateAlloca(mStreamSetStructType);
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)}));
70    iBuilder->CreateStore(ConstantInt::get(int8ty, 0), iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iEnd_of_input)}));
71    iBuilder->CreateStore(mStreamSetBufferPtr, iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iBuffer_ptr)}));
72    return mStreamSetBufferPtr;
73}
74
75// For a single block buffer, the block pointer is always the buffer base pointer.
76llvm::Value * SingleBlockBuffer::getStreamSetBlockPointer(llvm::Value * basePtr, llvm::Value * blockNo) {
77    Value * handle = iBuilder->CreateGEP(basePtr, {iBuilder->getInt32(0), iBuilder->getInt32(iBuffer_ptr)});
78    return iBuilder->CreateLoad(handle);
79}
80
81
82// External Unbounded Buffer
83
84size_t ExternalFileBuffer::getBufferSize() {
85    return 0;
86}
87
88void ExternalFileBuffer::setStreamSetBuffer(llvm::Value * ptr, Value * fileSize) {
89
90    Type * const size_ty = iBuilder->getSizeTy();
91    Type * const int8ty = iBuilder->getInt8Ty();
92
93    PointerType * t = getStreamBufferPointerType();   
94    mStreamSetBufferPtr = iBuilder->CreatePointerBitCastOrAddrSpaceCast(ptr, t);
95
96    mStreamSetStructPtr = iBuilder->CreateAlloca(mStreamSetStructType);
97    iBuilder->CreateStore(fileSize, iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iProducer_pos)}));
98    iBuilder->CreateStore(ConstantInt::get(size_ty, 0), iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iConsumer_pos)}));
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)}));
101}
102
103llvm::PointerType * ExternalFileBuffer::getStreamBufferPointerType() {
104    return PointerType::get(mStreamSetType.getStreamSetBlockType(iBuilder), mAddrSpace);
105}
106
107llvm::Value * ExternalFileBuffer::allocateBuffer() {
108    throw std::runtime_error("External buffers cannot be allocated.");
109}
110
111llvm::Value * ExternalFileBuffer::getStreamSetBlockPointer(llvm::Value * basePtr, llvm::Value * blockNo) {
112    Value * handle = iBuilder->CreateGEP(basePtr, {iBuilder->getInt32(0), iBuilder->getInt32(iBuffer_ptr)});
113    return iBuilder->CreateGEP(iBuilder->CreateLoad(handle), {blockNo});
114}
115
116
117// Circular Stack Allocated Buffer
118
119size_t CircularBuffer::getBufferSize() {
120    return mBufferBlocks; // * iBuilder->getBitBlockWidth();
121}
122
123llvm::Value * CircularBuffer::allocateBuffer() {
124    Type * const size_ty = iBuilder->getSizeTy();
125    Type * const int8ty = iBuilder->getInt8Ty();
126    mStreamSetBufferPtr = iBuilder->CreateAlloca(mStreamSetType.getStreamSetBlockType(iBuilder), ConstantInt::get(iBuilder->getSizeTy(), mBufferBlocks));
127    mStreamSetStructPtr = iBuilder->CreateAlloca(mStreamSetStructType);
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)}));
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
133    return mStreamSetBufferPtr;
134}
135
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))});
139}
140
Note: See TracBrowser for help on using the repository browser.