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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
|
#include "Halide.h"
#include "check_call_graphs.h"
#include <cstdio>
#include <functional>
namespace {
using std::map;
using std::string;
using std::vector;
using namespace Halide;
using namespace Halide::Internal;
class CountPredicatedStoreLoad : public IRVisitor {
public:
int store_count;
int load_count;
CountPredicatedStoreLoad()
: store_count(0), load_count(0) {
}
protected:
using IRVisitor::visit;
void visit(const Load *op) override {
if (!is_const_one(op->predicate)) {
load_count++;
}
IRVisitor::visit(op);
}
void visit(const Store *op) override {
if (!is_const_one(op->predicate)) {
store_count++;
}
IRVisitor::visit(op);
}
};
class CheckPredicatedStoreLoad : public IRMutator {
int expected_store_count;
int expected_load_count;
public:
CheckPredicatedStoreLoad(int store, int load)
: expected_store_count(store), expected_load_count(load) {
}
using IRMutator::mutate;
Stmt mutate(const Stmt &s) override {
CountPredicatedStoreLoad c;
s.accept(&c);
if (expected_store_count != c.store_count) {
printf("There were %d predicated stores; expect %d predicated stores\n",
c.store_count, expected_store_count);
exit(1);
}
if (expected_load_count != c.load_count) {
printf("There were %d predicated loads; expect %d predicated loads\n",
c.load_count, expected_load_count);
exit(1);
}
return s;
}
};
int predicated_tail_test(const Target &t) {
int size = 73;
for (auto i : {TailStrategy::Predicate, TailStrategy::PredicateLoads, TailStrategy::PredicateStores}) {
Var x("x"), y("y");
Func f("f"), g("g");
ImageParam p(Int(32), 2);
f(x, y) = p(x, y);
// We need a wrapper to avoid getting the bounds inflated by the rounding-up cases by realize.
g(x, y) = f(x, y);
f.compute_root();
const int vector_size = 32;
f.vectorize(x, vector_size, i);
if (t.has_feature(Target::HVX)) {
f.hexagon();
}
int predicated_loads = i != TailStrategy::PredicateStores ? 1 : 0;
int predicated_stores = i != TailStrategy::PredicateLoads ? 1 : 0;
g.add_custom_lowering_pass(new CheckPredicatedStoreLoad(predicated_stores, predicated_loads));
int buffer_size = size;
if (i == TailStrategy::PredicateStores) {
buffer_size = ((buffer_size + vector_size - 1) / vector_size) * vector_size;
}
Buffer<int> input(buffer_size, size);
input.fill([](int x, int y) { return x; });
p.set(input);
Buffer<int> im = g.realize({size, size});
auto func = [](int x, int y) {
return x;
};
if (check_image(im, func)) {
return 1;
}
}
return 0;
}
int predicated_tail_with_scalar_test(const Target &t) {
int size = 73;
Var x("x"), y("y");
Func f("f"), g("g");
g(x) = 10;
f(x, y) = x + g(0);
g.compute_at(f, y);
f.vectorize(x, 32, TailStrategy::Predicate);
if (t.has_feature(Target::HVX)) {
f.hexagon();
}
f.add_custom_lowering_pass(new CheckPredicatedStoreLoad(1, 0));
Buffer<int> im = f.realize({size, size});
auto func = [](int x, int y) {
return x + 10;
};
if (check_image(im, func)) {
return 1;
}
return 0;
}
int vectorized_predicated_store_scalarized_predicated_load_test(const Target &t) {
Var x("x"), y("y");
Func f("f"), g("g"), ref("ref");
g(x, y) = x + y;
g.compute_root();
RDom r(0, 100, 0, 100);
r.where(r.x + r.y < r.x * r.y);
ref(x, y) = 10;
ref(r.x, r.y) += g(2 * r.x, r.y) + g(2 * r.x + 1, r.y);
Buffer<int> im_ref = ref.realize({170, 170});
f(x, y) = 10;
f(r.x, r.y) += g(2 * r.x, r.y) + g(2 * r.x + 1, r.y);
f.update(0).vectorize(r.x, 32);
if (t.has_feature(Target::HVX)) {
f.update(0).hexagon();
}
f.add_custom_lowering_pass(new CheckPredicatedStoreLoad(2, 6));
Buffer<int> im = f.realize({170, 170});
auto func = [im_ref](int x, int y, int z) { return im_ref(x, y, z); };
if (check_image(im, func)) {
return 1;
}
return 0;
}
int vectorized_dense_load_with_stride_minus_one_test(const Target &t) {
int size = 73;
Var x("x"), y("y");
Func f("f"), g("g"), ref("ref");
g(x, y) = x * y;
g.compute_root();
ref(x, y) = select(x < 23, g(size - x, y) * 2 + g(20 - x, y), undef<int>());
Buffer<int> im_ref = ref.realize({size, size});
f(x, y) = select(x < 23, g(size - x, y) * 2 + g(20 - x, y), undef<int>());
f.vectorize(x, 32, TailStrategy::Predicate);
if (t.has_feature(Target::HVX)) {
f.hexagon();
}
f.add_custom_lowering_pass(new CheckPredicatedStoreLoad(3, 6));
Buffer<int> im = f.realize({size, size});
auto func = [&im_ref, &im](int x, int y, int z) {
// For x >= 23, the buffer is undef
return (x < 23) ? im_ref(x, y, z) : im(x, y, z);
};
if (check_image(im, func)) {
return 1;
}
return 0;
}
int multiple_vectorized_predicate_test(const Target &t) {
int size = 100;
Var x("x"), y("y");
Func f("f"), g("g"), ref("ref");
g(x, y) = x * y;
g.compute_root();
RDom r(0, size, 0, size);
r.where(r.x + r.y < 57);
r.where(r.x * r.y + r.x * r.x < 490);
ref(x, y) = 10;
ref(r.x, r.y) = g(size - r.x, r.y) * 2 + g(67 - r.x, r.y);
Buffer<int> im_ref = ref.realize({size, size});
f(x, y) = 10;
f(r.x, r.y) = g(size - r.x, r.y) * 2 + g(67 - r.x, r.y);
f.update(0).vectorize(r.x, 32);
if (t.has_feature(Target::HVX)) {
f.update(0).hexagon();
}
f.add_custom_lowering_pass(new CheckPredicatedStoreLoad(1, 2));
Buffer<int> im = f.realize({size, size});
auto func = [&im_ref](int x, int y, int z) { return im_ref(x, y, z); };
if (check_image(im, func)) {
return 1;
}
return 0;
}
int scalar_load_test(const Target &t) {
Var x("x"), y("y");
Func f("f"), g("g"), ref("ref");
g(x, y) = x + y;
g.compute_root();
RDom r(0, 80, 0, 80);
r.where(r.x + r.y < 48);
ref(x, y) = 10;
ref(r.x, r.y) += 1 + max(g(0, 1), g(2 * r.x + 1, r.y));
Buffer<int> im_ref = ref.realize({160, 160});
f(x, y) = 10;
f(r.x, r.y) += 1 + max(g(0, 1), g(2 * r.x + 1, r.y));
f.update(0).vectorize(r.x, 32);
if (t.has_feature(Target::HVX)) {
f.update(0).hexagon();
}
f.add_custom_lowering_pass(new CheckPredicatedStoreLoad(1, 2));
Buffer<int> im = f.realize({160, 160});
auto func = [im_ref](int x, int y, int z) { return im_ref(x, y, z); };
if (check_image(im, func)) {
return 1;
}
return 0;
}
int scalar_store_test(const Target &t) {
Var x("x"), y("y");
Func f("f"), g("g"), ref("ref");
g(x, y) = x + y;
g.compute_root();
RDom r(0, 80, 0, 80);
r.where(r.x + r.y < 48);
ref(x, y) = 10;
ref(13, 13) = max(g(0, 1), g(2 * r.x + 1, r.y));
Buffer<int> im_ref = ref.realize({160, 160});
f(x, y) = 10;
f(13, 13) = max(g(0, 1), g(2 * r.x + 1, r.y));
f.update(0).allow_race_conditions();
f.update(0).vectorize(r.x, 32);
if (t.has_feature(Target::HVX)) {
f.update(0).hexagon();
}
f.add_custom_lowering_pass(new CheckPredicatedStoreLoad(1, 1));
Buffer<int> im = f.realize({160, 160});
auto func = [im_ref](int x, int y, int z) { return im_ref(x, y, z); };
if (check_image(im, func)) {
return 1;
}
return 0;
}
int not_dependent_on_vectorized_var_test(const Target &t) {
Var x("x"), y("y"), z("z");
Func f("f"), g("g"), ref("ref");
g(x, y, z) = x + y + z;
g.compute_root();
RDom r(0, 80, 0, 80, 0, 80);
r.where(r.z * r.z < 47);
ref(x, y, z) = 10;
ref(r.x, r.y, 1) = max(g(0, 1, 2), g(r.x + 1, r.y, 2));
Buffer<int> im_ref = ref.realize({160, 160, 160});
f(x, y, z) = 10;
f(r.x, r.y, 1) = max(g(0, 1, 2), g(r.x + 1, r.y, 2));
f.update(0).allow_race_conditions();
f.update(0).vectorize(r.z, 32);
if (t.has_feature(Target::HVX)) {
f.update(0).hexagon();
}
f.add_custom_lowering_pass(new CheckPredicatedStoreLoad(0, 0));
Buffer<int> im = f.realize({160, 160, 160});
auto func = [im_ref](int x, int y, int z) { return im_ref(x, y, z); };
if (check_image(im, func)) {
return 1;
}
return 0;
}
int no_op_store_test(const Target &t) {
Var x("x"), y("y");
Func f("f"), ref("ref");
RDom r(0, 80, 0, 80);
r.where(r.x + r.y < 47);
ref(x, y) = x + y;
ref(2 * r.x + 1, r.y) = ref(2 * r.x + 1, r.y);
ref(2 * r.x, 3 * r.y) = ref(2 * r.x, 3 * r.y);
Buffer<int> im_ref = ref.realize({240, 240});
f(x, y) = x + y;
f(2 * r.x + 1, r.y) = f(2 * r.x + 1, r.y);
f(2 * r.x, 3 * r.y) = f(2 * r.x, 3 * r.y);
f.update(0).vectorize(r.x, 32);
f.update(1).vectorize(r.y, 32);
if (t.has_feature(Target::HVX)) {
f.update(0).hexagon();
f.update(1).hexagon();
}
Buffer<int> im = f.realize({240, 240});
auto func = [im_ref](int x, int y, int z) { return im_ref(x, y, z); };
if (check_image(im, func)) {
return 1;
}
return 0;
}
int vectorized_predicated_predicate_with_pure_call_test(const Target &t) {
Var x("x"), y("y");
Func f("f"), g("g"), ref("ref");
g(x, y) = x + y;
g.compute_root();
RDom r(0, 100, 0, 100);
r.where(r.x + r.y < r.x * r.y);
ref(x, y) = 10;
ref(r.x, r.y) += abs(r.x * r.y) + g(2 * r.x + 1, r.y);
Buffer<int> im_ref = ref.realize({160, 160});
f(x, y) = 10;
f(r.x, r.y) += abs(r.x * r.y) + g(2 * r.x + 1, r.y);
f.update(0).vectorize(r.x, 32);
if (t.has_feature(Target::HVX)) {
f.update(0).hexagon();
}
f.add_custom_lowering_pass(new CheckPredicatedStoreLoad(2, 4));
Buffer<int> im = f.realize({160, 160});
auto func = [im_ref](int x, int y, int z) { return im_ref(x, y, z); };
if (check_image(im, func)) {
return 1;
}
return 0;
}
int vectorized_predicated_load_const_index_test(const Target &t) {
Buffer<int> in(100, 100);
for (int y = 0; y < 100; y++) {
for (int x = 0; x < 100; x++) {
in(x, y) = rand();
}
}
Func f("f"), ref("ref");
Var x("x"), y("y");
ImageParam input(Int(32), 2, "input");
input.set(in);
RDom r(0, 100);
ref(x, y) = x + y;
ref(r.x, y) = clamp(select((r.x % 2) == 0, r.x, y) + input(r.x % 2, y), 0, 10);
Buffer<int> im_ref = ref.realize({100, 100});
f(x, y) = x + y;
f(r.x, y) = clamp(select((r.x % 2) == 0, r.x, y) + input(r.x % 2, y), 0, 10);
f.update().vectorize(r.x, 32);
if (t.has_feature(Target::HVX)) {
f.update().hexagon();
}
f.add_custom_lowering_pass(new CheckPredicatedStoreLoad(1, 2));
Buffer<int> im = f.realize({100, 100});
auto func = [im_ref](int x, int y) { return im_ref(x, y); };
if (check_image(im, func)) {
return 1;
}
return 0;
}
int vectorized_predicated_load_lut_test(const Target &t) {
if (t.arch != Target::X86) {
// This test will fail on Hexagon as the LUT is larger than 16 bits.
// Since using less than 16-bit LUT will make the predicate on the
// vector store/load disappear, only run the test for X86.
return 0;
}
constexpr int vector_size = 4;
constexpr int lut_height = vector_size + 2; // Any non-even multiple of vector-size will do.
constexpr int dst_len = 100;
Buffer<int32_t> lut(2, lut_height);
lut.fill(0);
Var x("x");
Func dst("dst");
RDom r(0, lut_height);
dst(x) = 0.f;
dst(clamp(lut(0, r), 0, dst_len - 1)) += 1.f;
dst.output_buffer().dim(0).set_min(0).set_extent(dst_len);
// Ignore the race condition so we can have predicated vectorized
// LUT loads on both LHS and RHS of the predicated vectorized store
dst.update().allow_race_conditions().vectorize(r, vector_size);
dst.add_custom_lowering_pass(new CheckPredicatedStoreLoad(1, 2));
dst.realize({dst_len});
return 0;
}
int predicated_atomic_store_test(const Target &t) {
// We don't support atomic predicated stores, so ensure that we don't
// generate them. See https://github.com/halide/Halide/issues/8280
ImageParam in(Float(32), 1);
Func f;
Var x;
RDom r(0, 20);
f(x) = 0.f;
f(x) += in(r) + x;
f.update().vectorize(x, 8, TailStrategy::GuardWithIf).atomic().parallel(r);
// This will cause an internal_error in the LLVM backend if we pass a
// predicated atomic store down to codegen.
f.compile_jit(t);
return 0;
}
} // namespace
int main(int argc, char **argv) {
Target t = get_jit_target_from_environment();
printf("Running vectorized dense load test\n");
if (predicated_tail_test(t) != 0) {
return 1;
}
printf("Running vectorized dense load with scalar test\n");
if (predicated_tail_with_scalar_test(t) != 0) {
return 1;
}
printf("Running vectorized dense load with stride minus one test\n");
if (vectorized_dense_load_with_stride_minus_one_test(t) != 0) {
return 1;
}
printf("Running multiple vectorized predicate test\n");
if (multiple_vectorized_predicate_test(t) != 0) {
return 1;
}
printf("Running vectorized predicated store scalarized predicated load test\n");
if (vectorized_predicated_store_scalarized_predicated_load_test(t) != 0) {
return 1;
}
printf("Running scalar load test\n");
if (scalar_load_test(t) != 0) {
return 1;
}
printf("Running scalar store test\n");
if (scalar_store_test(t) != 0) {
return 1;
}
printf("Running not dependent on vectorized var test\n");
if (not_dependent_on_vectorized_var_test(t) != 0) {
return 1;
}
printf("Running no-op store test\n");
if (no_op_store_test(t) != 0) {
return 1;
}
printf("Running vectorized predicated with pure call test\n");
if (vectorized_predicated_predicate_with_pure_call_test(t) != 0) {
return 1;
}
printf("Running vectorized predicated load with constant index test\n");
if (vectorized_predicated_load_const_index_test(t) != 0) {
return 1;
}
printf("Running vectorized predicated load lut test\n");
if (vectorized_predicated_load_lut_test(t) != 0) {
return 1;
}
printf("predicated atomic store test\n");
if (predicated_atomic_store_test(t) != 0) {
return 1;
}
printf("Success!\n");
return 0;
}
|