Skip to content

Commit 04d6308

Browse files
j2kunjoker-eph
authored andcommitted
[mlir] Add debug messages for failures of isValidIntOrFloat
I have run into assertion failures quite often when calling this method via `DenseElementsAttr::get`, and I think this would help, at the very least, by printing out the bit width size mismatches, rather than a plain assertion failure. I included all the other cases in the method for completeness
1 parent d11c454 commit 04d6308

File tree

1 file changed

+39
-12
lines changed

1 file changed

+39
-12
lines changed

mlir/lib/IR/BuiltinAttributes.cpp

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020
#include "llvm/ADT/APSInt.h"
2121
#include "llvm/ADT/Sequence.h"
2222
#include "llvm/ADT/TypeSwitch.h"
23+
#include "llvm/Support/Debug.h"
2324
#include "llvm/Support/Endian.h"
2425
#include <optional>
2526

27+
#define DEBUG_TYPE "builtinattributes"
28+
2629
using namespace mlir;
2730
using namespace mlir::detail;
2831

@@ -1098,24 +1101,44 @@ bool DenseElementsAttr::isValidRawBuffer(ShapedType type,
10981101
static bool isValidIntOrFloat(Type type, int64_t dataEltSize, bool isInt,
10991102
bool isSigned) {
11001103
// Make sure that the data element size is the same as the type element width.
1101-
if (getDenseElementBitWidth(type) !=
1102-
static_cast<size_t>(dataEltSize * CHAR_BIT))
1104+
auto denseEltBitWidth = getDenseElementBitWidth(type);
1105+
auto dataSize = static_cast<size_t>(dataEltSize * CHAR_BIT);
1106+
if (denseEltBitWidth != dataSize) {
1107+
LLVM_DEBUG(llvm::dbgs() << "expected dense element bit width "
1108+
<< denseEltBitWidth << " to match data size "
1109+
<< dataSize << " for type " << type << "\n");
11031110
return false;
1111+
}
11041112

11051113
// Check that the element type is either float or integer or index.
1106-
if (!isInt)
1107-
return llvm::isa<FloatType>(type);
1114+
if (!isInt) {
1115+
bool valid = llvm::isa<FloatType>(type);
1116+
if (!valid)
1117+
LLVM_DEBUG(llvm::dbgs()
1118+
<< "expected float type when isInt is false, but found "
1119+
<< type << "\n");
1120+
return valid;
1121+
}
11081122
if (type.isIndex())
11091123
return true;
11101124

11111125
auto intType = llvm::dyn_cast<IntegerType>(type);
1112-
if (!intType)
1126+
if (!intType) {
1127+
LLVM_DEBUG(llvm::dbgs()
1128+
<< "expected integer type when isInt is true, but found " << type
1129+
<< "\n");
11131130
return false;
1131+
}
11141132

11151133
// Make sure signedness semantics is consistent.
11161134
if (intType.isSignless())
11171135
return true;
1118-
return intType.isSigned() ? isSigned : !isSigned;
1136+
1137+
bool valid = intType.isSigned() == isSigned;
1138+
if (!valid)
1139+
LLVM_DEBUG(llvm::dbgs() << "expected signedness " << isSigned
1140+
<< " to match type " << type << "\n");
1141+
return valid;
11191142
}
11201143

11211144
/// Defaults down the subclass implementation.
@@ -1247,12 +1270,14 @@ DenseElementsAttr DenseElementsAttr::bitcast(Type newElType) {
12471270
DenseElementsAttr
12481271
DenseElementsAttr::mapValues(Type newElementType,
12491272
function_ref<APInt(const APInt &)> mapping) const {
1250-
return llvm::cast<DenseIntElementsAttr>(*this).mapValues(newElementType, mapping);
1273+
return llvm::cast<DenseIntElementsAttr>(*this).mapValues(newElementType,
1274+
mapping);
12511275
}
12521276

12531277
DenseElementsAttr DenseElementsAttr::mapValues(
12541278
Type newElementType, function_ref<APInt(const APFloat &)> mapping) const {
1255-
return llvm::cast<DenseFPElementsAttr>(*this).mapValues(newElementType, mapping);
1279+
return llvm::cast<DenseFPElementsAttr>(*this).mapValues(newElementType,
1280+
mapping);
12561281
}
12571282

12581283
ShapedType DenseElementsAttr::getType() const {
@@ -1331,8 +1356,9 @@ DenseElementsAttr DenseIntOrFPElementsAttr::getRawComplex(ShapedType type,
13311356
bool isInt,
13321357
bool isSigned) {
13331358
assert(::isValidIntOrFloat(
1334-
llvm::cast<ComplexType>(type.getElementType()).getElementType(),
1335-
dataEltSize / 2, isInt, isSigned));
1359+
llvm::cast<ComplexType>(type.getElementType()).getElementType(),
1360+
dataEltSize / 2, isInt, isSigned) &&
1361+
"Try re-running with -debug-only=builtinattributes");
13361362

13371363
int64_t numElements = data.size() / dataEltSize;
13381364
(void)numElements;
@@ -1347,8 +1373,9 @@ DenseElementsAttr
13471373
DenseIntOrFPElementsAttr::getRawIntOrFloat(ShapedType type, ArrayRef<char> data,
13481374
int64_t dataEltSize, bool isInt,
13491375
bool isSigned) {
1350-
assert(
1351-
::isValidIntOrFloat(type.getElementType(), dataEltSize, isInt, isSigned));
1376+
assert(::isValidIntOrFloat(type.getElementType(), dataEltSize, isInt,
1377+
isSigned) &&
1378+
"Try re-running with -debug-only=builtinattributes");
13521379

13531380
int64_t numElements = data.size() / dataEltSize;
13541381
assert(numElements == 1 || numElements == type.getNumElements());

0 commit comments

Comments
 (0)