SymbolRewriter.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. //===- SymbolRewriter.cpp - Symbol Rewriter ---------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // SymbolRewriter is a LLVM pass which can rewrite symbols transparently within
  11. // existing code. It is implemented as a compiler pass and is configured via a
  12. // YAML configuration file.
  13. //
  14. // The YAML configuration file format is as follows:
  15. //
  16. // RewriteMapFile := RewriteDescriptors
  17. // RewriteDescriptors := RewriteDescriptor | RewriteDescriptors
  18. // RewriteDescriptor := RewriteDescriptorType ':' '{' RewriteDescriptorFields '}'
  19. // RewriteDescriptorFields := RewriteDescriptorField | RewriteDescriptorFields
  20. // RewriteDescriptorField := FieldIdentifier ':' FieldValue ','
  21. // RewriteDescriptorType := Identifier
  22. // FieldIdentifier := Identifier
  23. // FieldValue := Identifier
  24. // Identifier := [0-9a-zA-Z]+
  25. //
  26. // Currently, the following descriptor types are supported:
  27. //
  28. // - function: (function rewriting)
  29. // + Source (original name of the function)
  30. // + Target (explicit transformation)
  31. // + Transform (pattern transformation)
  32. // + Naked (boolean, whether the function is undecorated)
  33. // - global variable: (external linkage global variable rewriting)
  34. // + Source (original name of externally visible variable)
  35. // + Target (explicit transformation)
  36. // + Transform (pattern transformation)
  37. // - global alias: (global alias rewriting)
  38. // + Source (original name of the aliased name)
  39. // + Target (explicit transformation)
  40. // + Transform (pattern transformation)
  41. //
  42. // Note that source and exactly one of [Target, Transform] must be provided
  43. //
  44. // New rewrite descriptors can be created. Addding a new rewrite descriptor
  45. // involves:
  46. //
  47. // a) extended the rewrite descriptor kind enumeration
  48. // (<anonymous>::RewriteDescriptor::RewriteDescriptorType)
  49. // b) implementing the new descriptor
  50. // (c.f. <anonymous>::ExplicitRewriteFunctionDescriptor)
  51. // c) extending the rewrite map parser
  52. // (<anonymous>::RewriteMapParser::parseEntry)
  53. //
  54. // Specify to rewrite the symbols using the `-rewrite-symbols` option, and
  55. // specify the map file to use for the rewriting via the `-rewrite-map-file`
  56. // option.
  57. //
  58. //===----------------------------------------------------------------------===//
  59. #define DEBUG_TYPE "symbol-rewriter"
  60. #include "llvm/CodeGen/Passes.h"
  61. #include "llvm/Pass.h"
  62. #include "llvm/ADT/SmallString.h"
  63. #include "llvm/IR/LegacyPassManager.h"
  64. #include "llvm/Support/CommandLine.h"
  65. #include "llvm/Support/Debug.h"
  66. #include "llvm/Support/MemoryBuffer.h"
  67. #include "llvm/Support/Regex.h"
  68. #include "llvm/Support/SourceMgr.h"
  69. #include "llvm/Support/YAMLParser.h"
  70. #include "llvm/Support/raw_ostream.h"
  71. #include "llvm/Transforms/IPO/PassManagerBuilder.h"
  72. #include "llvm/Transforms/Utils/SymbolRewriter.h"
  73. using namespace llvm;
  74. using namespace SymbolRewriter;
  75. #if 0 // HLSL Change Starts - option pending
  76. static cl::list<std::string> RewriteMapFiles("rewrite-map-file",
  77. cl::desc("Symbol Rewrite Map"),
  78. cl::value_desc("filename"));
  79. #endif // HLSL Change Ends
  80. static void rewriteComdat(Module &M, GlobalObject *GO,
  81. const std::string &Source,
  82. const std::string &Target) {
  83. if (Comdat *CD = GO->getComdat()) {
  84. auto &Comdats = M.getComdatSymbolTable();
  85. Comdat *C = M.getOrInsertComdat(Target);
  86. C->setSelectionKind(CD->getSelectionKind());
  87. GO->setComdat(C);
  88. Comdats.erase(Comdats.find(Source));
  89. }
  90. }
  91. namespace {
  92. template <RewriteDescriptor::Type DT, typename ValueType,
  93. ValueType *(llvm::Module::*Get)(StringRef) const>
  94. class ExplicitRewriteDescriptor : public RewriteDescriptor {
  95. public:
  96. const std::string Source;
  97. const std::string Target;
  98. ExplicitRewriteDescriptor(StringRef S, StringRef T, const bool Naked)
  99. : RewriteDescriptor(DT), Source(Naked ? StringRef("\01" + S.str()) : S),
  100. Target(T) {}
  101. bool performOnModule(Module &M) override;
  102. static bool classof(const RewriteDescriptor *RD) {
  103. return RD->getType() == DT;
  104. }
  105. };
  106. template <RewriteDescriptor::Type DT, typename ValueType,
  107. ValueType *(llvm::Module::*Get)(StringRef) const>
  108. bool ExplicitRewriteDescriptor<DT, ValueType, Get>::performOnModule(Module &M) {
  109. bool Changed = false;
  110. if (ValueType *S = (M.*Get)(Source)) {
  111. if (GlobalObject *GO = dyn_cast<GlobalObject>(S))
  112. rewriteComdat(M, GO, Source, Target);
  113. if (Value *T = (M.*Get)(Target))
  114. S->setValueName(T->getValueName());
  115. else
  116. S->setName(Target);
  117. Changed = true;
  118. }
  119. return Changed;
  120. }
  121. template <RewriteDescriptor::Type DT, typename ValueType,
  122. ValueType *(llvm::Module::*Get)(StringRef) const,
  123. iterator_range<typename iplist<ValueType>::iterator>
  124. (llvm::Module::*Iterator)()>
  125. class PatternRewriteDescriptor : public RewriteDescriptor {
  126. public:
  127. const std::string Pattern;
  128. const std::string Transform;
  129. PatternRewriteDescriptor(StringRef P, StringRef T)
  130. : RewriteDescriptor(DT), Pattern(P), Transform(T) { }
  131. bool performOnModule(Module &M) override;
  132. static bool classof(const RewriteDescriptor *RD) {
  133. return RD->getType() == DT;
  134. }
  135. };
  136. template <RewriteDescriptor::Type DT, typename ValueType,
  137. ValueType *(llvm::Module::*Get)(StringRef) const,
  138. iterator_range<typename iplist<ValueType>::iterator>
  139. (llvm::Module::*Iterator)()>
  140. bool PatternRewriteDescriptor<DT, ValueType, Get, Iterator>::
  141. performOnModule(Module &M) {
  142. bool Changed = false;
  143. for (auto &C : (M.*Iterator)()) {
  144. std::string Error;
  145. std::string Name = Regex(Pattern).sub(Transform, C.getName(), &Error);
  146. if (!Error.empty())
  147. report_fatal_error("unable to transforn " + C.getName() + " in " +
  148. M.getModuleIdentifier() + ": " + Error);
  149. if (C.getName() == Name)
  150. continue;
  151. if (GlobalObject *GO = dyn_cast<GlobalObject>(&C))
  152. rewriteComdat(M, GO, C.getName(), Name);
  153. if (Value *V = (M.*Get)(Name))
  154. C.setValueName(V->getValueName());
  155. else
  156. C.setName(Name);
  157. Changed = true;
  158. }
  159. return Changed;
  160. }
  161. /// Represents a rewrite for an explicitly named (function) symbol. Both the
  162. /// source function name and target function name of the transformation are
  163. /// explicitly spelt out.
  164. typedef ExplicitRewriteDescriptor<RewriteDescriptor::Type::Function,
  165. llvm::Function, &llvm::Module::getFunction>
  166. ExplicitRewriteFunctionDescriptor;
  167. /// Represents a rewrite for an explicitly named (global variable) symbol. Both
  168. /// the source variable name and target variable name are spelt out. This
  169. /// applies only to module level variables.
  170. typedef ExplicitRewriteDescriptor<RewriteDescriptor::Type::GlobalVariable,
  171. llvm::GlobalVariable,
  172. &llvm::Module::getGlobalVariable>
  173. ExplicitRewriteGlobalVariableDescriptor;
  174. /// Represents a rewrite for an explicitly named global alias. Both the source
  175. /// and target name are explicitly spelt out.
  176. typedef ExplicitRewriteDescriptor<RewriteDescriptor::Type::NamedAlias,
  177. llvm::GlobalAlias,
  178. &llvm::Module::getNamedAlias>
  179. ExplicitRewriteNamedAliasDescriptor;
  180. /// Represents a rewrite for a regular expression based pattern for functions.
  181. /// A pattern for the function name is provided and a transformation for that
  182. /// pattern to determine the target function name create the rewrite rule.
  183. typedef PatternRewriteDescriptor<RewriteDescriptor::Type::Function,
  184. llvm::Function, &llvm::Module::getFunction,
  185. &llvm::Module::functions>
  186. PatternRewriteFunctionDescriptor;
  187. /// Represents a rewrite for a global variable based upon a matching pattern.
  188. /// Each global variable matching the provided pattern will be transformed as
  189. /// described in the transformation pattern for the target. Applies only to
  190. /// module level variables.
  191. typedef PatternRewriteDescriptor<RewriteDescriptor::Type::GlobalVariable,
  192. llvm::GlobalVariable,
  193. &llvm::Module::getGlobalVariable,
  194. &llvm::Module::globals>
  195. PatternRewriteGlobalVariableDescriptor;
  196. /// PatternRewriteNamedAliasDescriptor - represents a rewrite for global
  197. /// aliases which match a given pattern. The provided transformation will be
  198. /// applied to each of the matching names.
  199. typedef PatternRewriteDescriptor<RewriteDescriptor::Type::NamedAlias,
  200. llvm::GlobalAlias,
  201. &llvm::Module::getNamedAlias,
  202. &llvm::Module::aliases>
  203. PatternRewriteNamedAliasDescriptor;
  204. } // namespace
  205. bool RewriteMapParser::parse(const std::string &MapFile,
  206. RewriteDescriptorList *DL) {
  207. ErrorOr<std::unique_ptr<MemoryBuffer>> Mapping =
  208. MemoryBuffer::getFile(MapFile);
  209. if (!Mapping)
  210. report_fatal_error("unable to read rewrite map '" + MapFile + "': " +
  211. Mapping.getError().message());
  212. if (!parse(*Mapping, DL))
  213. report_fatal_error("unable to parse rewrite map '" + MapFile + "'");
  214. return true;
  215. }
  216. bool RewriteMapParser::parse(std::unique_ptr<MemoryBuffer> &MapFile,
  217. RewriteDescriptorList *DL) {
  218. SourceMgr SM;
  219. yaml::Stream YS(MapFile->getBuffer(), SM);
  220. for (auto &Document : YS) {
  221. yaml::MappingNode *DescriptorList;
  222. // ignore empty documents
  223. if (isa<yaml::NullNode>(Document.getRoot()))
  224. continue;
  225. DescriptorList = dyn_cast<yaml::MappingNode>(Document.getRoot());
  226. if (!DescriptorList) {
  227. YS.printError(Document.getRoot(), "DescriptorList node must be a map");
  228. return false;
  229. }
  230. for (auto &Descriptor : *DescriptorList)
  231. if (!parseEntry(YS, Descriptor, DL))
  232. return false;
  233. }
  234. return true;
  235. }
  236. bool RewriteMapParser::parseEntry(yaml::Stream &YS, yaml::KeyValueNode &Entry,
  237. RewriteDescriptorList *DL) {
  238. yaml::ScalarNode *Key;
  239. yaml::MappingNode *Value;
  240. SmallString<32> KeyStorage;
  241. StringRef RewriteType;
  242. Key = dyn_cast<yaml::ScalarNode>(Entry.getKey());
  243. if (!Key) {
  244. YS.printError(Entry.getKey(), "rewrite type must be a scalar");
  245. return false;
  246. }
  247. Value = dyn_cast<yaml::MappingNode>(Entry.getValue());
  248. if (!Value) {
  249. YS.printError(Entry.getValue(), "rewrite descriptor must be a map");
  250. return false;
  251. }
  252. RewriteType = Key->getValue(KeyStorage);
  253. if (RewriteType.equals("function"))
  254. return parseRewriteFunctionDescriptor(YS, Key, Value, DL);
  255. else if (RewriteType.equals("global variable"))
  256. return parseRewriteGlobalVariableDescriptor(YS, Key, Value, DL);
  257. else if (RewriteType.equals("global alias"))
  258. return parseRewriteGlobalAliasDescriptor(YS, Key, Value, DL);
  259. YS.printError(Entry.getKey(), "unknown rewrite type");
  260. return false;
  261. }
  262. bool RewriteMapParser::
  263. parseRewriteFunctionDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
  264. yaml::MappingNode *Descriptor,
  265. RewriteDescriptorList *DL) {
  266. bool Naked = false;
  267. std::string Source;
  268. std::string Target;
  269. std::string Transform;
  270. for (auto &Field : *Descriptor) {
  271. yaml::ScalarNode *Key;
  272. yaml::ScalarNode *Value;
  273. SmallString<32> KeyStorage;
  274. SmallString<32> ValueStorage;
  275. StringRef KeyValue;
  276. Key = dyn_cast<yaml::ScalarNode>(Field.getKey());
  277. if (!Key) {
  278. YS.printError(Field.getKey(), "descriptor key must be a scalar");
  279. return false;
  280. }
  281. Value = dyn_cast<yaml::ScalarNode>(Field.getValue());
  282. if (!Value) {
  283. YS.printError(Field.getValue(), "descriptor value must be a scalar");
  284. return false;
  285. }
  286. KeyValue = Key->getValue(KeyStorage);
  287. if (KeyValue.equals("source")) {
  288. std::string Error;
  289. Source = Value->getValue(ValueStorage);
  290. if (!Regex(Source).isValid(Error)) {
  291. YS.printError(Field.getKey(), "invalid regex: " + Error);
  292. return false;
  293. }
  294. } else if (KeyValue.equals("target")) {
  295. Target = Value->getValue(ValueStorage);
  296. } else if (KeyValue.equals("transform")) {
  297. Transform = Value->getValue(ValueStorage);
  298. } else if (KeyValue.equals("naked")) {
  299. std::string Undecorated;
  300. Undecorated = Value->getValue(ValueStorage);
  301. Naked = StringRef(Undecorated).lower() == "true" || Undecorated == "1";
  302. } else {
  303. YS.printError(Field.getKey(), "unknown key for function");
  304. return false;
  305. }
  306. }
  307. if (Transform.empty() == Target.empty()) {
  308. YS.printError(Descriptor,
  309. "exactly one of transform or target must be specified");
  310. return false;
  311. }
  312. // TODO see if there is a more elegant solution to selecting the rewrite
  313. // descriptor type
  314. if (!Target.empty())
  315. DL->push_back(new ExplicitRewriteFunctionDescriptor(Source, Target, Naked));
  316. else
  317. DL->push_back(new PatternRewriteFunctionDescriptor(Source, Transform));
  318. return true;
  319. }
  320. bool RewriteMapParser::
  321. parseRewriteGlobalVariableDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
  322. yaml::MappingNode *Descriptor,
  323. RewriteDescriptorList *DL) {
  324. std::string Source;
  325. std::string Target;
  326. std::string Transform;
  327. for (auto &Field : *Descriptor) {
  328. yaml::ScalarNode *Key;
  329. yaml::ScalarNode *Value;
  330. SmallString<32> KeyStorage;
  331. SmallString<32> ValueStorage;
  332. StringRef KeyValue;
  333. Key = dyn_cast<yaml::ScalarNode>(Field.getKey());
  334. if (!Key) {
  335. YS.printError(Field.getKey(), "descriptor Key must be a scalar");
  336. return false;
  337. }
  338. Value = dyn_cast<yaml::ScalarNode>(Field.getValue());
  339. if (!Value) {
  340. YS.printError(Field.getValue(), "descriptor value must be a scalar");
  341. return false;
  342. }
  343. KeyValue = Key->getValue(KeyStorage);
  344. if (KeyValue.equals("source")) {
  345. std::string Error;
  346. Source = Value->getValue(ValueStorage);
  347. if (!Regex(Source).isValid(Error)) {
  348. YS.printError(Field.getKey(), "invalid regex: " + Error);
  349. return false;
  350. }
  351. } else if (KeyValue.equals("target")) {
  352. Target = Value->getValue(ValueStorage);
  353. } else if (KeyValue.equals("transform")) {
  354. Transform = Value->getValue(ValueStorage);
  355. } else {
  356. YS.printError(Field.getKey(), "unknown Key for Global Variable");
  357. return false;
  358. }
  359. }
  360. if (Transform.empty() == Target.empty()) {
  361. YS.printError(Descriptor,
  362. "exactly one of transform or target must be specified");
  363. return false;
  364. }
  365. if (!Target.empty())
  366. DL->push_back(new ExplicitRewriteGlobalVariableDescriptor(Source, Target,
  367. /*Naked*/false));
  368. else
  369. DL->push_back(new PatternRewriteGlobalVariableDescriptor(Source,
  370. Transform));
  371. return true;
  372. }
  373. bool RewriteMapParser::
  374. parseRewriteGlobalAliasDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
  375. yaml::MappingNode *Descriptor,
  376. RewriteDescriptorList *DL) {
  377. std::string Source;
  378. std::string Target;
  379. std::string Transform;
  380. for (auto &Field : *Descriptor) {
  381. yaml::ScalarNode *Key;
  382. yaml::ScalarNode *Value;
  383. SmallString<32> KeyStorage;
  384. SmallString<32> ValueStorage;
  385. StringRef KeyValue;
  386. Key = dyn_cast<yaml::ScalarNode>(Field.getKey());
  387. if (!Key) {
  388. YS.printError(Field.getKey(), "descriptor key must be a scalar");
  389. return false;
  390. }
  391. Value = dyn_cast<yaml::ScalarNode>(Field.getValue());
  392. if (!Value) {
  393. YS.printError(Field.getValue(), "descriptor value must be a scalar");
  394. return false;
  395. }
  396. KeyValue = Key->getValue(KeyStorage);
  397. if (KeyValue.equals("source")) {
  398. std::string Error;
  399. Source = Value->getValue(ValueStorage);
  400. if (!Regex(Source).isValid(Error)) {
  401. YS.printError(Field.getKey(), "invalid regex: " + Error);
  402. return false;
  403. }
  404. } else if (KeyValue.equals("target")) {
  405. Target = Value->getValue(ValueStorage);
  406. } else if (KeyValue.equals("transform")) {
  407. Transform = Value->getValue(ValueStorage);
  408. } else {
  409. YS.printError(Field.getKey(), "unknown key for Global Alias");
  410. return false;
  411. }
  412. }
  413. if (Transform.empty() == Target.empty()) {
  414. YS.printError(Descriptor,
  415. "exactly one of transform or target must be specified");
  416. return false;
  417. }
  418. if (!Target.empty())
  419. DL->push_back(new ExplicitRewriteNamedAliasDescriptor(Source, Target,
  420. /*Naked*/false));
  421. else
  422. DL->push_back(new PatternRewriteNamedAliasDescriptor(Source, Transform));
  423. return true;
  424. }
  425. namespace {
  426. class RewriteSymbols : public ModulePass {
  427. public:
  428. static char ID; // Pass identification, replacement for typeid
  429. RewriteSymbols();
  430. RewriteSymbols(SymbolRewriter::RewriteDescriptorList &DL);
  431. bool runOnModule(Module &M) override;
  432. private:
  433. void loadAndParseMapFiles();
  434. SymbolRewriter::RewriteDescriptorList Descriptors;
  435. };
  436. char RewriteSymbols::ID = 0;
  437. RewriteSymbols::RewriteSymbols() : ModulePass(ID) {
  438. initializeRewriteSymbolsPass(*PassRegistry::getPassRegistry());
  439. loadAndParseMapFiles();
  440. }
  441. RewriteSymbols::RewriteSymbols(SymbolRewriter::RewriteDescriptorList &DL)
  442. : ModulePass(ID) {
  443. Descriptors.splice(Descriptors.begin(), DL);
  444. }
  445. bool RewriteSymbols::runOnModule(Module &M) {
  446. bool Changed;
  447. Changed = false;
  448. for (auto &Descriptor : Descriptors)
  449. Changed |= Descriptor.performOnModule(M);
  450. return Changed;
  451. }
  452. void RewriteSymbols::loadAndParseMapFiles() {
  453. const std::vector<std::string> MapFiles; // HLSL Change - do not init from a global RewriteMapFiles
  454. SymbolRewriter::RewriteMapParser parser;
  455. for (const auto &MapFile : MapFiles)
  456. parser.parse(MapFile, &Descriptors);
  457. }
  458. }
  459. INITIALIZE_PASS(RewriteSymbols, "rewrite-symbols", "Rewrite Symbols", false,
  460. false)
  461. ModulePass *llvm::createRewriteSymbolsPass() { return new RewriteSymbols(); }
  462. ModulePass *
  463. llvm::createRewriteSymbolsPass(SymbolRewriter::RewriteDescriptorList &DL) {
  464. return new RewriteSymbols(DL);
  465. }