use isl_space_params_alloc
[pet.git] / pet.cc
blobd2421c4d335699a7571989a2fd5dcfb333678674
1 #include <stdlib.h>
2 #include <map>
3 #include <iostream>
4 #include <llvm/Support/raw_ostream.h>
5 #include <llvm/Support/ManagedStatic.h>
6 #include <llvm/Support/Host.h>
7 #include <clang/Basic/Version.h>
8 #include <clang/Basic/FileSystemOptions.h>
9 #include <clang/Basic/FileManager.h>
10 #include <clang/Basic/TargetOptions.h>
11 #include <clang/Basic/TargetInfo.h>
12 #include <clang/Frontend/CompilerInvocation.h>
13 #include <clang/Frontend/DiagnosticOptions.h>
14 #include <clang/Frontend/TextDiagnosticPrinter.h>
15 #include <clang/Frontend/HeaderSearchOptions.h>
16 #include <clang/Frontend/LangStandard.h>
17 #include <clang/Frontend/PreprocessorOptions.h>
18 #include <clang/Frontend/FrontendOptions.h>
19 #include <clang/Frontend/Utils.h>
20 #include <clang/Lex/HeaderSearch.h>
21 #include <clang/Lex/Preprocessor.h>
22 #include <clang/Lex/Pragma.h>
23 #include <clang/AST/ASTContext.h>
24 #include <clang/AST/ASTConsumer.h>
25 #include <clang/Sema/Sema.h>
26 #include <clang/Sema/SemaDiagnostic.h>
27 #include <clang/Parse/Parser.h>
28 #include <clang/Parse/ParseAST.h>
30 #include <isl/ctx.h>
31 #include <isl/constraint.h>
33 #include "scan.h"
35 #include "config.h"
37 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
39 using namespace std;
40 using namespace clang;
42 /* Called if we found something we didn't expect in one of the pragmas.
43 * We'll provide more informative warnings later.
45 static void unsupported(Preprocessor &PP, SourceLocation loc)
47 Diagnostic &diag = PP.getDiagnostics();
48 unsigned id = diag.getCustomDiagID(Diagnostic::Warning, "unsupported");
49 DiagnosticBuilder B = diag.Report(loc, id);
52 /* Set the lower and upper bounds on the given dimension of "set"
53 * to "lb" and "ub".
55 static __isl_give isl_set *set_bounds(__isl_take isl_set *set,
56 enum isl_dim_type type, int pos, int lb, int ub)
58 isl_constraint *c;
60 c = isl_inequality_alloc(isl_set_get_space(set));
61 isl_constraint_set_coefficient_si(c, type, pos, 1);
62 isl_constraint_set_constant_si(c, -lb);
63 set = isl_set_add_constraint(set, c);
65 c = isl_inequality_alloc(isl_set_get_space(set));
66 isl_constraint_set_coefficient_si(c, type, pos, -1);
67 isl_constraint_set_constant_si(c, ub);
68 set = isl_set_add_constraint(set, c);
70 return set;
73 static int get_int(const char *s)
75 return s[0] == '"' ? atoi(s + 1) : atoi(s);
78 static ValueDecl *get_value_decl(Sema &sema, Token &token)
80 IdentifierInfo *name;
81 Decl *decl;
83 if (token.isNot(tok::identifier))
84 return NULL;
86 name = token.getIdentifierInfo();
87 decl = sema.LookupSingleName(sema.TUScope, name,
88 token.getLocation(), Sema::LookupOrdinaryName);
89 return decl ? cast_or_null<ValueDecl>(decl) : NULL;
92 /* Handle pragmas of the form
94 * #pragma value_bounds identifier lower_bound upper_bound
96 * For each such pragma, add a mapping from the ValueDecl corresponding
97 * to "identifier" to a set { [i] : lower_bound <= i <= upper_bound }
98 * to the map value_bounds.
100 struct PragmaValueBoundsHandler : public PragmaHandler {
101 Sema &sema;
102 isl_ctx *ctx;
103 map<ValueDecl *, isl_set *> &value_bounds;
105 PragmaValueBoundsHandler(isl_ctx *ctx, Sema &sema,
106 map<ValueDecl *, isl_set *> &value_bounds) :
107 PragmaHandler("value_bounds"), ctx(ctx), sema(sema),
108 value_bounds(value_bounds) {}
110 virtual void HandlePragma(Preprocessor &PP,
111 PragmaIntroducerKind Introducer,
112 Token &ScopTok) {
113 isl_space *dim;
114 isl_set *set;
115 ValueDecl *vd;
116 Token token;
117 int lb;
118 int ub;
120 PP.Lex(token);
121 vd = get_value_decl(sema, token);
122 if (!vd) {
123 unsupported(PP, token.getLocation());
124 return;
127 PP.Lex(token);
128 if (!token.isLiteral()) {
129 unsupported(PP, token.getLocation());
130 return;
133 lb = get_int(token.getLiteralData());
135 PP.Lex(token);
136 if (!token.isLiteral()) {
137 unsupported(PP, token.getLocation());
138 return;
141 ub = get_int(token.getLiteralData());
143 dim = isl_space_set_alloc(ctx, 0, 1);
144 set = isl_set_universe(dim);
145 set = set_bounds(set, isl_dim_set, 0, lb, ub);
147 value_bounds[vd] = set;
151 /* Handle pragmas of the form
153 * #pragma parameter identifier lower_bound upper_bound
155 * For each such pragma, intersect the context with the set
156 * [identifier] -> { [] : lower_bound <= identifier <= upper_bound }
158 struct PragmaParameterHandler : public PragmaHandler {
159 Sema &sema;
160 isl_set *&context;
162 PragmaParameterHandler(Sema &sema, isl_set *&context) :
163 PragmaHandler("parameter"), sema(sema), context(context) {}
165 virtual void HandlePragma(Preprocessor &PP,
166 PragmaIntroducerKind Introducer,
167 Token &ScopTok) {
168 isl_id *id;
169 isl_ctx *ctx = isl_set_get_ctx(context);
170 isl_space *dim;
171 isl_set *set;
172 ValueDecl *vd;
173 Token token;
174 int lb;
175 int ub;
177 PP.Lex(token);
178 vd = get_value_decl(sema, token);
179 if (!vd) {
180 unsupported(PP, token.getLocation());
181 return;
184 PP.Lex(token);
185 if (!token.isLiteral()) {
186 unsupported(PP, token.getLocation());
187 return;
190 lb = get_int(token.getLiteralData());
192 PP.Lex(token);
193 if (!token.isLiteral()) {
194 unsupported(PP, token.getLocation());
195 return;
198 ub = get_int(token.getLiteralData());
200 id = isl_id_alloc(ctx, vd->getName().str().c_str(), vd);
201 dim = isl_space_set_alloc(ctx, 1, 0);
202 dim = isl_space_set_dim_id(dim, isl_dim_param, 0, id);
204 set = isl_set_universe(dim);
206 set = set_bounds(set, isl_dim_param, 0, lb, ub);
208 context = isl_set_intersect(context, set);
212 /* Handle pragmas of the form
214 * #pragma scop
216 * In particular, store the current location in loc.start.
218 struct PragmaScopHandler : public PragmaHandler {
219 ScopLoc &loc;
221 PragmaScopHandler(ScopLoc &loc) : PragmaHandler("scop"), loc(loc) {}
223 virtual void HandlePragma(Preprocessor &PP,
224 PragmaIntroducerKind Introducer,
225 Token &ScopTok) {
226 SourceManager &SM = PP.getSourceManager();
227 loc.start = SM.getFileOffset(ScopTok.getLocation());
231 /* Handle pragmas of the form
233 * #pragma endscop
235 * In particular, store the current location in loc.end.
237 struct PragmaEndScopHandler : public PragmaHandler {
238 ScopLoc &loc;
240 PragmaEndScopHandler(ScopLoc &loc) :
241 PragmaHandler("endscop"), loc(loc) {}
243 virtual void HandlePragma(Preprocessor &PP,
244 PragmaIntroducerKind Introducer,
245 Token &EndScopTok) {
246 SourceManager &SM = PP.getSourceManager();
247 loc.end = SM.getFileOffset(EndScopTok.getLocation());
251 /* Handle pragmas of the form
253 * #pragma live-out identifier, identifier, ...
255 * Each identifier on the line is stored in live_out.
257 struct PragmaLiveOutHandler : public PragmaHandler {
258 Sema &sema;
259 set<ValueDecl *> &live_out;
261 PragmaLiveOutHandler(Sema &sema, set<ValueDecl *> &live_out) :
262 PragmaHandler("live"), sema(sema), live_out(live_out) {}
264 virtual void HandlePragma(Preprocessor &PP,
265 PragmaIntroducerKind Introducer,
266 Token &ScopTok) {
267 Token token;
269 PP.Lex(token);
270 if (token.isNot(tok::minus))
271 return;
272 PP.Lex(token);
273 if (token.isNot(tok::identifier) ||
274 !token.getIdentifierInfo()->isStr("out"))
275 return;
277 PP.Lex(token);
278 while (token.isNot(tok::eod)) {
279 ValueDecl *vd;
281 vd = get_value_decl(sema, token);
282 if (!vd) {
283 unsupported(PP, token.getLocation());
284 return;
286 live_out.insert(vd);
287 PP.Lex(token);
288 if (token.is(tok::comma))
289 PP.Lex(token);
294 /* Extract a pet_scop from the appropriate function.
295 * If "function" is not NULL, then we only extract a pet_scop if the
296 * name of the function matches.
297 * If "autodetect" is false, then we only extract if we have seen
298 * scop and endscop pragmas and if these are situated inside the function
299 * body.
301 struct PetASTConsumer : public ASTConsumer {
302 Preprocessor &PP;
303 ASTContext &ast_context;
304 ScopLoc &loc;
305 const char *function;
306 bool autodetect;
307 isl_ctx *ctx;
308 struct pet_scop *scop;
310 PetASTConsumer(isl_ctx *ctx, Preprocessor &PP, ASTContext &ast_context,
311 ScopLoc &loc, const char *function, bool autodetect) :
312 ctx(ctx), PP(PP), ast_context(ast_context), loc(loc),
313 scop(NULL), function(function), autodetect(autodetect) { }
315 virtual void HandleTopLevelDecl(DeclGroupRef dg) {
316 DeclGroupRef::iterator it;
318 if (scop)
319 return;
320 for (it = dg.begin(); it != dg.end(); ++it) {
321 FunctionDecl *fd = dyn_cast<clang::FunctionDecl>(*it);
322 if (!fd)
323 continue;
324 if (!fd->hasBody())
325 continue;
326 if (function &&
327 fd->getNameInfo().getAsString() != function)
328 continue;
329 if (autodetect) {
330 PetScan ps(ctx, PP, ast_context, loc, 1);
331 scop = ps.scan(fd);
332 if (scop)
333 break;
334 else
335 continue;
337 if (!loc.end)
338 continue;
339 SourceManager &SM = PP.getSourceManager();
340 if (SM.getFileOffset(fd->getLocStart()) > loc.end)
341 continue;
342 if (SM.getFileOffset(fd->getLocEnd()) < loc.start)
343 continue;
344 PetScan ps(ctx, PP, ast_context, loc, 0);
345 scop = ps.scan(fd);
346 break;
351 static const char *ResourceDir = CLANG_PREFIX"/lib/clang/"CLANG_VERSION_STRING;
353 static const char *implicit_functions[] = {
354 "min", "max", "ceild", "floord"
357 static bool is_implicit(const IdentifierInfo *ident)
359 const char *name = ident->getNameStart();
360 for (int i = 0; i < ARRAY_SIZE(implicit_functions); ++i)
361 if (!strcmp(name, implicit_functions[i]))
362 return true;
363 return false;
366 /* Ignore implicit function declaration warnings on
367 * "min", "max", "ceild" and "floord" as we detect and handle these
368 * in PetScan.
370 struct MyDiagnosticPrinter : public TextDiagnosticPrinter {
371 MyDiagnosticPrinter(const DiagnosticOptions &DO) :
372 TextDiagnosticPrinter(llvm::errs(), DO) {}
373 virtual void HandleDiagnostic(Diagnostic::Level level,
374 const DiagnosticInfo &info) {
375 if (info.getID() == diag::ext_implicit_function_decl &&
376 info.getNumArgs() == 1 &&
377 info.getArgKind(0) == Diagnostic::ak_identifierinfo &&
378 is_implicit(info.getArgIdentifier(0)))
379 /* ignore warning */;
380 else
381 TextDiagnosticPrinter::HandleDiagnostic(level, info);
385 static void update_arrays(struct pet_scop *scop,
386 map<ValueDecl *, isl_set *> &value_bounds,
387 set<ValueDecl *> &live_out)
389 map<ValueDecl *, isl_set *>::iterator vb_it;
390 set<ValueDecl *>::iterator lo_it;
392 if (!scop)
393 return;
395 for (int i = 0; i < scop->n_array; ++i) {
396 isl_id *id;
397 ValueDecl *decl;
398 pet_array *array = scop->arrays[i];
400 id = isl_set_get_tuple_id(array->extent);
401 decl = (ValueDecl *)isl_id_get_user(id);
402 isl_id_free(id);
404 vb_it = value_bounds.find(decl);
405 if (vb_it != value_bounds.end())
406 array->value_bounds = isl_set_copy(vb_it->second);
408 lo_it = live_out.find(decl);
409 if (lo_it != live_out.end())
410 array->live_out = 1;
414 /* Extract a pet_scop from the C source file called "filename".
415 * If "function" is not NULL, extract the pet_scop from the function
416 * with that name.
417 * If "autodetect" is set, extract any pet_scop we can find.
418 * Otherwise, extract the pet_scop from the region delimited
419 * by "scop" and "endscop" pragmas.
421 * We first set up the clang parser and then try to extract the
422 * pet_scop from the appropriate function in PetASTConsumer.
423 * If we have found a pet_scop, we add the context and value_bounds
424 * constraints specified through pragmas.
426 struct pet_scop *pet_scop_extract_from_C_source(isl_ctx *ctx,
427 const char *filename, const char *function, int autodetect)
429 isl_space *dim;
430 isl_set *context;
431 set<ValueDecl *> live_out;
432 map<ValueDecl *, isl_set *> value_bounds;
433 map<ValueDecl *, isl_set *>::iterator vb_it;
435 FileSystemOptions FO;
436 FileManager FM(FO);
437 const FileEntry *file = FM.getFile(filename);
438 if (!file)
439 isl_die(ctx, isl_error_unknown, "unable to open file",
440 return NULL);
442 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
443 DiagnosticOptions DO;
444 Diagnostic Diags(DiagID, new MyDiagnosticPrinter(DO));
445 Diags.setSuppressSystemWarnings(true);
446 TargetOptions TO;
447 TO.Triple = llvm::sys::getHostTriple();
448 TargetInfo *target = TargetInfo::CreateTargetInfo(Diags, TO);
449 SourceManager SM(Diags, FM);
450 HeaderSearch HS(FM);
451 LangOptions LO;
452 CompilerInvocation::setLangDefaults(LO, IK_C,
453 LangStandard::lang_unspecified);
454 Preprocessor PP(Diags, LO, *target, SM, HS);
455 HeaderSearchOptions HSO;
456 PreprocessorOptions PPO;
457 FrontendOptions FEO;
458 HSO.ResourceDir = ResourceDir;
459 InitializePreprocessor(PP, PPO, HSO, FEO);
461 ScopLoc loc;
463 SM.createMainFileID(file);
465 ASTContext ast_context(LO, PP.getSourceManager(),
466 *target, PP.getIdentifierTable(), PP.getSelectorTable(),
467 PP.getBuiltinInfo(), 0);
468 PetASTConsumer consumer(ctx, PP, ast_context,
469 loc, function, autodetect);
470 Sema sema(PP, ast_context, consumer);
472 if (!autodetect) {
473 PP.AddPragmaHandler(new PragmaScopHandler(loc));
474 PP.AddPragmaHandler(new PragmaEndScopHandler(loc));
475 PP.AddPragmaHandler(new PragmaLiveOutHandler(sema, live_out));
478 dim = isl_space_params_alloc(ctx, 0);
479 context = isl_set_universe(dim);
480 PP.AddPragmaHandler(new PragmaParameterHandler(sema, context));
481 PP.AddPragmaHandler(new PragmaValueBoundsHandler(ctx, sema, value_bounds));
483 Diags.getClient()->BeginSourceFile(LO, &PP);
484 ParseAST(sema);
485 Diags.getClient()->EndSourceFile();
487 delete target;
488 llvm::llvm_shutdown();
490 if (consumer.scop)
491 consumer.scop->context = isl_set_intersect(context,
492 consumer.scop->context);
493 else
494 isl_set_free(context);
496 update_arrays(consumer.scop, value_bounds, live_out);
498 for (vb_it = value_bounds.begin(); vb_it != value_bounds.end(); vb_it++)
499 isl_set_free(vb_it->second);
501 return consumer.scop;