[PATCH] D47706: Add a factory method to ConstantDataArray that allows to pass in the data as StringRef

Adrian Kuegel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 4 04:42:49 PDT 2018


akuegel created this revision.
akuegel added a reviewer: bkramer.

This simplifies the case if we already have access to the raw data that we need to store in a ConstantDataArray.
The new factor method can also be reused for implementing the factory method that gets the data as ArrayRef.


Repository:
  rL LLVM

https://reviews.llvm.org/D47706

Files:
  Constants.h


Index: Constants.h
===================================================================
--- Constants.h
+++ Constants.h
@@ -698,9 +698,8 @@
   template <typename ElementTy>
   static Constant *get(LLVMContext &Context, ArrayRef<ElementTy> Elts) {
     const char *Data = reinterpret_cast<const char *>(Elts.data());
-    Type *Ty =
-        ArrayType::get(Type::getScalarTy<ElementTy>(Context), Elts.size());
-    return getImpl(StringRef(Data, Elts.size() * sizeof(ElementTy)), Ty);
+    return get(StringRef(Data, Elts.size() * sizeof(ElementTy)), Elts.size(),
+               Type::getScalarTy<ElementTy>(Context));
   }
 
   /// get() constructor - ArrayTy needs to be compatible with
@@ -710,6 +709,14 @@
     return ConstantDataArray::get(Context, makeArrayRef(Elts));
   }
 
+  /// get() constructor - Return a constant with array type with an element
+  /// count and element type matching the NumElements and ElementTy parameters
+  /// passed in. Note that this can return a ConstantAggregateZero object.
+  static Constant *get(StringRef Data, uint64_t NumElements, Type *ElementTy) {
+    Type *Ty = ArrayType::get(ElementTy, NumElements);
+    return getImpl(Data, Ty);
+  }
+
   /// getFP() constructors - Return a constant with array type with an element
   /// count and element type of float with precision matching the number of
   /// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D47706.149707.patch
Type: text/x-patch
Size: 1426 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180604/49ec8021/attachment.bin>


More information about the llvm-commits mailing list