Convert the Canonicalize and CSE passes to generic Operation Passes.

This allows for them to be used on other non-function, or even other function-like, operations. The algorithms are already generic, so this is simply changing the derived pass type. The majority of this change is just ensuring that the nesting of these passes remains the same, as the pass manager won't auto-nest them anymore.

PiperOrigin-RevId: 276573038
diff --git a/iree/compiler/IR/Interpreter/test/concat.mlir b/iree/compiler/IR/Interpreter/test/concat.mlir
index 750959e..9e4b589 100644
--- a/iree/compiler/IR/Interpreter/test/concat.mlir
+++ b/iree/compiler/IR/Interpreter/test/concat.mlir
@@ -1,4 +1,4 @@
-// RUN: iree-opt -canonicalize %s --split-input-file | FileCheck %s --dump-input=fail
+// RUN: iree-opt -pass-pipeline='func(canonicalize)' %s --split-input-file | FileCheck %s --dump-input=fail
 
 // CHECK-LABEL: func @concat.1D
 // CHECK-SAME: [[ARG0:%[a-zA-Z0-9]+]]
diff --git a/iree/compiler/IR/Sequencer/test/concat.mlir b/iree/compiler/IR/Sequencer/test/concat.mlir
index d5558a3..0746a29 100644
--- a/iree/compiler/IR/Sequencer/test/concat.mlir
+++ b/iree/compiler/IR/Sequencer/test/concat.mlir
@@ -1,4 +1,4 @@
-// RUN: iree-opt -canonicalize %s --split-input-file | FileCheck %s --dump-input=fail
+// RUN: iree-opt -pass-pipeline='func(canonicalize)' %s --split-input-file | FileCheck %s --dump-input=fail
 
 // CHECK-LABEL: func @concat.1D
 // CHECK-SAME: [[ARG0:%[a-zA-Z0-9]+]]
diff --git a/iree/compiler/IR/test/scalar_memref.mlir b/iree/compiler/IR/test/scalar_memref.mlir
index 861b619..c89eb358 100644
--- a/iree/compiler/IR/test/scalar_memref.mlir
+++ b/iree/compiler/IR/test/scalar_memref.mlir
@@ -1,4 +1,4 @@
-// RUN: iree-opt %s -canonicalize | FileCheck %s --dump-input=fail
+// RUN: iree-opt %s -pass-pipeline='func(canonicalize)' | FileCheck %s --dump-input=fail
 
 // CHECK-LABEL: @fold_memref_to_memref
 // CHECK-SAME: [[ARG:%[a-zA-Z0-9]+]]
diff --git a/iree/compiler/IR/test/tensor_memref.mlir b/iree/compiler/IR/test/tensor_memref.mlir
index d1642f8..0c66350 100644
--- a/iree/compiler/IR/test/tensor_memref.mlir
+++ b/iree/compiler/IR/test/tensor_memref.mlir
@@ -1,4 +1,4 @@
-// RUN: iree-opt %s -canonicalize | FileCheck %s --dump-input=fail
+// RUN: iree-opt %s -pass-pipeline='func(canonicalize)' | FileCheck %s --dump-input=fail
 
 // CHECK-LABEL: @fold_memref_to_memref
 // CHECK-SAME: [[ARG:%[a-zA-Z0-9]+]]
