From 11a22c7c50ebcaea9cb7667b385429551f7d84c8 Mon Sep 17 00:00:00 2001 From: Tres Popp Date: Fri, 13 Mar 2020 07:40:41 -0700 Subject: [PATCH] Update MLIR API. PiperOrigin-RevId: 300753498 Change-Id: I0df71658d4be2801ed90e701186b82d2f68d12e0 --- tensorflow/compiler/mlir/lite/BUILD | 1 + tensorflow/compiler/mlir/lite/ir/tfl_ops.td | 3 +- .../mlir/lite/transforms/extract_ophint.cc | 4 +- .../transforms/optimize_functional_ops.cc | 3 +- .../lite/transforms/while_loop_outline.cc | 1 - tensorflow/compiler/mlir/tensorflow/BUILD | 5 +- .../analysis/side_effect_analysis.cc | 6 ++- .../mlir/tensorflow/ir/control_flow_ops.h | 18 +++++-- .../compiler/mlir/tensorflow/ir/tf_op_base.td | 1 + .../compiler/mlir/tensorflow/ir/tf_ops.td | 2 +- .../tensorflow/transforms/constant_fold.cc | 4 +- tensorflow/compiler/mlir/xla/BUILD | 2 + tensorflow/compiler/mlir/xla/ir/hlo_ops.td | 4 +- tensorflow/compiler/mlir/xla/ir/lhlo_ops.td | 1 + .../xla/service/cpu/vector_support_library.h | 3 +- tensorflow/workspace.bzl | 4 +- third_party/mlir/BUILD | 50 ++++++++++++++++++- third_party/mlir/test.BUILD | 2 +- 18 files changed, 95 insertions(+), 19 deletions(-) diff --git a/tensorflow/compiler/mlir/lite/BUILD b/tensorflow/compiler/mlir/lite/BUILD index 60214c10b94..e048f9bc9f8 100644 --- a/tensorflow/compiler/mlir/lite/BUILD +++ b/tensorflow/compiler/mlir/lite/BUILD @@ -30,6 +30,7 @@ filegroup( "ir/tfl_ops.td", "//tensorflow/compiler/mlir/lite/quantization:quantization_td_files", "@llvm-project//mlir:OpBaseTdFiles", + "@llvm-project//mlir:include/mlir/Interfaces/SideEffects.td", "@llvm-project//mlir:include/mlir/Transforms/LoopLikeInterface.td", ], ) diff --git a/tensorflow/compiler/mlir/lite/ir/tfl_ops.td b/tensorflow/compiler/mlir/lite/ir/tfl_ops.td index e6dbcd2046f..970bfaca0fd 100644 --- a/tensorflow/compiler/mlir/lite/ir/tfl_ops.td +++ b/tensorflow/compiler/mlir/lite/ir/tfl_ops.td @@ -19,6 +19,7 @@ limitations under the License. #define TFL_OPS include "mlir/IR/OpBase.td" +include "mlir/Interfaces/SideEffects.td" include "mlir/Transforms/LoopLikeInterface.td" include "tensorflow/compiler/mlir/lite/ir/tfl_op_interfaces.td" include "tensorflow/compiler/mlir/lite/quantization/quantization.td" @@ -596,7 +597,7 @@ def TFL_ConcatenationOp : TFL_Op<"concatenation", let verifier = [{ return Verify(*this); }]; } -def TFL_ConstOp : Op { let summary = "Constant pseudo op."; diff --git a/tensorflow/compiler/mlir/lite/transforms/extract_ophint.cc b/tensorflow/compiler/mlir/lite/transforms/extract_ophint.cc index 3582046f13f..5893d4f3779 100644 --- a/tensorflow/compiler/mlir/lite/transforms/extract_ophint.cc +++ b/tensorflow/compiler/mlir/lite/transforms/extract_ophint.cc @@ -426,7 +426,9 @@ void PreprocessTopoSortGraph( } bool IsSideEffectOp(Operation* op) { - if (op->hasNoSideEffect()) return false; + // TODO(riverriddle) Properly handle region side effects. + if (MemoryEffectOpInterface::hasNoEffect(op) && op->getNumRegions() == 0) + return false; // Identity op has no side effect. // Check the OperationName maybe more elegant here. diff --git a/tensorflow/compiler/mlir/lite/transforms/optimize_functional_ops.cc b/tensorflow/compiler/mlir/lite/transforms/optimize_functional_ops.cc index 82e0ec0192f..83ecf0be820 100644 --- a/tensorflow/compiler/mlir/lite/transforms/optimize_functional_ops.cc +++ b/tensorflow/compiler/mlir/lite/transforms/optimize_functional_ops.cc @@ -60,7 +60,8 @@ static void UpdateFuncType(FuncOp func) { static bool IsSideEffectFree(FuncOp func) { return !func.getBody() .walk([&](Operation* op) { - if (!op->isKnownTerminator() && !op->hasNoSideEffect()) + if (!MemoryEffectOpInterface::hasNoEffect(op) && + !op->isKnownTerminator()) return WalkResult::interrupt(); return WalkResult::advance(); }) diff --git a/tensorflow/compiler/mlir/lite/transforms/while_loop_outline.cc b/tensorflow/compiler/mlir/lite/transforms/while_loop_outline.cc index 8ed5b0e0341..be024eccd45 100644 --- a/tensorflow/compiler/mlir/lite/transforms/while_loop_outline.cc +++ b/tensorflow/compiler/mlir/lite/transforms/while_loop_outline.cc @@ -98,7 +98,6 @@ void WhileOutlinePass::OutlineWhile(WhileOp while_op) { extern_values.insert(extern_value); continue; } - assert(extern_value.getDefiningOp()->hasNoSideEffect()); if (!const_none) { // Add constant at start of region. auto const_builder = diff --git a/tensorflow/compiler/mlir/tensorflow/BUILD b/tensorflow/compiler/mlir/tensorflow/BUILD index 7d65a01e2c5..3a2aec48699 100644 --- a/tensorflow/compiler/mlir/tensorflow/BUILD +++ b/tensorflow/compiler/mlir/tensorflow/BUILD @@ -26,6 +26,7 @@ filegroup( "ir/tf_ops.td", "@llvm-project//mlir:OpBaseTdFiles", "@llvm-project//mlir:include/mlir/Interfaces/CallInterfaces.td", + "@llvm-project//mlir:include/mlir/Interfaces/SideEffects.td", ], ) @@ -801,9 +802,8 @@ cc_library( "//tensorflow/stream_executor", "//tensorflow/stream_executor/lib", "@llvm-project//llvm:support", - "@llvm-project//mlir:Analysis", "@llvm-project//mlir:IR", - "@llvm-project//mlir:Pass", + "@llvm-project//mlir:SideEffects", "@llvm-project//mlir:Support", ], alwayslink = 1, @@ -958,6 +958,7 @@ genrule( name = "derived_attr_populator_inc", srcs = [ "@llvm-project//mlir:include/mlir/Interfaces/CallInterfaces.td", + "@llvm-project//mlir:include/mlir/Interfaces/SideEffects.td", "@llvm-project//mlir:include/mlir/IR/OpBase.td", "ir/tf_generated_ops.td", "ir/tf_op_base.td", diff --git a/tensorflow/compiler/mlir/tensorflow/analysis/side_effect_analysis.cc b/tensorflow/compiler/mlir/tensorflow/analysis/side_effect_analysis.cc index b8b45aaa025..931f24b9606 100644 --- a/tensorflow/compiler/mlir/tensorflow/analysis/side_effect_analysis.cc +++ b/tensorflow/compiler/mlir/tensorflow/analysis/side_effect_analysis.cc @@ -323,7 +323,11 @@ bool OpIsDeclaration(Operation* op, bool OpIsKnownToHaveNoSideEffect(Operation* op) { // TODO(riverriddle) We shouldn't treat all terminator operations as having // side effects, this should be relaxed. - if (op->hasNoSideEffect() && op->isKnownNonTerminator()) return true; + // TODO(riverriddle) Properly handle region side effects. + if (MemoryEffectOpInterface::hasNoEffect(op) && op->isKnownNonTerminator() && + op->getNumRegions() == 0) { + return true; + } if (auto if_op = llvm::dyn_cast(op)) { return if_op.is_stateless(); } diff --git a/tensorflow/compiler/mlir/tensorflow/ir/control_flow_ops.h b/tensorflow/compiler/mlir/tensorflow/ir/control_flow_ops.h index 15e7ddab0a2..0156d7e7e9d 100644 --- a/tensorflow/compiler/mlir/tensorflow/ir/control_flow_ops.h +++ b/tensorflow/compiler/mlir/tensorflow/ir/control_flow_ops.h @@ -85,7 +85,7 @@ class TFControlType : public Type::TypeBase { // Note: Additional result corresponds to the control output. class EnterOp : public Op::Impl, - OpTrait::NResults<2>::Impl, OpTrait::HasNoSideEffect> { + OpTrait::NResults<2>::Impl, MemoryEffectOpInterface::Trait> { public: using Op::Op; @@ -95,6 +95,9 @@ class EnterOp void setData(Value value) { setOperand(0, value); } LogicalResult verify(); + + // EnterOp has no side-effects. + void getEffects(SmallVectorImpl &) {} }; // The "_tf.Merge" operation takes a list of input operands and returns a value @@ -198,7 +201,7 @@ class NextIterationSinkOp // Note: Additional result corresponds to the control output. class LoopCondOp : public Op::Impl, - OpTrait::NResults<2>::Impl, OpTrait::HasNoSideEffect> { + OpTrait::NResults<2>::Impl, MemoryEffectOpInterface::Trait> { public: using Op::Op; static StringRef getOperationName() { return "_tf.LoopCond"; } @@ -207,6 +210,9 @@ class LoopCondOp void setData(Value value) { setOperand(0, value); } LogicalResult verify(); + + // LoopCondOp has no side-effects. + void getEffects(SmallVectorImpl &) {} }; // The "_tf.Switch" operation takes a data operand and a boolean predicate @@ -261,8 +267,9 @@ class SwitchOp : public Op::Impl, // (tensor<*xi32>, !_tf.control) // // Note: Additional result corresponds to the control output. -class ExitOp : public Op::Impl, - OpTrait::NResults<2>::Impl, OpTrait::HasNoSideEffect> { +class ExitOp + : public Op::Impl, + OpTrait::NResults<2>::Impl, MemoryEffectOpInterface::Trait> { public: using Op::Op; static StringRef getOperationName() { return "_tf.Exit"; } @@ -271,6 +278,9 @@ class ExitOp : public Op::Impl, void setData(Value value) { setOperand(0, value); } LogicalResult verify(); + + // ExitOp has no side-effects. + void getEffects(SmallVectorImpl &) {} }; } // namespace TFControlFlow diff --git a/tensorflow/compiler/mlir/tensorflow/ir/tf_op_base.td b/tensorflow/compiler/mlir/tensorflow/ir/tf_op_base.td index d49e2b5514b..773025c58df 100644 --- a/tensorflow/compiler/mlir/tensorflow/ir/tf_op_base.td +++ b/tensorflow/compiler/mlir/tensorflow/ir/tf_op_base.td @@ -23,6 +23,7 @@ limitations under the License. #define TF_OP_BASE include "mlir/IR/OpBase.td" +include "mlir/Interfaces/SideEffects.td" include "tensorflow/compiler/mlir/tensorflow/ir/tf_op_interfaces.td" //===----------------------------------------------------------------------===// diff --git a/tensorflow/compiler/mlir/tensorflow/ir/tf_ops.td b/tensorflow/compiler/mlir/tensorflow/ir/tf_ops.td index e6f562d896e..c1c6a643ef1 100644 --- a/tensorflow/compiler/mlir/tensorflow/ir/tf_ops.td +++ b/tensorflow/compiler/mlir/tensorflow/ir/tf_ops.td @@ -64,7 +64,7 @@ class TF_TensorListInitOp : TF_Op { // In MLIR, the TensorFlow tensor value is represented as an ElementsAttr, with // its type encoding the tensor's shape and data type. -def TF_ConstOp : TF_Op<"Const", [NoSideEffect]> { +def TF_ConstOp : TF_Op<"Const", [ConstantLike, NoSideEffect]> { let summary = "Constant tensor op"; let arguments = (ins diff --git a/tensorflow/compiler/mlir/tensorflow/transforms/constant_fold.cc b/tensorflow/compiler/mlir/tensorflow/transforms/constant_fold.cc index 7b46c6aec04..c1a87c289bf 100644 --- a/tensorflow/compiler/mlir/tensorflow/transforms/constant_fold.cc +++ b/tensorflow/compiler/mlir/tensorflow/transforms/constant_fold.cc @@ -17,6 +17,7 @@ limitations under the License. #include +#include "mlir/Interfaces/SideEffects.h" // TF:llvm-project #include "tensorflow/c/eager/c_api.h" #include "tensorflow/c/tf_status.h" #include "tensorflow/compiler/mlir/tensorflow/ir/tf_types.h" @@ -31,7 +32,8 @@ LogicalResult ConstantFoldFallbackHook( SmallVectorImpl& results) { // NOLINT // Instructions with side effects should not be constant folded to preserve // the original semantics. - if (!inst->hasNoSideEffect()) return failure(); + if (inst->getNumRegions() != 0 || !MemoryEffectOpInterface::hasNoEffect(inst)) + return failure(); // If any of the result types are variants, don't try to constant fold them. // This creates opaque variant constants which lose information and would diff --git a/tensorflow/compiler/mlir/xla/BUILD b/tensorflow/compiler/mlir/xla/BUILD index 91f42df48f7..f0a8a7aa6ad 100644 --- a/tensorflow/compiler/mlir/xla/BUILD +++ b/tensorflow/compiler/mlir/xla/BUILD @@ -36,6 +36,7 @@ filegroup( "ir/lhlo_ops.td", "@llvm-project//mlir:OpBaseTdFiles", "@llvm-project//mlir:include/mlir/Interfaces/InferTypeOpInterface.td", + "@llvm-project//mlir:include/mlir/Interfaces/SideEffects.td", ], ) @@ -699,6 +700,7 @@ genrule( name = "operator_writer_inc", srcs = [ "@llvm-project//mlir:include/mlir/Interfaces/InferTypeOpInterface.td", + "@llvm-project//mlir:include/mlir/Interfaces/SideEffects.td", "@llvm-project//mlir:include/mlir/IR/OpBase.td", ":ir/hlo_ops.td", ":ir/hlo_ops_base.td", diff --git a/tensorflow/compiler/mlir/xla/ir/hlo_ops.td b/tensorflow/compiler/mlir/xla/ir/hlo_ops.td index d61f1ad175d..950d33d5ab3 100644 --- a/tensorflow/compiler/mlir/xla/ir/hlo_ops.td +++ b/tensorflow/compiler/mlir/xla/ir/hlo_ops.td @@ -20,6 +20,7 @@ limitations under the License. include "mlir/IR/OpBase.td" include "mlir/Interfaces/InferTypeOpInterface.td" +include "mlir/Interfaces/SideEffects.td" include "tensorflow/compiler/mlir/xla/ir/hlo_ops_base.td" include "tensorflow/compiler/mlir/xla/ir/hlo_utils.td" @@ -96,7 +97,8 @@ def HLO_PredIntOrFpTensor : TensorOf<[HLO_Pred, HLO_Int, AnyFloat]>; // XLA nullary op definitions. //===----------------------------------------------------------------------===// -def HLO_ConstOp : HLO_Op<"constant", [NoSideEffect]>, BASE_HLO_ConstOp { +def HLO_ConstOp : HLO_Op<"constant", [ConstantLike, NoSideEffect]>, + BASE_HLO_ConstOp { let arguments = (ins ElementsAttr:$value ); diff --git a/tensorflow/compiler/mlir/xla/ir/lhlo_ops.td b/tensorflow/compiler/mlir/xla/ir/lhlo_ops.td index ec9c846a1bd..a37c530532d 100644 --- a/tensorflow/compiler/mlir/xla/ir/lhlo_ops.td +++ b/tensorflow/compiler/mlir/xla/ir/lhlo_ops.td @@ -19,6 +19,7 @@ limitations under the License. #define LHLO_OPS include "mlir/IR/OpBase.td" +include "mlir/Interfaces/SideEffects.td" include "tensorflow/compiler/mlir/xla/ir/hlo_ops_base.td" def LHLO_Dialect : Dialect { diff --git a/tensorflow/compiler/xla/service/cpu/vector_support_library.h b/tensorflow/compiler/xla/service/cpu/vector_support_library.h index 2f8be8c111b..cbbc4d7bf34 100644 --- a/tensorflow/compiler/xla/service/cpu/vector_support_library.h +++ b/tensorflow/compiler/xla/service/cpu/vector_support_library.h @@ -273,7 +273,8 @@ class VectorSupportLibrary { llvm::Value* GetConstantFloat(llvm::Type* type, const llvm::APFloat& f) { llvm::Constant* scalar_value = llvm::ConstantFP::get(type->getContext(), f); if (llvm::isa(type)) { - return llvm::ConstantVector::getSplat(vector_size(), scalar_value); + return llvm::ConstantVector::getSplat( + llvm::ElementCount(vector_size(), /*Scalable=*/false), scalar_value); } return scalar_value; } diff --git a/tensorflow/workspace.bzl b/tensorflow/workspace.bzl index 9dd700821d5..d52964c20c4 100755 --- a/tensorflow/workspace.bzl +++ b/tensorflow/workspace.bzl @@ -597,8 +597,8 @@ def tf_repositories(path_prefix = "", tf_repo_name = ""): ) # Check out LLVM and MLIR from llvm-project. - LLVM_COMMIT = "4016c6b07f2ade01c65750d1297f72b43f9eb244" - LLVM_SHA256 = "f67afc574c0f1bc3f43e256321c88f4feefb272d288d084ea6e99c509cb92141" + LLVM_COMMIT = "2c6c169dbd6041b4575b2234c532aad50a472e81" + LLVM_SHA256 = "2193d7972bab0e19134b32e962cfbf6581c3bb57e36c8c9c65c46f9409820796" LLVM_URLS = [ "https://storage.googleapis.com/mirror.tensorflow.org/github.com/llvm/llvm-project/archive/{commit}.tar.gz".format(commit = LLVM_COMMIT), "https://github.com/llvm/llvm-project/archive/{commit}.tar.gz".format(commit = LLVM_COMMIT), diff --git a/third_party/mlir/BUILD b/third_party/mlir/BUILD index a700967ada5..2d01bf2553a 100644 --- a/third_party/mlir/BUILD +++ b/third_party/mlir/BUILD @@ -144,6 +144,7 @@ filegroup( srcs = [ "include/mlir/Dialect/AffineOps/AffineOps.td", "include/mlir/Dialect/AffineOps/AffineOpsBase.td", + "include/mlir/Interfaces/SideEffects.td", "include/mlir/Transforms/LoopLikeInterface.td", ":OpBaseTdFiles", ], @@ -173,6 +174,7 @@ filegroup( name = "LoopOpsTdFiles", srcs = [ "include/mlir/Dialect/LoopOps/LoopOps.td", + "include/mlir/Interfaces/SideEffects.td", "include/mlir/Transforms/LoopLikeInterface.td", ":OpBaseTdFiles", ], @@ -540,6 +542,7 @@ filegroup( srcs = [ "include/mlir/Dialect/GPU/GPUOps.td", "include/mlir/Dialect/LLVMIR/LLVMOpBase.td", + "include/mlir/Interfaces/SideEffects.td", ":OpBaseTdFiles", ], ) @@ -617,6 +620,7 @@ filegroup( "include/mlir/Dialect/LLVMIR/LLVMOpBase.td", "include/mlir/Dialect/LLVMIR/LLVMOps.td", "include/mlir/Interfaces/ControlFlowInterfaces.td", + "include/mlir/Interfaces/SideEffects.td", ":OpBaseTdFiles", ], ) @@ -861,6 +865,7 @@ filegroup( srcs = [ "include/mlir/Dialect/LLVMIR/LLVMOpBase.td", "include/mlir/Dialect/LLVMIR/NVVMOps.td", + "include/mlir/Interfaces/SideEffects.td", ":OpBaseTdFiles", ], ) @@ -928,6 +933,7 @@ filegroup( srcs = [ "include/mlir/Dialect/LLVMIR/LLVMOpBase.td", "include/mlir/Dialect/LLVMIR/ROCDLOps.td", + "include/mlir/Interfaces/SideEffects.td", ":OpBaseTdFiles", ], ) @@ -974,6 +980,7 @@ filegroup( srcs = [ "include/mlir/Interfaces/CallInterfaces.td", "include/mlir/Interfaces/ControlFlowInterfaces.td", + "include/mlir/Interfaces/SideEffects.td", ":OpBaseTdFiles", ] + glob(["include/mlir/Dialect/SPIRV/*.td"]), ) @@ -1181,6 +1188,7 @@ cc_library( "lib/Dialect/SPIRV/SPIRVLowering.cpp", "lib/Dialect/SPIRV/Transforms/DecorateSPIRVCompositeTypeLayoutPass.cpp", "lib/Dialect/SPIRV/Transforms/LowerABIAttributesPass.cpp", + "lib/Dialect/SPIRV/Transforms/UpdateVCEPass.cpp", ], hdrs = [ "include/mlir/Dialect/SPIRV/Passes.h", @@ -1290,12 +1298,50 @@ cc_library( ":IR", ":LoopLikeOpInterfaceIncGen", ":LoopOps", + ":SideEffects", ":StandardOps", ":Support", "@llvm-project//llvm:support", ], ) +gentbl( + name = "DerivedAttributeOpInterfaceIncGen", + strip_include_prefix = "include", + tbl_outs = [ + ( + "-gen-op-interface-decls", + "include/mlir/Interfaces/DerivedAttributeOpInterface.h.inc", + ), + ( + "-gen-op-interface-defs", + "include/mlir/Interfaces/DerivedAttributeOpInterface.cpp.inc", + ), + ], + tblgen = ":mlir-tblgen", + td_file = "include/mlir/Interfaces/DerivedAttributeOpInterface.td", + td_srcs = [ + ":OpBaseTdFiles", + ], +) + +cc_library( + name = "DerivedAttributeOpInterface", + srcs = [ + "lib/Interfaces/DerivedAttributeOpInterface.cpp", + ], + hdrs = [ + "include/mlir/Interfaces/DerivedAttributeOpInterface.h", + ], + includes = ["include"], + deps = [ + ":DerivedAttributeOpInterfaceIncGen", + ":IR", + ":Support", + "@llvm-project//llvm:support", + ], +) + gentbl( name = "LoopLikeOpInterfaceIncGen", strip_include_prefix = "include", @@ -2154,6 +2200,7 @@ filegroup( srcs = [ "include/mlir/Dialect/QuantOps/QuantOps.td", "include/mlir/Dialect/QuantOps/QuantPredicates.td", + "include/mlir/Interfaces/SideEffects.td", ":OpBaseTdFiles", ], ) @@ -2220,6 +2267,7 @@ filegroup( srcs = [ "include/mlir/Dialect/FxpMathOps/FxpMathOps.td", "include/mlir/Dialect/QuantOps/QuantPredicates.td", + "include/mlir/Interfaces/SideEffects.td", ":OpBaseTdFiles", ], ) @@ -2686,11 +2734,11 @@ exports_files( "include/mlir/Interfaces/CallInterfaces.td", "include/mlir/Interfaces/ControlFlowInterfaces.h", "include/mlir/Interfaces/ControlFlowInterfaces.td", + "include/mlir/Interfaces/SideEffects.td", "include/mlir/Dialect/LLVMIR/LLVMOpBase.td", "include/mlir/Dialect/StandardOps/IR/Ops.td", "include/mlir/IR/OpAsmInterface.td", "include/mlir/IR/OpBase.td", - "include/mlir/Interfaces/SideEffects.td", "include/mlir/Transforms/InliningUtils.h", ], visibility = ["@llvm-project//mlir:friends"], diff --git a/third_party/mlir/test.BUILD b/third_party/mlir/test.BUILD index 7bd02be8282..95ee95d55ba 100644 --- a/third_party/mlir/test.BUILD +++ b/third_party/mlir/test.BUILD @@ -76,10 +76,10 @@ gentbl( td_srcs = [ "@llvm-project//mlir:OpBaseTdFiles", "@llvm-project//mlir:include/mlir/IR/OpAsmInterface.td", - "@llvm-project//mlir:include/mlir/Interfaces/SideEffects.td", "@llvm-project//mlir:include/mlir/Interfaces/CallInterfaces.td", "@llvm-project//mlir:include/mlir/Interfaces/ControlFlowInterfaces.td", "@llvm-project//mlir:include/mlir/Interfaces/InferTypeOpInterface.td", + "@llvm-project//mlir:include/mlir/Interfaces/SideEffects.td", ], test = True, )