|
| 1 | +//===------ GlobalMergeFunctions.h - Global merge functions -----*- C++ -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +/// |
| 9 | +/// This file defines global merge functions pass and related data structure. |
| 10 | +/// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#ifndef PIKA_TRANSFORMS_UTILS_GLOBALMERGEFUNCTIONS_H |
| 14 | +#define PIKA_TRANSFORMS_UTILS_GLOBALMERGEFUNCTIONS_H |
| 15 | + |
| 16 | +#include "llvm/ADT/DenseMap.h" |
| 17 | +#include "llvm/ADT/StableHashing.h" |
| 18 | +#include "llvm/ADT/StringRef.h" |
| 19 | +#include "llvm/CGData/StableFunctionMap.h" |
| 20 | +#include "llvm/IR/Instructions.h" |
| 21 | +#include "llvm/IR/Module.h" |
| 22 | +#include "llvm/Pass.h" |
| 23 | +#include <map> |
| 24 | +#include <mutex> |
| 25 | + |
| 26 | +enum class HashFunctionMode { |
| 27 | + Local, |
| 28 | + BuildingHashFuncion, |
| 29 | + UsingHashFunction, |
| 30 | +}; |
| 31 | + |
| 32 | +namespace llvm { |
| 33 | + |
| 34 | +// A vector of locations (the pair of (instruction, operand) indices) reachable |
| 35 | +// from a parameter. |
| 36 | +using ParamLocs = SmallVector<IndexPair, 4>; |
| 37 | +// A vector of parameters |
| 38 | +using ParamLocsVecTy = SmallVector<ParamLocs, 8>; |
| 39 | +// A map of stable hash to a vector of stable functions |
| 40 | + |
| 41 | +/// GlobalMergeFunc finds functions which only differ by constants in |
| 42 | +/// certain instructions, e.g. resulting from specialized functions of layout |
| 43 | +/// compatible types. |
| 44 | +/// Unlike PikaMergeFunc that directly compares IRs, this uses stable function |
| 45 | +/// hash to find the merge candidate. Similar to the global outliner, we can run |
| 46 | +/// codegen twice to collect function merge candidate in the first round, and |
| 47 | +/// merge functions globally in the second round. |
| 48 | +class GlobalMergeFunc : public ModulePass { |
| 49 | + HashFunctionMode MergerMode = HashFunctionMode::Local; |
| 50 | + |
| 51 | + std::unique_ptr<StableFunctionMap> LocalFunctionMap; |
| 52 | + |
| 53 | +public: |
| 54 | + static char ID; |
| 55 | + |
| 56 | + GlobalMergeFunc(); |
| 57 | + |
| 58 | + void initializeMergerMode(const Module &M); |
| 59 | + |
| 60 | + bool runOnModule(Module &M) override; |
| 61 | + |
| 62 | + /// Analyze module to create stable function into LocalFunctionMap. |
| 63 | + void analyze(Module &M); |
| 64 | + |
| 65 | + /// Emit LocalFunctionMap into __llvm_merge section. |
| 66 | + void emitFunctionMap(Module &M); |
| 67 | + |
| 68 | + /// Merge functions in the module using the global function map. |
| 69 | + bool merge(Module &M, const StableFunctionMap *FunctionMap); |
| 70 | +}; |
| 71 | + |
| 72 | +} // end namespace llvm |
| 73 | +#endif // PIKA_TRANSFORMS_UTILS_GLOBALMERGEFUNCTIONS_H |
0 commit comments