@@ -205,6 +205,8 @@ class FlatLinearConstraints : public presburger::IntegerPolyhedron {
205
205
// / where each non-local variable can have an SSA Value attached to it.
206
206
class FlatLinearValueConstraints : public FlatLinearConstraints {
207
207
public:
208
+ using Identifier = presburger::Identifier;
209
+
208
210
// / Constructs a constraint system reserving memory for the specified number
209
211
// / of constraints and variables. `valArgs` are the optional SSA values
210
212
// / associated with each dimension/symbol. These must either be empty or match
@@ -217,11 +219,12 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
217
219
: FlatLinearConstraints(numReservedInequalities, numReservedEqualities,
218
220
numReservedCols, numDims, numSymbols, numLocals) {
219
221
assert (valArgs.empty () || valArgs.size () == getNumDimAndSymbolVars ());
220
- values.reserve (numReservedCols);
221
- if (valArgs.empty ())
222
- values.resize (getNumDimAndSymbolVars (), std::nullopt);
223
- else
224
- values.append (valArgs.begin (), valArgs.end ());
222
+ // Use values in space for FlatLinearValueConstraints.
223
+ space.resetIds ();
224
+ // Set the values for the non-local variables.
225
+ for (unsigned i = 0 , e = valArgs.size (); i < e; ++i)
226
+ if (valArgs[i])
227
+ setValue (i, *valArgs[i]);
225
228
}
226
229
227
230
// / Constructs a constraint system reserving memory for the specified number
@@ -236,11 +239,12 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
236
239
: FlatLinearConstraints(numReservedInequalities, numReservedEqualities,
237
240
numReservedCols, numDims, numSymbols, numLocals) {
238
241
assert (valArgs.empty () || valArgs.size () == getNumDimAndSymbolVars ());
239
- values.reserve (numReservedCols);
240
- if (valArgs.empty ())
241
- values.resize (getNumDimAndSymbolVars (), std::nullopt);
242
- else
243
- values.append (valArgs.begin (), valArgs.end ());
242
+ // Use values in space for FlatLinearValueConstraints.
243
+ space.resetIds ();
244
+ // Set the values for the non-local variables.
245
+ for (unsigned i = 0 , e = valArgs.size (); i < e; ++i)
246
+ if (valArgs[i])
247
+ setValue (i, valArgs[i]);
244
248
}
245
249
246
250
// / Constructs a constraint system with the specified number of dimensions
@@ -273,10 +277,12 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
273
277
ArrayRef<std::optional<Value>> valArgs = {})
274
278
: FlatLinearConstraints(fac) {
275
279
assert (valArgs.empty () || valArgs.size () == getNumDimAndSymbolVars ());
276
- if (valArgs.empty ())
277
- values.resize (getNumDimAndSymbolVars (), std::nullopt);
278
- else
279
- values.append (valArgs.begin (), valArgs.end ());
280
+ // Use values in space for FlatLinearValueConstraints.
281
+ space.resetIds ();
282
+ // Set the values for the non-local variables.
283
+ for (unsigned i = 0 , e = valArgs.size (); i < e; ++i)
284
+ if (valArgs[i])
285
+ setValue (i, *valArgs[i]);
280
286
}
281
287
282
288
// / Creates an affine constraint system from an IntegerSet.
@@ -290,9 +296,6 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
290
296
cst->getKind () <= Kind::FlatAffineRelation;
291
297
}
292
298
293
- // / Replaces the contents of this FlatLinearValueConstraints with `other`.
294
- void clearAndCopyFrom (const IntegerRelation &other) override ;
295
-
296
299
// / Adds a constant bound for the variable associated with the given Value.
297
300
void addBound (presburger::BoundType type, Value val, int64_t value);
298
301
using FlatLinearConstraints::addBound;
@@ -302,7 +305,9 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
302
305
inline Value getValue (unsigned pos) const {
303
306
assert (pos < getNumDimAndSymbolVars () && " Invalid position" );
304
307
assert (hasValue (pos) && " variable's Value not set" );
305
- return *values[pos];
308
+ VarKind kind = getVarKindAt (pos);
309
+ unsigned relativePos = pos - getVarKindOffset (kind);
310
+ return space.getId (kind, relativePos).getValue <Value>();
306
311
}
307
312
308
313
// / Returns the Values associated with variables in range [start, end).
@@ -313,25 +318,48 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
313
318
assert (start <= end && " invalid start position" );
314
319
values->clear ();
315
320
values->reserve (end - start);
316
- for (unsigned i = start; i < end; i++ )
321
+ for (unsigned i = start; i < end; ++i )
317
322
values->push_back (getValue (i));
318
323
}
319
324
320
- inline ArrayRef<std::optional<Value>> getMaybeValues () const {
321
- return {values.data (), values.size ()};
325
+ inline SmallVector<std::optional<Value>> getMaybeValues () const {
326
+ SmallVector<std::optional<Value>> maybeValues;
327
+ maybeValues.reserve (getNumDimAndSymbolVars ());
328
+ for (unsigned i = 0 , e = getNumDimAndSymbolVars (); i < e; ++i) {
329
+ if (hasValue (i))
330
+ maybeValues.push_back (getValue (i));
331
+ else
332
+ maybeValues.push_back (std::nullopt);
333
+ }
334
+ return maybeValues;
322
335
}
323
336
324
- inline ArrayRef<std::optional<Value>>
325
- getMaybeValues (presburger::VarKind kind) const {
326
- assert (kind != VarKind::Local &&
327
- " Local variables do not have any value attached to them." );
328
- return {values.data () + getVarKindOffset (kind), getNumVarKind (kind)};
329
- }
337
+ inline SmallVector<std::optional<Value>>
338
+ getMaybeValues (presburger::VarKind kind) const {
339
+ assert (kind != VarKind::Local &&
340
+ " Local variables do not have any value attached to them." );
341
+ SmallVector<std::optional<Value>> maybeValues;
342
+ maybeValues.reserve (getNumVarKind (kind));
343
+ for (unsigned i = 0 , e = getNumVarKind (kind); i < e; ++i) {
344
+ const Identifier id = space.getId (kind, i);
345
+ if (id.hasValue ())
346
+ maybeValues.push_back (id.getValue <Value>());
347
+ else
348
+ maybeValues.push_back (std::nullopt);
349
+ }
350
+ return maybeValues;
351
+ }
330
352
331
353
// / Returns true if the pos^th variable has an associated Value.
332
354
inline bool hasValue (unsigned pos) const {
333
355
assert (pos < getNumDimAndSymbolVars () && " Invalid position" );
334
- return values[pos].has_value ();
356
+ VarKind kind = getVarKindAt (pos);
357
+ unsigned relativePos = pos - getVarKindOffset (kind);
358
+ return space.getId (kind, relativePos).hasValue ();
359
+ }
360
+
361
+ void resetValues () {
362
+ space.resetIds ();
335
363
}
336
364
337
365
unsigned appendDimVar (ValueRange vals);
@@ -360,7 +388,9 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
360
388
// / Sets the Value associated with the pos^th variable.
361
389
inline void setValue (unsigned pos, Value val) {
362
390
assert (pos < getNumDimAndSymbolVars () && " invalid var position" );
363
- values[pos] = val;
391
+ VarKind kind = getVarKindAt (pos);
392
+ unsigned relativePos = pos - getVarKindOffset (kind);
393
+ space.getId (kind, relativePos) = presburger::Identifier (val);
364
394
}
365
395
366
396
// / Sets the Values associated with the variables in the range [start, end).
@@ -387,9 +417,6 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
387
417
void projectOut (Value val);
388
418
using IntegerPolyhedron::projectOut;
389
419
390
- // / Swap the posA^th variable with the posB^th variable.
391
- void swapVar (unsigned posA, unsigned posB) override ;
392
-
393
420
// / Prints the number of constraints, dimensions, symbols and locals in the
394
421
// / FlatAffineValueConstraints. Also, prints for each variable whether there
395
422
// / is an SSA Value attached to it.
@@ -444,28 +471,6 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
444
471
// / output = {0 <= d0 <= 6, 1 <= d1 <= 15}
445
472
LogicalResult unionBoundingBox (const FlatLinearValueConstraints &other);
446
473
using IntegerPolyhedron::unionBoundingBox;
447
-
448
- protected:
449
- // / Eliminates the variable at the specified position using Fourier-Motzkin
450
- // / variable elimination, but uses Gaussian elimination if there is an
451
- // / equality involving that variable. If the result of the elimination is
452
- // / integer exact, `*isResultIntegerExact` is set to true. If `darkShadow` is
453
- // / set to true, a potential under approximation (subset) of the rational
454
- // / shadow / exact integer shadow is computed.
455
- // See implementation comments for more details.
456
- void fourierMotzkinEliminate (unsigned pos, bool darkShadow = false ,
457
- bool *isResultIntegerExact = nullptr ) override ;
458
-
459
- // / Returns false if the fields corresponding to various variable counts, or
460
- // / equality/inequality buffer sizes aren't consistent; true otherwise. This
461
- // / is meant to be used within an assert internally.
462
- bool hasConsistentState () const override ;
463
-
464
- // / Values corresponding to the (column) non-local variables of this
465
- // / constraint system appearing in the order the variables correspond to
466
- // / columns. Variables that aren't associated with any Value are set to
467
- // / std::nullopt.
468
- SmallVector<std::optional<Value>, 8 > values;
469
474
};
470
475
471
476
// / Flattens 'expr' into 'flattenedExpr', which contains the coefficients of the
0 commit comments