llvm-project
587 строк · 17.3 Кб
1from ctypes import *2
3isl = cdll.LoadLibrary("libisl.so")4
5
6class Context:7defaultInstance = None8instances = {}9
10def __init__(self):11ptr = isl.isl_ctx_alloc()12self.ptr = ptr13Context.instances[ptr] = self14
15def __del__(self):16isl.isl_ctx_free(self)17
18def from_param(self):19return self.ptr20
21@staticmethod22def from_ptr(ptr):23return Context.instances[ptr]24
25@staticmethod26def getDefaultInstance():27if Context.defaultInstance == None:28Context.defaultInstance = Context()29
30return Context.defaultInstance31
32
33class IslObject:34def __init__(self, string="", ctx=None, ptr=None):35self.initialize_isl_methods()36if ptr != None:37self.ptr = ptr38self.ctx = self.get_isl_method("get_ctx")(self)39return40
41if ctx == None:42ctx = Context.getDefaultInstance()43
44self.ctx = ctx45self.ptr = self.get_isl_method("read_from_str")(ctx, string, -1)46
47def __del__(self):48self.get_isl_method("free")(self)49
50def from_param(self):51return self.ptr52
53@property54def context(self):55return self.ctx56
57def __repr__(self):58p = Printer(self.ctx)59self.to_printer(p)60return p.getString()61
62def __str__(self):63p = Printer(self.ctx)64self.to_printer(p)65return p.getString()66
67@staticmethod68def isl_name():69return "No isl name available"70
71def initialize_isl_methods(self):72if hasattr(self.__class__, "initialized"):73return74
75self.__class__.initalized = True76self.get_isl_method("read_from_str").argtypes = [Context, c_char_p, c_int]77self.get_isl_method("copy").argtypes = [self.__class__]78self.get_isl_method("copy").restype = c_int79self.get_isl_method("free").argtypes = [self.__class__]80self.get_isl_method("get_ctx").argtypes = [self.__class__]81self.get_isl_method("get_ctx").restype = Context.from_ptr82getattr(isl, "isl_printer_print_" + self.isl_name()).argtypes = [83Printer,84self.__class__,85]86
87def get_isl_method(self, name):88return getattr(isl, "isl_" + self.isl_name() + "_" + name)89
90def to_printer(self, printer):91getattr(isl, "isl_printer_print_" + self.isl_name())(printer, self)92
93
94class BSet(IslObject):95@staticmethod96def from_ptr(ptr):97if not ptr:98return99return BSet(ptr=ptr)100
101@staticmethod102def isl_name():103return "basic_set"104
105
106class Set(IslObject):107@staticmethod108def from_ptr(ptr):109if not ptr:110return111return Set(ptr=ptr)112
113@staticmethod114def isl_name():115return "set"116
117
118class USet(IslObject):119@staticmethod120def from_ptr(ptr):121if not ptr:122return123return USet(ptr=ptr)124
125@staticmethod126def isl_name():127return "union_set"128
129
130class BMap(IslObject):131@staticmethod132def from_ptr(ptr):133if not ptr:134return135return BMap(ptr=ptr)136
137def __mul__(self, set):138return self.intersect_domain(set)139
140@staticmethod141def isl_name():142return "basic_map"143
144
145class Map(IslObject):146@staticmethod147def from_ptr(ptr):148if not ptr:149return150return Map(ptr=ptr)151
152def __mul__(self, set):153return self.intersect_domain(set)154
155@staticmethod156def isl_name():157return "map"158
159@staticmethod160def lex_lt(dim):161dim = isl.isl_dim_copy(dim)162return isl.isl_map_lex_lt(dim)163
164@staticmethod165def lex_le(dim):166dim = isl.isl_dim_copy(dim)167return isl.isl_map_lex_le(dim)168
169@staticmethod170def lex_gt(dim):171dim = isl.isl_dim_copy(dim)172return isl.isl_map_lex_gt(dim)173
174@staticmethod175def lex_ge(dim):176dim = isl.isl_dim_copy(dim)177return isl.isl_map_lex_ge(dim)178
179
180class UMap(IslObject):181@staticmethod182def from_ptr(ptr):183if not ptr:184return185return UMap(ptr=ptr)186
187@staticmethod188def isl_name():189return "union_map"190
191
192class Dim(IslObject):193@staticmethod194def from_ptr(ptr):195if not ptr:196return197return Dim(ptr=ptr)198
199@staticmethod200def isl_name():201return "dim"202
203def initialize_isl_methods(self):204if hasattr(self.__class__, "initialized"):205return206
207self.__class__.initalized = True208self.get_isl_method("copy").argtypes = [self.__class__]209self.get_isl_method("copy").restype = c_int210self.get_isl_method("free").argtypes = [self.__class__]211self.get_isl_method("get_ctx").argtypes = [self.__class__]212self.get_isl_method("get_ctx").restype = Context.from_ptr213
214def __repr__(self):215return str(self)216
217def __str__(self):218
219dimParam = isl.isl_dim_size(self, 1)220dimIn = isl.isl_dim_size(self, 2)221dimOut = isl.isl_dim_size(self, 3)222
223if dimIn:224return "<dim In:%s, Out:%s, Param:%s>" % (dimIn, dimOut, dimParam)225
226return "<dim Set:%s, Param:%s>" % (dimOut, dimParam)227
228
229class Printer:230FORMAT_ISL = 0231FORMAT_POLYLIB = 1232FORMAT_POLYLIB_CONSTRAINTS = 2233FORMAT_OMEGA = 3234FORMAT_C = 4235FORMAT_LATEX = 5236FORMAT_EXT_POLYLIB = 6237
238def __init__(self, ctx=None):239if ctx == None:240ctx = Context.getDefaultInstance()241
242self.ctx = ctx243self.ptr = isl.isl_printer_to_str(ctx)244
245def setFormat(self, format):246self.ptr = isl.isl_printer_set_output_format(self, format)247
248def from_param(self):249return self.ptr250
251def __del__(self):252isl.isl_printer_free(self)253
254def getString(self):255return isl.isl_printer_get_str(self)256
257
258functions = [259# Unary properties260("is_empty", BSet, [BSet], c_int),261("is_empty", Set, [Set], c_int),262("is_empty", USet, [USet], c_int),263("is_empty", BMap, [BMap], c_int),264("is_empty", Map, [Map], c_int),265("is_empty", UMap, [UMap], c_int),266# ("is_universe", Set, [Set], c_int),267# ("is_universe", Map, [Map], c_int),268("is_single_valued", Map, [Map], c_int),269("is_bijective", Map, [Map], c_int),270("is_wrapping", BSet, [BSet], c_int),271("is_wrapping", Set, [Set], c_int),272# Binary properties273("is_equal", BSet, [BSet, BSet], c_int),274("is_equal", Set, [Set, Set], c_int),275("is_equal", USet, [USet, USet], c_int),276("is_equal", BMap, [BMap, BMap], c_int),277("is_equal", Map, [Map, Map], c_int),278("is_equal", UMap, [UMap, UMap], c_int),279# is_disjoint missing280# ("is_subset", BSet, [BSet, BSet], c_int),281("is_subset", Set, [Set, Set], c_int),282("is_subset", USet, [USet, USet], c_int),283("is_subset", BMap, [BMap, BMap], c_int),284("is_subset", Map, [Map, Map], c_int),285("is_subset", UMap, [UMap, UMap], c_int),286# ("is_strict_subset", BSet, [BSet, BSet], c_int),287("is_strict_subset", Set, [Set, Set], c_int),288("is_strict_subset", USet, [USet, USet], c_int),289("is_strict_subset", BMap, [BMap, BMap], c_int),290("is_strict_subset", Map, [Map, Map], c_int),291("is_strict_subset", UMap, [UMap, UMap], c_int),292# Unary Operations293("complement", Set, [Set], Set),294("reverse", BMap, [BMap], BMap),295("reverse", Map, [Map], Map),296("reverse", UMap, [UMap], UMap),297# Projection missing298("range", BMap, [BMap], BSet),299("range", Map, [Map], Set),300("range", UMap, [UMap], USet),301("domain", BMap, [BMap], BSet),302("domain", Map, [Map], Set),303("domain", UMap, [UMap], USet),304("identity", Set, [Set], Map),305("identity", USet, [USet], UMap),306("deltas", BMap, [BMap], BSet),307("deltas", Map, [Map], Set),308("deltas", UMap, [UMap], USet),309("coalesce", Set, [Set], Set),310("coalesce", USet, [USet], USet),311("coalesce", Map, [Map], Map),312("coalesce", UMap, [UMap], UMap),313("detect_equalities", BSet, [BSet], BSet),314("detect_equalities", Set, [Set], Set),315("detect_equalities", USet, [USet], USet),316("detect_equalities", BMap, [BMap], BMap),317("detect_equalities", Map, [Map], Map),318("detect_equalities", UMap, [UMap], UMap),319("convex_hull", Set, [Set], Set),320("convex_hull", Map, [Map], Map),321("simple_hull", Set, [Set], Set),322("simple_hull", Map, [Map], Map),323("affine_hull", BSet, [BSet], BSet),324("affine_hull", Set, [Set], BSet),325("affine_hull", USet, [USet], USet),326("affine_hull", BMap, [BMap], BMap),327("affine_hull", Map, [Map], BMap),328("affine_hull", UMap, [UMap], UMap),329("polyhedral_hull", Set, [Set], Set),330("polyhedral_hull", USet, [USet], USet),331("polyhedral_hull", Map, [Map], Map),332("polyhedral_hull", UMap, [UMap], UMap),333# Power missing334# Transitive closure missing335# Reaching path lengths missing336("wrap", BMap, [BMap], BSet),337("wrap", Map, [Map], Set),338("wrap", UMap, [UMap], USet),339("unwrap", BSet, [BMap], BMap),340("unwrap", Set, [Map], Map),341("unwrap", USet, [UMap], UMap),342("flatten", Set, [Set], Set),343("flatten", Map, [Map], Map),344("flatten_map", Set, [Set], Map),345# Dimension manipulation missing346# Binary Operations347("intersect", BSet, [BSet, BSet], BSet),348("intersect", Set, [Set, Set], Set),349("intersect", USet, [USet, USet], USet),350("intersect", BMap, [BMap, BMap], BMap),351("intersect", Map, [Map, Map], Map),352("intersect", UMap, [UMap, UMap], UMap),353("intersect_domain", BMap, [BMap, BSet], BMap),354("intersect_domain", Map, [Map, Set], Map),355("intersect_domain", UMap, [UMap, USet], UMap),356("intersect_range", BMap, [BMap, BSet], BMap),357("intersect_range", Map, [Map, Set], Map),358("intersect_range", UMap, [UMap, USet], UMap),359("union", BSet, [BSet, BSet], Set),360("union", Set, [Set, Set], Set),361("union", USet, [USet, USet], USet),362("union", BMap, [BMap, BMap], Map),363("union", Map, [Map, Map], Map),364("union", UMap, [UMap, UMap], UMap),365("subtract", Set, [Set, Set], Set),366("subtract", Map, [Map, Map], Map),367("subtract", USet, [USet, USet], USet),368("subtract", UMap, [UMap, UMap], UMap),369("apply", BSet, [BSet, BMap], BSet),370("apply", Set, [Set, Map], Set),371("apply", USet, [USet, UMap], USet),372("apply_domain", BMap, [BMap, BMap], BMap),373("apply_domain", Map, [Map, Map], Map),374("apply_domain", UMap, [UMap, UMap], UMap),375("apply_range", BMap, [BMap, BMap], BMap),376("apply_range", Map, [Map, Map], Map),377("apply_range", UMap, [UMap, UMap], UMap),378("gist", BSet, [BSet, BSet], BSet),379("gist", Set, [Set, Set], Set),380("gist", USet, [USet, USet], USet),381("gist", BMap, [BMap, BMap], BMap),382("gist", Map, [Map, Map], Map),383("gist", UMap, [UMap, UMap], UMap),384# Lexicographic Optimizations385# partial_lexmin missing386("lexmin", BSet, [BSet], BSet),387("lexmin", Set, [Set], Set),388("lexmin", USet, [USet], USet),389("lexmin", BMap, [BMap], BMap),390("lexmin", Map, [Map], Map),391("lexmin", UMap, [UMap], UMap),392("lexmax", BSet, [BSet], BSet),393("lexmax", Set, [Set], Set),394("lexmax", USet, [USet], USet),395("lexmax", BMap, [BMap], BMap),396("lexmax", Map, [Map], Map),397("lexmax", UMap, [UMap], UMap),398# Undocumented399("lex_lt_union_set", USet, [USet, USet], UMap),400("lex_le_union_set", USet, [USet, USet], UMap),401("lex_gt_union_set", USet, [USet, USet], UMap),402("lex_ge_union_set", USet, [USet, USet], UMap),403]
404keep_functions = [405# Unary properties406("get_dim", BSet, [BSet], Dim),407("get_dim", Set, [Set], Dim),408("get_dim", USet, [USet], Dim),409("get_dim", BMap, [BMap], Dim),410("get_dim", Map, [Map], Dim),411("get_dim", UMap, [UMap], Dim),412]
413
414
415def addIslFunction(object, name):416functionName = "isl_" + object.isl_name() + "_" + name417islFunction = getattr(isl, functionName)418if len(islFunction.argtypes) == 1:419f = lambda a: islFunctionOneOp(islFunction, a)420elif len(islFunction.argtypes) == 2:421f = lambda a, b: islFunctionTwoOp(islFunction, a, b)422object.__dict__[name] = f423
424
425def islFunctionOneOp(islFunction, ops):426ops = getattr(isl, "isl_" + ops.isl_name() + "_copy")(ops)427return islFunction(ops)428
429
430def islFunctionTwoOp(islFunction, opOne, opTwo):431opOne = getattr(isl, "isl_" + opOne.isl_name() + "_copy")(opOne)432opTwo = getattr(isl, "isl_" + opTwo.isl_name() + "_copy")(opTwo)433return islFunction(opOne, opTwo)434
435
436for (operation, base, operands, ret) in functions:437functionName = "isl_" + base.isl_name() + "_" + operation438islFunction = getattr(isl, functionName)439if len(operands) == 1:440islFunction.argtypes = [c_int]441elif len(operands) == 2:442islFunction.argtypes = [c_int, c_int]443
444if ret == c_int:445islFunction.restype = ret446else:447islFunction.restype = ret.from_ptr448
449addIslFunction(base, operation)450
451
452def addIslFunctionKeep(object, name):453functionName = "isl_" + object.isl_name() + "_" + name454islFunction = getattr(isl, functionName)455if len(islFunction.argtypes) == 1:456f = lambda a: islFunctionOneOpKeep(islFunction, a)457elif len(islFunction.argtypes) == 2:458f = lambda a, b: islFunctionTwoOpKeep(islFunction, a, b)459object.__dict__[name] = f460
461
462def islFunctionOneOpKeep(islFunction, ops):463return islFunction(ops)464
465
466def islFunctionTwoOpKeep(islFunction, opOne, opTwo):467return islFunction(opOne, opTwo)468
469
470for (operation, base, operands, ret) in keep_functions:471functionName = "isl_" + base.isl_name() + "_" + operation472islFunction = getattr(isl, functionName)473if len(operands) == 1:474islFunction.argtypes = [c_int]475elif len(operands) == 2:476islFunction.argtypes = [c_int, c_int]477
478if ret == c_int:479islFunction.restype = ret480else:481islFunction.restype = ret.from_ptr482
483addIslFunctionKeep(base, operation)484
485isl.isl_ctx_free.argtypes = [Context]486isl.isl_basic_set_read_from_str.argtypes = [Context, c_char_p, c_int]487isl.isl_set_read_from_str.argtypes = [Context, c_char_p, c_int]488isl.isl_basic_set_copy.argtypes = [BSet]489isl.isl_basic_set_copy.restype = c_int490isl.isl_set_copy.argtypes = [Set]491isl.isl_set_copy.restype = c_int492isl.isl_set_copy.argtypes = [Set]493isl.isl_set_copy.restype = c_int494isl.isl_set_free.argtypes = [Set]495isl.isl_basic_set_get_ctx.argtypes = [BSet]496isl.isl_basic_set_get_ctx.restype = Context.from_ptr497isl.isl_set_get_ctx.argtypes = [Set]498isl.isl_set_get_ctx.restype = Context.from_ptr499isl.isl_basic_set_get_dim.argtypes = [BSet]500isl.isl_basic_set_get_dim.restype = Dim.from_ptr501isl.isl_set_get_dim.argtypes = [Set]502isl.isl_set_get_dim.restype = Dim.from_ptr503isl.isl_union_set_get_dim.argtypes = [USet]504isl.isl_union_set_get_dim.restype = Dim.from_ptr505
506isl.isl_basic_map_read_from_str.argtypes = [Context, c_char_p, c_int]507isl.isl_map_read_from_str.argtypes = [Context, c_char_p, c_int]508isl.isl_basic_map_free.argtypes = [BMap]509isl.isl_map_free.argtypes = [Map]510isl.isl_basic_map_copy.argtypes = [BMap]511isl.isl_basic_map_copy.restype = c_int512isl.isl_map_copy.argtypes = [Map]513isl.isl_map_copy.restype = c_int514isl.isl_map_get_ctx.argtypes = [Map]515isl.isl_basic_map_get_ctx.argtypes = [BMap]516isl.isl_basic_map_get_ctx.restype = Context.from_ptr517isl.isl_map_get_ctx.argtypes = [Map]518isl.isl_map_get_ctx.restype = Context.from_ptr519isl.isl_basic_map_get_dim.argtypes = [BMap]520isl.isl_basic_map_get_dim.restype = Dim.from_ptr521isl.isl_map_get_dim.argtypes = [Map]522isl.isl_map_get_dim.restype = Dim.from_ptr523isl.isl_union_map_get_dim.argtypes = [UMap]524isl.isl_union_map_get_dim.restype = Dim.from_ptr525isl.isl_printer_free.argtypes = [Printer]526isl.isl_printer_to_str.argtypes = [Context]527isl.isl_printer_print_basic_set.argtypes = [Printer, BSet]528isl.isl_printer_print_set.argtypes = [Printer, Set]529isl.isl_printer_print_basic_map.argtypes = [Printer, BMap]530isl.isl_printer_print_map.argtypes = [Printer, Map]531isl.isl_printer_get_str.argtypes = [Printer]532isl.isl_printer_get_str.restype = c_char_p533isl.isl_printer_set_output_format.argtypes = [Printer, c_int]534isl.isl_printer_set_output_format.restype = c_int535isl.isl_dim_size.argtypes = [Dim, c_int]536isl.isl_dim_size.restype = c_int537
538isl.isl_map_lex_lt.argtypes = [c_int]539isl.isl_map_lex_lt.restype = Map.from_ptr540isl.isl_map_lex_le.argtypes = [c_int]541isl.isl_map_lex_le.restype = Map.from_ptr542isl.isl_map_lex_gt.argtypes = [c_int]543isl.isl_map_lex_gt.restype = Map.from_ptr544isl.isl_map_lex_ge.argtypes = [c_int]545isl.isl_map_lex_ge.restype = Map.from_ptr546
547isl.isl_union_map_compute_flow.argtypes = [548c_int,549c_int,550c_int,551c_int,552c_void_p,553c_void_p,554c_void_p,555c_void_p,556]
557
558
559def dependences(sink, must_source, may_source, schedule):560sink = getattr(isl, "isl_" + sink.isl_name() + "_copy")(sink)561must_source = getattr(isl, "isl_" + must_source.isl_name() + "_copy")(must_source)562may_source = getattr(isl, "isl_" + may_source.isl_name() + "_copy")(may_source)563schedule = getattr(isl, "isl_" + schedule.isl_name() + "_copy")(schedule)564must_dep = c_int()565may_dep = c_int()566must_no_source = c_int()567may_no_source = c_int()568isl.isl_union_map_compute_flow(569sink,570must_source,571may_source,572schedule,573byref(must_dep),574byref(may_dep),575byref(must_no_source),576byref(may_no_source),577)578
579return (580UMap.from_ptr(must_dep),581UMap.from_ptr(may_dep),582USet.from_ptr(must_no_source),583USet.from_ptr(may_no_source),584)585
586
587__all__ = ["Set", "Map", "Printer", "Context"]588