diff --git a/iree/compiler/Transforms/Interpreter/test/clone.mlir b/iree/compiler/Transforms/Interpreter/test/clone.mlir
index 0e51119..b3ee1ad 100644
--- a/iree/compiler/Transforms/Interpreter/test/clone.mlir
+++ b/iree/compiler/Transforms/Interpreter/test/clone.mlir
@@ -1,4 +1,4 @@
-// RUN: iree-opt %s -canonicalize -split-input-file | FileCheck %s --dump-input=fail
+// RUN: iree-opt %s -pass-pipeline='func(canonicalize)' -split-input-file | FileCheck %s --dump-input=fail
 
 // CHECK-LABEL: @necessary_clone_not_removed
 func @necessary_clone_not_removed() -> (memref<i32>, memref<i32>) {
diff --git a/iree/compiler/Translation/Interpreter/InterpreterExecutableTranslation.cpp b/iree/compiler/Translation/Interpreter/InterpreterExecutableTranslation.cpp
index d21d6e0..33528bb 100644
--- a/iree/compiler/Translation/Interpreter/InterpreterExecutableTranslation.cpp
+++ b/iree/compiler/Translation/Interpreter/InterpreterExecutableTranslation.cpp
@@ -53,25 +53,27 @@
 // Builds a pass pipeline that optimizes and legalizes the module to the form
 // expected by translation.
 void buildLegalizeInputPassPipeline(PassManager *passManager) {
+  OpPassManager &optPM = passManager->nest<FuncOp>();
+
   // Standard passes that shake out a lot of garbage.
   // Some may have been run prior to translation but this ensures we are always
   // in a known state.
-  passManager->addPass(createCanonicalizerPass());
-  passManager->addPass(createLoopFusionPass());
-  passManager->addPass(createLoopInvariantCodeMotionPass());
-  passManager->addPass(createMemRefDataFlowOptPass());
-  passManager->addPass(createCanonicalizerPass());
-  passManager->addPass(createSimplifyAffineStructuresPass());
-  passManager->addPass(createCSEPass());
-  passManager->addPass(createCanonicalizerPass());
+  optPM.addPass(createCanonicalizerPass());
+  optPM.addPass(createLoopFusionPass());
+  optPM.addPass(createLoopInvariantCodeMotionPass());
+  optPM.addPass(createMemRefDataFlowOptPass());
+  optPM.addPass(createCanonicalizerPass());
+  optPM.addPass(createSimplifyAffineStructuresPass());
+  optPM.addPass(createCSEPass());
+  optPM.addPass(createCanonicalizerPass());
 
   // Eliminate ops we don't care about based on a lack of side-effects.
   // IREE does not guarantee exception/error behavior of dead ops.
-  passManager->addPass(createAggressiveOpEliminationPass());
+  optPM.addPass(createAggressiveOpEliminationPass());
 
   // Expand uses of tuples into independent args/results.
   passManager->addPass(createConvertFromTupleCallingConventionPass());
-  passManager->addPass(createCanonicalizerPass());
+  passManager->addNestedPass<FuncOp>(createCanonicalizerPass());
 }
 
 // Builds a pass pipeline that converts functions to the iree_hl_interp dialect.
@@ -83,12 +85,12 @@
   // Convert to the memref calling convention and optimize away as many
   // loads and stores as we can prior to progressing.
   passManager->addPass(createConvertToMemRefCallingConventionPass());
-  passManager->addPass(createCanonicalizerPass());
+  passManager->addNestedPass<FuncOp>(createCanonicalizerPass());
   passManager->addPass(createMemRefDataFlowOptPass());
 
   // Convert various dialects to IREE opcodes and cleanup leftover conversions.
   passManager->addPass(createLowerToInterpreterDialectPass());
-  passManager->addPass(createCanonicalizerPass());
+  passManager->addNestedPass<FuncOp>(createCanonicalizerPass());
   passManager->addPass(createAggressiveOpEliminationPass());
 
   // Widen reduction functions (that have iree.executable.reduction attrs) to
@@ -102,13 +104,13 @@
 
   // Perform any last-minute optimizations to trim down the IR.
   passManager->addPass(createAggressiveOpEliminationPass());
-  passManager->addPass(createCanonicalizerPass());
+  passManager->addNestedPass<FuncOp>(createCanonicalizerPass());
   passManager->addPass(createLoopFusionPass());
   passManager->addPass(createLoopInvariantCodeMotionPass());
   passManager->addPass(createMemRefDataFlowOptPass());
-  passManager->addPass(createCanonicalizerPass());
-  passManager->addPass(createCSEPass());
-  passManager->addPass(createCanonicalizerPass());
+  passManager->addNestedPass<FuncOp>(createCanonicalizerPass());
+  passManager->addNestedPass<FuncOp>(createCSEPass());
+  passManager->addNestedPass<FuncOp>(createCanonicalizerPass());
 
   // Drop all functions that are not reachable.
   passManager->addPass(createDropUnreachableExecutableFunctionsPass());
diff --git a/iree/compiler/Translation/Sequencer/SequencerModuleTranslation.cpp b/iree/compiler/Translation/Sequencer/SequencerModuleTranslation.cpp
index 7184f6b..b88725d 100644
--- a/iree/compiler/Translation/Sequencer/SequencerModuleTranslation.cpp
+++ b/iree/compiler/Translation/Sequencer/SequencerModuleTranslation.cpp
@@ -70,18 +70,18 @@
   // Standard passes that shake out a lot of garbage.
   // Some may have been run prior to translation but this ensures we are always
   // in a known state.
-  passManager->addPass(createCanonicalizerPass());
+  passManager->addNestedPass<FuncOp>(createCanonicalizerPass());
   passManager->addPass(createLoopFusionPass());
   passManager->addPass(createLoopInvariantCodeMotionPass());
   passManager->addPass(createMemRefDataFlowOptPass());
-  passManager->addPass(createCanonicalizerPass());
+  passManager->addNestedPass<FuncOp>(createCanonicalizerPass());
   passManager->addPass(createSimplifyAffineStructuresPass());
-  passManager->addPass(createCSEPass());
-  passManager->addPass(createCanonicalizerPass());
+  passManager->addNestedPass<FuncOp>(createCSEPass());
+  passManager->addNestedPass<FuncOp>(createCanonicalizerPass());
 
   // Expand uses of tuples into independent args/results.
   passManager->addPass(createConvertFromTupleCallingConventionPass());
-  passManager->addPass(createCanonicalizerPass());
+  passManager->addNestedPass<FuncOp>(createCanonicalizerPass());
 }
 
 // Builds a pass pipeline that partitions the module into sequencer functions
