Skip to content

Commit f6bf161

Browse files
committed
various fixes
1 parent ff7ce85 commit f6bf161

File tree

9 files changed

+247
-13
lines changed

9 files changed

+247
-13
lines changed

lib/conversions/src/RLCToPython.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,9 @@ namespace mlir::rlc
171171

172172
if (returnsVoid(type).failed())
173173
{
174-
w.write("__result = ");
174+
w.write("__result = (");
175175
w.writeType(resultType, 1);
176-
w.writenl("()");
176+
w.writenl(")()");
177177
}
178178

179179
w.write("lib.", mangledName);

lib/dialect/src/TypeCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ static mlir::LogicalResult deduceClassBody(
274274
auto checkedParameterType = scopedConverter.convertType(unchecked);
275275
if (not checkedParameterType)
276276
{
277-
return mlir::failure();
277+
return mlir::rlc::logError(op, "While analyzing class declaration, could not understand the semantics of " + mlir::rlc::prettyType(unchecked)) ;
278278
}
279279
checkedTemplateParameters.push_back(checkedParameterType);
280280
auto actualType =

lib/dialect/src/Types.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,11 @@ namespace mlir::rlc
500500
OS << maybeType.getName();
501501
return;
502502
}
503+
if (auto maybeType = mlir::dyn_cast<mlir::rlc::UncheckedTemplateParameterType>(t))
504+
{
505+
OS << "unchecked_" <<maybeType.getName();
506+
return;
507+
}
503508
if (auto maybeType = mlir::dyn_cast<mlir::rlc::ContextType>(t))
504509
{
505510
typeToMangled(OS, maybeType.getUnderlying());

python/ml/ppg/torch_util.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,16 @@ def have_cuda():
120120
and th.cuda.device_count() > 0
121121
)
122122

123+
def have_mps():
124+
return (th.backends.mps.is_available())
123125

124126
def default_device_type():
125-
return "cuda" if have_cuda() else "cpu"
126-
127+
if have_cuda():
128+
return "cuda"
129+
elif have_mps():
130+
return "mps"
131+
else:
132+
return "cpu"
127133

128134
no_grad = contextmanager_to_decorator(th.no_grad)
129135
DEFAULT_DEVICE = th.device(type=default_device_type())

stdlib/enum_utils.rl

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,26 @@
1515
#
1616

1717
trait<T> Enum:
18-
fun max(T obj) -> Int
19-
fun is_enum(T obj) -> Bool
20-
fun as_int(T obj) -> Int
21-
fun from_int(T obj, Int new_value)
22-
fun as_string_literal(T obj) -> StringLiteral
18+
fun max(T obj) -> Int
19+
fun is_enum(T obj) -> Bool
20+
fun as_int(T obj) -> Int
21+
fun from_int(T obj, Int new_value)
22+
fun as_string_literal(T obj) -> StringLiteral
23+
24+
cls<T> EnumRange:
25+
fun get(Int x) -> T:
26+
let to_return : T
27+
if to_return is Enum:
28+
from_int(to_return, x)
29+
return to_return
30+
31+
fun size() -> Int:
32+
let to_return : T
33+
if to_return is Enum:
34+
return max(to_return) + 1
35+
return 0
36+
37+
fun<Enum T> range(T t) -> EnumRange<T>:
38+
let to_return : EnumRange<T>
39+
return to_return
40+

stdlib/math/numeric.rl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ fun min(Int a, Int b) -> Int:
2727
ext fun sqrt(Float f) -> Float
2828

