update isl to version 0.25
[pet.git] / pet.cc
blob4527ea13524c4a0b7ece9b39d6eec69de81b7e4b
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2014 Ecole Normale Superieure. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
32 * Leiden University.
33 */
35 #include "config.h"
36 #undef PACKAGE
38 #include <stdlib.h>
39 #include <map>
40 #include <vector>
41 #include <iostream>
42 #ifdef HAVE_ADT_OWNINGPTR_H
43 #include <llvm/ADT/OwningPtr.h>
44 #else
45 #include <memory>
46 #endif
47 #ifdef HAVE_LLVM_OPTION_ARG_H
48 #include <llvm/Option/Arg.h>
49 #endif
50 #include <llvm/Support/raw_ostream.h>
51 #include <llvm/Support/ManagedStatic.h>
52 #include <llvm/Support/MemoryBuffer.h>
53 #include <llvm/Support/Host.h>
54 #include <clang/Basic/Version.h>
55 #include <clang/Basic/Builtins.h>
56 #include <clang/Basic/FileSystemOptions.h>
57 #include <clang/Basic/FileManager.h>
58 #include <clang/Basic/TargetOptions.h>
59 #include <clang/Basic/TargetInfo.h>
60 #include <clang/Driver/Compilation.h>
61 #include <clang/Driver/Driver.h>
62 #include <clang/Driver/Tool.h>
63 #include <clang/Frontend/CompilerInstance.h>
64 #include <clang/Frontend/CompilerInvocation.h>
65 #ifdef HAVE_BASIC_DIAGNOSTICOPTIONS_H
66 #include <clang/Basic/DiagnosticOptions.h>
67 #else
68 #include <clang/Frontend/DiagnosticOptions.h>
69 #endif
70 #include <clang/Frontend/TextDiagnosticPrinter.h>
71 #ifdef HAVE_LEX_HEADERSEARCHOPTIONS_H
72 #include <clang/Lex/HeaderSearchOptions.h>
73 #else
74 #include <clang/Frontend/HeaderSearchOptions.h>
75 #endif
76 #ifdef HAVE_CLANG_BASIC_LANGSTANDARD_H
77 #include <clang/Basic/LangStandard.h>
78 #else
79 #include <clang/Frontend/LangStandard.h>
80 #endif
81 #ifdef HAVE_LEX_PREPROCESSOROPTIONS_H
82 #include <clang/Lex/PreprocessorOptions.h>
83 #else
84 #include <clang/Frontend/PreprocessorOptions.h>
85 #endif
86 #include <clang/Frontend/FrontendOptions.h>
87 #include <clang/Frontend/Utils.h>
88 #include <clang/Lex/HeaderSearch.h>
89 #include <clang/Lex/Preprocessor.h>
90 #include <clang/Lex/Pragma.h>
91 #include <clang/AST/ASTContext.h>
92 #include <clang/AST/ASTConsumer.h>
93 #include <clang/Sema/Sema.h>
94 #include <clang/Sema/SemaDiagnostic.h>
95 #include <clang/Parse/Parser.h>
96 #include <clang/Parse/ParseAST.h>
98 #include <isl/ctx.h>
99 #include <isl/constraint.h>
101 #include <pet.h>
103 #include "clang_compatibility.h"
104 #include "id.h"
105 #include "options.h"
106 #include "scan.h"
107 #include "print.h"
109 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
111 using namespace std;
112 using namespace clang;
113 using namespace clang::driver;
114 #ifdef HAVE_LLVM_OPTION_ARG_H
115 using namespace llvm::opt;
116 #endif
118 #ifdef HAVE_ADT_OWNINGPTR_H
119 #define unique_ptr llvm::OwningPtr
120 #endif
122 /* Called if we found something we didn't expect in one of the pragmas.
123 * We'll provide more informative warnings later.
125 static void unsupported(Preprocessor &PP, SourceLocation loc)
127 DiagnosticsEngine &diag = PP.getDiagnostics();
128 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
129 "unsupported");
130 DiagnosticBuilder B = diag.Report(loc, id);
133 static int get_int(const char *s)
135 return s[0] == '"' ? atoi(s + 1) : atoi(s);
138 static ValueDecl *get_value_decl(Sema &sema, Token &token)
140 IdentifierInfo *name;
141 Decl *decl;
143 if (token.isNot(tok::identifier))
144 return NULL;
146 name = token.getIdentifierInfo();
147 decl = sema.LookupSingleName(sema.TUScope, name,
148 token.getLocation(), Sema::LookupOrdinaryName);
149 return decl ? cast_or_null<ValueDecl>(decl) : NULL;
152 /* Handle pragmas of the form
154 * #pragma value_bounds identifier lower_bound upper_bound
156 * For each such pragma, add a mapping
157 * { identifier[] -> [i] : lower_bound <= i <= upper_bound }
158 * to value_bounds.
160 struct PragmaValueBoundsHandler : public PragmaHandler {
161 Sema &sema;
162 isl_ctx *ctx;
163 isl_union_map *value_bounds;
165 PragmaValueBoundsHandler(isl_ctx *ctx, Sema &sema) :
166 PragmaHandler("value_bounds"), sema(sema), ctx(ctx) {
167 isl_space *space = isl_space_params_alloc(ctx, 0);
168 value_bounds = isl_union_map_empty(space);
171 ~PragmaValueBoundsHandler() {
172 isl_union_map_free(value_bounds);
175 virtual void HandlePragma(Preprocessor &PP,
176 PragmaIntroducer Introducer,
177 Token &ScopTok) {
178 isl_id *id;
179 isl_space *dim;
180 isl_map *map;
181 ValueDecl *vd;
182 Token token;
183 int lb;
184 int ub;
186 PP.Lex(token);
187 vd = get_value_decl(sema, token);
188 if (!vd) {
189 unsupported(PP, token.getLocation());
190 return;
193 PP.Lex(token);
194 if (!token.isLiteral()) {
195 unsupported(PP, token.getLocation());
196 return;
199 lb = get_int(token.getLiteralData());
201 PP.Lex(token);
202 if (!token.isLiteral()) {
203 unsupported(PP, token.getLocation());
204 return;
207 ub = get_int(token.getLiteralData());
209 dim = isl_space_alloc(ctx, 0, 0, 1);
210 map = isl_map_universe(dim);
211 map = isl_map_lower_bound_si(map, isl_dim_out, 0, lb);
212 map = isl_map_upper_bound_si(map, isl_dim_out, 0, ub);
213 id = isl_id_alloc(ctx, vd->getName().str().c_str(), vd);
214 map = isl_map_set_tuple_id(map, isl_dim_in, id);
216 value_bounds = isl_union_map_add_map(value_bounds, map);
220 /* Given a variable declaration, check if it has an integer initializer
221 * and if so, add a parameter corresponding to the variable to "value"
222 * with its value fixed to the integer initializer and return the result.
224 static __isl_give isl_set *extract_initialization(__isl_take isl_set *value,
225 ValueDecl *decl)
227 VarDecl *vd;
228 Expr *expr;
229 IntegerLiteral *il;
230 isl_val *v;
231 isl_ctx *ctx;
232 isl_id *id;
233 isl_space *space;
234 isl_set *set;
236 vd = cast<VarDecl>(decl);
237 if (!vd)
238 return value;
239 if (!vd->getType()->isIntegerType())
240 return value;
241 expr = vd->getInit();
242 if (!expr)
243 return value;
244 il = cast<IntegerLiteral>(expr);
245 if (!il)
246 return value;
248 ctx = isl_set_get_ctx(value);
249 id = isl_id_alloc(ctx, vd->getName().str().c_str(), vd);
250 space = isl_space_params_alloc(ctx, 1);
251 space = isl_space_set_dim_id(space, isl_dim_param, 0, id);
252 set = isl_set_universe(space);
254 v = PetScan::extract_int(ctx, il);
255 set = isl_set_fix_val(set, isl_dim_param, 0, v);
257 return isl_set_intersect(value, set);
260 /* Handle pragmas of the form
262 * #pragma parameter identifier lower_bound
263 * and
264 * #pragma parameter identifier lower_bound upper_bound
266 * For each such pragma, intersect the context with the set
267 * [identifier] -> { [] : lower_bound <= identifier <= upper_bound }
269 struct PragmaParameterHandler : public PragmaHandler {
270 Sema &sema;
271 isl_set *&context;
272 isl_set *&context_value;
274 PragmaParameterHandler(Sema &sema, isl_set *&context,
275 isl_set *&context_value) :
276 PragmaHandler("parameter"), sema(sema), context(context),
277 context_value(context_value) {}
279 virtual void HandlePragma(Preprocessor &PP,
280 PragmaIntroducer Introducer,
281 Token &ScopTok) {
282 isl_id *id;
283 isl_ctx *ctx = isl_set_get_ctx(context);
284 isl_space *dim;
285 isl_set *set;
286 ValueDecl *vd;
287 Token token;
288 int lb;
289 int ub;
290 bool has_ub = false;
292 PP.Lex(token);
293 vd = get_value_decl(sema, token);
294 if (!vd) {
295 unsupported(PP, token.getLocation());
296 return;
299 PP.Lex(token);
300 if (!token.isLiteral()) {
301 unsupported(PP, token.getLocation());
302 return;
305 lb = get_int(token.getLiteralData());
307 PP.Lex(token);
308 if (token.isLiteral()) {
309 has_ub = true;
310 ub = get_int(token.getLiteralData());
311 } else if (token.isNot(tok::eod)) {
312 unsupported(PP, token.getLocation());
313 return;
316 id = isl_id_alloc(ctx, vd->getName().str().c_str(), vd);
317 dim = isl_space_params_alloc(ctx, 1);
318 dim = isl_space_set_dim_id(dim, isl_dim_param, 0, id);
320 set = isl_set_universe(dim);
322 set = isl_set_lower_bound_si(set, isl_dim_param, 0, lb);
323 if (has_ub)
324 set = isl_set_upper_bound_si(set, isl_dim_param, 0, ub);
326 context = isl_set_intersect(context, set);
328 context_value = extract_initialization(context_value, vd);
332 /* Handle pragmas of the form
334 * #pragma pencil independent
336 * For each such pragma, add an entry to the "independent" vector.
338 struct PragmaPencilHandler : public PragmaHandler {
339 std::vector<Independent> &independent;
341 PragmaPencilHandler(std::vector<Independent> &independent) :
342 PragmaHandler("pencil"), independent(independent) {}
344 virtual void HandlePragma(Preprocessor &PP,
345 PragmaIntroducer Introducer,
346 Token &PencilTok) {
347 Token token;
348 IdentifierInfo *info;
350 PP.Lex(token);
351 if (token.isNot(tok::identifier))
352 return;
354 info = token.getIdentifierInfo();
355 if (!info->isStr("independent"))
356 return;
358 PP.Lex(token);
359 if (token.isNot(tok::eod))
360 return;
362 SourceManager &SM = PP.getSourceManager();
363 SourceLocation sloc = PencilTok.getLocation();
364 unsigned line = SM.getExpansionLineNumber(sloc);
365 independent.push_back(Independent(line));
369 #ifdef HAVE_TRANSLATELINECOL
371 /* Return a SourceLocation for line "line", column "col" of file "FID".
373 SourceLocation translateLineCol(SourceManager &SM, FileID FID, unsigned line,
374 unsigned col)
376 return SM.translateLineCol(FID, line, col);
379 #else
381 /* Return a SourceLocation for line "line", column "col" of file "FID".
383 SourceLocation translateLineCol(SourceManager &SM, FileID FID, unsigned line,
384 unsigned col)
386 return SM.getLocation(SM.getFileEntryForID(FID), line, col);
389 #endif
391 /* List of pairs of #pragma scop and #pragma endscop locations.
393 struct ScopLocList {
394 std::vector<ScopLoc> list;
396 /* Add a new start (#pragma scop) location to the list.
397 * If the last #pragma scop did not have a matching
398 * #pragma endscop then overwrite it.
399 * "start" points to the location of the scop pragma.
401 void add_start(SourceManager &SM, SourceLocation start) {
402 ScopLoc loc;
404 loc.scop = start;
405 int line = SM.getExpansionLineNumber(start);
406 start = translateLineCol(SM, SM.getFileID(start), line, 1);
407 loc.start_line = line;
408 loc.start = SM.getFileOffset(start);
409 if (list.size() == 0 || list[list.size() - 1].end != 0)
410 list.push_back(loc);
411 else
412 list[list.size() - 1] = loc;
415 /* Set the end location (#pragma endscop) of the last pair
416 * in the list.
417 * If there is no such pair of if the end of that pair
418 * is already set, then ignore the spurious #pragma endscop.
419 * "end" points to the location of the endscop pragma.
421 void add_end(SourceManager &SM, SourceLocation end) {
422 if (list.size() == 0 || list[list.size() - 1].end != 0)
423 return;
424 list[list.size() - 1].endscop = end;
425 int line = SM.getExpansionLineNumber(end);
426 end = translateLineCol(SM, SM.getFileID(end), line + 1, 1);
427 list[list.size() - 1].end = SM.getFileOffset(end);
431 /* Handle pragmas of the form
433 * #pragma scop
435 * In particular, store the location of the line containing
436 * the pragma in the list "scops".
438 struct PragmaScopHandler : public PragmaHandler {
439 ScopLocList &scops;
441 PragmaScopHandler(ScopLocList &scops) :
442 PragmaHandler("scop"), scops(scops) {}
444 virtual void HandlePragma(Preprocessor &PP,
445 PragmaIntroducer Introducer,
446 Token &ScopTok) {
447 SourceManager &SM = PP.getSourceManager();
448 SourceLocation sloc = ScopTok.getLocation();
449 scops.add_start(SM, sloc);
453 /* Handle pragmas of the form
455 * #pragma endscop
457 * In particular, store the location of the line following the one containing
458 * the pragma in the list "scops".
460 struct PragmaEndScopHandler : public PragmaHandler {
461 ScopLocList &scops;
463 PragmaEndScopHandler(ScopLocList &scops) :
464 PragmaHandler("endscop"), scops(scops) {}
466 virtual void HandlePragma(Preprocessor &PP,
467 PragmaIntroducer Introducer,
468 Token &EndScopTok) {
469 SourceManager &SM = PP.getSourceManager();
470 SourceLocation sloc = EndScopTok.getLocation();
471 scops.add_end(SM, sloc);
475 /* Handle pragmas of the form
477 * #pragma live-out identifier, identifier, ...
479 * Each identifier on the line is stored in live_out.
481 struct PragmaLiveOutHandler : public PragmaHandler {
482 Sema &sema;
483 set<ValueDecl *> &live_out;
485 PragmaLiveOutHandler(Sema &sema, set<ValueDecl *> &live_out) :
486 PragmaHandler("live"), sema(sema), live_out(live_out) {}
488 virtual void HandlePragma(Preprocessor &PP,
489 PragmaIntroducer Introducer,
490 Token &ScopTok) {
491 Token token;
493 PP.Lex(token);
494 if (token.isNot(tok::minus))
495 return;
496 PP.Lex(token);
497 if (token.isNot(tok::identifier) ||
498 !token.getIdentifierInfo()->isStr("out"))
499 return;
501 PP.Lex(token);
502 while (token.isNot(tok::eod)) {
503 ValueDecl *vd;
505 vd = get_value_decl(sema, token);
506 if (!vd) {
507 unsupported(PP, token.getLocation());
508 return;
510 live_out.insert(vd);
511 PP.Lex(token);
512 if (token.is(tok::comma))
513 PP.Lex(token);
518 /* For each array in "scop", set its value_bounds property
519 * based on the information in "value_bounds" and
520 * mark it as live_out if it appears in "live_out".
522 static void update_arrays(struct pet_scop *scop,
523 __isl_take isl_union_map *value_bounds, set<ValueDecl *> &live_out)
525 set<ValueDecl *>::iterator lo_it;
526 isl_ctx *ctx = isl_union_map_get_ctx(value_bounds);
528 if (!scop) {
529 isl_union_map_free(value_bounds);
530 return;
533 for (int i = 0; i < scop->n_array; ++i) {
534 isl_id *id;
535 isl_space *space;
536 isl_map *bounds;
537 ValueDecl *decl;
538 pet_array *array = scop->arrays[i];
540 id = isl_set_get_tuple_id(array->extent);
541 decl = pet_id_get_decl(id);
543 space = isl_space_alloc(ctx, 0, 0, 1);
544 space = isl_space_set_tuple_id(space, isl_dim_in, id);
546 bounds = isl_union_map_extract_map(value_bounds, space);
547 if (!isl_map_plain_is_empty(bounds))
548 array->value_bounds = isl_map_range(bounds);
549 else
550 isl_map_free(bounds);
552 lo_it = live_out.find(decl);
553 if (lo_it != live_out.end())
554 array->live_out = 1;
557 isl_union_map_free(value_bounds);
560 /* Extract a pet_scop (if any) from each appropriate function.
561 * Each detected scop is passed to "fn".
562 * When autodetecting, at most one scop is extracted from each function.
563 * If "function" is not NULL, then we only extract a pet_scop if the
564 * name of the function matches.
565 * If "autodetect" is false, then we only extract if we have seen
566 * scop and endscop pragmas and if these are situated inside the function
567 * body.
569 struct PetASTConsumer : public ASTConsumer {
570 Preprocessor &PP;
571 ASTContext &ast_context;
572 DiagnosticsEngine &diags;
573 ScopLocList &scops;
574 std::vector<Independent> independent;
575 const char *function;
576 pet_options *options;
577 isl_ctx *ctx;
578 isl_set *context;
579 isl_set *context_value;
580 set<ValueDecl *> live_out;
581 PragmaValueBoundsHandler *vb_handler;
582 isl_stat (*fn)(struct pet_scop *scop, void *user);
583 void *user;
584 bool error;
586 PetASTConsumer(isl_ctx *ctx, Preprocessor &PP, ASTContext &ast_context,
587 DiagnosticsEngine &diags, ScopLocList &scops,
588 const char *function, pet_options *options,
589 isl_stat (*fn)(struct pet_scop *scop, void *user), void *user) :
590 PP(PP), ast_context(ast_context), diags(diags),
591 scops(scops), function(function), options(options),
592 ctx(ctx),
593 vb_handler(NULL), fn(fn), user(user), error(false)
595 isl_space *space;
596 space = isl_space_params_alloc(ctx, 0);
597 context = isl_set_universe(isl_space_copy(space));
598 context_value = isl_set_universe(space);
601 ~PetASTConsumer() {
602 isl_set_free(context);
603 isl_set_free(context_value);
606 void handle_value_bounds(Sema *sema) {
607 vb_handler = new PragmaValueBoundsHandler(ctx, *sema);
608 PP.AddPragmaHandler(vb_handler);
611 /* Add all pragma handlers to this->PP.
612 * The pencil pragmas are only handled if the pencil option is set.
614 void add_pragma_handlers(Sema *sema) {
615 PP.AddPragmaHandler(new PragmaParameterHandler(*sema, context,
616 context_value));
617 if (options->pencil) {
618 PragmaHandler *PH;
619 PH = new PragmaPencilHandler(independent);
620 PP.AddPragmaHandler(PH);
622 handle_value_bounds(sema);
625 __isl_give isl_union_map *get_value_bounds() {
626 return isl_union_map_copy(vb_handler->value_bounds);
629 /* Pass "scop" to "fn" after performing some postprocessing.
630 * In particular, add the context and value_bounds constraints
631 * speficied through pragmas, add reference identifiers and
632 * reset user pointers on parameters and tuple ids.
634 * If "scop" does not contain any statements and autodetect
635 * is turned on, then skip it.
637 void call_fn(pet_scop *scop) {
638 if (!scop) {
639 error = true;
640 return;
642 if (diags.hasErrorOccurred()) {
643 error = true;
644 pet_scop_free(scop);
645 return;
647 if (options->autodetect && scop->n_stmt == 0) {
648 pet_scop_free(scop);
649 return;
651 scop->context = isl_set_intersect(scop->context,
652 isl_set_copy(context));
653 scop->context_value = isl_set_intersect(scop->context_value,
654 isl_set_copy(context_value));
656 update_arrays(scop, get_value_bounds(), live_out);
658 scop = pet_scop_add_ref_ids(scop);
659 scop = pet_scop_anonymize(scop);
661 if (fn(scop, user) < 0)
662 error = true;
665 /* For each explicitly marked scop (using pragmas),
666 * extract the scop and call "fn" on it if it is inside "fd".
668 void scan_scops(FunctionDecl *fd) {
669 unsigned start, end;
670 vector<ScopLoc>::iterator it;
671 isl_union_map *vb = vb_handler->value_bounds;
672 SourceManager &SM = PP.getSourceManager();
673 pet_scop *scop;
675 if (scops.list.size() == 0)
676 return;
678 start = SM.getFileOffset(begin_loc(fd));
679 end = SM.getFileOffset(end_loc(fd));
681 for (it = scops.list.begin(); it != scops.list.end(); ++it) {
682 ScopLoc loc = *it;
683 if (!loc.end)
684 continue;
685 if (start > loc.end)
686 continue;
687 if (end < loc.start)
688 continue;
689 PetScan ps(PP, ast_context, fd, loc, options,
690 isl_union_map_copy(vb), independent);
691 scop = ps.scan(fd);
692 call_fn(scop);
696 virtual HandleTopLevelDeclReturn HandleTopLevelDecl(DeclGroupRef dg) {
697 DeclGroupRef::iterator it;
699 if (error)
700 return HandleTopLevelDeclContinue;
702 for (it = dg.begin(); it != dg.end(); ++it) {
703 isl_union_map *vb = vb_handler->value_bounds;
704 FunctionDecl *fd = dyn_cast<clang::FunctionDecl>(*it);
705 if (!fd)
706 continue;
707 if (!fd->hasBody())
708 continue;
709 if (function &&
710 fd->getNameInfo().getAsString() != function)
711 continue;
712 if (options->autodetect) {
713 ScopLoc loc;
714 pet_scop *scop;
715 PetScan ps(PP, ast_context, fd, loc, options,
716 isl_union_map_copy(vb),
717 independent);
718 scop = ps.scan(fd);
719 if (!scop)
720 continue;
721 call_fn(scop);
722 continue;
724 scan_scops(fd);
727 return HandleTopLevelDeclContinue;
731 static const char *ResourceDir =
732 CLANG_PREFIX "/lib/clang/" CLANG_VERSION_STRING;
734 static const char *implicit_functions[] = {
735 "min", "max", "intMod", "intCeil", "intFloor", "ceild", "floord"
737 static const char *pencil_implicit_functions[] = {
738 "imin", "umin", "imax", "umax", "__pencil_kill"
741 /* Should "ident" be treated as an implicit function?
742 * If "pencil" is set, then also allow pencil specific builtins.
744 static bool is_implicit(const IdentifierInfo *ident, int pencil)
746 const char *name = ident->getNameStart();
747 for (size_t i = 0; i < ARRAY_SIZE(implicit_functions); ++i)
748 if (!strcmp(name, implicit_functions[i]))
749 return true;
750 if (!pencil)
751 return false;
752 for (size_t i = 0; i < ARRAY_SIZE(pencil_implicit_functions); ++i)
753 if (!strcmp(name, pencil_implicit_functions[i]))
754 return true;
755 return false;
758 /* Ignore implicit function declaration warnings on
759 * "min", "max", "ceild" and "floord" as we detect and handle these
760 * in PetScan.
761 * If "pencil" is set, then also ignore them on pencil specific
762 * builtins.
764 struct MyDiagnosticPrinter : public TextDiagnosticPrinter {
765 const DiagnosticOptions *DiagOpts;
766 int pencil;
767 #ifdef HAVE_BASIC_DIAGNOSTICOPTIONS_H
768 MyDiagnosticPrinter(DiagnosticOptions *DO, int pencil) :
769 TextDiagnosticPrinter(llvm::errs(), DO), pencil(pencil) {}
770 virtual DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
771 return new MyDiagnosticPrinter(&Diags.getDiagnosticOptions(),
772 pencil);
774 #else
775 MyDiagnosticPrinter(const DiagnosticOptions &DO, int pencil) :
776 DiagOpts(&DO), TextDiagnosticPrinter(llvm::errs(), DO),
777 pencil(pencil) {}
778 virtual DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
779 return new MyDiagnosticPrinter(*DiagOpts, pencil);
781 #endif
782 virtual void HandleDiagnostic(DiagnosticsEngine::Level level,
783 const DiagnosticInfo &info) {
784 if (info.getID() == diag::ext_implicit_function_decl_c99 &&
785 info.getNumArgs() >= 1 &&
786 info.getArgKind(0) == DiagnosticsEngine::ak_identifierinfo &&
787 is_implicit(info.getArgIdentifier(0), pencil))
788 /* ignore warning */;
789 else
790 TextDiagnosticPrinter::HandleDiagnostic(level, info);
794 #ifdef USE_ARRAYREF
796 #ifdef HAVE_CXXISPRODUCTION
797 static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags)
799 return new Driver(binary, llvm::sys::getDefaultTargetTriple(),
800 "", false, false, Diags);
802 #elif defined(HAVE_ISPRODUCTION)
803 static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags)
805 return new Driver(binary, llvm::sys::getDefaultTargetTriple(),
806 "", false, Diags);
808 #elif defined(DRIVER_CTOR_TAKES_DEFAULTIMAGENAME)
809 static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags)
811 return new Driver(binary, llvm::sys::getDefaultTargetTriple(),
812 "", Diags);
814 #else
815 static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags)
817 return new Driver(binary, llvm::sys::getDefaultTargetTriple(), Diags);
819 #endif
821 namespace clang { namespace driver { class Job; } }
823 /* Clang changed its API from 3.5 to 3.6 and once more in 3.7.
824 * We fix this with a simple overloaded function here.
826 struct ClangAPI {
827 static Job *command(Job *J) { return J; }
828 static Job *command(Job &J) { return &J; }
829 static Command *command(Command &C) { return &C; }
832 #ifdef CREATE_FROM_ARGS_TAKES_ARRAYREF
834 /* Call CompilerInvocation::CreateFromArgs with the right arguments.
835 * In this case, an ArrayRef<const char *>.
837 static void create_from_args(CompilerInvocation &invocation,
838 const ArgStringList *args, DiagnosticsEngine &Diags)
840 CompilerInvocation::CreateFromArgs(invocation, *args, Diags);
843 #else
845 /* Call CompilerInvocation::CreateFromArgs with the right arguments.
846 * In this case, two "const char *" pointers.
848 static void create_from_args(CompilerInvocation &invocation,
849 const ArgStringList *args, DiagnosticsEngine &Diags)
851 CompilerInvocation::CreateFromArgs(invocation, args->data() + 1,
852 args->data() + args->size(),
853 Diags);
856 #endif
858 /* Create a CompilerInvocation object that stores the command line
859 * arguments constructed by the driver.
860 * The arguments are mainly useful for setting up the system include
861 * paths on newer clangs and on some platforms.
863 static CompilerInvocation *construct_invocation(const char *filename,
864 DiagnosticsEngine &Diags)
866 const char *binary = CLANG_PREFIX"/bin/clang";
867 const unique_ptr<Driver> driver(construct_driver(binary, Diags));
868 std::vector<const char *> Argv;
869 Argv.push_back(binary);
870 Argv.push_back(filename);
871 const unique_ptr<Compilation> compilation(
872 driver->BuildCompilation(llvm::ArrayRef<const char *>(Argv)));
873 JobList &Jobs = compilation->getJobs();
874 if (Jobs.size() < 1)
875 return NULL;
877 Command *cmd = cast<Command>(ClangAPI::command(*Jobs.begin()));
878 if (strcmp(cmd->getCreator().getName(), "clang"))
879 return NULL;
881 const ArgStringList *args = &cmd->getArguments();
883 CompilerInvocation *invocation = new CompilerInvocation;
884 create_from_args(*invocation, args, Diags);
885 return invocation;
888 #else
890 static CompilerInvocation *construct_invocation(const char *filename,
891 DiagnosticsEngine &Diags)
893 return NULL;
896 #endif
898 #ifdef HAVE_BASIC_DIAGNOSTICOPTIONS_H
900 static MyDiagnosticPrinter *construct_printer(CompilerInstance *Clang,
901 int pencil)
903 return new MyDiagnosticPrinter(new DiagnosticOptions(), pencil);
906 #else
908 static MyDiagnosticPrinter *construct_printer(CompilerInstance *Clang,
909 int pencil)
911 return new MyDiagnosticPrinter(Clang->getDiagnosticOpts(), pencil);
914 #endif
916 #ifdef CREATETARGETINFO_TAKES_SHARED_PTR
918 static TargetInfo *create_target_info(CompilerInstance *Clang,
919 DiagnosticsEngine &Diags)
921 shared_ptr<TargetOptions> TO = Clang->getInvocation().TargetOpts;
922 TO->Triple = llvm::sys::getDefaultTargetTriple();
923 return TargetInfo::CreateTargetInfo(Diags, TO);
926 #elif defined(CREATETARGETINFO_TAKES_POINTER)
928 static TargetInfo *create_target_info(CompilerInstance *Clang,
929 DiagnosticsEngine &Diags)
931 TargetOptions &TO = Clang->getTargetOpts();
932 TO.Triple = llvm::sys::getDefaultTargetTriple();
933 return TargetInfo::CreateTargetInfo(Diags, &TO);
936 #else
938 static TargetInfo *create_target_info(CompilerInstance *Clang,
939 DiagnosticsEngine &Diags)
941 TargetOptions &TO = Clang->getTargetOpts();
942 TO.Triple = llvm::sys::getDefaultTargetTriple();
943 return TargetInfo::CreateTargetInfo(Diags, TO);
946 #endif
948 #ifdef CREATEDIAGNOSTICS_TAKES_ARG
950 static void create_diagnostics(CompilerInstance *Clang)
952 Clang->createDiagnostics(0, NULL);
955 #else
957 static void create_diagnostics(CompilerInstance *Clang)
959 Clang->createDiagnostics();
962 #endif
964 #ifdef CREATEPREPROCESSOR_TAKES_TUKIND
966 static void create_preprocessor(CompilerInstance *Clang)
968 Clang->createPreprocessor(TU_Complete);
971 #else
973 static void create_preprocessor(CompilerInstance *Clang)
975 Clang->createPreprocessor();
978 #endif
980 #ifdef ADDPATH_TAKES_4_ARGUMENTS
982 void add_path(HeaderSearchOptions &HSO, string Path)
984 HSO.AddPath(Path, frontend::Angled, false, false);
987 #else
989 void add_path(HeaderSearchOptions &HSO, string Path)
991 HSO.AddPath(Path, frontend::Angled, true, false, false);
994 #endif
996 #ifdef HAVE_SETMAINFILEID
998 static void create_main_file_id(SourceManager &SM, const FileEntry *file)
1000 SM.setMainFileID(SM.createFileID(file, SourceLocation(),
1001 SrcMgr::C_User));
1004 #else
1006 static void create_main_file_id(SourceManager &SM, const FileEntry *file)
1008 SM.createMainFileID(file);
1011 #endif
1013 #ifdef SETLANGDEFAULTS_TAKES_5_ARGUMENTS
1015 #include "set_lang_defaults_arg4.h"
1017 static void set_lang_defaults(CompilerInstance *Clang)
1019 PreprocessorOptions &PO = Clang->getPreprocessorOpts();
1020 TargetOptions &TO = Clang->getTargetOpts();
1021 llvm::Triple T(TO.Triple);
1022 SETLANGDEFAULTS::setLangDefaults(Clang->getLangOpts(), IK_C, T,
1023 setLangDefaultsArg4(PO),
1024 LangStandard::lang_unspecified);
1027 #else
1029 static void set_lang_defaults(CompilerInstance *Clang)
1031 CompilerInvocation::setLangDefaults(Clang->getLangOpts(), IK_C,
1032 LangStandard::lang_unspecified);
1035 #endif
1037 #ifdef SETINVOCATION_TAKES_SHARED_PTR
1039 static void set_invocation(CompilerInstance *Clang,
1040 CompilerInvocation *invocation)
1042 Clang->setInvocation(std::shared_ptr<CompilerInvocation>(invocation));
1045 #else
1047 static void set_invocation(CompilerInstance *Clang,
1048 CompilerInvocation *invocation)
1050 Clang->setInvocation(invocation);
1053 #endif
1055 /* Helper function for ignore_error that only gets enabled if T
1056 * (which is either const FileEntry * or llvm::ErrorOr<const FileEntry *>)
1057 * has getError method, i.e., if it is llvm::ErrorOr<const FileEntry *>.
1059 template <class T>
1060 static const FileEntry *ignore_error_helper(const T obj, int,
1061 int[1][sizeof(obj.getError())])
1063 return *obj;
1066 /* Helper function for ignore_error that is always enabled,
1067 * but that only gets selected if the variant above is not enabled,
1068 * i.e., if T is const FileEntry *.
1070 template <class T>
1071 static const FileEntry *ignore_error_helper(const T obj, long, void *)
1073 return obj;
1076 /* Given either a const FileEntry * or a llvm::ErrorOr<const FileEntry *>,
1077 * extract out the const FileEntry *.
1079 template <class T>
1080 static const FileEntry *ignore_error(const T obj)
1082 return ignore_error_helper(obj, 0, NULL);
1085 /* Return the FileEntry corresponding to the given file name
1086 * in the given compiler instances, ignoring any error.
1088 static const FileEntry *getFile(CompilerInstance *Clang, std::string Filename)
1090 return ignore_error(Clang->getFileManager().getFile(Filename));
1093 /* Return the ownership of "buffer".
1095 * If "buffer" is a pointer, simply return the pointer.
1096 * If "buffer" is a std::unique_ptr, call release() on it.
1097 * Note that std::unique_ptr was not being used back when clang
1098 * was still using llvm::OwningPtr.
1100 static llvm::MemoryBuffer *release(llvm::MemoryBuffer *buffer)
1102 return buffer;
1104 #ifndef HAVE_ADT_OWNINGPTR_H
1105 static llvm::MemoryBuffer *release(std::unique_ptr<llvm::MemoryBuffer> buffer)
1107 return buffer.release();
1109 #endif
1111 /* Pencil specific predefines.
1113 static const char *pencil_predefines =
1114 "void __pencil_assume(int assumption);\n"
1115 "#define pencil_access(f) annotate(\"pencil_access(\" #f \")\")\n";
1117 /* Add pet specific predefines to the preprocessor.
1118 * Currently, these are all pencil specific, so they are only
1119 * added if "pencil" is set.
1121 * Include a special "/pet" header and ensure it gets replaced
1122 * by "pencil_predefines" by mapping the "/pet" file to a memory buffer.
1124 static void add_predefines(PreprocessorOptions &PO, int pencil)
1126 if (!pencil)
1127 return;
1129 PO.Includes.push_back("/pet");
1130 PO.addRemappedFile("/pet",
1131 release(llvm::MemoryBuffer::getMemBuffer(pencil_predefines)));
1134 /* Do not treat implicit function declaration warnings as errors.
1136 * Only do this if DiagnosticsEngine::setDiagnosticGroupWarningAsError
1137 * is available. In earlier versions of clang, these warnings
1138 * are not treated as errors by default.
1140 #ifdef HAVE_SET_DIAGNOSTIC_GROUP_WARNING_AS_ERROR
1141 static void set_implicit_function_declaration_no_error(DiagnosticsEngine &Diags)
1143 Diags.setDiagnosticGroupWarningAsError("implicit-function-declaration",
1144 false);
1146 #else
1147 static void set_implicit_function_declaration_no_error(DiagnosticsEngine &Diags)
1150 #endif
1152 /* Extract a pet_scop from each function in the C source file called "filename".
1153 * Each detected scop is passed to "fn".
1154 * If "function" is not NULL, only extract a pet_scop from the function
1155 * with that name.
1156 * If "autodetect" is set, extract any pet_scop we can find.
1157 * Otherwise, extract the pet_scop from the region delimited
1158 * by "scop" and "endscop" pragmas.
1160 * We first set up the clang parser and then try to extract the
1161 * pet_scop from the appropriate function(s) in PetASTConsumer.
1163 static isl_stat foreach_scop_in_C_source(isl_ctx *ctx,
1164 const char *filename, const char *function, pet_options *options,
1165 isl_stat (*fn)(struct pet_scop *scop, void *user), void *user)
1167 CompilerInstance *Clang = new CompilerInstance();
1168 create_diagnostics(Clang);
1169 DiagnosticsEngine &Diags = Clang->getDiagnostics();
1170 Diags.setSuppressSystemWarnings(true);
1171 set_implicit_function_declaration_no_error(Diags);
1172 TargetInfo *target = create_target_info(Clang, Diags);
1173 Clang->setTarget(target);
1174 set_lang_defaults(Clang);
1175 CompilerInvocation *invocation = construct_invocation(filename, Diags);
1176 if (invocation)
1177 set_invocation(Clang, invocation);
1178 Diags.setClient(construct_printer(Clang, options->pencil));
1179 Clang->createFileManager();
1180 Clang->createSourceManager(Clang->getFileManager());
1181 HeaderSearchOptions &HSO = Clang->getHeaderSearchOpts();
1182 HSO.ResourceDir = ResourceDir;
1183 for (int i = 0; i < options->n_path; ++i)
1184 add_path(HSO, options->paths[i]);
1185 PreprocessorOptions &PO = Clang->getPreprocessorOpts();
1186 for (int i = 0; i < options->n_define; ++i)
1187 PO.addMacroDef(options->defines[i]);
1188 add_predefines(PO, options->pencil);
1189 create_preprocessor(Clang);
1190 Preprocessor &PP = Clang->getPreprocessor();
1191 PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
1192 PP.getLangOpts());
1194 ScopLocList scops;
1196 const FileEntry *file = getFile(Clang, filename);
1197 if (!file)
1198 isl_die(ctx, isl_error_unknown, "unable to open file",
1199 do { delete Clang; return isl_stat_error; } while (0));
1200 create_main_file_id(Clang->getSourceManager(), file);
1202 Clang->createASTContext();
1203 PetASTConsumer consumer(ctx, PP, Clang->getASTContext(), Diags,
1204 scops, function, options, fn, user);
1205 Sema *sema = new Sema(PP, Clang->getASTContext(), consumer);
1207 if (!options->autodetect) {
1208 PP.AddPragmaHandler(new PragmaScopHandler(scops));
1209 PP.AddPragmaHandler(new PragmaEndScopHandler(scops));
1210 PP.AddPragmaHandler(new PragmaLiveOutHandler(*sema,
1211 consumer.live_out));
1214 consumer.add_pragma_handlers(sema);
1216 Diags.getClient()->BeginSourceFile(Clang->getLangOpts(), &PP);
1217 ParseAST(*sema);
1218 Diags.getClient()->EndSourceFile();
1220 delete sema;
1221 delete Clang;
1223 return consumer.error ? isl_stat_error : isl_stat_ok;
1226 /* Extract a pet_scop from each function in the C source file called "filename".
1227 * Each detected scop is passed to "fn".
1229 * This wrapper around foreach_scop_in_C_source is mainly used to ensure
1230 * that all objects on the stack (of that function) are destroyed before we
1231 * call llvm_shutdown.
1233 static isl_stat pet_foreach_scop_in_C_source(isl_ctx *ctx,
1234 const char *filename, const char *function,
1235 isl_stat (*fn)(struct pet_scop *scop, void *user), void *user)
1237 isl_stat r;
1238 pet_options *options;
1239 bool allocated = false;
1241 options = isl_ctx_peek_pet_options(ctx);
1242 if (!options) {
1243 options = pet_options_new_with_defaults();
1244 allocated = true;
1247 r = foreach_scop_in_C_source(ctx, filename, function, options,
1248 fn, user);
1249 llvm::llvm_shutdown();
1251 if (allocated)
1252 pet_options_free(options);
1254 return r;
1257 /* Store "scop" into the address pointed to by "user".
1258 * Return -1 to indicate that we are not interested in any further scops.
1259 * This function should therefore not be called a second call
1260 * so in principle there is no need to check if we have already set *user.
1262 static isl_stat set_first_scop(pet_scop *scop, void *user)
1264 pet_scop **p = (pet_scop **) user;
1266 if (!*p)
1267 *p = scop;
1268 else
1269 pet_scop_free(scop);
1271 return isl_stat_error;
1274 /* Extract a pet_scop from the C source file called "filename".
1275 * If "function" is not NULL, extract the pet_scop from the function
1276 * with that name.
1278 * We start extracting scops from every function and then abort
1279 * as soon as we have extracted one scop.
1281 struct pet_scop *pet_scop_extract_from_C_source(isl_ctx *ctx,
1282 const char *filename, const char *function)
1284 pet_scop *scop = NULL;
1286 pet_foreach_scop_in_C_source(ctx, filename, function,
1287 &set_first_scop, &scop);
1289 return scop;
1292 /* Internal data structure for pet_transform_C_source
1294 * transform is the function that should be called to print a scop
1295 * in is the input source file
1296 * out is the output source file
1297 * end is the offset of the end of the previous scop (zero if we have not
1298 * found any scop yet)
1299 * p is a printer that prints to out.
1301 struct pet_transform_data {
1302 __isl_give isl_printer *(*transform)(__isl_take isl_printer *p,
1303 struct pet_scop *scop, void *user);
1304 void *user;
1306 FILE *in;
1307 FILE *out;
1308 unsigned end;
1309 isl_printer *p;
1312 /* This function is called each time a scop is detected.
1314 * We first copy the input text code from the end of the previous scop
1315 * until the start of "scop" and then print the scop itself through
1316 * a call to data->transform. We set up the printer to print
1317 * the transformed code with the same (initial) indentation as
1318 * the original code.
1319 * Finally, we keep track of the end of "scop" so that we can
1320 * continue copying when we find the next scop.
1322 * Before calling data->transform, we store a pointer to the original
1323 * input file in the extended scop in case the user wants to call
1324 * pet_scop_print_original from the callback.
1326 static isl_stat pet_transform(struct pet_scop *scop, void *user)
1328 struct pet_transform_data *data = (struct pet_transform_data *) user;
1329 unsigned start;
1331 if (!scop)
1332 return isl_stat_error;
1333 start = pet_loc_get_start(scop->loc);
1334 if (copy(data->in, data->out, data->end, start) < 0)
1335 goto error;
1336 data->end = pet_loc_get_end(scop->loc);
1337 scop = pet_scop_set_input_file(scop, data->in);
1338 data->p = isl_printer_set_indent_prefix(data->p,
1339 pet_loc_get_indent(scop->loc));
1340 data->p = data->transform(data->p, scop, data->user);
1341 if (!data->p)
1342 return isl_stat_error;
1343 return isl_stat_ok;
1344 error:
1345 pet_scop_free(scop);
1346 return isl_stat_error;
1349 /* Transform the C source file "input" by rewriting each scop
1350 * through a call to "transform".
1351 * When autodetecting scops, at most one scop per function is rewritten.
1352 * The transformed C code is written to "output".
1354 * For each scop we find, we first copy the input text code
1355 * from the end of the previous scop (or the beginning of the file
1356 * in case of the first scop) until the start of the scop
1357 * and then print the scop itself through a call to "transform".
1358 * At the end we copy everything from the end of the final scop
1359 * until the end of the input file to "output".
1361 int pet_transform_C_source(isl_ctx *ctx, const char *input, FILE *out,
1362 __isl_give isl_printer *(*transform)(__isl_take isl_printer *p,
1363 struct pet_scop *scop, void *user), void *user)
1365 struct pet_transform_data data;
1366 int r;
1368 data.in = stdin;
1369 data.out = out;
1370 if (input && strcmp(input, "-")) {
1371 data.in = fopen(input, "r");
1372 if (!data.in)
1373 isl_die(ctx, isl_error_unknown, "unable to open file",
1374 return -1);
1377 data.p = isl_printer_to_file(ctx, data.out);
1378 data.p = isl_printer_set_output_format(data.p, ISL_FORMAT_C);
1380 data.transform = transform;
1381 data.user = user;
1382 data.end = 0;
1383 r = pet_foreach_scop_in_C_source(ctx, input, NULL,
1384 &pet_transform, &data);
1386 isl_printer_free(data.p);
1387 if (!data.p)
1388 r = -1;
1389 if (r == 0 && copy(data.in, data.out, data.end, -1) < 0)
1390 r = -1;
1392 if (data.in != stdin)
1393 fclose(data.in);
1395 return r;