pet.cc: handle change in CompilerInvocation::CreateFromArgs arguments
[pet.git] / pet.cc
blob006fce06ffcc696be20ea53fef69fe6c5892e83b
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/Host.h>
53 #include <clang/Basic/Version.h>
54 #include <clang/Basic/FileSystemOptions.h>
55 #include <clang/Basic/FileManager.h>
56 #include <clang/Basic/TargetOptions.h>
57 #include <clang/Basic/TargetInfo.h>
58 #include <clang/Driver/Compilation.h>
59 #include <clang/Driver/Driver.h>
60 #include <clang/Driver/Tool.h>
61 #include <clang/Frontend/CompilerInstance.h>
62 #include <clang/Frontend/CompilerInvocation.h>
63 #ifdef HAVE_BASIC_DIAGNOSTICOPTIONS_H
64 #include <clang/Basic/DiagnosticOptions.h>
65 #else
66 #include <clang/Frontend/DiagnosticOptions.h>
67 #endif
68 #include <clang/Frontend/TextDiagnosticPrinter.h>
69 #ifdef HAVE_LEX_HEADERSEARCHOPTIONS_H
70 #include <clang/Lex/HeaderSearchOptions.h>
71 #else
72 #include <clang/Frontend/HeaderSearchOptions.h>
73 #endif
74 #ifdef HAVE_CLANG_BASIC_LANGSTANDARD_H
75 #include <clang/Basic/LangStandard.h>
76 #else
77 #include <clang/Frontend/LangStandard.h>
78 #endif
79 #ifdef HAVE_LEX_PREPROCESSOROPTIONS_H
80 #include <clang/Lex/PreprocessorOptions.h>
81 #else
82 #include <clang/Frontend/PreprocessorOptions.h>
83 #endif
84 #include <clang/Frontend/FrontendOptions.h>
85 #include <clang/Frontend/Utils.h>
86 #include <clang/Lex/HeaderSearch.h>
87 #include <clang/Lex/Preprocessor.h>
88 #include <clang/Lex/Pragma.h>
89 #include <clang/AST/ASTContext.h>
90 #include <clang/AST/ASTConsumer.h>
91 #include <clang/Sema/Sema.h>
92 #include <clang/Sema/SemaDiagnostic.h>
93 #include <clang/Parse/Parser.h>
94 #include <clang/Parse/ParseAST.h>
96 #include <isl/ctx.h>
97 #include <isl/constraint.h>
99 #include <pet.h>
101 #include "clang_compatibility.h"
102 #include "id.h"
103 #include "options.h"
104 #include "scan.h"
105 #include "print.h"
107 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
109 using namespace std;
110 using namespace clang;
111 using namespace clang::driver;
112 #ifdef HAVE_LLVM_OPTION_ARG_H
113 using namespace llvm::opt;
114 #endif
116 #ifdef HAVE_ADT_OWNINGPTR_H
117 #define unique_ptr llvm::OwningPtr
118 #endif
120 /* Called if we found something we didn't expect in one of the pragmas.
121 * We'll provide more informative warnings later.
123 static void unsupported(Preprocessor &PP, SourceLocation loc)
125 DiagnosticsEngine &diag = PP.getDiagnostics();
126 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
127 "unsupported");
128 DiagnosticBuilder B = diag.Report(loc, id);
131 static int get_int(const char *s)
133 return s[0] == '"' ? atoi(s + 1) : atoi(s);
136 static ValueDecl *get_value_decl(Sema &sema, Token &token)
138 IdentifierInfo *name;
139 Decl *decl;
141 if (token.isNot(tok::identifier))
142 return NULL;
144 name = token.getIdentifierInfo();
145 decl = sema.LookupSingleName(sema.TUScope, name,
146 token.getLocation(), Sema::LookupOrdinaryName);
147 return decl ? cast_or_null<ValueDecl>(decl) : NULL;
150 /* Handle pragmas of the form
152 * #pragma value_bounds identifier lower_bound upper_bound
154 * For each such pragma, add a mapping
155 * { identifier[] -> [i] : lower_bound <= i <= upper_bound }
156 * to value_bounds.
158 struct PragmaValueBoundsHandler : public PragmaHandler {
159 Sema &sema;
160 isl_ctx *ctx;
161 isl_union_map *value_bounds;
163 PragmaValueBoundsHandler(isl_ctx *ctx, Sema &sema) :
164 PragmaHandler("value_bounds"), sema(sema), ctx(ctx) {
165 isl_space *space = isl_space_params_alloc(ctx, 0);
166 value_bounds = isl_union_map_empty(space);
169 ~PragmaValueBoundsHandler() {
170 isl_union_map_free(value_bounds);
173 virtual void HandlePragma(Preprocessor &PP,
174 PragmaIntroducer Introducer,
175 Token &ScopTok) {
176 isl_id *id;
177 isl_space *dim;
178 isl_map *map;
179 ValueDecl *vd;
180 Token token;
181 int lb;
182 int ub;
184 PP.Lex(token);
185 vd = get_value_decl(sema, token);
186 if (!vd) {
187 unsupported(PP, token.getLocation());
188 return;
191 PP.Lex(token);
192 if (!token.isLiteral()) {
193 unsupported(PP, token.getLocation());
194 return;
197 lb = get_int(token.getLiteralData());
199 PP.Lex(token);
200 if (!token.isLiteral()) {
201 unsupported(PP, token.getLocation());
202 return;
205 ub = get_int(token.getLiteralData());
207 dim = isl_space_alloc(ctx, 0, 0, 1);
208 map = isl_map_universe(dim);
209 map = isl_map_lower_bound_si(map, isl_dim_out, 0, lb);
210 map = isl_map_upper_bound_si(map, isl_dim_out, 0, ub);
211 id = isl_id_alloc(ctx, vd->getName().str().c_str(), vd);
212 map = isl_map_set_tuple_id(map, isl_dim_in, id);
214 value_bounds = isl_union_map_add_map(value_bounds, map);
218 /* Given a variable declaration, check if it has an integer initializer
219 * and if so, add a parameter corresponding to the variable to "value"
220 * with its value fixed to the integer initializer and return the result.
222 static __isl_give isl_set *extract_initialization(__isl_take isl_set *value,
223 ValueDecl *decl)
225 VarDecl *vd;
226 Expr *expr;
227 IntegerLiteral *il;
228 isl_val *v;
229 isl_ctx *ctx;
230 isl_id *id;
231 isl_space *space;
232 isl_set *set;
234 vd = cast<VarDecl>(decl);
235 if (!vd)
236 return value;
237 if (!vd->getType()->isIntegerType())
238 return value;
239 expr = vd->getInit();
240 if (!expr)
241 return value;
242 il = cast<IntegerLiteral>(expr);
243 if (!il)
244 return value;
246 ctx = isl_set_get_ctx(value);
247 id = isl_id_alloc(ctx, vd->getName().str().c_str(), vd);
248 space = isl_space_params_alloc(ctx, 1);
249 space = isl_space_set_dim_id(space, isl_dim_param, 0, id);
250 set = isl_set_universe(space);
252 v = PetScan::extract_int(ctx, il);
253 set = isl_set_fix_val(set, isl_dim_param, 0, v);
255 return isl_set_intersect(value, set);
258 /* Handle pragmas of the form
260 * #pragma parameter identifier lower_bound
261 * and
262 * #pragma parameter identifier lower_bound upper_bound
264 * For each such pragma, intersect the context with the set
265 * [identifier] -> { [] : lower_bound <= identifier <= upper_bound }
267 struct PragmaParameterHandler : public PragmaHandler {
268 Sema &sema;
269 isl_set *&context;
270 isl_set *&context_value;
272 PragmaParameterHandler(Sema &sema, isl_set *&context,
273 isl_set *&context_value) :
274 PragmaHandler("parameter"), sema(sema), context(context),
275 context_value(context_value) {}
277 virtual void HandlePragma(Preprocessor &PP,
278 PragmaIntroducer Introducer,
279 Token &ScopTok) {
280 isl_id *id;
281 isl_ctx *ctx = isl_set_get_ctx(context);
282 isl_space *dim;
283 isl_set *set;
284 ValueDecl *vd;
285 Token token;
286 int lb;
287 int ub;
288 bool has_ub = false;
290 PP.Lex(token);
291 vd = get_value_decl(sema, token);
292 if (!vd) {
293 unsupported(PP, token.getLocation());
294 return;
297 PP.Lex(token);
298 if (!token.isLiteral()) {
299 unsupported(PP, token.getLocation());
300 return;
303 lb = get_int(token.getLiteralData());
305 PP.Lex(token);
306 if (token.isLiteral()) {
307 has_ub = true;
308 ub = get_int(token.getLiteralData());
309 } else if (token.isNot(tok::eod)) {
310 unsupported(PP, token.getLocation());
311 return;
314 id = isl_id_alloc(ctx, vd->getName().str().c_str(), vd);
315 dim = isl_space_params_alloc(ctx, 1);
316 dim = isl_space_set_dim_id(dim, isl_dim_param, 0, id);
318 set = isl_set_universe(dim);
320 set = isl_set_lower_bound_si(set, isl_dim_param, 0, lb);
321 if (has_ub)
322 set = isl_set_upper_bound_si(set, isl_dim_param, 0, ub);
324 context = isl_set_intersect(context, set);
326 context_value = extract_initialization(context_value, vd);
330 /* Handle pragmas of the form
332 * #pragma pencil independent
334 * For each such pragma, add an entry to the "independent" vector.
336 struct PragmaPencilHandler : public PragmaHandler {
337 std::vector<Independent> &independent;
339 PragmaPencilHandler(std::vector<Independent> &independent) :
340 PragmaHandler("pencil"), independent(independent) {}
342 virtual void HandlePragma(Preprocessor &PP,
343 PragmaIntroducer Introducer,
344 Token &PencilTok) {
345 Token token;
346 IdentifierInfo *info;
348 PP.Lex(token);
349 if (token.isNot(tok::identifier))
350 return;
352 info = token.getIdentifierInfo();
353 if (!info->isStr("independent"))
354 return;
356 PP.Lex(token);
357 if (token.isNot(tok::eod))
358 return;
360 SourceManager &SM = PP.getSourceManager();
361 SourceLocation sloc = PencilTok.getLocation();
362 unsigned line = SM.getExpansionLineNumber(sloc);
363 independent.push_back(Independent(line));
367 #ifdef HAVE_TRANSLATELINECOL
369 /* Return a SourceLocation for line "line", column "col" of file "FID".
371 SourceLocation translateLineCol(SourceManager &SM, FileID FID, unsigned line,
372 unsigned col)
374 return SM.translateLineCol(FID, line, col);
377 #else
379 /* Return a SourceLocation for line "line", column "col" of file "FID".
381 SourceLocation translateLineCol(SourceManager &SM, FileID FID, unsigned line,
382 unsigned col)
384 return SM.getLocation(SM.getFileEntryForID(FID), line, col);
387 #endif
389 /* List of pairs of #pragma scop and #pragma endscop locations.
391 struct ScopLocList {
392 std::vector<ScopLoc> list;
394 /* Add a new start (#pragma scop) location to the list.
395 * If the last #pragma scop did not have a matching
396 * #pragma endscop then overwrite it.
397 * "start" points to the location of the scop pragma.
399 void add_start(SourceManager &SM, SourceLocation start) {
400 ScopLoc loc;
402 loc.scop = start;
403 int line = SM.getExpansionLineNumber(start);
404 start = translateLineCol(SM, SM.getFileID(start), line, 1);
405 loc.start_line = line;
406 loc.start = SM.getFileOffset(start);
407 if (list.size() == 0 || list[list.size() - 1].end != 0)
408 list.push_back(loc);
409 else
410 list[list.size() - 1] = loc;
413 /* Set the end location (#pragma endscop) of the last pair
414 * in the list.
415 * If there is no such pair of if the end of that pair
416 * is already set, then ignore the spurious #pragma endscop.
417 * "end" points to the location of the endscop pragma.
419 void add_end(SourceManager &SM, SourceLocation end) {
420 if (list.size() == 0 || list[list.size() - 1].end != 0)
421 return;
422 list[list.size() - 1].endscop = end;
423 int line = SM.getExpansionLineNumber(end);
424 end = translateLineCol(SM, SM.getFileID(end), line + 1, 1);
425 list[list.size() - 1].end = SM.getFileOffset(end);
429 /* Handle pragmas of the form
431 * #pragma scop
433 * In particular, store the location of the line containing
434 * the pragma in the list "scops".
436 struct PragmaScopHandler : public PragmaHandler {
437 ScopLocList &scops;
439 PragmaScopHandler(ScopLocList &scops) :
440 PragmaHandler("scop"), scops(scops) {}
442 virtual void HandlePragma(Preprocessor &PP,
443 PragmaIntroducer Introducer,
444 Token &ScopTok) {
445 SourceManager &SM = PP.getSourceManager();
446 SourceLocation sloc = ScopTok.getLocation();
447 scops.add_start(SM, sloc);
451 /* Handle pragmas of the form
453 * #pragma endscop
455 * In particular, store the location of the line following the one containing
456 * the pragma in the list "scops".
458 struct PragmaEndScopHandler : public PragmaHandler {
459 ScopLocList &scops;
461 PragmaEndScopHandler(ScopLocList &scops) :
462 PragmaHandler("endscop"), scops(scops) {}
464 virtual void HandlePragma(Preprocessor &PP,
465 PragmaIntroducer Introducer,
466 Token &EndScopTok) {
467 SourceManager &SM = PP.getSourceManager();
468 SourceLocation sloc = EndScopTok.getLocation();
469 scops.add_end(SM, sloc);
473 /* Handle pragmas of the form
475 * #pragma live-out identifier, identifier, ...
477 * Each identifier on the line is stored in live_out.
479 struct PragmaLiveOutHandler : public PragmaHandler {
480 Sema &sema;
481 set<ValueDecl *> &live_out;
483 PragmaLiveOutHandler(Sema &sema, set<ValueDecl *> &live_out) :
484 PragmaHandler("live"), sema(sema), live_out(live_out) {}
486 virtual void HandlePragma(Preprocessor &PP,
487 PragmaIntroducer Introducer,
488 Token &ScopTok) {
489 Token token;
491 PP.Lex(token);
492 if (token.isNot(tok::minus))
493 return;
494 PP.Lex(token);
495 if (token.isNot(tok::identifier) ||
496 !token.getIdentifierInfo()->isStr("out"))
497 return;
499 PP.Lex(token);
500 while (token.isNot(tok::eod)) {
501 ValueDecl *vd;
503 vd = get_value_decl(sema, token);
504 if (!vd) {
505 unsupported(PP, token.getLocation());
506 return;
508 live_out.insert(vd);
509 PP.Lex(token);
510 if (token.is(tok::comma))
511 PP.Lex(token);
516 /* For each array in "scop", set its value_bounds property
517 * based on the information in "value_bounds" and
518 * mark it as live_out if it appears in "live_out".
520 static void update_arrays(struct pet_scop *scop,
521 __isl_take isl_union_map *value_bounds, set<ValueDecl *> &live_out)
523 set<ValueDecl *>::iterator lo_it;
524 isl_ctx *ctx = isl_union_map_get_ctx(value_bounds);
526 if (!scop) {
527 isl_union_map_free(value_bounds);
528 return;
531 for (int i = 0; i < scop->n_array; ++i) {
532 isl_id *id;
533 isl_space *space;
534 isl_map *bounds;
535 ValueDecl *decl;
536 pet_array *array = scop->arrays[i];
538 id = isl_set_get_tuple_id(array->extent);
539 decl = pet_id_get_decl(id);
541 space = isl_space_alloc(ctx, 0, 0, 1);
542 space = isl_space_set_tuple_id(space, isl_dim_in, id);
544 bounds = isl_union_map_extract_map(value_bounds, space);
545 if (!isl_map_plain_is_empty(bounds))
546 array->value_bounds = isl_map_range(bounds);
547 else
548 isl_map_free(bounds);
550 lo_it = live_out.find(decl);
551 if (lo_it != live_out.end())
552 array->live_out = 1;
555 isl_union_map_free(value_bounds);
558 /* Extract a pet_scop (if any) from each appropriate function.
559 * Each detected scop is passed to "fn".
560 * When autodetecting, at most one scop is extracted from each function.
561 * If "function" is not NULL, then we only extract a pet_scop if the
562 * name of the function matches.
563 * If "autodetect" is false, then we only extract if we have seen
564 * scop and endscop pragmas and if these are situated inside the function
565 * body.
567 struct PetASTConsumer : public ASTConsumer {
568 Preprocessor &PP;
569 ASTContext &ast_context;
570 DiagnosticsEngine &diags;
571 ScopLocList &scops;
572 std::vector<Independent> independent;
573 const char *function;
574 pet_options *options;
575 isl_ctx *ctx;
576 isl_set *context;
577 isl_set *context_value;
578 set<ValueDecl *> live_out;
579 PragmaValueBoundsHandler *vb_handler;
580 isl_stat (*fn)(struct pet_scop *scop, void *user);
581 void *user;
582 bool error;
584 PetASTConsumer(isl_ctx *ctx, Preprocessor &PP, ASTContext &ast_context,
585 DiagnosticsEngine &diags, ScopLocList &scops,
586 const char *function, pet_options *options,
587 isl_stat (*fn)(struct pet_scop *scop, void *user), void *user) :
588 PP(PP), ast_context(ast_context), diags(diags),
589 scops(scops), function(function), options(options),
590 ctx(ctx),
591 vb_handler(NULL), fn(fn), user(user), error(false)
593 isl_space *space;
594 space = isl_space_params_alloc(ctx, 0);
595 context = isl_set_universe(isl_space_copy(space));
596 context_value = isl_set_universe(space);
599 ~PetASTConsumer() {
600 isl_set_free(context);
601 isl_set_free(context_value);
604 void handle_value_bounds(Sema *sema) {
605 vb_handler = new PragmaValueBoundsHandler(ctx, *sema);
606 PP.AddPragmaHandler(vb_handler);
609 /* Add all pragma handlers to this->PP.
610 * The pencil pragmas are only handled if the pencil option is set.
612 void add_pragma_handlers(Sema *sema) {
613 PP.AddPragmaHandler(new PragmaParameterHandler(*sema, context,
614 context_value));
615 if (options->pencil) {
616 PragmaHandler *PH;
617 PH = new PragmaPencilHandler(independent);
618 PP.AddPragmaHandler(PH);
620 handle_value_bounds(sema);
623 __isl_give isl_union_map *get_value_bounds() {
624 return isl_union_map_copy(vb_handler->value_bounds);
627 /* Pass "scop" to "fn" after performing some postprocessing.
628 * In particular, add the context and value_bounds constraints
629 * speficied through pragmas, add reference identifiers and
630 * reset user pointers on parameters and tuple ids.
632 * If "scop" does not contain any statements and autodetect
633 * is turned on, then skip it.
635 void call_fn(pet_scop *scop) {
636 if (!scop) {
637 error = true;
638 return;
640 if (diags.hasErrorOccurred()) {
641 error = true;
642 pet_scop_free(scop);
643 return;
645 if (options->autodetect && scop->n_stmt == 0) {
646 pet_scop_free(scop);
647 return;
649 scop->context = isl_set_intersect(scop->context,
650 isl_set_copy(context));
651 scop->context_value = isl_set_intersect(scop->context_value,
652 isl_set_copy(context_value));
654 update_arrays(scop, get_value_bounds(), live_out);
656 scop = pet_scop_add_ref_ids(scop);
657 scop = pet_scop_anonymize(scop);
659 if (fn(scop, user) < 0)
660 error = true;
663 /* For each explicitly marked scop (using pragmas),
664 * extract the scop and call "fn" on it if it is inside "fd".
666 void scan_scops(FunctionDecl *fd) {
667 unsigned start, end;
668 vector<ScopLoc>::iterator it;
669 isl_union_map *vb = vb_handler->value_bounds;
670 SourceManager &SM = PP.getSourceManager();
671 pet_scop *scop;
673 if (scops.list.size() == 0)
674 return;
676 start = SM.getFileOffset(begin_loc(fd));
677 end = SM.getFileOffset(end_loc(fd));
679 for (it = scops.list.begin(); it != scops.list.end(); ++it) {
680 ScopLoc loc = *it;
681 if (!loc.end)
682 continue;
683 if (start > loc.end)
684 continue;
685 if (end < loc.start)
686 continue;
687 PetScan ps(PP, ast_context, fd, loc, options,
688 isl_union_map_copy(vb), independent);
689 scop = ps.scan(fd);
690 call_fn(scop);
694 virtual HandleTopLevelDeclReturn HandleTopLevelDecl(DeclGroupRef dg) {
695 DeclGroupRef::iterator it;
697 if (error)
698 return HandleTopLevelDeclContinue;
700 for (it = dg.begin(); it != dg.end(); ++it) {
701 isl_union_map *vb = vb_handler->value_bounds;
702 FunctionDecl *fd = dyn_cast<clang::FunctionDecl>(*it);
703 if (!fd)
704 continue;
705 if (!fd->hasBody())
706 continue;
707 if (function &&
708 fd->getNameInfo().getAsString() != function)
709 continue;
710 if (options->autodetect) {
711 ScopLoc loc;
712 pet_scop *scop;
713 PetScan ps(PP, ast_context, fd, loc, options,
714 isl_union_map_copy(vb),
715 independent);
716 scop = ps.scan(fd);
717 if (!scop)
718 continue;
719 call_fn(scop);
720 continue;
722 scan_scops(fd);
725 return HandleTopLevelDeclContinue;
729 static const char *ResourceDir =
730 CLANG_PREFIX "/lib/clang/" CLANG_VERSION_STRING;
732 static const char *implicit_functions[] = {
733 "min", "max", "intMod", "intCeil", "intFloor", "ceild", "floord"
735 static const char *pencil_implicit_functions[] = {
736 "imin", "umin", "imax", "umax", "__pencil_kill"
739 /* Should "ident" be treated as an implicit function?
740 * If "pencil" is set, then also allow pencil specific builtins.
742 static bool is_implicit(const IdentifierInfo *ident, int pencil)
744 const char *name = ident->getNameStart();
745 for (size_t i = 0; i < ARRAY_SIZE(implicit_functions); ++i)
746 if (!strcmp(name, implicit_functions[i]))
747 return true;
748 if (!pencil)
749 return false;
750 for (size_t i = 0; i < ARRAY_SIZE(pencil_implicit_functions); ++i)
751 if (!strcmp(name, pencil_implicit_functions[i]))
752 return true;
753 return false;
756 /* Ignore implicit function declaration warnings on
757 * "min", "max", "ceild" and "floord" as we detect and handle these
758 * in PetScan.
759 * If "pencil" is set, then also ignore them on pencil specific
760 * builtins.
762 struct MyDiagnosticPrinter : public TextDiagnosticPrinter {
763 const DiagnosticOptions *DiagOpts;
764 int pencil;
765 #ifdef HAVE_BASIC_DIAGNOSTICOPTIONS_H
766 MyDiagnosticPrinter(DiagnosticOptions *DO, int pencil) :
767 TextDiagnosticPrinter(llvm::errs(), DO), pencil(pencil) {}
768 virtual DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
769 return new MyDiagnosticPrinter(&Diags.getDiagnosticOptions(),
770 pencil);
772 #else
773 MyDiagnosticPrinter(const DiagnosticOptions &DO, int pencil) :
774 DiagOpts(&DO), TextDiagnosticPrinter(llvm::errs(), DO),
775 pencil(pencil) {}
776 virtual DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
777 return new MyDiagnosticPrinter(*DiagOpts, pencil);
779 #endif
780 virtual void HandleDiagnostic(DiagnosticsEngine::Level level,
781 const DiagnosticInfo &info) {
782 if (info.getID() == diag::ext_implicit_function_decl &&
783 info.getNumArgs() >= 1 &&
784 info.getArgKind(0) == DiagnosticsEngine::ak_identifierinfo &&
785 is_implicit(info.getArgIdentifier(0), pencil))
786 /* ignore warning */;
787 else
788 TextDiagnosticPrinter::HandleDiagnostic(level, info);
792 #ifdef USE_ARRAYREF
794 #ifdef HAVE_CXXISPRODUCTION
795 static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags)
797 return new Driver(binary, llvm::sys::getDefaultTargetTriple(),
798 "", false, false, Diags);
800 #elif defined(HAVE_ISPRODUCTION)
801 static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags)
803 return new Driver(binary, llvm::sys::getDefaultTargetTriple(),
804 "", false, Diags);
806 #elif defined(DRIVER_CTOR_TAKES_DEFAULTIMAGENAME)
807 static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags)
809 return new Driver(binary, llvm::sys::getDefaultTargetTriple(),
810 "", Diags);
812 #else
813 static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags)
815 return new Driver(binary, llvm::sys::getDefaultTargetTriple(), Diags);
817 #endif
819 namespace clang { namespace driver { class Job; } }
821 /* Clang changed its API from 3.5 to 3.6 and once more in 3.7.
822 * We fix this with a simple overloaded function here.
824 struct ClangAPI {
825 static Job *command(Job *J) { return J; }
826 static Job *command(Job &J) { return &J; }
827 static Command *command(Command &C) { return &C; }
830 #ifdef CREATE_FROM_ARGS_TAKES_ARRAYREF
832 /* Call CompilerInvocation::CreateFromArgs with the right arguments.
833 * In this case, an ArrayRef<const char *>.
835 static void create_from_args(CompilerInvocation &invocation,
836 const ArgStringList *args, DiagnosticsEngine &Diags)
838 CompilerInvocation::CreateFromArgs(invocation, *args, Diags);
841 #else
843 /* Call CompilerInvocation::CreateFromArgs with the right arguments.
844 * In this case, two "const char *" pointers.
846 static void create_from_args(CompilerInvocation &invocation,
847 const ArgStringList *args, DiagnosticsEngine &Diags)
849 CompilerInvocation::CreateFromArgs(invocation, args->data() + 1,
850 args->data() + args->size(),
851 Diags);
854 #endif
856 /* Create a CompilerInvocation object that stores the command line
857 * arguments constructed by the driver.
858 * The arguments are mainly useful for setting up the system include
859 * paths on newer clangs and on some platforms.
861 static CompilerInvocation *construct_invocation(const char *filename,
862 DiagnosticsEngine &Diags)
864 const char *binary = CLANG_PREFIX"/bin/clang";
865 const unique_ptr<Driver> driver(construct_driver(binary, Diags));
866 std::vector<const char *> Argv;
867 Argv.push_back(binary);
868 Argv.push_back(filename);
869 const unique_ptr<Compilation> compilation(
870 driver->BuildCompilation(llvm::ArrayRef<const char *>(Argv)));
871 JobList &Jobs = compilation->getJobs();
872 if (Jobs.size() < 1)
873 return NULL;
875 Command *cmd = cast<Command>(ClangAPI::command(*Jobs.begin()));
876 if (strcmp(cmd->getCreator().getName(), "clang"))
877 return NULL;
879 const ArgStringList *args = &cmd->getArguments();
881 CompilerInvocation *invocation = new CompilerInvocation;
882 create_from_args(*invocation, args, Diags);
883 return invocation;
886 #else
888 static CompilerInvocation *construct_invocation(const char *filename,
889 DiagnosticsEngine &Diags)
891 return NULL;
894 #endif
896 #ifdef HAVE_BASIC_DIAGNOSTICOPTIONS_H
898 static MyDiagnosticPrinter *construct_printer(CompilerInstance *Clang,
899 int pencil)
901 return new MyDiagnosticPrinter(new DiagnosticOptions(), pencil);
904 #else
906 static MyDiagnosticPrinter *construct_printer(CompilerInstance *Clang,
907 int pencil)
909 return new MyDiagnosticPrinter(Clang->getDiagnosticOpts(), pencil);
912 #endif
914 #ifdef CREATETARGETINFO_TAKES_SHARED_PTR
916 static TargetInfo *create_target_info(CompilerInstance *Clang,
917 DiagnosticsEngine &Diags)
919 shared_ptr<TargetOptions> TO = Clang->getInvocation().TargetOpts;
920 TO->Triple = llvm::sys::getDefaultTargetTriple();
921 return TargetInfo::CreateTargetInfo(Diags, TO);
924 #elif defined(CREATETARGETINFO_TAKES_POINTER)
926 static TargetInfo *create_target_info(CompilerInstance *Clang,
927 DiagnosticsEngine &Diags)
929 TargetOptions &TO = Clang->getTargetOpts();
930 TO.Triple = llvm::sys::getDefaultTargetTriple();
931 return TargetInfo::CreateTargetInfo(Diags, &TO);
934 #else
936 static TargetInfo *create_target_info(CompilerInstance *Clang,
937 DiagnosticsEngine &Diags)
939 TargetOptions &TO = Clang->getTargetOpts();
940 TO.Triple = llvm::sys::getDefaultTargetTriple();
941 return TargetInfo::CreateTargetInfo(Diags, TO);
944 #endif
946 #ifdef CREATEDIAGNOSTICS_TAKES_ARG
948 static void create_diagnostics(CompilerInstance *Clang)
950 Clang->createDiagnostics(0, NULL);
953 #else
955 static void create_diagnostics(CompilerInstance *Clang)
957 Clang->createDiagnostics();
960 #endif
962 #ifdef CREATEPREPROCESSOR_TAKES_TUKIND
964 static void create_preprocessor(CompilerInstance *Clang)
966 Clang->createPreprocessor(TU_Complete);
969 #else
971 static void create_preprocessor(CompilerInstance *Clang)
973 Clang->createPreprocessor();
976 #endif
978 #ifdef ADDPATH_TAKES_4_ARGUMENTS
980 void add_path(HeaderSearchOptions &HSO, string Path)
982 HSO.AddPath(Path, frontend::Angled, false, false);
985 #else
987 void add_path(HeaderSearchOptions &HSO, string Path)
989 HSO.AddPath(Path, frontend::Angled, true, false, false);
992 #endif
994 #ifdef HAVE_SETMAINFILEID
996 static void create_main_file_id(SourceManager &SM, const FileEntry *file)
998 SM.setMainFileID(SM.createFileID(file, SourceLocation(),
999 SrcMgr::C_User));
1002 #else
1004 static void create_main_file_id(SourceManager &SM, const FileEntry *file)
1006 SM.createMainFileID(file);
1009 #endif
1011 #ifdef SETLANGDEFAULTS_TAKES_5_ARGUMENTS
1013 static void set_lang_defaults(CompilerInstance *Clang)
1015 PreprocessorOptions &PO = Clang->getPreprocessorOpts();
1016 TargetOptions &TO = Clang->getTargetOpts();
1017 llvm::Triple T(TO.Triple);
1018 CompilerInvocation::setLangDefaults(Clang->getLangOpts(), IK_C, T, PO,
1019 LangStandard::lang_unspecified);
1022 #else
1024 static void set_lang_defaults(CompilerInstance *Clang)
1026 CompilerInvocation::setLangDefaults(Clang->getLangOpts(), IK_C,
1027 LangStandard::lang_unspecified);
1030 #endif
1032 #ifdef SETINVOCATION_TAKES_SHARED_PTR
1034 static void set_invocation(CompilerInstance *Clang,
1035 CompilerInvocation *invocation)
1037 Clang->setInvocation(std::shared_ptr<CompilerInvocation>(invocation));
1040 #else
1042 static void set_invocation(CompilerInstance *Clang,
1043 CompilerInvocation *invocation)
1045 Clang->setInvocation(invocation);
1048 #endif
1050 /* Helper function for ignore_error that only gets enabled if T
1051 * (which is either const FileEntry * or llvm::ErrorOr<const FileEntry *>)
1052 * has getError method, i.e., if it is llvm::ErrorOr<const FileEntry *>.
1054 template <class T>
1055 static const FileEntry *ignore_error_helper(const T obj, int,
1056 int[1][sizeof(obj.getError())])
1058 return *obj;
1061 /* Helper function for ignore_error that is always enabled,
1062 * but that only gets selected if the variant above is not enabled,
1063 * i.e., if T is const FileEntry *.
1065 template <class T>
1066 static const FileEntry *ignore_error_helper(const T obj, long, void *)
1068 return obj;
1071 /* Given either a const FileEntry * or a llvm::ErrorOr<const FileEntry *>,
1072 * extract out the const FileEntry *.
1074 template <class T>
1075 static const FileEntry *ignore_error(const T obj)
1077 return ignore_error_helper(obj, 0, NULL);
1080 /* Return the FileEntry corresponding to the given file name
1081 * in the given compiler instances, ignoring any error.
1083 static const FileEntry *getFile(CompilerInstance *Clang, std::string Filename)
1085 return ignore_error(Clang->getFileManager().getFile(Filename));
1088 /* Add pet specific predefines to the preprocessor.
1089 * Currently, these are all pencil specific, so they are only
1090 * added if "pencil" is set.
1092 * We mimic the way <command line> is handled inside clang.
1094 void add_predefines(Preprocessor &PP, int pencil)
1096 string s;
1098 if (!pencil)
1099 return;
1101 s = PP.getPredefines();
1102 s += "# 1 \"<pet>\" 1\n"
1103 "void __pencil_assume(int assumption);\n"
1104 "#define pencil_access(f) annotate(\"pencil_access(\" #f \")\")\n"
1105 "# 1 \"<built-in>\" 2\n";
1106 PP.setPredefines(s);
1109 /* Extract a pet_scop from each function in the C source file called "filename".
1110 * Each detected scop is passed to "fn".
1111 * If "function" is not NULL, only extract a pet_scop from the function
1112 * with that name.
1113 * If "autodetect" is set, extract any pet_scop we can find.
1114 * Otherwise, extract the pet_scop from the region delimited
1115 * by "scop" and "endscop" pragmas.
1117 * We first set up the clang parser and then try to extract the
1118 * pet_scop from the appropriate function(s) in PetASTConsumer.
1120 static isl_stat foreach_scop_in_C_source(isl_ctx *ctx,
1121 const char *filename, const char *function, pet_options *options,
1122 isl_stat (*fn)(struct pet_scop *scop, void *user), void *user)
1124 CompilerInstance *Clang = new CompilerInstance();
1125 create_diagnostics(Clang);
1126 DiagnosticsEngine &Diags = Clang->getDiagnostics();
1127 Diags.setSuppressSystemWarnings(true);
1128 CompilerInvocation *invocation = construct_invocation(filename, Diags);
1129 if (invocation)
1130 set_invocation(Clang, invocation);
1131 Diags.setClient(construct_printer(Clang, options->pencil));
1132 Clang->createFileManager();
1133 Clang->createSourceManager(Clang->getFileManager());
1134 TargetInfo *target = create_target_info(Clang, Diags);
1135 Clang->setTarget(target);
1136 set_lang_defaults(Clang);
1137 HeaderSearchOptions &HSO = Clang->getHeaderSearchOpts();
1138 HSO.ResourceDir = ResourceDir;
1139 for (int i = 0; i < options->n_path; ++i)
1140 add_path(HSO, options->paths[i]);
1141 PreprocessorOptions &PO = Clang->getPreprocessorOpts();
1142 for (int i = 0; i < options->n_define; ++i)
1143 PO.addMacroDef(options->defines[i]);
1144 create_preprocessor(Clang);
1145 Preprocessor &PP = Clang->getPreprocessor();
1146 add_predefines(PP, options->pencil);
1147 PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
1148 PP.getLangOpts());
1150 ScopLocList scops;
1152 const FileEntry *file = getFile(Clang, filename);
1153 if (!file)
1154 isl_die(ctx, isl_error_unknown, "unable to open file",
1155 do { delete Clang; return isl_stat_error; } while (0));
1156 create_main_file_id(Clang->getSourceManager(), file);
1158 Clang->createASTContext();
1159 PetASTConsumer consumer(ctx, PP, Clang->getASTContext(), Diags,
1160 scops, function, options, fn, user);
1161 Sema *sema = new Sema(PP, Clang->getASTContext(), consumer);
1163 if (!options->autodetect) {
1164 PP.AddPragmaHandler(new PragmaScopHandler(scops));
1165 PP.AddPragmaHandler(new PragmaEndScopHandler(scops));
1166 PP.AddPragmaHandler(new PragmaLiveOutHandler(*sema,
1167 consumer.live_out));
1170 consumer.add_pragma_handlers(sema);
1172 Diags.getClient()->BeginSourceFile(Clang->getLangOpts(), &PP);
1173 ParseAST(*sema);
1174 Diags.getClient()->EndSourceFile();
1176 delete sema;
1177 delete Clang;
1179 return consumer.error ? isl_stat_error : isl_stat_ok;
1182 /* Extract a pet_scop from each function in the C source file called "filename".
1183 * Each detected scop is passed to "fn".
1185 * This wrapper around foreach_scop_in_C_source is mainly used to ensure
1186 * that all objects on the stack (of that function) are destroyed before we
1187 * call llvm_shutdown.
1189 static isl_stat pet_foreach_scop_in_C_source(isl_ctx *ctx,
1190 const char *filename, const char *function,
1191 isl_stat (*fn)(struct pet_scop *scop, void *user), void *user)
1193 isl_stat r;
1194 pet_options *options;
1195 bool allocated = false;
1197 options = isl_ctx_peek_pet_options(ctx);
1198 if (!options) {
1199 options = pet_options_new_with_defaults();
1200 allocated = true;
1203 r = foreach_scop_in_C_source(ctx, filename, function, options,
1204 fn, user);
1205 llvm::llvm_shutdown();
1207 if (allocated)
1208 pet_options_free(options);
1210 return r;
1213 /* Store "scop" into the address pointed to by "user".
1214 * Return -1 to indicate that we are not interested in any further scops.
1215 * This function should therefore not be called a second call
1216 * so in principle there is no need to check if we have already set *user.
1218 static isl_stat set_first_scop(pet_scop *scop, void *user)
1220 pet_scop **p = (pet_scop **) user;
1222 if (!*p)
1223 *p = scop;
1224 else
1225 pet_scop_free(scop);
1227 return isl_stat_error;
1230 /* Extract a pet_scop from the C source file called "filename".
1231 * If "function" is not NULL, extract the pet_scop from the function
1232 * with that name.
1234 * We start extracting scops from every function and then abort
1235 * as soon as we have extracted one scop.
1237 struct pet_scop *pet_scop_extract_from_C_source(isl_ctx *ctx,
1238 const char *filename, const char *function)
1240 pet_scop *scop = NULL;
1242 pet_foreach_scop_in_C_source(ctx, filename, function,
1243 &set_first_scop, &scop);
1245 return scop;
1248 /* Internal data structure for pet_transform_C_source
1250 * transform is the function that should be called to print a scop
1251 * in is the input source file
1252 * out is the output source file
1253 * end is the offset of the end of the previous scop (zero if we have not
1254 * found any scop yet)
1255 * p is a printer that prints to out.
1257 struct pet_transform_data {
1258 __isl_give isl_printer *(*transform)(__isl_take isl_printer *p,
1259 struct pet_scop *scop, void *user);
1260 void *user;
1262 FILE *in;
1263 FILE *out;
1264 unsigned end;
1265 isl_printer *p;
1268 /* This function is called each time a scop is detected.
1270 * We first copy the input text code from the end of the previous scop
1271 * until the start of "scop" and then print the scop itself through
1272 * a call to data->transform. We set up the printer to print
1273 * the transformed code with the same (initial) indentation as
1274 * the original code.
1275 * Finally, we keep track of the end of "scop" so that we can
1276 * continue copying when we find the next scop.
1278 * Before calling data->transform, we store a pointer to the original
1279 * input file in the extended scop in case the user wants to call
1280 * pet_scop_print_original from the callback.
1282 static isl_stat pet_transform(struct pet_scop *scop, void *user)
1284 struct pet_transform_data *data = (struct pet_transform_data *) user;
1285 unsigned start;
1287 if (!scop)
1288 return isl_stat_error;
1289 start = pet_loc_get_start(scop->loc);
1290 if (copy(data->in, data->out, data->end, start) < 0)
1291 goto error;
1292 data->end = pet_loc_get_end(scop->loc);
1293 scop = pet_scop_set_input_file(scop, data->in);
1294 data->p = isl_printer_set_indent_prefix(data->p,
1295 pet_loc_get_indent(scop->loc));
1296 data->p = data->transform(data->p, scop, data->user);
1297 if (!data->p)
1298 return isl_stat_error;
1299 return isl_stat_ok;
1300 error:
1301 pet_scop_free(scop);
1302 return isl_stat_error;
1305 /* Transform the C source file "input" by rewriting each scop
1306 * through a call to "transform".
1307 * When autodetecting scops, at most one scop per function is rewritten.
1308 * The transformed C code is written to "output".
1310 * For each scop we find, we first copy the input text code
1311 * from the end of the previous scop (or the beginning of the file
1312 * in case of the first scop) until the start of the scop
1313 * and then print the scop itself through a call to "transform".
1314 * At the end we copy everything from the end of the final scop
1315 * until the end of the input file to "output".
1317 int pet_transform_C_source(isl_ctx *ctx, const char *input, FILE *out,
1318 __isl_give isl_printer *(*transform)(__isl_take isl_printer *p,
1319 struct pet_scop *scop, void *user), void *user)
1321 struct pet_transform_data data;
1322 int r;
1324 data.in = stdin;
1325 data.out = out;
1326 if (input && strcmp(input, "-")) {
1327 data.in = fopen(input, "r");
1328 if (!data.in)
1329 isl_die(ctx, isl_error_unknown, "unable to open file",
1330 return -1);
1333 data.p = isl_printer_to_file(ctx, data.out);
1334 data.p = isl_printer_set_output_format(data.p, ISL_FORMAT_C);
1336 data.transform = transform;
1337 data.user = user;
1338 data.end = 0;
1339 r = pet_foreach_scop_in_C_source(ctx, input, NULL,
1340 &pet_transform, &data);
1342 isl_printer_free(data.p);
1343 if (!data.p)
1344 r = -1;
1345 if (r == 0 && copy(data.in, data.out, data.end, -1) < 0)
1346 r = -1;
1348 if (data.in != stdin)
1349 fclose(data.in);
1351 return r;