Lower away unsupported XLA HLO operations prior to sequencer outlining
We don't actually support all of XLA HLO and want to get rid of e.g. While and GeneralDot before doing sequencer outlining
PiperOrigin-RevId: 275026865
diff --git a/iree/compiler/Transforms/Sequencer/BUILD b/iree/compiler/Transforms/Sequencer/BUILD
index 15120bb..777e28b 100644
--- a/iree/compiler/Transforms/Sequencer/BUILD
+++ b/iree/compiler/Transforms/Sequencer/BUILD
@@ -13,6 +13,7 @@
"FoldCompatibleDispatchRegions.cpp",
"IdentifyDispatchRegions.cpp",
"IdentifyReductionRegions.cpp",
+ "LegalizeInputs.cpp",
"LowerSequencerDialect.cpp",
"LowerStdToSequencerDialect.cpp",
"LowerToSequencerDialect.cpp",
diff --git a/iree/compiler/Transforms/Sequencer/CMakeLists.txt b/iree/compiler/Transforms/Sequencer/CMakeLists.txt
index 1396a6e..1e38610 100644
--- a/iree/compiler/Transforms/Sequencer/CMakeLists.txt
+++ b/iree/compiler/Transforms/Sequencer/CMakeLists.txt
@@ -24,6 +24,7 @@
"FoldCompatibleDispatchRegions.cpp"
"IdentifyDispatchRegions.cpp"
"IdentifyReductionRegions.cpp"
+ "LegalizeInputs.cpp"
"LowerSequencerDialect.cpp"
"LowerStdToSequencerDialect.cpp"
"LowerToSequencerDialect.cpp"
diff --git a/iree/compiler/Transforms/Sequencer/LegalizeInputs.cpp b/iree/compiler/Transforms/Sequencer/LegalizeInputs.cpp
new file mode 100644
index 0000000..5d79791
--- /dev/null
+++ b/iree/compiler/Transforms/Sequencer/LegalizeInputs.cpp
@@ -0,0 +1,53 @@
+// Copyright 2019 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "iree/compiler/IR/Dialect.h"
+#include "iree/compiler/Transforms/Rewrites.h"
+#include "iree/compiler/Transforms/Sequencer/Rewrites.h"
+#include "mlir/Dialect/StandardOps/Ops.h"
+#include "mlir/IR/PatternMatch.h"
+#include "mlir/Pass/Pass.h"
+#include "mlir/Transforms/DialectConversion.h"
+#include "tensorflow/compiler/mlir/xla/ir/hlo_ops.h"
+#include "tensorflow/compiler/mlir/xla/transforms/rewriters.h"
+
+namespace mlir {
+namespace iree_compiler {
+namespace {
+
+class LegalizeInputOpsPass
+ : public FunctionPass<LegalizeInputOpsPass> {
+ public:
+ void runOnFunction() override {
+ OwningRewritePatternList patterns;
+ xla_hlo::PopulateGeneralDotOpLoweringPatterns(&patterns, &getContext());
+
+ ConversionTarget target(getContext());
+ target.addLegalDialect<xla_hlo::XlaHloDialect, StandardOpsDialect>();
+ target.addLegalOp<FuncOp, ReturnOp>();
+ target.addIllegalOp<xla_hlo::DotGeneralOp, xla_hlo::WhileOp>();
+ if (failed(applyFullConversion(getFunction(), target, patterns))) {
+ return signalPassFailure();
+ }
+ }
+};
+
+} // namespace
+
+std::unique_ptr<OpPassBase<FuncOp>> createLegalizeInputOpsPass() {
+ return std::make_unique<LegalizeInputOpsPass>();
+}
+
+} // namespace iree_compiler
+} // namespace mlir
diff --git a/iree/compiler/Transforms/Sequencer/Passes.h b/iree/compiler/Transforms/Sequencer/Passes.h
index 37a2d8b..1a4e402 100644
--- a/iree/compiler/Transforms/Sequencer/Passes.h
+++ b/iree/compiler/Transforms/Sequencer/Passes.h
@@ -51,6 +51,10 @@
// Lowering/Conversion
//===----------------------------------------------------------------------===//
+// Lowers inputs to legal IREE inputs (a subset of XLA and Standard ops). Also
+// serves to verify input is only made up of known ops.
+std::unique_ptr<OpPassBase<FuncOp>> createLegalizeInputOpsPass();
+
// Lowers input dialect ops (e.g. std, xla_hlo) to IREE Sequencer HL dialect.
std::unique_ptr<OpPassBase<FuncOp>> createLowerToSequencerDialectPass();
diff --git a/iree/compiler/Translation/Sequencer/SequencerModuleTranslation.cpp b/iree/compiler/Translation/Sequencer/SequencerModuleTranslation.cpp
index 672bb6b..c44b007 100644
--- a/iree/compiler/Translation/Sequencer/SequencerModuleTranslation.cpp
+++ b/iree/compiler/Translation/Sequencer/SequencerModuleTranslation.cpp
@@ -62,6 +62,11 @@
// Builds a pass pipeline that optimizes and legalizes the module to the form
// expected by partitioning.
void buildLegalizeInputPassPipeline(PassManager *passManager) {
+ // Convert to the subset of XLA HLO and Standard dialects supported as IREE
+ // input. In particular, move from XLA HLO to standard control flow.
+ passManager->addPass(xla_hlo::createLegalizeControlFlowPass());
+ passManager->addPass(createLegalizeInputOpsPass());
+
// 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.
@@ -74,9 +79,6 @@
passManager->addPass(createCSEPass());
passManager->addPass(createCanonicalizerPass());
- // Get out of XLA HLO into a sane control flow representation.
- passManager->addPass(xla_hlo::createLegalizeControlFlowPass());
-
// Expand uses of tuples into independent args/results.
passManager->addPass(createConvertFromTupleCallingConventionPass());
passManager->addPass(createCanonicalizerPass());