Skip to content

Commit 051ed1c

Browse files
joker-ephtensorflower-gardener
authored andcommitted
Remove dependency on Dialect global registration from //tensorflow/compiler/mlir/lite/...
PiperOrigin-RevId: 328109152 Change-Id: Ia460e89f785e9a2aaf21538083733e7e13730299
1 parent 227b348 commit 051ed1c

10 files changed

Lines changed: 35 additions & 25 deletions

File tree

‎tensorflow/compiler/mlir/lite/BUILD‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ tf_cc_binary(
760760
deps = [
761761
":flatbuffer_translate_registeration",
762762
# TODO(b/155809683): Link only necessary dialects.
763-
"@llvm-project//mlir:AllPassesAndDialects",
763+
"@llvm-project//mlir:AllPassesAndDialectsNoRegistration",
764764
],
765765
)
766766

@@ -812,7 +812,7 @@ tf_cc_binary(
812812
"@com_google_absl//absl/strings",
813813
"@llvm-project//llvm:Support",
814814
# TODO(b/155809683): Link only necessary dialects.
815-
"@llvm-project//mlir:AllPassesAndDialects",
815+
"@llvm-project//mlir:AllPassesAndDialectsNoRegistration",
816816
"@llvm-project//mlir:IR",
817817
"@llvm-project//mlir:Pass",
818818
"@llvm-project//mlir:Support",
@@ -836,19 +836,18 @@ tf_cc_binary(
836836
deps = [
837837
":flatbuffer_translate_lib",
838838
":flatbuffer_translate_registeration",
839-
"@com_google_absl//absl/strings",
840-
"@llvm-project//llvm:Support",
841-
# TODO(b/155809683): Link only necessary dialects.
842-
"@llvm-project//mlir:AllPassesAndDialects",
843-
"@llvm-project//mlir:IR",
844-
"@llvm-project//mlir:Parser",
845-
"@llvm-project//mlir:Support",
846-
"//tensorflow/compiler/mlir/tensorflow:mlir_roundtrip_flags",
839+
":tensorflow_lite",
840+
"//tensorflow/compiler/mlir/tensorflow",
847841
"//tensorflow/core:lib",
848842
"//tensorflow/core/platform:logging",
849843
"//tensorflow/lite:framework",
850844
"//tensorflow/lite/delegates/flex:delegate",
851845
"//tensorflow/lite/kernels:builtin_ops",
846+
"@com_google_absl//absl/strings",
847+
"@llvm-project//llvm:Support",
848+
"@llvm-project//mlir:IR",
849+
"@llvm-project//mlir:Parser",
850+
"@llvm-project//mlir:StandardOps",
852851
],
853852
)
854853

@@ -875,7 +874,7 @@ cc_library(
875874
"//tensorflow/compiler/mlir/tensorflow:translate_lib",
876875
"//tensorflow/core:core_cpu_base",
877876
"@llvm-project//llvm:Support",
878-
"@llvm-project//mlir:AllPassesAndDialects",
877+
"@llvm-project//mlir:AllPassesAndDialectsNoRegistration",
879878
"@llvm-project//mlir:IR",
880879
"@llvm-project//mlir:Pass",
881880
"@llvm-project//mlir:Transforms",
@@ -909,7 +908,7 @@ cc_library(
909908
"//tensorflow/stream_executor/lib",
910909
"@com_google_absl//absl/types:span",
911910
"@llvm-project//llvm:Support",
912-
"@llvm-project//mlir:AllPassesAndDialects",
911+
"@llvm-project//mlir:AllPassesAndDialectsNoRegistration",
913912
"@llvm-project//mlir:IR",
914913
"@llvm-project//mlir:Parser",
915914
"@llvm-project//mlir:Pass",

‎tensorflow/compiler/mlir/lite/mlir_tflite_runner.cc‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,16 @@ limitations under the License.
3030
#include "llvm/Support/MemoryBuffer.h"
3131
#include "llvm/Support/SMLoc.h"
3232
#include "llvm/Support/SourceMgr.h"
33+
#include "mlir/Dialect/StandardOps/IR/Ops.h" // from @llvm-project
34+
#include "mlir/IR/Dialect.h" // from @llvm-project
3335
#include "mlir/IR/Function.h" // from @llvm-project
3436
#include "mlir/IR/MLIRContext.h" // from @llvm-project
3537
#include "mlir/IR/Module.h" // from @llvm-project
3638
#include "mlir/Parser.h" // from @llvm-project
3739
#include "tensorflow/compiler/mlir/lite/flatbuffer_export.h"
3840
#include "tensorflow/compiler/mlir/lite/flatbuffer_export_flags.h"
41+
#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h"
42+
#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h"
3943
#include "tensorflow/core/platform/init_main.h"
4044
#include "tensorflow/core/platform/logging.h"
4145
#include "tensorflow/lite/delegates/flex/delegate.h"
@@ -98,7 +102,10 @@ int main(int argc, char** argv) {
98102

99103
// Load the MLIR module.
100104
mlir::MLIRContext context;
101-
context.loadAllGloballyRegisteredDialects();
105+
context.getDialectRegistry()
106+
.insert<mlir::TF::TensorFlowDialect, mlir::TFL::TensorFlowLiteDialect,
107+
mlir::StandardOpsDialect>();
108+
102109
llvm::SourceMgr source_mgr;
103110
source_mgr.AddNewSourceBuffer(std::move(*file_or_err), llvm::SMLoc());
104111
mlir::OwningModuleRef module(mlir::parseSourceFile(source_mgr, &context));

‎tensorflow/compiler/mlir/lite/python/graphdef_to_tfl_flatbuffer.cc‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ Status ConvertGraphDefToTFLiteFlatBuffer(const toco::ModelFlags& model_flags,
4949
const GraphDef& input,
5050
string* result) {
5151
mlir::MLIRContext context;
52-
context.loadAllGloballyRegisteredDialects();
5352
GraphImportConfig specs;
5453
mlir::TFL::QuantizationSpecs quant_specs;
5554

‎tensorflow/compiler/mlir/lite/python/saved_model_to_tfl_flatbuffer.cc‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ Status ConvertSavedModelToTFLiteFlatBuffer(
122122
const toco::ModelFlags& model_flags, const toco::TocoFlags& toco_flags,
123123
string* result) {
124124
mlir::MLIRContext context;
125-
context.loadAllGloballyRegisteredDialects();
126125
mlir::TFL::QuantizationSpecs quant_specs;
127126

128127
// Parse input arrays.

‎tensorflow/compiler/mlir/lite/quantization/import_quant_stats_pass.cc‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ class ImportQuantStatsPass
6262

6363
void runOnFunction() override;
6464

65+
void getDependentDialects(DialectRegistry &registry) const override {
66+
registry.insert<quant::QuantizationDialect>();
67+
}
68+
6569
// Parses the serialized quant stats protobuf and initialize the internal
6670
// data structure. This method must be called after the pass is created.
6771
bool ParseQuantStats(const std::string &stats_str);

‎tensorflow/compiler/mlir/lite/quantization/lite/BUILD‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ cc_library(
2828
deps = [
2929
"//tensorflow/compiler/mlir/lite:common",
3030
"//tensorflow/compiler/mlir/lite:flatbuffer_translate_lib",
31+
"//tensorflow/compiler/mlir/lite:tensorflow_lite",
3132
"//tensorflow/compiler/mlir/lite:tensorflow_lite_quantize",
3233
"//tensorflow/compiler/mlir/lite/quantization:quantization_config",
3334
"//tensorflow/compiler/mlir/tensorflow:error_util",
@@ -74,6 +75,6 @@ tf_cc_binary(
7475
"//tensorflow/lite/schema:schema_fbs",
7576
"@com_google_absl//absl/strings",
7677
"@llvm-project//llvm:Support",
77-
"@llvm-project//mlir:AllPassesAndDialects",
78+
"@llvm-project//mlir:AllPassesAndDialectsNoRegistration",
7879
],
7980
)

‎tensorflow/compiler/mlir/lite/quantization/lite/quantize_model.cc‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ limitations under the License.
2525
#include "tensorflow/compiler/mlir/lite/common/tfl_pass_config.h"
2626
#include "tensorflow/compiler/mlir/lite/flatbuffer_export.h"
2727
#include "tensorflow/compiler/mlir/lite/flatbuffer_import.h"
28+
#include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h"
2829
#include "tensorflow/compiler/mlir/lite/quantization/quantization_config.h"
2930
#include "tensorflow/compiler/mlir/lite/transforms/passes.h"
3031
#include "tensorflow/compiler/mlir/lite/utils/convert_type.h"
@@ -52,7 +53,7 @@ TfLiteStatus QuantizeModel(
5253
}
5354

5455
MLIRContext context;
55-
context.loadAllGloballyRegisteredDialects();
56+
context.getDialectRegistry().insert<mlir::TFL::TensorFlowLiteDialect>();
5657
StatusScopedDiagnosticHandler statusHandler(&context,
5758
/*propagate=*/true);
5859

‎tensorflow/compiler/mlir/lite/sparsity/sparsify_model.cc‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ TfLiteStatus SparsifyModel(const tflite::ModelT& input_model,
3737
flatbuffers::FlatBufferBuilder* builder,
3838
tflite::ErrorReporter* error_reporter) {
3939
MLIRContext context;
40-
context.loadAllGloballyRegisteredDialects();
4140
StatusScopedDiagnosticHandler statusHandler(&context,
4241
/*propagate=*/true);
4342

‎tensorflow/compiler/mlir/lite/transforms/prepare_tf.cc‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ limitations under the License.
4040
#include "llvm/Support/Debug.h"
4141
#include "mlir/Analysis/LoopAnalysis.h" // from @llvm-project
4242
#include "mlir/Dialect/Quant/FakeQuantSupport.h" // from @llvm-project
43+
#include "mlir/Dialect/Quant/QuantOps.h" // from @llvm-project
4344
#include "mlir/Dialect/Quant/UniformSupport.h" // from @llvm-project
4445
#include "mlir/Dialect/StandardOps/IR/Ops.h" // from @llvm-project
4546
#include "mlir/IR/Attributes.h" // from @llvm-project
@@ -84,6 +85,11 @@ class PrepareTFPass : public PassWrapper<PrepareTFPass, FunctionPass> {
8485
: unfold_batch_matmul_(unfold_batch_matmul) {}
8586
void runOnFunction() override;
8687

88+
void getDependentDialects(DialectRegistry &registry) const override {
89+
registry.insert<mhlo::MhloDialect, quant::QuantizationDialect,
90+
TFL::TensorFlowLiteDialect>();
91+
}
92+
8793
private:
8894
bool unfold_batch_matmul_;
8995
};

‎tensorflow/compiler/mlir/lite/utils/lstm_utils_test.cc‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ class LstmUtilsTest : public ::testing::Test {
9393
LstmUtilsTest() {}
9494

9595
void SetUp() override {
96-
RegisterDialects();
9796
context_ = std::make_unique<mlir::MLIRContext>();
97+
context_->loadDialect<mlir::StandardOpsDialect, mlir::TF::TensorFlowDialect,
98+
TensorFlowLiteDialect>();
9899
builder_ = std::unique_ptr<mlir::Builder>(new Builder(context_.get()));
99100
fused_lstm_func_ = createLstmCompositeFunc(builder_.get(), false, false);
100101
fused_lstm_func_cifg_ =
@@ -109,12 +110,6 @@ class LstmUtilsTest : public ::testing::Test {
109110
builder_.reset();
110111
}
111112

112-
void RegisterDialects() {
113-
mlir::registerDialect<mlir::StandardOpsDialect>();
114-
mlir::registerDialect<mlir::TF::TensorFlowDialect>();
115-
mlir::registerDialect<TensorFlowLiteDialect>();
116-
}
117-
118113
FuncOp fused_lstm_func_;
119114
FuncOp fused_lstm_func_cifg_;
120115
FuncOp fused_ln_lstm_func_;

0 commit comments

Comments
 (0)