File indexing completed on 2026-05-10 08:48:20
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef POLLY_SUPPORT_GIC_HELPER_H
0014 #define POLLY_SUPPORT_GIC_HELPER_H
0015
0016 #include "llvm/ADT/APInt.h"
0017 #include "llvm/IR/DiagnosticInfo.h"
0018 #include "llvm/Support/raw_ostream.h"
0019 #include "isl/ctx.h"
0020 #include "isl/isl-noexceptions.h"
0021 #include "isl/options.h"
0022
0023 namespace polly {
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054 __isl_give isl_val *isl_valFromAPInt(isl_ctx *Ctx, const llvm::APInt Int,
0055 bool IsSigned);
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086 inline isl::val valFromAPInt(isl_ctx *Ctx, const llvm::APInt Int,
0087 bool IsSigned) {
0088 return isl::manage(isl_valFromAPInt(Ctx, Int, IsSigned));
0089 }
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118 llvm::APInt APIntFromVal(__isl_take isl_val *Val);
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147 inline llvm::APInt APIntFromVal(isl::val V) {
0148 return APIntFromVal(V.release());
0149 }
0150
0151
0152
0153 #define ISL_CPP_OBJECT_TO_STRING(name) \
0154 inline std::string stringFromIslObj(const name &Obj, \
0155 std::string DefaultValue = "") { \
0156 return stringFromIslObj(Obj.get(), DefaultValue); \
0157 }
0158
0159 #define ISL_OBJECT_TO_STRING(name) \
0160 std::string stringFromIslObj(__isl_keep isl_##name *Obj, \
0161 std::string DefaultValue = ""); \
0162 ISL_CPP_OBJECT_TO_STRING(isl::name)
0163
0164 ISL_OBJECT_TO_STRING(aff)
0165 ISL_OBJECT_TO_STRING(ast_expr)
0166 ISL_OBJECT_TO_STRING(ast_node)
0167 ISL_OBJECT_TO_STRING(basic_map)
0168 ISL_OBJECT_TO_STRING(basic_set)
0169 ISL_OBJECT_TO_STRING(map)
0170 ISL_OBJECT_TO_STRING(set)
0171 ISL_OBJECT_TO_STRING(id)
0172 ISL_OBJECT_TO_STRING(multi_aff)
0173 ISL_OBJECT_TO_STRING(multi_pw_aff)
0174 ISL_OBJECT_TO_STRING(multi_union_pw_aff)
0175 ISL_OBJECT_TO_STRING(point)
0176 ISL_OBJECT_TO_STRING(pw_aff)
0177 ISL_OBJECT_TO_STRING(pw_multi_aff)
0178 ISL_OBJECT_TO_STRING(schedule)
0179 ISL_OBJECT_TO_STRING(schedule_node)
0180 ISL_OBJECT_TO_STRING(space)
0181 ISL_OBJECT_TO_STRING(union_access_info)
0182 ISL_OBJECT_TO_STRING(union_flow)
0183 ISL_OBJECT_TO_STRING(union_set)
0184 ISL_OBJECT_TO_STRING(union_map)
0185 ISL_OBJECT_TO_STRING(union_pw_aff)
0186 ISL_OBJECT_TO_STRING(union_pw_multi_aff)
0187
0188
0189 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
0190
0191
0192
0193 #define ISL_DUMP_OBJECT(name) \
0194 void dumpIslObj(const isl::name &Obj); \
0195 void dumpIslObj(isl_##name *Obj);
0196
0197 ISL_DUMP_OBJECT(aff)
0198 ISL_DUMP_OBJECT(aff_list)
0199 ISL_DUMP_OBJECT(ast_expr)
0200 ISL_DUMP_OBJECT(ast_node)
0201 ISL_DUMP_OBJECT(ast_node_list)
0202 ISL_DUMP_OBJECT(basic_map)
0203 ISL_DUMP_OBJECT(basic_map_list)
0204 ISL_DUMP_OBJECT(basic_set)
0205 ISL_DUMP_OBJECT(basic_set_list)
0206 ISL_DUMP_OBJECT(constraint)
0207 ISL_DUMP_OBJECT(id)
0208 ISL_DUMP_OBJECT(id_list)
0209 ISL_DUMP_OBJECT(id_to_ast_expr)
0210 ISL_DUMP_OBJECT(local_space)
0211 ISL_DUMP_OBJECT(map)
0212 ISL_DUMP_OBJECT(map_list)
0213 ISL_DUMP_OBJECT(multi_aff)
0214 ISL_DUMP_OBJECT(multi_pw_aff)
0215 ISL_DUMP_OBJECT(multi_union_pw_aff)
0216 ISL_DUMP_OBJECT(multi_val)
0217 ISL_DUMP_OBJECT(point)
0218 ISL_DUMP_OBJECT(pw_aff)
0219 ISL_DUMP_OBJECT(pw_aff_list)
0220 ISL_DUMP_OBJECT(pw_multi_aff)
0221 ISL_DUMP_OBJECT(schedule)
0222 ISL_DUMP_OBJECT(schedule_constraints)
0223 ISL_DUMP_OBJECT(schedule_node)
0224 ISL_DUMP_OBJECT(set)
0225 ISL_DUMP_OBJECT(set_list)
0226 ISL_DUMP_OBJECT(space)
0227 ISL_DUMP_OBJECT(union_map)
0228 ISL_DUMP_OBJECT(union_pw_aff)
0229 ISL_DUMP_OBJECT(union_pw_aff_list)
0230 ISL_DUMP_OBJECT(union_pw_multi_aff)
0231 ISL_DUMP_OBJECT(union_set)
0232 ISL_DUMP_OBJECT(union_set_list)
0233 ISL_DUMP_OBJECT(val)
0234 ISL_DUMP_OBJECT(val_list)
0235
0236
0237
0238
0239 void dumpIslObj(const isl::schedule_node &Node, llvm::raw_ostream &OS);
0240 void dumpIslObj(__isl_keep isl_schedule_node *node, llvm::raw_ostream &OS);
0241
0242 #endif
0243
0244 inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
0245 __isl_keep isl_union_map *Map) {
0246 OS << polly::stringFromIslObj(Map, "null");
0247 return OS;
0248 }
0249
0250 inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
0251 __isl_keep isl_map *Map) {
0252 OS << polly::stringFromIslObj(Map, "null");
0253 return OS;
0254 }
0255
0256 inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
0257 __isl_keep isl_set *Set) {
0258 OS << polly::stringFromIslObj(Set, "null");
0259 return OS;
0260 }
0261
0262 inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
0263 __isl_keep isl_pw_aff *Map) {
0264 OS << polly::stringFromIslObj(Map, "null");
0265 return OS;
0266 }
0267
0268 inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
0269 __isl_keep isl_pw_multi_aff *PMA) {
0270 OS << polly::stringFromIslObj(PMA, "null");
0271 return OS;
0272 }
0273
0274 inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
0275 __isl_keep isl_multi_aff *MA) {
0276 OS << polly::stringFromIslObj(MA, "null");
0277 return OS;
0278 }
0279
0280 inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
0281 __isl_keep isl_union_pw_multi_aff *UPMA) {
0282 OS << polly::stringFromIslObj(UPMA, "null");
0283 return OS;
0284 }
0285
0286 inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
0287 __isl_keep isl_schedule *Schedule) {
0288 OS << polly::stringFromIslObj(Schedule, "null");
0289 return OS;
0290 }
0291
0292 inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
0293 __isl_keep isl_space *Space) {
0294 OS << polly::stringFromIslObj(Space, "null");
0295 return OS;
0296 }
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312 std::string getIslCompatibleName(const std::string &Prefix,
0313 const llvm::Value *Val, long Number,
0314 const std::string &Suffix,
0315 bool UseInstructionNames);
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329
0330
0331 std::string getIslCompatibleName(const std::string &Prefix,
0332 const std::string &Middle, long Number,
0333 const std::string &Suffix,
0334 bool UseInstructionNames);
0335
0336 std::string getIslCompatibleName(const std::string &Prefix,
0337 const std::string &Middle,
0338 const std::string &Suffix);
0339
0340 inline llvm::DiagnosticInfoOptimizationBase &
0341 operator<<(llvm::DiagnosticInfoOptimizationBase &OS,
0342 const isl::union_map &Obj) {
0343 OS << stringFromIslObj(Obj);
0344 return OS;
0345 }
0346
0347
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357 class IslQuotaScope final {
0358 isl_ctx *IslCtx;
0359 int OldOnError;
0360
0361 public:
0362 IslQuotaScope() : IslCtx(nullptr) {}
0363 IslQuotaScope(const IslQuotaScope &) = delete;
0364 IslQuotaScope(IslQuotaScope &&Other)
0365 : IslCtx(Other.IslCtx), OldOnError(Other.OldOnError) {
0366 Other.IslCtx = nullptr;
0367 }
0368 const IslQuotaScope &operator=(IslQuotaScope &&Other) {
0369 std::swap(this->IslCtx, Other.IslCtx);
0370 std::swap(this->OldOnError, Other.OldOnError);
0371 return *this;
0372 }
0373
0374
0375
0376
0377 explicit IslQuotaScope(isl_ctx *IslCtx, unsigned long LocalMaxOps)
0378 : IslCtx(IslCtx) {
0379 assert(IslCtx);
0380 assert(isl_ctx_get_max_operations(IslCtx) == 0 && "Incorrect nesting");
0381 if (LocalMaxOps == 0) {
0382 this->IslCtx = nullptr;
0383 return;
0384 }
0385
0386 OldOnError = isl_options_get_on_error(IslCtx);
0387 isl_options_set_on_error(IslCtx, ISL_ON_ERROR_CONTINUE);
0388 isl_ctx_reset_error(IslCtx);
0389 isl_ctx_set_max_operations(IslCtx, LocalMaxOps);
0390 }
0391
0392 ~IslQuotaScope() {
0393 if (!IslCtx)
0394 return;
0395
0396 assert(isl_ctx_get_max_operations(IslCtx) > 0 && "Incorrect nesting");
0397 assert(isl_options_get_on_error(IslCtx) == ISL_ON_ERROR_CONTINUE &&
0398 "Incorrect nesting");
0399 isl_ctx_set_max_operations(IslCtx, 0);
0400 isl_options_set_on_error(IslCtx, OldOnError);
0401 }
0402
0403
0404 bool hasQuotaExceeded() const {
0405 if (!IslCtx)
0406 return false;
0407
0408 return isl_ctx_last_error(IslCtx) == isl_error_quota;
0409 }
0410 };
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424 class IslMaxOperationsGuard final {
0425 private:
0426
0427
0428
0429
0430 isl_ctx *IslCtx;
0431
0432
0433 unsigned long LocalMaxOps;
0434
0435
0436 IslQuotaScope TopLevelScope;
0437
0438 public:
0439
0440
0441
0442
0443
0444
0445
0446
0447
0448
0449 IslMaxOperationsGuard(isl_ctx *IslCtx, unsigned long LocalMaxOps,
0450 bool AutoEnter = true)
0451 : IslCtx(IslCtx), LocalMaxOps(LocalMaxOps) {
0452 assert(IslCtx);
0453 assert(isl_ctx_get_max_operations(IslCtx) == 0 &&
0454 "Nested max operations not supported");
0455
0456
0457
0458
0459
0460 isl_ctx_reset_error(IslCtx);
0461
0462 if (LocalMaxOps == 0) {
0463
0464 this->IslCtx = nullptr;
0465 return;
0466 }
0467
0468 isl_ctx_reset_operations(IslCtx);
0469 TopLevelScope = enter(AutoEnter);
0470 }
0471
0472
0473
0474
0475
0476
0477 IslQuotaScope enter(bool AllowReturnNull = true) {
0478 return AllowReturnNull && IslCtx ? IslQuotaScope(IslCtx, LocalMaxOps)
0479 : IslQuotaScope();
0480 }
0481
0482
0483 bool hasQuotaExceeded() const {
0484 if (!IslCtx)
0485 return false;
0486
0487 return isl_ctx_last_error(IslCtx) == isl_error_quota;
0488 }
0489 };
0490 }
0491
0492 #endif