1# Copyright 2016-2017 Tobias Grosser 2# 3# Use of this software is governed by the MIT license 4# 5# Written by Tobias Grosser, Weststrasse 47, CH-8003, Zurich 6 7import sys 8import isl 9 10# Test that isl objects can be constructed. 11# 12# This tests: 13# - construction from a string 14# - construction from an integer 15# - static constructor without a parameter 16# - conversion construction 17# - construction of empty union set 18# 19# The tests to construct from integers and strings cover functionality that 20# is also tested in the parameter type tests, but here the presence of 21# multiple overloaded constructors and overload resolution is tested. 22# 23def test_constructors(): 24 zero1 = isl.val("0") 25 assert(zero1.is_zero()) 26 27 zero2 = isl.val(0) 28 assert(zero2.is_zero()) 29 30 zero3 = isl.val.zero() 31 assert(zero3.is_zero()) 32 33 bs = isl.basic_set("{ [1] }") 34 result = isl.set("{ [1] }") 35 s = isl.set(bs) 36 assert(s.is_equal(result)) 37 38 us = isl.union_set("{ A[1]; B[2, 3] }") 39 empty = isl.union_set.empty() 40 assert(us.is_equal(us.union(empty))) 41 42# Test integer function parameters for a particular integer value. 43# 44def test_int(i): 45 val_int = isl.val(i) 46 val_str = isl.val(str(i)) 47 assert(val_int.eq(val_str)) 48 49# Test integer function parameters. 50# 51# Verify that extreme values and zero work. 52# 53def test_parameters_int(): 54 test_int(sys.maxsize) 55 test_int(-sys.maxsize - 1) 56 test_int(0) 57 58# Test isl objects parameters. 59# 60# Verify that isl objects can be passed as lvalue and rvalue parameters. 61# Also verify that isl object parameters are automatically type converted if 62# there is an inheritance relation. Finally, test function calls without 63# any additional parameters, apart from the isl object on which 64# the method is called. 65# 66def test_parameters_obj(): 67 a = isl.set("{ [0] }") 68 b = isl.set("{ [1] }") 69 c = isl.set("{ [2] }") 70 expected = isl.set("{ [i] : 0 <= i <= 2 }") 71 72 tmp = a.union(b) 73 res_lvalue_param = tmp.union(c) 74 assert(res_lvalue_param.is_equal(expected)) 75 76 res_rvalue_param = a.union(b).union(c) 77 assert(res_rvalue_param.is_equal(expected)) 78 79 a2 = isl.basic_set("{ [0] }") 80 assert(a.is_equal(a2)) 81 82 two = isl.val(2) 83 half = isl.val("1/2") 84 res_only_this_param = two.inv() 85 assert(res_only_this_param.eq(half)) 86 87# Test different kinds of parameters to be passed to functions. 88# 89# This includes integer and isl object parameters. 90# 91def test_parameters(): 92 test_parameters_int() 93 test_parameters_obj() 94 95# Test that isl objects are returned correctly. 96# 97# This only tests that after combining two objects, the result is successfully 98# returned. 99# 100def test_return_obj(): 101 one = isl.val("1") 102 two = isl.val("2") 103 three = isl.val("3") 104 105 res = one.add(two) 106 107 assert(res.eq(three)) 108 109# Test that integer values are returned correctly. 110# 111def test_return_int(): 112 one = isl.val("1") 113 neg_one = isl.val("-1") 114 zero = isl.val("0") 115 116 assert(one.sgn() > 0) 117 assert(neg_one.sgn() < 0) 118 assert(zero.sgn() == 0) 119 120# Test that isl_bool values are returned correctly. 121# 122# In particular, check the conversion to bool in case of true and false. 123# 124def test_return_bool(): 125 empty = isl.set("{ : false }") 126 univ = isl.set("{ : }") 127 128 b_true = empty.is_empty() 129 b_false = univ.is_empty() 130 131 assert(b_true) 132 assert(not b_false) 133 134# Test that strings are returned correctly. 135# Do so by calling overloaded isl.ast_build.from_expr methods. 136# 137def test_return_string(): 138 context = isl.set("[n] -> { : }") 139 build = isl.ast_build.from_context(context) 140 pw_aff = isl.pw_aff("[n] -> { [n] }") 141 set = isl.set("[n] -> { : n >= 0 }") 142 143 expr = build.expr_from(pw_aff) 144 expected_string = "n" 145 assert(expected_string == expr.to_C_str()) 146 147 expr = build.expr_from(set) 148 expected_string = "n >= 0" 149 assert(expected_string == expr.to_C_str()) 150 151# Test that return values are handled correctly. 152# 153# Test that isl objects, integers, boolean values, and strings are 154# returned correctly. 155# 156def test_return(): 157 test_return_obj() 158 test_return_int() 159 test_return_bool() 160 test_return_string() 161 162# Test that foreach functions are modeled correctly. 163# 164# Verify that closures are correctly called as callback of a 'foreach' 165# function and that variables captured by the closure work correctly. Also 166# check that the foreach function handles exceptions thrown from 167# the closure and that it propagates the exception. 168# 169def test_foreach(): 170 s = isl.set("{ [0]; [1]; [2] }") 171 172 list = [] 173 def add(bs): 174 list.append(bs) 175 s.foreach_basic_set(add) 176 177 assert(len(list) == 3) 178 assert(list[0].is_subset(s)) 179 assert(list[1].is_subset(s)) 180 assert(list[2].is_subset(s)) 181 assert(not list[0].is_equal(list[1])) 182 assert(not list[0].is_equal(list[2])) 183 assert(not list[1].is_equal(list[2])) 184 185 def fail(bs): 186 raise "fail" 187 188 caught = False 189 try: 190 s.foreach_basic_set(fail) 191 except: 192 caught = True 193 assert(caught) 194 195# Test the functionality of "every" functions. 196# 197# In particular, test the generic functionality and 198# test that exceptions are properly propagated. 199# 200def test_every(): 201 us = isl.union_set("{ A[i]; B[j] }") 202 203 def is_empty(s): 204 return s.is_empty() 205 assert(not us.every_set(is_empty)) 206 207 def is_non_empty(s): 208 return not s.is_empty() 209 assert(us.every_set(is_non_empty)) 210 211 def in_A(s): 212 return s.is_subset(isl.set("{ A[x] }")) 213 assert(not us.every_set(in_A)) 214 215 def not_in_A(s): 216 return not s.is_subset(isl.set("{ A[x] }")) 217 assert(not us.every_set(not_in_A)) 218 219 def fail(s): 220 raise "fail" 221 222 caught = False 223 try: 224 us.ever_set(fail) 225 except: 226 caught = True 227 assert(caught) 228 229# Check basic construction of spaces. 230# 231def test_space(): 232 unit = isl.space.unit() 233 set_space = unit.add_named_tuple("A", 3) 234 map_space = set_space.add_named_tuple("B", 2) 235 236 set = isl.set.universe(set_space) 237 map = isl.map.universe(map_space) 238 assert(set.is_equal(isl.set("{ A[*,*,*] }"))) 239 assert(map.is_equal(isl.map("{ A[*,*,*] -> B[*,*] }"))) 240 241# Construct a simple schedule tree with an outer sequence node and 242# a single-dimensional band node in each branch, with one of them 243# marked coincident. 244# 245def construct_schedule_tree(): 246 A = isl.union_set("{ A[i] : 0 <= i < 10 }") 247 B = isl.union_set("{ B[i] : 0 <= i < 20 }") 248 249 node = isl.schedule_node.from_domain(A.union(B)) 250 node = node.child(0) 251 252 filters = isl.union_set_list(A).add(B) 253 node = node.insert_sequence(filters) 254 255 f_A = isl.multi_union_pw_aff("[ { A[i] -> [i] } ]") 256 node = node.child(0) 257 node = node.child(0) 258 node = node.insert_partial_schedule(f_A) 259 node = node.member_set_coincident(0, True) 260 node = node.ancestor(2) 261 262 f_B = isl.multi_union_pw_aff("[ { B[i] -> [i] } ]") 263 node = node.child(1) 264 node = node.child(0) 265 node = node.insert_partial_schedule(f_B) 266 node = node.ancestor(2) 267 268 return node.schedule() 269 270# Test basic schedule tree functionality. 271# 272# In particular, create a simple schedule tree and 273# - check that the root node is a domain node 274# - test map_descendant_bottom_up 275# - test foreach_descendant_top_down 276# - test every_descendant 277# 278def test_schedule_tree(): 279 schedule = construct_schedule_tree() 280 root = schedule.root() 281 282 assert(type(root) == isl.schedule_node_domain) 283 284 count = [0] 285 def inc_count(node): 286 count[0] += 1 287 return node 288 root = root.map_descendant_bottom_up(inc_count) 289 assert(count[0] == 8) 290 291 def fail_map(node): 292 raise "fail" 293 return node 294 caught = False 295 try: 296 root.map_descendant_bottom_up(fail_map) 297 except: 298 caught = True 299 assert(caught) 300 301 count = [0] 302 def inc_count(node): 303 count[0] += 1 304 return True 305 root.foreach_descendant_top_down(inc_count) 306 assert(count[0] == 8) 307 308 count = [0] 309 def inc_count(node): 310 count[0] += 1 311 return False 312 root.foreach_descendant_top_down(inc_count) 313 assert(count[0] == 1) 314 315 def is_not_domain(node): 316 return type(node) != isl.schedule_node_domain 317 assert(root.child(0).every_descendant(is_not_domain)) 318 assert(not root.every_descendant(is_not_domain)) 319 320 def fail(node): 321 raise "fail" 322 caught = False 323 try: 324 root.every_descendant(fail) 325 except: 326 caught = True 327 assert(caught) 328 329 domain = root.domain() 330 filters = [isl.union_set("{}")] 331 def collect_filters(node): 332 if type(node) == isl.schedule_node_filter: 333 filters[0] = filters[0].union(node.filter()) 334 return True 335 root.every_descendant(collect_filters) 336 assert(domain.is_equal(filters[0])) 337 338# Test marking band members for unrolling. 339# "schedule" is the schedule created by construct_schedule_tree. 340# It schedules two statements, with 10 and 20 instances, respectively. 341# Unrolling all band members therefore results in 30 at-domain calls 342# by the AST generator. 343# 344def test_ast_build_unroll(schedule): 345 root = schedule.root() 346 def mark_unroll(node): 347 if type(node) == isl.schedule_node_band: 348 node = node.member_set_ast_loop_unroll(0) 349 return node 350 root = root.map_descendant_bottom_up(mark_unroll) 351 schedule = root.schedule() 352 353 count_ast = [0] 354 def inc_count_ast(node, build): 355 count_ast[0] += 1 356 return node 357 358 build = isl.ast_build() 359 build = build.set_at_each_domain(inc_count_ast) 360 ast = build.node_from(schedule) 361 assert(count_ast[0] == 30) 362 363# Test basic AST generation from a schedule tree. 364# 365# In particular, create a simple schedule tree and 366# - generate an AST from the schedule tree 367# - test at_each_domain 368# - test unrolling 369# 370def test_ast_build(): 371 schedule = construct_schedule_tree() 372 373 count_ast = [0] 374 def inc_count_ast(node, build): 375 count_ast[0] += 1 376 return node 377 378 build = isl.ast_build() 379 build_copy = build.set_at_each_domain(inc_count_ast) 380 ast = build.node_from(schedule) 381 assert(count_ast[0] == 0) 382 count_ast[0] = 0 383 ast = build_copy.node_from(schedule) 384 assert(count_ast[0] == 2) 385 build = build_copy 386 count_ast[0] = 0 387 ast = build.node_from(schedule) 388 assert(count_ast[0] == 2) 389 390 do_fail = True 391 count_ast_fail = [0] 392 def fail_inc_count_ast(node, build): 393 count_ast_fail[0] += 1 394 if do_fail: 395 raise "fail" 396 return node 397 build = isl.ast_build() 398 build = build.set_at_each_domain(fail_inc_count_ast) 399 caught = False 400 try: 401 ast = build.node_from(schedule) 402 except: 403 caught = True 404 assert(caught) 405 assert(count_ast_fail[0] > 0) 406 build_copy = build 407 build_copy = build_copy.set_at_each_domain(inc_count_ast) 408 count_ast[0] = 0 409 ast = build_copy.node_from(schedule) 410 assert(count_ast[0] == 2) 411 count_ast_fail[0] = 0 412 do_fail = False 413 ast = build.node_from(schedule) 414 assert(count_ast_fail[0] == 2) 415 416 test_ast_build_unroll(schedule) 417 418# Test basic AST expression generation from an affine expression. 419# 420def test_ast_build_expr(): 421 pa = isl.pw_aff("[n] -> { [n + 1] }") 422 build = isl.ast_build.from_context(pa.domain()) 423 424 op = build.expr_from(pa) 425 assert(type(op) == isl.ast_expr_op_add) 426 assert(op.n_arg() == 2) 427 428# Test the isl Python interface 429# 430# This includes: 431# - Object construction 432# - Different parameter types 433# - Different return types 434# - Foreach functions 435# - Every functions 436# - Spaces 437# - Schedule trees 438# - AST generation 439# - AST expression generation 440# 441test_constructors() 442test_parameters() 443test_return() 444test_foreach() 445test_every() 446test_space() 447test_schedule_tree() 448test_ast_build() 449test_ast_build_expr() 450