[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
Tue Jun 19 01:17:30 PDT 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rL335028: Add a factory method to ConstantDataArray that allows to pass in the data as… (authored by akuegel, committed by ).

Changed prior to commit:
  https://reviews.llvm.org/D47706?vs=150472&id=151865#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D47706

Files:
  llvm/trunk/include/llvm/IR/Constants.h


Index: llvm/trunk/include/llvm/IR/Constants.h
===================================================================
--- llvm/trunk/include/llvm/IR/Constants.h
+++ llvm/trunk/include/llvm/IR/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 getRaw(StringRef(Data, Elts.size() * sizeof(ElementTy)), Elts.size(),
+                  Type::getScalarTy<ElementTy>(Context));
   }
 
   /// get() constructor - ArrayTy needs to be compatible with
@@ -710,6 +709,17 @@
     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.
+  /// ElementTy needs to be one of i8/i16/i32/i64/float/double. Data is the
+  /// buffer containing the elements. Be careful to make sure Data uses the
+  /// right endianness, the buffer will be used as-is.
+  static Constant *getRaw(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.151865.patch
Type: text/x-patch
Size: 1726 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180619/6d9b1c1a/attachment.bin>


More information about the llvm-commits mailing list