[PATCH] D54387: [ExecutionEngine] Add support for aggregate constants in the interpreter

Eugene Sharygin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Nov 10 16:11:54 PST 2018


eush created this revision.
eush added reviewers: andrew.w.kaylor, lhames, nadav.

This adds support for constants of aggregate types, i.e. structs and arrays.

Previously the conversion from a constant aggregate Value to a GenericValue
was missing in `ExecutionEngine::getConstantValue`, and this patch implements
just that by writing all elements in sequence into the corresponding
GenericValue field.


Repository:
  rL LLVM

https://reviews.llvm.org/D54387

Files:
  lib/ExecutionEngine/ExecutionEngine.cpp
  test/ExecutionEngine/Interpreter/constant-aggzero.ll
  test/ExecutionEngine/Interpreter/constant-array.ll
  test/ExecutionEngine/Interpreter/constant-struct.ll


Index: test/ExecutionEngine/Interpreter/constant-struct.ll
===================================================================
--- /dev/null
+++ test/ExecutionEngine/Interpreter/constant-struct.ll
@@ -0,0 +1,11 @@
+; RUN: %lli -force-interpreter %s
+
+define { i32, i32 } @f() {
+  ret { i32, i32 } { i32 0, i32 1 }
+}
+
+define i32 @main() {
+  %s = call { i32, i32 } @f()
+  %t = extractvalue { i32, i32 } %s, 0
+  ret i32 %t
+}
Index: test/ExecutionEngine/Interpreter/constant-array.ll
===================================================================
--- /dev/null
+++ test/ExecutionEngine/Interpreter/constant-array.ll
@@ -0,0 +1,11 @@
+; RUN: %lli -force-interpreter %s
+
+define [2 x i32] @f() {
+  ret [2 x i32] [i32 0, i32 1]
+}
+
+define i32 @main() {
+  %a = call [2 x i32] @f()
+  %t = extractvalue [2 x i32] %a, 0
+  ret i32 %t
+}
Index: test/ExecutionEngine/Interpreter/constant-aggzero.ll
===================================================================
--- /dev/null
+++ test/ExecutionEngine/Interpreter/constant-aggzero.ll
@@ -0,0 +1,11 @@
+; RUN: %lli -force-interpreter %s
+
+define { i32, i32 } @f() {
+  ret { i32, i32 } { i32 0, i32 0 }
+}
+
+define i32 @main() {
+  %s = call { i32, i32 } @f()
+  %t = extractvalue { i32, i32 } %s, 0
+  ret i32 %t
+}
Index: lib/ExecutionEngine/ExecutionEngine.cpp
===================================================================
--- lib/ExecutionEngine/ExecutionEngine.cpp
+++ lib/ExecutionEngine/ExecutionEngine.cpp
@@ -37,8 +37,10 @@
 #include "llvm/Support/TargetRegistry.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Target/TargetMachine.h"
+#include <algorithm>
 #include <cmath>
 #include <cstring>
+#include <iterator>
 using namespace llvm;
 
 #define DEBUG_TYPE "jit"
@@ -902,6 +904,29 @@
   case Type::IntegerTyID:
     Result.IntVal = cast<ConstantInt>(C)->getValue();
     break;
+  case Type::StructTyID:
+  case Type::ArrayTyID: {
+    if (!isa<ConstantData>(C)) {
+      std::transform(
+          C->op_begin(), C->op_end(), std::back_inserter(Result.AggregateVal),
+          [this](auto &E) { return getConstantValue(cast<Constant>(E)); });
+      break;
+    }
+
+    auto *CDS = dyn_cast<ConstantDataSequential>(C);
+    auto *CAZ = dyn_cast<ConstantAggregateZero>(C);
+    assert((CDS || CAZ) && "Unsupported ConstantData subclass");
+
+    unsigned NumElems = CDS ? CDS->getNumElements() : CAZ->getNumElements();
+
+    for (unsigned ENum = 0; ENum < NumElems; ++ENum) {
+      Constant *E =
+          CDS ? CDS->getElementAsConstant(ENum) : CAZ->getElementValue(ENum);
+
+      Result.AggregateVal.push_back(getConstantValue(E));
+    }
+    break;
+  }
   case Type::PointerTyID:
     while (auto *A = dyn_cast<GlobalAlias>(C)) {
       C = A->getAliasee();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D54387.173539.patch
Type: text/x-patch
Size: 2760 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181111/72ed8b45/attachment.bin>


More information about the llvm-commits mailing list