Skip to content

Commit 5b936af

Browse files
committed
[KnownBitsTest] Print name of function when exhaustive tests fail
When exhaustive unary/binary tests fail, print the name of the function being tested as well as the values of the inputs and outputs. Example of a simulated failure in testing "udiv exact": unittests/Support/KnownBitsTest.cpp:99: Failure Value of: checkResult(Name, Exact, Computed, {Known1, Known2}, CheckOptimality) Actual: false (udiv exact: Inputs = ???1, ????, Computed = ???1, Exact = 0???) Expected: true
1 parent f07ee19 commit 5b936af

File tree

1 file changed

+49
-44
lines changed

1 file changed

+49
-44
lines changed

llvm/unittests/Support/KnownBitsTest.cpp

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "llvm/ADT/ArrayRef.h"
14+
#include "llvm/ADT/StringRef.h"
15+
#include "llvm/ADT/Twine.h"
1416
#include "llvm/Support/KnownBits.h"
1517
#include "KnownBitsTest.h"
1618
#include "gtest/gtest.h"
@@ -25,7 +27,8 @@ using BinaryBitsFn =
2527
using BinaryIntFn =
2628
llvm::function_ref<std::optional<APInt>(const APInt &, const APInt &)>;
2729

28-
static testing::AssertionResult checkResult(const KnownBits &Exact,
30+
static testing::AssertionResult checkResult(Twine Name,
31+
const KnownBits &Exact,
2932
const KnownBits &Computed,
3033
ArrayRef<KnownBits> Inputs,
3134
bool CheckOptimality) {
@@ -41,14 +44,15 @@ static testing::AssertionResult checkResult(const KnownBits &Exact,
4144
}
4245

4346
testing::AssertionResult Result = testing::AssertionFailure();
47+
Result << Name << ": ";
4448
Result << "Inputs = ";
4549
for (const KnownBits &Input : Inputs)
4650
Result << Input << ", ";
4751
Result << "Computed = " << Computed << ", Exact = " << Exact;
4852
return Result;
4953
}
5054

51-
static void testUnaryOpExhaustive(UnaryBitsFn BitsFn, UnaryIntFn IntFn,
55+
static void testUnaryOpExhaustive(StringRef Name, UnaryBitsFn BitsFn, UnaryIntFn IntFn,
5256
bool CheckOptimality = true) {
5357
for (unsigned Bits : {1, 4}) {
5458
ForeachKnownBits(Bits, [&](const KnownBits &Known) {
@@ -65,12 +69,12 @@ static void testUnaryOpExhaustive(UnaryBitsFn BitsFn, UnaryIntFn IntFn,
6569
});
6670

6771
EXPECT_TRUE(!Computed.hasConflict());
68-
EXPECT_TRUE(checkResult(Exact, Computed, Known, CheckOptimality));
72+
EXPECT_TRUE(checkResult(Name, Exact, Computed, Known, CheckOptimality));
6973
});
7074
}
7175
}
7276

73-
static void testBinaryOpExhaustive(BinaryBitsFn BitsFn, BinaryIntFn IntFn,
77+
static void testBinaryOpExhaustive(StringRef Name, BinaryBitsFn BitsFn, BinaryIntFn IntFn,
7478
bool CheckOptimality = true,
7579
bool RefinePoisonToZero = false) {
7680
for (unsigned Bits : {1, 4}) {
@@ -92,7 +96,7 @@ static void testBinaryOpExhaustive(BinaryBitsFn BitsFn, BinaryIntFn IntFn,
9296

9397
EXPECT_TRUE(!Computed.hasConflict());
9498
EXPECT_TRUE(
95-
checkResult(Exact, Computed, {Known1, Known2}, CheckOptimality));
99+
checkResult(Name, Exact, Computed, {Known1, Known2}, CheckOptimality));
96100
// In some cases we choose to return zero if the result is always
97101
// poison.
98102
if (RefinePoisonToZero && Exact.hasConflict()) {
@@ -137,6 +141,7 @@ TEST(KnownBitsTest, AddCarryExhaustive) {
137141
}
138142

139143
static void TestAddSubExhaustive(bool IsAdd) {
144+
Twine Name = IsAdd ? "add" : "sub";
140145
unsigned Bits = 4;
141146
ForeachKnownBits(Bits, [&](const KnownBits &Known1) {
142147
ForeachKnownBits(Bits, [&](const KnownBits &Known2) {
@@ -186,22 +191,22 @@ static void TestAddSubExhaustive(bool IsAdd) {
186191

187192
KnownBits KnownComputed = KnownBits::computeForAddSub(
188193
IsAdd, /*NSW=*/false, /*NUW=*/false, Known1, Known2);
189-
EXPECT_TRUE(checkResult(Known, KnownComputed, {Known1, Known2},
194+
EXPECT_TRUE(checkResult(Name, Known, KnownComputed, {Known1, Known2},
190195
/*CheckOptimality=*/true));
191196

192197
KnownBits KnownNSWComputed = KnownBits::computeForAddSub(
193198
IsAdd, /*NSW=*/true, /*NUW=*/false, Known1, Known2);
194-
EXPECT_TRUE(checkResult(KnownNSW, KnownNSWComputed, {Known1, Known2},
199+
EXPECT_TRUE(checkResult(Name + " nsw", KnownNSW, KnownNSWComputed, {Known1, Known2},
195200
/*CheckOptimality=*/true));
196201

197202
KnownBits KnownNUWComputed = KnownBits::computeForAddSub(
198203
IsAdd, /*NSW=*/false, /*NUW=*/true, Known1, Known2);
199-
EXPECT_TRUE(checkResult(KnownNUW, KnownNUWComputed, {Known1, Known2},
204+
EXPECT_TRUE(checkResult(Name + " nuw", KnownNUW, KnownNUWComputed, {Known1, Known2},
200205
/*CheckOptimality=*/true));
201206

202207
KnownBits KnownNSWAndNUWComputed = KnownBits::computeForAddSub(
203208
IsAdd, /*NSW=*/true, /*NUW=*/true, Known1, Known2);
204-
EXPECT_TRUE(checkResult(KnownNSWAndNUW, KnownNSWAndNUWComputed,
209+
EXPECT_TRUE(checkResult(Name + " nsw nuw", KnownNSWAndNUW, KnownNSWAndNUWComputed,
205210
{Known1, Known2}, /*CheckOptimality=*/true));
206211
});
207212
});
@@ -266,28 +271,28 @@ TEST(KnownBitsTest, SignBitUnknown) {
266271
}
267272

268273
TEST(KnownBitsTest, BinaryExhaustive) {
269-
testBinaryOpExhaustive(
274+
testBinaryOpExhaustive("and",
270275
[](const KnownBits &Known1, const KnownBits &Known2) {
271276
return Known1 & Known2;
272277
},
273278
[](const APInt &N1, const APInt &N2) { return N1 & N2; });
274-
testBinaryOpExhaustive(
279+
testBinaryOpExhaustive("or",
275280
[](const KnownBits &Known1, const KnownBits &Known2) {
276281
return Known1 | Known2;
277282
},
278283
[](const APInt &N1, const APInt &N2) { return N1 | N2; });
279-
testBinaryOpExhaustive(
284+
testBinaryOpExhaustive("xor",
280285
[](const KnownBits &Known1, const KnownBits &Known2) {
281286
return Known1 ^ Known2;
282287
},
283288
[](const APInt &N1, const APInt &N2) { return N1 ^ N2; });
284-
testBinaryOpExhaustive(KnownBits::umax, APIntOps::umax);
285-
testBinaryOpExhaustive(KnownBits::umin, APIntOps::umin);
286-
testBinaryOpExhaustive(KnownBits::smax, APIntOps::smax);
287-
testBinaryOpExhaustive(KnownBits::smin, APIntOps::smin);
288-
testBinaryOpExhaustive(KnownBits::abdu, APIntOps::abdu);
289-
testBinaryOpExhaustive(KnownBits::abds, APIntOps::abds);
290-
testBinaryOpExhaustive(
289+
testBinaryOpExhaustive("umax", KnownBits::umax, APIntOps::umax);
290+
testBinaryOpExhaustive("umin", KnownBits::umin, APIntOps::umin);
291+
testBinaryOpExhaustive("smax", KnownBits::smax, APIntOps::smax);
292+
testBinaryOpExhaustive("smin", KnownBits::smin, APIntOps::smin);
293+
testBinaryOpExhaustive("abdu", KnownBits::abdu, APIntOps::abdu);
294+
testBinaryOpExhaustive("abds", KnownBits::abds, APIntOps::abds);
295+
testBinaryOpExhaustive("udiv",
291296
[](const KnownBits &Known1, const KnownBits &Known2) {
292297
return KnownBits::udiv(Known1, Known2);
293298
},
@@ -297,7 +302,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
297302
return N1.udiv(N2);
298303
},
299304
/*CheckOptimality=*/false);
300-
testBinaryOpExhaustive(
305+
testBinaryOpExhaustive("udiv exact",
301306
[](const KnownBits &Known1, const KnownBits &Known2) {
302307
return KnownBits::udiv(Known1, Known2, /*Exact*/ true);
303308
},
@@ -307,7 +312,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
307312
return N1.udiv(N2);
308313
},
309314
/*CheckOptimality=*/false);
310-
testBinaryOpExhaustive(
315+
testBinaryOpExhaustive("sdiv",
311316
[](const KnownBits &Known1, const KnownBits &Known2) {
312317
return KnownBits::sdiv(Known1, Known2);
313318
},
@@ -317,7 +322,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
317322
return N1.sdiv(N2);
318323
},
319324
/*CheckOptimality=*/false);
320-
testBinaryOpExhaustive(
325+
testBinaryOpExhaustive("sdiv exact",
321326
[](const KnownBits &Known1, const KnownBits &Known2) {
322327
return KnownBits::sdiv(Known1, Known2, /*Exact*/ true);
323328
},
@@ -328,47 +333,47 @@ TEST(KnownBitsTest, BinaryExhaustive) {
328333
return N1.sdiv(N2);
329334
},
330335
/*CheckOptimality=*/false);
331-
testBinaryOpExhaustive(
336+
testBinaryOpExhaustive("urem",
332337
KnownBits::urem,
333338
[](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
334339
if (N2.isZero())
335340
return std::nullopt;
336341
return N1.urem(N2);
337342
},
338343
/*CheckOptimality=*/false);
339-
testBinaryOpExhaustive(
344+
testBinaryOpExhaustive("srem",
340345
KnownBits::srem,
341346
[](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
342347
if (N2.isZero())
343348
return std::nullopt;
344349
return N1.srem(N2);
345350
},
346351
/*CheckOptimality=*/false);
347-
testBinaryOpExhaustive(
352+
testBinaryOpExhaustive("sadd_sat",
348353
KnownBits::sadd_sat,
349354
[](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
350355
return N1.sadd_sat(N2);
351356
},
352357
/*CheckOptimality=*/false);
353-
testBinaryOpExhaustive(
358+
testBinaryOpExhaustive("uadd_sat",
354359
KnownBits::uadd_sat,
355360
[](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
356361
return N1.uadd_sat(N2);
357362
},
358363
/*CheckOptimality=*/false);
359-
testBinaryOpExhaustive(
364+
testBinaryOpExhaustive("ssub_sat",
360365
KnownBits::ssub_sat,
361366
[](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
362367
return N1.ssub_sat(N2);
363368
},
364369
/*CheckOptimality=*/false);
365-
testBinaryOpExhaustive(
370+
testBinaryOpExhaustive("usub_sat",
366371
KnownBits::usub_sat,
367372
[](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
368373
return N1.usub_sat(N2);
369374
},
370375
/*CheckOptimality=*/false);
371-
testBinaryOpExhaustive(
376+
testBinaryOpExhaustive("shl",
372377
[](const KnownBits &Known1, const KnownBits &Known2) {
373378
return KnownBits::shl(Known1, Known2);
374379
},
@@ -378,7 +383,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
378383
return N1.shl(N2);
379384
},
380385
/*CheckOptimality=*/true, /* RefinePoisonToZero */ true);
381-
testBinaryOpExhaustive(
386+
testBinaryOpExhaustive("ushl_ov",
382387
[](const KnownBits &Known1, const KnownBits &Known2) {
383388
return KnownBits::shl(Known1, Known2, /* NUW */ true);
384389
},
@@ -390,7 +395,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
390395
return Res;
391396
},
392397
/*CheckOptimality=*/true, /* RefinePoisonToZero */ true);
393-
testBinaryOpExhaustive(
398+
testBinaryOpExhaustive("shl nsw",
394399
[](const KnownBits &Known1, const KnownBits &Known2) {
395400
return KnownBits::shl(Known1, Known2, /* NUW */ false, /* NSW */ true);
396401
},
@@ -402,7 +407,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
402407
return Res;
403408
},
404409
/*CheckOptimality=*/true, /* RefinePoisonToZero */ true);
405-
testBinaryOpExhaustive(
410+
testBinaryOpExhaustive("shl nuw",
406411
[](const KnownBits &Known1, const KnownBits &Known2) {
407412
return KnownBits::shl(Known1, Known2, /* NUW */ true, /* NSW */ true);
408413
},
@@ -416,7 +421,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
416421
},
417422
/*CheckOptimality=*/true, /* RefinePoisonToZero */ true);
418423

419-
testBinaryOpExhaustive(
424+
testBinaryOpExhaustive("lshr",
420425
[](const KnownBits &Known1, const KnownBits &Known2) {
421426
return KnownBits::lshr(Known1, Known2);
422427
},
@@ -426,7 +431,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
426431
return N1.lshr(N2);
427432
},
428433
/*CheckOptimality=*/true, /* RefinePoisonToZero */ true);
429-
testBinaryOpExhaustive(
434+
testBinaryOpExhaustive("lshr exact",
430435
[](const KnownBits &Known1, const KnownBits &Known2) {
431436
return KnownBits::lshr(Known1, Known2, /*ShAmtNonZero=*/false,
432437
/*Exact=*/true);
@@ -439,7 +444,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
439444
return N1.lshr(N2);
440445
},
441446
/*CheckOptimality=*/true, /* RefinePoisonToZero */ true);
442-
testBinaryOpExhaustive(
447+
testBinaryOpExhaustive("ashr",
443448
[](const KnownBits &Known1, const KnownBits &Known2) {
444449
return KnownBits::ashr(Known1, Known2);
445450
},
@@ -449,7 +454,7 @@ TEST(KnownBitsTest, BinaryExhaustive) {
449454
return N1.ashr(N2);
450455
},
451456
/*CheckOptimality=*/true, /* RefinePoisonToZero */ true);
452-
testBinaryOpExhaustive(
457+
testBinaryOpExhaustive("ashr exact",
453458
[](const KnownBits &Known1, const KnownBits &Known2) {
454459
return KnownBits::ashr(Known1, Known2, /*ShAmtNonZero=*/false,
455460
/*Exact=*/true);
@@ -462,39 +467,39 @@ TEST(KnownBitsTest, BinaryExhaustive) {
462467
return N1.ashr(N2);
463468
},
464469
/*CheckOptimality=*/true, /* RefinePoisonToZero */ true);
465-
testBinaryOpExhaustive(
470+
testBinaryOpExhaustive("mul",
466471
[](const KnownBits &Known1, const KnownBits &Known2) {
467472
return KnownBits::mul(Known1, Known2);
468473
},
469474
[](const APInt &N1, const APInt &N2) { return N1 * N2; },
470475
/*CheckOptimality=*/false);
471-
testBinaryOpExhaustive(
476+
testBinaryOpExhaustive("mulhs",
472477
KnownBits::mulhs,
473478
[](const APInt &N1, const APInt &N2) { return APIntOps::mulhs(N1, N2); },
474479
/*CheckOptimality=*/false);
475-
testBinaryOpExhaustive(
480+
testBinaryOpExhaustive("mulhu",
476481
KnownBits::mulhu,
477482
[](const APInt &N1, const APInt &N2) { return APIntOps::mulhu(N1, N2); },
478483
/*CheckOptimality=*/false);
479484
}
480485

481486
TEST(KnownBitsTest, UnaryExhaustive) {
482-
testUnaryOpExhaustive([](const KnownBits &Known) { return Known.abs(); },
487+
testUnaryOpExhaustive("abs", [](const KnownBits &Known) { return Known.abs(); },
483488
[](const APInt &N) { return N.abs(); });
484489

485-
testUnaryOpExhaustive([](const KnownBits &Known) { return Known.abs(true); },
490+
testUnaryOpExhaustive("abs(true)", [](const KnownBits &Known) { return Known.abs(true); },
486491
[](const APInt &N) -> std::optional<APInt> {
487492
if (N.isMinSignedValue())
488493
return std::nullopt;
489494
return N.abs();
490495
});
491496

492-
testUnaryOpExhaustive([](const KnownBits &Known) { return Known.blsi(); },
497+
testUnaryOpExhaustive("blsi", [](const KnownBits &Known) { return Known.blsi(); },
493498
[](const APInt &N) { return N & -N; });
494-
testUnaryOpExhaustive([](const KnownBits &Known) { return Known.blsmsk(); },
499+
testUnaryOpExhaustive("blsmsk", [](const KnownBits &Known) { return Known.blsmsk(); },
495500
[](const APInt &N) { return N ^ (N - 1); });
496501

497-
testUnaryOpExhaustive(
502+
testUnaryOpExhaustive("mul self",
498503
[](const KnownBits &Known) {
499504
return KnownBits::mul(Known, Known, /*SelfMultiply*/ true);
500505
},

0 commit comments

Comments
 (0)