privately export pet_expr_foreach_access
[pet.git] / pet.cc
blob0368bd226549f20da8a82cb23cffac618d8f8dc9
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials provided
14 * with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * The views and conclusions contained in the software and documentation
29 * are those of the authors and should not be interpreted as
30 * representing official policies, either expressed or implied, of
31 * Leiden University.
32 */
34 #include <stdlib.h>
35 #include <map>
36 #include <iostream>
37 #include <llvm/Support/raw_ostream.h>
38 #include <llvm/Support/ManagedStatic.h>
39 #include <llvm/Support/Host.h>
40 #include <clang/Basic/Version.h>
41 #include <clang/Basic/FileSystemOptions.h>
42 #include <clang/Basic/FileManager.h>
43 #include <clang/Basic/TargetOptions.h>
44 #include <clang/Basic/TargetInfo.h>
45 #include <clang/Driver/Compilation.h>
46 #include <clang/Driver/Driver.h>
47 #include <clang/Driver/Tool.h>
48 #include <clang/Frontend/CompilerInstance.h>
49 #include <clang/Frontend/CompilerInvocation.h>
50 #include <clang/Frontend/DiagnosticOptions.h>
51 #include <clang/Frontend/TextDiagnosticPrinter.h>
52 #include <clang/Frontend/HeaderSearchOptions.h>
53 #include <clang/Frontend/LangStandard.h>
54 #include <clang/Frontend/PreprocessorOptions.h>
55 #include <clang/Frontend/FrontendOptions.h>
56 #include <clang/Frontend/Utils.h>
57 #include <clang/Lex/HeaderSearch.h>
58 #include <clang/Lex/Preprocessor.h>
59 #include <clang/Lex/Pragma.h>
60 #include <clang/AST/ASTContext.h>
61 #include <clang/AST/ASTConsumer.h>
62 #include <clang/Sema/Sema.h>
63 #include <clang/Sema/SemaDiagnostic.h>
64 #include <clang/Parse/Parser.h>
65 #include <clang/Parse/ParseAST.h>
67 #include <isl/ctx.h>
68 #include <isl/constraint.h>
70 #include "options.h"
71 #include "scan.h"
73 #include "config.h"
75 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
77 using namespace std;
78 using namespace clang;
79 using namespace clang::driver;
81 /* Called if we found something we didn't expect in one of the pragmas.
82 * We'll provide more informative warnings later.
84 static void unsupported(Preprocessor &PP, SourceLocation loc)
86 DiagnosticsEngine &diag = PP.getDiagnostics();
87 unsigned id = diag.getCustomDiagID(DiagnosticsEngine::Warning,
88 "unsupported");
89 DiagnosticBuilder B = diag.Report(loc, id);
92 static int get_int(const char *s)
94 return s[0] == '"' ? atoi(s + 1) : atoi(s);
97 static ValueDecl *get_value_decl(Sema &sema, Token &token)
99 IdentifierInfo *name;
100 Decl *decl;
102 if (token.isNot(tok::identifier))
103 return NULL;
105 name = token.getIdentifierInfo();
106 decl = sema.LookupSingleName(sema.TUScope, name,
107 token.getLocation(), Sema::LookupOrdinaryName);
108 return decl ? cast_or_null<ValueDecl>(decl) : NULL;
111 /* Handle pragmas of the form
113 * #pragma value_bounds identifier lower_bound upper_bound
115 * For each such pragma, add a mapping from the ValueDecl corresponding
116 * to "identifier" to a set { [i] : lower_bound <= i <= upper_bound }
117 * to the map value_bounds.
119 struct PragmaValueBoundsHandler : public PragmaHandler {
120 Sema &sema;
121 isl_ctx *ctx;
122 map<ValueDecl *, isl_set *> &value_bounds;
124 PragmaValueBoundsHandler(isl_ctx *ctx, Sema &sema,
125 map<ValueDecl *, isl_set *> &value_bounds) :
126 PragmaHandler("value_bounds"), ctx(ctx), sema(sema),
127 value_bounds(value_bounds) {}
129 virtual void HandlePragma(Preprocessor &PP,
130 PragmaIntroducerKind Introducer,
131 Token &ScopTok) {
132 isl_space *dim;
133 isl_set *set;
134 ValueDecl *vd;
135 Token token;
136 int lb;
137 int ub;
139 PP.Lex(token);
140 vd = get_value_decl(sema, token);
141 if (!vd) {
142 unsupported(PP, token.getLocation());
143 return;
146 PP.Lex(token);
147 if (!token.isLiteral()) {
148 unsupported(PP, token.getLocation());
149 return;
152 lb = get_int(token.getLiteralData());
154 PP.Lex(token);
155 if (!token.isLiteral()) {
156 unsupported(PP, token.getLocation());
157 return;
160 ub = get_int(token.getLiteralData());
162 dim = isl_space_set_alloc(ctx, 0, 1);
163 set = isl_set_universe(dim);
164 set = isl_set_lower_bound_si(set, isl_dim_set, 0, lb);
165 set = isl_set_upper_bound_si(set, isl_dim_set, 0, ub);
167 value_bounds[vd] = set;
171 /* Given a variable declaration, check if it has an integer initializer
172 * and if so, add a parameter corresponding to the variable to "value"
173 * with its value fixed to the integer initializer and return the result.
175 static __isl_give isl_set *extract_initialization(__isl_take isl_set *value,
176 ValueDecl *decl)
178 VarDecl *vd;
179 Expr *expr;
180 IntegerLiteral *il;
181 isl_int v;
182 isl_ctx *ctx;
183 isl_id *id;
184 isl_space *space;
185 isl_set *set;
187 vd = cast<VarDecl>(decl);
188 if (!vd)
189 return value;
190 if (!vd->getType()->isIntegerType())
191 return value;
192 expr = vd->getInit();
193 if (!expr)
194 return value;
195 il = cast<IntegerLiteral>(expr);
196 if (!il)
197 return value;
199 ctx = isl_set_get_ctx(value);
200 id = isl_id_alloc(ctx, vd->getName().str().c_str(), vd);
201 space = isl_space_params_alloc(ctx, 1);
202 space = isl_space_set_dim_id(space, isl_dim_param, 0, id);
203 set = isl_set_universe(space);
205 isl_int_init(v);
206 PetScan::extract_int(il, &v);
207 set = isl_set_fix(set, isl_dim_param, 0, v);
208 isl_int_clear(v);
210 return isl_set_intersect(value, set);
213 /* Handle pragmas of the form
215 * #pragma parameter identifier lower_bound
216 * and
217 * #pragma parameter identifier lower_bound upper_bound
219 * For each such pragma, intersect the context with the set
220 * [identifier] -> { [] : lower_bound <= identifier <= upper_bound }
222 struct PragmaParameterHandler : public PragmaHandler {
223 Sema &sema;
224 isl_set *&context;
225 isl_set *&context_value;
227 PragmaParameterHandler(Sema &sema, isl_set *&context,
228 isl_set *&context_value) :
229 PragmaHandler("parameter"), sema(sema), context(context),
230 context_value(context_value) {}
232 virtual void HandlePragma(Preprocessor &PP,
233 PragmaIntroducerKind Introducer,
234 Token &ScopTok) {
235 isl_id *id;
236 isl_ctx *ctx = isl_set_get_ctx(context);
237 isl_space *dim;
238 isl_set *set;
239 ValueDecl *vd;
240 Token token;
241 int lb;
242 int ub;
243 bool has_ub = false;
245 PP.Lex(token);
246 vd = get_value_decl(sema, token);
247 if (!vd) {
248 unsupported(PP, token.getLocation());
249 return;
252 PP.Lex(token);
253 if (!token.isLiteral()) {
254 unsupported(PP, token.getLocation());
255 return;
258 lb = get_int(token.getLiteralData());
260 PP.Lex(token);
261 if (token.isLiteral()) {
262 has_ub = true;
263 ub = get_int(token.getLiteralData());
264 } else if (token.isNot(tok::eod)) {
265 unsupported(PP, token.getLocation());
266 return;
269 id = isl_id_alloc(ctx, vd->getName().str().c_str(), vd);
270 dim = isl_space_params_alloc(ctx, 1);
271 dim = isl_space_set_dim_id(dim, isl_dim_param, 0, id);
273 set = isl_set_universe(dim);
275 set = isl_set_lower_bound_si(set, isl_dim_param, 0, lb);
276 if (has_ub)
277 set = isl_set_upper_bound_si(set, isl_dim_param, 0, ub);
279 context = isl_set_intersect(context, set);
281 context_value = extract_initialization(context_value, vd);
285 /* Handle pragmas of the form
287 * #pragma scop
289 * In particular, store the current location in loc.start.
291 struct PragmaScopHandler : public PragmaHandler {
292 ScopLoc &loc;
294 PragmaScopHandler(ScopLoc &loc) : PragmaHandler("scop"), loc(loc) {}
296 virtual void HandlePragma(Preprocessor &PP,
297 PragmaIntroducerKind Introducer,
298 Token &ScopTok) {
299 SourceManager &SM = PP.getSourceManager();
300 loc.start = SM.getFileOffset(ScopTok.getLocation());
304 /* Handle pragmas of the form
306 * #pragma endscop
308 * In particular, store the current location in loc.end.
310 struct PragmaEndScopHandler : public PragmaHandler {
311 ScopLoc &loc;
313 PragmaEndScopHandler(ScopLoc &loc) :
314 PragmaHandler("endscop"), loc(loc) {}
316 virtual void HandlePragma(Preprocessor &PP,
317 PragmaIntroducerKind Introducer,
318 Token &EndScopTok) {
319 SourceManager &SM = PP.getSourceManager();
320 loc.end = SM.getFileOffset(EndScopTok.getLocation());
324 /* Handle pragmas of the form
326 * #pragma live-out identifier, identifier, ...
328 * Each identifier on the line is stored in live_out.
330 struct PragmaLiveOutHandler : public PragmaHandler {
331 Sema &sema;
332 set<ValueDecl *> &live_out;
334 PragmaLiveOutHandler(Sema &sema, set<ValueDecl *> &live_out) :
335 PragmaHandler("live"), sema(sema), live_out(live_out) {}
337 virtual void HandlePragma(Preprocessor &PP,
338 PragmaIntroducerKind Introducer,
339 Token &ScopTok) {
340 Token token;
342 PP.Lex(token);
343 if (token.isNot(tok::minus))
344 return;
345 PP.Lex(token);
346 if (token.isNot(tok::identifier) ||
347 !token.getIdentifierInfo()->isStr("out"))
348 return;
350 PP.Lex(token);
351 while (token.isNot(tok::eod)) {
352 ValueDecl *vd;
354 vd = get_value_decl(sema, token);
355 if (!vd) {
356 unsupported(PP, token.getLocation());
357 return;
359 live_out.insert(vd);
360 PP.Lex(token);
361 if (token.is(tok::comma))
362 PP.Lex(token);
367 /* Extract a pet_scop from the appropriate function.
368 * If "function" is not NULL, then we only extract a pet_scop if the
369 * name of the function matches.
370 * If "autodetect" is false, then we only extract if we have seen
371 * scop and endscop pragmas and if these are situated inside the function
372 * body.
374 struct PetASTConsumer : public ASTConsumer {
375 Preprocessor &PP;
376 ASTContext &ast_context;
377 ScopLoc &loc;
378 const char *function;
379 bool autodetect;
380 isl_ctx *ctx;
381 struct pet_scop *scop;
383 PetASTConsumer(isl_ctx *ctx, Preprocessor &PP, ASTContext &ast_context,
384 ScopLoc &loc, const char *function, bool autodetect) :
385 ctx(ctx), PP(PP), ast_context(ast_context), loc(loc),
386 scop(NULL), function(function), autodetect(autodetect) { }
388 virtual void HandleTopLevelDecl(DeclGroupRef dg) {
389 DeclGroupRef::iterator it;
391 if (scop)
392 return;
393 for (it = dg.begin(); it != dg.end(); ++it) {
394 FunctionDecl *fd = dyn_cast<clang::FunctionDecl>(*it);
395 if (!fd)
396 continue;
397 if (!fd->hasBody())
398 continue;
399 if (function &&
400 fd->getNameInfo().getAsString() != function)
401 continue;
402 if (autodetect) {
403 PetScan ps(ctx, PP, ast_context, loc, 1);
404 scop = ps.scan(fd);
405 if (scop)
406 break;
407 else
408 continue;
410 if (!loc.end)
411 continue;
412 SourceManager &SM = PP.getSourceManager();
413 if (SM.getFileOffset(fd->getLocStart()) > loc.end)
414 continue;
415 if (SM.getFileOffset(fd->getLocEnd()) < loc.start)
416 continue;
417 PetScan ps(ctx, PP, ast_context, loc, 0);
418 scop = ps.scan(fd);
419 break;
424 static const char *ResourceDir = CLANG_PREFIX"/lib/clang/"CLANG_VERSION_STRING;
426 static const char *implicit_functions[] = {
427 "min", "max", "ceild", "floord"
430 static bool is_implicit(const IdentifierInfo *ident)
432 const char *name = ident->getNameStart();
433 for (int i = 0; i < ARRAY_SIZE(implicit_functions); ++i)
434 if (!strcmp(name, implicit_functions[i]))
435 return true;
436 return false;
439 /* Ignore implicit function declaration warnings on
440 * "min", "max", "ceild" and "floord" as we detect and handle these
441 * in PetScan.
443 * The cloned field keeps track of whether the clone method
444 * has ever been called. Newer clangs (by default) clone
445 * the DiagnosticConsumer passed to createDiagnostics and
446 * then take ownership of the clone, which means that
447 * the original has to be deleted by the calling code.
449 struct MyDiagnosticPrinter : public TextDiagnosticPrinter {
450 const DiagnosticOptions *DiagOpts;
451 static bool cloned;
452 MyDiagnosticPrinter(const DiagnosticOptions &DO) :
453 DiagOpts(&DO), TextDiagnosticPrinter(llvm::errs(), DO) {}
454 virtual DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
455 cloned = true;
456 return new MyDiagnosticPrinter(*DiagOpts);
458 virtual void HandleDiagnostic(DiagnosticsEngine::Level level,
459 const DiagnosticInfo &info) {
460 if (info.getID() == diag::ext_implicit_function_decl &&
461 info.getNumArgs() == 1 &&
462 info.getArgKind(0) == DiagnosticsEngine::ak_identifierinfo &&
463 is_implicit(info.getArgIdentifier(0)))
464 /* ignore warning */;
465 else
466 TextDiagnosticPrinter::HandleDiagnostic(level, info);
470 bool MyDiagnosticPrinter::cloned = false;
472 static void update_arrays(struct pet_scop *scop,
473 map<ValueDecl *, isl_set *> &value_bounds,
474 set<ValueDecl *> &live_out)
476 map<ValueDecl *, isl_set *>::iterator vb_it;
477 set<ValueDecl *>::iterator lo_it;
479 if (!scop)
480 return;
482 for (int i = 0; i < scop->n_array; ++i) {
483 isl_id *id;
484 ValueDecl *decl;
485 pet_array *array = scop->arrays[i];
487 id = isl_set_get_tuple_id(array->extent);
488 decl = (ValueDecl *)isl_id_get_user(id);
489 isl_id_free(id);
491 vb_it = value_bounds.find(decl);
492 if (vb_it != value_bounds.end())
493 array->value_bounds = isl_set_copy(vb_it->second);
495 lo_it = live_out.find(decl);
496 if (lo_it != live_out.end())
497 array->live_out = 1;
501 #ifdef USE_ARRAYREF
503 #ifdef HAVE_CXXISPRODUCTION
504 static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags)
506 return new Driver(binary, llvm::sys::getDefaultTargetTriple(),
507 "", false, false, Diags);
509 #else
510 static Driver *construct_driver(const char *binary, DiagnosticsEngine &Diags)
512 return new Driver(binary, llvm::sys::getDefaultTargetTriple(),
513 "", false, Diags);
515 #endif
517 /* Create a CompilerInvocation object that stores the command line
518 * arguments constructed by the driver.
519 * The arguments are mainly useful for setting up the system include
520 * paths on newer clangs and on some platforms.
522 static CompilerInvocation *construct_invocation(const char *filename,
523 DiagnosticsEngine &Diags)
525 const char *binary = CLANG_PREFIX"/bin/clang";
526 const llvm::OwningPtr<Driver> driver(construct_driver(binary, Diags));
527 std::vector<const char *> Argv;
528 Argv.push_back(binary);
529 Argv.push_back(filename);
530 const llvm::OwningPtr<Compilation> compilation(
531 driver->BuildCompilation(ArrayRef<const char *>(Argv)));
532 JobList &Jobs = compilation->getJobs();
534 Command *cmd = cast<Command>(*Jobs.begin());
535 if (strcmp(cmd->getCreator().getName(), "clang"))
536 return NULL;
538 const ArgStringList *args = &cmd->getArguments();
540 CompilerInvocation *invocation = new CompilerInvocation;
541 CompilerInvocation::CreateFromArgs(*invocation, args->data() + 1,
542 args->data() + args->size(),
543 Diags);
544 return invocation;
547 #else
549 static CompilerInvocation *construct_invocation(const char *filename,
550 DiagnosticsEngine &Diags)
552 return NULL;
555 #endif
557 /* Extract a pet_scop from the C source file called "filename".
558 * If "function" is not NULL, extract the pet_scop from the function
559 * with that name.
560 * If "autodetect" is set, extract any pet_scop we can find.
561 * Otherwise, extract the pet_scop from the region delimited
562 * by "scop" and "endscop" pragmas.
564 * We first set up the clang parser and then try to extract the
565 * pet_scop from the appropriate function in PetASTConsumer.
566 * If we have found a pet_scop, we add the context and value_bounds
567 * constraints specified through pragmas.
569 static struct pet_scop *scop_extract_from_C_source(isl_ctx *ctx,
570 const char *filename, const char *function, pet_options *options)
572 isl_space *dim;
573 isl_set *context;
574 isl_set *context_value;
575 set<ValueDecl *> live_out;
576 map<ValueDecl *, isl_set *> value_bounds;
577 map<ValueDecl *, isl_set *>::iterator vb_it;
579 CompilerInstance *Clang = new CompilerInstance();
580 DiagnosticOptions DO;
581 MyDiagnosticPrinter *printer = new MyDiagnosticPrinter(DO);
582 Clang->createDiagnostics(0, NULL, printer);
583 if (printer->cloned)
584 delete printer;
585 DiagnosticsEngine &Diags = Clang->getDiagnostics();
586 Diags.setSuppressSystemWarnings(true);
587 CompilerInvocation *invocation = construct_invocation(filename, Diags);
588 if (invocation)
589 Clang->setInvocation(invocation);
590 Clang->createFileManager();
591 Clang->createSourceManager(Clang->getFileManager());
592 TargetOptions TO;
593 TO.Triple = llvm::sys::getDefaultTargetTriple();
594 TargetInfo *target = TargetInfo::CreateTargetInfo(Diags, TO);
595 Clang->setTarget(target);
596 CompilerInvocation::setLangDefaults(Clang->getLangOpts(), IK_C,
597 LangStandard::lang_unspecified);
598 HeaderSearchOptions &HSO = Clang->getHeaderSearchOpts();
599 HSO.ResourceDir = ResourceDir;
600 for (int i = 0; i < options->n_path; ++i)
601 HSO.AddPath(options->paths[i],
602 frontend::Angled, true, false, false);
603 PreprocessorOptions &PO = Clang->getPreprocessorOpts();
604 for (int i = 0; i < options->n_define; ++i)
605 PO.addMacroDef(options->defines[i]);
606 Clang->createPreprocessor();
607 Preprocessor &PP = Clang->getPreprocessor();
609 ScopLoc loc;
611 const FileEntry *file = Clang->getFileManager().getFile(filename);
612 if (!file)
613 isl_die(ctx, isl_error_unknown, "unable to open file",
614 return NULL);
615 Clang->getSourceManager().createMainFileID(file);
617 Clang->createASTContext();
618 PetASTConsumer consumer(ctx, PP, Clang->getASTContext(),
619 loc, function, options->autodetect);
620 Sema *sema = new Sema(PP, Clang->getASTContext(), consumer);
622 if (!options->autodetect) {
623 PP.AddPragmaHandler(new PragmaScopHandler(loc));
624 PP.AddPragmaHandler(new PragmaEndScopHandler(loc));
625 PP.AddPragmaHandler(new PragmaLiveOutHandler(*sema, live_out));
628 dim = isl_space_params_alloc(ctx, 0);
629 context = isl_set_universe(isl_space_copy(dim));
630 context_value = isl_set_universe(dim);
631 PP.AddPragmaHandler(new PragmaParameterHandler(*sema, context,
632 context_value));
633 PP.AddPragmaHandler(new PragmaValueBoundsHandler(ctx, *sema, value_bounds));
635 Diags.getClient()->BeginSourceFile(Clang->getLangOpts(), &PP);
636 ParseAST(*sema);
637 Diags.getClient()->EndSourceFile();
639 delete sema;
640 delete Clang;
641 llvm::llvm_shutdown();
643 if (consumer.scop) {
644 consumer.scop->context = isl_set_intersect(context,
645 consumer.scop->context);
646 consumer.scop->context_value = isl_set_intersect(context_value,
647 consumer.scop->context_value);
648 } else {
649 isl_set_free(context);
650 isl_set_free(context_value);
653 update_arrays(consumer.scop, value_bounds, live_out);
655 for (vb_it = value_bounds.begin(); vb_it != value_bounds.end(); vb_it++)
656 isl_set_free(vb_it->second);
658 return consumer.scop;
661 struct pet_scop *pet_scop_extract_from_C_source(isl_ctx *ctx,
662 const char *filename, const char *function)
664 pet_scop *scop;
665 pet_options *options;
666 bool allocated = false;
668 options = isl_ctx_peek_pet_options(ctx);
669 if (!options) {
670 options = pet_options_new_with_defaults();
671 allocated = true;
674 scop = scop_extract_from_C_source(ctx, filename, function, options);
676 if (allocated)
677 pet_options_free(options);
679 return scop;