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 /* Handle pragmas of the form
80 * #pragma value_bounds identifier lower_bound upper_bound
82 * For each such pragma, add a mapping from the ValueDecl corresponding
83 * to "identifier" to a set { [i] : lower_bound <= i <= upper_bound }
84 * to the map value_bounds.
86 struct PragmaValueBoundsHandler
: public PragmaHandler
{
89 map
<ValueDecl
*, isl_set
*> &value_bounds
;
91 PragmaValueBoundsHandler(isl_ctx
*ctx
, Sema
&sema
,
92 map
<ValueDecl
*, isl_set
*> &value_bounds
) :
93 PragmaHandler("value_bounds"), ctx(ctx
), sema(sema
),
94 value_bounds(value_bounds
) {}
96 virtual void HandlePragma(Preprocessor
&PP
,
97 PragmaIntroducerKind Introducer
,
101 IdentifierInfo
*name
;
109 if (token
.isNot(tok::identifier
)) {
110 unsupported(PP
, token
.getLocation());
114 name
= token
.getIdentifierInfo();
115 decl
= sema
.LookupSingleName(sema
.TUScope
, name
,
116 token
.getLocation(), Sema::LookupOrdinaryName
);
117 vd
= decl
? cast_or_null
<ValueDecl
>(decl
) : NULL
;
119 unsupported(PP
, token
.getLocation());
124 if (!token
.isLiteral()) {
125 unsupported(PP
, token
.getLocation());
129 lb
= get_int(token
.getLiteralData());
132 if (!token
.isLiteral()) {
133 unsupported(PP
, token
.getLocation());
137 ub
= get_int(token
.getLiteralData());
139 dim
= isl_dim_set_alloc(ctx
, 0, 1);
140 set
= isl_set_universe(dim
);
141 set
= set_bounds(set
, isl_dim_set
, 0, lb
, ub
);
143 value_bounds
[vd
] = set
;
147 /* Handle pragmas of the form
149 * #pragma parameter identifier lower_bound upper_bound
151 * For each such pragma, intersect the context with the set
152 * [identifier] -> { [] : lower_bound <= identifier <= upper_bound }
154 struct PragmaParameterHandler
: public PragmaHandler
{
158 PragmaParameterHandler(Sema
&sema
, isl_set
*&context
) :
159 PragmaHandler("parameter"), sema(sema
), context(context
) {}
161 virtual void HandlePragma(Preprocessor
&PP
,
162 PragmaIntroducerKind Introducer
,
165 isl_ctx
*ctx
= isl_set_get_ctx(context
);
168 IdentifierInfo
*name
;
176 if (token
.isNot(tok::identifier
)) {
177 unsupported(PP
, token
.getLocation());
181 name
= token
.getIdentifierInfo();
182 decl
= sema
.LookupSingleName(sema
.TUScope
, name
,
183 token
.getLocation(), Sema::LookupOrdinaryName
);
184 vd
= decl
? cast_or_null
<ValueDecl
>(decl
) : NULL
;
186 unsupported(PP
, token
.getLocation());
191 if (!token
.isLiteral()) {
192 unsupported(PP
, token
.getLocation());
196 lb
= get_int(token
.getLiteralData());
199 if (!token
.isLiteral()) {
200 unsupported(PP
, token
.getLocation());
204 ub
= get_int(token
.getLiteralData());
206 id
= isl_id_alloc(ctx
, vd
->getName().str().c_str(), vd
);
207 dim
= isl_dim_set_alloc(ctx
, 1, 0);
208 dim
= isl_dim_set_dim_id(dim
, isl_dim_param
, 0, id
);
210 set
= isl_set_universe(dim
);
212 set
= set_bounds(set
, isl_dim_param
, 0, lb
, ub
);
214 context
= isl_set_intersect(context
, set
);
218 /* Handle pragmas of the form
222 * In particular, store the current location in loc.start.
224 struct PragmaScopHandler
: public PragmaHandler
{
227 PragmaScopHandler(ScopLoc
&loc
) : PragmaHandler("scop"), loc(loc
) {}
229 virtual void HandlePragma(Preprocessor
&PP
,
230 PragmaIntroducerKind Introducer
,
232 SourceManager
&SM
= PP
.getSourceManager();
233 loc
.start
= SM
.getFileOffset(ScopTok
.getLocation());
237 /* Handle pragmas of the form
241 * In particular, store the current location in loc.end.
243 struct PragmaEndScopHandler
: public PragmaHandler
{
246 PragmaEndScopHandler(ScopLoc
&loc
) :
247 PragmaHandler("endscop"), loc(loc
) {}
249 virtual void HandlePragma(Preprocessor
&PP
,
250 PragmaIntroducerKind Introducer
,
252 SourceManager
&SM
= PP
.getSourceManager();
253 loc
.end
= SM
.getFileOffset(EndScopTok
.getLocation());
257 /* Extract a pet_scop from the appropriate function.
258 * If "function" is not NULL, then we only extract a pet_scop if the
259 * name of the function matches.
260 * If "autodetect" is false, then we only extract if we have seen
261 * scop and endscop pragmas and if these are situated inside the function
264 struct PetASTConsumer
: public ASTConsumer
{
266 ASTContext
&ast_context
;
268 const char *function
;
271 struct pet_scop
*scop
;
273 PetASTConsumer(isl_ctx
*ctx
, Preprocessor
&PP
, ASTContext
&ast_context
,
274 ScopLoc
&loc
, const char *function
, bool autodetect
) :
275 ctx(ctx
), PP(PP
), ast_context(ast_context
), loc(loc
),
276 scop(NULL
), function(function
), autodetect(autodetect
) { }
278 virtual void HandleTopLevelDecl(DeclGroupRef dg
) {
279 DeclGroupRef::iterator it
;
283 for (it
= dg
.begin(); it
!= dg
.end(); ++it
) {
284 FunctionDecl
*fd
= dyn_cast
<clang::FunctionDecl
>(*it
);
290 fd
->getNameInfo().getAsString() != function
)
293 PetScan
ps(ctx
, PP
, ast_context
, loc
, 1);
302 SourceManager
&SM
= PP
.getSourceManager();
303 if (SM
.getFileOffset(fd
->getLocStart()) > loc
.end
)
305 if (SM
.getFileOffset(fd
->getLocEnd()) < loc
.start
)
307 PetScan
ps(ctx
, PP
, ast_context
, loc
, 0);
314 static const char *ResourceDir
= CLANG_PREFIX
"/lib/clang/"CLANG_VERSION_STRING
;
316 static const char *implicit_functions
[] = {
317 "min", "max", "ceild", "floord"
320 static bool is_implicit(const IdentifierInfo
*ident
)
322 const char *name
= ident
->getNameStart();
323 for (int i
= 0; i
< ARRAY_SIZE(implicit_functions
); ++i
)
324 if (!strcmp(name
, implicit_functions
[i
]))
329 /* Ignore implicit function declaration warnings on
330 * "min", "max", "ceild" and "floord" as we detect and handle these
333 struct MyDiagnosticPrinter
: public TextDiagnosticPrinter
{
334 MyDiagnosticPrinter(const DiagnosticOptions
&DO
) :
335 TextDiagnosticPrinter(llvm::errs(), DO
) {}
336 virtual void HandleDiagnostic(Diagnostic::Level level
,
337 const DiagnosticInfo
&info
) {
338 if (info
.getID() == diag::ext_implicit_function_decl
&&
339 info
.getNumArgs() == 1 &&
340 info
.getArgKind(0) == Diagnostic::ak_identifierinfo
&&
341 is_implicit(info
.getArgIdentifier(0)))
342 /* ignore warning */;
344 TextDiagnosticPrinter::HandleDiagnostic(level
, info
);
348 /* Extract a pet_scop from the C source file called "filename".
349 * If "function" is not NULL, extract the pet_scop from the function
351 * If "autodetect" is set, extract any pet_scop we can find.
352 * Otherwise, extract the pet_scop from the region delimited
353 * by "scop" and "endscop" pragmas.
355 * We first set up the clang parser and then try to extract the
356 * pet_scop from the appropriate function in PetASTConsumer.
357 * If we have found a pet_scop, we add the context and value_bounds
358 * constraints specified through pragmas.
360 struct pet_scop
*pet_scop_extract_from_C_source(isl_ctx
*ctx
,
361 const char *filename
, const char *function
, int autodetect
)
365 map
<ValueDecl
*, isl_set
*> value_bounds
;
366 map
<ValueDecl
*, isl_set
*>::iterator vb_it
;
368 FileSystemOptions FO
;
370 const FileEntry
*file
= FM
.getFile(filename
);
372 isl_die(ctx
, isl_error_unknown
, "unable to open file",
375 llvm::IntrusiveRefCntPtr
<DiagnosticIDs
> DiagID(new DiagnosticIDs());
376 DiagnosticOptions DO
;
377 Diagnostic
Diags(DiagID
, new MyDiagnosticPrinter(DO
));
379 TO
.Triple
= llvm::sys::getHostTriple();
380 TargetInfo
*target
= TargetInfo::CreateTargetInfo(Diags
, TO
);
381 SourceManager
SM(Diags
, FM
);
384 CompilerInvocation::setLangDefaults(LO
, IK_C
,
385 LangStandard::lang_unspecified
);
386 Preprocessor
PP(Diags
, LO
, *target
, SM
, HS
);
387 HeaderSearchOptions HSO
;
388 PreprocessorOptions PPO
;
390 HSO
.ResourceDir
= ResourceDir
;
391 InitializePreprocessor(PP
, PPO
, HSO
, FEO
);
396 PP
.AddPragmaHandler(new PragmaScopHandler(loc
));
397 PP
.AddPragmaHandler(new PragmaEndScopHandler(loc
));
400 SM
.createMainFileID(file
);
402 ASTContext
ast_context(LO
, PP
.getSourceManager(),
403 *target
, PP
.getIdentifierTable(), PP
.getSelectorTable(),
404 PP
.getBuiltinInfo(), 0);
405 PetASTConsumer
consumer(ctx
, PP
, ast_context
,
406 loc
, function
, autodetect
);
407 Sema
sema(PP
, ast_context
, consumer
);
409 dim
= isl_dim_set_alloc(ctx
, 0, 0);
410 context
= isl_set_universe(dim
);
411 PP
.AddPragmaHandler(new PragmaParameterHandler(sema
, context
));
412 PP
.AddPragmaHandler(new PragmaValueBoundsHandler(ctx
, sema
, value_bounds
));
414 Diags
.getClient()->BeginSourceFile(LO
, &PP
);
416 Diags
.getClient()->EndSourceFile();
419 llvm::llvm_shutdown();
422 consumer
.scop
->context
= isl_set_intersect(context
,
423 consumer
.scop
->context
);
425 isl_set_free(context
);
428 for (int i
= 0; i
< consumer
.scop
->n_array
; ++i
) {
431 pet_array
*array
= consumer
.scop
->arrays
[i
];
433 id
= isl_set_get_tuple_id(array
->extent
);
434 decl
= (ValueDecl
*)isl_id_get_user(id
);
437 vb_it
= value_bounds
.find(decl
);
438 if (vb_it
!= value_bounds
.end())
439 array
->value_bounds
= isl_set_copy(vb_it
->second
);
443 for (vb_it
= value_bounds
.begin(); vb_it
!= value_bounds
.end(); vb_it
++)
444 isl_set_free(vb_it
->second
);
446 return consumer
.scop
;