2929
fun abs(Int a) -> Int:
30-
if 0 > a:
31-
return -a
32-
return a
30+
if 0 > a:
31+
return -a
32+
return a
33+
34+
fun near(Float value, Float target, Float distance) -> Bool:
35+
return target + distance > value and target - distance < value
36+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# RUN: split-file %s %t
2+
# RUN: rlc %t/source.rl -o %t/lib%sharedext -i %stdlib --shared --pyrlc-lib=%pyrlc_lib
3+
# RUN: rlc %t/source.rl -o %t/wrapper.py -i %stdlib --python
4+
# RUN: python %t/to_run.py
5+
6+
#--- source.rl
7+
8+
fun asd() -> Int[10]:
9+
let x : Int[10]
10+
return x
11+
12+
13+
#--- to_run.py
14+
import wrapper
15+
16+
assert len(wrapper.asd()) == 10
17+
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
# REQUIRES: has_ruby
2+
# RUN: split-file %s %t
3+
4+
# RUN: rlc %t/source.rl -o %t/lib%sharedext -i %stdlib --shared
5+
# RUN: rlc %t/source.rl -o %t/lib%libext -i %stdlib --compile
6+
7+
# RUN: rlc %t/source.rl -o %t/library.rb -i %stdlib --ruby
8+
# RUN: ruby %t/to_run.rb
9+
10+
11+
#--- source.rl
12+
import serialization.to_byte_vector
13+
import string
14+
import action
15+
import learn
16+
17+
cls Board:
18+
BInt<0, 3>[9] slots
19+
Bool playerTurn
20+
21+
fun get(Int x, Int y) -> Int:
22+
return self.slots[x + (y * 3)].value
23+
24+
fun set(Int x, Int y, Int val):
25+
self.slots[x + (y * 3)].value = val
26+
27+
fun full() -> Bool:
28+
let x = 0
29+
30+
while x < 3:
31+
let y = 0
32+
while y < 3:
33+
if self.get(x, y) == 0:
34+
return false
35+
y = y + 1
36+
x = x + 1
37+
38+
return true
39+
40+
fun three_in_a_line_player_row(Int player_id, Int row) -> Bool:
41+
return self.get(0, row) == self.get(1, row) and self.get(0, row) == self.get(2, row) and self.get(0, row) == player_id
42+
43+
fun three_in_a_line_player(Int player_id) -> Bool:
44+
let x = 0
45+
while x < 3:
46+
if self.get(x, 0) == self.get(x, 1) and self.get(x, 0) == self.get(x, 2) and self.get(x, 0) == player_id:
47+
return true
48+
49+
if self.three_in_a_line_player_row(player_id, x):
50+
return true
51+
x = x + 1
52+
53+
if self.get(0, 0) == self.get(1, 1) and self.get(0, 0) == self.get(2, 2) and self.get(0, 0) == player_id:
54+
return true
55+
56+
if self.get(0, 2) == self.get(1, 1) and self.get(0, 2) == self.get(2, 0) and self.get(0, 2) == player_id:
57+
return true
58+
59+
return false
60+
61+
fun current_player() -> Int:
62+
return int(self.playerTurn) + 1
63+
64+
fun next_turn():
65+
self.playerTurn = !self.playerTurn
66+
67+
# tic tac toe implementation
68+
69+
@classes
70+
act play() -> Game:
71+
frm board : Board
72+
frm score = 10
73+
while !board.full():
74+
# sets the indicated board as beloning
75+
# to the current player
76+
act mark(BInt<0, 3> x, BInt<0, 3> y) { board.get(x.value, y.value) == 0 }
77+
78+
score = score - 1
79+
board.set(x.value, y.value, board.current_player())
80+
81+
if board.three_in_a_line_player(board.current_player()):
82+
return
83+
board.next_turn()
84+
85+
fun get_current_player(Game g) -> Int:
86+
return int(g.board.playerTurn)
87+
88+
fun score(Game g, Int player_id) -> Float:
89+
if !g.is_done():
90+
return 0.0
91+
if g.board.three_in_a_line_player(player_id + 1):
92+
return 1.0
93+
else if g.board.three_in_a_line_player(((player_id + 1) % 2) + 1):
94+
return -1.0
95+
96+
return 0.0
97+
98+
fun get_num_players() -> Int:
99+
return 2
100+
101+
fun fuzz(Vector<Byte> input):
102+
if input.size() == 0:
103+
return
104+
let state = play()
105+
let action : AnyGameAction
106+
parse_and_execute(state, action, input)
107+
108+
fun main() -> Int:
109+
let game = play()
110+
let x : BInt<0, 3>
111+
let y : BInt<0, 3>
112+
x.value = 0
113+
y.value = 0
114+
game.mark(x, y)
115+
if game.board.full():
116+
return 1
117+
x.value = 1
118+
y.value = 0
119+
game.mark(x, y)
120+
if game.board.full():
121+
return 2
122+
x.value = 1
123+
y.value = 1
124+
game.mark(x, y)
125+
if game.board.full():
126+
return 3
127+
x.value = 2
128+
y.value = 0
129+
game.mark(x, y)
130+
if game.board.full():
131+
return 4
132+
x.value = 2
133+
y.value = 2
134+
game.mark(x, y)
135+
if game.board.full():
136+
return 5
137+
if game.board.three_in_a_line_player(1):
138+
return 0
139+
else:
140+
return 1
141+
142+
fun pretty_print(Game g):
143+
let i = 0
144+
while i != 3:
145+
let to_print : String
146+
let y = 0
147+
while y != 3:
148+
to_print.append(to_string(g.board.get(i, y)))
149+
y = y + 1
150+
print(to_print)
151+
i = i + 1
152+
#--- to_run.rb
153+
require_relative 'library'
154+
action = RLC::RLCAnyGameAction.new
155+
_actions = RLC::enumerate(action)
156+
print(_actions)
157+
actions = (0..._actions.size).map { |i| _actions.get(i) }
158+
action = actions[0]
159+
puts(action[0])
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# RUN: split-file %s %t
2+
# RUN: python %pyscript/action.py %t/source.rl %t/trace.txt --stdlib %stdlib -ii | FileCheck %s
3+
4+
# CHECK: asd {x: 4}
5+
# CHECK-NEXT: asd2 {y: {field: 5}}
6+
# CHECK-NEXT: asd3 {}
7+
# CHECK-NEXT: {resume_index: -1}
8+
9+
#--- source.rl
10+
import action
11+
12+
cls Struct:
13+
Int field
14+
15+
@classes
16+
act play() -> Game:
17+
act asd(Int x) {x == 4}
18+
act asd2(Struct y) {y.field == 5}
19+
act asd3()
20+
21+
#--- trace.txt
22+
asd {x: 4}
23+
asd2 {y: {field: 5}}
24+
asd {x: 10}
25+
asd3 {}

0 commit comments

Comments
 (0)