Coverage Report

Created: 2017-04-29 12:21

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/polly/lib/Exchange/JSONExporter.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- JSONExporter.cpp  - Export Scops as JSON  -------------------------===//
2
//
3
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
10
// Export the Scops build by ScopInfo pass as a JSON file.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "polly/DependenceInfo.h"
15
#include "polly/LinkAllPasses.h"
16
#include "polly/Options.h"
17
#include "polly/ScopInfo.h"
18
#include "polly/ScopPass.h"
19
#include "polly/Support/ScopLocation.h"
20
#include "llvm/ADT/Statistic.h"
21
#include "llvm/Analysis/RegionInfo.h"
22
#include "llvm/IR/Module.h"
23
#include "llvm/Support/FileSystem.h"
24
#include "llvm/Support/MemoryBuffer.h"
25
#include "llvm/Support/ToolOutputFile.h"
26
#include "llvm/Support/raw_ostream.h"
27
#include "isl/constraint.h"
28
#include "isl/map.h"
29
#include "isl/printer.h"
30
#include "isl/set.h"
31
#include "isl/union_map.h"
32
#include "json/reader.h"
33
#include "json/writer.h"
34
#include <memory>
35
#include <string>
36
#include <system_error>
37
38
using namespace llvm;
39
using namespace polly;
40
41
#define DEBUG_TYPE "polly-import-jscop"
42
43
STATISTIC(NewAccessMapFound, "Number of updated access functions");
44
45
namespace {
46
static cl::opt<std::string>
47
    ImportDir("polly-import-jscop-dir",
48
              cl::desc("The directory to import the .jscop files from."),
49
              cl::Hidden, cl::value_desc("Directory path"), cl::ValueRequired,
50
              cl::init("."), cl::cat(PollyCategory));
51
52
static cl::opt<std::string>
53
    ImportPostfix("polly-import-jscop-postfix",
54
                  cl::desc("Postfix to append to the import .jsop files."),
55
                  cl::Hidden, cl::value_desc("File postfix"), cl::ValueRequired,
56
                  cl::init(""), cl::cat(PollyCategory));
57
58
struct JSONExporter : public ScopPass {
59
  static char ID;
60
0
  explicit JSONExporter() : ScopPass(ID) {}
61
62
  std::string getFileName(Scop &S) const;
63
  Json::Value getJSON(Scop &S) const;
64
65
  /// Export the SCoP @p S to a JSON file.
66
  bool runOnScop(Scop &S) override;
67
68
  /// Print the SCoP @p S as it is exported.
69
  void printScop(raw_ostream &OS, Scop &S) const override;
70
71
  /// Register all analyses and transformation required.
72
  void getAnalysisUsage(AnalysisUsage &AU) const override;
73
};
74
75
struct JSONImporter : public ScopPass {
76
  static char ID;
77
  std::vector<std::string> NewAccessStrings;
78
40
  explicit JSONImporter() : ScopPass(ID) {}
79
80
  /// Import a new context from JScop.
81
  ///
82
  /// @param S The scop to update.
83
  /// @param JScop The JScop file describing the new schedule.
84
  ///
85
  /// @returns True if the import succeeded, otherwise False.
86
  bool importContext(Scop &S, Json::Value &JScop);
87
88
  /// Import a new schedule from JScop.
89
  ///
90
  /// ... and verify that the new schedule does preserve existing data
91
  /// dependences.
92
  ///
93
  /// @param S The scop to update.
94
  /// @param JScop The JScop file describing the new schedule.
95
  /// @param D The data dependences of the @p S.
96
  ///
97
  /// @returns True if the import succeeded, otherwise False.
98
  bool importSchedule(Scop &S, Json::Value &JScop, const Dependences &D);
99
100
  /// Import new arrays from JScop.
101
  ///
102
  /// @param S The scop to update.
103
  /// @param JScop The JScop file describing new arrays.
104
  ///
105
  /// @returns True if the import succeeded, otherwise False.
106
  bool importArrays(Scop &S, Json::Value &JScop);
107
108
  /// Import new memory accesses from JScop.
109
  ///
110
  /// @param S The scop to update.
111
  /// @param JScop The JScop file describing the new schedule.
112
  /// @param DL The datalayout to assume.
113
  ///
114
  /// @returns True if the import succeeded, otherwise False.
115
  bool importAccesses(Scop &S, Json::Value &JScop, const DataLayout &DL);
116
117
  std::string getFileName(Scop &S) const;
118
119
  /// Import new access functions for SCoP @p S from a JSON file.
120
  bool runOnScop(Scop &S) override;
121
122
  /// Print the SCoP @p S and the imported access functions.
123
  void printScop(raw_ostream &OS, Scop &S) const override;
124
125
  /// Register all analyses and transformation required.
126
  void getAnalysisUsage(AnalysisUsage &AU) const override;
127
};
128
} // namespace
129
130
char JSONExporter::ID = 0;
131
0
std::string JSONExporter::getFileName(Scop &S) const {
132
0
  std::string FunctionName = S.getFunction().getName();
133
0
  std::string FileName = FunctionName + "___" + S.getNameStr() + ".jscop";
134
0
  return FileName;
135
0
}
136
137
0
void JSONExporter::printScop(raw_ostream &OS, Scop &S) const { S.print(OS); }
138
139
/// Export all arrays from the Scop.
140
///
141
/// @param S The Scop containing the arrays.
142
///
143
/// @returns Json::Value containing the arrays.
144
0
Json::Value exportArrays(const Scop &S) {
145
0
  Json::Value Arrays;
146
0
  std::string Buffer;
147
0
  llvm::raw_string_ostream RawStringOstream(Buffer);
148
0
149
0
  for (auto &SAI : S.arrays()) {
150
0
    if (!SAI->isArrayKind())
151
0
      continue;
152
0
153
0
    Json::Value Array;
154
0
    Array["name"] = SAI->getName();
155
0
    unsigned i = 0;
156
0
    if (
!SAI->getDimensionSize(i)0
)
{0
157
0
      Array["sizes"].append("*");
158
0
      i++;
159
0
    }
160
0
    for (; 
i < SAI->getNumberOfDimensions()0
;
i++0
)
{0
161
0
      SAI->getDimensionSize(i)->print(RawStringOstream);
162
0
      Array["sizes"].append(RawStringOstream.str());
163
0
      Buffer.clear();
164
0
    }
165
0
    SAI->getElementType()->print(RawStringOstream);
166
0
    Array["type"] = RawStringOstream.str();
167
0
    Buffer.clear();
168
0
    Arrays.append(Array);
169
0
  }
170
0
  return Arrays;
171
0
}
172
173
0
Json::Value JSONExporter::getJSON(Scop &S) const {
174
0
  Json::Value root;
175
0
  unsigned LineBegin, LineEnd;
176
0
  std::string FileName;
177
0
178
0
  getDebugLocation(&S.getRegion(), LineBegin, LineEnd, FileName);
179
0
  std::string Location;
180
0
  if (LineBegin != (unsigned)-1)
181
0
    Location = FileName + ":" + std::to_string(LineBegin) + "-" +
182
0
               std::to_string(LineEnd);
183
0
184
0
  root["name"] = S.getNameStr();
185
0
  root["context"] = S.getContextStr();
186
0
  if (LineBegin != (unsigned)-1)
187
0
    root["location"] = Location;
188
0
189
0
  root["arrays"] = exportArrays(S);
190
0
191
0
  root["statements"];
192
0
193
0
  for (ScopStmt &Stmt : S) {
194
0
    Json::Value statement;
195
0
196
0
    statement["name"] = Stmt.getBaseName();
197
0
    statement["domain"] = Stmt.getDomainStr();
198
0
    statement["schedule"] = Stmt.getScheduleStr();
199
0
    statement["accesses"];
200
0
201
0
    for (MemoryAccess *MA : Stmt) {
202
0
      Json::Value access;
203
0
204
0
      access["kind"] = MA->isRead() ? 
"read"0
:
"write"0
;
205
0
      access["relation"] = MA->getOriginalAccessRelationStr();
206
0
207
0
      statement["accesses"].append(access);
208
0
    }
209
0
210
0
    root["statements"].append(statement);
211
0
  }
212
0
213
0
  return root;
214
0
}
215
216
0
bool JSONExporter::runOnScop(Scop &S) {
217
0
  std::string FileName = ImportDir + "/" + getFileName(S);
218
0
219
0
  Json::Value jscop = getJSON(S);
220
0
  Json::StyledWriter writer;
221
0
  std::string fileContent = writer.write(jscop);
222
0
223
0
  // Write to file.
224
0
  std::error_code EC;
225
0
  tool_output_file F(FileName, EC, llvm::sys::fs::F_Text);
226
0
227
0
  std::string FunctionName = S.getFunction().getName();
228
0
  errs() << "Writing JScop '" << S.getNameStr() << "' in function '"
229
0
         << FunctionName << "' to '" << FileName << "'.\n";
230
0
231
0
  if (
!EC0
)
{0
232
0
    F.os() << fileContent;
233
0
    F.os().close();
234
0
    if (
!F.os().has_error()0
)
{0
235
0
      errs() << "\n";
236
0
      F.keep();
237
0
      return false;
238
0
    }
239
0
  }
240
0
241
0
  errs() << "  error opening file for writing!\n";
242
0
  F.os().clear_error();
243
0
244
0
  return false;
245
0
}
246
247
0
void JSONExporter::getAnalysisUsage(AnalysisUsage &AU) const {
248
0
  AU.setPreservesAll();
249
0
  AU.addRequired<ScopInfoRegionPass>();
250
0
}
251
252
0
Pass *polly::createJSONExporterPass() { return new JSONExporter(); }
253
254
char JSONImporter::ID = 0;
255
41
std::string JSONImporter::getFileName(Scop &S) const {
256
41
  std::string FunctionName = S.getFunction().getName();
257
41
  std::string FileName = FunctionName + "___" + S.getNameStr() + ".jscop";
258
41
259
41
  if (ImportPostfix != "")
260
18
    FileName += "." + ImportPostfix;
261
41
262
41
  return FileName;
263
41
}
264
265
19
void JSONImporter::printScop(raw_ostream &OS, Scop &S) const {
266
19
  S.print(OS);
267
19
  for (std::vector<std::string>::const_iterator I = NewAccessStrings.begin(),
268
19
                                                E = NewAccessStrings.end();
269
36
       
I != E36
;
I++17
)
270
17
    OS << "New access function '" << *I << "' detected in JSCOP file\n";
271
19
}
272
273
typedef Dependences::StatementToIslMapTy StatementToIslMapTy;
274
275
41
bool JSONImporter::importContext(Scop &S, Json::Value &JScop) {
276
41
  isl_set *OldContext = S.getContext();
277
41
  isl_set *NewContext =
278
41
      isl_set_read_from_str(S.getIslCtx(), JScop["context"].asCString());
279
41
280
60
  for (unsigned i = 0; 
i < isl_set_dim(OldContext, isl_dim_param)60
;
i++19
)
{19
281
19
    isl_id *Id = isl_set_get_dim_id(OldContext, isl_dim_param, i);
282
19
    NewContext = isl_set_set_dim_id(NewContext, isl_dim_param, i, Id);
283
19
  }
284
41
285
41
  isl_set_free(OldContext);
286
41
  S.setContext(NewContext);
287
41
  return true;
288
41
}
289
290
bool JSONImporter::importSchedule(Scop &S, Json::Value &JScop,
291
41
                                  const Dependences &D) {
292
41
  StatementToIslMapTy NewSchedule;
293
41
294
41
  int Index = 0;
295
57
  for (ScopStmt &Stmt : S) {
296
57
    Json::Value Schedule = JScop["statements"][Index]["schedule"];
297
57
    assert(!Schedule.asString().empty() &&
298
57
           "Schedules that contain extension nodes require special handling.");
299
57
    isl_map *Map = isl_map_read_from_str(S.getIslCtx(), Schedule.asCString());
300
57
    isl_space *Space = Stmt.getDomainSpace();
301
57
302
57
    // Copy the old tuple id. This is necessary to retain the user pointer,
303
57
    // that stores the reference to the ScopStmt this schedule belongs to.
304
57
    Map = isl_map_set_tuple_id(Map, isl_dim_in,
305
57
                               isl_space_get_tuple_id(Space, isl_dim_set));
306
82
    for (unsigned i = 0; 
i < isl_space_dim(Space, isl_dim_param)82
;
i++25
)
{25
307
25
      isl_id *Id = isl_space_get_dim_id(Space, isl_dim_param, i);
308
25
      Map = isl_map_set_dim_id(Map, isl_dim_param, i, Id);
309
25
    }
310
57
    isl_space_free(Space);
311
57
    NewSchedule[&Stmt] = Map;
312
57
    Index++;
313
57
  }
314
41
315
41
  if (
!D.isValidSchedule(S, &NewSchedule)41
)
{0
316
0
    errs() << "JScop file contains a schedule that changes the "
317
0
           << "dependences. Use -disable-polly-legality to continue anyways\n";
318
0
    for (auto Element : NewSchedule)
319
0
      isl_map_free(Element.second);
320
0
    return false;
321
0
  }
322
41
323
41
  auto ScheduleMap = isl_union_map_empty(S.getParamSpace());
324
57
  for (ScopStmt &Stmt : S) {
325
57
    if (NewSchedule.find(&Stmt) != NewSchedule.end())
326
57
      ScheduleMap = isl_union_map_add_map(ScheduleMap, NewSchedule[&Stmt]);
327
57
    else
328
0
      ScheduleMap = isl_union_map_add_map(ScheduleMap, Stmt.getSchedule());
329
57
  }
330
41
331
41
  S.setSchedule(ScheduleMap);
332
41
333
41
  return true;
334
41
}
335
336
bool JSONImporter::importAccesses(Scop &S, Json::Value &JScop,
337
41
                                  const DataLayout &DL) {
338
41
  int StatementIdx = 0;
339
57
  for (ScopStmt &Stmt : S) {
340
57
    int MemoryAccessIdx = 0;
341
115
    for (MemoryAccess *MA : Stmt) {
342
115
      Json::Value Accesses = JScop["statements"][StatementIdx]["accesses"]
343
115
                                  [MemoryAccessIdx]["relation"];
344
115
      isl_map *NewAccessMap =
345
115
          isl_map_read_from_str(S.getIslCtx(), Accesses.asCString());
346
115
      isl_map *CurrentAccessMap = MA->getAccessRelation();
347
115
348
115
      if (isl_map_dim(NewAccessMap, isl_dim_param) !=
349
0
          isl_map_dim(CurrentAccessMap, isl_dim_param)) {
350
0
        errs() << "JScop file changes the number of parameter dimensions\n";
351
0
        isl_map_free(CurrentAccessMap);
352
0
        isl_map_free(NewAccessMap);
353
0
        return false;
354
0
      }
355
115
356
115
      isl_id *NewOutId;
357
115
358
115
      // If the NewAccessMap has zero dimensions, it is the scalar access; it
359
115
      // must be the same as before.
360
115
      // If it has at least one dimension, it's an array access; search for its
361
115
      // ScopArrayInfo.
362
115
      if (
isl_map_dim(NewAccessMap, isl_dim_out) >= 1115
)
{111
363
111
        NewOutId = isl_map_get_tuple_id(NewAccessMap, isl_dim_out);
364
111
        auto *SAI = S.getArrayInfoByName(isl_id_get_name(NewOutId));
365
111
        isl_id *OutId = isl_map_get_tuple_id(CurrentAccessMap, isl_dim_out);
366
111
        auto *OutSAI = ScopArrayInfo::getFromId(OutId);
367
111
        if (
!SAI || 111
SAI->getElementType() != OutSAI->getElementType()111
)
{1
368
1
          errs() << "JScop file contains access function with undeclared "
369
1
                    "ScopArrayInfo\n";
370
1
          isl_map_free(CurrentAccessMap);
371
1
          isl_map_free(NewAccessMap);
372
1
          isl_id_free(NewOutId);
373
1
          return false;
374
1
        }
375
110
        isl_id_free(NewOutId);
376
110
        NewOutId = SAI->getBasePtrId();
377
4
      } else {
378
4
        NewOutId = isl_map_get_tuple_id(CurrentAccessMap, isl_dim_out);
379
4
      }
380
115
381
114
      NewAccessMap = isl_map_set_tuple_id(NewAccessMap, isl_dim_out, NewOutId);
382
114
383
114
      if (
MA->isArrayKind()114
)
{94
384
94
        // We keep the old alignment, thus we cannot allow accesses to memory
385
94
        // locations that were not accessed before if the alignment of the
386
94
        // access is not the default alignment.
387
94
        bool SpecialAlignment = true;
388
94
        if (LoadInst *
LoadI94
= dyn_cast<LoadInst>(MA->getAccessInstruction()))
{44
389
44
          SpecialAlignment =
390
44
              LoadI->getAlignment() &&
391
42
              DL.getABITypeAlignment(LoadI->getType()) != LoadI->getAlignment();
392
50
        } else 
if (StoreInst *50
StoreI50
=
393
50
                       dyn_cast<StoreInst>(MA->getAccessInstruction())) {
394
50
          SpecialAlignment =
395
50
              StoreI->getAlignment() &&
396
39
              DL.getABITypeAlignment(StoreI->getValueOperand()->getType()) !=
397
39
                  StoreI->getAlignment();
398
50
        }
399
94
400
94
        if (
SpecialAlignment94
)
{1
401
1
          isl_set *NewAccessSet = isl_map_range(isl_map_copy(NewAccessMap));
402
1
          isl_set *CurrentAccessSet =
403
1
              isl_map_range(isl_map_copy(CurrentAccessMap));
404
1
          bool IsSubset = isl_set_is_subset(NewAccessSet, CurrentAccessSet);
405
1
          isl_set_free(NewAccessSet);
406
1
          isl_set_free(CurrentAccessSet);
407
1
408
1
          if (
!IsSubset1
)
{1
409
1
            errs() << "JScop file changes the accessed memory\n";
410
1
            isl_map_free(CurrentAccessMap);
411
1
            isl_map_free(NewAccessMap);
412
1
            return false;
413
1
          }
414
1
        }
415
94
      }
416
114
417
114
      // We need to copy the isl_ids for the parameter dimensions to the new
418
114
      // map. Without doing this the current map would have different
419
114
      // ids then the new one, even though both are named identically.
420
172
      
for (unsigned i = 0; 113
i < isl_map_dim(CurrentAccessMap, isl_dim_param)172
;
421
59
           
i++59
)
{59
422
59
        isl_id *Id = isl_map_get_dim_id(CurrentAccessMap, isl_dim_param, i);
423
59
        NewAccessMap = isl_map_set_dim_id(NewAccessMap, isl_dim_param, i, Id);
424
59
      }
425
113
426
113
      // Copy the old tuple id. This is necessary to retain the user pointer,
427
113
      // that stores the reference to the ScopStmt this access belongs to.
428
113
      isl_id *Id = isl_map_get_tuple_id(CurrentAccessMap, isl_dim_in);
429
113
      NewAccessMap = isl_map_set_tuple_id(NewAccessMap, isl_dim_in, Id);
430
113
431
113
      auto NewAccessDomain = isl_map_domain(isl_map_copy(NewAccessMap));
432
113
      auto CurrentAccessDomain = isl_map_domain(isl_map_copy(CurrentAccessMap));
433
113
434
113
      if (
!isl_set_has_equal_space(NewAccessDomain, CurrentAccessDomain)113
)
{0
435
0
        errs() << "JScop file contains access function with incompatible "
436
0
               << "dimensions\n";
437
0
        isl_map_free(CurrentAccessMap);
438
0
        isl_map_free(NewAccessMap);
439
0
        isl_set_free(NewAccessDomain);
440
0
        isl_set_free(CurrentAccessDomain);
441
0
        return false;
442
0
      }
443
113
444
113
      NewAccessDomain =
445
113
          isl_set_intersect_params(NewAccessDomain, S.getContext());
446
113
      CurrentAccessDomain =
447
113
          isl_set_intersect_params(CurrentAccessDomain, S.getContext());
448
113
449
113
      if (isl_set_is_subset(CurrentAccessDomain, NewAccessDomain) ==
450
0
          isl_bool_false) {
451
0
        errs() << "Mapping not defined for all iteration domain elements\n";
452
0
        isl_set_free(CurrentAccessDomain);
453
0
        isl_set_free(NewAccessDomain);
454
0
        isl_map_free(CurrentAccessMap);
455
0
        isl_map_free(NewAccessMap);
456
0
        return false;
457
0
      }
458
113
459
113
      isl_set_free(CurrentAccessDomain);
460
113
      isl_set_free(NewAccessDomain);
461
113
462
113
      if (
!isl_map_is_equal(NewAccessMap, CurrentAccessMap)113
)
{61
463
61
        // Statistics.
464
61
        ++NewAccessMapFound;
465
61
        NewAccessStrings.push_back(Accesses.asCString());
466
61
        MA->setNewAccessRelation(NewAccessMap);
467
52
      } else {
468
52
        isl_map_free(NewAccessMap);
469
52
      }
470
113
      isl_map_free(CurrentAccessMap);
471
113
      MemoryAccessIdx++;
472
113
    }
473
55
    StatementIdx++;
474
55
  }
475
41
476
39
  return true;
477
41
}
478
479
/// Check whether @p SAI and @p Array represent the same array.
480
12
bool areArraysEqual(ScopArrayInfo *SAI, Json::Value Array) {
481
12
  std::string Buffer;
482
12
  llvm::raw_string_ostream RawStringOstream(Buffer);
483
12
484
12
  if (SAI->getName() != Array["name"].asCString())
485
0
    return false;
486
12
487
12
  
if (12
SAI->getNumberOfDimensions() != Array["sizes"].size()12
)
488
0
    return false;
489
12
490
16
  
for (unsigned i = 1; 12
i < Array["sizes"].size()16
;
i++4
)
{4
491
4
    SAI->getDimensionSize(i)->print(RawStringOstream);
492
4
    if (RawStringOstream.str() != Array["sizes"][i].asCString())
493
0
      return false;
494
4
    Buffer.clear();
495
4
  }
496
12
497
12
  SAI->getElementType()->print(RawStringOstream);
498
12
  if (RawStringOstream.str() != Array["type"].asCString())
499
0
    return false;
500
12
501
12
  return true;
502
12
}
503
504
/// Get the accepted primitive type from its textual representation
505
///        @p TypeTextRepresentation.
506
///
507
/// @param TypeTextRepresentation The textual representation of the type.
508
/// @return The pointer to the primitive type, if this type is accepted
509
///         or nullptr otherwise.
510
Type *parseTextType(const std::string &TypeTextRepresentation,
511
6
                    LLVMContext &LLVMContext) {
512
6
  std::map<std::string, Type *> MapStrToType = {
513
6
      {"void", Type::getVoidTy(LLVMContext)},
514
6
      {"half", Type::getHalfTy(LLVMContext)},
515
6
      {"float", Type::getFloatTy(LLVMContext)},
516
6
      {"double", Type::getDoubleTy(LLVMContext)},
517
6
      {"x86_fp80", Type::getX86_FP80Ty(LLVMContext)},
518
6
      {"fp128", Type::getFP128Ty(LLVMContext)},
519
6
      {"ppc_fp128", Type::getPPC_FP128Ty(LLVMContext)},
520
6
      {"i1", Type::getInt1Ty(LLVMContext)},
521
6
      {"i8", Type::getInt8Ty(LLVMContext)},
522
6
      {"i16", Type::getInt16Ty(LLVMContext)},
523
6
      {"i32", Type::getInt32Ty(LLVMContext)},
524
6
      {"i64", Type::getInt64Ty(LLVMContext)},
525
6
      {"i128", Type::getInt128Ty(LLVMContext)}};
526
6
527
6
  auto It = MapStrToType.find(TypeTextRepresentation);
528
6
  if (It != MapStrToType.end())
529
6
    return It->second;
530
6
531
0
  errs() << "Textual representation can not be parsed: "
532
0
         << TypeTextRepresentation << "\n";
533
0
  return nullptr;
534
6
}
535
536
41
bool JSONImporter::importArrays(Scop &S, Json::Value &JScop) {
537
41
  Json::Value Arrays = JScop["arrays"];
538
41
539
41
  if (Arrays.size() == 0)
540
35
    return true;
541
41
542
6
  unsigned ArrayIdx = 0;
543
20
  for (auto &SAI : S.arrays()) {
544
20
    if (!SAI->isArrayKind())
545
8
      continue;
546
12
    
if (12
ArrayIdx + 1 > Arrays.size()12
)
547
0
      return false;
548
12
    
if (12
!areArraysEqual(SAI, Arrays[ArrayIdx])12
)
549
0
      return false;
550
12
    ArrayIdx++;
551
12
  }
552
6
553
12
  
for (; 6
ArrayIdx < Arrays.size()12
;
ArrayIdx++6
)
{6
554
6
    auto *ElementType = parseTextType(Arrays[ArrayIdx]["type"].asCString(),
555
6
                                      S.getSE()->getContext());
556
6
    if (!ElementType)
557
0
      return false;
558
6
    std::vector<unsigned> DimSizes;
559
14
    for (unsigned i = 0; 
i < Arrays[ArrayIdx]["sizes"].size()14
;
i++8
)
560
8
      DimSizes.push_back(std::stoi(Arrays[ArrayIdx]["sizes"][i].asCString()));
561
6
    S.createScopArrayInfo(ElementType, Arrays[ArrayIdx]["name"].asCString(),
562
6
                          DimSizes);
563
6
  }
564
6
565
6
  return true;
566
6
}
567
568
41
bool JSONImporter::runOnScop(Scop &S) {
569
41
  const Dependences &D =
570
41
      getAnalysis<DependenceInfo>().getDependences(Dependences::AL_Statement);
571
41
  const DataLayout &DL = S.getFunction().getParent()->getDataLayout();
572
41
573
41
  std::string FileName = ImportDir + "/" + getFileName(S);
574
41
575
41
  std::string FunctionName = S.getFunction().getName();
576
41
  errs() << "Reading JScop '" << S.getNameStr() << "' in function '"
577
41
         << FunctionName << "' from '" << FileName << "'.\n";
578
41
  ErrorOr<std::unique_ptr<MemoryBuffer>> result =
579
41
      MemoryBuffer::getFile(FileName);
580
41
  std::error_code ec = result.getError();
581
41
582
41
  if (
ec41
)
{0
583
0
    errs() << "File could not be read: " << ec.message() << "\n";
584
0
    return false;
585
0
  }
586
41
587
41
  Json::Reader reader;
588
41
  Json::Value jscop;
589
41
590
41
  bool parsingSuccessful = reader.parse(result.get()->getBufferStart(), jscop);
591
41
592
41
  if (
!parsingSuccessful41
)
{0
593
0
    errs() << "JSCoP file could not be parsed\n";
594
0
    return false;
595
0
  }
596
41
597
41
  bool Success = importContext(S, jscop);
598
41
599
41
  if (!Success)
600
0
    return false;
601
41
602
41
  Success = importSchedule(S, jscop, D);
603
41
604
41
  if (!Success)
605
0
    return false;
606
41
607
41
  Success = importArrays(S, jscop);
608
41
609
41
  if (!Success)
610
0
    return false;
611
41
612
41
  Success = importAccesses(S, jscop, DL);
613
41
614
41
  if (!Success)
615
2
    return false;
616
41
617
39
  return false;
618
41
}
619
620
40
void JSONImporter::getAnalysisUsage(AnalysisUsage &AU) const {
621
40
  ScopPass::getAnalysisUsage(AU);
622
40
  AU.addRequired<DependenceInfo>();
623
40
}
624
625
0
Pass *polly::createJSONImporterPass() { return new JSONImporter(); }
626
627
39.7k
INITIALIZE_PASS_BEGIN39.7k
(JSONExporter, "polly-export-jscop",39.7k
628
39.7k
                      "Polly - Export Scops as JSON"
629
39.7k
                      " (Writes a .jscop file for each Scop)",
630
39.7k
                      false, false);
631
39.7k
INITIALIZE_PASS_DEPENDENCY(DependenceInfo)
632
39.7k
INITIALIZE_PASS_END(JSONExporter, "polly-export-jscop",
633
                    "Polly - Export Scops as JSON"
634
                    " (Writes a .jscop file for each Scop)",
635
                    false, false)
636
637
39.7k
INITIALIZE_PASS_BEGIN39.7k
(JSONImporter, "polly-import-jscop",39.7k
638
39.7k
                      "Polly - Import Scops from JSON"
639
39.7k
                      " (Reads a .jscop file for each Scop)",
640
39.7k
                      false, false);
641
39.7k
INITIALIZE_PASS_DEPENDENCY(DependenceInfo)
642
39.7k
INITIALIZE_PASS_END(JSONImporter, "polly-import-jscop",
643
                    "Polly - Import Scops from JSON"
644
                    " (Reads a .jscop file for each Scop)",
645
                    false, false)