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
|
//===- PresburgerSpaceTest.cpp - Tests for PresburgerSpace ----------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "mlir/Analysis/Presburger/PresburgerSpace.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
using namespace mlir;
using namespace presburger;
TEST(PresburgerSpaceTest, insertId) {
PresburgerSpace space = PresburgerSpace::getRelationSpace(2, 2, 1);
// Try inserting 2 domain ids.
space.insertVar(VarKind::Domain, 0, 2);
EXPECT_EQ(space.getNumDomainVars(), 4u);
// Try inserting 1 range ids.
space.insertVar(VarKind::Range, 0, 1);
EXPECT_EQ(space.getNumRangeVars(), 3u);
}
TEST(PresburgerSpaceTest, insertIdSet) {
PresburgerSpace space = PresburgerSpace::getSetSpace(2, 1);
// Try inserting 2 dimension ids. The space should have 4 range ids since
// spaces which do not distinguish between domain, range are implemented like
// this.
space.insertVar(VarKind::SetDim, 0, 2);
EXPECT_EQ(space.getNumRangeVars(), 4u);
}
TEST(PresburgerSpaceTest, removeIdRange) {
PresburgerSpace space = PresburgerSpace::getRelationSpace(2, 1, 3);
// Remove 1 domain identifier.
space.removeVarRange(VarKind::Domain, 0, 1);
EXPECT_EQ(space.getNumDomainVars(), 1u);
// Remove 1 symbol and 1 range identifier.
space.removeVarRange(VarKind::Symbol, 0, 1);
space.removeVarRange(VarKind::Range, 0, 1);
EXPECT_EQ(space.getNumDomainVars(), 1u);
EXPECT_EQ(space.getNumRangeVars(), 0u);
EXPECT_EQ(space.getNumSymbolVars(), 2u);
}
TEST(PresburgerSpaceTest, insertVarIdentifier) {
PresburgerSpace space = PresburgerSpace::getRelationSpace(2, 2, 1, 0);
int identifiers[2] = {0, 1};
// Attach identifiers to domain ids.
space.setId(VarKind::Domain, 0, Identifier(&identifiers[0]));
space.setId(VarKind::Domain, 1, Identifier(&identifiers[1]));
// Try inserting 2 domain ids.
space.insertVar(VarKind::Domain, 0, 2);
EXPECT_EQ(space.getNumDomainVars(), 4u);
// Try inserting 1 range ids.
space.insertVar(VarKind::Range, 0, 1);
EXPECT_EQ(space.getNumRangeVars(), 3u);
// Check if the identifiers for the old ids are still attached properly.
EXPECT_EQ(space.getId(VarKind::Domain, 2), Identifier(&identifiers[0]));
EXPECT_EQ(space.getId(VarKind::Domain, 3), Identifier(&identifiers[1]));
}
TEST(PresburgerSpaceTest, removeVarRangeIdentifier) {
PresburgerSpace space = PresburgerSpace::getRelationSpace(2, 1, 3, 0);
int identifiers[6] = {0, 1, 2, 3, 4, 5};
// Attach identifiers to domain identifiers.
space.setId(VarKind::Domain, 0, Identifier(&identifiers[0]));
space.setId(VarKind::Domain, 1, Identifier(&identifiers[1]));
// Attach identifiers to range identifiers.
space.setId(VarKind::Range, 0, Identifier(&identifiers[2]));
// Attach identifiers to symbol identifiers.
space.setId(VarKind::Symbol, 0, Identifier(&identifiers[3]));
space.setId(VarKind::Symbol, 1, Identifier(&identifiers[4]));
space.setId(VarKind::Symbol, 2, Identifier(&identifiers[5]));
// Remove 1 domain identifier.
space.removeVarRange(VarKind::Domain, 0, 1);
EXPECT_EQ(space.getNumDomainVars(), 1u);
// Remove 1 symbol and 1 range identifier.
space.removeVarRange(VarKind::Symbol, 0, 1);
space.removeVarRange(VarKind::Range, 0, 1);
EXPECT_EQ(space.getNumDomainVars(), 1u);
EXPECT_EQ(space.getNumRangeVars(), 0u);
EXPECT_EQ(space.getNumSymbolVars(), 2u);
// Check if domain identifiers are attached properly.
EXPECT_EQ(space.getId(VarKind::Domain, 0), Identifier(&identifiers[1]));
// Check if symbol identifiers are attached properly.
EXPECT_EQ(space.getId(VarKind::Range, 0), Identifier(&identifiers[4]));
EXPECT_EQ(space.getId(VarKind::Range, 1), Identifier(&identifiers[5]));
}
TEST(PresburgerSpaceTest, IdentifierIsEqual) {
PresburgerSpace space = PresburgerSpace::getRelationSpace(1, 2, 0, 0);
int identifiers[2] = {0, 1};
space.setId(VarKind::Domain, 0, Identifier(&identifiers[0]));
space.setId(VarKind::Range, 0, Identifier(&identifiers[0]));
space.setId(VarKind::Range, 1, Identifier(&identifiers[1]));
EXPECT_EQ(space.getId(VarKind::Domain, 0), space.getId(VarKind::Range, 0));
EXPECT_FALSE(
space.getId(VarKind::Range, 0).isEqual(space.getId(VarKind::Range, 1)));
}
TEST(PresburgerSpaceTest, convertVarKind) {
PresburgerSpace space = PresburgerSpace::getRelationSpace(2, 2, 0, 0);
// Attach identifiers.
int identifiers[4] = {0, 1, 2, 3};
space.setId(VarKind::Domain, 0, Identifier(&identifiers[0]));
space.setId(VarKind::Domain, 1, Identifier(&identifiers[1]));
space.setId(VarKind::Range, 0, Identifier(&identifiers[2]));
space.setId(VarKind::Range, 1, Identifier(&identifiers[3]));
// Convert Range variables to symbols.
space.convertVarKind(VarKind::Range, 0, 2, VarKind::Symbol, 0);
// Check if the identifiers are moved to symbols.
EXPECT_EQ(space.getId(VarKind::Symbol, 0), Identifier(&identifiers[2]));
EXPECT_EQ(space.getId(VarKind::Symbol, 1), Identifier(&identifiers[3]));
// Convert 1 symbol to range identifier.
space.convertVarKind(VarKind::Symbol, 1, 1, VarKind::Range, 0);
// Check if the identifier is moved to range.
EXPECT_EQ(space.getId(VarKind::Range, 0), Identifier(&identifiers[3]));
}
TEST(PresburgerSpaceTest, convertVarKindLocals) {
PresburgerSpace space = PresburgerSpace::getRelationSpace(2, 2, 0, 0);
// Attach identifiers to range variables.
int identifiers[4] = {0, 1};
space.setId(VarKind::Range, 0, Identifier(&identifiers[0]));
space.setId(VarKind::Range, 1, Identifier(&identifiers[1]));
// Convert Range variables to locals i.e. project them out.
space.convertVarKind(VarKind::Range, 0, 2, VarKind::Local, 0);
// Check if the variables were moved.
EXPECT_EQ(space.getNumVarKind(VarKind::Range), 0u);
EXPECT_EQ(space.getNumVarKind(VarKind::Local), 2u);
// Convert the Local variables back to Range variables.
space.convertVarKind(VarKind::Local, 0, 2, VarKind::Range, 0);
// The identifier information should be lost.
EXPECT_FALSE(space.getId(VarKind::Range, 0).hasValue());
EXPECT_FALSE(space.getId(VarKind::Range, 1).hasValue());
}
TEST(PresburgerSpaceTest, convertVarKind2) {
PresburgerSpace space = PresburgerSpace::getRelationSpace(0, 2, 2, 0);
// Attach identifiers.
int identifiers[4] = {0, 1, 2, 3};
space.setId(VarKind::Range, 0, Identifier(&identifiers[0]));
space.setId(VarKind::Range, 1, Identifier(&identifiers[1]));
space.setId(VarKind::Symbol, 0, Identifier(&identifiers[2]));
space.setId(VarKind::Symbol, 1, Identifier(&identifiers[3]));
// Convert Range variables to symbols.
space.convertVarKind(VarKind::Range, 0, 2, VarKind::Symbol, 1);
// Check if the identifiers are moved to symbols.
EXPECT_EQ(space.getId(VarKind::Symbol, 0), Identifier(&identifiers[2]));
EXPECT_EQ(space.getId(VarKind::Symbol, 1), Identifier(&identifiers[0]));
EXPECT_EQ(space.getId(VarKind::Symbol, 2), Identifier(&identifiers[1]));
EXPECT_EQ(space.getId(VarKind::Symbol, 3), Identifier(&identifiers[3]));
}
TEST(PresburgerSpaceTest, mergeAndAlignSymbols) {
PresburgerSpace space = PresburgerSpace::getRelationSpace(3, 3, 2, 0);
PresburgerSpace otherSpace = PresburgerSpace::getRelationSpace(3, 2, 3, 0);
// Attach identifiers.
int identifiers[7] = {0, 1, 2, 3, 4, 5, 6};
int otherIdentifiers[8] = {10, 11, 12, 13, 14, 15, 16, 17};
space.setId(VarKind::Domain, 0, Identifier(&identifiers[0]));
space.setId(VarKind::Domain, 1, Identifier(&identifiers[1]));
// Note the common identifier.
space.setId(VarKind::Domain, 2, Identifier(&otherIdentifiers[2]));
space.setId(VarKind::Range, 0, Identifier(&identifiers[2]));
space.setId(VarKind::Range, 1, Identifier(&identifiers[3]));
space.setId(VarKind::Range, 2, Identifier(&identifiers[4]));
space.setId(VarKind::Symbol, 0, Identifier(&identifiers[5]));
space.setId(VarKind::Symbol, 1, Identifier(&identifiers[6]));
otherSpace.setId(VarKind::Domain, 0, Identifier(&otherIdentifiers[0]));
otherSpace.setId(VarKind::Domain, 1, Identifier(&otherIdentifiers[1]));
otherSpace.setId(VarKind::Domain, 2, Identifier(&otherIdentifiers[2]));
otherSpace.setId(VarKind::Range, 0, Identifier(&otherIdentifiers[3]));
otherSpace.setId(VarKind::Range, 1, Identifier(&otherIdentifiers[4]));
// Note the common identifier.
otherSpace.setId(VarKind::Symbol, 0, Identifier(&identifiers[6]));
otherSpace.setId(VarKind::Symbol, 1, Identifier(&otherIdentifiers[5]));
otherSpace.setId(VarKind::Symbol, 2, Identifier(&otherIdentifiers[7]));
space.mergeAndAlignSymbols(otherSpace);
// Check if merge & align is successful.
// Check symbol var identifiers.
EXPECT_EQ(4u, space.getNumSymbolVars());
EXPECT_EQ(4u, otherSpace.getNumSymbolVars());
EXPECT_EQ(space.getId(VarKind::Symbol, 0), Identifier(&identifiers[5]));
EXPECT_EQ(space.getId(VarKind::Symbol, 1), Identifier(&identifiers[6]));
EXPECT_EQ(space.getId(VarKind::Symbol, 2), Identifier(&otherIdentifiers[5]));
EXPECT_EQ(space.getId(VarKind::Symbol, 3), Identifier(&otherIdentifiers[7]));
EXPECT_EQ(otherSpace.getId(VarKind::Symbol, 0), Identifier(&identifiers[5]));
EXPECT_EQ(otherSpace.getId(VarKind::Symbol, 1), Identifier(&identifiers[6]));
EXPECT_EQ(otherSpace.getId(VarKind::Symbol, 2),
Identifier(&otherIdentifiers[5]));
EXPECT_EQ(otherSpace.getId(VarKind::Symbol, 3),
Identifier(&otherIdentifiers[7]));
// Check that domain and range var identifiers are not affected.
EXPECT_EQ(3u, space.getNumDomainVars());
EXPECT_EQ(3u, space.getNumRangeVars());
EXPECT_EQ(space.getId(VarKind::Domain, 0), Identifier(&identifiers[0]));
EXPECT_EQ(space.getId(VarKind::Domain, 1), Identifier(&identifiers[1]));
EXPECT_EQ(space.getId(VarKind::Domain, 2), Identifier(&otherIdentifiers[2]));
EXPECT_EQ(space.getId(VarKind::Range, 0), Identifier(&identifiers[2]));
EXPECT_EQ(space.getId(VarKind::Range, 1), Identifier(&identifiers[3]));
EXPECT_EQ(space.getId(VarKind::Range, 2), Identifier(&identifiers[4]));
EXPECT_EQ(3u, otherSpace.getNumDomainVars());
EXPECT_EQ(2u, otherSpace.getNumRangeVars());
EXPECT_EQ(otherSpace.getId(VarKind::Domain, 0),
Identifier(&otherIdentifiers[0]));
EXPECT_EQ(otherSpace.getId(VarKind::Domain, 1),
Identifier(&otherIdentifiers[1]));
EXPECT_EQ(otherSpace.getId(VarKind::Domain, 2),
Identifier(&otherIdentifiers[2]));
EXPECT_EQ(otherSpace.getId(VarKind::Range, 0),
Identifier(&otherIdentifiers[3]));
EXPECT_EQ(otherSpace.getId(VarKind::Range, 1),
Identifier(&otherIdentifiers[4]));
}
|