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
{
267 const char *function
;
270 struct pet_scop
*scop
;
272 PetASTConsumer(isl_ctx
*ctx
, Preprocessor
&PP
, ScopLoc
&loc
,
273 const char *function
, bool autodetect
) :
274 ctx(ctx
), PP(PP
), loc(loc
), scop(NULL
),
275 function(function
), autodetect(autodetect
) { }
277 virtual void HandleTopLevelDecl(DeclGroupRef dg
) {
278 DeclGroupRef::iterator it
;
282 for (it
= dg
.begin(); it
!= dg
.end(); ++it
) {
283 FunctionDecl
*fd
= dyn_cast
<clang::FunctionDecl
>(*it
);
289 fd
->getNameInfo().getAsString() != function
)
292 PetScan
ps(ctx
, PP
, loc
, 1);
301 SourceManager
&SM
= PP
.getSourceManager();
302 if (SM
.getFileOffset(fd
->getLocStart()) > loc
.end
)
304 if (SM
.getFileOffset(fd
->getLocEnd()) < loc
.start
)
306 PetScan
ps(ctx
, PP
, loc
, 0);
313 static const char *ResourceDir
= CLANG_PREFIX
"/lib/clang/"CLANG_VERSION_STRING
;
315 static const char *implicit_functions
[] = {
316 "min", "max", "ceild", "floord"
319 static bool is_implicit(const IdentifierInfo
*ident
)
321 const char *name
= ident
->getNameStart();
322 for (int i
= 0; i
< ARRAY_SIZE(implicit_functions
); ++i
)
323 if (!strcmp(name
, implicit_functions
[i
]))
328 /* Ignore implicit function declaration warnings on
329 * "min", "max", "ceild" and "floord" as we detect and handle these
332 struct MyDiagnosticPrinter
: public TextDiagnosticPrinter
{
333 MyDiagnosticPrinter(const DiagnosticOptions
&DO
) :
334 TextDiagnosticPrinter(llvm::errs(), DO
) {}
335 virtual void HandleDiagnostic(Diagnostic::Level level
,
336 const DiagnosticInfo
&info
) {
337 if (info
.getID() == diag::ext_implicit_function_decl
&&
338 info
.getNumArgs() == 1 &&
339 info
.getArgKind(0) == Diagnostic::ak_identifierinfo
&&
340 is_implicit(info
.getArgIdentifier(0)))
341 /* ignore warning */;
343 TextDiagnosticPrinter::HandleDiagnostic(level
, info
);
347 /* Extract a pet_scop from the C source file called "filename".
348 * If "function" is not NULL, extract the pet_scop from the function
350 * If "autodetect" is set, extract any pet_scop we can find.
351 * Otherwise, extract the pet_scop from the region delimited
352 * by "scop" and "endscop" pragmas.
354 * We first set up the clang parser and then try to extract the
355 * pet_scop from the appropriate function in PetASTConsumer.
356 * If we have found a pet_scop, we add the context and value_bounds
357 * constraints specified through pragmas.
359 struct pet_scop
*pet_scop_extract_from_C_source(isl_ctx
*ctx
,
360 const char *filename
, const char *function
, int autodetect
)
364 map
<ValueDecl
*, isl_set
*> value_bounds
;
365 map
<ValueDecl
*, isl_set
*>::iterator vb_it
;
367 FileSystemOptions FO
;
369 const FileEntry
*file
= FM
.getFile(filename
);
371 isl_die(ctx
, isl_error_unknown
, "unable to open file",
374 llvm::IntrusiveRefCntPtr
<DiagnosticIDs
> DiagID(new DiagnosticIDs());
375 DiagnosticOptions DO
;
376 Diagnostic
Diags(DiagID
, new MyDiagnosticPrinter(DO
));
378 TO
.Triple
= llvm::sys::getHostTriple();
379 TargetInfo
*target
= TargetInfo::CreateTargetInfo(Diags
, TO
);
380 SourceManager
SM(Diags
, FM
);
383 CompilerInvocation::setLangDefaults(LO
, IK_C
,
384 LangStandard::lang_unspecified
);
385 Preprocessor
PP(Diags
, LO
, *target
, SM
, HS
);
386 HeaderSearchOptions HSO
;
387 PreprocessorOptions PPO
;
389 HSO
.ResourceDir
= ResourceDir
;
390 InitializePreprocessor(PP
, PPO
, HSO
, FEO
);
395 PP
.AddPragmaHandler(new PragmaScopHandler(loc
));
396 PP
.AddPragmaHandler(new PragmaEndScopHandler(loc
));
399 SM
.createMainFileID(file
);
401 ASTContext
ast_context(LO
, PP
.getSourceManager(),
402 *target
, PP
.getIdentifierTable(), PP
.getSelectorTable(),
403 PP
.getBuiltinInfo(), 0);
404 PetASTConsumer
consumer(ctx
, PP
, loc
, function
, autodetect
);
405 Sema
sema(PP
, ast_context
, consumer
);
407 dim
= isl_dim_set_alloc(ctx
, 0, 0);
408 context
= isl_set_universe(dim
);
409 PP
.AddPragmaHandler(new PragmaParameterHandler(sema
, context
));
410 PP
.AddPragmaHandler(new PragmaValueBoundsHandler(ctx
, sema
, value_bounds
));
412 Diags
.getClient()->BeginSourceFile(LO
, &PP
);
414 Diags
.getClient()->EndSourceFile();
417 llvm::llvm_shutdown();
420 consumer
.scop
->context
= isl_set_intersect(context
,
421 consumer
.scop
->context
);
423 isl_set_free(context
);
426 for (int i
= 0; i
< consumer
.scop
->n_array
; ++i
) {
429 pet_array
*array
= consumer
.scop
->arrays
[i
];
431 id
= isl_set_get_tuple_id(array
->extent
);
432 decl
= (ValueDecl
*)isl_id_get_user(id
);
435 vb_it
= value_bounds
.find(decl
);
436 if (vb_it
!= value_bounds
.end())
437 array
->value_bounds
= isl_set_copy(vb_it
->second
);
441 for (vb_it
= value_bounds
.begin(); vb_it
!= value_bounds
.end(); vb_it
++)
442 isl_set_free(vb_it
->second
);
444 return consumer
.scop
;