11from typing import Dict
22from dataclasses import dataclass
33
4+ from kirin import ir
5+ from kirin .rewrite .abc import RewriteRule , RewriteResult
6+ from kirin .print .printer import Printer
7+
8+ from bloqade .squin import op , wire
49from bloqade .analysis .address import Address
510from bloqade .squin .analysis .nsites import Sites
611
7- from kirin import ir
8- from kirin .rewrite .abc import RewriteResult , RewriteRule
12+ # Probably best to move these attributes to a
13+ # separate file? Keep here for now
14+ # to get things working first
15+
16+
17+ @wire .dialect .register
18+ @dataclass
19+ class AddressAttribute (ir .Attribute ):
20+
21+ name = "Address"
22+ address : Address
23+
24+ def __hash__ (self ) -> int :
25+ return hash (self .address )
26+
27+ def print_impl (self , printer : Printer ) -> None :
28+ # Can return to implementing this later
29+ pass
930
1031
32+ @op .dialect .register
1133@dataclass
12- class SquinToStim (RewriteRule ):
13-
14- # Somehow need to plug in Address and Sites
15- # into the SSAValue Hints field, which only accepts
16- # Attribute types
34+ class SitesAttribute (ir .Attribute ):
1735
18- ## Could literally just plug in `ir.Attribute` into
19- ## the Address and Site lattices?
20- ## Couldn't I just create my own attributes instead?
36+ name = "Sites"
37+ sites : Sites
38+
39+ def __hash__ (self ) -> int :
40+ return hash (self .sites )
41+
42+ def print_impl (self , printer : Printer ) -> None :
43+ # Can return to implementing this later
44+ pass
45+
46+
47+ @dataclass
48+ class WrapSquinAnalysis (RewriteRule ):
2149
2250 address_analysis : Dict [ir .SSAValue , Address ]
23- op_site_analysis : Dict [ir .SSAValue , Sites ]
51+ op_site_analysis : Dict [ir .SSAValue , Sites ]
52+
53+ def wrap (self , value : ir .SSAValue ) -> bool :
54+ address_analysis_result = self .address_analysis [value ]
55+ op_site_analysis_result = self .op_site_analysis [value ]
56+
57+ if value .hints ["address" ] and value .hints ["sites" ]:
58+ return False
59+ else :
60+ value .hints ["address" ] = AddressAttribute (address_analysis_result )
61+ value .hints ["sites" ] = SitesAttribute (op_site_analysis_result )
62+
63+ return True
2464
25- # need to plug in data into the SSAValue
26- # for the rewrite from these passes,
27- # then something should look at those hints
28- # and generate the corresponding stim statements
65+ def rewrite_Block (self , node : ir .Block ) -> RewriteResult :
66+ has_done_something = False
67+ for arg in node .args :
68+ if self .wrap (arg ):
69+ has_done_something = True
70+ return RewriteResult (has_done_something = has_done_something )
2971
30- pass
72+ def rewrite_Statement (self , node : ir .Statement ) -> RewriteResult :
73+ has_done_something = False
74+ for result in node .results :
75+ if self .wrap (result ):
76+ has_done_something = True
77+ return RewriteResult (has_done_something = has_done_something )
0 commit comments