@@ -196,7 +196,33 @@ void OslScopBuilder::buildScopStmtMap(mlir::FuncOp f,
196
196
void OslScopBuilder::buildScopContext (OslScop *scop,
197
197
OslScop::ScopStmtMap *scopStmtMap,
198
198
FlatAffineValueConstraints &ctx) const {
199
- ctx.reset ();
199
+ LLVM_DEBUG (dbgs () << " --- Building SCoP context ...\n " );
200
+
201
+ // First initialize the symbols of the ctx by the order of arg number.
202
+ // This simply aims to make mergeAndAlignIdsWithOthers work.
203
+ SmallVector<Value> symbols;
204
+ for (const auto &it : *scopStmtMap) {
205
+ auto domain = it.second .getDomain ();
206
+ SmallVector<Value> syms;
207
+ domain->getValues (domain->getNumDimIds (), domain->getNumDimAndSymbolIds (),
208
+ &syms);
209
+
210
+ for (Value sym : syms) {
211
+ // Find the insertion position.
212
+ auto it = symbols.begin ();
213
+ while (it != symbols.end ()) {
214
+ auto lhs = it->cast <BlockArgument>();
215
+ auto rhs = sym.cast <BlockArgument>();
216
+ if (lhs.getArgNumber () >= rhs.getArgNumber ())
217
+ break ;
218
+ ++it;
219
+ }
220
+ if (*it != sym)
221
+ symbols.insert (it, sym);
222
+ }
223
+ }
224
+ ctx.reset (/* numDims=*/ 0 , /* numSymbols=*/ symbols.size ());
225
+ ctx.setValues (0 , symbols.size (), symbols);
200
226
201
227
// Union with the domains of all Scop statements. We first merge and align the
202
228
// IDs of the context and the domain of the scop statement, and then append
@@ -210,6 +236,31 @@ void OslScopBuilder::buildScopContext(OslScop *scop,
210
236
ctx.mergeAndAlignIdsWithOther (0 , &cst);
211
237
ctx.append (cst);
212
238
ctx.removeRedundantConstraints ();
239
+
240
+ LLVM_DEBUG (dbgs () << " Statement:\n " );
241
+ LLVM_DEBUG (it.second .getCaller ().dump ());
242
+ LLVM_DEBUG (it.second .getCallee ().dump ());
243
+ LLVM_DEBUG (dbgs () << " Target domain: \n " );
244
+ LLVM_DEBUG (domain->dump ());
245
+
246
+ LLVM_DEBUG ({
247
+ dbgs () << " Domain values: \n " ;
248
+ SmallVector<Value> values;
249
+ domain->getValues (0 , domain->getNumDimAndSymbolIds (), &values);
250
+ for (Value value : values)
251
+ dbgs () << " * " << value << ' \n ' ;
252
+ });
253
+
254
+ LLVM_DEBUG (dbgs () << " Updated context: \n " );
255
+ LLVM_DEBUG (ctx.dump ());
256
+
257
+ LLVM_DEBUG ({
258
+ dbgs () << " Context values: \n " ;
259
+ SmallVector<Value> values;
260
+ ctx.getValues (0 , ctx.getNumDimAndSymbolIds (), &values);
261
+ for (Value value : values)
262
+ dbgs () << " * " << value << ' \n ' ;
263
+ });
213
264
}
214
265
215
266
// Then, create the single context relation in scop.
@@ -221,19 +272,43 @@ void OslScopBuilder::buildScopContext(OslScop *scop,
221
272
SmallVector<mlir::Value, 8 > symValues;
222
273
ctx.getValues (ctx.getNumDimIds (), ctx.getNumDimAndSymbolIds (), &symValues);
223
274
275
+ // Add and align domain SYMBOL columns.
224
276
for (const auto &it : *scopStmtMap) {
225
277
FlatAffineValueConstraints *domain = it.second .getDomain ();
278
+ // For any symbol missing in the domain, add them directly to the end.
279
+ for (unsigned i = 0 ; i < ctx.getNumSymbolIds (); ++i) {
280
+ unsigned pos;
281
+ if (!domain->findId (symValues[i], &pos)) // insert to the back
282
+ domain->appendSymbolId (symValues[i]);
283
+ else
284
+ LLVM_DEBUG (dbgs () << " Found " << symValues[i] << ' \n ' );
285
+ }
226
286
287
+ // Then do the aligning.
288
+ LLVM_DEBUG (domain->dump ());
227
289
for (unsigned i = 0 ; i < ctx.getNumSymbolIds (); i++) {
228
290
mlir::Value sym = symValues[i];
229
291
unsigned pos;
230
- if (domain->findId (sym, &pos)) {
231
- if (pos != i + domain-> getNumDimIds ())
232
- domain-> swapId ( i + domain->getNumDimIds (), pos );
233
- } else {
234
- domain-> insertSymbolId (i, sym);
235
- }
292
+ assert (domain->findId (sym, &pos));
293
+
294
+ unsigned posAsCtx = i + domain->getNumDimIds ();
295
+ LLVM_DEBUG ( dbgs () << " Swapping " << posAsCtx << " " << pos << " \n " );
296
+ if (pos != posAsCtx)
297
+ domain-> swapId (posAsCtx, pos);
236
298
}
299
+
300
+ // for (unsigned i = 0; i < ctx.getNumSymbolIds(); i++) {
301
+ // mlir::Value sym = symValues[i];
302
+ // unsigned pos;
303
+ // // If the symbol can be found in the domain, we put it in the same
304
+ // // position as the ctx.
305
+ // if (domain->findId(sym, &pos)) {
306
+ // if (pos != i + domain->getNumDimIds())
307
+ // domain->swapId(i + domain->getNumDimIds(), pos);
308
+ // } else {
309
+ // domain->insertSymbolId(i, sym);
310
+ // }
311
+ // }
237
312
}
238
313
}
239
314
0 commit comments