[Mlir-commits] [mlir] 737391b - [mlir] Slightly optimize getRegions checks by inlining size check

River Riddle llvmlistbot at llvm.org
Mon Dec 5 11:32:00 PST 2022


Author: River Riddle
Date: 2022-12-05T11:31:50-08:00
New Revision: 737391bdf3d09fdb6789b24891b07a2668d3e44f

URL: https://github.com/llvm/llvm-project/commit/737391bdf3d09fdb6789b24891b07a2668d3e44f
DIFF: https://github.com/llvm/llvm-project/commit/737391bdf3d09fdb6789b24891b07a2668d3e44f.diff

LOG: [mlir] Slightly optimize getRegions checks by inlining size check

Calculating the position of the region trailing objects isn't free,
given that it's the last trailing object, and inlining the size check
removes the need for users to explicitly add size checks for
micro-optimization.

Added: 
    

Modified: 
    mlir/include/mlir/IR/Operation.h
    mlir/lib/IR/AsmPrinter.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/IR/Operation.h b/mlir/include/mlir/IR/Operation.h
index b7945710bd701..3192438c68e29 100644
--- a/mlir/include/mlir/IR/Operation.h
+++ b/mlir/include/mlir/IR/Operation.h
@@ -478,6 +478,10 @@ class alignas(8) Operation final
 
   /// Returns the regions held by this operation.
   MutableArrayRef<Region> getRegions() {
+    // Check the count first, as computing the trailing objects can be slow.
+    if (numRegions == 0)
+      return MutableArrayRef<Region>();
+
     auto *regions = getTrailingObjects<Region>();
     return {regions, numRegions};
   }

diff  --git a/mlir/lib/IR/AsmPrinter.cpp b/mlir/lib/IR/AsmPrinter.cpp
index 3f7442d4e306e..b528d826fe068 100644
--- a/mlir/lib/IR/AsmPrinter.cpp
+++ b/mlir/lib/IR/AsmPrinter.cpp
@@ -618,11 +618,9 @@ class DummyAliasOperationPrinter : private OpAsmPrinter {
   /// Print the given operation in the generic form.
   void printGenericOp(Operation *op, bool printOpName = true) override {
     // Consider nested operations for aliases.
-    if (op->getNumRegions() != 0) {
-      for (Region &region : op->getRegions())
-        printRegion(region, /*printEntryBlockArgs=*/true,
-                    /*printBlockTerminators=*/true);
-    }
+    for (Region &region : op->getRegions())
+      printRegion(region, /*printEntryBlockArgs=*/true,
+                  /*printBlockTerminators=*/true);
 
     // Visit all the types used in the operation.
     for (Type type : op->getOperandTypes())


        


More information about the Mlir-commits mailing list