[PATCH] D75662: [MLIR] add negi in standard dialect

OuYang Jin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 4 19:35:42 PST 2020


qqsun8819 created this revision.
Herald added subscribers: llvm-commits, Joonsoo, liufengdb, lucyrfox, mgester, arpith-jacob, nicolasvasilache, antiagainst, shauheen, burmako, jpienaar, rriddle, mehdi_amini.
Herald added a project: LLVM.

Now we only have negf to accept float arguments. In llvm ir neg can also support integer arguments,
so add a negi in standard dialect so that neg can support integer arguments
This patch is inspired by lowering TF dialect to HLO dialect when a hlo.neg is created whose argument is integer, like:

  %1 = "xla_hlo.neg"(%13) : (tensor<64>) -> tensor<i64>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D75662

Files:
  mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
  mlir/include/mlir/Dialect/StandardOps/IR/Ops.td
  mlir/lib/Conversion/StandardToLLVM/ConvertStandardToLLVM.cpp


Index: mlir/lib/Conversion/StandardToLLVM/ConvertStandardToLLVM.cpp
===================================================================
--- mlir/lib/Conversion/StandardToLLVM/ConvertStandardToLLVM.cpp
+++ mlir/lib/Conversion/StandardToLLVM/ConvertStandardToLLVM.cpp
@@ -1259,6 +1259,9 @@
 struct NegFOpLowering : public UnaryOpLLVMOpLowering<NegFOp, LLVM::FNegOp> {
   using Super::Super;
 };
+struct NegIOpLowering : public UnaryOpLLVMOpLowering<NegIOp, LLVM::NegOp> {
+  using Super::Super;
+};
 struct AddIOpLowering : public BinaryOpLLVMOpLowering<AddIOp, LLVM::AddOp> {
   using Super::Super;
 };
@@ -2802,6 +2805,7 @@
       MulFOpLowering,
       MulIOpLowering,
       NegFOpLowering,
+      NegIOpLowering,
       OrOpLowering,
       PrefetchOpLowering,
       RemFOpLowering,
Index: mlir/include/mlir/Dialect/StandardOps/IR/Ops.td
===================================================================
--- mlir/include/mlir/Dialect/StandardOps/IR/Ops.td
+++ mlir/include/mlir/Dialect/StandardOps/IR/Ops.td
@@ -76,6 +76,10 @@
   }];
 }
 
+class IntegerUnaryOp<string mnemonic, list<OpTrait> traits = []> :
+    UnaryOpSameOperandAndResultType<mnemonic, traits>,
+    Arguments<(ins SignlessIntegerLike:$operand)>;
+
 class FloatUnaryOp<string mnemonic, list<OpTrait> traits = []> :
     UnaryOpSameOperandAndResultType<mnemonic, traits>,
     Arguments<(ins FloatLike:$operand)>;
@@ -1042,6 +1046,16 @@
   }];
 }
 
+def NegIOp : IntegerUnaryOp<"negi"> {
+  let summary = "integer negation";
+  let description = [{
+    The `negi` operation computes the negation of a given value. It takes one
+    operand and returns one result of the same type. This type may be a integer 
+    scalar type, a vector whose element type is integer, or a tensor of integers.
+    It has no standard attributes.
+  }];
+}
+  
 def OrOp : IntArithmeticOp<"or", [Commutative]> {
   let summary = "integer binary or";
   let hasFolder = 1;
Index: mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
===================================================================
--- mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
+++ mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
@@ -138,6 +138,7 @@
 def LLVM_ShlOp : LLVM_ArithmeticOp<"shl", "CreateShl">;
 def LLVM_LShrOp : LLVM_ArithmeticOp<"lshr", "CreateLShr">;
 def LLVM_AShrOp : LLVM_ArithmeticOp<"ashr", "CreateAShr">;
+def LLVM_NegOp : LLVM_UnaryArithmeticOp<"neg", "CreateNeg">;
 
 // Predicate for integer comparisons.
 def ICmpPredicateEQ  : I64EnumAttrCase<"eq", 0>;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D75662.248373.patch
Type: text/x-patch
Size: 2492 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200305/72153ac6/attachment.bin>


More information about the llvm-commits mailing list