source: icGREP/icgrep-devel/icgrep/kernels/instance.cpp @ 5051

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

s2p kernel with new infrastructure, includes s2p_FinalBlock

File size: 2.9 KB
Line 
1#include <kernels/instance.h>
2#include <IDISA/idisa_builder.h>
3#include <llvm/Support/raw_ostream.h>
4
5using namespace llvm;
6
7namespace kernel {
8
9SlabAllocator<Instance> Instance::mAllocator;
10
11inline bool isPowerOfTwo(const unsigned x) {
12    return (x != 0) && (x & (x - 1)) == 0;
13}
14
15/** ------------------------------------------------------------------------------------------------------------- *
16 * @brief getStreamSet
17 *
18 * Get the stream of the given offset value.
19 ** ------------------------------------------------------------------------------------------------------------- */
20Value * Instance::getStreamSet(Type * const type, Value * const base, const unsigned index, const unsigned bufferSize) {
21    assert ("Base stream set cannot be null!" && base);
22    assert ("Illegal stream set index provided!" && ((index == 0 && bufferSize == 0) || (index < bufferSize)));
23    Value * addr = base;
24    if (bufferSize != 1) {
25        Value * offset = iBuilder->CreateLoad(getBlockNo());
26        assert (offset);
27        if (index) {
28            offset = iBuilder->CreateAdd(offset, ConstantInt::get(offset->getType(), index));
29        }
30        if (bufferSize != 0) {
31            if (isPowerOfTwo(bufferSize)) {
32                offset = iBuilder->CreateAnd(offset, ConstantInt::get(offset->getType(), bufferSize - 1));
33            } else {
34                offset = iBuilder->CreateURem(offset, ConstantInt::get(offset->getType(), bufferSize));
35            }
36        }
37        addr = iBuilder->CreateGEP(base, offset);
38    }
39    return iBuilder->CreatePointerCast(addr, type->getPointerTo());
40}
41
42/** ------------------------------------------------------------------------------------------------------------- *
43 * @brief CreateDoBlockCall
44 ** ------------------------------------------------------------------------------------------------------------- */
45Value * Instance::CreateDoBlockCall() {
46    assert (mDefinition->getDoBlockFunction());
47    std::vector<Value *> params;
48    params.push_back(mKernelState);
49    if (mInputStreamSet) {
50        for (unsigned offset : mDefinition->getInputStreamOffsets()) {
51            params.push_back(getInputStreamSet(offset));
52        }
53    }
54    if (mOutputStreamSet) {
55        params.push_back(getOutputStreamSet());
56    }
57    return iBuilder->CreateCall(mDefinition->getDoBlockFunction(), params);
58}
59
60/** ------------------------------------------------------------------------------------------------------------- *
61 * @brief clearOutputStreamSet
62 ** ------------------------------------------------------------------------------------------------------------- */
63void Instance::clearOutputStreamSet() {
64    Value * ptr = getOutputStreamSet();
65    Type * type = ptr->getType();
66    assert(type->isPtrOrPtrVectorTy());
67    type = type->getPointerElementType();
68    assert(type->isStructTy() || type->isArrayTy());
69    iBuilder->CreateStore(Constant::getNullValue(type), ptr);
70}
71
72}
Note: See TracBrowser for help on using the repository browser.