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>
31 #include <isl/constraint.h>
37 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
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"
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
)
60 c
= isl_inequality_alloc(isl_set_get_dim(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_dim(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
);
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
)
83 if (token
.isNot(tok::identifier
))
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
{
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
,
121 vd
= get_value_decl(sema
, token
);
123 unsupported(PP
, token
.getLocation());
128 if (!token
.isLiteral()) {
129 unsupported(PP
, token
.getLocation());
133 lb
= get_int(token
.getLiteralData());
136 if (!token
.isLiteral()) {
137 unsupported(PP
, token
.getLocation());
141 ub
= get_int(token
.getLiteralData());
143 dim
= isl_dim_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
{
162 PragmaParameterHandler(Sema
&sema
, isl_set
*&context
) :
163 PragmaHandler("parameter"), sema(sema
), context(context
) {}
165 virtual void HandlePragma(Preprocessor
&PP
,
166 PragmaIntroducerKind Introducer
,
169 isl_ctx
*ctx
= isl_set_get_ctx(context
);
178 vd
= get_value_decl(sema
, token
);
180 unsupported(PP
, token
.getLocation());
185 if (!token
.isLiteral()) {
186 unsupported(PP
, token
.getLocation());
190 lb
= get_int(token
.getLiteralData());
193 if (!token
.isLiteral()) {
194 unsupported(PP
, token
.getLocation());
198 ub
= get_int(token
.getLiteralData());
200 id
= isl_id_alloc(ctx
, vd
->getName().str().c_str(), vd
);
201 dim
= isl_dim_set_alloc(ctx
, 1, 0);
202 dim
= isl_dim_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
216 * In particular, store the current location in loc.start.
218 struct PragmaScopHandler
: public PragmaHandler
{
221 PragmaScopHandler(ScopLoc
&loc
) : PragmaHandler("scop"), loc(loc
) {}
223 virtual void HandlePragma(Preprocessor
&PP
,
224 PragmaIntroducerKind Introducer
,
226 SourceManager
&SM
= PP
.getSourceManager();
227 loc
.start
= SM
.getFileOffset(ScopTok
.getLocation());
231 /* Handle pragmas of the form
235 * In particular, store the current location in loc.end.
237 struct PragmaEndScopHandler
: public PragmaHandler
{
240 PragmaEndScopHandler(ScopLoc
&loc
) :
241 PragmaHandler("endscop"), loc(loc
) {}
243 virtual void HandlePragma(Preprocessor
&PP
,
244 PragmaIntroducerKind Introducer
,
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
{
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
,
270 if (token
.isNot(tok::minus
))
273 if (token
.isNot(tok::identifier
) ||
274 !token
.getIdentifierInfo()->isStr("out"))
278 while (token
.isNot(tok::eod
)) {
281 vd
= get_value_decl(sema
, token
);
283 unsupported(PP
, token
.getLocation());
288 if (token
.is(tok::comma
))
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
301 struct PetASTConsumer
: public ASTConsumer
{
303 ASTContext
&ast_context
;
305 const char *function
;
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
;
320 for (it
= dg
.begin(); it
!= dg
.end(); ++it
) {
321 FunctionDecl
*fd
= dyn_cast
<clang::FunctionDecl
>(*it
);
327 fd
->getNameInfo().getAsString() != function
)
330 PetScan
ps(ctx
, PP
, ast_context
, loc
, 1);
339 SourceManager
&SM
= PP
.getSourceManager();
340 if (SM
.getFileOffset(fd
->getLocStart()) > loc
.end
)
342 if (SM
.getFileOffset(fd
->getLocEnd()) < loc
.start
)
344 PetScan
ps(ctx
, PP
, ast_context
, loc
, 0);
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
]))
366 /* Ignore implicit function declaration warnings on
367 * "min", "max", "ceild" and "floord" as we detect and handle these
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 */;
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
;
395 for (int i
= 0; i
< scop
->n_array
; ++i
) {
398 pet_array
*array
= scop
->arrays
[i
];
400 id
= isl_set_get_tuple_id(array
->extent
);
401 decl
= (ValueDecl
*)isl_id_get_user(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())
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
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
)
431 set
<ValueDecl
*> live_out
;
432 map
<ValueDecl
*, isl_set
*> value_bounds
;
433 map
<ValueDecl
*, isl_set
*>::iterator vb_it
;
435 FileSystemOptions FO
;
437 const FileEntry
*file
= FM
.getFile(filename
);
439 isl_die(ctx
, isl_error_unknown
, "unable to open file",
442 llvm::IntrusiveRefCntPtr
<DiagnosticIDs
> DiagID(new DiagnosticIDs());
443 DiagnosticOptions DO
;
444 Diagnostic
Diags(DiagID
, new MyDiagnosticPrinter(DO
));
445 Diags
.setSuppressSystemWarnings(true);
447 TO
.Triple
= llvm::sys::getHostTriple();
448 TargetInfo
*target
= TargetInfo::CreateTargetInfo(Diags
, TO
);
449 SourceManager
SM(Diags
, FM
);
452 CompilerInvocation::setLangDefaults(LO
, IK_C
,
453 LangStandard::lang_unspecified
);
454 Preprocessor
PP(Diags
, LO
, *target
, SM
, HS
);
455 HeaderSearchOptions HSO
;
456 PreprocessorOptions PPO
;
458 HSO
.ResourceDir
= ResourceDir
;
459 InitializePreprocessor(PP
, PPO
, HSO
, FEO
);
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
);
473 PP
.AddPragmaHandler(new PragmaScopHandler(loc
));
474 PP
.AddPragmaHandler(new PragmaEndScopHandler(loc
));
475 PP
.AddPragmaHandler(new PragmaLiveOutHandler(sema
, live_out
));
478 dim
= isl_dim_set_alloc(ctx
, 0, 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
);
485 Diags
.getClient()->EndSourceFile();
488 llvm::llvm_shutdown();
491 consumer
.scop
->context
= isl_set_intersect(context
,
492 consumer
.scop
->context
);
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
;