2020
2121using namespace mlir ;
2222
23+ // / Generate an error message string for the given op and the specified error.
24+ static std::string generateErrorMessage (Operation *op, const std::string &msg) {
25+ std::string buffer;
26+ llvm::raw_string_ostream stream (buffer);
27+ OpPrintingFlags flags;
28+ // We may generate a lot of error messages and so we need to ensure the
29+ // printing is fast.
30+ flags.elideLargeElementsAttrs ();
31+ flags.printGenericOpForm ();
32+ flags.skipRegions ();
33+ flags.useLocalScope ();
34+ stream << " ERROR: Runtime op verification failed\n " ;
35+ op->print (stream, flags);
36+ stream << " \n ^ " << msg;
37+ stream << " \n Location: " ;
38+ op->getLoc ().print (stream);
39+ return stream.str ();
40+ }
41+
2342namespace mlir {
2443namespace memref {
2544namespace {
@@ -43,10 +62,8 @@ struct CastOpInterface
4362 builder.create <arith::ConstantIndexOp>(loc, resultType.getRank ());
4463 Value isSameRank = builder.create <arith::CmpIOp>(
4564 loc, arith::CmpIPredicate::eq, srcRank, resultRank);
46- builder.create <cf::AssertOp>(
47- loc, isSameRank,
48- RuntimeVerifiableOpInterface::generateErrorMessage (op,
49- " rank mismatch" ));
65+ builder.create <cf::AssertOp>(loc, isSameRank,
66+ generateErrorMessage (op, " rank mismatch" ));
5067 }
5168
5269 // Get source offset and strides. We do not have an op to get offsets and
@@ -84,8 +101,8 @@ struct CastOpInterface
84101 loc, arith::CmpIPredicate::eq, srcDimSz, resultDimSz);
85102 builder.create <cf::AssertOp>(
86103 loc, isSameSz,
87- RuntimeVerifiableOpInterface:: generateErrorMessage (
88- op, " size mismatch of dim " + std::to_string (it.index ())));
104+ generateErrorMessage (op, " size mismatch of dim " +
105+ std::to_string (it.index ())));
89106 }
90107
91108 // Get result offset and strides.
@@ -102,10 +119,8 @@ struct CastOpInterface
102119 builder.create <arith::ConstantIndexOp>(loc, resultOffset);
103120 Value isSameOffset = builder.create <arith::CmpIOp>(
104121 loc, arith::CmpIPredicate::eq, srcOffset, resultOffsetVal);
105- builder.create <cf::AssertOp>(
106- loc, isSameOffset,
107- RuntimeVerifiableOpInterface::generateErrorMessage (
108- op, " offset mismatch" ));
122+ builder.create <cf::AssertOp>(loc, isSameOffset,
123+ generateErrorMessage (op, " offset mismatch" ));
109124 }
110125
111126 // Check strides.
@@ -122,8 +137,8 @@ struct CastOpInterface
122137 loc, arith::CmpIPredicate::eq, srcStride, resultStrideVal);
123138 builder.create <cf::AssertOp>(
124139 loc, isSameStride,
125- RuntimeVerifiableOpInterface:: generateErrorMessage (
126- op, " stride mismatch of dim " + std::to_string (it.index ())));
140+ generateErrorMessage (op, " stride mismatch of dim " +
141+ std::to_string (it.index ())));
127142 }
128143 }
129144};
@@ -163,9 +178,7 @@ struct LoadStoreOpInterface
163178 : andOp;
164179 }
165180 builder.create <cf::AssertOp>(
166- loc, assertCond,
167- RuntimeVerifiableOpInterface::generateErrorMessage (
168- op, " out-of-bounds access" ));
181+ loc, assertCond, generateErrorMessage (op, " out-of-bounds access" ));
169182 }
170183};
171184
@@ -235,7 +248,7 @@ struct ReinterpretCastOpInterface
235248
236249 builder.create <cf::AssertOp>(
237250 loc, assertCond,
238- RuntimeVerifiableOpInterface:: generateErrorMessage (
251+ generateErrorMessage (
239252 op,
240253 " result of reinterpret_cast is out-of-bounds of the base memref" ));
241254 }
@@ -280,8 +293,8 @@ struct SubViewOpInterface
280293
281294 builder.create <cf::AssertOp>(
282295 loc, assertCond,
283- RuntimeVerifiableOpInterface:: generateErrorMessage (
284- op, " subview is out-of-bounds of the base memref" ));
296+ generateErrorMessage (op,
297+ " subview is out-of-bounds of the base memref" ));
285298 }
286299};
287300
@@ -321,9 +334,8 @@ struct ExpandShapeOpInterface
321334 builder.create <arith::ConstantIndexOp>(loc, 0 ));
322335 builder.create <cf::AssertOp>(
323336 loc, isModZero,
324- RuntimeVerifiableOpInterface::generateErrorMessage (
325- op, " static result dims in reassoc group do not "
326- " divide src dim evenly" ));
337+ generateErrorMessage (op, " static result dims in reassoc group do not "
338+ " divide src dim evenly" ));
327339 }
328340 }
329341};
0 commit comments