20
20
#include " llvm/ADT/APSInt.h"
21
21
#include " llvm/ADT/Sequence.h"
22
22
#include " llvm/ADT/TypeSwitch.h"
23
+ #include " llvm/Support/Debug.h"
23
24
#include " llvm/Support/Endian.h"
24
25
#include < optional>
25
26
27
+ #define DEBUG_TYPE " builtinattributes"
28
+
26
29
using namespace mlir ;
27
30
using namespace mlir ::detail;
28
31
@@ -1098,24 +1101,44 @@ bool DenseElementsAttr::isValidRawBuffer(ShapedType type,
1098
1101
static bool isValidIntOrFloat (Type type, int64_t dataEltSize, bool isInt,
1099
1102
bool isSigned) {
1100
1103
// 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 " );
1103
1110
return false ;
1111
+ }
1104
1112
1105
1113
// 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
+ }
1108
1122
if (type.isIndex ())
1109
1123
return true ;
1110
1124
1111
1125
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 " );
1113
1130
return false ;
1131
+ }
1114
1132
1115
1133
// Make sure signedness semantics is consistent.
1116
1134
if (intType.isSignless ())
1117
1135
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;
1119
1142
}
1120
1143
1121
1144
// / Defaults down the subclass implementation.
@@ -1247,12 +1270,14 @@ DenseElementsAttr DenseElementsAttr::bitcast(Type newElType) {
1247
1270
DenseElementsAttr
1248
1271
DenseElementsAttr::mapValues (Type newElementType,
1249
1272
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);
1251
1275
}
1252
1276
1253
1277
DenseElementsAttr DenseElementsAttr::mapValues (
1254
1278
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);
1256
1281
}
1257
1282
1258
1283
ShapedType DenseElementsAttr::getType () const {
@@ -1331,8 +1356,9 @@ DenseElementsAttr DenseIntOrFPElementsAttr::getRawComplex(ShapedType type,
1331
1356
bool isInt,
1332
1357
bool isSigned) {
1333
1358
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" );
1336
1362
1337
1363
int64_t numElements = data.size () / dataEltSize;
1338
1364
(void )numElements;
@@ -1347,8 +1373,9 @@ DenseElementsAttr
1347
1373
DenseIntOrFPElementsAttr::getRawIntOrFloat (ShapedType type, ArrayRef<char > data,
1348
1374
int64_t dataEltSize, bool isInt,
1349
1375
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" );
1352
1379
1353
1380
int64_t numElements = data.size () / dataEltSize;
1354
1381
assert (numElements == 1 || numElements == type.getNumElements ());
0 commit comments