Skip to content

Commit

Permalink
Make sure clashSimulation is False in HDL even with -O2
Browse files Browse the repository at this point in the history
The SpecConstr pass in GHC is able to poke through the OPAQUE of
clashSimulation.
Adding a noinline seems to block it from seeing the True inside.

Fixes #2736
  • Loading branch information
leonschoorl committed Jun 20, 2024
1 parent a40ff92 commit 68fb3da
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
4 changes: 3 additions & 1 deletion clash-prelude/src/Clash/Magic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ module Clash.Magic
) where

import Data.String.Interpolate (__i)
import GHC.Magic (noinline)
import GHC.Stack (HasCallStack, withFrozenCallStack)
import Clash.NamedTypes ((:::))
import GHC.TypeLits (Nat,Symbol)
Expand Down Expand Up @@ -259,7 +260,8 @@ noDeDup = id

-- | 'True' in Haskell/Clash simulation. Replaced by 'False' when generating HDL.
clashSimulation :: Bool
clashSimulation = True
clashSimulation = noinline True
-- The 'noinline' is here to prevent SpecConstr from poking through the OPAQUE, see #2736
-- See: https://github.com/clash-lang/clash-compiler/pull/2511
{-# CLASH_OPAQUE clashSimulation #-}

Expand Down
2 changes: 1 addition & 1 deletion tests/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ runClashTest = defaultMain $ clashTestRoot
, runTest "NameInstance" def{hdlSim=[]}
, outputTest "NameInstance" def
, outputTest "SetName" def{hdlTargets=[VHDL]}
, outputTest "SimulationMagic" def{hdlTargets=[VHDL]}
, outputTest "SimulationMagic2736" def{hdlTargets=[VHDL]}
, runTest "PatError" def{hdlSim=[]}
, runTest "ByteSwap32" def
, runTest "CharTest" def
Expand Down
4 changes: 4 additions & 0 deletions tests/clash-testsuite.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ library
-- an external interface file. See
-- https://github.com/clash-lang/clash-compiler/issues/1796 for an example.
shouldwork/LoadModules/precompiled
shouldwork/Basic/precompiled

exposed-modules:
Test.Tasty.Common
Expand All @@ -122,6 +123,9 @@ library
-- From tests/shouldwork/LoadModules/precompiled
T1796a

-- From tests/shouldwork/Basic/precompiled
SimulationMagic2736a

other-modules:
Paths_clash_testsuite

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
module SimulationMagic where
{-# LANGUAGE TemplateHaskellQuotes #-}
{-# OPTIONS_GHC -O2 -fspec-constr #-}
module SimulationMagic2736 where

import Prelude as P
import Data.List (isInfixOf)
import System.Environment (getArgs)
import System.FilePath ((</>))

import Clash.Prelude
import Clash.Magic (clashSimulation)
import SimulationMagic2736a

f :: Int
f | clashSimulation = 123
| otherwise = 456
f = f0
{-# ANN f (defSyn "f") #-}

assertNotIn :: String -> String -> IO ()
Expand Down
8 changes: 8 additions & 0 deletions tests/shouldwork/Basic/precompiled/SimulationMagic2736a.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{-# OPTIONS_GHC -O2 -fspec-constr #-}
module SimulationMagic2736a where

import Clash.Prelude

f0 :: Int
f0 | clashSimulation = 123
| otherwise = 456

0 comments on commit 68fb3da

Please sign in to comment.