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

tudasc / TypeART / 25177145272

30 Apr 2026 04:30PM UTC coverage: 84.647% (-5.6%) from 90.246%
25177145272

Pull #188

github

web-flow
Merge 88918912c into 278119205
Pull Request #188: GPU memory allocation support

127 of 259 new or added lines in 19 files covered. (49.03%)

200 existing lines in 18 files now uncovered.

4510 of 5328 relevant lines covered (84.65%)

27776.73 hits per line

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

0.0
/lib/passes/support/TypeUtil.cpp
1
// TypeART library
2
//
3
// Copyright (c) 2017-2026 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 "TypeUtil.h"
14

15
#include "support/Logger.h"
16

17
#include "llvm/IR/Constants.h"
18
#include "llvm/IR/DataLayout.h"
19
#include "llvm/IR/DerivedTypes.h"
20
#include "llvm/IR/Instructions.h"
21
#include "llvm/IR/Type.h"
22
#include "llvm/IR/Value.h"
23
#include "llvm/Support/Casting.h"
24
#include "llvm/Support/TypeSize.h"
25
#include "llvm/Support/raw_ostream.h"
26

27
#include <optional>
28

29
using namespace llvm;
30

31
namespace typeart::util::type {
32

33
#if LLVM_VERSION_MAJOR < 15
34
bool isi64Ptr(llvm::Type* type) {
35
  return type->isPointerTy() && type->getPointerElementType()->isIntegerTy(64);
36
}
37

38
bool isVoidPtr(llvm::Type* type) {
39
  return type->isPointerTy() && type->getPointerElementType()->isIntegerTy(8);
40
}
41
#endif
42

43
/**
44
 * Code was imported from jplehr/llvm-memprofiler project
45
 */
UNCOV
46
unsigned getTypeSizeInBytes(llvm::Type* t, const llvm::DataLayout& dl) {
×
UNCOV
47
  unsigned bytes = getScalarSizeInBytes(t);
×
48

UNCOV
49
  if (t->isArrayTy()) {
×
UNCOV
50
    bytes = getArraySizeInBytes(t, dl);
×
UNCOV
51
  } else if (t->isStructTy()) {
×
UNCOV
52
    bytes = getStructSizeInBytes(t, dl);
×
UNCOV
53
  } else if (t->isPointerTy()) {
×
UNCOV
54
    bytes = getPointerSizeInBytes(t, dl);
×
UNCOV
55
  } else if (t->isVectorTy()) {
×
UNCOV
56
    bytes = getVectorSizeInBytes(t, dl);
×
UNCOV
57
  }
×
58

UNCOV
59
  return bytes;
×
60
}
61

UNCOV
62
unsigned getScalarSizeInBytes(llvm::Type* t) {
×
UNCOV
63
  return t->getScalarSizeInBits() / 8;
×
64
}
65

UNCOV
66
unsigned getArraySizeInBytes(llvm::Type* arrT, const llvm::DataLayout& dl) {
×
UNCOV
67
  auto st = dyn_cast<ArrayType>(arrT);
×
UNCOV
68
  return getTypeSizeInBytes(getArrayElementType(st), dl) * getArrayLengthFlattened(st);
×
69
}
70

UNCOV
71
unsigned getVectorSizeInBytes(llvm::Type* vectorT, const llvm::DataLayout& dl) {
×
72
  // TODO: Most of these utility functions can be eliminated with the use of getTypeAllocSize() and getTypeStoreSize()
73
  //  auto vt = dyn_cast<VectorType>(vectorT);
UNCOV
74
  return dl.getTypeAllocSize(vectorT);
×
75
  // return getTypeSizeInBytes(vt->getVectorElementType(), dl) * vt->getVectorNumElements();
76
}
77

78
/**
79
 * \brief Resolves the element type of the given array recursively. Works for multidimensional arrays.
80
 */
UNCOV
81
llvm::Type* getArrayElementType(llvm::Type* arrT) {
×
UNCOV
82
  while (arrT->isArrayTy()) {
×
UNCOV
83
    arrT = arrT->getArrayElementType();
×
84
  }
UNCOV
85
  return arrT;
×
86
}
87

88
/**
89
 * \brief Determines the length of the flattened array.
90
 *  TODO: Handle VLAs
91
 */
UNCOV
92
unsigned getArrayLengthFlattened(llvm::Type* arrT) {
×
UNCOV
93
  unsigned len = 1;
×
UNCOV
94
  while (arrT->isArrayTy()) {
×
UNCOV
95
    len *= arrT->getArrayNumElements();
×
UNCOV
96
    arrT = arrT->getArrayElementType();
×
97
  }
UNCOV
98
  return len;
×
99
}
100

UNCOV
101
unsigned getStructSizeInBytes(llvm::Type* structT, const llvm::DataLayout& dl) {
×
UNCOV
102
  auto st                    = dyn_cast<llvm::StructType>(structT);
×
UNCOV
103
  const StructLayout* layout = dl.getStructLayout(st);
×
UNCOV
104
  return layout->getSizeInBytes();
×
105
}
106

UNCOV
107
unsigned getPointerSizeInBytes(llvm::Type* /*ptrT*/, const llvm::DataLayout& dl) {
×
UNCOV
108
  return dl.getPointerSizeInBits() / 8;
×
109
}
110

NEW
111
std::optional<llvm::Type*> getPointerElementType(llvm::Type* ptr_type) {
×
NEW
112
  auto* pointer_type = dyn_cast_or_null<llvm::PointerType>(ptr_type);
×
NEW
113
  if (pointer_type == nullptr) {
×
NEW
114
    return {};
×
115
  }
116

117
#if LLVM_VERSION_MAJOR < 15
118
  return pointer_type->getPointerElementType();
119
#else
NEW
120
  return {};
×
121
#endif
NEW
122
}
×
123

124
}  // namespace typeart::util::type
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