1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include "llvm/Support/SourceMgr.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include "mlir/Dialect/SCF/SCF.h" // from @llvm-project
19 #include "mlir/Dialect/Shape/IR/Shape.h" // from @llvm-project
20 #include "mlir/Dialect/StandardOps/IR/Ops.h" // from @llvm-project
21 #include "mlir/IR/AsmState.h" // from @llvm-project
22 #include "mlir/IR/MLIRContext.h" // from @llvm-project
23 #include "mlir/IR/Verifier.h" // from @llvm-project
24 #include "mlir/Parser.h" // from @llvm-project
25 #include "pybind11/pybind11.h"
26 #include "pybind11/stl.h"
27 #include "tensorflow/compiler/mlir/tensorflow/dialect_registration.h"
28 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_executor.h"
29 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h"
30 #include "tensorflow/compiler/mlir/tfr/ir/tfr_ops.h"
31 #include "tensorflow/python/lib/core/pybind11_lib.h"
32 #include "tensorflow/python/lib/core/pybind11_status.h"
33
PYBIND11_MODULE(tfr_wrapper,m)34 PYBIND11_MODULE(tfr_wrapper, m) {
35 m.def("verify", [](std::string input) {
36 mlir::DialectRegistry registry;
37 registry.insert<mlir::scf::SCFDialect, mlir::TF::TensorFlowDialect,
38 mlir::StandardOpsDialect, mlir::shape::ShapeDialect,
39 mlir::TFR::TFRDialect>();
40 mlir::MLIRContext ctx(registry);
41 ctx.loadAllAvailableDialects();
42
43 llvm::SourceMgr source_mgr = llvm::SourceMgr();
44 source_mgr.AddNewSourceBuffer(llvm::MemoryBuffer::getMemBuffer(input),
45 llvm::SMLoc());
46 auto module = mlir::parseSourceFile(source_mgr, &ctx);
47 if (!module) {
48 return false;
49 }
50
51 mlir::SourceMgrDiagnosticHandler sourceMgrHandler(source_mgr, &ctx);
52 if (failed(mlir::verify(*module))) {
53 module->emitError("Invalid MLIR module: failed verification.");
54 return false;
55 }
56 return true;
57 });
58 }
59