GCC Code Coverage Report


Directory: ./
File: openvdb_ax/openvdb_ax/codegen/Types.cc
Date: 2022-07-25 17:40:05
Exec Total Coverage
Lines: 52 66 78.8%
Functions: 2 4 50.0%
Branches: 72 98 73.5%

Line Branch Exec Source
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3
4 /// @file codegen/Types.cc
5 ///
6 /// @authors Nick Avramoussis
7 ///
8
9 #include "Types.h"
10
11 namespace openvdb {
12 OPENVDB_USE_VERSION_NAMESPACE
13 namespace OPENVDB_VERSION_NAME {
14
15 namespace ax {
16 namespace codegen {
17
18 /// @brief Returns an llvm IntegerType given a requested size and context
19 /// @param size The number of bits of the integer type
20 /// @param C The LLVMContext to request the Type from.
21 ///
22 llvm::IntegerType*
23 llvmIntType(const uint32_t size, llvm::LLVMContext& C)
24 {
25 switch (size) {
26 case 1 : return llvm::cast<llvm::IntegerType>(LLVMType<bool>::get(C));
27 case 8 : return llvm::cast<llvm::IntegerType>(LLVMType<int8_t>::get(C));
28 case 16 : return llvm::cast<llvm::IntegerType>(LLVMType<int16_t>::get(C));
29 case 32 : return llvm::cast<llvm::IntegerType>(LLVMType<int32_t>::get(C));
30 case 64 : return llvm::cast<llvm::IntegerType>(LLVMType<int64_t>::get(C));
31 default : return llvm::Type::getIntNTy(C, size);
32 }
33 }
34
35
36 /// @brief Returns an llvm floating point Type given a requested size and context
37 /// @param size The size of the float to request, i.e. float - 32, double - 64 etc.
38 /// @param C The LLVMContext to request the Type from.
39 ///
40 llvm::Type*
41 llvmFloatType(const uint32_t size, llvm::LLVMContext& C)
42 {
43 switch (size) {
44 case 32 : return LLVMType<float>::get(C);
45 case 64 : return LLVMType<double>::get(C);
46 default : OPENVDB_THROW(AXCodeGenError,
47 "Invalid float size requested from LLVM Context");
48 }
49 }
50
51 /// @brief Returns an llvm type representing a type defined by a string.
52 /// @note For string types, this function returns the element type, not the
53 /// object type! The llvm type representing a char block of memory
54 /// is LLVMType<char*>::get(C);
55 /// @param type The name of the type to request.
56 /// @param C The LLVMContext to request the Type from.
57 ///
58 llvm::Type*
59 54711 llvmTypeFromToken(const ast::tokens::CoreType& type,
60 llvm::LLVMContext& C)
61 {
62
20/21
✓ Branch 0 taken 7241 times.
✓ Branch 1 taken 564 times.
✓ Branch 2 taken 5000 times.
✓ Branch 3 taken 1640 times.
✓ Branch 4 taken 10425 times.
✓ Branch 5 taken 6495 times.
✓ Branch 6 taken 885 times.
✓ Branch 7 taken 929 times.
✓ Branch 8 taken 920 times.
✓ Branch 9 taken 1451 times.
✓ Branch 10 taken 6383 times.
✓ Branch 11 taken 2442 times.
✓ Branch 12 taken 1249 times.
✓ Branch 13 taken 1282 times.
✓ Branch 14 taken 1327 times.
✓ Branch 15 taken 1322 times.
✓ Branch 16 taken 1465 times.
✓ Branch 17 taken 1341 times.
✓ Branch 18 taken 1486 times.
✓ Branch 19 taken 864 times.
✗ Branch 20 not taken.
54711 switch (type) {
63 7241 case ast::tokens::BOOL : return LLVMType<bool>::get(C);
64 564 case ast::tokens::INT16 : return LLVMType<int16_t>::get(C);
65 5000 case ast::tokens::INT32 : return LLVMType<int32_t>::get(C);
66 1640 case ast::tokens::INT64 : return LLVMType<int64_t>::get(C);
67 10425 case ast::tokens::FLOAT : return LLVMType<float>::get(C);
68 6495 case ast::tokens::DOUBLE : return LLVMType<double>::get(C);
69 885 case ast::tokens::VEC2I : return LLVMType<int32_t[2]>::get(C);
70 929 case ast::tokens::VEC2F : return LLVMType<float[2]>::get(C);
71 920 case ast::tokens::VEC2D : return LLVMType<double[2]>::get(C);
72 1451 case ast::tokens::VEC3I : return LLVMType<int32_t[3]>::get(C);
73 6383 case ast::tokens::VEC3F : return LLVMType<float[3]>::get(C);
74 2442 case ast::tokens::VEC3D : return LLVMType<double[3]>::get(C);
75 1249 case ast::tokens::VEC4I : return LLVMType<int32_t[4]>::get(C);
76 1282 case ast::tokens::VEC4F : return LLVMType<float[4]>::get(C);
77 1327 case ast::tokens::VEC4D : return LLVMType<double[4]>::get(C);
78 1322 case ast::tokens::MAT3F : return LLVMType<float[9]>::get(C);
79 1465 case ast::tokens::MAT3D : return LLVMType<double[9]>::get(C);
80 1341 case ast::tokens::MAT4F : return LLVMType<float[16]>::get(C);
81 1486 case ast::tokens::MAT4D : return LLVMType<double[16]>::get(C);
82 864 case ast::tokens::STRING : return LLVMType<codegen::String>::get(C);
83 case ast::tokens::UNKNOWN :
84 default :
85 OPENVDB_THROW(AXCodeGenError,
86 "Token type not recognised in request for LLVM type");
87 }
88 }
89
90 ast::tokens::CoreType
91
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 44 times.
67 tokenFromLLVMType(const llvm::Type* type)
92 {
93
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 44 times.
67 if (type->isPointerTy()) {
94 type = type->getPointerElementType();
95 }
96
2/2
✓ Branch 1 taken 66 times.
✓ Branch 2 taken 1 times.
67 if (type->isIntegerTy(1)) return ast::tokens::BOOL;
97
2/2
✓ Branch 1 taken 64 times.
✓ Branch 2 taken 2 times.
66 if (type->isIntegerTy(16)) return ast::tokens::INT16;
98
2/2
✓ Branch 1 taken 51 times.
✓ Branch 2 taken 13 times.
64 if (type->isIntegerTy(32)) return ast::tokens::INT32;
99
2/2
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 7 times.
51 if (type->isIntegerTy(64)) return ast::tokens::INT64;
100
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 10 times.
44 if (type->isFloatTy()) return ast::tokens::FLOAT;
101
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 6 times.
34 if (type->isDoubleTy()) return ast::tokens::DOUBLE;
102
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 8 times.
28 if (type->isArrayTy()) {
103 const ast::tokens::CoreType elementType =
104 20 tokenFromLLVMType(type->getArrayElementType());
105 const size_t size = type->getArrayNumElements();
106
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 17 times.
20 if (size == 2) {
107
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (elementType == ast::tokens::INT32) return ast::tokens::VEC2I;
108
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (elementType == ast::tokens::FLOAT) return ast::tokens::VEC2F;
109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (elementType == ast::tokens::DOUBLE) return ast::tokens::VEC2D;
110 }
111
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 9 times.
17 else if (size == 3) {
112
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 if (elementType == ast::tokens::INT32) return ast::tokens::VEC3I;
113
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
6 if (elementType == ast::tokens::FLOAT) return ast::tokens::VEC3F;
114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (elementType == ast::tokens::DOUBLE) return ast::tokens::VEC3D;
115 }
116
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
9 else if (size == 4) {
117
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (elementType == ast::tokens::INT32) return ast::tokens::VEC4I;
118
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (elementType == ast::tokens::FLOAT) return ast::tokens::VEC4F;
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (elementType == ast::tokens::DOUBLE) return ast::tokens::VEC4D;
120 }
121
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 else if (size == 9) {
122
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (elementType == ast::tokens::FLOAT) return ast::tokens::MAT3F;
123
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (elementType == ast::tokens::DOUBLE) return ast::tokens::MAT3D;
124 }
125
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 else if (size == 16) {
126
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (elementType == ast::tokens::FLOAT) return ast::tokens::MAT4F;
127
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (elementType == ast::tokens::DOUBLE) return ast::tokens::MAT4D;
128 }
129 }
130
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 7 times.
10 if (type == LLVMType<codegen::String>::get(type->getContext())) {
131 3 return ast::tokens::STRING;
132 }
133 return ast::tokens::UNKNOWN;
134 }
135
136 } // namespace codegen
137 } // namespace ax
138 } // namespace OPENVDB_VERSION_NAME
139 } // namespace openvdb
140
141