source: icGREP/icgrep-devel/icgrep/kernels/kernel.cpp @ 5310

Last change on this file since 5310 was 5310, checked in by nmedfort, 3 years ago

Adjusted pablo compiler to use getInputStream and getOutputStream when accessing packed stream fields.

File size: 24.2 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#include "kernel.h"
7#include <toolchain.h>
8#include <kernels/streamset.h>
9#include <llvm/IR/Constants.h>
10#include <llvm/IR/Function.h>
11#include <llvm/IR/Instructions.h>
12#include <llvm/IR/Module.h>
13#include <llvm/Support/raw_ostream.h>
14
15static const auto BLOCK_NO_SCALAR = "blockNo";
16
17static const auto DO_BLOCK_SUFFIX = "_DoBlock";
18
19static const auto FINAL_BLOCK_SUFFIX = "_FinalBlock";
20
21static const auto LOGICAL_SEGMENT_NO_SCALAR = "logicalSegNo";
22
23static const auto PROCESSED_ITEM_COUNT_SUFFIX = "_processedItemCount";
24
25static const auto PRODUCED_ITEM_COUNT_SUFFIX = "_producedItemCount";
26
27static const auto TERMINATION_SIGNAL = "terminationSignal";
28
29static const auto BUFFER_PTR_SUFFIX = "_bufferPtr";
30
31static const auto BLOCK_MASK_SUFFIX = "_blkMask";
32
33using namespace llvm;
34using namespace kernel;
35using namespace parabix;
36
37unsigned KernelBuilder::addScalar(Type * const type, const std::string & name) {
38    if (LLVM_UNLIKELY(mKernelStateType != nullptr)) {
39        report_fatal_error("Cannot add kernel field " + name + " after kernel state finalized");
40    }
41    if (LLVM_UNLIKELY(mKernelMap.count(name))) {
42        report_fatal_error("Kernel already contains field " + name);
43    }
44    const auto index = mKernelFields.size();
45    mKernelMap.emplace(name, index);
46    mKernelFields.push_back(type);
47    return index;
48}
49
50unsigned KernelBuilder::addUnnamedScalar(Type * const type) {
51    if (LLVM_UNLIKELY(mKernelStateType != nullptr)) {
52        report_fatal_error("Cannot add unnamed kernel field after kernel state finalized");
53    }
54    const auto index = mKernelFields.size();
55    mKernelFields.push_back(type);
56    return index;
57}
58
59void KernelBuilder::prepareKernelSignature() {
60    for (unsigned i = 0; i < mStreamSetInputs.size(); i++) {
61        mStreamSetNameMap.emplace(mStreamSetInputs[i].name, i);
62    }
63    for (unsigned i = 0; i < mStreamSetOutputs.size(); i++) {
64        mStreamSetNameMap.emplace(mStreamSetOutputs[i].name, i);
65    }
66}
67   
68void KernelBuilder::prepareKernel() {
69    if (LLVM_UNLIKELY(mKernelStateType != nullptr)) {
70        report_fatal_error("Cannot prepare kernel after kernel state finalized");
71    }
72    if (mStreamSetInputs.size() != mStreamSetInputBuffers.size()) {
73        std::string tmp;
74        raw_string_ostream out(tmp);
75        out << "kernel contains " << mStreamSetInputBuffers.size() << " input buffers for "
76        << mStreamSetInputs.size() << " input stream sets.";
77        report_fatal_error(out.str());
78    }
79    if (mStreamSetOutputs.size() != mStreamSetOutputBuffers.size()) {
80        std::string tmp;
81        raw_string_ostream out(tmp);
82        out << "kernel contains " << mStreamSetOutputBuffers.size() << " output buffers for "
83        << mStreamSetOutputs.size() << " output stream sets.";
84        report_fatal_error(out.str());
85    }
86    const auto blockSize = iBuilder->getBitBlockWidth();
87    for (unsigned i = 0; i < mStreamSetInputs.size(); i++) {
88        if ((mStreamSetInputBuffers[i]->getBufferBlocks() > 0) && (mStreamSetInputBuffers[i]->getBufferBlocks() < codegen::SegmentSize + (blockSize + mLookAheadPositions - 1)/blockSize)) {
89            report_fatal_error("Kernel preparation: Buffer size too small " + mStreamSetInputs[i].name);
90        }
91        mScalarInputs.emplace_back(mStreamSetInputBuffers[i]->getPointerType(), mStreamSetInputs[i].name + BUFFER_PTR_SUFFIX);
92        addScalar(iBuilder->getSizeTy(), mStreamSetInputs[i].name + PROCESSED_ITEM_COUNT_SUFFIX);
93    }
94    for (unsigned i = 0; i < mStreamSetOutputs.size(); i++) {
95        mScalarInputs.emplace_back(mStreamSetOutputBuffers[i]->getPointerType(), mStreamSetOutputs[i].name + BUFFER_PTR_SUFFIX);
96        addScalar(iBuilder->getSizeTy(), mStreamSetOutputs[i].name + PRODUCED_ITEM_COUNT_SUFFIX);
97    }
98    for (const auto binding : mScalarInputs) {
99        addScalar(binding.type, binding.name);
100    }
101    for (const auto binding : mScalarOutputs) {
102        addScalar(binding.type, binding.name);
103    }
104    if (mStreamSetNameMap.empty()) {
105        prepareKernelSignature();
106    }
107    for (auto binding : mInternalScalars) {
108        addScalar(binding.type, binding.name);
109    }
110    addScalar(iBuilder->getSizeTy(), BLOCK_NO_SCALAR);
111    addScalar(iBuilder->getSizeTy(), LOGICAL_SEGMENT_NO_SCALAR);
112    addScalar(iBuilder->getInt1Ty(), TERMINATION_SIGNAL);
113    mKernelStateType = StructType::create(iBuilder->getContext(), mKernelFields, getName());
114}
115
116std::unique_ptr<Module> KernelBuilder::createKernelModule(const std::vector<StreamSetBuffer *> & inputs, const std::vector<StreamSetBuffer *> & outputs) {
117    auto saveModule = iBuilder->getModule();
118    auto savePoint = iBuilder->saveIP();
119    auto module = make_unique<Module>(getName() + "_" + iBuilder->getBitBlockTypeName(), iBuilder->getContext());
120    iBuilder->setModule(module.get());
121    generateKernel(inputs, outputs);
122    iBuilder->setModule(saveModule);
123    iBuilder->restoreIP(savePoint);
124    return module;
125}
126
127void KernelBuilder::generateKernel(const std::vector<StreamSetBuffer *> & inputs, const std::vector<StreamSetBuffer *> & outputs) {
128    auto savePoint = iBuilder->saveIP();
129    Module * const m = iBuilder->getModule();
130    mStreamSetInputBuffers.assign(inputs.begin(), inputs.end());
131    mStreamSetOutputBuffers.assign(outputs.begin(), outputs.end());
132    prepareKernel(); // possibly overridden by the KernelBuilder subtype
133    addKernelDeclarations(m);
134    callGenerateInitMethod();
135    generateInternalMethods();
136    callGenerateDoSegmentMethod();
137    // Implement the accumulator get functions
138    for (auto binding : mScalarOutputs) {
139        Function * f = getAccumulatorFunction(binding.name);
140        iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "get_" + binding.name, f));
141        Value * self = &*(f->arg_begin());
142        Value * ptr = iBuilder->CreateGEP(self, {iBuilder->getInt32(0), getScalarIndex(binding.name)});
143        Value * retVal = iBuilder->CreateLoad(ptr);
144        iBuilder->CreateRet(retVal);
145    }
146    iBuilder->restoreIP(savePoint);
147}
148
149void KernelBuilder::callGenerateDoSegmentMethod() {
150    mCurrentFunction = getDoSegmentFunction();
151    iBuilder->SetInsertPoint(CreateBasicBlock(getName() + "_entry"));
152    auto args = mCurrentFunction->arg_begin();
153    mSelf = &*(args++);
154    Value * doFinal = &*(args++);
155    std::vector<Value *> producerPos;
156    for (unsigned i = 0; i < mStreamSetInputs.size(); i++) {
157        producerPos.push_back(&*(args++));
158    }
159    assert (args == mCurrentFunction->arg_end());
160    generateDoSegmentMethod(doFinal, producerPos); // must be overridden by the KernelBuilder subtype
161    iBuilder->CreateRetVoid();
162}
163
164void KernelBuilder::callGenerateInitMethod() {
165    mCurrentFunction = getInitFunction();
166    iBuilder->SetInsertPoint(CreateBasicBlock("Init_entry"));
167    Function::arg_iterator args = mCurrentFunction->arg_begin();
168    mSelf = &*(args++);
169    iBuilder->CreateStore(ConstantAggregateZero::get(mKernelStateType), mSelf);
170    for (auto binding : mScalarInputs) {
171        Value * param = &*(args++);
172        Value * ptr = iBuilder->CreateGEP(mSelf, {iBuilder->getInt32(0), getScalarIndex(binding.name)});
173        iBuilder->CreateStore(param, ptr);
174    }
175    generateInitMethod();
176    iBuilder->CreateRetVoid();
177}
178
179ConstantInt * KernelBuilder::getScalarIndex(const std::string & name) const {
180    const auto f = mKernelMap.find(name);
181    if (LLVM_UNLIKELY(f == mKernelMap.end())) {
182        report_fatal_error("Kernel does not contain scalar: " + name);
183    }
184    return iBuilder->getInt32(f->second);
185}
186
187unsigned KernelBuilder::getScalarCount() const {
188    return mKernelFields.size();
189}
190
191Value * KernelBuilder::getScalarFieldPtr(Value * instance, Value * index) const {
192    return iBuilder->CreateGEP(instance, {iBuilder->getInt32(0), index});
193}
194
195Value * KernelBuilder::getScalarFieldPtr(Value * instance, const std::string & fieldName) const {
196    return getScalarFieldPtr(instance, getScalarIndex(fieldName));
197}
198
199Value * KernelBuilder::getScalarField(Value * instance, const std::string & fieldName) const {
200    return iBuilder->CreateLoad(getScalarFieldPtr(instance, fieldName));
201}
202
203Value * KernelBuilder::getScalarField(Value * instance, Value * index) const {
204    return iBuilder->CreateLoad(getScalarFieldPtr(instance, index));
205}
206
207void KernelBuilder::setScalarField(Value * instance, const std::string & fieldName, Value * value) const {
208    iBuilder->CreateStore(value, getScalarFieldPtr(instance, fieldName));
209}
210
211void KernelBuilder::setScalarField(Value * instance, Value * index, Value * value) const {
212    iBuilder->CreateStore(value, getScalarFieldPtr(instance, index));
213}
214
215Value * KernelBuilder::getProcessedItemCount(Value * instance, const std::string & name) const {
216    return getScalarField(instance, name + PROCESSED_ITEM_COUNT_SUFFIX);
217}
218
219Value * KernelBuilder::getProducedItemCount(Value * instance, const std::string & name) const {
220    return getScalarField(instance, name + PRODUCED_ITEM_COUNT_SUFFIX);
221}
222
223void KernelBuilder::setProcessedItemCount(Value * instance, const std::string & name, Value * value) const {
224    setScalarField(instance, name + PROCESSED_ITEM_COUNT_SUFFIX, value);
225}
226
227void KernelBuilder::setProducedItemCount(Value * instance, const std::string & name, Value * value) const {
228    setScalarField(instance, name + PRODUCED_ITEM_COUNT_SUFFIX, value);
229}
230
231Value * KernelBuilder::getTerminationSignal(Value * instance) const {
232    return getScalarField(instance, TERMINATION_SIGNAL);
233}
234
235void KernelBuilder::setTerminationSignal(Value * instance) const {
236    setScalarField(instance, TERMINATION_SIGNAL, iBuilder->getInt1(true));
237}
238
239LoadInst * KernelBuilder::acquireLogicalSegmentNo(Value * instance) const {
240    return iBuilder->CreateAtomicLoadAcquire(getScalarFieldPtr(instance, LOGICAL_SEGMENT_NO_SCALAR));
241}
242
243void KernelBuilder::releaseLogicalSegmentNo(Value * instance, Value * newCount) const {
244    iBuilder->CreateAtomicStoreRelease(newCount, getScalarFieldPtr(instance, LOGICAL_SEGMENT_NO_SCALAR));
245}
246
247Value * KernelBuilder::getBlockNo() const {
248    return getScalarField(mSelf, BLOCK_NO_SCALAR);
249}
250
251void KernelBuilder::setBlockNo(Value * value) const {
252    setScalarField(mSelf, BLOCK_NO_SCALAR, value);
253}
254
255inline Value * KernelBuilder::computeBlockIndex(const std::vector<Binding> & bindings, const std::string & name, Value * itemCount) const {
256    for (const Binding & b : bindings) {
257        if (b.name == name) {
258            const auto divisor = (b.step == 0) ? iBuilder->getBitBlockWidth() : b.step;
259            if (LLVM_LIKELY((divisor & (divisor - 1)) == 0)) {
260                return iBuilder->CreateLShr(itemCount, std::log2(divisor));
261            } else {
262                return iBuilder->CreateUDiv(itemCount, iBuilder->getSize(divisor));
263            }
264        }
265    }
266    report_fatal_error("Error: no binding in " + getName() + " for " + name);
267}
268
269Value * KernelBuilder::getInputStream(const std::string & name, Value * streamIndex) const {
270    Value * const blockIndex = computeBlockIndex(mStreamSetInputs, name, getProcessedItemCount(name));
271    const StreamSetBuffer * const buf = getInputStreamSetBuffer(name);
272    return buf->getStream(getStreamSetBufferPtr(name), streamIndex, blockIndex);
273}
274
275Value * KernelBuilder::getInputStream(const std::string & name, Value * streamIndex, Value * packIndex) const {
276    Value * const blockIndex = computeBlockIndex(mStreamSetInputs, name, getProcessedItemCount(name));
277    const StreamSetBuffer * const buf = getInputStreamSetBuffer(name);
278    return buf->getStream(getStreamSetBufferPtr(name), streamIndex, blockIndex, packIndex);
279}
280
281Value * KernelBuilder::getOutputStream(const std::string & name, Value * streamIndex) const {
282    Value * const blockIndex = computeBlockIndex(mStreamSetOutputs, name, getProducedItemCount(name));
283    const StreamSetBuffer * const buf = getOutputStreamSetBuffer(name);
284    return buf->getStream(getStreamSetBufferPtr(name), streamIndex, blockIndex);
285}
286
287Value * KernelBuilder::getOutputStream(const std::string & name, Value * streamIndex, Value * packIndex) const {
288    Value * const blockIndex = computeBlockIndex(mStreamSetOutputs, name, getProducedItemCount(name));
289    const StreamSetBuffer * const buf = getOutputStreamSetBuffer(name);
290    return buf->getStream(getStreamSetBufferPtr(name), streamIndex, blockIndex, packIndex);
291}
292
293Value * KernelBuilder::getRawInputPointer(const std::string & name, Value * streamIndex, Value * absolutePosition) const {
294    return getInputStreamSetBuffer(name)->getRawItemPointer(getStreamSetBufferPtr(name), streamIndex, absolutePosition);
295}
296
297Value * KernelBuilder::getRawOutputPointer(const std::string & name, Value * streamIndex, Value * absolutePosition) const {
298    return getOutputStreamSetBuffer(name)->getRawItemPointer(getStreamSetBufferPtr(name), streamIndex, absolutePosition);
299}
300
301unsigned KernelBuilder::getStreamSetIndex(const std::string & name) const {
302    const auto f = mStreamSetNameMap.find(name);
303    if (LLVM_UNLIKELY(f == mStreamSetNameMap.end())) {
304        throw std::runtime_error("Kernel " + getName() + " does not contain stream set: " + name);
305    }
306    return f->second;
307}
308
309Value * KernelBuilder::getStreamSetBufferPtr(const std::string & name) const {
310    return getScalarField(getSelf(), name + BUFFER_PTR_SUFFIX);
311}
312
313Argument * KernelBuilder::getParameter(Function * const f, const std::string & name) const {
314    for (auto & arg : f->getArgumentList()) {
315        if (arg.getName().equals(name)) {
316            return &arg;
317        }
318    }
319    report_fatal_error(f->getName() + " does not have parameter " + name);
320}
321
322Value * KernelBuilder::createDoSegmentCall(const std::vector<Value *> & args) const {
323    return iBuilder->CreateCall(getDoSegmentFunction(), args);
324}
325
326Value * KernelBuilder::createGetAccumulatorCall(Value * self, const std::string & accumName) const {
327    return iBuilder->CreateCall(getAccumulatorFunction(accumName), {self});
328}
329
330Value * KernelBuilder::getInputStreamSetPtr(const std::string & name, Value * blockNo) const {
331    return getInputStreamSetBuffer(name)->getStreamSetPtr(getStreamSetBufferPtr(name), blockNo);
332}
333
334BasicBlock * KernelBuilder::CreateBasicBlock(std::string && name) const {
335    return BasicBlock::Create(iBuilder->getContext(), name, mCurrentFunction);
336}
337
338void KernelBuilder::createInstance() {
339    if (LLVM_UNLIKELY(mKernelStateType == nullptr)) {
340        report_fatal_error("Cannot create kernel instance before calling prepareKernel()");
341    }
342    mKernelInstance = iBuilder->CreateCacheAlignedAlloca(mKernelStateType);
343    std::vector<Value *> init_args = {mKernelInstance};
344    for (auto a : mInitialArguments) {
345        init_args.push_back(a);
346    }
347    for (auto b : mStreamSetInputBuffers) {
348        init_args.push_back(b->getStreamSetBasePtr());
349    }
350    for (auto b : mStreamSetOutputBuffers) {
351        init_args.push_back(b->getStreamSetBasePtr());
352    }
353    Function * initMethod = getInitFunction();
354    iBuilder->CreateCall(initMethod, init_args);
355}
356
357//  The default finalBlock method simply dispatches to the doBlock routine.
358void BlockOrientedKernel::generateFinalBlockMethod(Value * remainingBytes) {
359//    std::vector<Value *> args = {self};
360//    for (Argument & arg : function->getArgumentList()){
361//        args.push_back(&arg);
362//    }
363    CreateDoBlockMethodCall();
364}
365
366//Value * BlockOrientedKernel::loadBlock(const std::string & inputName, Value * const streamIndex) const {
367
368//}
369
370//Value * BlockOrientedKernel::loadPack(const std::string & inputName, Value * const streamIndex, Value * const packIndex) const {
371
372//}
373
374
375//  The default doSegment method dispatches to the doBlock routine for
376//  each block of the given number of blocksToDo, and then updates counts.
377void BlockOrientedKernel::generateDoSegmentMethod(Value * doFinal, const std::vector<Value *> & producerPos) {
378
379    BasicBlock * const entryBlock = iBuilder->GetInsertBlock();
380    BasicBlock * const strideLoopCond = CreateBasicBlock(getName() + "_strideLoopCond");
381    BasicBlock * const strideLoopBody = CreateBasicBlock(getName() + "_strideLoopBody");
382    BasicBlock * const stridesDone = CreateBasicBlock(getName() + "_stridesDone");
383    BasicBlock * const doFinalBlock = CreateBasicBlock(getName() + "_doFinalBlock");
384    BasicBlock * const segmentDone = CreateBasicBlock(getName() + "_segmentDone");
385
386    ConstantInt * stride = iBuilder->getSize(iBuilder->getStride());
387    ConstantInt * strideBlocks = iBuilder->getSize(iBuilder->getStride() / iBuilder->getBitBlockWidth());
388
389    Value * availablePos = producerPos[0];
390    for (unsigned i = 1; i < mStreamSetInputs.size(); i++) {
391        Value * p = producerPos[i];
392        availablePos = iBuilder->CreateSelect(iBuilder->CreateICmpULT(availablePos, p), availablePos, p);
393    }
394
395    Value * processed = getProcessedItemCount(mStreamSetInputs[0].name);
396    Value * itemsAvail = iBuilder->CreateSub(availablePos, processed);
397    Value * stridesToDo = iBuilder->CreateUDiv(itemsAvail, stride);
398    iBuilder->CreateBr(strideLoopCond);
399
400    iBuilder->SetInsertPoint(strideLoopCond);
401    PHINode * stridesRemaining = iBuilder->CreatePHI(iBuilder->getSizeTy(), 2, "stridesRemaining");
402    stridesRemaining->addIncoming(stridesToDo, entryBlock);
403    Value * notDone = iBuilder->CreateICmpNE(stridesRemaining, iBuilder->getSize(0));
404    iBuilder->CreateCondBr(notDone, strideLoopBody, stridesDone);
405
406    iBuilder->SetInsertPoint(strideLoopBody);
407    Value * blockNo = getBlockNo();
408
409    CreateDoBlockMethodCall();
410
411    setBlockNo(iBuilder->CreateAdd(blockNo, strideBlocks));
412
413    // Update counts
414
415    for (unsigned i = 0; i < mStreamSetInputs.size(); i++) {
416        Value * processed = getProcessedItemCount(mStreamSetInputs[i].name);
417        processed = iBuilder->CreateAdd(processed, stride);
418        setProcessedItemCount(mStreamSetInputs[i].name, processed);
419    }
420
421    if (!mDoBlockUpdatesProducedItemCountsAttribute) {
422        for (unsigned i = 0; i < mStreamSetOutputs.size(); i++) {
423            Value * produced = getProducedItemCount(mStreamSetOutputs[i].name);
424            produced = iBuilder->CreateAdd(produced, stride);
425            setProducedItemCount(mStreamSetOutputs[i].name, produced);
426        }
427    }
428
429    stridesRemaining->addIncoming(iBuilder->CreateSub(stridesRemaining, iBuilder->getSize(1)), strideLoopBody);
430    iBuilder->CreateBr(strideLoopCond);
431
432    iBuilder->SetInsertPoint(stridesDone);
433
434    // Now conditionally perform the final block processing depending on the doFinal parameter.
435    iBuilder->CreateCondBr(doFinal, doFinalBlock, segmentDone);
436    iBuilder->SetInsertPoint(doFinalBlock);
437
438    Value * remainingItems = iBuilder->CreateSub(producerPos[0], getProcessedItemCount(mStreamSetInputs[0].name));
439
440    CreateDoFinalBlockMethodCall(remainingItems);
441
442    for (unsigned i = 0; i < mStreamSetInputs.size(); i++) {
443        Value * preProcessed = getProcessedItemCount(mStreamSetInputs[i].name);
444        setProcessedItemCount(mStreamSetInputs[i].name, iBuilder->CreateAdd(preProcessed, remainingItems));
445    }
446    if (!mDoBlockUpdatesProducedItemCountsAttribute) {
447        for (unsigned i = 0; i < mStreamSetOutputs.size(); i++) {
448            Value * preProduced = getProducedItemCount(mStreamSetOutputs[i].name);
449            setProducedItemCount(mStreamSetOutputs[i].name, iBuilder->CreateAdd(preProduced, remainingItems));
450        }
451    }
452    setTerminationSignal();
453    iBuilder->CreateBr(segmentDone);
454
455    iBuilder->SetInsertPoint(segmentDone);
456
457}
458
459void BlockOrientedKernel::generateInternalMethods() {
460
461    callGenerateDoBlockMethod();
462
463    callGenerateDoFinalBlockMethod();
464}
465
466void BlockOrientedKernel::callGenerateDoBlockMethod() {
467    mCurrentFunction = getDoBlockFunction();
468    auto args = mCurrentFunction->arg_begin();
469    mSelf = &(*args);
470    iBuilder->SetInsertPoint(CreateBasicBlock("entry"));
471    generateDoBlockMethod(); // must be implemented by the KernelBuilder subtype
472    iBuilder->CreateRetVoid();
473}
474
475
476void BlockOrientedKernel::callGenerateDoFinalBlockMethod() {
477    mCurrentFunction = getDoFinalBlockFunction();
478    auto args = mCurrentFunction->arg_begin();
479    mSelf = &(*args++);
480    Value * const remainingBytes = &(*args);
481    iBuilder->SetInsertPoint(CreateBasicBlock("entry"));
482    generateFinalBlockMethod(remainingBytes); // possibly overridden by the KernelBuilder subtype
483    iBuilder->CreateRetVoid();
484}
485
486Function * BlockOrientedKernel::getDoBlockFunction() const {
487    const auto name = getName() + DO_BLOCK_SUFFIX;
488    Function * const f = iBuilder->getModule()->getFunction(name);
489    if (LLVM_UNLIKELY(f == nullptr)) {
490        report_fatal_error("Cannot find " + name);
491    }
492    return f;
493}
494
495CallInst * BlockOrientedKernel::CreateDoBlockMethodCall() const {
496    return iBuilder->CreateCall(getDoBlockFunction(), mSelf);
497}
498
499Function * BlockOrientedKernel::getDoFinalBlockFunction() const {
500    const auto name = getName() + FINAL_BLOCK_SUFFIX;
501    Function * const f = iBuilder->getModule()->getFunction(name);
502    if (LLVM_UNLIKELY(f == nullptr)) {
503        report_fatal_error("Cannot find " + name);
504    }
505    return f;
506}
507
508CallInst * BlockOrientedKernel::CreateDoFinalBlockMethodCall(Value * remainingItems) const {
509    return iBuilder->CreateCall(getDoFinalBlockFunction(), {mSelf, remainingItems});
510}
511
512void BlockOrientedKernel::addAdditionalKernelDeclarations(Module * m, PointerType * selfType) {
513    // Create the doBlock and finalBlock function prototypes
514    FunctionType * const doBlockType = FunctionType::get(iBuilder->getVoidTy(), {selfType}, false);
515    Function * const doBlock = Function::Create(doBlockType, GlobalValue::ExternalLinkage, getName() + DO_BLOCK_SUFFIX, m);
516    doBlock->setCallingConv(CallingConv::C);
517    doBlock->setDoesNotThrow();
518    doBlock->setDoesNotCapture(1);
519    auto args = doBlock->arg_begin();
520    args->setName("self");
521    assert ((++args) == doBlock->arg_end());
522
523    FunctionType * const finalBlockType = FunctionType::get(iBuilder->getVoidTy(), {selfType, iBuilder->getSizeTy()}, false);
524    Function * const finalBlock = Function::Create(finalBlockType, GlobalValue::ExternalLinkage, getName() + FINAL_BLOCK_SUFFIX, m);
525    finalBlock->setCallingConv(CallingConv::C);
526    finalBlock->setDoesNotThrow();
527    finalBlock->setDoesNotCapture(1);
528    args = finalBlock->arg_begin();
529    args->setName("self");
530    (++args)->setName("remainingBytes");
531    assert ((++args) == finalBlock->arg_end());
532}
533
534// CONSTRUCTOR
535KernelBuilder::KernelBuilder(IDISA::IDISA_Builder * builder,
536                             std::string && kernelName,
537                             std::vector<Binding> && stream_inputs,
538                             std::vector<Binding> && stream_outputs,
539                             std::vector<Binding> && scalar_parameters,
540                             std::vector<Binding> && scalar_outputs,
541                             std::vector<Binding> && internal_scalars)
542: KernelInterface(builder, std::move(kernelName), std::move(stream_inputs), std::move(stream_outputs), std::move(scalar_parameters), std::move(scalar_outputs), std::move(internal_scalars))
543, mNoTerminateAttribute(false)
544, mDoBlockUpdatesProducedItemCountsAttribute(false) {
545
546}
547
548KernelBuilder::~KernelBuilder() { }
549
550// CONSTRUCTOR
551BlockOrientedKernel::BlockOrientedKernel(IDISA::IDISA_Builder * builder,
552                                         std::string && kernelName,
553                                         std::vector<Binding> && stream_inputs,
554                                         std::vector<Binding> && stream_outputs,
555                                         std::vector<Binding> && scalar_parameters,
556                                         std::vector<Binding> && scalar_outputs,
557                                         std::vector<Binding> && internal_scalars)
558: KernelBuilder(builder, std::move(kernelName), std::move(stream_inputs), std::move(stream_outputs), std::move(scalar_parameters), std::move(scalar_outputs), std::move(internal_scalars)) {
559
560}
561
562
563
564
565// CONSTRUCTOR
566SegmentOrientedKernel::SegmentOrientedKernel(IDISA::IDISA_Builder * builder,
567                                             std::string && kernelName,
568                                             std::vector<Binding> && stream_inputs,
569                                             std::vector<Binding> && stream_outputs,
570                                             std::vector<Binding> && scalar_parameters,
571                                             std::vector<Binding> && scalar_outputs,
572                                             std::vector<Binding> && internal_scalars)
573: KernelBuilder(builder, std::move(kernelName), std::move(stream_inputs), std::move(stream_outputs), std::move(scalar_parameters), std::move(scalar_outputs), std::move(internal_scalars)) {
574
575}
Note: See TracBrowser for help on using the repository browser.