[PATCH] D62881: [ExecutionEngine] Add UnaryOperator visitor to the interpreter

Cameron McInally via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 4 14:47:55 PDT 2019


cameron.mcinally created this revision.
cameron.mcinally added reviewers: spatel, craig.topper, andrew.w.kaylor, arsenm, kpn, nadav, bkramer, MaskRay.
Herald added subscribers: llvm-commits, hiraditya, wdng.
Herald added a project: LLVM.

I'm not familiar with the ExecutionEngine at all. A thorough review would be appreciated.

Also, it isn't clear to me how the tests verify functionality. Am I missing something?


Repository:
  rL LLVM

https://reviews.llvm.org/D62881

Files:
  llvm/lib/ExecutionEngine/Interpreter/Execution.cpp
  llvm/lib/ExecutionEngine/Interpreter/Interpreter.h
  llvm/test/ExecutionEngine/test-interp-vec-arithm_float.ll


Index: llvm/test/ExecutionEngine/test-interp-vec-arithm_float.ll
===================================================================
--- llvm/test/ExecutionEngine/test-interp-vec-arithm_float.ll
+++ llvm/test/ExecutionEngine/test-interp-vec-arithm_float.ll
@@ -8,6 +8,7 @@
     %C_float = fmul <4 x float> %B_float, %B_float
     %D_float = fdiv <4 x float> %C_float, %B_float
     %E_float = frem <4 x float> %D_float, %A_float
+    %F_float = fneg <4 x float> %E_float
 
 
     %A_double = fadd <3 x double> <double 0.0, double 111.0, double 222.0>, <double 444.0, double 555.0, double 665.0>
@@ -15,6 +16,7 @@
     %C_double = fmul <3 x double> %B_double, %B_double
     %D_double = fdiv <3 x double> %C_double, %B_double
     %E_double = frem <3 x double> %D_double, %A_double
+    %F_double = fneg <3 x double> %E_double
 
     ret i32 0
 }
Index: llvm/lib/ExecutionEngine/Interpreter/Interpreter.h
===================================================================
--- llvm/lib/ExecutionEngine/Interpreter/Interpreter.h
+++ llvm/lib/ExecutionEngine/Interpreter/Interpreter.h
@@ -124,6 +124,7 @@
   void visitSwitchInst(SwitchInst &I);
   void visitIndirectBrInst(IndirectBrInst &I);
 
+  void visitUnaryOperator(UnaryOperator &I);
   void visitBinaryOperator(BinaryOperator &I);
   void visitICmpInst(ICmpInst &I);
   void visitFCmpInst(FCmpInst &I);
Index: llvm/lib/ExecutionEngine/Interpreter/Execution.cpp
===================================================================
--- llvm/lib/ExecutionEngine/Interpreter/Execution.cpp
+++ llvm/lib/ExecutionEngine/Interpreter/Execution.cpp
@@ -43,6 +43,62 @@
 }
 
 //===----------------------------------------------------------------------===//
+//                    Unary Instruction Implementations
+//===----------------------------------------------------------------------===//
+
+static void executeFNegInst(GenericValue &Dest, GenericValue Src, Type *Ty) {
+  switch (Ty->getTypeID()) {
+  case Type::FloatTyID:
+    Dest.FloatVal = -Src.FloatVal;
+  case Type::DoubleTyID:
+    Dest.DoubleVal = -Src.DoubleVal;
+  default:
+    dbgs() << "Unhandled type for FNeg instruction: " << *Ty << "\n";
+    llvm_unreachable(nullptr);
+  }
+}
+
+void Interpreter::visitUnaryOperator(UnaryOperator &I) {
+  ExecutionContext &SF = ECStack.back();
+  Type *Ty = I.getOperand(0)->getType();
+  GenericValue Src = getOperandValue(I.getOperand(0), SF);
+  GenericValue R; // Result
+
+  // First process vector operation
+  if (Ty->isVectorTy()) {
+    R.AggregateVal.resize(Src.AggregateVal.size());
+
+    switch(I.getOpcode()) {
+    default:
+      dbgs() << "Don't know how to handle this unary operator!\n-->" << I;
+      llvm_unreachable(nullptr);
+      break;
+    case Instruction::FNeg:
+      if (cast<VectorType>(Ty)->getElementType()->isFloatTy()) {
+        for (unsigned i = 0; i < R.AggregateVal.size(); ++i)
+          R.AggregateVal[i].FloatVal = -Src.AggregateVal[i].FloatVal;
+      } else if (cast<VectorType>(Ty)->getElementType()->isDoubleTy()) {
+        for (unsigned i = 0; i < R.AggregateVal.size(); ++i)
+          R.AggregateVal[i].DoubleVal = -Src.AggregateVal[i].DoubleVal;
+      } else {
+        dbgs() << "Unhandled type for OP instruction: " << *Ty << "\n";
+        llvm_unreachable(0);
+      }
+      break;
+    }
+  } else {
+    switch (I.getOpcode()) {
+    default:
+      dbgs() << "Don't know how to handle this unary operator!\n-->" << I;
+      llvm_unreachable(nullptr);
+      break;
+    case Instruction::FNeg: executeFNegInst(R, Src, Ty); break;
+    }
+  }
+  SetValue(&I, R, SF);
+}
+
+//===----------------------------------------------------------------------===//
 //                    Binary Instruction Implementations
 //===----------------------------------------------------------------------===//
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D62881.203028.patch
Type: text/x-patch
Size: 3815 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190604/85dc0288/attachment.bin>


More information about the llvm-commits mailing list