brintos

brintos / llvm-project-archived public Read only

0
0
Text · 9.2 KiB · 01308d1 Raw
382 lines · plain
1/* Copyright 2016-2017 Tobias Grosser2 *3 * Use of this software is governed by the MIT license4 *5 * Written by Tobias Grosser, Weststrasse 47, CH-8003, Zurich6 */7 8#include <vector>9#include <string>10#include <limits.h>11#include <stdio.h>12#include <stdlib.h>13#include <string.h>14 15#include <map>16 17#include <isl/options.h>18#include <isl/typed_cpp.h>19 20static void die_impl(const char *file, int line, const char *message)21{22	fprintf(stderr, "Assertion failed in %s:%d %s\n", file, line, message);23	exit(EXIT_FAILURE);24}25 26static void assert_impl(bool condition, const char *file, int line,27	const char *message)28{29	if (condition)30		return;31 32	return die_impl(file, line, message);33}34 35#define die(msg) die_impl(__FILE__, __LINE__, msg)36#undef assert37#define assert(exp) assert_impl(exp, __FILE__, __LINE__, #exp)38 39#include "isl_test_cpp-generic.cc"40 41/* Test that isl_bool values are returned correctly.42 *43 * In particular, check the conversion to bool in case of true and false, and44 * exception throwing in case of error.45 */46static void test_return_bool(isl::ctx ctx)47{48	isl::set empty(ctx, "{ : false }");49	isl::set univ(ctx, "{ : }");50	isl::set null;51 52	bool b_true = empty.is_empty();53	bool b_false = univ.is_empty();54	bool caught = false;55	try {56		null.is_empty();57		die("no exception raised");58	} catch (const isl::exception_invalid &e) {59		caught = true;60	}61 62	assert(b_true);63	assert(!b_false);64	assert(caught);65}66 67/* Test that return values are handled correctly.68 *69 * Test that isl C++ objects, integers, boolean values, and strings are70 * returned correctly.71 */72static void test_return(isl::ctx ctx)73{74	test_return_obj(ctx);75	test_return_int(ctx);76	test_return_bool(ctx);77	test_return_string(ctx);78}79 80/* Test that foreach functions are modeled correctly.81 *82 * Verify that lambdas are correctly called as callback of a 'foreach'83 * function and that variables captured by the lambda work correctly. Also84 * check that the foreach function handles exceptions thrown from85 * the lambda and that it propagates the exception.86 */87static void test_foreach(isl::ctx ctx)88{89	isl::set s(ctx, "{ [0]; [1]; [2] }");90 91	std::vector<isl::basic_set> basic_sets;92 93	auto add_to_vector = [&] (isl::basic_set bs) {94		basic_sets.push_back(bs);95	};96 97	s.foreach_basic_set(add_to_vector);98 99	assert(basic_sets.size() == 3);100	assert(isl::set(basic_sets[0]).is_subset(s));101	assert(isl::set(basic_sets[1]).is_subset(s));102	assert(isl::set(basic_sets[2]).is_subset(s));103	assert(!basic_sets[0].is_equal(basic_sets[1]));104 105	auto fail = [&] (isl::basic_set bs) {106		throw "fail";107	};108 109	bool caught = false;110	try {111		s.foreach_basic_set(fail);112		die("no exception raised");113	} catch (char const *s) {114		caught = true;115	}116	assert(caught);117}118 119/* Test the functionality of "foreach_scc" functions.120 *121 * In particular, test it on a list of elements that can be completely sorted122 * but where two of the elements ("a" and "b") are incomparable.123 */124static void test_foreach_scc(isl::ctx ctx)125{126	isl::multi_pw_aff id;127	isl::id_list list(ctx, 3);128	isl::id_list sorted(ctx, 3);129	std::map<std::string, isl::map> data = {130		{ "a", isl::map(ctx, "{ [0] -> [1] }") },131		{ "b", isl::map(ctx, "{ [1] -> [0] }") },132		{ "c", isl::map(ctx, "{ [i = 0:1] -> [i] }") },133	};134 135	for (const auto &kvp: data)136		list = list.add(kvp.first);137	id = data.at("a").space().domain().identity_multi_pw_aff_on_domain();138	list.foreach_scc([&data, &id] (isl::id a, isl::id b) {139		auto map = data.at(b.name()).apply_domain(data.at(a.name()));140		return !map.lex_ge_at(id).is_empty();141	}, [&sorted] (isl::id_list scc) {142		assert(scc.size() == 1);143		sorted = sorted.concat(scc);144	});145	assert(sorted.size() == 3);146	assert(sorted.at(0).name() == "b");147	assert(sorted.at(1).name() == "c");148	assert(sorted.at(2).name() == "a");149}150 151/* Test the functionality of "every" functions.152 *153 * In particular, test the generic functionality and154 * test that exceptions are properly propagated.155 */156static void test_every(isl::ctx ctx)157{158	isl::union_set us(ctx, "{ A[i]; B[j] }");159 160	test_every_generic(ctx);161 162	auto fail = [] (isl::set s) -> bool {163		throw "fail";164	};165	bool caught = false;166	try {167		us.every_set(fail);168		die("no exception raised");169	} catch (char const *s) {170		caught = true;171	}172	assert(caught);173}174 175/* Test that an exception is generated for an isl error and176 * that the error message is captured by the exception.177 * Also check that the exception can be copied and that copying178 * does not throw any exceptions.179 */180static void test_exception(isl::ctx ctx)181{182	isl::multi_union_pw_aff mupa(ctx, "[]");183	isl::exception copy;184 185	static_assert(std::is_nothrow_copy_constructible<isl::exception>::value,186		"exceptions must be nothrow-copy-constructible");187	static_assert(std::is_nothrow_assignable<isl::exception,188						isl::exception>::value,189		"exceptions must be nothrow-assignable");190 191	try {192		auto umap = isl::union_map::from(mupa);193	} catch (const isl::exception_unsupported &error) {194		die("caught wrong exception");195	} catch (const isl::exception &error) {196		assert(strstr(error.what(), "without explicit domain"));197		copy = error;198	}199	assert(strstr(copy.what(), "without explicit domain"));200}201 202/* Test basic schedule tree functionality.203 *204 * In particular, create a simple schedule tree and205 * - perform some generic tests206 * - test map_descendant_bottom_up in the failing case207 * - test foreach_descendant_top_down208 * - test every_descendant209 */210static void test_schedule_tree(isl::ctx ctx)211{212	auto root = test_schedule_tree_generic(ctx);213 214	auto fail_map = [](isl::schedule_node node) {215		throw "fail";216		return node;217	};218	auto caught = false;219	try {220		root.map_descendant_bottom_up(fail_map);221		die("no exception raised");222	} catch (char const *s) {223		caught = true;224	}225	assert(caught);226 227	int count = 0;228	auto inc_count = [&count](isl::schedule_node node) {229		count++;230		return true;231	};232	root.foreach_descendant_top_down(inc_count);233	assert(count == 8);234 235	count = 0;236	auto inc_count_once = [&count](isl::schedule_node node) {237		count++;238		return false;239	};240	root.foreach_descendant_top_down(inc_count_once);241	assert(count == 1);242 243	auto is_not_domain = [](isl::schedule_node node) {244		return !node.isa<isl::schedule_node_domain>();245	};246	assert(root.child(0).every_descendant(is_not_domain));247	assert(!root.every_descendant(is_not_domain));248 249	auto fail = [](isl::schedule_node node) {250		throw "fail";251		return true;252	};253	caught = false;254	try {255		root.every_descendant(fail);256		die("no exception raised");257	} catch (char const *s) {258		caught = true;259	}260	assert(caught);261 262	auto domain = root.as<isl::schedule_node_domain>().domain();263	auto filters = isl::union_set(ctx, "{}");264	auto collect_filters = [&filters](isl::schedule_node node) {265		if (node.isa<isl::schedule_node_filter>()) {266			auto filter = node.as<isl::schedule_node_filter>();267			filters = filters.unite(filter.filter());268		}269		return true;270	};271	root.every_descendant(collect_filters);272	assert(domain.is_equal(filters));273}274 275/* Test basic AST generation from a schedule tree.276 *277 * In particular, create a simple schedule tree and278 * - perform some generic tests279 * - test at_each_domain in the failing case280 */281static void test_ast_build(isl::ctx ctx)282{283	auto schedule = test_ast_build_generic(ctx);284 285	bool do_fail = true;286	int count_ast_fail = 0;287	auto fail_inc_count_ast =288	    [&count_ast_fail, &do_fail](isl::ast_node node,289					isl::ast_build build) {290		count_ast_fail++;291		if (do_fail)292			throw "fail";293		return node;294	};295	auto build = isl::ast_build(ctx);296	build = build.set_at_each_domain(fail_inc_count_ast);297	auto caught = false;298	try {299		auto ast = build.node_from(schedule);300	} catch (char const *s) {301		caught = true;302	}303	assert(caught);304	assert(count_ast_fail > 0);305	auto build_copy = build;306	int count_ast = 0;307	auto inc_count_ast =308	    [&count_ast](isl::ast_node node, isl::ast_build build) {309		count_ast++;310		return node;311	};312	build_copy = build_copy.set_at_each_domain(inc_count_ast);313	auto ast = build_copy.node_from(schedule);314	assert(count_ast == 2);315	count_ast_fail = 0;316	do_fail = false;317	ast = build.node_from(schedule);318	assert(count_ast_fail == 2);319}320 321/* Basic test of the templated interface.322 *323 * Intersecting the domain of an access relation324 * with statement instances should be allowed,325 * while intersecting the range with statement instances326 * should result in a compile-time error.327 */328static void test_typed(isl::ctx ctx)329{330	struct ST {};331	struct AR {};332	isl::typed::map<ST, AR> access(ctx, "{ S[i, j] -> A[i] }");333	isl::typed::set<ST> instances(ctx, "{ S[i, j] : 0 <= i, j < 10 }");334 335#ifndef COMPILE_ERROR336	access.intersect_domain(instances);337#else338	access.intersect_range(instances);339#endif340}341 342/* Test the (unchecked) isl C++ interface343 *344 * This includes:345 *  - The isl C <-> C++ pointer interface346 *  - Object construction347 *  - Different parameter types348 *  - Different return types349 *  - Foreach functions350 *  - Foreach SCC function351 *  - Exceptions352 *  - Spaces353 *  - Schedule trees354 *  - AST generation355 *  - AST expression generation356 *  - Templated interface357 */358int main()359{360	isl_ctx *ctx = isl_ctx_alloc();361 362	isl_options_set_on_error(ctx, ISL_ON_ERROR_ABORT);363 364	test_pointer(ctx);365	test_constructors(ctx);366	test_parameters(ctx);367	test_return(ctx);368	test_foreach(ctx);369	test_foreach_scc(ctx);370	test_every(ctx);371	test_exception(ctx);372	test_space(ctx);373	test_schedule_tree(ctx);374	test_ast_build(ctx);375	test_ast_build_expr(ctx);376	test_typed(ctx);377 378	isl_ctx_free(ctx);379 380	return EXIT_SUCCESS;381}382