llvm.org GIT mirror llvm / bd26e6a
[LTO API] add lto_codegen_set_module to set the destination module. When debugging LTO issues with ld64, we use -save-temps to save the merged optimized bitcode file, then invoke ld64 again on the single bitcode file to speed up debugging code generation passes and ld64 stuff after code generation. llvm linking a single bitcode file via lto_codegen_add_module will generate a different bitcode file from the single input. With the newly-added lto_codegen_set_module, we can make sure the destination module is the same as the input. lto_codegen_set_module will transfer the ownship of the module to code generator. rdar://19024554 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@230290 91177308-0d34-0410-b5e6-96231b3b80d8 Manman Ren 4 years ago
9 changed file(s) with 96 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
6565
6666 // Merge given module, return true on success.
6767 bool addModule(struct LTOModule *);
68
69 // Set the destination module.
70 void setModule(struct LTOModule *);
6871
6972 void setTargetOptions(TargetOptions options);
7073 void setDebugInfo(lto_debug_model);
6969 /// Returns true on error.
7070 bool linkInModule(Module *Src);
7171
72 /// \brief Set the composite to the passed-in module.
73 void setModule(Module *Dst);
74
7275 static bool LinkModules(Module *Dest, Module *Src,
7376 DiagnosticHandlerFunction DiagnosticHandler);
7477
3939 * @{
4040 */
4141
42 #define LTO_API_VERSION 12
42 #define LTO_API_VERSION 13
4343
4444 /**
4545 * \since prior to LTO_API_VERSION=3
395395 lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod);
396396
397397 /**
398 * Sets the object module for code generation. This will transfer the ownship of
399 * the module to code generator.
400 *
401 * \c cg and \c mod must both be in the same context.
402 *
403 * \since prior to LTO_API_VERSION=13
404 */
405 extern void
406 lto_codegen_set_module(lto_code_gen_t cg, lto_module_t mod);
407
408 /**
398409 * Sets if debug info should be generated.
399410 * Returns true on error (check lto_get_error_message() for details).
400411 *
140140 return !ret;
141141 }
142142
143 void LTOCodeGenerator::setModule(LTOModule *Mod) {
144 assert(&Mod->getModule().getContext() == &Context &&
145 "Expected module in same context");
146
147 // Delete the old merged module.
148 if (IRLinker.getModule())
149 IRLinker.deleteModule();
150 AsmUndefinedRefs.clear();
151
152 IRLinker.setModule(&Mod->getModule());
153
154 const std::vector &Undefs = Mod->getAsmUndefinedRefs();
155 for (int I = 0, E = Undefs.size(); I != E; ++I)
156 AsmUndefinedRefs[Undefs[I]] = 1;
157 }
158
143159 void LTOCodeGenerator::setTargetOptions(TargetOptions options) {
144160 Options = options;
145161 }
17471747 return RetCode;
17481748 }
17491749
1750 void Linker::setModule(Module *Dst) {
1751 init(Dst, DiagnosticHandler);
1752 }
1753
17501754 //===----------------------------------------------------------------------===//
17511755 // LinkModules entrypoint.
17521756 //===----------------------------------------------------------------------===//
0 ; RUN: llvm-as < %s >%t1
1 ; RUN: llvm-lto -exported-symbol=_main -set-merged-module -o %t2 %t1
2 ; RUN: llvm-objdump -d %t2 | FileCheck %s
3
4 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
5 target triple = "x86_64-apple-macosx10.10.0"
6
7 ; CHECK: _main
8 ; CHECK: movl $132
9 define i32 @_Z3fooi(i32 %a) {
10 entry:
11 %a.addr = alloca i32, align 4
12 store i32 %a, i32* %a.addr, align 4
13 %0 = load i32* %a.addr, align 4
14 %1 = load i32* %a.addr, align 4
15 %call = call i32 @_Z4bar2i(i32 %1)
16 %add = add nsw i32 %0, %call
17 ret i32 %add
18 }
19
20 define i32 @_Z4bar2i(i32 %a) {
21 entry:
22 %a.addr = alloca i32, align 4
23 store i32 %a, i32* %a.addr, align 4
24 %0 = load i32* %a.addr, align 4
25 %mul = mul nsw i32 2, %0
26 ret i32 %mul
27 }
28
29 define i32 @main() {
30 entry:
31 %retval = alloca i32, align 4
32 store i32 0, i32* %retval
33 %call = call i32 @_Z3fooi(i32 44)
34 ret i32 %call
35 }
6767 static cl::opt ListSymbolsOnly(
6868 "list-symbols-only", cl::init(false),
6969 cl::desc("Instead of running LTO, list the symbols in each IR file"));
70
71 static cl::opt SetMergedModule(
72 "set-merged-module", cl::init(false),
73 cl::desc("Use the first input module as the merged module"));
7074
7175 namespace {
7276 struct ModuleInfo {
193197 return 1;
194198 }
195199
196 if (!CodeGen.addModule(Module.get()))
197 return 1;
198
199 unsigned NumSyms = Module->getSymbolCount();
200 LTOModule *LTOMod = Module.get();
201
202 // We use the first input module as the destination module when
203 // SetMergedModule is true.
204 if (SetMergedModule && i == BaseArg) {
205 // Transfer ownership to the code generator.
206 CodeGen.setModule(Module.release());
207 } else if (!CodeGen.addModule(Module.get()))
208 return 1;
209
210 unsigned NumSyms = LTOMod->getSymbolCount();
200211 for (unsigned I = 0; I < NumSyms; ++I) {
201 StringRef Name = Module->getSymbolName(I);
212 StringRef Name = LTOMod->getSymbolName(I);
202213 if (!DSOSymbolsSet.count(Name))
203214 continue;
204 lto_symbol_attributes Attrs = Module->getSymbolAttributes(I);
215 lto_symbol_attributes Attrs = LTOMod->getSymbolAttributes(I);
205216 unsigned Scope = Attrs & LTO_SYMBOL_SCOPE_MASK;
206217 if (Scope != LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN)
207218 KeptDSOSyms.push_back(Name);
247247 return !unwrap(cg)->addModule(unwrap(mod));
248248 }
249249
250 void lto_codegen_set_module(lto_code_gen_t cg, lto_module_t mod) {
251 unwrap(cg)->setModule(unwrap(mod));
252 }
253
250254 bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
251255 unwrap(cg)->setDebugInfo(debug);
252256 return false;
2424 lto_api_version
2525 lto_codegen_set_diagnostic_handler
2626 lto_codegen_add_module
27 lto_codegen_set_module
2728 lto_codegen_add_must_preserve_symbol
2829 lto_codegen_compile
2930 lto_codegen_create