• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tudasc / TypeART / 12904858633

22 Jan 2025 09:11AM UTC coverage: 90.735% (+0.005%) from 90.73%
12904858633

Pull #149

github

web-flow
Merge f0a5eda87 into 37d7f8cb1
Pull Request #149: Replace llvm::Optional with std::optional

88 of 102 new or added lines in 23 files covered. (86.27%)

1 existing line in 1 file now uncovered.

3829 of 4220 relevant lines covered (90.73%)

117945.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.93
/lib/passes/typegen/ir/VectorTypeHandler.cpp
1
// TypeART library
2
//
3
// Copyright (c) 2017-2025 TypeART Authors
4
// Distributed under the BSD 3-Clause license.
5
// (See accompanying file LICENSE.txt or copy at
6
// https://opensource.org/licenses/BSD-3-Clause)
7
//
8
// Project home: https://github.com/tudasc/TypeART
9
//
10
// SPDX-License-Identifier: BSD-3-Clause
11
//
12

13
#include "VectorTypeHandler.h"
14

15
#include "TypeManager.h"
16
#include "support/Logger.h"
17
#include "support/Util.h"
18
#include "typelib/TypeDatabase.h"
19
#include "typelib/TypeInterface.h"
20

21
#include "llvm/ADT/StringRef.h"
22
#include "llvm/IR/DataLayout.h"
23
#include "llvm/IR/DerivedTypes.h"
24
#include "llvm/IR/Type.h"
25
#include "llvm/Support/TypeSize.h"
26
#include "llvm/Support/raw_ostream.h"
27

28
#include <cassert>
29
#include <vector>
30

31
namespace typeart {
32

33
using namespace llvm;
34

35
namespace compat {
36
auto num_elements(llvm::VectorType* type) {
63✔
37
#if LLVM_VERSION_MAJOR < 11
38
  return type->getVectorNumElements();
39
#else
40
  auto* fixed_vec = dyn_cast<llvm::FixedVectorType>(type);
63✔
41
  assert(fixed_vec);
63✔
42
  return fixed_vec->getNumElements();
63✔
43
#endif
44
}
45
auto type_elements(llvm::VectorType* type) {
126✔
46
#if LLVM_VERSION_MAJOR < 11
47
  return type->getVectorElementType();
48
#else
49
  auto* fixed_vec = dyn_cast<llvm::FixedVectorType>(type);
126✔
50
  assert(fixed_vec);
126✔
51
  return fixed_vec->getElementType();
126✔
52
#endif
53
}
54
}  // namespace compat
55

56
Type* VectorTypeHandler::getElementType(llvm::VectorType* type) {
126✔
57
  auto element_type = compat::type_elements(type);
126✔
58

59
  // Should never happen, as vectors are first class types.
60
  assert(!element_type->isAggregateType() && "Unexpected vector type encountered: vector of aggregate type.");
252✔
61

62
  return element_type;
126✔
63
}
64

65
std::optional<VectorTypeHandler::VectorData> VectorTypeHandler::getVectorData() const {
15✔
66
  size_t vectorBytes = dl.getTypeAllocSize(type);
15✔
67

68
  size_t vectorSize = compat::num_elements(type);
15✔
69
  auto element_data = getElementData();
15✔
70

71
  if (!element_data) {
15✔
72
    LOG_DEBUG("Element data empty for: " << util::dump(*type))
NEW
73
    return {};
×
74
  }
75

76
  auto vec_name = getName(element_data.value());
15✔
77

78
  return VectorData{vec_name, vectorBytes, vectorSize};
15✔
79
}
15✔
80

81
std::optional<VectorTypeHandler::ElementData> VectorTypeHandler::getElementData() const {
63✔
82
  const auto element_id = getElementID();
63✔
83
  if (!element_id || element_id.value() == TYPEART_UNKNOWN_TYPE) {
63✔
84
    LOG_WARNING("Unknown vector element id.")
×
NEW
85
    return {};
×
86
  }
87

88
  auto element_name = m_type_db->getTypeName(element_id.value());
63✔
89
  auto element_type = VectorTypeHandler::getElementType(type);
63✔
90
  return ElementData{element_id.value(), element_type, element_name};
63✔
91
}
63✔
92

93
std::optional<int> VectorTypeHandler::getElementID() const {
63✔
94
  auto element_type     = getElementType(type);
63✔
95
  const auto element_id = m.getTypeID(element_type, dl);
63✔
96

97
  if (element_id == TYPEART_UNKNOWN_TYPE) {
63✔
98
    LOG_ERROR("Encountered vector of unknown type" << util::dump(*type));
×
99
    return TYPEART_UNKNOWN_TYPE;
×
100
  }
101

102
  return element_id;
63✔
103
}
63✔
104

105
std::string VectorTypeHandler::getName(const ElementData& data) const {
48✔
106
  size_t vectorSize = compat::num_elements(type);
48✔
107
  auto name         = "vec" + std::to_string(vectorSize) + ":" + data.element_name;
48✔
108

109
  return name;
48✔
110
}
48✔
111

112
std::optional<int> VectorTypeHandler::getID() const {
33✔
113
  auto element_data = getElementData();
33✔
114

115
  if (!element_data) {
33✔
116
    LOG_ERROR("Cannot determine element data for " << util::dump(*type))
×
117
    return TYPEART_UNKNOWN_TYPE;
×
118
  }
119

120
  const auto name = getName(element_data.value());
33✔
121

122
  if (auto it = m_struct_map->find(name); it != m_struct_map->end()) {
33✔
123
    if (!m_type_db->isVectorType(it->second)) {
18✔
124
      LOG_ERROR("Expected vector type for name:" << name << " Vector: " << util::dump(*type))
×
125
      return TYPEART_UNKNOWN_TYPE;
×
126
    }
127
    return it->second;
18✔
128
  }
129

130
  return {};
15✔
131
}
33✔
132

133
}  // namespace typeart
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc