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

Last change on this file since 5637 was 5637, checked in by cameron, 18 months ago

Trace option for produced, processed, consumed item counts

File size: 15.3 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) {
132    Kernel::Port port; unsigned index;
133    std::tie(port, index) = mKernel->getStreamPort(name);
134    if (port == Kernel::Port::Input) {
135        const StreamSetBuffer * const buf = mKernel->getInputStreamSetBuffer(name);
136        return buf->getLinearlyAccessibleItems(this, getStreamSetBufferPtr(name), fromPosition);
137    }
138    else {
139        const StreamSetBuffer * const buf = mKernel->getOutputStreamSetBuffer(name);
140        return buf->getLinearlyAccessibleItems(this, getStreamSetBufferPtr(name), fromPosition);
141    }
142}
143
144Value * KernelBuilder::getLinearlyAccessibleBlocks(const std::string & name, Value * fromBlock) {
145    Kernel::Port port; unsigned index;
146    std::tie(port, index) = mKernel->getStreamPort(name);
147    if (port == Kernel::Port::Input) {
148        const StreamSetBuffer * const buf = mKernel->getInputStreamSetBuffer(name);
149        return buf->getLinearlyAccessibleBlocks(this, getStreamSetBufferPtr(name), fromBlock);
150    } else {
151        const StreamSetBuffer * const buf = mKernel->getOutputStreamSetBuffer(name);
152        return buf->getLinearlyAccessibleBlocks(this, getStreamSetBufferPtr(name), fromBlock);
153    }
154}
155
156Value * KernelBuilder::getLinearlyWritableItems(const std::string & name, Value * fromPosition) {
157    const StreamSetBuffer * const buf = mKernel->getOutputStreamSetBuffer(name);
158    return buf->getLinearlyWritableItems(this, getStreamSetBufferPtr(name), fromPosition);
159}
160
161Value * KernelBuilder::getLinearlyWritableBlocks(const std::string & name, Value * fromBlock) {
162    const StreamSetBuffer * const buf = mKernel->getOutputStreamSetBuffer(name);
163    return buf->getLinearlyWritableBlocks(this, getStreamSetBufferPtr(name), fromBlock);
164}
165
166Value * KernelBuilder::getConsumerLock(const std::string & name) {
167    return getScalarField(name + Kernel::CONSUMER_SUFFIX);
168}
169
170void KernelBuilder::setConsumerLock(const std::string & name, Value * value) {
171    setScalarField(name + Kernel::CONSUMER_SUFFIX, value);
172}
173
174inline Value * KernelBuilder::computeBlockIndex(Value * itemCount) {
175    const auto divisor = getBitBlockWidth();
176    if (LLVM_LIKELY((divisor & (divisor - 1)) == 0)) {
177        return CreateLShr(itemCount, std::log2(divisor));
178    } else {
179        return CreateUDiv(itemCount, getSize(divisor));
180    }
181}
182
183Value * KernelBuilder::getInputStreamBlockPtr(const std::string & name, Value * streamIndex) {
184    Value * const blockIndex = computeBlockIndex(getProcessedItemCount(name));
185    const StreamSetBuffer * const buf = mKernel->getInputStreamSetBuffer(name);
186    return buf->getStreamBlockPtr(this, getStreamSetBufferPtr(name), streamIndex, blockIndex, true);
187}
188
189Value * KernelBuilder::loadInputStreamBlock(const std::string & name, Value * streamIndex) {
190    return CreateBlockAlignedLoad(getInputStreamBlockPtr(name, streamIndex));
191}
192
193Value * KernelBuilder::getInputStreamPackPtr(const std::string & name, Value * streamIndex, Value * packIndex) {
194    Value * const blockIndex = computeBlockIndex(getProcessedItemCount(name));
195    const StreamSetBuffer * const buf = mKernel->getInputStreamSetBuffer(name);
196    return buf->getStreamPackPtr(this, getStreamSetBufferPtr(name), streamIndex, blockIndex, packIndex, true);
197}
198
199Value * KernelBuilder::loadInputStreamPack(const std::string & name, Value * streamIndex, Value * packIndex) {
200    return CreateBlockAlignedLoad(getInputStreamPackPtr(name, streamIndex, packIndex));
201}
202
203Value * KernelBuilder::getInputStreamSetCount(const std::string & name) {
204    const StreamSetBuffer * const buf = mKernel->getInputStreamSetBuffer(name);
205    return buf->getStreamSetCount(this, getStreamSetBufferPtr(name));
206}
207
208Value * KernelBuilder::getAdjustedInputStreamBlockPtr(Value * blockAdjustment, const std::string & name, Value * streamIndex) {
209    Value * const blockIndex = CreateAdd(computeBlockIndex(getProcessedItemCount(name)), blockAdjustment);
210    const StreamSetBuffer * const buf = mKernel->getInputStreamSetBuffer(name);
211    return buf->getStreamBlockPtr(this, getStreamSetBufferPtr(name), streamIndex, blockIndex, true);
212}
213
214Value * KernelBuilder::getOutputStreamBlockPtr(const std::string & name, Value * streamIndex) {
215    Value * const blockIndex = computeBlockIndex(getProducedItemCount(name));
216    const StreamSetBuffer * const buf = mKernel->getOutputStreamSetBuffer(name);
217    return buf->getStreamBlockPtr(this, getStreamSetBufferPtr(name), streamIndex, blockIndex, false);
218}
219
220StoreInst * KernelBuilder::storeOutputStreamBlock(const std::string & name, Value * streamIndex, Value * toStore) {
221    return CreateBlockAlignedStore(toStore, getOutputStreamBlockPtr(name, streamIndex));
222}
223
224Value * KernelBuilder::getOutputStreamPackPtr(const std::string & name, Value * streamIndex, Value * packIndex) {
225    Value * const blockIndex = computeBlockIndex(getProducedItemCount(name));
226    const StreamSetBuffer * const buf = mKernel->getOutputStreamSetBuffer(name);
227    return buf->getStreamPackPtr(this, getStreamSetBufferPtr(name), streamIndex, blockIndex, packIndex, false);
228}
229
230StoreInst * KernelBuilder::storeOutputStreamPack(const std::string & name, Value * streamIndex, Value * packIndex, Value * toStore) {
231    return CreateBlockAlignedStore(toStore, getOutputStreamPackPtr(name, streamIndex, packIndex));
232}
233
234Value * KernelBuilder::getOutputStreamSetCount(const std::string & name) {
235    const StreamSetBuffer * const buf = mKernel->getOutputStreamSetBuffer(name);
236    return buf->getStreamSetCount(this, getStreamSetBufferPtr(name));
237}
238
239Value * KernelBuilder::getRawInputPointer(const std::string & name, Value * streamIndex, Value * absolutePosition) {
240    const StreamSetBuffer * const buf = mKernel->getInputStreamSetBuffer(name);
241    return buf->getRawItemPointer(this, getStreamSetBufferPtr(name), streamIndex, absolutePosition);
242}
243
244Value * KernelBuilder::getRawOutputPointer(const std::string & name, Value * streamIndex, Value * absolutePosition) {
245    const StreamSetBuffer * const buf = mKernel->getOutputStreamSetBuffer(name);
246    return buf->getRawItemPointer(this, getStreamSetBufferPtr(name), streamIndex, absolutePosition);
247}
248
249Value * KernelBuilder::getBaseAddress(const std::string & name) {
250    return mKernel->getAnyStreamSetBuffer(name)->getBaseAddress(this, getStreamSetBufferPtr(name));
251}
252
253void KernelBuilder::setBaseAddress(const std::string & name, Value * const addr) {
254    return mKernel->getAnyStreamSetBuffer(name)->setBaseAddress(this, getStreamSetBufferPtr(name), addr);
255}
256
257Value * KernelBuilder::getBufferedSize(const std::string & name) {
258    return mKernel->getAnyStreamSetBuffer(name)->getBufferedSize(this, getStreamSetBufferPtr(name));
259}
260
261void KernelBuilder::setBufferedSize(const std::string & name, Value * size) {
262    mKernel->getAnyStreamSetBuffer(name)->setBufferedSize(this, getStreamSetBufferPtr(name), size);
263}
264
265
266Value * KernelBuilder::getCapacity(const std::string & name) {
267    return mKernel->getAnyStreamSetBuffer(name)->getCapacity(this, getStreamSetBufferPtr(name));
268}
269
270void KernelBuilder::setCapacity(const std::string & name, Value * c) {
271    mKernel->getAnyStreamSetBuffer(name)->setCapacity(this, getStreamSetBufferPtr(name), c);
272}
273
274   
275CallInst * KernelBuilder::createDoSegmentCall(const std::vector<Value *> & args) {
276    Function * const doSegment = mKernel->getDoSegmentFunction(getModule());
277    assert (doSegment->getArgumentList().size() == args.size());
278    return CreateCall(doSegment, args);
279}
280
281Value * KernelBuilder::getAccumulator(const std::string & accumName) {
282    auto results = mKernel->mOutputScalarResult;
283    if (LLVM_UNLIKELY(results == nullptr)) {
284        report_fatal_error("Cannot get accumulator " + accumName + " until " + mKernel->getName() + " has terminated.");
285    }
286    const auto & outputs = mKernel->getScalarOutputs();
287    const auto n = outputs.size();
288    if (LLVM_UNLIKELY(n == 0)) {
289        report_fatal_error(mKernel->getName() + " has no output scalars.");
290    } else {
291        for (unsigned i = 0; i < n; ++i) {
292            const Binding & b = outputs[i];
293            if (b.name == accumName) {
294                if (n == 1) {
295                    return results;
296                } else {
297                    return CreateExtractValue(results, {i});
298                }
299            }
300        }
301        report_fatal_error(mKernel->getName() + " has no output scalar named " + accumName);
302    }
303}
304
305BasicBlock * KernelBuilder::CreateConsumerWait() {
306    const auto consumers = mKernel->getStreamOutputs();
307    BasicBlock * const entry = GetInsertBlock();
308    if (consumers.empty()) {
309        return entry;
310    } else {
311        Function * const parent = entry->getParent();
312        IntegerType * const sizeTy = getSizeTy();
313        ConstantInt * const zero = getInt32(0);
314        ConstantInt * const one = getInt32(1);
315        ConstantInt * const size0 = getSize(0);
316
317        Value * const segNo = acquireLogicalSegmentNo();
318        const auto n = consumers.size();
319        BasicBlock * load[n + 1];
320        BasicBlock * wait[n];
321        for (unsigned i = 0; i < n; ++i) {
322            load[i] = BasicBlock::Create(getContext(), consumers[i].name + "Load", parent);
323            wait[i] = BasicBlock::Create(getContext(), consumers[i].name + "Wait", parent);
324        }
325        load[n] = BasicBlock::Create(getContext(), "Resume", parent);
326        CreateBr(load[0]);
327        for (unsigned i = 0; i < n; ++i) {
328
329            SetInsertPoint(load[i]);
330            Value * const outputConsumers = getConsumerLock(consumers[i].name);
331
332            Value * const consumerCount = CreateLoad(CreateGEP(outputConsumers, {zero, zero}));
333            Value * const consumerPtr = CreateLoad(CreateGEP(outputConsumers, {zero, one}));
334            Value * const noConsumers = CreateICmpEQ(consumerCount, size0);
335            CreateUnlikelyCondBr(noConsumers, load[i + 1], wait[i]);
336
337            SetInsertPoint(wait[i]);
338            PHINode * const consumerPhi = CreatePHI(sizeTy, 2);
339            consumerPhi->addIncoming(size0, load[i]);
340
341            Value * const conSegPtr = CreateLoad(CreateGEP(consumerPtr, consumerPhi));
342            Value * const processedSegmentCount = CreateAtomicLoadAcquire(conSegPtr);
343            Value * const ready = CreateICmpEQ(segNo, processedSegmentCount);
344            assert (ready->getType() == getInt1Ty());
345            Value * const nextConsumerIdx = CreateAdd(consumerPhi, CreateZExt(ready, sizeTy));
346            consumerPhi->addIncoming(nextConsumerIdx, wait[i]);
347            Value * const next = CreateICmpEQ(nextConsumerIdx, consumerCount);
348            CreateCondBr(next, load[i + 1], wait[i]);
349        }
350
351        BasicBlock * const exit = load[n];
352        SetInsertPoint(exit);
353        return exit;
354    }
355}
356
357}
Note: See TracBrowser for help on using the repository browser.