<commit_before>#include "CodeGen_SPIR_Dev.h"
#include "IROperator.h"
#include <iostream>
#include "buffer_t.h"
#include "IRPrinter.h"
#include "IRMatch.h"
#include "Debug.h"
#include "Util.h"
#include "Var.h"
#include "Param.h"
#include "Target.h"
#include "integer_division_table.h"
#include "LLVM_Headers.h"

namespace Halide {
namespace Internal {

using std::vector;
using std::string;

using namespace llvm;

CodeGen_SPIR_Dev::CodeGen_SPIR_Dev(int bits) : CodeGen(), bits(bits) {
    #if !(WITH_SPIR)
    assert(false && "spir not enabled for this build of Halide.");
    #endif
}

static vector<Value *> init_kernel_metadata(LLVMContext &ctx, const char *name) {
    vector<Value *> md;
    md.push_back(MDString::get(ctx, name));
    return md;
}

void CodeGen_SPIR_Dev::add_kernel(Stmt stmt, std::string name, const std::vector<Argument> &args) {

    // Now deduce the types of the arguments to our function
    vector<llvm::Type *> arg_types(args.size()+1);
    for (size_t i = 0; i < args.size(); i++) {
        if (args[i].is_buffer) {
            arg_types[i] = llvm_type_of(UInt(8))->getPointerTo(1); // __global = addrspace(1)
        } else {
            arg_types[i] = llvm_type_of(args[i].type);
        }
    }
    // Add local (shared) memory buffer parameter.
    arg_types[args.size()] = llvm_type_of(UInt(8))->getPointerTo(3); // __local = addrspace(3)

    // Make our function
    function_name = name;
    FunctionType *func_t = FunctionType::get(void_t, arg_types, false);
    function = llvm::Function::Create(func_t, llvm::Function::ExternalLinkage, name, module);
    function->setCallingConv(llvm::CallingConv::SPIR_KERNEL);

    // Mark the buffer args as no alias
    for (size_t i = 0; i < args.size(); i++) {
        if (args[i].is_buffer) {
            function->setDoesNotAlias(i+1);
        }
    }
    // Mark the local memory as no alias (probably not necessary?)
    function->setDoesNotAlias(args.size());


    // Make the initial basic block
    entry_block = BasicBlock::Create(*context, "entry", function);
    builder->SetInsertPoint(entry_block);
    
    vector<Value *> kernel_arg_address_space = init_kernel_metadata(*context, "kernel_arg_address_space");
    vector<Value *> kernel_arg_access_qual = init_kernel_metadata(*context, "kernel_arg_access_qual");
    vector<Value *> kernel_arg_type = init_kernel_metadata(*context, "kernel_arg_type");
    vector<Value *> kernel_arg_base_type = init_kernel_metadata(*context, "kernel_arg_base_type");
    vector<Value *> kernel_arg_type_qual = init_kernel_metadata(*context, "kernel_arg_type_qual");
    vector<Value *> kernel_arg_name = init_kernel_metadata(*context, "kernel_arg_name");
    
    // Put the arguments in the symbol table
    {
        llvm::Function::arg_iterator arg = function->arg_begin();
        for (std::vector<Argument>::const_iterator iter = args.begin();
            iter != args.end();
            ++iter, ++arg) {
            if (iter->is_buffer) {
                // HACK: codegen expects a load from foo to use base
                // address 'foo.host', so we store the device pointer
                // as foo.host in this scope.
                sym_push(iter->name + ".host", arg);
                <commit_msg>Fixed address space metadata per current SPIR spec<commit_after>#include "CodeGen_SPIR_Dev.h"
#include "IROperator.h"
#include <iostream>
#include "buffer_t.h"
#include "IRPrinter.h"
#include "IRMatch.h"
#include "Debug.h"
#include "Util.h"
#include "Var.h"
#include "Param.h"
#include "Target.h"
#include "integer_division_table.h"
#include "LLVM_Headers.h"

namespace Halide {
namespace Internal {

using std::vector;
using std::string;

using namespace llvm;

CodeGen_SPIR_Dev::CodeGen_SPIR_Dev(int bits) : CodeGen(), bits(bits) {
    #if !(WITH_SPIR)
    assert(false && "spir not enabled for this build of Halide.");
    #endif
}

static vector<Value *> init_kernel_metadata(LLVMContext &ctx, const char *name) {
    vector<Value *> md;
    md.push_back(MDString::get(ctx, name));
    return md;
}

void CodeGen_SPIR_Dev::add_kernel(Stmt stmt, std::string name, const std::vector<Argument> &args) {

    // Now deduce the types of the arguments to our function
    vector<llvm::Type *> arg_types(args.size()+1);
    for (size_t i = 0; i < args.size(); i++) {
        if (args[i].is_buffer) {
            arg_types[i] = llvm_type_of(UInt(8))->getPointerTo(1); // __global = addrspace(1)
        } else {
            arg_types[i] = llvm_type_of(args[i].type);
        }
    }
    // Add local (shared) memory buffer parameter.
    arg_types[args.size()] = llvm_type_of(UInt(8))->getPointerTo(3); // __local = addrspace(3)

    // Make our function
    function_name = name;
    FunctionType *func_t = FunctionType::get(void_t, arg_types, false);
    function = llvm::Function::Create(func_t, llvm::Function::ExternalLinkage, name, module);
    function->setCallingConv(llvm::CallingConv::SPIR_KERNEL);

    // Mark the buffer args as no alias
    for (size_t i = 0; i < args.size(); i++) {
        if (args[i].is_buffer) {
            function->setDoesNotAlias(i+1);
        }
    }
    // Mark the local memory as no alias (probably not necessary?)
    function->setDoesNotAlias(args.size());


    // Make the initial basic block
    entry_block = BasicBlock::Create(*context, "entry", function);
    builder->SetInsertPoint(entry_block);
    
    vector<Value *> kernel_arg_address_space = init_kernel_metadata(*context, "kernel_arg_addr_space");
    vector<Value *> kernel_arg_access_qual = init_kernel_metadata(*context, "kernel_arg_access_qual");
    vector<Value *> kernel_arg_type = init_kernel_metadata(*context, "kernel_arg_type");
    vector<Value *> kernel_arg_base_type = init_kernel_metadata(*context, "kernel_arg_base_type");
    vector<Value *> kernel_arg_type_qual = init_kernel_metadata(*context, "kernel_arg_type_qual");
    vector<Value *> kernel_arg_name = init_kernel_metadata(*context, "kernel_arg_name");
    
    // Put the arguments in the symbol table
    {
        llvm::Function::arg_iterator arg = function->arg_begin();
        for (std::vector<Argument>::const_iterator iter = args.begin();
            iter != args.end();
            ++iter, ++arg) {
            if (iter->is_buffer) {
                // HACK: codegen expects a load from foo to use base
                // address 'foo.host', so we store the device pointer
                // as foo.host in this scope.
                sym_push(iter->name + ".host", arg);
                
{'fraction_non_alphanumeric': 0.13531758208039277, 'fraction_numerical': 0.003068425897514575, 'mean_word_length': 3.47119341563786, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7524182', 'n_tokens_mistral': 2108, 'n_tokens_neox': 2092, 'n_words': 479}