@@ -92,11 +92,11 @@
   // fused reduction regions as possible. The remaining ops will be put into
   // dispatch regions.
   passManager->addPass(createIdentifyReductionRegionsPass());
-  passManager->addPass(createCSEPass());
+  passManager->addNestedPass<FuncOp>(createCSEPass());
 
   // Create all of the dispatch regions, CSE their workloads, and fold.
   passManager->addPass(createIdentifyDispatchRegionsPass());
-  passManager->addPass(createCSEPass());
+  passManager->addNestedPass<FuncOp>(createCSEPass());
   passManager->addPass(createFoldCompatibleDispatchRegionsPass());
 
   // Note that as we are rematerializing things here it's critical we do not run
@@ -110,7 +110,7 @@
   passManager->addPass(createOutlineReductionRegionsPass());
 
   // Cleanup identity sequencer tensor-to-memref ops that clutter up the IR.
-  passManager->addPass(createCanonicalizerPass());
+  passManager->addNestedPass<FuncOp>(createCanonicalizerPass());
 
   // Drop all functions that are no longer reachable.
   // This is important as many of the functions remaining are probably
@@ -136,7 +136,7 @@
 
   // Cleanup identity sequencer tensor-to-memref ops and other memory accesses
   // that clutter up the IR.
-  passManager->addPass(createCanonicalizerPass());
+  passManager->addNestedPass<FuncOp>(createCanonicalizerPass());
   passManager->addPass(createMemRefDataFlowOptPass());
 
   // Eliminate ops we don't care about based on a lack of side-effects.
@@ -144,9 +144,9 @@
   passManager->addPass(createAggressiveOpEliminationPass());
 
   // Perform any last-minute optimizations to trim down the IR.
-  passManager->addPass(createCanonicalizerPass());
+  passManager->addNestedPass<FuncOp>(createCanonicalizerPass());
   passManager->addPass(createMemRefDataFlowOptPass());
-  passManager->addPass(createCSEPass());
+  passManager->addNestedPass<FuncOp>(createCSEPass());
 }
 
 // Builds a pass pipeline that lowers the iree_seq.hl dialect to the iree_seq.ll
@@ -154,7 +154,7 @@
 void buildSequencerLoweringPassPipeline(PassManager *passManager) {
   // Lower iree_hl_seq -> iree_ll_seq.
   passManager->addPass(createLowerSequencerDialectPass());
-  passManager->addPass(createCanonicalizerPass());
+  passManager->addNestedPass<FuncOp>(createCanonicalizerPass());
   passManager->addPass(createMemRefDataFlowOptPass());
   passManager->addPass(createAggressiveOpEliminationPass());