<commit_before>module Futhark.EnablingOpts.Simplifier.DataDependencies
  ( dataDependencies
  )
  where

import Data.Maybe

import qualified Data.HashMap.Lazy as HM
import qualified Data.HashSet as HS

import Futhark.InternalRep

dataDependencies :: Body -> HM.HashMap VName (HS.HashSet VName)
dataDependencies = dataDependencies' HM.empty
  where dataDependencies' deps = foldl grow deps . bodyBindings
        grow deps (Let pat (If c tb fb _ _)) =
          let tdeps = dataDependencies' deps tb
              fdeps = dataDependencies' deps fb
              cdeps = depsOf c deps
              comb (v, tres, fres) =
                (identName v, HS.unions [cdeps, depsOf tres tdeps, depsOf fres fdeps])
              branchdeps =
                HM.fromList $ map comb $ zip3 pat (resultSubExps $ bodyResult tb)
                                                  (resultSubExps $ bodyResult fb)
          in branchdeps `HM.union` deps

        grow deps (Let pat e) =
          let free = freeNamesInExp e
              freeDeps = HS.unions $ map (`nameDeps` deps) $ HS.toList free
          in HM.fromList [ (identName v, freeDeps) | v <- pat ] `HM.union` deps

        nameDeps name deps = fromMaybe HS.empty $ HM.lookup name deps

        depsOf (Constant _ _) _ = HS.empty
        depsOf (Var v) deps     = nameDeps (identName v) deps
<commit_msg>Fixes to data dependency computation.<commit_after>module Futhark.EnablingOpts.Simplifier.DataDependencies
  ( dataDependencies
  )
  where

import Data.Maybe

import qualified Data.HashMap.Lazy as HM
import qualified Data.HashSet as HS

import Futhark.InternalRep

dataDependencies :: Body -> HM.HashMap VName (HS.HashSet VName)
dataDependencies = dataDependencies' HM.empty
  where dataDependencies' deps = foldl grow deps . bodyBindings
        grow deps (Let pat (If c tb fb _ _)) =
          let tdeps = dataDependencies' deps tb
              fdeps = dataDependencies' deps fb
              cdeps = depsOf c deps
              comb (v, tres, fres) =
                (identName v, HS.unions [cdeps, depsOf tres tdeps, depsOf fres fdeps])
              branchdeps =
                HM.fromList $ map comb $ zip3 pat (resultSubExps $ bodyResult tb)
                                                  (resultSubExps $ bodyResult fb)
          in branchdeps `HM.union` deps

        grow deps (Let pat (DoLoop _ _ bound body _)) =
          let bodydeps = dataDependencies' deps body
              bounddeps = depsOf bound deps
              comb v e =
                (identName v, HS.unions [bounddeps, depsOf e bodydeps])
          in HM.fromList $ zipWith comb pat $ resultSubExps $ bodyResult body

        grow deps (Let pat e) =
          let free = freeNamesInExp e
              freeDeps = HS.unions $ free : map (`nameDeps` deps) (HS.toList free)
          in HM.fromList [ (identName v, freeDeps) | v <- pat ] `HM.union` deps

        nameDeps name deps = fromMaybe HS.empty $ HM.lookup name deps

        depsOf (Constant _ _) _ = HS.empty
        depsOf (Var v) deps     = nameDeps (identName v) deps

{'fraction_non_alphanumeric': 0.07333767926988266, 'fraction_numerical': 0.000651890482398957, 'mean_word_length': 2.056772908366534, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5932741', 'n_tokens_mistral': 979, 'n_tokens_neox': 904, 'n_words': 297}