source: icGREP/icgrep-devel/icgrep/kernels/kernel_builder.cpp @ 5497

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

Restore check-ins from the last several days

File size: 13.3 KB
Line 
1#include "kernel_builder.h"
2#include <kernels/kernel.h>
3#include <kernels/streamset.h>
4
5using namespace llvm;
6using namespace parabix;
7
8using Value = Value;
9
10namespace kernel {
11
12Value * KernelBuilder::getScalarFieldPtr(llvm::Value * instance, Value * const index) {
13    assert (instance);
14    CreateAssert(instance, "instance cannot be null!");
15    return CreateGEP(instance, {getInt32(0), index});
16}
17
18Value * KernelBuilder::getScalarFieldPtr(llvm::Value * instance, const std::string & fieldName) {
19    return getScalarFieldPtr(instance, getInt32(mKernel->getScalarIndex(fieldName)));
20}
21
22llvm::Value * KernelBuilder::getScalarFieldPtr(llvm::Value * index) {
23    return getScalarFieldPtr(mKernel->getInstance(), index);
24}
25
26llvm::Value *KernelBuilder:: getScalarFieldPtr(const std::string & fieldName) {
27    return getScalarFieldPtr(mKernel->getInstance(), fieldName);
28}
29
30Value * KernelBuilder::getScalarField(const std::string & fieldName) {
31    return CreateLoad(getScalarFieldPtr(fieldName), fieldName);
32}
33
34void KernelBuilder::setScalarField(const std::string & fieldName, Value * value) {
35    CreateStore(value, getScalarFieldPtr(fieldName));
36}
37
38Value * KernelBuilder::getStreamSetBufferPtr(const std::string & name) {
39    Value * const ptr = getScalarField(name + Kernel::BUFFER_PTR_SUFFIX);
40    CreateAssert(ptr, name + " cannot be null!");
41    return ptr;
42}
43
44LoadInst * KernelBuilder::acquireLogicalSegmentNo() {
45    return CreateAtomicLoadAcquire(getScalarFieldPtr(Kernel::LOGICAL_SEGMENT_NO_SCALAR));
46}
47
48void KernelBuilder::releaseLogicalSegmentNo(Value * nextSegNo) {
49    CreateAtomicStoreRelease(nextSegNo, getScalarFieldPtr(Kernel::LOGICAL_SEGMENT_NO_SCALAR));
50}
51
52Value * KernelBuilder::getProducedItemCount(const std::string & name, Value * doFinal) {
53    Kernel::Port port; unsigned index;
54    std::tie(port, index) = mKernel->getStreamPort(name);
55    assert (port == Kernel::Port::Output);
56    const auto rate = mKernel->getStreamOutput(index).rate;
57    if (rate.isExact()) {
58        const auto & refSet = rate.referenceStreamSet();
59        std::string principalField;
60        if (refSet.empty()) {
61            if (mKernel->getStreamInputs().empty()) {
62                principalField = mKernel->getStreamOutput(0).name + Kernel::PRODUCED_ITEM_COUNT_SUFFIX;
63            } else {
64                principalField = mKernel->getStreamInput(0).name + Kernel::PROCESSED_ITEM_COUNT_SUFFIX;
65            }
66        } else {
67            std::tie(port, index) = mKernel->getStreamPort(refSet);
68            if (port == Kernel::Port::Input) {
69               principalField = refSet + Kernel::PROCESSED_ITEM_COUNT_SUFFIX;
70            } else {
71               principalField = refSet + Kernel::PRODUCED_ITEM_COUNT_SUFFIX;
72            }
73        }
74        Value * const principleCount = getScalarField(principalField);
75        return rate.CreateRatioCalculation(this, principleCount, doFinal);
76    }
77    return getScalarField(name + Kernel::PRODUCED_ITEM_COUNT_SUFFIX);
78}
79
80Value * KernelBuilder::getProcessedItemCount(const std::string & name) {
81    Kernel::Port port; unsigned index;
82    std::tie(port, index) = mKernel->getStreamPort(name);
83    assert (port == Kernel::Port::Input);
84    const auto & rate = mKernel->getStreamInput(index).rate;
85    if (rate.isExact()) {
86        std::string refSet = rate.referenceStreamSet();
87        if (refSet.empty()) {
88            refSet = mKernel->getStreamInput(0).name;
89        }
90        Value * const principleCount = getScalarField(refSet + Kernel::PROCESSED_ITEM_COUNT_SUFFIX);
91        return rate.CreateRatioCalculation(this, principleCount);
92    }
93    return getScalarField(name + Kernel::PROCESSED_ITEM_COUNT_SUFFIX);
94}
95
96Value * KernelBuilder::getAvailableItemCount(const std::string & name) {
97    const auto & inputs = mKernel->getStreamInputs();
98    for (unsigned i = 0; i < inputs.size(); ++i) {
99        if (inputs[i].name == name) {
100            return mKernel->getAvailableItemCount(i);
101        }
102    }
103    return nullptr;
104}
105
106Value * KernelBuilder::getConsumedItemCount(const std::string & name) {
107    return getScalarField(name + Kernel::CONSUMED_ITEM_COUNT_SUFFIX);
108}
109
110void KernelBuilder::setProducedItemCount(const std::string & name, Value * value) {
111    setScalarField(name + Kernel::PRODUCED_ITEM_COUNT_SUFFIX, value);
112}
113
114void KernelBuilder::setProcessedItemCount(const std::string & name, Value * value) {
115    setScalarField(name + Kernel::PROCESSED_ITEM_COUNT_SUFFIX, value);
116}
117
118void KernelBuilder::setConsumedItemCount(const std::string & name, Value * value) {
119    setScalarField(name + Kernel::CONSUMED_ITEM_COUNT_SUFFIX, value);
120}
121
122Value * KernelBuilder::getTerminationSignal() {
123    return getScalarField(Kernel::TERMINATION_SIGNAL);
124}
125
126void KernelBuilder::setTerminationSignal() {
127    setScalarField(Kernel::TERMINATION_SIGNAL, getTrue());
128}
129
130Value * KernelBuilder::getLinearlyAccessibleItems(const std::string & name, Value * fromPosition) {
131    const StreamSetBuffer * const buf = mKernel->getInputStreamSetBuffer(name);
132    return buf->getLinearlyAccessibleItems(this, fromPosition);
133}
134
135Value * KernelBuilder::getConsumerLock(const std::string & name) {
136    return getScalarField(name + Kernel::CONSUMER_SUFFIX);
137}
138
139void KernelBuilder::setConsumerLock(const std::string & name, Value * value) {
140    setScalarField(name + Kernel::CONSUMER_SUFFIX, value);
141}
142
143inline Value * KernelBuilder::computeBlockIndex(Value * itemCount) {
144    const auto divisor = getBitBlockWidth();
145    if (LLVM_LIKELY((divisor & (divisor - 1)) == 0)) {
146        return CreateLShr(itemCount, std::log2(divisor));
147    } else {
148        return CreateUDiv(itemCount, getSize(divisor));
149    }
150}
151
152Value * KernelBuilder::getInputStreamBlockPtr(const std::string & name, Value * streamIndex) {
153    Value * const blockIndex = computeBlockIndex(getProcessedItemCount(name));
154    const StreamSetBuffer * const buf = mKernel->getInputStreamSetBuffer(name);
155    return buf->getStreamBlockPtr(this, getStreamSetBufferPtr(name), streamIndex, blockIndex, true);
156}
157
158Value * KernelBuilder::loadInputStreamBlock(const std::string & name, Value * streamIndex) {
159    return CreateBlockAlignedLoad(getInputStreamBlockPtr(name, streamIndex));
160}
161
162Value * KernelBuilder::getInputStreamPackPtr(const std::string & name, Value * streamIndex, Value * packIndex) {
163    Value * const blockIndex = computeBlockIndex(getProcessedItemCount(name));
164    const StreamSetBuffer * const buf = mKernel->getInputStreamSetBuffer(name);
165    return buf->getStreamPackPtr(this, getStreamSetBufferPtr(name), streamIndex, blockIndex, packIndex, true);
166}
167
168Value * KernelBuilder::loadInputStreamPack(const std::string & name, Value * streamIndex, Value * packIndex) {
169    return CreateBlockAlignedLoad(getInputStreamPackPtr(name, streamIndex, packIndex));
170}
171
172Value * KernelBuilder::getInputStreamSetCount(const std::string & name) {
173    const StreamSetBuffer * const buf = mKernel->getInputStreamSetBuffer(name);
174    return buf->getStreamSetCount(this, getStreamSetBufferPtr(name));
175}
176
177Value * KernelBuilder::getAdjustedInputStreamBlockPtr(Value * blockAdjustment, const std::string & name, Value * streamIndex) {
178    Value * const blockIndex = CreateAdd(computeBlockIndex(getProcessedItemCount(name)), blockAdjustment);
179    const StreamSetBuffer * const buf = mKernel->getInputStreamSetBuffer(name);
180    return buf->getStreamBlockPtr(this, getStreamSetBufferPtr(name), streamIndex, blockIndex, true);
181}
182
183Value * KernelBuilder::getOutputStreamBlockPtr(const std::string & name, Value * streamIndex) {
184    Value * const blockIndex = computeBlockIndex(getProducedItemCount(name));
185    const StreamSetBuffer * const buf = mKernel->getOutputStreamSetBuffer(name);
186    return buf->getStreamBlockPtr(this, getStreamSetBufferPtr(name), streamIndex, blockIndex, false);
187}
188
189StoreInst * KernelBuilder::storeOutputStreamBlock(const std::string & name, Value * streamIndex, Value * toStore) {
190    return CreateBlockAlignedStore(toStore, getOutputStreamBlockPtr(name, streamIndex));
191}
192
193Value * KernelBuilder::getOutputStreamPackPtr(const std::string & name, Value * streamIndex, Value * packIndex) {
194    Value * const blockIndex = computeBlockIndex(getProducedItemCount(name));
195    const StreamSetBuffer * const buf = mKernel->getOutputStreamSetBuffer(name);
196    return buf->getStreamPackPtr(this, getStreamSetBufferPtr(name), streamIndex, blockIndex, packIndex, false);
197}
198
199StoreInst * KernelBuilder::storeOutputStreamPack(const std::string & name, Value * streamIndex, Value * packIndex, Value * toStore) {
200    return CreateBlockAlignedStore(toStore, getOutputStreamPackPtr(name, streamIndex, packIndex));
201}
202
203Value * KernelBuilder::getOutputStreamSetCount(const std::string & name) {
204    const StreamSetBuffer * const buf = mKernel->getOutputStreamSetBuffer(name);
205    return buf->getStreamSetCount(this, getStreamSetBufferPtr(name));
206}
207
208Value * KernelBuilder::getRawInputPointer(const std::string & name, Value * streamIndex, Value * absolutePosition) {
209    const StreamSetBuffer * const buf = mKernel->getInputStreamSetBuffer(name);
210    return buf->getRawItemPointer(this, getStreamSetBufferPtr(name), streamIndex, absolutePosition);
211}
212
213Value * KernelBuilder::getRawOutputPointer(const std::string & name, Value * streamIndex, Value * absolutePosition) {
214    const StreamSetBuffer * const buf = mKernel->getOutputStreamSetBuffer(name);
215    return buf->getRawItemPointer(this, getStreamSetBufferPtr(name), streamIndex, absolutePosition);
216}
217
218Value * KernelBuilder::getBaseAddress(const std::string & name) {
219    return mKernel->getAnyStreamSetBuffer(name)->getBaseAddress(this, getStreamSetBufferPtr(name));
220}
221
222void KernelBuilder::setBaseAddress(const std::string & name, Value * const addr) {
223    return mKernel->getAnyStreamSetBuffer(name)->setBaseAddress(this, getStreamSetBufferPtr(name), addr);
224}
225
226Value * KernelBuilder::getBufferedSize(const std::string & name) {
227    return mKernel->getAnyStreamSetBuffer(name)->getBufferedSize(this, getStreamSetBufferPtr(name));
228}
229
230void KernelBuilder::setBufferedSize(const std::string & name, Value * size) {
231    mKernel->getAnyStreamSetBuffer(name)->setBufferedSize(this, getStreamSetBufferPtr(name), size);
232}
233
234
235CallInst * KernelBuilder::createDoSegmentCall(const std::vector<Value *> & args) {
236    Function * const doSegment = mKernel->getDoSegmentFunction(getModule());
237    assert (doSegment->getArgumentList().size() == args.size());
238    return CreateCall(doSegment, args);
239}
240
241Value * KernelBuilder::getAccumulator(const std::string & accumName) {
242    auto results = mKernel->mOutputScalarResult;
243    if (LLVM_UNLIKELY(results == nullptr)) {
244        report_fatal_error("Cannot get accumulator " + accumName + " until " + mKernel->getName() + " has terminated.");
245    }
246    const auto & outputs = mKernel->getScalarOutputs();
247    const auto n = outputs.size();
248    if (LLVM_UNLIKELY(n == 0)) {
249        report_fatal_error(mKernel->getName() + " has no output scalars.");
250    } else {
251        for (unsigned i = 0; i < n; ++i) {
252            const Binding & b = outputs[i];
253            if (b.name == accumName) {
254                if (n == 1) {
255                    return results;
256                } else {
257                    return CreateExtractValue(results, {i});
258                }
259            }
260        }
261        report_fatal_error(mKernel->getName() + " has no output scalar named " + accumName);
262    }
263}
264
265BasicBlock * KernelBuilder::CreateConsumerWait() {
266    const auto consumers = mKernel->getStreamOutputs();
267    BasicBlock * const entry = GetInsertBlock();
268    if (consumers.empty()) {
269        return entry;
270    } else {
271        Function * const parent = entry->getParent();
272        IntegerType * const sizeTy = getSizeTy();
273        ConstantInt * const zero = getInt32(0);
274        ConstantInt * const one = getInt32(1);
275        ConstantInt * const size0 = getSize(0);
276
277        Value * const segNo = acquireLogicalSegmentNo();
278        const auto n = consumers.size();
279        BasicBlock * load[n + 1];
280        BasicBlock * wait[n];
281        for (unsigned i = 0; i < n; ++i) {
282            load[i] = BasicBlock::Create(getContext(), consumers[i].name + "Load", parent);
283            wait[i] = BasicBlock::Create(getContext(), consumers[i].name + "Wait", parent);
284        }
285        load[n] = BasicBlock::Create(getContext(), "Resume", parent);
286        CreateBr(load[0]);
287        for (unsigned i = 0; i < n; ++i) {
288
289            SetInsertPoint(load[i]);
290            Value * const outputConsumers = getConsumerLock(consumers[i].name);
291
292            Value * const consumerCount = CreateLoad(CreateGEP(outputConsumers, {zero, zero}));
293            Value * const consumerPtr = CreateLoad(CreateGEP(outputConsumers, {zero, one}));
294            Value * const noConsumers = CreateICmpEQ(consumerCount, size0);
295            CreateUnlikelyCondBr(noConsumers, load[i + 1], wait[i]);
296
297            SetInsertPoint(wait[i]);
298            PHINode * const consumerPhi = CreatePHI(sizeTy, 2);
299            consumerPhi->addIncoming(size0, load[i]);
300
301            Value * const conSegPtr = CreateLoad(CreateGEP(consumerPtr, consumerPhi));
302            Value * const processedSegmentCount = CreateAtomicLoadAcquire(conSegPtr);
303            Value * const ready = CreateICmpEQ(segNo, processedSegmentCount);
304            assert (ready->getType() == getInt1Ty());
305            Value * const nextConsumerIdx = CreateAdd(consumerPhi, CreateZExt(ready, sizeTy));
306            consumerPhi->addIncoming(nextConsumerIdx, wait[i]);
307            Value * const next = CreateICmpEQ(nextConsumerIdx, consumerCount);
308            CreateCondBr(next, load[i + 1], wait[i]);
309        }
310
311        BasicBlock * const exit = load[n];
312        SetInsertPoint(exit);
313        return exit;
314    }
315}
316
317}
Note: See TracBrowser for help on using the repository browser.