From: Mehdi Amini Date: Mon, 13 Mar 2023 13:33:31 +0000 (+0100) Subject: Add a `skipRegion()` feature to the OpPrintingFlags for MLIR ASM printer X-Git-Tag: upstream/17.0.6~14284 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=5736a8a2dab438c8266c79b49d5d5c90f78fdcb4;p=platform%2Fupstream%2Fllvm.git Add a `skipRegion()` feature to the OpPrintingFlags for MLIR ASM printer This is a convenient flag for context where we intend to summarize a top-level operation without the full-blown regions it may hold. Reviewed By: rriddle Differential Revision: https://reviews.llvm.org/D145889 --- diff --git a/mlir/include/mlir/IR/OperationSupport.h b/mlir/include/mlir/IR/OperationSupport.h index ebeb0a9..ba8f0a8 100644 --- a/mlir/include/mlir/IR/OperationSupport.h +++ b/mlir/include/mlir/IR/OperationSupport.h @@ -834,6 +834,9 @@ public: /// Always print operations in the generic form. OpPrintingFlags &printGenericOpForm(); + /// Skip printing regions. + OpPrintingFlags &skipRegions(); + /// Do not verify the operation when using custom operation printers. OpPrintingFlags &assumeVerified(); @@ -861,6 +864,9 @@ public: /// Return if operations should be printed in the generic form. bool shouldPrintGenericOpForm() const; + /// Return if regions should be skipped. + bool shouldSkipRegions() const; + /// Return if operation verification should be skipped. bool shouldAssumeVerified() const; @@ -882,6 +888,9 @@ private: /// Print operations in the generic form. bool printGenericOpFormFlag : 1; + /// Always skip Regions. + bool skipRegionsFlag : 1; + /// Skip operation verification. bool assumeVerifiedFlag : 1; diff --git a/mlir/lib/IR/AsmPrinter.cpp b/mlir/lib/IR/AsmPrinter.cpp index 18c35f0..dd31125 100644 --- a/mlir/lib/IR/AsmPrinter.cpp +++ b/mlir/lib/IR/AsmPrinter.cpp @@ -183,8 +183,9 @@ void mlir::registerAsmPrinterCLOptions() { /// Initialize the printing flags with default supplied by the cl::opts above. OpPrintingFlags::OpPrintingFlags() : printDebugInfoFlag(false), printDebugInfoPrettyFormFlag(false), - printGenericOpFormFlag(false), assumeVerifiedFlag(false), - printLocalScope(false), printValueUsersFlag(false) { + printGenericOpFormFlag(false), skipRegionsFlag(false), + assumeVerifiedFlag(false), printLocalScope(false), + printValueUsersFlag(false) { // Initialize based upon command line options, if they are available. if (!clOptions.isConstructed()) return; @@ -223,6 +224,12 @@ OpPrintingFlags &OpPrintingFlags::printGenericOpForm() { return *this; } +/// Always skip Regions. +OpPrintingFlags &OpPrintingFlags::skipRegions() { + skipRegionsFlag = true; + return *this; +} + /// Do not verify the operation when using custom operation printers. OpPrintingFlags &OpPrintingFlags::assumeVerified() { assumeVerifiedFlag = true; @@ -270,6 +277,9 @@ bool OpPrintingFlags::shouldPrintGenericOpForm() const { return printGenericOpFormFlag; } +/// Return if Region should be skipped. +bool OpPrintingFlags::shouldSkipRegions() const { return skipRegionsFlag; } + /// Return if operation verification should be skipped. bool OpPrintingFlags::shouldAssumeVerified() const { return assumeVerifiedFlag; @@ -614,9 +624,11 @@ private: /// Print the given operation in the generic form. void printGenericOp(Operation *op, bool printOpName = true) override { // Consider nested operations for aliases. - for (Region ®ion : op->getRegions()) - printRegion(region, /*printEntryBlockArgs=*/true, - /*printBlockTerminators=*/true); + if (!printerFlags.shouldSkipRegions()) { + for (Region ®ion : op->getRegions()) + printRegion(region, /*printEntryBlockArgs=*/true, + /*printBlockTerminators=*/true); + } // Visit all the types used in the operation. for (Type type : op->getOperandTypes()) @@ -665,6 +677,10 @@ private: bool printEmptyBlock = false) override { if (region.empty()) return; + if (printerFlags.shouldSkipRegions()) { + os << "{...}"; + return; + } auto *entryBlock = ®ion.front(); print(entryBlock, printEntryBlockArgs, printBlockTerminators); @@ -3463,6 +3479,10 @@ void OperationPrinter::printSuccessorAndUseList(Block *successor, void OperationPrinter::printRegion(Region ®ion, bool printEntryBlockArgs, bool printBlockTerminators, bool printEmptyBlock) { + if (printerFlags.shouldSkipRegions()) { + os << "{...}"; + return; + } os << "{" << newLine; if (!region.empty()) { auto restoreDefaultDialect = diff --git a/mlir/lib/Tools/mlir-lsp-server/MLIRServer.cpp b/mlir/lib/Tools/mlir-lsp-server/MLIRServer.cpp index 397f259..8597423 100644 --- a/mlir/lib/Tools/mlir-lsp-server/MLIRServer.cpp +++ b/mlir/lib/Tools/mlir-lsp-server/MLIRServer.cpp @@ -516,23 +516,12 @@ std::optional MLIRDocument::buildHoverForOperation( os << "Generic Form:\n\n```mlir\n"; - // Temporary drop the regions of this operation so that they don't get - // printed in the output. This helps keeps the size of the output hover - // small. - SmallVector> regions; - for (Region ®ion : op.op->getRegions()) { - regions.emplace_back(std::make_unique()); - regions.back()->takeBody(region); - } - - op.op->print( - os, OpPrintingFlags().printGenericOpForm().elideLargeElementsAttrs()); + op.op->print(os, OpPrintingFlags() + .printGenericOpForm() + .elideLargeElementsAttrs() + .skipRegions()); os << "\n```\n"; - // Move the regions back to the current operation. - for (Region ®ion : op.op->getRegions()) - region.takeBody(*regions.back()); - return hover; } diff --git a/mlir/test/mlir-lsp-server/hover.test b/mlir/test/mlir-lsp-server/hover.test index df0fe20..3a8a443 100644 --- a/mlir/test/mlir-lsp-server/hover.test +++ b/mlir/test/mlir-lsp-server/hover.test @@ -114,7 +114,7 @@ // CHECK-NEXT: "result": { // CHECK-NEXT: "contents": { // CHECK-NEXT: "kind": "markdown", -// CHECK-NEXT: "value": "\"func.func\" : public @foo\n\nGeneric Form:\n\n```mlir\n\"func.func\"() ({\n}) {function_type = (i1) -> (), sym_name = \"foo\"} : () -> ()\n```\n" +// CHECK-NEXT: "value": "\"func.func\" : public @foo\n\nGeneric Form:\n\n```mlir\n\"func.func\"() ({...}) {function_type = (i1) -> (), sym_name = \"foo\"} : () -> ()\n```\n" // CHECK-NEXT: }, // CHECK-NEXT: "range": { // CHECK-NEXT: "end": { @@ -138,7 +138,7 @@ // CHECK-NEXT: "result": { // CHECK-NEXT: "contents": { // CHECK-NEXT: "kind": "markdown", -// CHECK-NEXT: "value": "\"func.func\" : public @foo\n\nGeneric Form:\n\n```mlir\n\"func.func\"() ({\n}) {function_type = (i1) -> (), sym_name = \"foo\"} : () -> ()\n```\n" +// CHECK-NEXT: "value": "\"func.func\" : public @foo\n\nGeneric Form:\n\n```mlir\n\"func.func\"() ({...}) {function_type = (i1) -> (), sym_name = \"foo\"} : () -> ()\n```\n" // CHECK-NEXT: }, // CHECK-NEXT: "range": { // CHECK-NEXT: "end": {