llvm.org GIT mirror llvm / e32f0e2
IR: Allow vectors of halfs to be ConstantDataVectors Currently, vectors of halfs end up as ConstantVectors, but there isn't a good reason they can't be ConstantDataVectors. This should save some memory. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@254991 91177308-0d34-0410-b5e6-96231b3b80d8 Justin Bogner 4 years ago
3 changed file(s) with 58 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
898898 else if (CI->getType()->isIntegerTy(64))
899899 return getIntSequenceIfElementsMatch(V);
900900 } else if (ConstantFP *CFP = dyn_cast(C)) {
901 if (CFP->getType()->isFloatTy())
901 if (CFP->getType()->isHalfTy())
902 return getFPSequenceIfElementsMatch(V);
903 else if (CFP->getType()->isFloatTy())
902904 return getFPSequenceIfElementsMatch(V);
903905 else if (CFP->getType()->isDoubleTy())
904906 return getFPSequenceIfElementsMatch(V);
23642366 /// ConstantDataArray only works with normal float and int types that are
23652367 /// stored densely in memory, not with things like i42 or x86_f80.
23662368 bool ConstantDataSequential::isElementTypeCompatible(Type *Ty) {
2367 if (Ty->isFloatTy() || Ty->isDoubleTy()) return true;
2369 if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) return true;
23682370 if (auto *IT = dyn_cast(Ty)) {
23692371 switch (IT->getBitWidth()) {
23702372 case 8:
26362638 }
26372639
26382640 if (ConstantFP *CFP = dyn_cast(V)) {
2641 if (CFP->getType()->isHalfTy()) {
2642 SmallVector Elts(
2643 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
2644 return getFP(V->getContext(), Elts);
2645 }
26392646 if (CFP->getType()->isFloatTy()) {
26402647 SmallVector Elts(
26412648 NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue());
26812688 switch (getElementType()->getTypeID()) {
26822689 default:
26832690 llvm_unreachable("Accessor can only be used when element is float/double!");
2691 case Type::HalfTyID: {
2692 auto EltVal = *reinterpret_cast(EltPtr);
2693 return APFloat(APFloat::IEEEhalf, APInt(16, EltVal));
2694 }
26842695 case Type::FloatTyID: {
26852696 auto EltVal = *reinterpret_cast(EltPtr);
26862697 return APFloat(APFloat::IEEEsingle, APInt(32, EltVal));
27152726 /// Note that this has to compute a new constant to return, so it isn't as
27162727 /// efficient as getElementAsInteger/Float/Double.
27172728 Constant *ConstantDataSequential::getElementAsConstant(unsigned Elt) const {
2718 if (getElementType()->isFloatTy() || getElementType()->isDoubleTy())
2729 if (getElementType()->isHalfTy() || getElementType()->isFloatTy() ||
2730 getElementType()->isDoubleTy())
27192731 return ConstantFP::get(getContext(), getElementAsAPFloat(Elt));
27202732
27212733 return ConstantInt::get(getElementType(), getElementAsInteger(Elt));
88 @var64 = global double -0.0, align 8
99 @var32 = global float -0.0, align 4
1010 @var16 = global half -0.0, align 2
11 @var4f32 = global <4 x float>
12 @var4f16 = global <4 x half>
1113
1214 ; CHECK: var128:
1315 ; CHECK-NEXT: .quad 0 # fp128 -0
3840 ; CHECK-NEXT: .short 32768 # half -0
3941 ; CHECK-NEXT: .size
4042
43 ; CHECK: var4f32:
44 ; CHECK-NEXT: .long 2147483648 # float -0
45 ; CHECK-NEXT: .long 0 # float 0
46 ; CHECK-NEXT: .long 1065353216 # float 1
47 ; CHECK-NEXT: .long 1073741824 # float 2
48 ; CHECK-NEXT: .size
49
50 ; CHECK: var4f16:
51 ; CHECK-NEXT: .short 32768 # half -0
52 ; CHECK-NEXT: .short 0 # half 0
53 ; CHECK-NEXT: .short 15360 # half 1
54 ; CHECK-NEXT: .short 16384 # half 2
55 ; CHECK-NEXT: .size
381381 ASSERT_EQ(unwrap(AliasRef)->getAliasee(), Aliasee);
382382 }
383383
384 static std::string getNameOfType(Type *T) {
385 std::string S;
386 raw_string_ostream RSOS(S);
387 T->print(RSOS);
388 return S;
389 }
390
391 TEST(ConstantsTest, BuildConstantDataVectors) {
392 LLVMContext Context;
393 std::unique_ptr M(new Module("MyModule", Context));
394
395 for (Type *T : {Type::getInt8Ty(Context), Type::getInt16Ty(Context),
396 Type::getInt32Ty(Context), Type::getInt64Ty(Context)}) {
397 Constant *Vals[] = {ConstantInt::get(T, 0), ConstantInt::get(T, 1)};
398 Constant *CDV = ConstantVector::get(Vals);
399 ASSERT_TRUE(dyn_cast(CDV) != nullptr)
400 << " T = " << getNameOfType(T);
401 }
402
403 for (Type *T : {Type::getHalfTy(Context), Type::getFloatTy(Context),
404 Type::getDoubleTy(Context)}) {
405 Constant *Vals[] = {ConstantFP::get(T, 0), ConstantFP::get(T, 1)};
406 Constant *CDV = ConstantVector::get(Vals);
407 ASSERT_TRUE(dyn_cast(CDV) != nullptr)
408 << " T = " << getNameOfType(T);
409 }
410 }
411
384412 } // end anonymous namespace
385413 } // end namespace llvm