1
1
import inspect
2
2
import ast
3
3
4
- from llvm import core as lc
4
+ import llvmlite . ir as ll
5
5
6
6
from artiq .py2llvm .values import VGeneric , operators
7
7
from artiq .py2llvm .base_types import VBool , VInt , VFloat
@@ -21,11 +21,15 @@ def init_module(module):
21
21
func_def = ast .parse (inspect .getsource (_gcd )).body [0 ]
22
22
function , _ = module .compile_function (func_def ,
23
23
{"a" : VInt (64 ), "b" : VInt (64 )})
24
- function .linkage = lc . LINKAGE_INTERNAL
24
+ function .linkage = "internal"
25
25
26
26
27
27
def _reduce (builder , a , b ):
28
- gcd_f = builder .basic_block .function .module .get_function_named ("_gcd" )
28
+ module = builder .basic_block .function .module
29
+ for f in module .functions :
30
+ if f .name == "_gcd" :
31
+ gcd_f = f
32
+ break
29
33
gcd = builder .call (gcd_f , [a , b ])
30
34
a = builder .sdiv (a , gcd )
31
35
b = builder .sdiv (b , gcd )
@@ -38,46 +42,42 @@ def _signnum(builder, a, b):
38
42
swap_block = function .append_basic_block ("sn_swap" )
39
43
merge_block = function .append_basic_block ("sn_merge" )
40
44
41
- condition = builder .icmp (
42
- lc . ICMP_SLT , b , lc .Constant . int ( lc . Type . int (64 ), 0 ))
45
+ condition = builder .icmp_signed (
46
+ "<" , b , ll .Constant ( ll . IntType (64 ), 0 ))
43
47
builder .cbranch (condition , swap_block , merge_block )
44
48
45
49
builder .position_at_end (swap_block )
46
- minusone = lc .Constant . int ( lc . Type . int (64 ), - 1 )
50
+ minusone = ll .Constant ( ll . IntType (64 ), - 1 )
47
51
a_swp = builder .mul (minusone , a )
48
52
b_swp = builder .mul (minusone , b )
49
53
builder .branch (merge_block )
50
54
51
55
builder .position_at_end (merge_block )
52
- a_phi = builder .phi (lc . Type . int (64 ))
56
+ a_phi = builder .phi (ll . IntType (64 ))
53
57
a_phi .add_incoming (a , orig_block )
54
58
a_phi .add_incoming (a_swp , swap_block )
55
- b_phi = builder .phi (lc . Type . int (64 ))
59
+ b_phi = builder .phi (ll . IntType (64 ))
56
60
b_phi .add_incoming (b , orig_block )
57
61
b_phi .add_incoming (b_swp , swap_block )
58
62
59
63
return a_phi , b_phi
60
64
61
65
62
66
def _make_ssa (builder , n , d ):
63
- value = lc .Constant .undef (lc .Type .vector (lc .Type .int (64 ), 2 ))
64
- value = builder .insert_element (
65
- value , n , lc .Constant .int (lc .Type .int (), 0 ))
66
- value = builder .insert_element (
67
- value , d , lc .Constant .int (lc .Type .int (), 1 ))
67
+ value = ll .Constant (ll .ArrayType (ll .IntType (64 ), 2 ), ll .Undefined )
68
+ value = builder .insert_value (value , n , 0 )
69
+ value = builder .insert_value (value , d , 1 )
68
70
return value
69
71
70
72
71
73
class VFraction (VGeneric ):
72
74
def get_llvm_type (self ):
73
- return lc . Type . vector ( lc . Type . int (64 ), 2 )
75
+ return ll . ArrayType ( ll . IntType (64 ), 2 )
74
76
75
77
def _nd (self , builder ):
76
78
ssa_value = self .auto_load (builder )
77
- a = builder .extract_element (
78
- ssa_value , lc .Constant .int (lc .Type .int (), 0 ))
79
- b = builder .extract_element (
80
- ssa_value , lc .Constant .int (lc .Type .int (), 1 ))
79
+ a = builder .extract_value (ssa_value , 0 )
80
+ b = builder .extract_value (ssa_value , 1 )
81
81
return a , b
82
82
83
83
def set_value_nd (self , builder , a , b ):
@@ -101,19 +101,16 @@ def o_getattr(self, attr, builder):
101
101
raise AttributeError
102
102
r = VInt (64 )
103
103
if builder is not None :
104
- elt = builder .extract_element (
105
- self .auto_load (builder ),
106
- lc .Constant .int (lc .Type .int (), idx ))
104
+ elt = builder .extract_value (self .auto_load (builder ), idx )
107
105
r .auto_store (builder , elt )
108
106
return r
109
107
110
108
def o_bool (self , builder ):
111
109
r = VBool ()
112
110
if builder is not None :
113
- zero = lc .Constant .int (lc .Type .int (64 ), 0 )
114
- a = builder .extract_element (
115
- self .auto_load (builder ), lc .Constant .int (lc .Type .int (), 0 ))
116
- r .auto_store (builder , builder .icmp (lc .ICMP_NE , a , zero ))
111
+ zero = ll .Constant (ll .IntType (64 ), 0 )
112
+ a = builder .extract_element (self .auto_load (builder ), 0 )
113
+ r .auto_store (builder , builder .icmp_signed ("!=" , a , zero ))
117
114
return r
118
115
119
116
def o_intx (self , target_bits , builder ):
@@ -131,15 +128,15 @@ def o_roundx(self, target_bits, builder):
131
128
else :
132
129
r = VInt (64 )
133
130
a , b = self ._nd (builder )
134
- h_b = builder .ashr (b , lc .Constant . int ( lc . Type . int (64 ), 1 ))
131
+ h_b = builder .ashr (b , ll .Constant ( ll . IntType (64 ), 1 ))
135
132
136
133
function = builder .basic_block .function
137
134
add_block = function .append_basic_block ("fr_add" )
138
135
sub_block = function .append_basic_block ("fr_sub" )
139
136
merge_block = function .append_basic_block ("fr_merge" )
140
137
141
- condition = builder .icmp (
142
- lc . ICMP_SLT , a , lc .Constant . int ( lc . Type . int (64 ), 0 ))
138
+ condition = builder .icmp_signed (
139
+ "<" , a , ll .Constant ( ll . IntType (64 ), 0 ))
143
140
builder .cbranch (condition , sub_block , add_block )
144
141
145
142
builder .position_at_end (add_block )
@@ -150,7 +147,7 @@ def o_roundx(self, target_bits, builder):
150
147
builder .branch (merge_block )
151
148
152
149
builder .position_at_end (merge_block )
153
- a = builder .phi (lc . Type . int (64 ))
150
+ a = builder .phi (ll . IntType (64 ))
154
151
a .add_incoming (a_add , add_block )
155
152
a .add_incoming (a_sub , sub_block )
156
153
r .auto_store (builder , builder .sdiv (a , b ))
@@ -165,19 +162,19 @@ def _o_eq_inv(self, other, builder, ne):
165
162
other = other .o_int64 (builder )
166
163
a , b = self ._nd (builder )
167
164
ssa_r = builder .and_ (
168
- builder .icmp ( lc . ICMP_EQ , a ,
169
- other .auto_load ()),
170
- builder .icmp ( lc . ICMP_EQ , b ,
171
- lc .Constant . int ( lc . Type . int (64 ), 1 )))
165
+ builder .icmp_signed ( "==" , a ,
166
+ other .auto_load ()),
167
+ builder .icmp_signed ( "==" , b ,
168
+ ll .Constant ( ll . IntType (64 ), 1 )))
172
169
else :
173
170
a , b = self ._nd (builder )
174
171
c , d = other ._nd (builder )
175
172
ssa_r = builder .and_ (
176
- builder .icmp ( lc . ICMP_EQ , a , c ),
177
- builder .icmp ( lc . ICMP_EQ , b , d ))
173
+ builder .icmp_signed ( "==" , a , c ),
174
+ builder .icmp_signed ( "==" , b , d ))
178
175
if ne :
179
176
ssa_r = builder .xor (ssa_r ,
180
- lc .Constant . int ( lc . Type . int (1 ), 1 ))
177
+ ll .Constant ( ll . IntType (1 ), 1 ))
181
178
r .auto_store (builder , ssa_r )
182
179
return r
183
180
@@ -194,24 +191,23 @@ def _o_cmp(self, other, icmp, builder):
194
191
r = VBool ()
195
192
if builder is not None :
196
193
diff = diff .auto_load (builder )
197
- a = builder .extract_element (
198
- diff , lc .Constant .int (lc .Type .int (), 0 ))
199
- zero = lc .Constant .int (lc .Type .int (64 ), 0 )
194
+ a = builder .extract_value (diff , 0 )
195
+ zero = ll .Constant (ll .IntType (64 ), 0 )
200
196
ssa_r = builder .icmp (icmp , a , zero )
201
197
r .auto_store (builder , ssa_r )
202
198
return r
203
199
204
200
def o_lt (self , other , builder ):
205
- return self ._o_cmp (other , lc . ICMP_SLT , builder )
201
+ return self ._o_cmp (other , "<" , builder )
206
202
207
203
def o_le (self , other , builder ):
208
- return self ._o_cmp (other , lc . ICMP_SLE , builder )
204
+ return self ._o_cmp (other , "<=" , builder )
209
205
210
206
def o_gt (self , other , builder ):
211
- return self ._o_cmp (other , lc . ICMP_SGT , builder )
207
+ return self ._o_cmp (other , ">" , builder )
212
208
213
209
def o_ge (self , other , builder ):
214
- return self ._o_cmp (other , lc . ICMP_SGE , builder )
210
+ return self ._o_cmp (other , ">=" , builder )
215
211
216
212
def _o_addsub (self , other , builder , sub , invert = False ):
217
213
if isinstance (other , VFloat ):
0 commit comments