Skip to content

Commit 4aef434

Browse files
KorovinVladigcbot
authored andcommitted
Replace invalid symbols in function names generated by llvm
Replace '.', '$' symbols with '_'
1 parent 1ec85ab commit 4aef434

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed

IGC/VectorCompiler/lib/GenXCodeGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ set(CODEGEN_SOURCES
8484
OCLRuntimeInfoPrinter.cpp
8585
GenXStructSplitter.cpp
8686
GenXDebugLegalization.cpp
87+
GenXFixInvalidFuncName.cpp
8788
)
8889

8990
add_library(VCCodeGen ${CODEGEN_SOURCES})

IGC/VectorCompiler/lib/GenXCodeGen/GenX.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ ModulePass *createGenXStackUsagePass();
118118
ModulePass *createGenXStructSplitterPass();
119119
FunctionPass *createGenXPredRegionLoweringPass();
120120
FunctionPass *createGenXDebugLegalizationPass();
121+
FunctionPass *createGenXFixInvalidFuncNamePass();
121122

122123
namespace genx {
123124

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*========================== begin_copyright_notice ============================
2+
3+
Copyright (C) 2022 Intel Corporation
4+
5+
SPDX-License-Identifier: MIT
6+
7+
============================= end_copyright_notice ===========================*/
8+
9+
//
10+
/// GenXFixInvalidFuncName
11+
/// ------------
12+
///
13+
/// GenXFixInvalidFuncName is a function pass that replaces all '.' and '$'
14+
/// characters with '_' in function names generated by llvm in all call
15+
/// instructions in the current function and in the name of the current function.
16+
/// It needs to be kept in sync with the same pass on the SPMD side.
17+
///
18+
//===----------------------------------------------------------------------===//
19+
20+
#include "GenX.h"
21+
22+
#include "llvm/IR/Function.h"
23+
#include "llvm/InitializePasses.h"
24+
#include "llvm/Pass.h"
25+
#include "llvm/Support/Debug.h"
26+
27+
#define DEBUG_TYPE "GENX_FIX_INVALID_FUNC_NAMES"
28+
29+
using namespace llvm;
30+
31+
class GenXFixInvalidFuncName : public FunctionPass
32+
{
33+
public:
34+
static char ID;
35+
explicit GenXFixInvalidFuncName() : FunctionPass(ID) {}
36+
llvm::StringRef getPassName() const override { return "Fix Invalid Func Name"; }
37+
bool runOnFunction(Function& F) override;
38+
private:
39+
std::string replaceInvalidCharToUnderline(std::string str);
40+
bool isSupportedCallingConv(CallingConv::ID callingConv);
41+
bool changeFuncName(Function& F);
42+
};
43+
44+
bool GenXFixInvalidFuncName::runOnFunction(Function& F) {
45+
bool modified = false;
46+
if(isSupportedCallingConv(F.getCallingConv())) {
47+
modified = changeFuncName(F);
48+
}
49+
for (Function::iterator fi = F.begin(), fe = F.end(); fi != fe; ++fi) {
50+
BasicBlock *BB = &*fi;
51+
for (BasicBlock::iterator bi = BB->begin(), be = BB->end(); bi != be; ++bi) {
52+
Instruction *Inst = &*bi;
53+
if (CallInst* callInst = dyn_cast<CallInst>(Inst)) {
54+
if (isSupportedCallingConv(callInst->getCallingConv())) {
55+
Function* func = callInst->getCalledFunction();
56+
if (func) {
57+
modified = changeFuncName(*func);
58+
}
59+
}
60+
}
61+
}
62+
}
63+
return modified;
64+
}
65+
66+
std::string GenXFixInvalidFuncName::replaceInvalidCharToUnderline(std::string str) {
67+
std::replace_if(str.begin(), str.end(), [](const char c){ return c == '.' || c == '$'; }, '_');
68+
return str;
69+
}
70+
71+
bool GenXFixInvalidFuncName::isSupportedCallingConv(CallingConv::ID callingConv) {
72+
return callingConv == CallingConv::SPIR_FUNC;
73+
}
74+
75+
bool GenXFixInvalidFuncName::changeFuncName(Function& F) {
76+
bool isNameChanged = false;
77+
std::string original = F.getName().str();
78+
std::string changed = replaceInvalidCharToUnderline(original);
79+
if (original != changed) {
80+
F.setName(changed);
81+
isNameChanged = true;
82+
}
83+
return isNameChanged;
84+
}
85+
86+
char GenXFixInvalidFuncName::ID = 0;
87+
namespace llvm {
88+
void initializeGenXFixInvalidFuncNamePass(PassRegistry &);
89+
}
90+
INITIALIZE_PASS_BEGIN(GenXFixInvalidFuncName, "GenXFixInvalidFuncName", "GenXFixInvalidFuncName", false, false)
91+
INITIALIZE_PASS_END(GenXFixInvalidFuncName, "GenXFixInvalidFuncName", "GenXFixInvalidFuncName", false, false)
92+
FunctionPass *llvm::createGenXFixInvalidFuncNamePass() {
93+
initializeGenXFixInvalidFuncNamePass(*PassRegistry::getPassRegistry());
94+
return new GenXFixInvalidFuncName;
95+
}

IGC/VectorCompiler/lib/GenXCodeGen/GenXTargetMachine.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ void initializeGenXPasses(PassRegistry &registry) {
152152
initializeGenXLinkageCorruptorPass(registry);
153153
initializeGenXInlineAsmLoweringPass(registry);
154154
initializeGenXDebugLegalizationPass(registry);
155+
initializeGenXFixInvalidFuncNamePass(registry);
155156

156157
// WRITE HERE MORE PASSES IF IT'S NEEDED;
157158
}
@@ -300,6 +301,8 @@ bool GenXTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
300301
vc::addPass(PM, PassConfig);
301302
const GenXBackendConfig &BackendConfig = PassConfig->getBackendConfig();
302303

304+
vc::addPass(PM, createGenXFixInvalidFuncNamePass());
305+
303306
// Install GenX-specific TargetTransformInfo for passes such as
304307
// LowerAggrCopies and InfoAddressSpace
305308
vc::addPass(PM, createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
@@ -592,6 +595,13 @@ bool GenXTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
592595
}
593596

594597
void GenXTargetMachine::adjustPassManager(PassManagerBuilder &PMBuilder) {
598+
// Fix function names.
599+
PMBuilder.addExtension(
600+
PassManagerBuilder::EP_EarlyAsPossible,
601+
[](const PassManagerBuilder &Builder, PassManagerBase &PM) {
602+
PM.add(createGenXFixInvalidFuncNamePass());
603+
});
604+
595605
// Lower aggr copies.
596606
PMBuilder.addExtension(
597607
PassManagerBuilder::EP_EarlyAsPossible,

IGC/VectorCompiler/lib/GenXCodeGen/GenXTargetMachine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ void initializeGenXStructSplitterPass(PassRegistry &);
253253
void initializeGenXPredRegionLoweringPass(PassRegistry &);
254254
void initializeGenXInlineAsmLoweringPass(PassRegistry &);
255255
void initializeGenXDebugLegalizationPass(PassRegistry &);
256+
void initializeGenXFixInvalidFuncNamePass(PassRegistry &);
256257
} // End llvm namespace
257258

258259
#endif

0 commit comments

Comments
 (0)