| 1 |
//===- llvm/unittests/Target/DirectX/CBufferDataLayoutTests.cpp -----------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "CBufferDataLayout.h" |
| 10 |
#include "llvm/IR/DataLayout.h" |
| 11 |
#include "llvm/IR/Instructions.h" |
| 12 |
#include "llvm/IR/LLVMContext.h" |
| 13 |
#include "llvm/IR/Type.h" |
| 14 |
|
| 15 |
#include "gmock/gmock.h" |
| 16 |
#include "gtest/gtest.h" |
| 17 |
|
| 18 |
using ::testing::Contains; |
| 19 |
using ::testing::Pair; |
| 20 |
|
| 21 |
using namespace llvm; |
| 22 |
using namespace llvm::dxil; |
| 23 |
|
| 24 |
void checkLegacyLayout(CBufferDataLayout &CBDL, Type *T16, Type *T32, |
| 25 |
Type *T64) { |
| 26 |
// Basic types. |
| 27 |
EXPECT_EQ(2ULL, CBDL.getTypeAllocSizeInBytes(T16).getFixedValue()); |
| 28 |
EXPECT_EQ(4ULL, CBDL.getTypeAllocSizeInBytes(T32).getFixedValue()); |
| 29 |
EXPECT_EQ(8ULL, CBDL.getTypeAllocSizeInBytes(T64).getFixedValue()); |
| 30 |
// Vector types. |
| 31 |
Type *T16V2 = FixedVectorType::get(T16, 2); |
| 32 |
Type *T32V2 = FixedVectorType::get(T32, 2); |
| 33 |
Type *T64V2 = FixedVectorType::get(T64, 2); |
| 34 |
Type *T16V3 = FixedVectorType::get(T16, 3); |
| 35 |
Type *T32V3 = FixedVectorType::get(T32, 3); |
| 36 |
Type *T64V3 = FixedVectorType::get(T64, 3); |
| 37 |
Type *T16V4 = FixedVectorType::get(T16, 4); |
| 38 |
Type *T32V4 = FixedVectorType::get(T32, 4); |
| 39 |
Type *T64V4 = FixedVectorType::get(T64, 4); |
| 40 |
EXPECT_EQ(4ULL, CBDL.getTypeAllocSizeInBytes(T16V2).getFixedValue()); |
| 41 |
EXPECT_EQ(8ULL, CBDL.getTypeAllocSizeInBytes(T32V2).getFixedValue()); |
| 42 |
EXPECT_EQ(16ULL, CBDL.getTypeAllocSizeInBytes(T64V2).getFixedValue()); |
| 43 |
EXPECT_EQ(6ULL, CBDL.getTypeAllocSizeInBytes(T16V3).getFixedValue()); |
| 44 |
EXPECT_EQ(12ULL, CBDL.getTypeAllocSizeInBytes(T32V3).getFixedValue()); |
| 45 |
EXPECT_EQ(24ULL, CBDL.getTypeAllocSizeInBytes(T64V3).getFixedValue()); |
| 46 |
EXPECT_EQ(8ULL, CBDL.getTypeAllocSizeInBytes(T16V4).getFixedValue()); |
| 47 |
EXPECT_EQ(16ULL, CBDL.getTypeAllocSizeInBytes(T32V4).getFixedValue()); |
| 48 |
EXPECT_EQ(32ULL, CBDL.getTypeAllocSizeInBytes(T64V4).getFixedValue()); |
| 49 |
|
| 50 |
// Array types. |
| 51 |
|
| 52 |
ArrayType *T16A3 = ArrayType::get(T16, 3); |
| 53 |
ArrayType *T32A3 = ArrayType::get(T32, 3); |
| 54 |
ArrayType *T64A3 = ArrayType::get(T64, 3); |
| 55 |
|
| 56 |
EXPECT_EQ(34ULL, CBDL.getTypeAllocSizeInBytes(T16A3).getFixedValue()); |
| 57 |
EXPECT_EQ(36ULL, CBDL.getTypeAllocSizeInBytes(T32A3).getFixedValue()); |
| 58 |
EXPECT_EQ(40ULL, CBDL.getTypeAllocSizeInBytes(T64A3).getFixedValue()); |
| 59 |
|
| 60 |
ArrayType *T16V3A3 = ArrayType::get(T16V3, 3); |
| 61 |
ArrayType *T32V3A3 = ArrayType::get(T32V3, 3); |
| 62 |
ArrayType *T64V3A3 = ArrayType::get(T64V3, 3); |
| 63 |
|
| 64 |
EXPECT_EQ(38ULL, CBDL.getTypeAllocSizeInBytes(T16V3A3).getFixedValue()); |
| 65 |
EXPECT_EQ(44ULL, CBDL.getTypeAllocSizeInBytes(T32V3A3).getFixedValue()); |
| 66 |
EXPECT_EQ(88ULL, CBDL.getTypeAllocSizeInBytes(T64V3A3).getFixedValue()); |
| 67 |
|
| 68 |
ArrayType *T16V3A3A3 = ArrayType::get(T16V3A3, 3); |
| 69 |
ArrayType *T32V3A3A3 = ArrayType::get(T32V3A3, 3); |
| 70 |
ArrayType *T64V3A3A3 = ArrayType::get(T64V3A3, 3); |
| 71 |
|
| 72 |
EXPECT_EQ((48 * 2 + 38ULL), |
| 73 |
CBDL.getTypeAllocSizeInBytes(T16V3A3A3).getFixedValue()); |
| 74 |
EXPECT_EQ((48 * 2 + 44ULL), |
| 75 |
CBDL.getTypeAllocSizeInBytes(T32V3A3A3).getFixedValue()); |
| 76 |
EXPECT_EQ((96 * 2 + 88ULL), |
| 77 |
CBDL.getTypeAllocSizeInBytes(T64V3A3A3).getFixedValue()); |
| 78 |
|
| 79 |
// Struct types. |
| 80 |
StructType *BasicMix0 = StructType::get(T16, T32, T64); |
| 81 |
StructType *BasicMix1 = StructType::get(T16, T64, T32); |
| 82 |
StructType *BasicMix2 = StructType::get(T32, T64, T16); |
| 83 |
StructType *BasicMix3 = StructType::get(T32, T16, T64); |
| 84 |
StructType *BasicMix4 = StructType::get(T64, T16, T32); |
| 85 |
StructType *BasicMix5 = StructType::get(T64, T32, T16); |
| 86 |
|
| 87 |
EXPECT_EQ(16ULL, CBDL.getTypeAllocSizeInBytes(BasicMix0).getFixedValue()); |
| 88 |
EXPECT_EQ(20ULL, CBDL.getTypeAllocSizeInBytes(BasicMix1).getFixedValue()); |
| 89 |
EXPECT_EQ(18ULL, CBDL.getTypeAllocSizeInBytes(BasicMix2).getFixedValue()); |
| 90 |
EXPECT_EQ(16ULL, CBDL.getTypeAllocSizeInBytes(BasicMix3).getFixedValue()); |
| 91 |
EXPECT_EQ(16ULL, CBDL.getTypeAllocSizeInBytes(BasicMix4).getFixedValue()); |
| 92 |
EXPECT_EQ(14ULL, CBDL.getTypeAllocSizeInBytes(BasicMix5).getFixedValue()); |
| 93 |
|
| 94 |
StructType *VecMix0 = StructType::get(T16V3, T16, T32, T64V2); |
| 95 |
StructType *VecMix1 = StructType::get(T16V3, T32, T64V2, T16); |
| 96 |
StructType *VecMix2 = StructType::get(T16V3, T64, T32V2, T16); |
| 97 |
StructType *VecMix3 = StructType::get(T32V3, T64, T16V2, T32); |
| 98 |
StructType *VecMix4 = StructType::get(T32V3, T16, T16V2, T64); |
| 99 |
StructType *VecMix5 = StructType::get(T32V3, T64V3, T16V2, T64); |
| 100 |
|
| 101 |
EXPECT_EQ(32ULL, CBDL.getTypeAllocSizeInBytes(VecMix0).getFixedValue()); |
| 102 |
EXPECT_EQ(34ULL, CBDL.getTypeAllocSizeInBytes(VecMix1).getFixedValue()); |
| 103 |
EXPECT_EQ(26ULL, CBDL.getTypeAllocSizeInBytes(VecMix2).getFixedValue()); |
| 104 |
EXPECT_EQ(32ULL, CBDL.getTypeAllocSizeInBytes(VecMix3).getFixedValue()); |
| 105 |
EXPECT_EQ(32ULL, CBDL.getTypeAllocSizeInBytes(VecMix4).getFixedValue()); |
| 106 |
EXPECT_EQ(56ULL, CBDL.getTypeAllocSizeInBytes(VecMix5).getFixedValue()); |
| 107 |
|
| 108 |
StructType *ArrayMix0 = StructType::get(T16A3, T16, T32, T64A3); |
| 109 |
StructType *ArrayMix1 = StructType::get(T32A3, T16, T32, T16A3); |
| 110 |
StructType *ArrayMix2 = StructType::get(T16A3, T32, T64, T32A3); |
| 111 |
StructType *ArrayMix3 = StructType::get(T32A3, T32, T64, T16A3); |
| 112 |
StructType *ArrayMix4 = StructType::get(T16A3, T64, T16, T64A3); |
| 113 |
StructType *ArrayMix5 = StructType::get(T32A3, T64, T16, T32A3); |
| 114 |
|
| 115 |
EXPECT_EQ(88ULL, CBDL.getTypeAllocSizeInBytes(ArrayMix0).getFixedValue()); |
| 116 |
EXPECT_EQ(82ULL, CBDL.getTypeAllocSizeInBytes(ArrayMix1).getFixedValue()); |
| 117 |
EXPECT_EQ(84ULL, CBDL.getTypeAllocSizeInBytes(ArrayMix2).getFixedValue()); |
| 118 |
EXPECT_EQ(82ULL, CBDL.getTypeAllocSizeInBytes(ArrayMix3).getFixedValue()); |
| 119 |
EXPECT_EQ(104ULL, CBDL.getTypeAllocSizeInBytes(ArrayMix4).getFixedValue()); |
| 120 |
EXPECT_EQ(100ULL, CBDL.getTypeAllocSizeInBytes(ArrayMix5).getFixedValue()); |
| 121 |
|
| 122 |
StructType *StructMix0 = StructType::get(T16A3, T16, T32, ArrayMix0); |
| 123 |
StructType *StructMix1 = StructType::get(StructMix0, T16, T32, ArrayMix1); |
| 124 |
ArrayType *StructArray0 = ArrayType::get(StructMix1, 3); |
| 125 |
StructType *StructMix2 = StructType::get(StructArray0, T64); |
| 126 |
ArrayType *StructArray1 = ArrayType::get(StructMix2, 3); |
| 127 |
StructType *StructMix3 = StructType::get(StructArray1, T32); |
| 128 |
EXPECT_EQ(136ULL, CBDL.getTypeAllocSizeInBytes(StructMix0).getFixedValue()); |
| 129 |
EXPECT_EQ(226ULL, CBDL.getTypeAllocSizeInBytes(StructMix1).getFixedValue()); |
| 130 |
EXPECT_EQ(706ULL, CBDL.getTypeAllocSizeInBytes(StructArray0).getFixedValue()); |
| 131 |
EXPECT_EQ(720ULL, CBDL.getTypeAllocSizeInBytes(StructMix2).getFixedValue()); |
| 132 |
EXPECT_EQ(2160ULL, |
| 133 |
CBDL.getTypeAllocSizeInBytes(StructArray1).getFixedValue()); |
| 134 |
EXPECT_EQ(2164ULL, CBDL.getTypeAllocSizeInBytes(StructMix3).getFixedValue()); |
| 135 |
} |
| 136 |
|
| 137 |
TEST(CBufferDataLayout, LegacyLayout) { |
| 138 |
LLVMContext Context; |
| 139 |
llvm::DataLayout DL("e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-" |
| 140 |
"f32:32-f64:64-n8:16:32:64"); |
| 141 |
|
| 142 |
CBufferDataLayout CBDL(DL, true); |
| 143 |
|
| 144 |
Type *F16 = Type::getHalfTy(Context); |
| 145 |
Type *F32 = Type::getFloatTy(Context); |
| 146 |
Type *F64 = Type::getDoubleTy(Context); |
| 147 |
|
| 148 |
Type *I16 = Type::getInt16Ty(Context); |
| 149 |
Type *I32 = Type::getInt32Ty(Context); |
| 150 |
Type *I64 = Type::getInt64Ty(Context); |
| 151 |
|
| 152 |
checkLegacyLayout(CBDL, F16, F32, F64); |
| 153 |
checkLegacyLayout(CBDL, I16, I32, I64); |
| 154 |
} |