packages feed

fir-0.1.0.0: test/Tests/Control/TemporaryBranches.hs

{-# LANGUAGE BlockArguments      #-}
{-# LANGUAGE DataKinds           #-}
{-# LANGUAGE PolyKinds           #-}
{-# LANGUAGE RebindableSyntax    #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications    #-}
{-# LANGUAGE TypeOperators       #-}

module Tests.Control.TemporaryBranches where

-- base
import Data.Maybe
  ( fromJust )

-- vector-sized
import qualified Data.Vector.Sized as Vector

-- fir
import FIR

------------------------------------------------
-- program

-- Indexing into an array value with a run-time index requires copying the array
-- into a local variable. The copy made in one branch must not be reused
-- in the other branch, nor after the branches merge, as the store that
-- initialises the local variable does not dominate those uses.

table :: Array 4 Float
table = MkArray ( fromJust $ Vector.fromList [ 1, 2, 3, 4 ] )

type Defs =
  '[ "ubo"  ':-> Uniform '[ DescriptorSet 0, Binding 0 ]
                   ( Struct '[ "i" ':-> Word32 ] )
   , "out"  ':-> StorageBuffer '[ DescriptorSet 0, Binding 1 ]
                   ( Struct '[ "res" ':-> Float ] )
   , "main" ':-> EntryPoint '[ LocalSize 1 1 1 ] Compute
   ]

program :: Module Defs
program = Module do

  entryPoint @"main" @Compute do

    let
      arr :: Code ( Array 4 Float )
      arr = Lit table
    i <- use @( Name "ubo" :.: Name "i" )

    r <-
      if i > 2
      then pure ( view @( AnIndex ( Code Word32 ) ) i arr )
      else pure ( view @( AnIndex ( Code Word32 ) ) ( i - 1 ) arr )

    assign @( Name "out" :.: Name "res" ) ( r + view @( AnIndex ( Code Word32 ) ) ( i + 1 ) arr )