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

Last change on this file since 5678 was 5650, checked in by cameron, 21 months ago

Multiblock kernel builder support for ExternalBuffer? inputs; clean-up

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