Skip to content

Commit 941eecf

Browse files
nanavaticlaude
authored andcommitted
ATF cache: record only coherent resolutions; pin the hit path with tests
An incoherent match is an information-dependent choice (it varies with -allow-incoherent-matches, in-scope instances, and import order), so persisting its ATF projection into the .bo cache would deliver a context-dependent answer to importing compiles as if it were canonical. Drop the recordATFs call from the incoherent arm; coherent full discharges still record. The cache previously had only miss-path tests, so a lost recording site would go unnoticed by a green suite. Add hit tests: a same-package hit (ground resolution recorded by a monomorphic definition, consumed when elaborating a polymorphic body) and a cross-package hit delivered through the .bo cache, both asserted via -trace-atf-cache. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019cqQdWKp7jr73GHQYnvhiq
1 parent 1e2d76e commit 941eecf

5 files changed

Lines changed: 117 additions & 1 deletion

File tree

src/comp/TCMisc.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,11 @@ sat dvs ps p =
403403
(ps@(_:_), sbs, s_final) -> return $ (ps, sbs, s_final)
404404
([], sbs, s_final) -> do
405405
recordPackageUse mpkg
406-
recordATFs s_final
406+
-- No recordATFs here: an incoherent match is an
407+
-- information-dependent choice (context-, flag- and
408+
-- import-order-dependent), so its ATF projection must
409+
-- not be recorded where the .bo cache would deliver it
410+
-- to importing compiles as if it were canonical.
407411
let (vp_pred, inst_pred) = niceTypes (apSub s_final (toPred p, h))
408412
let pos = getPosition $ getVPredPositions p
409413
let VPred dictId _ = p
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package ATFCacheHit where
2+
3+
-- Test that a ground ATF resolution recorded by ctxreduce during
4+
-- typecheck is delivered to iExpand and HITS in the ATF cache.
5+
-- forceGround solves Encode ((UInt 5, UInt 5)) monomorphically at
6+
-- typecheck time, so EncSize ((UInt 5, UInt 5)) is recorded ground;
7+
-- elaborating checkPair's polymorphic body at (UInt 5, UInt 5) then
8+
-- finds it in the cache instead of falling back to normTFun.
9+
-- (Cache-hit twin of ATFPolyCacheMiss.bs.)
10+
11+
class Encode a n | a -> n where
12+
type EncSize a = n
13+
encode :: a -> Bit n
14+
15+
instance Encode (UInt k) k where
16+
encode x = pack x
17+
18+
instance Encode Bool 1 where
19+
encode True = 1
20+
encode False = 0
21+
22+
instance (Encode a sa, Encode b sb) => Encode (a, b) (TAdd sa sb) where
23+
encode (x, y) = encode x ++ encode y
24+
25+
data Encoded a = Encoded (Bit (EncSize a))
26+
27+
wrap :: (Encode a sa) => a -> Encoded a
28+
wrap x = Encoded (encode x)
29+
30+
checkPair :: (Encode a sa) => a -> Bool
31+
checkPair x = case wrap (x, x) of
32+
Encoded b -> msb b == 1
33+
34+
-- The monomorphic forcing definition: typechecking this solves
35+
-- Encode ((UInt 5, UInt 5)) ground and records its EncSize result.
36+
forceGround :: (UInt 5, UInt 5) -> Encoded (UInt 5, UInt 5)
37+
forceGround p = wrap p
38+
39+
{-# verilog sysATFCacheHit #-}
40+
sysATFCacheHit :: Module Empty
41+
sysATFCacheHit = module
42+
r :: Reg (UInt 5) <- mkReg 7
43+
rules
44+
"test": when True ==>
45+
action
46+
let result = checkPair r
47+
$display "result: %b" (pack result)
48+
$finish 0
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package ATFCacheHitDef where
2+
3+
-- Cross-package half of the ATF cache-hit test: the ground
4+
-- resolution EncSize ((UInt 5, UInt 5)) is solved and recorded
5+
-- HERE (by forceGround), stored in this package's .bo cache, and
6+
-- must be delivered to the importing package's elaboration.
7+
8+
class Encode a n | a -> n where
9+
type EncSize a = n
10+
encode :: a -> Bit n
11+
12+
instance Encode (UInt k) k where
13+
encode x = pack x
14+
15+
instance Encode Bool 1 where
16+
encode True = 1
17+
encode False = 0
18+
19+
instance (Encode a sa, Encode b sb) => Encode (a, b) (TAdd sa sb) where
20+
encode (x, y) = encode x ++ encode y
21+
22+
data Encoded a = Encoded (Bit (EncSize a))
23+
24+
wrap :: (Encode a sa) => a -> Encoded a
25+
wrap x = Encoded (encode x)
26+
27+
checkPair :: (Encode a sa) => a -> Bool
28+
checkPair x = case wrap (x, x) of
29+
Encoded b -> msb b == 1
30+
31+
forceGround :: (UInt 5, UInt 5) -> Encoded (UInt 5, UInt 5)
32+
forceGround p = wrap p
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package ATFCacheHitUse where
2+
3+
import ATFCacheHitDef
4+
5+
-- The only ground pred THIS package's typecheck solves is
6+
-- Encode (UInt 5) (checkPair's proviso), so a hit on the
7+
-- pair-shaped EncSize ((UInt 5, UInt 5)) during elaboration can
8+
-- only come from ATFCacheHitDef's .bo-delivered cache.
9+
10+
{-# verilog sysATFCacheHitUse #-}
11+
sysATFCacheHitUse :: Module Empty
12+
sysATFCacheHitUse = module
13+
r :: Reg (UInt 5) <- mkReg 7
14+
rules
15+
"test": when True ==>
16+
action
17+
let result = checkPair r
18+
$display "result: %b" (pack result)
19+
$finish 0

testsuite/bsc.typechecker/typeclasses/typeclasses.exp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,19 @@ compile_verilog_pass ATFPolyStructMultiParam.bs
118118
compile_verilog_pass ATFPolyCacheMiss.bs {} "-dexpanded=expanded_%m.out"
119119
find_n_strings expanded_sysATFPolyCacheMiss.out "EncSize" 0
120120

121+
# Test that a ground ATF resolution recorded at typecheck time HITS
122+
# the cache during iExpand (same package)
123+
compile_backend_pass ATFCacheHit.bs {-trace-atf-cache}
124+
find_regexp ATFCacheHit.bs.bsc-out {ATF cache hit: .*EncSize.*PrimPair}
125+
find_n_strings ATFCacheHit.bs.bsc-out {ATF cache miss: ATFCacheHit.EncSize} 0
126+
127+
# Test that a ground ATF resolution recorded in an imported package
128+
# is delivered through its .bo file and HITS in the importer's iExpand
129+
compile_pass ATFCacheHitDef.bs
130+
compile_backend_pass ATFCacheHitUse.bs {-trace-atf-cache} 1
131+
find_regexp ATFCacheHitUse.bs.bsc-out {ATF cache hit: .*EncSize.*PrimPair}
132+
find_n_strings ATFCacheHitUse.bs.bsc-out {ATF cache miss: ATFCacheHitDef.EncSize} 0
133+
121134
# Test ATF error cases
122135

123136
compile_fail_error ATFWrongArity.bs T0025

0 commit comments

Comments
 (0)