<commit_before>package opencl.ir

import ir._
import opencl.ir.toLocal

abstract class OpenCLAddressSpace

object LocalMemory extends OpenCLAddressSpace {
  override def toString() =  "local"
}

object GlobalMemory extends OpenCLAddressSpace {
  override def toString() =  "global"
}

object UndefAddressSpace extends OpenCLAddressSpace

case class OpenCLMemory(variable: Var, size: Expr, t: Type, addressSpace: OpenCLAddressSpace) extends Memory {
  override def toString() = "(" + variable + ", " + ExprSimplifier.simplify(size) + ", " + t + ", " + addressSpace + ")"
}


object OpenCLNullMemory extends OpenCLMemory(Var("NULL"), Cst(0), UndefType, UndefAddressSpace)

object OpenCLMemory {
  
  def asOpenCLMemory(m : Memory) : OpenCLMemory = {
    m match {
      case oclm : OpenCLMemory => oclm
      case NullMemory => OpenCLNullMemory
      case _ => throw new IllegalArgumentException
    }
  }

  private def alloc(f: Fun, inputMem : Memory = NullMemory, outputMem : Memory = NullMemory) : Memory = {

    val size = Type.getSizeInBytes(f.ouT)

    val outMem = f match {

      case cf: CompFun => cf.funs.foldRight(inputMem)((f,mem) => alloc(f, mem, NullMemory))

      case tl: toLocal => {
        val localMem = OpenCLMemory(Var(ContinousRange(Cst(0), size)), size, f.ouT, LocalMemory)
        alloc(tl.f, inputMem, localMem)
        localMem
      }
      case tg: toLocal => {
        val globalMem = OpenCLMemory(Var(ContinousRange(Cst(0), size)), size, f.ouT, GlobalMemory)
        alloc(tg.f, inputMem, globalMem)
        globalMem
      }

      case in: Input =>
        OpenCLMemory(Var(ContinousRange(Cst(0), size)), size, in.ouT, GlobalMemory) // global address space for all inputs

      case i : Iterate => {
        
      }

      case _ =>
        val outMem =
          if (outputMem == NullMemory)
            OpenCLMemory(Var(ContinousRange(Cst(0), size)), size, f.ouT, asOpenCLMemory(inputMem).addressSpace) // same address space as the input
          else
            outputMem
        f match {
          case fp: FPattern => alloc(fp.f, outMem)
          case _ =>
        }
        outMem
    }

    assert (outMem != NullMemory)
<commit_msg>WIP: new memory allocator<commit_after>package opencl.ir

import ir._
import opencl.ir.toLocal

abstract class OpenCLAddressSpace

object LocalMemory extends OpenCLAddressSpace {
  override def toString() =  "local"
}

object GlobalMemory extends OpenCLAddressSpace {
  override def toString() =  "global"
}

object UndefAddressSpace extends OpenCLAddressSpace

case class OpenCLMemory(variable: Var, size: Expr, t: Type, addressSpace: OpenCLAddressSpace) extends Memory {
  override def toString() = "(" + variable + ", " + ExprSimplifier.simplify(size) + ", " + t + ", " + addressSpace + ")"
}


object OpenCLNullMemory extends OpenCLMemory(Var("NULL"), Cst(0), UndefType, UndefAddressSpace)

object OpenCLMemory {
  
  def asOpenCLMemory(m : Memory) : OpenCLMemory = {
    m match {
      case oclm : OpenCLMemory => oclm
      case NullMemory => OpenCLNullMemory
      case _ => throw new IllegalArgumentException
    }
  }

  private def alloc(f: Fun, inputMem : Memory = NullMemory, outputMem : Memory = NullMemory) : Memory = {

    val size = Type.getSizeInBytes(f.ouT)

    val outMem = f match {

      case cf: CompFun => cf.funs.foldRight(inputMem)((f,mem) => alloc(f, mem, NullMemory))

      case tl: toLocal => {
        val localMem = OpenCLMemory(Var(ContinousRange(Cst(0), size)), size, f.ouT, LocalMemory)
        alloc(tl.f, inputMem, localMem)
        localMem
      }
      case tg: toLocal => {
        val globalMem = OpenCLMemory(Var(ContinousRange(Cst(0), size)), size, f.ouT, GlobalMemory)
        alloc(tg.f, inputMem, globalMem)
        globalMem
      }

      case in: Input =>
        OpenCLMemory(Var(ContinousRange(Cst(0), size)), size, in.ouT, GlobalMemory) // global address space for all inputs

      case i : Iterate => {

      }

      case _ =>
        val outMem =
          if (outputMem == NullMemory)
            OpenCLMemory(Var(ContinousRange(Cst(0), size)), size, f.ouT, asOpenCLMemory(inputMem).addressSpace) // same address space as the input
          else
            outputMem
        f match {
          case fp: FPattern => alloc(fp.f, outMem)
          case _ =>
        }
        outMem
    }

    assert (outMem != NullMemory)

{'fraction_non_alphanumeric': 0.1092032967032967, 'fraction_numerical': 0.0022893772893772895, 'mean_word_length': 2.8870106761565837, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '3485961', 'n_tokens_mistral': 1356, 'n_tokens_neox': 1340, 'n_words': 353}