GICHelper.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //===- GmpConv.cpp - Recreate LLVM IR from the Scop. ---------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Functions for converting between gmp objects and llvm::APInt.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "polly/Support/GICHelper.h"
  13. #include "llvm/ADT/APInt.h"
  14. #include "isl/val.h"
  15. using namespace llvm;
  16. __isl_give isl_val *polly::isl_valFromAPInt(isl_ctx *Ctx, const APInt Int,
  17. bool IsSigned) {
  18. APInt Abs;
  19. isl_val *v;
  20. // As isl is interpreting the input always as unsigned value, we need some
  21. // additional pre and post processing to import signed values. The approach
  22. // we take is to first obtain the absolute value of Int and then negate the
  23. // value after it has been imported to isl.
  24. //
  25. // It should be noted that the smallest integer value represented in two's
  26. // complement with a certain amount of bits does not have a corresponding
  27. // positive representation in two's complement representation with the same
  28. // number of bits. E.g. 110 (-2) does not have a corresponding value for (2).
  29. // To ensure that there is always a corresponding value available we first
  30. // sign-extend the input by one bit and only then take the absolute value.
  31. if (IsSigned)
  32. Abs = Int.sext(Int.getBitWidth() + 1).abs();
  33. else
  34. Abs = Int;
  35. const uint64_t *Data = Abs.getRawData();
  36. unsigned Words = Abs.getNumWords();
  37. v = isl_val_int_from_chunks(Ctx, Words, sizeof(uint64_t), Data);
  38. if (IsSigned && Int.isNegative())
  39. v = isl_val_neg(v);
  40. return v;
  41. }
  42. APInt polly::APIntFromVal(__isl_take isl_val *Val) {
  43. uint64_t *Data;
  44. int NumChunks;
  45. const static int ChunkSize = sizeof(uint64_t);
  46. assert(isl_val_is_int(Val) && "Only integers can be converted to APInt");
  47. NumChunks = isl_val_n_abs_num_chunks(Val, ChunkSize);
  48. Data = (uint64_t *)malloc(NumChunks * ChunkSize);
  49. isl_val_get_abs_num_chunks(Val, ChunkSize, Data);
  50. int NumBits = CHAR_BIT * ChunkSize * NumChunks;
  51. APInt A(NumBits, NumChunks, Data);
  52. // As isl provides only an interface to obtain data that describes the
  53. // absolute value of an isl_val, A at this point always contains a positive
  54. // number. In case Val was originally negative, we expand the size of A by
  55. // one and negate the value (in two's complement representation). As a result,
  56. // the new value in A corresponds now with Val.
  57. if (isl_val_is_neg(Val)) {
  58. A = A.zext(A.getBitWidth() + 1);
  59. A = -A;
  60. }
  61. // isl may represent small numbers with more than the minimal number of bits.
  62. // We truncate the APInt to the minimal number of bits needed to represent the
  63. // signed value it contains, to ensure that the bitwidth is always minimal.
  64. if (A.getMinSignedBits() < A.getBitWidth())
  65. A = A.trunc(A.getMinSignedBits());
  66. free(Data);
  67. isl_val_free(Val);
  68. return A;
  69. }
  70. template <typename ISLTy, typename ISL_CTX_GETTER, typename ISL_PRINTER>
  71. static inline std::string stringFromIslObjInternal(__isl_keep ISLTy *isl_obj,
  72. ISL_CTX_GETTER ctx_getter_fn,
  73. ISL_PRINTER printer_fn,
  74. std::string DefaultValue) {
  75. if (!isl_obj)
  76. return DefaultValue;
  77. isl_ctx *ctx = ctx_getter_fn(isl_obj);
  78. isl_printer *p = isl_printer_to_str(ctx);
  79. p = printer_fn(p, isl_obj);
  80. char *char_str = isl_printer_get_str(p);
  81. std::string string;
  82. if (char_str)
  83. string = char_str;
  84. else
  85. string = DefaultValue;
  86. free(char_str);
  87. isl_printer_free(p);
  88. return string;
  89. }
  90. #define ISL_C_OBJECT_TO_STRING(name) \
  91. std::string polly::stringFromIslObj(__isl_keep isl_##name *Obj, \
  92. std::string DefaultValue) { \
  93. return stringFromIslObjInternal(Obj, isl_##name##_get_ctx, \
  94. isl_printer_print_##name, DefaultValue); \
  95. }
  96. ISL_C_OBJECT_TO_STRING(aff)
  97. ISL_C_OBJECT_TO_STRING(ast_expr)
  98. ISL_C_OBJECT_TO_STRING(ast_node)
  99. ISL_C_OBJECT_TO_STRING(basic_map)
  100. ISL_C_OBJECT_TO_STRING(basic_set)
  101. ISL_C_OBJECT_TO_STRING(map)
  102. ISL_C_OBJECT_TO_STRING(set)
  103. ISL_C_OBJECT_TO_STRING(id)
  104. ISL_C_OBJECT_TO_STRING(multi_aff)
  105. ISL_C_OBJECT_TO_STRING(multi_pw_aff)
  106. ISL_C_OBJECT_TO_STRING(multi_union_pw_aff)
  107. ISL_C_OBJECT_TO_STRING(point)
  108. ISL_C_OBJECT_TO_STRING(pw_aff)
  109. ISL_C_OBJECT_TO_STRING(pw_multi_aff)
  110. ISL_C_OBJECT_TO_STRING(schedule)
  111. ISL_C_OBJECT_TO_STRING(schedule_node)
  112. ISL_C_OBJECT_TO_STRING(space)
  113. ISL_C_OBJECT_TO_STRING(union_access_info)
  114. ISL_C_OBJECT_TO_STRING(union_flow)
  115. ISL_C_OBJECT_TO_STRING(union_set)
  116. ISL_C_OBJECT_TO_STRING(union_map)
  117. ISL_C_OBJECT_TO_STRING(union_pw_aff)
  118. ISL_C_OBJECT_TO_STRING(union_pw_multi_aff)
  119. static void replace(std::string &str, const std::string &find,
  120. const std::string &replace) {
  121. size_t pos = 0;
  122. while ((pos = str.find(find, pos)) != std::string::npos) {
  123. str.replace(pos, find.length(), replace);
  124. pos += replace.length();
  125. }
  126. }
  127. static void makeIslCompatible(std::string &str) {
  128. replace(str, ".", "_");
  129. replace(str, "\"", "_");
  130. replace(str, " ", "__");
  131. replace(str, "=>", "TO");
  132. replace(str, "+", "_");
  133. }
  134. std::string polly::getIslCompatibleName(const std::string &Prefix,
  135. const std::string &Middle,
  136. const std::string &Suffix) {
  137. std::string S = Prefix + Middle + Suffix;
  138. makeIslCompatible(S);
  139. return S;
  140. }
  141. std::string polly::getIslCompatibleName(const std::string &Prefix,
  142. const std::string &Name, long Number,
  143. const std::string &Suffix,
  144. bool UseInstructionNames) {
  145. std::string S = Prefix;
  146. if (UseInstructionNames)
  147. S += std::string("_") + Name;
  148. else
  149. S += std::to_string(Number);
  150. S += Suffix;
  151. makeIslCompatible(S);
  152. return S;
  153. }
  154. std::string polly::getIslCompatibleName(const std::string &Prefix,
  155. const Value *Val, long Number,
  156. const std::string &Suffix,
  157. bool UseInstructionNames) {
  158. std::string ValStr;
  159. if (UseInstructionNames && Val->hasName())
  160. ValStr = std::string("_") + std::string(Val->getName());
  161. else
  162. ValStr = std::to_string(Number);
  163. return getIslCompatibleName(Prefix, ValStr, Suffix);
  164. }
  165. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  166. #define ISL_DUMP_OBJECT_IMPL(NAME) \
  167. void polly::dumpIslObj(const isl::NAME &Obj) { \
  168. isl_##NAME##_dump(Obj.get()); \
  169. } \
  170. void polly::dumpIslObj(isl_##NAME *Obj) { isl_##NAME##_dump(Obj); }
  171. ISL_DUMP_OBJECT_IMPL(aff)
  172. ISL_DUMP_OBJECT_IMPL(aff_list)
  173. ISL_DUMP_OBJECT_IMPL(ast_expr)
  174. ISL_DUMP_OBJECT_IMPL(ast_node)
  175. ISL_DUMP_OBJECT_IMPL(ast_node_list)
  176. ISL_DUMP_OBJECT_IMPL(basic_map)
  177. ISL_DUMP_OBJECT_IMPL(basic_map_list)
  178. ISL_DUMP_OBJECT_IMPL(basic_set)
  179. ISL_DUMP_OBJECT_IMPL(basic_set_list)
  180. ISL_DUMP_OBJECT_IMPL(constraint)
  181. ISL_DUMP_OBJECT_IMPL(id)
  182. ISL_DUMP_OBJECT_IMPL(id_list)
  183. ISL_DUMP_OBJECT_IMPL(id_to_ast_expr)
  184. ISL_DUMP_OBJECT_IMPL(local_space)
  185. ISL_DUMP_OBJECT_IMPL(map)
  186. ISL_DUMP_OBJECT_IMPL(map_list)
  187. ISL_DUMP_OBJECT_IMPL(multi_aff)
  188. ISL_DUMP_OBJECT_IMPL(multi_pw_aff)
  189. ISL_DUMP_OBJECT_IMPL(multi_union_pw_aff)
  190. ISL_DUMP_OBJECT_IMPL(multi_val)
  191. ISL_DUMP_OBJECT_IMPL(point)
  192. ISL_DUMP_OBJECT_IMPL(pw_aff)
  193. ISL_DUMP_OBJECT_IMPL(pw_aff_list)
  194. ISL_DUMP_OBJECT_IMPL(pw_multi_aff)
  195. ISL_DUMP_OBJECT_IMPL(schedule)
  196. ISL_DUMP_OBJECT_IMPL(schedule_constraints)
  197. ISL_DUMP_OBJECT_IMPL(schedule_node)
  198. ISL_DUMP_OBJECT_IMPL(set)
  199. ISL_DUMP_OBJECT_IMPL(set_list)
  200. ISL_DUMP_OBJECT_IMPL(space)
  201. ISL_DUMP_OBJECT_IMPL(union_map)
  202. ISL_DUMP_OBJECT_IMPL(union_pw_aff)
  203. ISL_DUMP_OBJECT_IMPL(union_pw_aff_list)
  204. ISL_DUMP_OBJECT_IMPL(union_pw_multi_aff)
  205. ISL_DUMP_OBJECT_IMPL(union_set)
  206. ISL_DUMP_OBJECT_IMPL(union_set_list)
  207. ISL_DUMP_OBJECT_IMPL(val)
  208. ISL_DUMP_OBJECT_IMPL(val_list)
  209. void polly::dumpIslObj(__isl_keep isl_schedule_node *node, raw_ostream &OS) {
  210. if (!node)
  211. return;
  212. isl_ctx *ctx = isl_schedule_node_get_ctx(node);
  213. isl_printer *p = isl_printer_to_str(ctx);
  214. p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_BLOCK);
  215. p = isl_printer_print_schedule_node(p, node);
  216. char *char_str = isl_printer_get_str(p);
  217. OS << char_str;
  218. free(char_str);
  219. isl_printer_free(p);
  220. }
  221. void polly::dumpIslObj(const isl::schedule_node &Node, raw_ostream &OS) {
  222. dumpIslObj(Node.get(), OS);
  223. }
  224. #endif