-
Notifications
You must be signed in to change notification settings - Fork 133
/
ompAstConstruction.cpp
2536 lines (2378 loc) · 97.7 KB
/
ompAstConstruction.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Put here code used to construct SgOmp* nodes
// Liao 10/8/2010
#include "sage3basic.h"
#include "rose_paths.h"
#include "astPostProcessing.h"
#include "sageBuilder.h"
#include "OmpAttribute.h"
#include "ompAstConstruction.h"
//void processOpenMP(SgSourceFile* sageFilePtr);
//Liao, 10/27/2008: parsing OpenMP pragma here
//Handle OpenMP pragmas. This should be called after preprocessing information is attached since macro calls may exist within pragmas, Liao, 3/31/2009
extern int omp_parse();
extern OmpSupport::OmpAttribute* getParsedDirective();
extern void omp_parser_init(SgNode* aNode, const char* str);
//Fortran OpenMP parser interface
void parse_fortran_openmp(SgSourceFile *sageFilePtr);
using namespace std;
using namespace SageInterface;
using namespace SageBuilder;
using namespace OmpSupport;
// Liao 4/23/2011, special function to copy file info of the original SgPragma or Fortran comments
static bool copyStartFileInfo (SgNode* src, SgNode* dest, OmpAttribute* oa)
{
bool result = false;
ROSE_ASSERT (src && dest);
// same src and dest, no copy is needed
if (src == dest) return true;
SgLocatedNode* lsrc = isSgLocatedNode(src);
ROSE_ASSERT (lsrc);
SgLocatedNode* ldest= isSgLocatedNode(dest);
ROSE_ASSERT (ldest);
// ROSE_ASSERT (lsrc->get_file_info()->isTransformation() == false);
// already the same, no copy is needed
if (lsrc->get_startOfConstruct()->get_filename() == ldest->get_startOfConstruct()->get_filename()
&& lsrc->get_startOfConstruct()->get_line() == ldest->get_startOfConstruct()->get_line()
&& lsrc->get_startOfConstruct()->get_col() == ldest->get_startOfConstruct()->get_col())
return true;
Sg_File_Info* copy = new Sg_File_Info (*(lsrc->get_startOfConstruct()));
ROSE_ASSERT (copy != NULL);
// delete old start of construct
Sg_File_Info *old_info = ldest->get_startOfConstruct();
if (old_info) delete (old_info);
ldest->set_startOfConstruct(copy);
copy->set_parent(ldest);
// cout<<"debug: set ldest@"<<ldest <<" with file info @"<< copy <<endl;
ROSE_ASSERT (lsrc->get_startOfConstruct()->get_filename() == ldest->get_startOfConstruct()->get_filename());
ROSE_ASSERT (lsrc->get_startOfConstruct()->get_line() == ldest->get_startOfConstruct()->get_line());
ROSE_ASSERT (lsrc->get_startOfConstruct()->get_col() == ldest->get_startOfConstruct()->get_col());
ROSE_ASSERT (lsrc->get_startOfConstruct()->get_filename() == ldest->get_file_info()->get_filename());
ROSE_ASSERT (lsrc->get_startOfConstruct()->get_line() == ldest->get_file_info()->get_line());
ROSE_ASSERT (lsrc->get_startOfConstruct()->get_col() == ldest->get_file_info()->get_col());
ROSE_ASSERT (ldest->get_file_info() == copy);
// Adjustment for Fortran, the AST node attaching the Fortran comment will not actual give out the accurate line number for the comment
if (is_Fortran_language())
{
ROSE_ASSERT (oa != NULL);
PreprocessingInfo *currentPreprocessingInfoPtr = oa->getPreprocessingInfo();
ROSE_ASSERT (currentPreprocessingInfoPtr != NULL);
int commentLine = currentPreprocessingInfoPtr->getLineNumber();
ldest->get_file_info()->set_line(commentLine);
}
return result;
}
// Liao 3/11/2013, special function to copy end file info of the original SgPragma or Fortran comments (src) to OpenMP node (dest)
// If the OpenMP node is a body statement, we have to use the body's end file info as the node's end file info.
static bool copyEndFileInfo (SgNode* src, SgNode* dest, OmpAttribute* oa)
{
bool result = false;
ROSE_ASSERT (src && dest);
if (isSgOmpBodyStatement(dest))
src = isSgOmpBodyStatement(dest)->get_body();
// same src and dest, no copy is needed
if (src == dest) return true;
SgLocatedNode* lsrc = isSgLocatedNode(src);
ROSE_ASSERT (lsrc);
SgLocatedNode* ldest= isSgLocatedNode(dest);
ROSE_ASSERT (ldest);
// ROSE_ASSERT (lsrc->get_file_info()->isTransformation() == false);
// already the same, no copy is needed
if (lsrc->get_endOfConstruct()->get_filename() == ldest->get_endOfConstruct()->get_filename()
&& lsrc->get_endOfConstruct()->get_line() == ldest->get_endOfConstruct()->get_line()
&& lsrc->get_endOfConstruct()->get_col() == ldest->get_endOfConstruct()->get_col())
return true;
Sg_File_Info* copy = new Sg_File_Info (*(lsrc->get_endOfConstruct()));
ROSE_ASSERT (copy != NULL);
// delete old start of construct
Sg_File_Info *old_info = ldest->get_endOfConstruct();
if (old_info) delete (old_info);
ldest->set_endOfConstruct(copy);
copy->set_parent(ldest);
// cout<<"debug: set ldest@"<<ldest <<" with file info @"<< copy <<endl;
ROSE_ASSERT (lsrc->get_endOfConstruct()->get_filename() == ldest->get_endOfConstruct()->get_filename());
ROSE_ASSERT (lsrc->get_endOfConstruct()->get_line() == ldest->get_endOfConstruct()->get_line());
ROSE_ASSERT (lsrc->get_endOfConstruct()->get_col() == ldest->get_endOfConstruct()->get_col());
ROSE_ASSERT (ldest->get_endOfConstruct() == copy);
// Adjustment for Fortran, the AST node attaching the Fortran comment will not actual give out the accurate line number for the comment
if (is_Fortran_language())
{ //TODO fortran support of end file info
// cerr<<"Error. ompAstConstruction.cpp: copying end file info is not yet implemented for Fortran."<<endl;
// ROSE_ASSERT (false);
// ROSE_ASSERT (oa != NULL);
// PreprocessingInfo *currentPreprocessingInfoPtr = oa->getPreprocessingInfo();
// ROSE_ASSERT (currentPreprocessingInfoPtr != NULL);
// int commentLine = currentPreprocessingInfoPtr->getLineNumber();
// ldest->get_file_info()->set_line(commentLine);
}
return result;
}
namespace OmpSupport
{
// an internal data structure to avoid redundant AST traversal to find OpenMP pragmas
static std::list<SgPragmaDeclaration* > omp_pragma_list;
// a similar list to save encountered Fortran comments which are OpenMP directives
std::list<OmpAttribute* > omp_comment_list;
// A pragma list to store the dangling pragmas for Fortran end directives.
// There are stored to ensure correct unparsing after converting Fortran comments into pragmas
// But they should be immediately removed during the OpenMP lowering phase
// static std::list<SgPragmaDeclaration* > omp_end_pragma_list;
// find all SgPragmaDeclaration nodes within a file and parse OpenMP pragmas into OmpAttribute info.
void attachOmpAttributeInfo(SgSourceFile *sageFilePtr)
{
ROSE_ASSERT(sageFilePtr != NULL);
if (sageFilePtr->get_openmp() == false)
return;
// For Fortran, search comments for OpenMP directives
if (sageFilePtr->get_Fortran_only()||sageFilePtr->get_F77_only()||sageFilePtr->get_F90_only()||
sageFilePtr->get_F95_only() || sageFilePtr->get_F2003_only())
{
parse_fortran_openmp(sageFilePtr);
} //end if (fortran)
else
{
// For C/C++, search pragma declarations for OpenMP directives
std::vector <SgNode*> all_pragmas = NodeQuery::querySubTree (sageFilePtr, V_SgPragmaDeclaration);
std::vector<SgNode*>::iterator iter;
for(iter=all_pragmas.begin();iter!=all_pragmas.end();iter++)
{
SgPragmaDeclaration* pragmaDeclaration = isSgPragmaDeclaration(*iter);
ROSE_ASSERT(pragmaDeclaration != NULL);
#if 0 // We should not enforce this since the pragma may come from transformation-generated node
if ((pragmaDeclaration->get_file_info()->isTransformation()
&& pragmaDeclaration->get_file_info()->get_filename()==string("transformation")))
{
cout<<"Found a pragma which is transformation generated. @"<< pragmaDeclaration;
cout<<pragmaDeclaration->unparseToString()<<endl;
pragmaDeclaration->get_file_info()->display("debug transformation generated pragma declaration.");
// Liao 4/23/2011
// #pragma omp task can shown up before a single statement body of a for loop,
// In this case, the frontend will insert a basic block under the loop
// and put both the pragma and the single statement into the block.
// AstPostProcessing() will reset the transformation flag for the pragma
// since its parent(the block) is transformation generated, not in the original code
ROSE_ASSERT(pragmaDeclaration->get_file_info()->isTransformation() ==false || pragmaDeclaration->get_file_info()->get_filename()!=string("transformation"));
}
#endif
SageInterface::replaceMacroCallsWithExpandedStrings(pragmaDeclaration);
string pragmaString = pragmaDeclaration->get_pragma()->get_pragma();
istringstream istr(pragmaString);
std::string key;
istr >> key;
if (key == "omp")
{
// Liao, 3/12/2009
// Outliner may move pragma statements to a new file
// after the pragma has been attached OmpAttribute.
// We have to skip generating the attribute again in the new file
OmpAttributeList* previous = getOmpAttributeList(pragmaDeclaration);
// store them into a buffer, reused by build_OpenMP_AST()
omp_pragma_list.push_back(pragmaDeclaration);
if (previous == NULL )
{
// Call parser
#ifndef ROSE_USE_INTERNAL_FRONTEND_DEVELOPMENT
omp_parser_init(pragmaDeclaration,pragmaString.c_str());
omp_parse();
#endif
OmpAttribute* attribute = getParsedDirective();
//cout<<"sage_gen_be.C:23758 debug:\n"<<pragmaString<<endl;
//attribute->print();//debug only for now
addOmpAttribute(attribute,pragmaDeclaration);
//cout<<"debug: attachOmpAttributeInfo() for a pragma:"<<pragmaString<<"at address:"<<pragmaDeclaration<<endl;
//cout<<"file info for it is:"<<pragmaDeclaration->get_file_info()->get_filename()<<endl;
#if 1 // Liao, 2/12/2010, this could be a bad idea. It causes trouble in comparing
//user-defined and compiler-generated OmpAttribute.
// We attach the attribute redundantly on affected loops also
// for easier loop handling later on in autoTuning's outlining step (reproducing lost pragmas)
if (attribute->getOmpDirectiveType() ==e_for ||attribute->getOmpDirectiveType() ==e_parallel_for)
{
SgForStatement* forstmt = isSgForStatement(getNextStatement(pragmaDeclaration));
ROSE_ASSERT(forstmt != NULL);
//forstmt->addNewAttribute("OmpAttribute",attribute);
addOmpAttribute(attribute,forstmt);
}
#endif
}
}
}// end for
}
}
// Clause node builders
//----------------------------------------------------------
//! Build SgOmpDefaultClause from OmpAttribute, if any
SgOmpDefaultClause * buildOmpDefaultClause(OmpAttribute* att)
{
ROSE_ASSERT(att != NULL);
if (!att->hasClause(e_default))
return NULL;
//grab default option
omp_construct_enum dv = att->getDefaultValue();
SgOmpClause::omp_default_option_enum sg_dv;
switch (dv)
{
case e_default_none:
sg_dv = SgOmpClause::e_omp_default_none;
break;
case e_default_shared:
sg_dv = SgOmpClause::e_omp_default_shared;
break;
case e_default_private:
sg_dv = SgOmpClause::e_omp_default_private;
break;
case e_default_firstprivate:
sg_dv = SgOmpClause::e_omp_default_firstprivate;
break;
default:
{
cerr<<"error: buildOmpDefaultClase() Unacceptable default option from OmpAttribute:"
<<OmpSupport::toString(dv)<<endl;
ROSE_ABORT() ;
}
}//end switch
SgOmpDefaultClause* result = new SgOmpDefaultClause(sg_dv);
setOneSourcePositionForTransformation(result);
ROSE_ASSERT(result);
return result;
}
//! Build SgOmpProcBindClause from OmpAttribute, if any
SgOmpProcBindClause * buildOmpProcBindClause(OmpAttribute* att)
{
ROSE_ASSERT(att != NULL);
if (!att->hasClause(e_proc_bind))
return NULL;
//grab policy
omp_construct_enum dv = att->getProcBindPolicy();
SgOmpClause::omp_proc_bind_policy_enum sg_dv;
switch (dv)
{
case e_proc_bind_close:
sg_dv = SgOmpClause::e_omp_proc_bind_policy_close;
break;
case e_proc_bind_master:
sg_dv = SgOmpClause::e_omp_proc_bind_policy_master;
break;
case e_proc_bind_spread:
sg_dv = SgOmpClause::e_omp_proc_bind_policy_spread;
break;
default:
{
cerr<<"error: buildOmpProcBindClause () Unacceptable default option from OmpAttribute:"
<<OmpSupport::toString(dv)<<endl;
ROSE_ABORT() ;
}
}//end switch
SgOmpProcBindClause* result = new SgOmpProcBindClause(sg_dv);
setOneSourcePositionForTransformation(result);
ROSE_ASSERT(result);
return result;
}
SgOmpAtomicClause * buildOmpAtomicClause(OmpAttribute* att)
{
ROSE_ASSERT(att != NULL);
if (!att->hasClause(e_atomic_clause))
return NULL;
//grab value
omp_construct_enum dv = att->getAtomicAtomicity();
SgOmpClause::omp_atomic_clause_enum sg_dv;
switch (dv)
{
case e_atomic_read:
sg_dv = SgOmpClause::e_omp_atomic_clause_read;
break;
case e_atomic_write:
sg_dv = SgOmpClause::e_omp_atomic_clause_write;
break;
case e_atomic_update:
sg_dv = SgOmpClause::e_omp_atomic_clause_update;
break;
case e_atomic_capture:
sg_dv = SgOmpClause::e_omp_atomic_clause_capture;
break;
default:
{
cerr<<"error: "<<__FUNCTION__ << " Unacceptable default option from OmpAttribute:"
<<OmpSupport::toString(dv)<<endl;
ROSE_ABORT() ;
}
}//end switch
SgOmpAtomicClause* result = new SgOmpAtomicClause(sg_dv);
setOneSourcePositionForTransformation(result);
ROSE_ASSERT(result);
return result;
}
// Sara Royuela ( Nov 2, 2012 ): Check for clause parameters that can be defined in macros
// This adds support for the use of macro definitions in OpenMP clauses
// We need a traversal over SgExpression to support macros in any position of an "assignment_expr"
// F.i.: #define THREADS_1 16
// #define THREADS_2 8
// int main( int arg, char** argv ) {
// #pragma omp parallel num_threads( THREADS_1 + THREADS_2 )
// {}
// }
SgVarRefExpVisitor::SgVarRefExpVisitor()
: expressions()
{}
std::vector<SgExpression*> SgVarRefExpVisitor::get_expressions()
{
return expressions;
}
void SgVarRefExpVisitor::visit(SgNode* node)
{
SgExpression* expr = isSgVarRefExp(node);
if(expr != NULL)
{
expressions.push_back(expr);
}
}
SgExpression* replace_expression_with_macro_value(std::string define_macro, SgExpression* old_exp, bool& macro_replaced,
omp_construct_enum)
{
SgExpression* newExp = old_exp;
// Parse the macro: we are only interested in macros with the form #define MACRO_NAME MACRO_VALUE, the constant macro
size_t parenthesis = define_macro.find("(");
if(parenthesis == string::npos)
{ // Non function macro, constant macro
unsigned int macroNameInitPos = (unsigned int)(define_macro.find("define")) + 6;
while(macroNameInitPos<define_macro.size() && define_macro[macroNameInitPos]==' ')
macroNameInitPos++;
unsigned int macroNameEndPos = define_macro.find(" ", macroNameInitPos);
std::string macroName = define_macro.substr(macroNameInitPos, macroNameEndPos-macroNameInitPos);
if(macroName == isSgVarRefExp(old_exp)->get_symbol()->get_name().getString())
{ // Clause is defined in a macro
size_t comma = define_macro.find(",");
if(comma == string::npos) // Macros like "#define MACRO_NAME VALUE1, VALUE2" are not accepted
{ // We create here an expression with the value of the clause defined in the macro
unsigned int macroValueInitPos = macroNameEndPos + 1;
while(macroValueInitPos<define_macro.size() && define_macro[macroValueInitPos]==' ')
macroValueInitPos++;
unsigned int macroValueEndPos = macroValueInitPos;
while(macroValueEndPos<define_macro.size() &&
define_macro[macroValueEndPos]!=' ' && define_macro[macroValueEndPos]!='\n')
macroValueEndPos++;
std::string macroValue = define_macro.substr(macroValueInitPos, macroValueEndPos-macroValueInitPos);
// Check whether the value is a valid integer
std::string::const_iterator it = macroValue.begin();
while (it != macroValue.end() && std::isdigit(*it))
++it;
ROSE_ASSERT(!macroValue.empty() && it == macroValue.end());
newExp = buildIntVal(atoi(macroValue.c_str()));
if(!isSgPragmaDeclaration(old_exp->get_parent()))
replaceExpression(old_exp, newExp);
macro_replaced = true;
}
}
}
return newExp;
}
SgExpression* checkOmpExpressionClause( SgExpression* clause_expression, SgGlobal* global, omp_construct_enum clause_type )
{
SgExpression* newExp = clause_expression;
// ordered (n): optional (n)
if (clause_expression == NULL && clause_type == e_ordered_clause)
return NULL;
ROSE_ASSERT(clause_expression != NULL);
bool returnNewExpression = false;
if( isSgTypeUnknown( clause_expression->get_type( ) ) )
{
SgVarRefExpVisitor v;
v.traverse(clause_expression, preorder);
std::vector<SgExpression*> expressions = v.get_expressions();
if( !expressions.empty() )
{
if( expressions.size() == 1 )
{ // create the new expression and return it
// otherwise, replace the expression and return the original, which is now modified
returnNewExpression = true;
}
bool macroReplaced;
SgDeclarationStatementPtrList& declarations = global->get_declarations();
while( !expressions.empty() )
{
macroReplaced = false;
SgExpression* oldExp = expressions.back();
for(SgDeclarationStatementPtrList::iterator declIt = declarations.begin(); declIt != declarations.end() && !macroReplaced; ++declIt)
{
SgDeclarationStatement * declaration = *declIt;
AttachedPreprocessingInfoType * preprocInfo = declaration->getAttachedPreprocessingInfo();
if( preprocInfo != NULL )
{ // There is preprocessed info attached to the current node
for(AttachedPreprocessingInfoType::iterator infoIt = preprocInfo->begin();
infoIt != preprocInfo->end() && !macroReplaced; infoIt++)
{
if((*infoIt)->getTypeOfDirective() == PreprocessingInfo::CpreprocessorDefineDeclaration)
{
newExp = replace_expression_with_macro_value( (*infoIt)->getString(), oldExp, macroReplaced, clause_type );
}
}
}
}
// When a macro is defined in a header without any statement, the preprocessed information is attached to the SgFile
if(!macroReplaced)
{
SgProject* project = SageInterface::getProject();
int nFiles = project->numberOfFiles();
for(int fileIt=0; fileIt<nFiles && !macroReplaced; fileIt++)
{
SgFile& file = project->get_file(fileIt);
ROSEAttributesListContainerPtr filePreprocInfo = file.get_preprocessorDirectivesAndCommentsList();
if( filePreprocInfo != NULL )
{
std::map<std::string, ROSEAttributesList*> preprocInfoMap = filePreprocInfo->getList();
for(std::map<std::string, ROSEAttributesList*>::iterator mapIt=preprocInfoMap.begin();
mapIt!=preprocInfoMap.end() && !macroReplaced; mapIt++)
{
std::vector<PreprocessingInfo*> preprocInfoList = mapIt->second->getList();
for(std::vector<PreprocessingInfo*>::iterator infoIt=preprocInfoList.begin();
infoIt!=preprocInfoList.end() && !macroReplaced; infoIt++)
{
if((*infoIt)->getTypeOfDirective() == PreprocessingInfo::CpreprocessorDefineDeclaration)
{
newExp = replace_expression_with_macro_value( (*infoIt)->getString(), oldExp, macroReplaced, clause_type );
}
}
}
}
}
}
expressions.pop_back();
}
}
else
{
printf("error in checkOmpExpressionClause(): no expression found in an expression clause\n");
ROSE_ABORT();
}
}
return (returnNewExpression ? newExp : clause_expression);
}
//Build expression clauses
SgOmpExpressionClause* buildOmpExpressionClause(OmpAttribute* att, omp_construct_enum clause_type)
{
ROSE_ASSERT(att != NULL);
if (!att->hasClause(clause_type))
return NULL;
SgOmpExpressionClause * result = NULL ;
SgGlobal* global = SageInterface::getGlobalScope( att->getNode() );
switch (clause_type)
{
case e_ordered_clause:
{
SgExpression* param = checkOmpExpressionClause( att->getExpression(e_ordered_clause).second, global, e_ordered_clause);
result = new SgOmpOrderedClause(param);
break;
}
case e_collapse:
{
SgExpression* collapseParam = checkOmpExpressionClause( att->getExpression(e_collapse).second, global, e_collapse );
result = new SgOmpCollapseClause(collapseParam);
break;
}
case e_if:
{
SgExpression* ifParam = checkOmpExpressionClause( att->getExpression(e_if).second, global, e_if );
result = new SgOmpIfClause(ifParam);
break;
}
case e_num_threads:
{
SgExpression* numThreadsParam = checkOmpExpressionClause( att->getExpression(e_num_threads).second, global, e_num_threads );
result = new SgOmpNumThreadsClause(numThreadsParam);
break;
}
case e_device:
{
SgExpression* param = checkOmpExpressionClause( att->getExpression(e_device).second, global, e_device );
result = new SgOmpDeviceClause(param);
break;
}
case e_safelen:
{
SgExpression* param = checkOmpExpressionClause( att->getExpression(e_safelen).second, global, e_safelen );
result = new SgOmpSafelenClause(param);
break;
}
case e_simdlen:
{
SgExpression* param = checkOmpExpressionClause( att->getExpression(e_simdlen).second, global, e_simdlen );
result = new SgOmpSimdlenClause(param);
break;
}
case e_final:
{
SgExpression* Param = checkOmpExpressionClause( att->getExpression(e_final).second, global, e_final );
result = new SgOmpFinalClause(Param);
break;
}
case e_priority:
{
SgExpression* Param = checkOmpExpressionClause( att->getExpression(e_priority).second, global, e_priority );
result = new SgOmpPriorityClause(Param);
break;
}
default:
{
printf("error in buildOmpExpressionClause(): unacceptable clause type:%s\n",
OmpSupport::toString(clause_type).c_str());
ROSE_ABORT();
}
}
ROSE_ASSERT(result != NULL);
setOneSourcePositionForTransformation(result);
return result;
}
SgOmpNowaitClause * buildOmpNowaitClause(OmpAttribute* att)
{
ROSE_ASSERT(att != NULL);
if (!att->hasClause(e_nowait))
return NULL;
SgOmpNowaitClause* result = new SgOmpNowaitClause();
ROSE_ASSERT(result);
setOneSourcePositionForTransformation(result);
return result;
}
//TODO: move this builder functions to SageBuilder namespace
SgOmpEndClause * buildOmpEndClause(OmpAttribute* att)
{
ROSE_ASSERT(att != NULL);
// check if input attribute has e_end clause
if (!att->hasClause(e_end))
return NULL;
SgOmpEndClause* result = new SgOmpEndClause();
ROSE_ASSERT(result);
setOneSourcePositionForTransformation(result);
return result;
}
SgOmpBeginClause * buildOmpBeginClause(OmpAttribute* att)
{
ROSE_ASSERT(att != NULL);
// check if input attribute has e_end clause
if (!att->hasClause(e_begin))
return NULL;
SgOmpBeginClause* result = new SgOmpBeginClause();
ROSE_ASSERT(result);
setOneSourcePositionForTransformation(result);
return result;
}
// This becomes an expression clause since OpenMP 4.5
#if 0
SgOmpOrderedClause * buildOmpOrderedClause(OmpAttribute* att)
{
ROSE_ASSERT(att != NULL);
if (!att->hasClause(e_ordered_clause))
return NULL;
SgOmpOrderedClause* result = new SgOmpOrderedClause();
ROSE_ASSERT(result);
setOneSourcePositionForTransformation(result);
return result;
}
#endif
SgOmpUntiedClause * buildOmpUntiedClause(OmpAttribute* att)
{
ROSE_ASSERT(att != NULL);
if (!att->hasClause(e_untied))
return NULL;
SgOmpUntiedClause* result = new SgOmpUntiedClause();
ROSE_ASSERT(result);
setOneSourcePositionForTransformation(result);
return result;
}
SgOmpMergeableClause * buildOmpMergeableClause(OmpAttribute* att)
{
ROSE_ASSERT(att != NULL);
if (!att->hasClause(e_mergeable))
return NULL;
SgOmpMergeableClause* result = new SgOmpMergeableClause();
ROSE_ASSERT(result);
setOneSourcePositionForTransformation(result);
return result;
}
SgOmpInbranchClause * buildOmpInbranchClause(OmpAttribute* att)
{
ROSE_ASSERT(att != NULL);
if (!att->hasClause(e_inbranch))
return NULL;
SgOmpInbranchClause* result = new SgOmpInbranchClause();
ROSE_ASSERT(result);
setOneSourcePositionForTransformation(result);
return result;
}
SgOmpNotinbranchClause * buildOmpNotinbranchClause(OmpAttribute* att)
{
ROSE_ASSERT(att != NULL);
if (!att->hasClause(e_notinbranch))
return NULL;
SgOmpNotinbranchClause* result = new SgOmpNotinbranchClause();
ROSE_ASSERT(result);
setOneSourcePositionForTransformation(result);
return result;
}
//Build SgOmpScheduleClause from OmpAttribute, if any
SgOmpScheduleClause* buildOmpScheduleClause(OmpAttribute* att)
{
ROSE_ASSERT(att != NULL);
if (!att->hasClause(e_schedule))
return NULL;
// convert OmpAttribute schedule kind to SgOmpClause schedule kind
omp_construct_enum oa_kind = att->getScheduleKind();
SgOmpClause::omp_schedule_kind_enum sg_kind;
switch (oa_kind)
{
case e_schedule_static:
sg_kind = SgOmpClause::e_omp_schedule_static;
break;
case e_schedule_dynamic:
sg_kind = SgOmpClause::e_omp_schedule_dynamic;
break;
case e_schedule_guided:
sg_kind = SgOmpClause::e_omp_schedule_guided;
break;
case e_schedule_auto:
sg_kind = SgOmpClause::e_omp_schedule_auto;
break;
case e_schedule_runtime:
sg_kind = SgOmpClause::e_omp_schedule_runtime;
break;
default:
{
cerr<<"error: buildOmpScheduleClause() Unacceptable schedule kind from OmpAttribute:"
<<OmpSupport::toString(oa_kind)<<endl;
ROSE_ABORT() ;
}
}
SgExpression* chunksize_exp = att->getExpression(e_schedule).second;
// ROSE_ASSERT(chunksize_exp != NULL); // chunk size is optional
// finally build the node
SgOmpScheduleClause* result = new SgOmpScheduleClause(sg_kind, chunksize_exp);
// setOneSourcePositionForTransformation(result);
ROSE_ASSERT(result != NULL);
return result;
}
static SgOmpClause::omp_map_operator_enum toSgOmpClauseMapOperator(omp_construct_enum at_op)
{
SgOmpClause::omp_map_operator_enum result = SgOmpClause::e_omp_map_unknown;
switch (at_op)
{
case e_map_tofrom:
{
result = SgOmpClause::e_omp_map_tofrom;
break;
}
case e_map_to:
{
result = SgOmpClause::e_omp_map_to;
break;
}
case e_map_from:
{
result = SgOmpClause::e_omp_map_from;
break;
}
case e_map_alloc:
{
result = SgOmpClause::e_omp_map_alloc;
break;
}
default:
{
printf("error: unacceptable omp construct enum for map operator conversion:%s\n", OmpSupport::toString(at_op).c_str());
ROSE_ABORT();
}
}
ROSE_ASSERT(result != SgOmpClause::e_omp_map_unknown);
return result;
}
//! A helper function to convert OmpAttribute reduction operator to SgClause reduction operator
//TODO move to sageInterface?
static SgOmpClause::omp_reduction_operator_enum toSgOmpClauseReductionOperator(omp_construct_enum at_op)
{
SgOmpClause::omp_reduction_operator_enum result = SgOmpClause::e_omp_reduction_unknown;
switch (at_op)
{
case e_reduction_plus: //+
{
result = SgOmpClause::e_omp_reduction_plus;
break;
}
case e_reduction_mul: //*
{
result = SgOmpClause::e_omp_reduction_mul;
break;
}
case e_reduction_minus: // -
{
result = SgOmpClause::e_omp_reduction_minus;
break;
}
// C/C++ only
case e_reduction_bitand: // &
{
result = SgOmpClause::e_omp_reduction_bitand;
break;
}
case e_reduction_bitor: // |
{
result = SgOmpClause::e_omp_reduction_bitor;
break;
}
case e_reduction_bitxor: // ^
{
result = SgOmpClause::e_omp_reduction_bitxor;
break;
}
case e_reduction_logand: // &&
{
result = SgOmpClause::e_omp_reduction_logand;
break;
}
case e_reduction_logor: // ||
{
result = SgOmpClause::e_omp_reduction_logor;
break;
}
// fortran operator
case e_reduction_and: // .and.
{
result = SgOmpClause::e_omp_reduction_and;
break;
}
case e_reduction_or: // .or.
{
result = SgOmpClause::e_omp_reduction_or;
break;
}
case e_reduction_eqv: // fortran .eqv.
{
result = SgOmpClause::e_omp_reduction_eqv;
break;
}
case e_reduction_neqv: // fortran .neqv.
// reduction intrinsic procedure name for Fortran
{
result = SgOmpClause::e_omp_reduction_neqv;
break;
}
case e_reduction_max:
{
result = SgOmpClause::e_omp_reduction_max;
break;
}
case e_reduction_min:
{
result = SgOmpClause::e_omp_reduction_min;
break;
}
case e_reduction_iand:
{
result = SgOmpClause::e_omp_reduction_iand;
break;
}
case e_reduction_ior:
{
result = SgOmpClause::e_omp_reduction_ior;
break;
}
case e_reduction_ieor:
{
result = SgOmpClause::e_omp_reduction_ieor;
break;
}
default:
{
printf("error: unacceptable omp construct enum for reduction operator conversion:%s\n", OmpSupport::toString(at_op).c_str());
ROSE_ABORT();
}
}
ROSE_ASSERT(result != SgOmpClause::e_omp_reduction_unknown);
return result;
}
//A helper function to set SgVarRefExpPtrList from OmpAttribute's construct-varlist map
static void setClauseVariableList(SgOmpVariablesClause* target, OmpAttribute* att, omp_construct_enum key)
{
ROSE_ASSERT(target&&att);
// build variable list
std::vector<std::pair<std::string,SgNode* > > varlist = att->getVariableList(key);
#if 0
// Liao 6/10/2010 we relax this assertion to workaround
// shared(num_threads), a clause keyword is used as a variable
// we skip variable list of shared() for now so shared clause will have empty variable list
#endif
ROSE_ASSERT(varlist.size()!=0);
std::vector<std::pair<std::string,SgNode* > >::iterator iter;
for (iter = varlist.begin(); iter!= varlist.end(); iter ++)
{
// cout<<"debug setClauseVariableList: " << target <<":"<<(*iter).second->class_name() <<endl;
// We now start to use SgExpression* to store variables showing up in a varlist
if (SgInitializedName* iname = isSgInitializedName((*iter).second))
{
//target->get_variables().push_back(iname);
// Liao 1/27/2010, fix the empty parent pointer of the SgVarRefExp here
SgVarRefExp * var_ref = buildVarRefExp(iname);
target->get_variables()->get_expressions().push_back(var_ref);
var_ref->set_parent(target);
}
else if (SgPntrArrRefExp* aref= isSgPntrArrRefExp((*iter).second))
{
target->get_variables()->get_expressions().push_back(aref);
aref->set_parent(target);
}
else if (SgVarRefExp* vref = isSgVarRefExp((*iter).second))
{
target->get_variables()->get_expressions().push_back(vref);
vref->set_parent(target);
}
else
{
cerr<<"error: unhandled type of variable within a list:"<< ((*iter).second)->class_name();
ROSE_ABORT();
}
}
}
//! Try to build a reduction clause with a given operation type from OmpAttribute
SgOmpReductionClause* buildOmpReductionClause(OmpAttribute* att, omp_construct_enum reduction_op)
{
ROSE_ASSERT(att !=NULL);
if (!att->hasReductionOperator(reduction_op))
return NULL;
SgOmpClause::omp_reduction_operator_enum sg_op = toSgOmpClauseReductionOperator(reduction_op);
SgExprListExp* explist=buildExprListExp();
SgOmpReductionClause* result = new SgOmpReductionClause(explist, sg_op);
ROSE_ASSERT(result != NULL);
explist->set_parent(result);
setOneSourcePositionForTransformation(result);
// build variable list
setClauseVariableList(result, att, reduction_op);
return result;
}
//! A helper function to convert OmpAttribute depend type operator to SgClause's one
//TODO move to sageInterface?
static SgOmpClause::omp_dependence_type_enum toSgOmpClauseDependenceType(omp_construct_enum at_op)
{
SgOmpClause::omp_dependence_type_enum result = SgOmpClause::e_omp_depend_unknown;
switch (at_op)
{
case e_depend_in:
{
result = SgOmpClause::e_omp_depend_in;
break;
}
case e_depend_out:
{
result = SgOmpClause::e_omp_depend_out;
break;
}
case e_depend_inout:
{
result = SgOmpClause::e_omp_depend_inout;
break;
}
default:
{
printf("error: unacceptable omp construct enum for dependence type conversion:%s\n", OmpSupport::toString(at_op).c_str());
ROSE_ABORT();
}
}
ROSE_ASSERT(result != SgOmpClause::e_omp_depend_unknown);
return result;
}
//! Try to build a depend clause with a given operation type from OmpAttribute
SgOmpDependClause* buildOmpDependClause(OmpAttribute* att, omp_construct_enum dep_type)
{
ROSE_ASSERT(att !=NULL);
if (!att->hasDependenceType(dep_type))
return NULL;
SgOmpClause::omp_dependence_type_enum sg_op = toSgOmpClauseDependenceType(dep_type);
SgExprListExp* explist=buildExprListExp();
SgOmpDependClause* result = new SgOmpDependClause(explist, sg_op);
ROSE_ASSERT(result != NULL);
explist->set_parent(result);
setOneSourcePositionForTransformation(result);
// build variable list
setClauseVariableList(result, att, dep_type);
//this is somewhat inefficient.
// since the attribute has dimension info for all map clauses
//But we don't want to move the dimension info to directive level
result->set_array_dimensions(att->array_dimensions);
return result;
}
//! Build a map clause with a given operation type from OmpAttribute
// map may have several variants: tofrom, to, from, and alloc.
// the variables for each may have dimension info
SgOmpMapClause* buildOmpMapClause(OmpAttribute* att, omp_construct_enum map_op)
{
ROSE_ASSERT(att !=NULL);
ROSE_ASSERT (att->isMapVariant(map_op));
if (!att->hasMapVariant(map_op))
return NULL;
SgOmpClause::omp_map_operator_enum sg_op = toSgOmpClauseMapOperator(map_op);
SgExprListExp* explist=buildExprListExp();
SgOmpMapClause* result = new SgOmpMapClause(explist, sg_op);
ROSE_ASSERT(result != NULL);
setOneSourcePositionForTransformation(result);
explist->set_parent(result);
// build variable list
setClauseVariableList(result, att, map_op);
//this is somewhat inefficient.
// since the attribute has dimension info for all map clauses
//But we don't want to move the dimension info to directive level
result->set_array_dimensions(att->array_dimensions);
//A translation from OmpSupport::omp_construct_enum to SgOmpClause::omp_map_dist_data_enum is needed here.
std::map<SgSymbol*, std::vector<std::pair<OmpSupport::omp_construct_enum, SgExpression*> > > attDistMap = att->dist_data_policies;
std::map<SgSymbol*, std::vector<std::pair<OmpSupport::omp_construct_enum, SgExpression*> > >::iterator iter;
std::map<SgSymbol*, std::vector<std::pair<SgOmpClause::omp_map_dist_data_enum, SgExpression*> > > convertedDistMap;
for (iter= attDistMap.begin(); iter!=attDistMap.end(); iter++)
{
SgSymbol* s = (*iter).first;
std::vector<std::pair<OmpSupport::omp_construct_enum, SgExpression*> > src_vec = (*iter).second;
std::vector<std::pair<OmpSupport::omp_construct_enum, SgExpression*> >::iterator iter2;