-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathllvm_codegen_utils.cpp
More file actions
149 lines (142 loc) · 4.74 KB
/
Copy pathllvm_codegen_utils.cpp
File metadata and controls
149 lines (142 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "llvm_codegen_utils.h"
namespace gstaichi::lang {
std::string type_name(llvm::Type *type) {
std::string type_name_str;
llvm::raw_string_ostream rso(type_name_str);
type->print(rso, /*IsForDebug=*/false, /*NoDetails=*/true);
return type_name_str;
}
/*
* Determine whether two types are the same
* (a type is a renamed version of the other one) based on the
* type name. Check recursively if the types are function types.
*
* Types like `PhysicalCoordinates` occur in every struct module.
* When a struct module is copied into a LLVM context,
* types in the module which already exist in the context are renamed
* by adding a suffix starting with a "." following by a number.
* For example, "PhysicalCoordinates" may be renamed to
* names like "PhysicalCoordinates.0" and "PhysicalCoordinates.8".
*/
bool is_same_type(llvm::Type *a, llvm::Type *b) {
if (a == b) {
return true;
}
if (a->isPointerTy() != b->isPointerTy()) {
return false;
}
if (a->isPointerTy()) {
auto ptr_a = llvm::cast<llvm::PointerType>(a);
auto ptr_b = llvm::cast<llvm::PointerType>(b);
return ptr_a->getAddressSpace() == ptr_b->getAddressSpace();
}
if (a->isFunctionTy() != b->isFunctionTy()) {
return false;
}
if (a->isFunctionTy()) {
auto req_func = llvm::cast<llvm::FunctionType>(a);
auto prov_func = llvm::cast<llvm::FunctionType>(b);
if (!is_same_type(req_func->getReturnType(), prov_func->getReturnType())) {
return false;
}
if (req_func->getNumParams() != prov_func->getNumParams()) {
return false;
}
for (int j = 0; j < req_func->getNumParams(); j++) {
if (!is_same_type(req_func->getParamType(j),
prov_func->getParamType(j))) {
return false;
}
}
return true;
}
auto a_name = type_name(a);
auto b_name = type_name(b);
if (a_name.size() > b_name.size()) {
std::swap(a_name, b_name);
}
int len_same = 0;
while (len_same < a_name.size()) {
if (a_name[len_same] != b_name[len_same]) {
break;
}
len_same++;
}
if (len_same == a_name.size()) {
TI_ASSERT(len_same != b_name.size());
if (b_name[len_same] == '.') {
// a is xxx, and b is xxx.yyy, yyy is a number
for (int i = len_same + 1; i < b_name.size(); i++) {
if (!std::isdigit(b_name[i])) {
return false;
}
}
return true;
}
}
// a is xxx.yyy, and b is xxx.zzz, yyy and zzz are numbers
if (len_same == 0) {
return false;
}
int dot_pos = len_same - 1;
while (dot_pos && a_name[dot_pos] != '.') {
dot_pos--;
}
if (!dot_pos) {
return false;
}
for (int i = dot_pos + 1; i < a_name.size(); i++) {
if (!std::isdigit(a_name[i])) {
return false;
}
}
for (int i = dot_pos + 1; i < b_name.size(); i++) {
if (!std::isdigit(b_name[i])) {
return false;
}
}
return true;
}
void check_func_call_signature(llvm::FunctionType *func_type,
llvm::StringRef func_name,
std::vector<llvm::Value *> &arglist,
llvm::IRBuilder<> *builder) {
int num_params = func_type->getFunctionNumParams();
if (func_type->isFunctionVarArg()) {
TI_ASSERT(num_params <= arglist.size());
} else {
TI_ERROR_IF(num_params != arglist.size(),
"Function \"{}\" requires {} arguments but {} provided",
std::string(func_name), num_params, arglist.size());
}
for (int i = 0; i < num_params; i++) {
auto required = func_type->getFunctionParamType(i);
auto provided = arglist[i]->getType();
/*
* When importing a module from file, the imported `llvm::Type`s can get
* conflict with the same type in the llvm::Context. In such scenario,
* the imported types will be renamed from "original_type" to
* "original_type.xxx", making them separate types in essence.
* To make the types of the argument and parameter the same,
* a pointer cast must be performed.
*/
if (required != provided) {
if (is_same_type(required, provided)) {
arglist[i] = builder->CreatePointerCast(arglist[i], required);
continue;
}
TI_INFO("Function : {}", std::string(func_name));
TI_INFO(" Type : {}", type_name(func_type));
if (&required->getContext() != &provided->getContext()) {
TI_INFO(" parameter {} types are from different contexts", i);
TI_INFO(" required from context {}",
(void *)&required->getContext());
TI_INFO(" provided from context {}",
(void *)&provided->getContext());
}
TI_ERROR(" parameter {} mismatch: required={}, provided={}", i,
type_name(required), type_name(provided));
}
}
}
} // namespace gstaichi::lang