Skip to content

Commit c68257e

Browse files
authored
cmake fix for enzyme+opt (rust-lang#691)
* test cmake fix
1 parent 47ac42b commit c68257e

File tree

2 files changed

+85
-17
lines changed

2 files changed

+85
-17
lines changed

enzyme/Enzyme/ActivityAnalysis.cpp

Lines changed: 83 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
#include "ActivityAnalysis.h"
4747
#include "Utils.h"
4848

49+
#if LLVM_VERSION_MAJOR >= 9
50+
#include "llvm/Demangle/Demangle.h"
51+
#endif
52+
4953
#include "FunctionUtils.h"
5054
#include "LibraryFuncs.h"
5155
#include "TypeAnalysis/TBAA.h"
@@ -79,24 +83,22 @@ cl::opt<bool>
7983
#include <unordered_map>
8084

8185
const char *KnownInactiveFunctionsStartingWith[] = {
82-
"_ZN4core3fmt",
83-
"_ZN3std2io5stdio6_print",
8486
"f90io",
8587
"$ss5print",
88+
#if LLVM_VERSION_MAJOR <= 8
89+
"_ZN4core3fmt",
90+
"_ZN3std2io5stdio6_print",
8691
"_ZNSt7__cxx1112basic_string",
8792
"_ZNSt7__cxx1118basic_string",
8893
"_ZNKSt7__cxx1112basic_string",
8994
"_ZN9__gnu_cxx12__to_xstringINSt7__cxx1112basic_string",
90-
// filebuf
9195
"_ZNSt12__basic_file",
9296
"_ZNSt15basic_streambufIcSt11char_traits",
9397
"_ZNSt13basic_filebufIcSt11char_traits",
9498
"_ZNSt14basic_ofstreamIcSt11char_traits",
95-
// ifstream
9699
"_ZNSi4readEPcl",
97100
"_ZNKSt14basic_ifstreamIcSt11char_traits",
98101
"_ZNSt14basic_ifstreamIcSt11char_traits",
99-
// ostream generic <<
100102
"_ZNSo5writeEPKcl",
101103
"_ZNSt19basic_ostringstreamIcSt11char_traits",
102104
"_ZStrsIcSt11char_traitsIcESaIcEERSt13basic_istream",
@@ -109,42 +111,31 @@ const char *KnownInactiveFunctionsStartingWith[] = {
109111
"_ZSt16__ostream_insert",
110112
"_ZStlsIwSt11char_traitsIwEERSt13basic_ostream",
111113
"_ZNSo9_M_insert",
112-
// ostream wchar
113114
"_ZNSt13basic_ostream",
114-
// ostream put
115115
"_ZNSo3put",
116-
// std::istream: widen_init, get, getline, >>, sync, ignore
117116
"_ZNKSt5ctypeIcE13_M_widen_init",
118117
"_ZNSi3get",
119118
"_ZNSi7getline",
120119
"_ZNSirsER",
121120
"_ZNSt7__cxx1115basic_stringbuf",
122121
"_ZNSi6ignore",
123-
// std::ios_base
124122
"_ZNSt8ios_base",
125123
"_ZNKSt9basic_ios",
126124
"_ZNSt9basic_ios",
127125
"_ZStorSt13_Ios_OpenmodeS_",
128-
// std::local
129126
"_ZNSt6locale",
130127
"_ZNKSt6locale4name",
131-
// init
132128
"_ZStL8__ioinit"
133129
"_ZNSt9basic_ios",
134-
// std::cout
135130
"_ZSt4cout",
136-
// std::cin
137131
"_ZSt3cin",
138132
"_ZNSi10_M_extract",
139-
// generic <<
140133
"_ZNSolsE",
141-
// std::flush
142134
"_ZSt5flush",
143135
"_ZNSo5flush",
144-
// std::endl
145136
"_ZSt4endl",
146-
// std::allocator
147137
"_ZNSaIcE",
138+
#endif
148139
};
149140

150141
const char *KnownInactiveFunctionsContains[] = {
@@ -202,6 +193,7 @@ const std::set<std::string> KnownInactiveFunctions = {
202193
"snprintf",
203194
"sprintf",
204195
"printf",
196+
"fprintf",
205197
"putchar",
206198
"fprintf",
207199
"vprintf",
@@ -266,6 +258,32 @@ const std::set<std::string> KnownInactiveFunctions = {
266258
"logbl",
267259
};
268260

261+
const char *DemangledKnownInactiveFunctionsStartingWith[] = {
262+
"fprintf",
263+
"std::allocator",
264+
"std::string",
265+
"std::cerr",
266+
"std::basic_ios",
267+
"std::basic_istream",
268+
"std::basic_ostream",
269+
"std::basic_ifstream",
270+
"std::basic_ofstream",
271+
"std::basic_filebuf",
272+
"std::basic_streambuf",
273+
"std::istream",
274+
"std::ostream",
275+
"std::ios_base",
276+
"std::locale",
277+
"std::ctype<char>",
278+
"std::__cxx11::basic_string",
279+
"std::__cxx11::basic_ostringstream",
280+
"std::__cxx11::basic_istringstream",
281+
"std::__cxx11::basic_stringbuf",
282+
"std::__basic_file",
283+
"std::__ioinit",
284+
"std::__basic_file",
285+
};
286+
269287
/// Is the use of value val as an argument of call CI known to be inactive
270288
/// This tool can only be used when in DOWN mode
271289
bool ActivityAnalyzer::isFunctionArgumentConstant(CallInst *CI, Value *val) {
@@ -292,11 +310,28 @@ bool ActivityAnalyzer::isFunctionArgumentConstant(CallInst *CI, Value *val) {
292310
if (Name == "posix_memalign")
293311
return true;
294312

313+
#if LLVM_VERSION_MAJOR >= 9
314+
std::string demangledName = llvm::demangle(Name.str());
315+
auto dName = StringRef(demangledName);
316+
for (auto FuncName : DemangledKnownInactiveFunctionsStartingWith) {
317+
if (dName.startswith(FuncName)) {
318+
return true;
319+
}
320+
}
321+
if (demangledName == Name.str()) {
322+
// Either demangeling failed
323+
// or they are equal but matching failed
324+
// if (!Name.startswith("llvm."))
325+
// llvm::errs() << "matching failed: " << Name.str() << " "
326+
// << demangledName << "\n";
327+
}
328+
#endif
295329
for (auto FuncName : KnownInactiveFunctionsStartingWith) {
296330
if (Name.startswith(FuncName)) {
297331
return true;
298332
}
299333
}
334+
300335
for (auto FuncName : KnownInactiveFunctionsContains) {
301336
if (Name.contains(FuncName)) {
302337
return true;
@@ -305,6 +340,7 @@ bool ActivityAnalyzer::isFunctionArgumentConstant(CallInst *CI, Value *val) {
305340
if (KnownInactiveFunctions.count(Name.str())) {
306341
return true;
307342
}
343+
308344
if (MPIInactiveCommAllocators.find(Name.str()) !=
309345
MPIInactiveCommAllocators.end()) {
310346
return true;
@@ -1253,13 +1289,25 @@ bool ActivityAnalyzer::isConstantValue(TypeResults const &TR, Value *Val) {
12531289
return true;
12541290
}
12551291

1292+
#if LLVM_VERSION_MAJOR >= 9
1293+
auto dName = demangle(called->getName().str());
1294+
for (auto FuncName : DemangledKnownInactiveFunctionsStartingWith) {
1295+
if (StringRef(dName).startswith(FuncName)) {
1296+
InsertConstantValue(TR, Val);
1297+
insertConstantsFrom(TR, *UpHypothesis);
1298+
return true;
1299+
}
1300+
}
1301+
#endif
1302+
12561303
for (auto FuncName : KnownInactiveFunctionsStartingWith) {
12571304
if (called->getName().startswith(FuncName)) {
12581305
InsertConstantValue(TR, Val);
12591306
insertConstantsFrom(TR, *UpHypothesis);
12601307
return true;
12611308
}
12621309
}
1310+
12631311
for (auto FuncName : KnownInactiveFunctionsContains) {
12641312
if (called->getName().contains(FuncName)) {
12651313
InsertConstantValue(TR, Val);
@@ -1438,6 +1486,14 @@ bool ActivityAnalyzer::isConstantValue(TypeResults const &TR, Value *Val) {
14381486
F->getName() == "__fd_sincos_1") {
14391487
return false;
14401488
}
1489+
#if LLVM_VERSION_MAJOR >= 9
1490+
auto dName = demangle(F->getName().str());
1491+
for (auto FuncName : DemangledKnownInactiveFunctionsStartingWith) {
1492+
if (StringRef(dName).startswith(FuncName)) {
1493+
return false;
1494+
}
1495+
}
1496+
#endif
14411497
for (auto FuncName : KnownInactiveFunctionsStartingWith) {
14421498
if (F->getName().startswith(FuncName)) {
14431499
return false;
@@ -2008,11 +2064,21 @@ bool ActivityAnalyzer::isInstructionInactiveFromOrigin(TypeResults const &TR,
20082064
return true;
20092065
}
20102066

2067+
#if LLVM_VERSION_MAJOR >= 9
2068+
auto dName = demangle(called->getName().str());
2069+
for (auto FuncName : DemangledKnownInactiveFunctionsStartingWith) {
2070+
if (StringRef(dName).startswith(FuncName)) {
2071+
return true;
2072+
}
2073+
}
2074+
#endif
2075+
20112076
for (auto FuncName : KnownInactiveFunctionsStartingWith) {
20122077
if (called->getName().startswith(FuncName)) {
20132078
return true;
20142079
}
20152080
}
2081+
20162082
for (auto FuncName : KnownInactiveFunctionsContains) {
20172083
if (called->getName().contains(FuncName)) {
20182084
return true;

enzyme/Enzyme/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# set(LLVM_LINK_COMPONENTS Core Support)
33
#endif()
44

5+
set(LLVM_LINK_COMPONENTS Demangle)
6+
57
file(GLOB ENZYME_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
68
"*.cpp"
79
)

0 commit comments

Comments
 (0)