| 1 |
//===- Utils.h - Utils for Presburger Tests ---------------------*- C++ -*-===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
// |
| 9 |
// This file defines helper functions for Presburger unittests. |
| 10 |
// |
| 11 |
//===----------------------------------------------------------------------===// |
| 12 |
|
| 13 |
#ifndef MLIR_UNITTESTS_ANALYSIS_PRESBURGER_UTILS_H |
| 14 |
#define MLIR_UNITTESTS_ANALYSIS_PRESBURGER_UTILS_H |
| 15 |
|
| 16 |
#include "mlir/Analysis/Presburger/IntegerRelation.h" |
| 17 |
#include "mlir/Analysis/Presburger/PWMAFunction.h" |
| 18 |
#include "mlir/Analysis/Presburger/PresburgerRelation.h" |
| 19 |
#include "mlir/Analysis/Presburger/Simplex.h" |
| 20 |
#include "mlir/IR/MLIRContext.h" |
| 21 |
#include "mlir/Support/LLVM.h" |
| 22 |
|
| 23 |
#include <gtest/gtest.h> |
| 24 |
#include <optional> |
| 25 |
|
| 26 |
namespace mlir { |
| 27 |
namespace presburger { |
| 28 |
|
| 29 |
inline Matrix makeMatrix(unsigned numRow, unsigned numColumns, |
| 30 |
ArrayRef<SmallVector<int64_t, 8>> matrix) { |
| 31 |
Matrix results(numRow, numColumns); |
| 32 |
assert(matrix.size() == numRow); |
| 33 |
for (unsigned i = 0; i < numRow; ++i) { |
| 34 |
assert(matrix[i].size() == numColumns && |
| 35 |
"Output expression has incorrect dimensionality!"); |
| 36 |
for (unsigned j = 0; j < numColumns; ++j) |
| 37 |
results(i, j) = matrix[i][j]; |
| 38 |
} |
| 39 |
return results; |
| 40 |
} |
| 41 |
|
| 42 |
/// lhs and rhs represent non-negative integers or positive infinity. The |
| 43 |
/// infinity case corresponds to when the Optional is empty. |
| 44 |
inline bool infinityOrUInt64LE(std::optional<MPInt> lhs, |
| 45 |
std::optional<MPInt> rhs) { |
| 46 |
// No constraint. |
| 47 |
if (!rhs) |
| 48 |
return true; |
| 49 |
// Finite rhs provided so lhs has to be finite too. |
| 50 |
if (!lhs) |
| 51 |
return false; |
| 52 |
return *lhs <= *rhs; |
| 53 |
} |
| 54 |
|
| 55 |
/// Expect that the computed volume is a valid overapproximation of |
| 56 |
/// the true volume `trueVolume`, while also being at least as good an |
| 57 |
/// approximation as `resultBound`. |
| 58 |
inline void expectComputedVolumeIsValidOverapprox( |
| 59 |
const std::optional<MPInt> &computedVolume, |
| 60 |
const std::optional<MPInt> &trueVolume, |
| 61 |
const std::optional<MPInt> &resultBound) { |
| 62 |
assert(infinityOrUInt64LE(trueVolume, resultBound) && |
| 63 |
"can't expect result to be less than the true volume"); |
| 64 |
EXPECT_TRUE(infinityOrUInt64LE(trueVolume, computedVolume)); |
| 65 |
EXPECT_TRUE(infinityOrUInt64LE(computedVolume, resultBound)); |
| 66 |
} |
| 67 |
|
| 68 |
inline void expectComputedVolumeIsValidOverapprox( |
| 69 |
const std::optional<MPInt> &computedVolume, |
| 70 |
std::optional<int64_t> trueVolume, std::optional<int64_t> resultBound) { |
| 71 |
expectComputedVolumeIsValidOverapprox( |
| 72 |
computedVolume, llvm::transformOptional(trueVolume, mpintFromInt64), |
| 73 |
llvm::transformOptional(resultBound, mpintFromInt64)); |
| 74 |
} |
| 75 |
|
| 76 |
} // namespace presburger |
| 77 |
} // namespace mlir |
| 78 |
|
| 79 |
#endif // MLIR_UNITTESTS_ANALYSIS_PRESBURGER_UTILS_H |