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 /* Extract a pet_scop from the appropriate function.
252 * If "function" is not NULL, then we only extract a pet_scop if the
253 * name of the function matches.
254 * If "autodetect" is false, then we only extract if we have seen
255 * scop and endscop pragmas and if these are situated inside the function
258 struct PetASTConsumer
: public ASTConsumer
{
260 ASTContext
&ast_context
;
262 const char *function
;
265 struct pet_scop
*scop
;
267 PetASTConsumer(isl_ctx
*ctx
, Preprocessor
&PP
, ASTContext
&ast_context
,
268 ScopLoc
&loc
, const char *function
, bool autodetect
) :
269 ctx(ctx
), PP(PP
), ast_context(ast_context
), loc(loc
),
270 scop(NULL
), function(function
), autodetect(autodetect
) { }
272 virtual void HandleTopLevelDecl(DeclGroupRef dg
) {
273 DeclGroupRef::iterator it
;
277 for (it
= dg
.begin(); it
!= dg
.end(); ++it
) {
278 FunctionDecl
*fd
= dyn_cast
<clang::FunctionDecl
>(*it
);
284 fd
->getNameInfo().getAsString() != function
)
287 PetScan
ps(ctx
, PP
, ast_context
, loc
, 1);
296 SourceManager
&SM
= PP
.getSourceManager();
297 if (SM
.getFileOffset(fd
->getLocStart()) > loc
.end
)
299 if (SM
.getFileOffset(fd
->getLocEnd()) < loc
.start
)
301 PetScan
ps(ctx
, PP
, ast_context
, loc
, 0);
308 static const char *ResourceDir
= CLANG_PREFIX
"/lib/clang/"CLANG_VERSION_STRING
;
310 static const char *implicit_functions
[] = {
311 "min", "max", "ceild", "floord"
314 static bool is_implicit(const IdentifierInfo
*ident
)
316 const char *name
= ident
->getNameStart();
317 for (int i
= 0; i
< ARRAY_SIZE(implicit_functions
); ++i
)
318 if (!strcmp(name
, implicit_functions
[i
]))
323 /* Ignore implicit function declaration warnings on
324 * "min", "max", "ceild" and "floord" as we detect and handle these
327 struct MyDiagnosticPrinter
: public TextDiagnosticPrinter
{
328 MyDiagnosticPrinter(const DiagnosticOptions
&DO
) :
329 TextDiagnosticPrinter(llvm::errs(), DO
) {}
330 virtual void HandleDiagnostic(Diagnostic::Level level
,
331 const DiagnosticInfo
&info
) {
332 if (info
.getID() == diag::ext_implicit_function_decl
&&
333 info
.getNumArgs() == 1 &&
334 info
.getArgKind(0) == Diagnostic::ak_identifierinfo
&&
335 is_implicit(info
.getArgIdentifier(0)))
336 /* ignore warning */;
338 TextDiagnosticPrinter::HandleDiagnostic(level
, info
);
342 /* Extract a pet_scop from the C source file called "filename".
343 * If "function" is not NULL, extract the pet_scop from the function
345 * If "autodetect" is set, extract any pet_scop we can find.
346 * Otherwise, extract the pet_scop from the region delimited
347 * by "scop" and "endscop" pragmas.
349 * We first set up the clang parser and then try to extract the
350 * pet_scop from the appropriate function in PetASTConsumer.
351 * If we have found a pet_scop, we add the context and value_bounds
352 * constraints specified through pragmas.
354 struct pet_scop
*pet_scop_extract_from_C_source(isl_ctx
*ctx
,
355 const char *filename
, const char *function
, int autodetect
)
359 map
<ValueDecl
*, isl_set
*> value_bounds
;
360 map
<ValueDecl
*, isl_set
*>::iterator vb_it
;
362 FileSystemOptions FO
;
364 const FileEntry
*file
= FM
.getFile(filename
);
366 isl_die(ctx
, isl_error_unknown
, "unable to open file",
369 llvm::IntrusiveRefCntPtr
<DiagnosticIDs
> DiagID(new DiagnosticIDs());
370 DiagnosticOptions DO
;
371 Diagnostic
Diags(DiagID
, new MyDiagnosticPrinter(DO
));
373 TO
.Triple
= llvm::sys::getHostTriple();
374 TargetInfo
*target
= TargetInfo::CreateTargetInfo(Diags
, TO
);
375 SourceManager
SM(Diags
, FM
);
378 CompilerInvocation::setLangDefaults(LO
, IK_C
,
379 LangStandard::lang_unspecified
);
380 Preprocessor
PP(Diags
, LO
, *target
, SM
, HS
);
381 HeaderSearchOptions HSO
;
382 PreprocessorOptions PPO
;
384 HSO
.ResourceDir
= ResourceDir
;
385 InitializePreprocessor(PP
, PPO
, HSO
, FEO
);
390 PP
.AddPragmaHandler(new PragmaScopHandler(loc
));
391 PP
.AddPragmaHandler(new PragmaEndScopHandler(loc
));
394 SM
.createMainFileID(file
);
396 ASTContext
ast_context(LO
, PP
.getSourceManager(),
397 *target
, PP
.getIdentifierTable(), PP
.getSelectorTable(),
398 PP
.getBuiltinInfo(), 0);
399 PetASTConsumer
consumer(ctx
, PP
, ast_context
,
400 loc
, function
, autodetect
);
401 Sema
sema(PP
, ast_context
, consumer
);
403 dim
= isl_dim_set_alloc(ctx
, 0, 0);
404 context
= isl_set_universe(dim
);
405 PP
.AddPragmaHandler(new PragmaParameterHandler(sema
, context
));
406 PP
.AddPragmaHandler(new PragmaValueBoundsHandler(ctx
, sema
, value_bounds
));
408 Diags
.getClient()->BeginSourceFile(LO
, &PP
);
410 Diags
.getClient()->EndSourceFile();
413 llvm::llvm_shutdown();
416 consumer
.scop
->context
= isl_set_intersect(context
,
417 consumer
.scop
->context
);
419 isl_set_free(context
);
422 for (int i
= 0; i
< consumer
.scop
->n_array
; ++i
) {
425 pet_array
*array
= consumer
.scop
->arrays
[i
];
427 id
= isl_set_get_tuple_id(array
->extent
);
428 decl
= (ValueDecl
*)isl_id_get_user(id
);
431 vb_it
= value_bounds
.find(decl
);
432 if (vb_it
!= value_bounds
.end())
433 array
->value_bounds
= isl_set_copy(vb_it
->second
);
437 for (vb_it
= value_bounds
.begin(); vb_it
!= value_bounds
.end(); vb_it
++)
438 isl_set_free(vb_it
->second
);
440 return consumer
.scop
;