@@ -67,7 +67,7 @@ namespace xlscc {
67
67
68
68
absl::Status Translator::GenerateIR_Loop (
69
69
bool always_first_iter, const clang::Stmt* loop_stmt,
70
- clang::ArrayRef<const clang::Attr *> attrs, const clang::Stmt* init,
70
+ clang::ArrayRef<const clang::AnnotateAttr *> attrs, const clang::Stmt* init,
71
71
const clang::Expr* cond_expr, const clang::Stmt* inc,
72
72
const clang::Stmt* body, const clang::PresumedLoc& presumed_loc,
73
73
const xls::SourceInfo& loc, clang::ASTContext& ctx) {
@@ -79,54 +79,33 @@ absl::Status Translator::GenerateIR_Loop(
79
79
}
80
80
}
81
81
82
+ bool is_asap = HasAnnotation (attrs, " xlscc_asap" );
83
+
82
84
int64_t init_interval = -1 ;
85
+ XLS_ASSIGN_OR_RETURN (std::optional<int64_t > init_interval_optional,
86
+ GetAnnotationWithNonNegativeIntegerParam (
87
+ attrs, " hls_pipeline_init_interval" , loc, ctx));
88
+
89
+ if (init_interval_optional.has_value ()) {
90
+ init_interval = init_interval_optional.value ();
91
+ } else {
92
+ // Pipelined loops can inherit their initiation interval from enclosing
93
+ // loops, so they can be allowed not to have a #pragma.
94
+ CHECK (!context ().in_pipelined_for_body ||
95
+ (context ().outer_pipelined_loop_init_interval > 0 ));
96
+ init_interval = context ().outer_pipelined_loop_init_interval ;
97
+ }
98
+
83
99
int64_t unroll_factor = context ().for_loops_default_unroll
84
100
? std::numeric_limits<int64_t >::max ()
85
101
: 0 ;
86
- bool is_asap = false ;
87
- for (const clang::Attr* attr : attrs) {
88
- if (const clang::AnnotateAttr* annotate =
89
- llvm::dyn_cast<clang::AnnotateAttr>(attr);
90
- annotate != nullptr ) {
91
- if (annotate->getAnnotation () == " hls_unroll" ) {
92
- if (annotate->args_size () == 0 ) {
93
- unroll_factor = std::numeric_limits<int64_t >::max ();
94
- } else if (annotate->args_size () > 1 ) {
95
- return absl::InvalidArgumentError (
96
- " hls_unroll must have at most one argument" );
97
- } else if (clang::Expr::EvalResult result;
98
- (*annotate->args ().begin ())->EvaluateAsInt (result, ctx) &&
99
- result.Val .getInt ().isStrictlyPositive () &&
100
- result.Val .getInt ().isRepresentableByInt64 ()) {
101
- unroll_factor = result.Val .getInt ().getExtValue ();
102
- } else {
103
- return absl::InvalidArgumentError (
104
- " hls_unroll argument (if provided) must be a positive integer" );
105
- }
106
- continue ;
107
- }
108
-
109
- if (annotate->getAnnotation () == " hls_pipeline_init_interval" ) {
110
- if (annotate->args_size () != 1 ) {
111
- return absl::InvalidArgumentError (
112
- " hls_pipeline_init_interval must have exactly one argument" );
113
- }
114
- clang::Expr::EvalResult result;
115
- if (!(*annotate->args ().begin ())->EvaluateAsInt (result, ctx) ||
116
- !result.Val .getInt ().isStrictlyPositive () ||
117
- !result.Val .getInt ().isRepresentableByInt64 ()) {
118
- return absl::InvalidArgumentError (
119
- " the argument to the 'hls_pipeline_init_interval' attribute must "
120
- " be an integer >= 1" );
121
- }
122
- init_interval = result.Val .getInt ().getExtValue ();
123
- continue ;
124
- }
102
+ XLS_ASSIGN_OR_RETURN (
103
+ std::optional<int64_t > unroll_factor_optional,
104
+ GetAnnotationWithNonNegativeIntegerParam (attrs, " hls_unroll" , loc, ctx,
105
+ /* default_value=*/ 1 ));
125
106
126
- if (annotate->getAnnotation () == " xlscc_asap" ) {
127
- is_asap = true ;
128
- }
129
- }
107
+ if (unroll_factor_optional.has_value ()) {
108
+ unroll_factor = unroll_factor_optional.value ();
130
109
}
131
110
132
111
if (unroll_factor > 0 ) {
@@ -141,14 +120,6 @@ absl::Status Translator::GenerateIR_Loop(
141
120
body, ctx, loc);
142
121
}
143
122
144
- // Pipelined loops can inherit their initiation interval from enclosing
145
- // loops, so they can be allowed not to have a #pragma.
146
- if (init_interval < 0 ) {
147
- CHECK (!context ().in_pipelined_for_body ||
148
- (context ().outer_pipelined_loop_init_interval > 0 ));
149
- init_interval = context ().outer_pipelined_loop_init_interval ;
150
- }
151
-
152
123
if (init_interval <= 0 ) {
153
124
return absl::UnimplementedError (
154
125
ErrorMessage (loc, " Loop statement missing #pragma or attribute" ));
0 commit comments