packages feed

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

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

module Tests.Control.DeadCode where

-- fir
import FIR

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

-- Code following a 'break' is unreachable. It must not be emitted into the block
-- that the 'break' terminated, and control flow constructs within it must still be
-- emitted consistently (e.g. a selection whose merge block is missing is invalid).

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

program :: Module Defs
program = Module do

  entryPoint @"main" @Compute do

    _ <- def @"x" @RW @Float 0

    -- Conditional in dead code after a break.
    loop do
      x <- get @"x"
      if x > 3
      then do
        break @1
        if x > 4
        then put @"x" 1
        else put @"x" 2
      else put @"x" ( x + 1 )

    -- Loop body ending with a break.
    while ( ( < 10 ) <<$>> get @"x" ) do
      modify @"x" ( * 2 )
      break @1

    x <- get @"x"
    assign @( Name "out" :.: Name "res" ) x