|
43 | 43 | #include "llvm/Support/Regex.h"
|
44 | 44 | #include "llvm/Support/SaveAndRestore.h"
|
45 | 45 | #include "llvm/Support/Threading.h"
|
| 46 | +#include "llvm/Support/raw_ostream.h" |
46 | 47 |
|
47 | 48 | #include <optional>
|
48 | 49 | #include <tuple>
|
@@ -140,6 +141,11 @@ struct AsmPrinterOptions {
|
140 | 141 | llvm::cl::desc("Elide ElementsAttrs with \"...\" that have "
|
141 | 142 | "more elements than the given upper limit")};
|
142 | 143 |
|
| 144 | + llvm::cl::opt<unsigned> elideResourceStringsIfLarger{ |
| 145 | + "mlir-elide-resource-strings-if-larger", |
| 146 | + llvm::cl::desc( |
| 147 | + "Elide printing value of resources if string is too long in chars.")}; |
| 148 | + |
143 | 149 | llvm::cl::opt<bool> printDebugInfoOpt{
|
144 | 150 | "mlir-print-debuginfo", llvm::cl::init(false),
|
145 | 151 | llvm::cl::desc("Print debug info in MLIR output")};
|
@@ -191,6 +197,8 @@ OpPrintingFlags::OpPrintingFlags()
|
191 | 197 | return;
|
192 | 198 | if (clOptions->elideElementsAttrIfLarger.getNumOccurrences())
|
193 | 199 | elementsAttrElementLimit = clOptions->elideElementsAttrIfLarger;
|
| 200 | + if (clOptions->elideResourceStringsIfLarger.getNumOccurrences()) |
| 201 | + resourceStringCharLimit = clOptions->elideResourceStringsIfLarger; |
194 | 202 | printDebugInfoFlag = clOptions->printDebugInfoOpt;
|
195 | 203 | printDebugInfoPrettyFormFlag = clOptions->printPrettyDebugInfoOpt;
|
196 | 204 | printGenericOpFormFlag = clOptions->printGenericOpFormOpt;
|
@@ -262,6 +270,11 @@ std::optional<int64_t> OpPrintingFlags::getLargeElementsAttrLimit() const {
|
262 | 270 | return elementsAttrElementLimit;
|
263 | 271 | }
|
264 | 272 |
|
| 273 | +/// Return the size limit for printing large ElementsAttr. |
| 274 | +std::optional<uint64_t> OpPrintingFlags::getLargeResourceStringLimit() const { |
| 275 | + return resourceStringCharLimit; |
| 276 | +} |
| 277 | + |
265 | 278 | /// Return if debug information should be printed.
|
266 | 279 | bool OpPrintingFlags::shouldPrintDebugInfo() const {
|
267 | 280 | return printDebugInfoFlag;
|
@@ -3085,11 +3098,11 @@ class OperationPrinter : public AsmPrinter::Impl, private OpAsmPrinter {
|
3085 | 3098 | ~ResourceBuilder() override = default;
|
3086 | 3099 |
|
3087 | 3100 | void buildBool(StringRef key, bool data) final {
|
3088 |
| - printFn(key, [&](raw_ostream &os) { p.os << (data ? "true" : "false"); }); |
| 3101 | + printFn(key, [&](raw_ostream &os) { os << (data ? "true" : "false"); }); |
3089 | 3102 | }
|
3090 | 3103 |
|
3091 | 3104 | void buildString(StringRef key, StringRef data) final {
|
3092 |
| - printFn(key, [&](raw_ostream &os) { p.printEscapedString(data); }); |
| 3105 | + printFn(key, [&](raw_ostream &os) { os << "\"" << data << "\""; }); |
3093 | 3106 | }
|
3094 | 3107 |
|
3095 | 3108 | void buildBlob(StringRef key, ArrayRef<char> data,
|
@@ -3176,23 +3189,41 @@ void OperationPrinter::printResourceFileMetadata(
|
3176 | 3189 | auto printFn = [&](StringRef key, ResourceBuilder::ValueFn valueFn) {
|
3177 | 3190 | checkAddMetadataDict();
|
3178 | 3191 |
|
3179 |
| - // Emit the top-level resource entry if we haven't yet. |
3180 |
| - if (!std::exchange(hadResource, true)) { |
3181 |
| - if (needResourceComma) |
3182 |
| - os << "," << newLine; |
3183 |
| - os << " " << dictName << "_resources: {" << newLine; |
3184 |
| - } |
3185 |
| - // Emit the parent resource entry if we haven't yet. |
3186 |
| - if (!std::exchange(hadEntry, true)) { |
3187 |
| - if (needEntryComma) |
| 3192 | + auto printFormatting = [&]() { |
| 3193 | + // Emit the top-level resource entry if we haven't yet. |
| 3194 | + if (!std::exchange(hadResource, true)) { |
| 3195 | + if (needResourceComma) |
| 3196 | + os << "," << newLine; |
| 3197 | + os << " " << dictName << "_resources: {" << newLine; |
| 3198 | + } |
| 3199 | + // Emit the parent resource entry if we haven't yet. |
| 3200 | + if (!std::exchange(hadEntry, true)) { |
| 3201 | + if (needEntryComma) |
| 3202 | + os << "," << newLine; |
| 3203 | + os << " " << name << ": {" << newLine; |
| 3204 | + } else { |
3188 | 3205 | os << "," << newLine;
|
3189 |
| - os << " " << name << ": {" << newLine; |
| 3206 | + } |
| 3207 | + }; |
| 3208 | + |
| 3209 | + std::optional<uint64_t> charLimit = |
| 3210 | + printerFlags.getLargeResourceStringLimit(); |
| 3211 | + if (charLimit.has_value()) { |
| 3212 | + std::string resourceStr; |
| 3213 | + llvm::raw_string_ostream ss(resourceStr); |
| 3214 | + valueFn(ss); |
| 3215 | + |
| 3216 | + // Only print entry if it's string is small enough |
| 3217 | + if (resourceStr.size() > charLimit.value()) |
| 3218 | + return; |
| 3219 | + |
| 3220 | + printFormatting(); |
| 3221 | + os << " " << key << ": " << resourceStr; |
3190 | 3222 | } else {
|
3191 |
| - os << "," << newLine; |
| 3223 | + printFormatting(); |
| 3224 | + os << " " << key << ": "; |
| 3225 | + valueFn(os); |
3192 | 3226 | }
|
3193 |
| - |
3194 |
| - os << " " << key << ": "; |
3195 |
| - valueFn(os); |
3196 | 3227 | };
|
3197 | 3228 | ResourceBuilder entryBuilder(*this, printFn);
|
3198 | 3229 | provider.buildResources(op, providerArgs..., entryBuilder);
|
|
0 commit comments