LLVM 19.0.0git
FunctionImportUtils.h
Go to the documentation of this file.
1//===- FunctionImportUtils.h - Importing support utilities -----*- 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 the FunctionImportGlobalProcessing class which is used
10// to perform the necessary global value handling for function importing.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_TRANSFORMS_UTILS_FUNCTIONIMPORTUTILS_H
15#define LLVM_TRANSFORMS_UTILS_FUNCTIONIMPORTUTILS_H
16
17#include "llvm/ADT/SetVector.h"
19
20namespace llvm {
21class Module;
22
23/// Class to handle necessary GlobalValue changes required by ThinLTO
24/// function importing, including linkage changes and any necessary renaming.
26 /// The Module which we are exporting or importing functions from.
27 Module &M;
28
29 /// Module summary index passed in for function importing/exporting handling.
30 const ModuleSummaryIndex &ImportIndex;
31
32 /// Globals to import from this module, all other functions will be
33 /// imported as declarations instead of definitions.
34 SetVector<GlobalValue *> *GlobalsToImport;
35
36 /// Set to true if the given ModuleSummaryIndex contains any functions
37 /// from this source module, in which case we must conservatively assume
38 /// that any of its functions may be imported into another module
39 /// as part of a different backend compilation process.
40 bool HasExportedFunctions = false;
41
42 /// Set to true (only applicatable to ELF -fpic) if dso_local should be
43 /// dropped for a declaration.
44 ///
45 /// On ELF, the assembler is conservative and assumes a global default
46 /// visibility symbol can be interposable. No direct access relocation is
47 /// allowed, if the definition is not in the translation unit, even if the
48 /// definition is available in the linkage unit. Thus we need to clear
49 /// dso_local to disable direct access.
50 ///
51 /// This flag should not be set for -fno-pic or -fpie, which would
52 /// unnecessarily disable direct access.
53 bool ClearDSOLocalOnDeclarations;
54
55 /// Set of llvm.*used values, in order to validate that we don't try
56 /// to promote any non-renamable values.
58
59 /// Keep track of any COMDATs that require renaming (because COMDAT
60 /// leader was promoted and renamed). Maps from original COMDAT to one
61 /// with new name.
63
64 /// Check if we should promote the given local value to global scope.
65 bool shouldPromoteLocalToGlobal(const GlobalValue *SGV, ValueInfo VI);
66
67#ifndef NDEBUG
68 /// Check if the given value is a local that can't be renamed (promoted).
69 /// Only used in assertion checking, and disabled under NDEBUG since the Used
70 /// set will not be populated.
71 bool isNonRenamableLocal(const GlobalValue &GV) const;
72#endif
73
74 /// Helper methods to check if we are importing from or potentially
75 /// exporting from the current source module.
76 bool isPerformingImport() const { return GlobalsToImport != nullptr; }
77 bool isModuleExporting() const { return HasExportedFunctions; }
78
79 /// If we are importing from the source module, checks if we should
80 /// import SGV as a definition, otherwise import as a declaration.
81 bool doImportAsDefinition(const GlobalValue *SGV);
82
83 /// Get the name for a local SGV that should be promoted and renamed to global
84 /// scope in the linked destination module.
85 std::string getPromotedName(const GlobalValue *SGV);
86
87 /// Process globals so that they can be used in ThinLTO. This includes
88 /// promoting local variables so that they can be reference externally by
89 /// thin lto imported globals and converting strong external globals to
90 /// available_externally.
91 void processGlobalsForThinLTO();
92 void processGlobalForThinLTO(GlobalValue &GV);
93
94 /// Get the new linkage for SGV that should be used in the linked destination
95 /// module. Specifically, for ThinLTO importing or exporting it may need
96 /// to be adjusted. When \p DoPromote is true then we must adjust the
97 /// linkage for a required promotion of a local to global scope.
98 GlobalValue::LinkageTypes getLinkage(const GlobalValue *SGV, bool DoPromote);
99
100public:
102 SetVector<GlobalValue *> *GlobalsToImport,
103 bool ClearDSOLocalOnDeclarations)
104 : M(M), ImportIndex(Index), GlobalsToImport(GlobalsToImport),
105 ClearDSOLocalOnDeclarations(ClearDSOLocalOnDeclarations) {
106 // If we have a ModuleSummaryIndex but no function to import,
107 // then this is the primary module being compiled in a ThinLTO
108 // backend compilation, and we need to see if it has functions that
109 // may be exported to another backend compilation.
110 if (!GlobalsToImport)
111 HasExportedFunctions = ImportIndex.hasExportedFunctions(M);
112
113#ifndef NDEBUG
115 // First collect those in the llvm.used set.
116 collectUsedGlobalVariables(M, Vec, /*CompilerUsed=*/false);
117 // Next collect those in the llvm.compiler.used set.
118 collectUsedGlobalVariables(M, Vec, /*CompilerUsed=*/true);
119 Used = {Vec.begin(), Vec.end()};
120#endif
121 }
122
123 bool run();
124};
125
126/// Perform in-place global value handling on the given Module for
127/// exported local functions renamed and promoted for ThinLTO.
129 Module &M, const ModuleSummaryIndex &Index,
130 bool ClearDSOLocalOnDeclarations,
131 SetVector<GlobalValue *> *GlobalsToImport = nullptr);
132
133} // End llvm namespace
134
135#endif
Machine Check Debug Module
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
This file implements a set that has insertion order iteration characteristics.
Class to handle necessary GlobalValue changes required by ThinLTO function importing,...
FunctionImportGlobalProcessing(Module &M, const ModuleSummaryIndex &Index, SetVector< GlobalValue * > *GlobalsToImport, bool ClearDSOLocalOnDeclarations)
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:51
Class to hold module path string table and global value map, and encapsulate methods for operating on...
bool hasExportedFunctions(const Module &M) const
Check if the given Module has any functions available for exporting in the index.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
A vector that has set insertion semantics.
Definition: SetVector.h:57
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:427
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index, bool ClearDSOLocalOnDeclarations, SetVector< GlobalValue * > *GlobalsToImport=nullptr)
Perform in-place global value handling on the given Module for exported local functions renamed and p...
GlobalVariable * collectUsedGlobalVariables(const Module &M, SmallVectorImpl< GlobalValue * > &Vec, bool CompilerUsed)
Given "llvm.used" or "llvm.compiler.used" as a global name, collect the initializer elements of that ...
Definition: Module.cpp:843
Struct that holds a reference to a particular GUID in a global value summary.