/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/lib/Exchange/JSONExporter.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===-- JSONExporter.cpp - Export Scops as JSON -------------------------===// |
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 | | // Export the Scops build by ScopInfo pass as a JSON file. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #include "polly/JSONExporter.h" |
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/IR/Module.h" |
22 | | #include "llvm/Support/FileSystem.h" |
23 | | #include "llvm/Support/JSON.h" |
24 | | #include "llvm/Support/MemoryBuffer.h" |
25 | | #include "llvm/Support/ToolOutputFile.h" |
26 | | #include "llvm/Support/raw_ostream.h" |
27 | | #include "isl/map.h" |
28 | | #include "isl/set.h" |
29 | | #include <memory> |
30 | | #include <string> |
31 | | #include <system_error> |
32 | | |
33 | | using namespace llvm; |
34 | | using namespace polly; |
35 | | |
36 | | #define DEBUG_TYPE "polly-import-jscop" |
37 | | |
38 | | STATISTIC(NewAccessMapFound, "Number of updated access functions"); |
39 | | |
40 | | namespace { |
41 | | static cl::opt<std::string> |
42 | | ImportDir("polly-import-jscop-dir", |
43 | | cl::desc("The directory to import the .jscop files from."), |
44 | | cl::Hidden, cl::value_desc("Directory path"), cl::ValueRequired, |
45 | | cl::init("."), cl::cat(PollyCategory)); |
46 | | |
47 | | static cl::opt<std::string> |
48 | | ImportPostfix("polly-import-jscop-postfix", |
49 | | cl::desc("Postfix to append to the import .jsop files."), |
50 | | cl::Hidden, cl::value_desc("File postfix"), cl::ValueRequired, |
51 | | cl::init(""), cl::cat(PollyCategory)); |
52 | | |
53 | | struct JSONExporter : public ScopPass { |
54 | | static char ID; |
55 | 0 | explicit JSONExporter() : ScopPass(ID) {} |
56 | | |
57 | | /// Export the SCoP @p S to a JSON file. |
58 | | bool runOnScop(Scop &S) override; |
59 | | |
60 | | /// Print the SCoP @p S as it is exported. |
61 | | void printScop(raw_ostream &OS, Scop &S) const override; |
62 | | |
63 | | /// Register all analyses and transformation required. |
64 | | void getAnalysisUsage(AnalysisUsage &AU) const override; |
65 | | }; |
66 | | |
67 | | struct JSONImporter : public ScopPass { |
68 | | static char ID; |
69 | | std::vector<std::string> NewAccessStrings; |
70 | 98 | explicit JSONImporter() : ScopPass(ID) {} |
71 | | /// Import new access functions for SCoP @p S from a JSON file. |
72 | | bool runOnScop(Scop &S) override; |
73 | | |
74 | | /// Print the SCoP @p S and the imported access functions. |
75 | | void printScop(raw_ostream &OS, Scop &S) const override; |
76 | | |
77 | | /// Register all analyses and transformation required. |
78 | | void getAnalysisUsage(AnalysisUsage &AU) const override; |
79 | | }; |
80 | | } // namespace |
81 | | |
82 | 99 | static std::string getFileName(Scop &S, StringRef Suffix = "") { |
83 | 99 | std::string FunctionName = S.getFunction().getName(); |
84 | 99 | std::string FileName = FunctionName + "___" + S.getNameStr() + ".jscop"; |
85 | 99 | |
86 | 99 | if (Suffix != "") |
87 | 59 | FileName += "." + Suffix.str(); |
88 | 99 | |
89 | 99 | return FileName; |
90 | 99 | } |
91 | | |
92 | | /// Export all arrays from the Scop. |
93 | | /// |
94 | | /// @param S The Scop containing the arrays. |
95 | | /// |
96 | | /// @returns Json::Value containing the arrays. |
97 | 0 | static json::Array exportArrays(const Scop &S) { |
98 | 0 | json::Array Arrays; |
99 | 0 | std::string Buffer; |
100 | 0 | llvm::raw_string_ostream RawStringOstream(Buffer); |
101 | 0 |
|
102 | 0 | for (auto &SAI : S.arrays()) { |
103 | 0 | if (!SAI->isArrayKind()) |
104 | 0 | continue; |
105 | 0 | |
106 | 0 | json::Object Array; |
107 | 0 | json::Array Sizes; |
108 | 0 | Array["name"] = SAI->getName(); |
109 | 0 | unsigned i = 0; |
110 | 0 | if (!SAI->getDimensionSize(i)) { |
111 | 0 | Sizes.push_back("*"); |
112 | 0 | i++; |
113 | 0 | } |
114 | 0 | for (; i < SAI->getNumberOfDimensions(); i++) { |
115 | 0 | SAI->getDimensionSize(i)->print(RawStringOstream); |
116 | 0 | Sizes.push_back(RawStringOstream.str()); |
117 | 0 | Buffer.clear(); |
118 | 0 | } |
119 | 0 | Array["sizes"] = std::move(Sizes); |
120 | 0 | SAI->getElementType()->print(RawStringOstream); |
121 | 0 | Array["type"] = RawStringOstream.str(); |
122 | 0 | Buffer.clear(); |
123 | 0 | Arrays.push_back(std::move(Array)); |
124 | 0 | } |
125 | 0 | return Arrays; |
126 | 0 | } |
127 | | |
128 | 0 | static json::Value getJSON(Scop &S) { |
129 | 0 | json::Object root; |
130 | 0 | unsigned LineBegin, LineEnd; |
131 | 0 | std::string FileName; |
132 | 0 |
|
133 | 0 | getDebugLocation(&S.getRegion(), LineBegin, LineEnd, FileName); |
134 | 0 | std::string Location; |
135 | 0 | if (LineBegin != (unsigned)-1) |
136 | 0 | Location = FileName + ":" + std::to_string(LineBegin) + "-" + |
137 | 0 | std::to_string(LineEnd); |
138 | 0 |
|
139 | 0 | root["name"] = S.getNameStr(); |
140 | 0 | root["context"] = S.getContextStr(); |
141 | 0 | if (LineBegin != (unsigned)-1) |
142 | 0 | root["location"] = Location; |
143 | 0 |
|
144 | 0 | root["arrays"] = exportArrays(S); |
145 | 0 |
|
146 | 0 | root["statements"]; |
147 | 0 |
|
148 | 0 | json::Array Statements; |
149 | 0 | for (ScopStmt &Stmt : S) { |
150 | 0 | json::Object statement; |
151 | 0 |
|
152 | 0 | statement["name"] = Stmt.getBaseName(); |
153 | 0 | statement["domain"] = Stmt.getDomainStr(); |
154 | 0 | statement["schedule"] = Stmt.getScheduleStr(); |
155 | 0 |
|
156 | 0 | json::Array Accesses; |
157 | 0 | for (MemoryAccess *MA : Stmt) { |
158 | 0 | json::Object access; |
159 | 0 |
|
160 | 0 | access["kind"] = MA->isRead() ? "read" : "write"; |
161 | 0 | access["relation"] = MA->getAccessRelationStr(); |
162 | 0 |
|
163 | 0 | Accesses.push_back(std::move(access)); |
164 | 0 | } |
165 | 0 | statement["accesses"] = std::move(Accesses); |
166 | 0 |
|
167 | 0 | Statements.push_back(std::move(statement)); |
168 | 0 | } |
169 | 0 |
|
170 | 0 | root["statements"] = std::move(Statements); |
171 | 0 | return json::Value(std::move(root)); |
172 | 0 | } |
173 | | |
174 | 0 | static void exportScop(Scop &S) { |
175 | 0 | std::string FileName = ImportDir + "/" + getFileName(S); |
176 | 0 |
|
177 | 0 | json::Value jscop = getJSON(S); |
178 | 0 |
|
179 | 0 | // Write to file. |
180 | 0 | std::error_code EC; |
181 | 0 | ToolOutputFile F(FileName, EC, llvm::sys::fs::F_Text); |
182 | 0 |
|
183 | 0 | std::string FunctionName = S.getFunction().getName(); |
184 | 0 | errs() << "Writing JScop '" << S.getNameStr() << "' in function '" |
185 | 0 | << FunctionName << "' to '" << FileName << "'.\n"; |
186 | 0 |
|
187 | 0 | if (!EC) { |
188 | 0 | F.os() << formatv("{0:3}", jscop); |
189 | 0 | F.os().close(); |
190 | 0 | if (!F.os().has_error()) { |
191 | 0 | errs() << "\n"; |
192 | 0 | F.keep(); |
193 | 0 | return; |
194 | 0 | } |
195 | 0 | } |
196 | 0 | |
197 | 0 | errs() << " error opening file for writing!\n"; |
198 | 0 | F.os().clear_error(); |
199 | 0 | } |
200 | | |
201 | | typedef Dependences::StatementToIslMapTy StatementToIslMapTy; |
202 | | |
203 | | /// Import a new context from JScop. |
204 | | /// |
205 | | /// @param S The scop to update. |
206 | | /// @param JScop The JScop file describing the new schedule. |
207 | | /// |
208 | | /// @returns True if the import succeeded, otherwise False. |
209 | 99 | static bool importContext(Scop &S, const json::Object &JScop) { |
210 | 99 | isl::set OldContext = S.getContext(); |
211 | 99 | |
212 | 99 | // Check if key 'context' is present. |
213 | 99 | if (!JScop.get("context")) { |
214 | 1 | errs() << "JScop file has no key named 'context'.\n"; |
215 | 1 | return false; |
216 | 1 | } |
217 | 98 | |
218 | 98 | isl::set NewContext = |
219 | 98 | isl::set{S.getIslCtx().get(), JScop.getString("context").getValue()}; |
220 | 98 | |
221 | 98 | // Check whether the context was parsed successfully. |
222 | 98 | if (!NewContext) { |
223 | 1 | errs() << "The context was not parsed successfully by ISL.\n"; |
224 | 1 | return false; |
225 | 1 | } |
226 | 97 | |
227 | 97 | // Check if the isl_set is a parameter set. |
228 | 97 | if (!NewContext.is_params()) { |
229 | 1 | errs() << "The isl_set is not a parameter set.\n"; |
230 | 1 | return false; |
231 | 1 | } |
232 | 96 | |
233 | 96 | unsigned OldContextDim = OldContext.dim(isl::dim::param); |
234 | 96 | unsigned NewContextDim = NewContext.dim(isl::dim::param); |
235 | 96 | |
236 | 96 | // Check if the imported context has the right number of parameters. |
237 | 96 | if (OldContextDim != NewContextDim) { |
238 | 1 | errs() << "Imported context has the wrong number of parameters : " |
239 | 1 | << "Found " << NewContextDim << " Expected " << OldContextDim |
240 | 1 | << "\n"; |
241 | 1 | return false; |
242 | 1 | } |
243 | 95 | |
244 | 156 | for (unsigned i = 0; 95 i < OldContextDim; i++61 ) { |
245 | 61 | isl::id Id = OldContext.get_dim_id(isl::dim::param, i); |
246 | 61 | NewContext = NewContext.set_dim_id(isl::dim::param, i, Id); |
247 | 61 | } |
248 | 95 | |
249 | 95 | S.setContext(NewContext); |
250 | 95 | return true; |
251 | 95 | } |
252 | | |
253 | | /// Import a new schedule from JScop. |
254 | | /// |
255 | | /// ... and verify that the new schedule does preserve existing data |
256 | | /// dependences. |
257 | | /// |
258 | | /// @param S The scop to update. |
259 | | /// @param JScop The JScop file describing the new schedule. |
260 | | /// @param D The data dependences of the @p S. |
261 | | /// |
262 | | /// @returns True if the import succeeded, otherwise False. |
263 | | static bool importSchedule(Scop &S, const json::Object &JScop, |
264 | 95 | const Dependences &D) { |
265 | 95 | StatementToIslMapTy NewSchedule; |
266 | 95 | |
267 | 95 | // Check if key 'statements' is present. |
268 | 95 | if (!JScop.get("statements")) { |
269 | 2 | errs() << "JScop file has no key name 'statements'.\n"; |
270 | 2 | return false; |
271 | 2 | } |
272 | 93 | |
273 | 93 | const json::Array &statements = *JScop.getArray("statements"); |
274 | 93 | |
275 | 93 | // Check whether the number of indices equals the number of statements |
276 | 93 | if (statements.size() != S.getSize()) { |
277 | 2 | errs() << "The number of indices and the number of statements differ.\n"; |
278 | 2 | return false; |
279 | 2 | } |
280 | 91 | |
281 | 91 | int Index = 0; |
282 | 141 | for (ScopStmt &Stmt : S) { |
283 | 141 | // Check if key 'schedule' is present. |
284 | 141 | if (!statements[Index].getAsObject()->get("schedule")) { |
285 | 1 | errs() << "Statement " << Index << " has no 'schedule' key.\n"; |
286 | 1 | return false; |
287 | 1 | } |
288 | 140 | Optional<StringRef> Schedule = |
289 | 140 | statements[Index].getAsObject()->getString("schedule"); |
290 | 140 | assert(Schedule.hasValue() && |
291 | 140 | "Schedules that contain extension nodes require special handling."); |
292 | 140 | isl_map *Map = isl_map_read_from_str(S.getIslCtx().get(), |
293 | 140 | Schedule.getValue().str().c_str()); |
294 | 140 | |
295 | 140 | // Check whether the schedule was parsed successfully |
296 | 140 | if (!Map) { |
297 | 1 | errs() << "The schedule was not parsed successfully (index = " << Index |
298 | 1 | << ").\n"; |
299 | 1 | return false; |
300 | 1 | } |
301 | 139 | |
302 | 139 | isl_space *Space = Stmt.getDomainSpace().release(); |
303 | 139 | |
304 | 139 | // Copy the old tuple id. This is necessary to retain the user pointer, |
305 | 139 | // that stores the reference to the ScopStmt this schedule belongs to. |
306 | 139 | Map = isl_map_set_tuple_id(Map, isl_dim_in, |
307 | 139 | isl_space_get_tuple_id(Space, isl_dim_set)); |
308 | 229 | for (unsigned i = 0; i < isl_space_dim(Space, isl_dim_param); i++90 ) { |
309 | 90 | isl_id *Id = isl_space_get_dim_id(Space, isl_dim_param, i); |
310 | 90 | Map = isl_map_set_dim_id(Map, isl_dim_param, i, Id); |
311 | 90 | } |
312 | 139 | isl_space_free(Space); |
313 | 139 | NewSchedule[&Stmt] = isl::manage(Map); |
314 | 139 | Index++; |
315 | 139 | } |
316 | 91 | |
317 | 91 | // Check whether the new schedule is valid or not. |
318 | 91 | if (89 !D.isValidSchedule(S, NewSchedule)89 ) { |
319 | 0 | errs() << "JScop file contains a schedule that changes the " |
320 | 0 | << "dependences. Use -disable-polly-legality to continue anyways\n"; |
321 | 0 | return false; |
322 | 0 | } |
323 | 89 | |
324 | 89 | auto ScheduleMap = isl::union_map::empty(S.getParamSpace()); |
325 | 138 | for (ScopStmt &Stmt : S) { |
326 | 138 | if (NewSchedule.find(&Stmt) != NewSchedule.end()) |
327 | 138 | ScheduleMap = ScheduleMap.add_map(NewSchedule[&Stmt]); |
328 | 0 | else |
329 | 0 | ScheduleMap = ScheduleMap.add_map(Stmt.getSchedule()); |
330 | 138 | } |
331 | 89 | |
332 | 89 | S.setSchedule(ScheduleMap); |
333 | 89 | |
334 | 89 | return true; |
335 | 89 | } |
336 | | |
337 | | /// Import new memory accesses from JScop. |
338 | | /// |
339 | | /// @param S The scop to update. |
340 | | /// @param JScop The JScop file describing the new schedule. |
341 | | /// @param DL The data layout to assume. |
342 | | /// @param NewAccessStrings optionally record the imported access strings |
343 | | /// |
344 | | /// @returns True if the import succeeded, otherwise False. |
345 | | static bool |
346 | | importAccesses(Scop &S, const json::Object &JScop, const DataLayout &DL, |
347 | 84 | std::vector<std::string> *NewAccessStrings = nullptr) { |
348 | 84 | int StatementIdx = 0; |
349 | 84 | |
350 | 84 | // Check if key 'statements' is present. |
351 | 84 | if (!JScop.get("statements")) { |
352 | 0 | errs() << "JScop file has no key name 'statements'.\n"; |
353 | 0 | return false; |
354 | 0 | } |
355 | 84 | const json::Array &statements = *JScop.getArray("statements"); |
356 | 84 | |
357 | 84 | // Check whether the number of indices equals the number of statements |
358 | 84 | if (statements.size() != S.getSize()) { |
359 | 0 | errs() << "The number of indices and the number of statements differ.\n"; |
360 | 0 | return false; |
361 | 0 | } |
362 | 84 | |
363 | 129 | for (ScopStmt &Stmt : S)84 { |
364 | 129 | int MemoryAccessIdx = 0; |
365 | 129 | const json::Object *Statement = statements[StatementIdx].getAsObject(); |
366 | 129 | assert(Statement); |
367 | 129 | |
368 | 129 | // Check if key 'accesses' is present. |
369 | 129 | if (!Statement->get("accesses")) { |
370 | 1 | errs() |
371 | 1 | << "Statement from JScop file has no key name 'accesses' for index " |
372 | 1 | << StatementIdx << ".\n"; |
373 | 1 | return false; |
374 | 1 | } |
375 | 128 | const json::Array &JsonAccesses = *Statement->getArray("accesses"); |
376 | 128 | |
377 | 128 | // Check whether the number of indices equals the number of memory |
378 | 128 | // accesses |
379 | 128 | if (Stmt.size() != JsonAccesses.size()) { |
380 | 1 | errs() << "The number of memory accesses in the JSop file and the number " |
381 | 1 | "of memory accesses differ for index " |
382 | 1 | << StatementIdx << ".\n"; |
383 | 1 | return false; |
384 | 1 | } |
385 | 127 | |
386 | 277 | for (MemoryAccess *MA : Stmt)127 { |
387 | 277 | // Check if key 'relation' is present. |
388 | 277 | const json::Object *JsonMemoryAccess = |
389 | 277 | JsonAccesses[MemoryAccessIdx].getAsObject(); |
390 | 277 | assert(JsonMemoryAccess); |
391 | 277 | if (!JsonMemoryAccess->get("relation")) { |
392 | 1 | errs() << "Memory access number " << MemoryAccessIdx |
393 | 1 | << " has no key name 'relation' for statement number " |
394 | 1 | << StatementIdx << ".\n"; |
395 | 1 | return false; |
396 | 1 | } |
397 | 276 | StringRef Accesses = JsonMemoryAccess->getString("relation").getValue(); |
398 | 276 | isl_map *NewAccessMap = |
399 | 276 | isl_map_read_from_str(S.getIslCtx().get(), Accesses.str().c_str()); |
400 | 276 | |
401 | 276 | // Check whether the access was parsed successfully |
402 | 276 | if (!NewAccessMap) { |
403 | 1 | errs() << "The access was not parsed successfully by ISL.\n"; |
404 | 1 | return false; |
405 | 1 | } |
406 | 275 | isl_map *CurrentAccessMap = MA->getAccessRelation().release(); |
407 | 275 | |
408 | 275 | // Check if the number of parameter change |
409 | 275 | if (isl_map_dim(NewAccessMap, isl_dim_param) != |
410 | 275 | isl_map_dim(CurrentAccessMap, isl_dim_param)) { |
411 | 1 | errs() << "JScop file changes the number of parameter dimensions.\n"; |
412 | 1 | isl_map_free(CurrentAccessMap); |
413 | 1 | isl_map_free(NewAccessMap); |
414 | 1 | return false; |
415 | 1 | } |
416 | 274 | |
417 | 274 | isl_id *NewOutId; |
418 | 274 | |
419 | 274 | // If the NewAccessMap has zero dimensions, it is the scalar access; it |
420 | 274 | // must be the same as before. |
421 | 274 | // If it has at least one dimension, it's an array access; search for |
422 | 274 | // its ScopArrayInfo. |
423 | 274 | if (isl_map_dim(NewAccessMap, isl_dim_out) >= 1) { |
424 | 259 | NewOutId = isl_map_get_tuple_id(NewAccessMap, isl_dim_out); |
425 | 259 | auto *SAI = S.getArrayInfoByName(isl_id_get_name(NewOutId)); |
426 | 259 | isl_id *OutId = isl_map_get_tuple_id(CurrentAccessMap, isl_dim_out); |
427 | 259 | auto *OutSAI = ScopArrayInfo::getFromId(isl::manage(OutId)); |
428 | 259 | if (!SAI || SAI->getElementType() != OutSAI->getElementType()258 ) { |
429 | 1 | errs() << "JScop file contains access function with undeclared " |
430 | 1 | "ScopArrayInfo\n"; |
431 | 1 | isl_map_free(CurrentAccessMap); |
432 | 1 | isl_map_free(NewAccessMap); |
433 | 1 | isl_id_free(NewOutId); |
434 | 1 | return false; |
435 | 1 | } |
436 | 258 | isl_id_free(NewOutId); |
437 | 258 | NewOutId = SAI->getBasePtrId().release(); |
438 | 258 | } else { |
439 | 15 | NewOutId = isl_map_get_tuple_id(CurrentAccessMap, isl_dim_out); |
440 | 15 | } |
441 | 274 | |
442 | 274 | NewAccessMap = isl_map_set_tuple_id(NewAccessMap, isl_dim_out, NewOutId); |
443 | 273 | |
444 | 273 | if (MA->isArrayKind()) { |
445 | 190 | // We keep the old alignment, thus we cannot allow accesses to memory |
446 | 190 | // locations that were not accessed before if the alignment of the |
447 | 190 | // access is not the default alignment. |
448 | 190 | bool SpecialAlignment = true; |
449 | 190 | if (LoadInst *LoadI = dyn_cast<LoadInst>(MA->getAccessInstruction())) { |
450 | 75 | SpecialAlignment = |
451 | 75 | LoadI->getAlignment() && |
452 | 75 | DL.getABITypeAlignment(LoadI->getType()) != LoadI->getAlignment()65 ; |
453 | 115 | } else if (StoreInst *StoreI = |
454 | 115 | dyn_cast<StoreInst>(MA->getAccessInstruction())) { |
455 | 115 | SpecialAlignment = |
456 | 115 | StoreI->getAlignment() && |
457 | 115 | DL.getABITypeAlignment(StoreI->getValueOperand()->getType()) != |
458 | 56 | StoreI->getAlignment(); |
459 | 115 | } |
460 | 190 | |
461 | 190 | if (SpecialAlignment) { |
462 | 1 | isl_set *NewAccessSet = isl_map_range(isl_map_copy(NewAccessMap)); |
463 | 1 | isl_set *CurrentAccessSet = |
464 | 1 | isl_map_range(isl_map_copy(CurrentAccessMap)); |
465 | 1 | bool IsSubset = isl_set_is_subset(NewAccessSet, CurrentAccessSet); |
466 | 1 | isl_set_free(NewAccessSet); |
467 | 1 | isl_set_free(CurrentAccessSet); |
468 | 1 | |
469 | 1 | // Check if the JScop file changes the accessed memory. |
470 | 1 | if (!IsSubset) { |
471 | 1 | errs() << "JScop file changes the accessed memory\n"; |
472 | 1 | isl_map_free(CurrentAccessMap); |
473 | 1 | isl_map_free(NewAccessMap); |
474 | 1 | return false; |
475 | 1 | } |
476 | 272 | } |
477 | 190 | } |
478 | 272 | |
479 | 272 | // We need to copy the isl_ids for the parameter dimensions to the new |
480 | 272 | // map. Without doing this the current map would have different |
481 | 272 | // ids then the new one, even though both are named identically. |
482 | 448 | for (unsigned i = 0; 272 i < isl_map_dim(CurrentAccessMap, isl_dim_param); |
483 | 272 | i++176 ) { |
484 | 176 | isl_id *Id = isl_map_get_dim_id(CurrentAccessMap, isl_dim_param, i); |
485 | 176 | NewAccessMap = isl_map_set_dim_id(NewAccessMap, isl_dim_param, i, Id); |
486 | 176 | } |
487 | 272 | |
488 | 272 | // Copy the old tuple id. This is necessary to retain the user pointer, |
489 | 272 | // that stores the reference to the ScopStmt this access belongs to. |
490 | 272 | isl_id *Id = isl_map_get_tuple_id(CurrentAccessMap, isl_dim_in); |
491 | 272 | NewAccessMap = isl_map_set_tuple_id(NewAccessMap, isl_dim_in, Id); |
492 | 272 | |
493 | 272 | auto NewAccessDomain = isl_map_domain(isl_map_copy(NewAccessMap)); |
494 | 272 | auto CurrentAccessDomain = isl_map_domain(isl_map_copy(CurrentAccessMap)); |
495 | 272 | |
496 | 272 | if (!isl_set_has_equal_space(NewAccessDomain, CurrentAccessDomain)) { |
497 | 0 | errs() << "JScop file contains access function with incompatible " |
498 | 0 | << "dimensions\n"; |
499 | 0 | isl_map_free(CurrentAccessMap); |
500 | 0 | isl_map_free(NewAccessMap); |
501 | 0 | isl_set_free(NewAccessDomain); |
502 | 0 | isl_set_free(CurrentAccessDomain); |
503 | 0 | return false; |
504 | 0 | } |
505 | 272 | |
506 | 272 | NewAccessDomain = |
507 | 272 | isl_set_intersect_params(NewAccessDomain, S.getContext().release()); |
508 | 272 | CurrentAccessDomain = isl_set_intersect_params(CurrentAccessDomain, |
509 | 272 | S.getContext().release()); |
510 | 272 | CurrentAccessDomain = |
511 | 272 | isl_set_intersect(CurrentAccessDomain, Stmt.getDomain().release()); |
512 | 272 | |
513 | 272 | if (MA->isRead() && |
514 | 272 | isl_set_is_subset(CurrentAccessDomain, NewAccessDomain) == |
515 | 118 | isl_bool_false) { |
516 | 0 | errs() << "Mapping not defined for all iteration domain elements\n"; |
517 | 0 | isl_set_free(CurrentAccessDomain); |
518 | 0 | isl_set_free(NewAccessDomain); |
519 | 0 | isl_map_free(CurrentAccessMap); |
520 | 0 | isl_map_free(NewAccessMap); |
521 | 0 | return false; |
522 | 0 | } |
523 | 272 | |
524 | 272 | isl_set_free(CurrentAccessDomain); |
525 | 272 | isl_set_free(NewAccessDomain); |
526 | 272 | |
527 | 272 | if (!isl_map_is_equal(NewAccessMap, CurrentAccessMap)) { |
528 | 150 | // Statistics. |
529 | 150 | ++NewAccessMapFound; |
530 | 150 | if (NewAccessStrings) |
531 | 150 | NewAccessStrings->push_back(Accesses); |
532 | 150 | MA->setNewAccessRelation(isl::manage(NewAccessMap)); |
533 | 150 | } else { |
534 | 122 | isl_map_free(NewAccessMap); |
535 | 122 | } |
536 | 272 | isl_map_free(CurrentAccessMap); |
537 | 272 | MemoryAccessIdx++; |
538 | 272 | } |
539 | 127 | StatementIdx++; |
540 | 122 | } |
541 | 84 | |
542 | 84 | return true77 ; |
543 | 84 | } |
544 | | |
545 | | /// Check whether @p SAI and @p Array represent the same array. |
546 | 70 | static bool areArraysEqual(ScopArrayInfo *SAI, const json::Object &Array) { |
547 | 70 | std::string Buffer; |
548 | 70 | llvm::raw_string_ostream RawStringOstream(Buffer); |
549 | 70 | |
550 | 70 | // Check if key 'type' is present. |
551 | 70 | if (!Array.get("type")) { |
552 | 1 | errs() << "Array has no key 'type'.\n"; |
553 | 1 | return false; |
554 | 1 | } |
555 | 69 | |
556 | 69 | // Check if key 'sizes' is present. |
557 | 69 | if (!Array.get("sizes")) { |
558 | 1 | errs() << "Array has no key 'sizes'.\n"; |
559 | 1 | return false; |
560 | 1 | } |
561 | 68 | |
562 | 68 | // Check if key 'name' is present. |
563 | 68 | if (!Array.get("name")) { |
564 | 1 | errs() << "Array has no key 'name'.\n"; |
565 | 1 | return false; |
566 | 1 | } |
567 | 67 | |
568 | 67 | if (SAI->getName() != Array.getString("name").getValue()) |
569 | 0 | return false; |
570 | 67 | |
571 | 67 | if (SAI->getNumberOfDimensions() != Array.getArray("sizes")->size()) |
572 | 0 | return false; |
573 | 67 | |
574 | 85 | for (unsigned i = 1; 67 i < Array.getArray("sizes")->size(); i++18 ) { |
575 | 18 | SAI->getDimensionSize(i)->print(RawStringOstream); |
576 | 18 | const json::Array &SizesArray = *Array.getArray("sizes"); |
577 | 18 | if (RawStringOstream.str() != SizesArray[i].getAsString().getValue()) |
578 | 0 | return false; |
579 | 18 | Buffer.clear(); |
580 | 18 | } |
581 | 67 | |
582 | 67 | // Check if key 'type' differs from the current one or is not valid. |
583 | 67 | SAI->getElementType()->print(RawStringOstream); |
584 | 67 | if (RawStringOstream.str() != Array.getString("type").getValue()) { |
585 | 1 | errs() << "Array has not a valid type.\n"; |
586 | 1 | return false; |
587 | 1 | } |
588 | 66 | |
589 | 66 | return true; |
590 | 66 | } |
591 | | |
592 | | /// Get the accepted primitive type from its textual representation |
593 | | /// @p TypeTextRepresentation. |
594 | | /// |
595 | | /// @param TypeTextRepresentation The textual representation of the type. |
596 | | /// @return The pointer to the primitive type, if this type is accepted |
597 | | /// or nullptr otherwise. |
598 | | static Type *parseTextType(const std::string &TypeTextRepresentation, |
599 | 13 | LLVMContext &LLVMContext) { |
600 | 13 | std::map<std::string, Type *> MapStrToType = { |
601 | 13 | {"void", Type::getVoidTy(LLVMContext)}, |
602 | 13 | {"half", Type::getHalfTy(LLVMContext)}, |
603 | 13 | {"float", Type::getFloatTy(LLVMContext)}, |
604 | 13 | {"double", Type::getDoubleTy(LLVMContext)}, |
605 | 13 | {"x86_fp80", Type::getX86_FP80Ty(LLVMContext)}, |
606 | 13 | {"fp128", Type::getFP128Ty(LLVMContext)}, |
607 | 13 | {"ppc_fp128", Type::getPPC_FP128Ty(LLVMContext)}, |
608 | 13 | {"i1", Type::getInt1Ty(LLVMContext)}, |
609 | 13 | {"i8", Type::getInt8Ty(LLVMContext)}, |
610 | 13 | {"i16", Type::getInt16Ty(LLVMContext)}, |
611 | 13 | {"i32", Type::getInt32Ty(LLVMContext)}, |
612 | 13 | {"i64", Type::getInt64Ty(LLVMContext)}, |
613 | 13 | {"i128", Type::getInt128Ty(LLVMContext)}}; |
614 | 13 | |
615 | 13 | auto It = MapStrToType.find(TypeTextRepresentation); |
616 | 13 | if (It != MapStrToType.end()) |
617 | 13 | return It->second; |
618 | 0 | |
619 | 0 | errs() << "Textual representation can not be parsed: " |
620 | 0 | << TypeTextRepresentation << "\n"; |
621 | 0 | return nullptr; |
622 | 0 | } |
623 | | |
624 | | /// Import new arrays from JScop. |
625 | | /// |
626 | | /// @param S The scop to update. |
627 | | /// @param JScop The JScop file describing new arrays. |
628 | | /// |
629 | | /// @returns True if the import succeeded, otherwise False. |
630 | 89 | static bool importArrays(Scop &S, const json::Object &JScop) { |
631 | 89 | if (!JScop.get("arrays")) |
632 | 42 | return true; |
633 | 47 | const json::Array &Arrays = *JScop.getArray("arrays"); |
634 | 47 | if (Arrays.size() == 0) |
635 | 0 | return true; |
636 | 47 | |
637 | 47 | unsigned ArrayIdx = 0; |
638 | 111 | for (auto &SAI : S.arrays()) { |
639 | 111 | if (!SAI->isArrayKind()) |
640 | 41 | continue; |
641 | 70 | if (ArrayIdx + 1 > Arrays.size()) { |
642 | 0 | errs() << "Not enough array entries in JScop file.\n"; |
643 | 0 | return false; |
644 | 0 | } |
645 | 70 | if (!areArraysEqual(SAI, *Arrays[ArrayIdx].getAsObject())) { |
646 | 4 | errs() << "No match for array '" << SAI->getName() << "' in JScop.\n"; |
647 | 4 | return false; |
648 | 4 | } |
649 | 66 | ArrayIdx++; |
650 | 66 | } |
651 | 47 | |
652 | 55 | for (; 43 ArrayIdx < Arrays.size(); ArrayIdx++12 ) { |
653 | 13 | const json::Object &Array = *Arrays[ArrayIdx].getAsObject(); |
654 | 13 | auto *ElementType = parseTextType( |
655 | 13 | Array.get("type")->getAsString().getValue(), S.getSE()->getContext()); |
656 | 13 | if (!ElementType) { |
657 | 0 | errs() << "Error while parsing element type for new array.\n"; |
658 | 0 | return false; |
659 | 0 | } |
660 | 13 | const json::Array &SizesArray = *Array.getArray("sizes"); |
661 | 13 | std::vector<unsigned> DimSizes; |
662 | 29 | for (unsigned i = 0; i < SizesArray.size(); i++16 ) { |
663 | 17 | auto Size = std::stoi(SizesArray[i].getAsString().getValue()); |
664 | 17 | |
665 | 17 | // Check if the size if positive. |
666 | 17 | if (Size <= 0) { |
667 | 1 | errs() << "The size at index " << i << " is =< 0.\n"; |
668 | 1 | return false; |
669 | 1 | } |
670 | 16 | |
671 | 16 | DimSizes.push_back(Size); |
672 | 16 | } |
673 | 13 | |
674 | 13 | auto NewSAI = S.createScopArrayInfo( |
675 | 12 | ElementType, Array.getString("name").getValue(), DimSizes); |
676 | 12 | |
677 | 12 | if (Array.get("allocation")) { |
678 | 6 | NewSAI->setIsOnHeap(Array.getString("allocation").getValue() == "heap"); |
679 | 6 | } |
680 | 12 | } |
681 | 43 | |
682 | 43 | return true42 ; |
683 | 43 | } |
684 | | |
685 | | /// Import a Scop from a JSCOP file |
686 | | /// @param S The scop to be modified |
687 | | /// @param D Dependence Info |
688 | | /// @param DL The DataLayout of the function |
689 | | /// @param NewAccessStrings Optionally record the imported access strings |
690 | | /// |
691 | | /// @returns true on success, false otherwise. Beware that if this returns |
692 | | /// false, the Scop may still have been modified. In this case the Scop contains |
693 | | /// invalid information. |
694 | | static bool importScop(Scop &S, const Dependences &D, const DataLayout &DL, |
695 | 99 | std::vector<std::string> *NewAccessStrings = nullptr) { |
696 | 99 | std::string FileName = ImportDir + "/" + getFileName(S, ImportPostfix); |
697 | 99 | |
698 | 99 | std::string FunctionName = S.getFunction().getName(); |
699 | 99 | errs() << "Reading JScop '" << S.getNameStr() << "' in function '" |
700 | 99 | << FunctionName << "' from '" << FileName << "'.\n"; |
701 | 99 | ErrorOr<std::unique_ptr<MemoryBuffer>> result = |
702 | 99 | MemoryBuffer::getFile(FileName); |
703 | 99 | std::error_code ec = result.getError(); |
704 | 99 | |
705 | 99 | if (ec) { |
706 | 0 | errs() << "File could not be read: " << ec.message() << "\n"; |
707 | 0 | return false; |
708 | 0 | } |
709 | 99 | |
710 | 99 | Expected<json::Value> ParseResult = |
711 | 99 | json::parse(result.get().get()->getBuffer()); |
712 | 99 | |
713 | 99 | if (Error E = ParseResult.takeError()) { |
714 | 0 | errs() << "JSCoP file could not be parsed\n"; |
715 | 0 | errs() << E << "\n"; |
716 | 0 | consumeError(std::move(E)); |
717 | 0 | return false; |
718 | 0 | } |
719 | 99 | json::Object &jscop = *ParseResult.get().getAsObject(); |
720 | 99 | |
721 | 99 | bool Success = importContext(S, jscop); |
722 | 99 | |
723 | 99 | if (!Success) |
724 | 4 | return false; |
725 | 95 | |
726 | 95 | Success = importSchedule(S, jscop, D); |
727 | 95 | |
728 | 95 | if (!Success) |
729 | 6 | return false; |
730 | 89 | |
731 | 89 | Success = importArrays(S, jscop); |
732 | 89 | |
733 | 89 | if (!Success) |
734 | 5 | return false; |
735 | 84 | |
736 | 84 | Success = importAccesses(S, jscop, DL, NewAccessStrings); |
737 | 84 | |
738 | 84 | if (!Success) |
739 | 7 | return false; |
740 | 77 | return true; |
741 | 77 | } |
742 | | |
743 | | char JSONExporter::ID = 0; |
744 | 0 | void JSONExporter::printScop(raw_ostream &OS, Scop &S) const { OS << S; } |
745 | | |
746 | 0 | bool JSONExporter::runOnScop(Scop &S) { |
747 | 0 | exportScop(S); |
748 | 0 | return false; |
749 | 0 | } |
750 | | |
751 | 0 | void JSONExporter::getAnalysisUsage(AnalysisUsage &AU) const { |
752 | 0 | AU.setPreservesAll(); |
753 | 0 | AU.addRequired<ScopInfoRegionPass>(); |
754 | 0 | } |
755 | | |
756 | 0 | Pass *polly::createJSONExporterPass() { return new JSONExporter(); } |
757 | | |
758 | | PreservedAnalyses JSONExportPass::run(Scop &S, ScopAnalysisManager &SAM, |
759 | | ScopStandardAnalysisResults &SAR, |
760 | 0 | SPMUpdater &) { |
761 | 0 | exportScop(S); |
762 | 0 | return PreservedAnalyses::all(); |
763 | 0 | } |
764 | | |
765 | | char JSONImporter::ID = 0; |
766 | | |
767 | 41 | void JSONImporter::printScop(raw_ostream &OS, Scop &S) const { |
768 | 41 | OS << S; |
769 | 41 | for (std::vector<std::string>::const_iterator I = NewAccessStrings.begin(), |
770 | 41 | E = NewAccessStrings.end(); |
771 | 120 | I != E; I++79 ) |
772 | 79 | OS << "New access function '" << *I << "' detected in JSCOP file\n"; |
773 | 41 | } |
774 | | |
775 | 99 | bool JSONImporter::runOnScop(Scop &S) { |
776 | 99 | const Dependences &D = |
777 | 99 | getAnalysis<DependenceInfo>().getDependences(Dependences::AL_Statement); |
778 | 99 | const DataLayout &DL = S.getFunction().getParent()->getDataLayout(); |
779 | 99 | |
780 | 99 | if (!importScop(S, D, DL, &NewAccessStrings)) |
781 | 22 | report_fatal_error("Tried to import a malformed jscop file."); |
782 | 77 | |
783 | 77 | return false; |
784 | 77 | } |
785 | | |
786 | 98 | void JSONImporter::getAnalysisUsage(AnalysisUsage &AU) const { |
787 | 98 | ScopPass::getAnalysisUsage(AU); |
788 | 98 | AU.addRequired<DependenceInfo>(); |
789 | 98 | |
790 | 98 | // TODO: JSONImporter should throw away DependenceInfo. |
791 | 98 | AU.addPreserved<DependenceInfo>(); |
792 | 98 | } |
793 | | |
794 | 0 | Pass *polly::createJSONImporterPass() { return new JSONImporter(); } |
795 | | |
796 | | PreservedAnalyses JSONImportPass::run(Scop &S, ScopAnalysisManager &SAM, |
797 | | ScopStandardAnalysisResults &SAR, |
798 | 0 | SPMUpdater &) { |
799 | 0 | const Dependences &D = |
800 | 0 | SAM.getResult<DependenceAnalysis>(S, SAR).getDependences( |
801 | 0 | Dependences::AL_Statement); |
802 | 0 | const DataLayout &DL = S.getFunction().getParent()->getDataLayout(); |
803 | 0 |
|
804 | 0 | if (!importScop(S, D, DL)) |
805 | 0 | report_fatal_error("Tried to import a malformed jscop file."); |
806 | 0 | |
807 | 0 | // This invalidates all analyses on Scop. |
808 | 0 | PreservedAnalyses PA; |
809 | 0 | PA.preserveSet<AllAnalysesOn<Module>>(); |
810 | 0 | PA.preserveSet<AllAnalysesOn<Function>>(); |
811 | 0 | PA.preserveSet<AllAnalysesOn<Loop>>(); |
812 | 0 | return PA; |
813 | 0 | } |
814 | | |
815 | 48.2k | INITIALIZE_PASS_BEGIN(JSONExporter, "polly-export-jscop", |
816 | 48.2k | "Polly - Export Scops as JSON" |
817 | 48.2k | " (Writes a .jscop file for each Scop)", |
818 | 48.2k | false, false); |
819 | 48.2k | INITIALIZE_PASS_DEPENDENCY(DependenceInfo) |
820 | 48.2k | INITIALIZE_PASS_END(JSONExporter, "polly-export-jscop", |
821 | | "Polly - Export Scops as JSON" |
822 | | " (Writes a .jscop file for each Scop)", |
823 | | false, false) |
824 | | |
825 | 48.2k | INITIALIZE_PASS_BEGIN(JSONImporter, "polly-import-jscop", |
826 | 48.2k | "Polly - Import Scops from JSON" |
827 | 48.2k | " (Reads a .jscop file for each Scop)", |
828 | 48.2k | false, false); |
829 | 48.2k | INITIALIZE_PASS_DEPENDENCY(DependenceInfo) |
830 | 48.2k | INITIALIZE_PASS_END(JSONImporter, "polly-import-jscop", |
831 | | "Polly - Import Scops from JSON" |
832 | | " (Reads a .jscop file for each Scop)", |
833 | | false, false) |