llvm.org GIT mirror llvm / c16b74e
Recommit "Use StringRef in LTOModule implementation (NFC)"" This reverts commit r283456 and reapply r282997, with explicitly zeroing the struct member to workaround a bug in MSVC2013 with zero-initialization: https://connect.microsoft.com/VisualStudio/feedback/details/802160 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@283581 91177308-0d34-0410-b5e6-96231b3b80d8 Mehdi Amini 3 years ago
4 changed file(s) with 72 addition(s) and 71 deletion(s). Raw diff Collapse all Expand all
3636 struct LTOModule {
3737 private:
3838 struct NameAndAttributes {
39 const char *name;
40 uint32_t attributes;
41 bool isFunction;
42 const GlobalValue *symbol;
39 StringRef name;
40 uint32_t attributes = 0;
41 bool isFunction = 0;
42 const GlobalValue *symbol = 0;
4343 };
4444
4545 std::unique_ptr OwnedContext;
5353 // _defines and _undefines only needed to disambiguate tentative definitions
5454 StringSet<> _defines;
5555 StringMap _undefines;
56 std::vector<const char*> _asm_undefines;
56 std::vector<StringRef> _asm_undefines;
5757
5858 LTOModule(std::unique_ptr Obj, TargetMachine *TM);
5959
6262
6363 /// Returns 'true' if the file or memory contents is LLVM bitcode.
6464 static bool isBitcodeFile(const void *mem, size_t length);
65 static bool isBitcodeFile(const char *path);
65 static bool isBitcodeFile(StringRef path);
6666
6767 /// Returns 'true' if the Module is produced for ThinLTO.
6868 bool isThinLTO();
9090 /// InitializeAllAsmPrinters();
9191 /// InitializeAllAsmParsers();
9292 static ErrorOr>
93 createFromFile(LLVMContext &Context, const char *path,
93 createFromFile(LLVMContext &Context, StringRef path,
9494 const TargetOptions &options);
9595 static ErrorOr>
96 createFromOpenFile(LLVMContext &Context, int fd, const char *path,
97 size_t size, const TargetOptions &options);
98 static ErrorOr>
99 createFromOpenFileSlice(LLVMContext &Context, int fd, const char *path,
96 createFromOpenFile(LLVMContext &Context, int fd, StringRef path, size_t size,
97 const TargetOptions &options);
98 static ErrorOr>
99 createFromOpenFileSlice(LLVMContext &Context, int fd, StringRef path,
100100 size_t map_size, off_t offset,
101101 const TargetOptions &options);
102102 static ErrorOr>
139139 }
140140
141141 /// Get the name of the symbol at the specified index.
142 const char *getSymbolName(uint32_t index) {
142 StringRef getSymbolName(uint32_t index) {
143143 if (index < _symbols.size())
144144 return _symbols[index].name;
145 return nullptr;
145 return StringRef();
146146 }
147147
148148 const GlobalValue *getSymbolGV(uint32_t index) {
151151 return nullptr;
152152 }
153153
154 const char *getLinkerOpts() {
155 return LinkerOpts.c_str();
156 }
157
158 const std::vector &getAsmUndefinedRefs() {
159 return _asm_undefines;
160 }
154 StringRef getLinkerOpts() { return LinkerOpts; }
155
156 const std::vector &getAsmUndefinedRefs() { return _asm_undefines; }
161157
162158 private:
163159 /// Parse metadata from the module
173169 bool isFunc);
174170
175171 /// Add a defined symbol to the list.
176 void addDefinedSymbol(const char *Name, const GlobalValue *def,
172 void addDefinedSymbol(StringRef Name, const GlobalValue *def,
177173 bool isFunction);
178174
179175 /// Add a data symbol as defined to the list.
180176 void addDefinedDataSymbol(const object::BasicSymbolRef &Sym);
181 void addDefinedDataSymbol(const char*Name, const GlobalValue *v);
177 void addDefinedDataSymbol(StringRef Name, const GlobalValue *v);
182178
183179 /// Add a function symbol as defined to the list.
184180 void addDefinedFunctionSymbol(const object::BasicSymbolRef &Sym);
185 void addDefinedFunctionSymbol(const char *Name, const Function *F);
181 void addDefinedFunctionSymbol(StringRef Name, const Function *F);
186182
187183 /// Add a global symbol from module-level ASM to the defined list.
188 void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope);
184 void addAsmGlobalSymbol(StringRef, lto_symbol_attributes scope);
189185
190186 /// Add a global symbol from module-level ASM to the undefined list.
191 void addAsmGlobalSymbolUndef(const char *);
187 void addAsmGlobalSymbolUndef(StringRef);
192188
193189 /// Parse i386/ppc ObjC class data structure.
194190 void addObjCClass(const GlobalVariable *clgv);
130130 }
131131
132132 void LTOCodeGenerator::setAsmUndefinedRefs(LTOModule *Mod) {
133 const std::vector<const char *> &undefs = Mod->getAsmUndefinedRefs();
133 const std::vector<StringRef> &undefs = Mod->getAsmUndefinedRefs();
134134 for (int i = 0, e = undefs.size(); i != e; ++i)
135135 AsmUndefinedRefs[undefs[i]] = 1;
136136 }
6161 return bool(BCData);
6262 }
6363
64 bool LTOModule::isBitcodeFile(const char *Path) {
64 bool LTOModule::isBitcodeFile(StringRef Path) {
6565 ErrorOr> BufferOrErr =
6666 MemoryBuffer::getFile(Path);
6767 if (!BufferOrErr)
105105 }
106106
107107 ErrorOr>
108 LTOModule::createFromFile(LLVMContext &Context, const char *path,
108 LTOModule::createFromFile(LLVMContext &Context, StringRef path,
109109 const TargetOptions &options) {
110110 ErrorOr> BufferOrErr =
111111 MemoryBuffer::getFile(path);
119119 }
120120
121121 ErrorOr>
122 LTOModule::createFromOpenFile(LLVMContext &Context, int fd, const char *path,
122 LTOModule::createFromOpenFile(LLVMContext &Context, int fd, StringRef path,
123123 size_t size, const TargetOptions &options) {
124124 return createFromOpenFileSlice(Context, fd, path, size, 0, options);
125125 }
126126
127127 ErrorOr>
128 LTOModule::createFromOpenFileSlice(LLVMContext &Context, int fd,
129 const char *path, size_t map_size,
130 off_t offset, const TargetOptions &options) {
128 LTOModule::createFromOpenFileSlice(LLVMContext &Context, int fd, StringRef path,
129 size_t map_size, off_t offset,
130 const TargetOptions &options) {
131131 ErrorOr> BufferOrErr =
132132 MemoryBuffer::getOpenFileSlice(fd, path, map_size, offset);
133133 if (std::error_code EC = BufferOrErr.getError()) {
279279 _undefines.insert(std::make_pair(superclassName, NameAndAttributes()));
280280 if (IterBool.second) {
281281 NameAndAttributes &info = IterBool.first->second;
282 info.name = IterBool.first->first().data();
282 info.name = IterBool.first->first();
283283 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
284284 info.isFunction = false;
285285 info.symbol = clgv;
292292 auto Iter = _defines.insert(className).first;
293293
294294 NameAndAttributes info;
295 info.name = Iter->first().data();
295 info.name = Iter->first();
296296 info.attributes = LTO_SYMBOL_PERMISSIONS_DATA |
297297 LTO_SYMBOL_DEFINITION_REGULAR | LTO_SYMBOL_SCOPE_DEFAULT;
298298 info.isFunction = false;
318318 return;
319319
320320 NameAndAttributes &info = IterBool.first->second;
321 info.name = IterBool.first->first().data();
321 info.name = IterBool.first->first();
322322 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
323323 info.isFunction = false;
324324 info.symbol = clgv;
337337 return;
338338
339339 NameAndAttributes &info = IterBool.first->second;
340 info.name = IterBool.first->first().data();
340 info.name = IterBool.first->first();
341341 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
342342 info.isFunction = false;
343343 info.symbol = clgv;
348348 {
349349 raw_svector_ostream OS(Buffer);
350350 Sym.printName(OS);
351 Buffer.c_str();
351352 }
352353
353354 const GlobalValue *V = IRFile->getSymbolGV(Sym.getRawDataRefImpl());
354 addDefinedDataSymbol(Buffer.c_str(), V);
355 }
356
357 void LTOModule::addDefinedDataSymbol(const char *Name, const GlobalValue *v) {
355 addDefinedDataSymbol(Buffer, V);
356 }
357
358 void LTOModule::addDefinedDataSymbol(StringRef Name, const GlobalValue *v) {
358359 // Add to list of defined symbols.
359360 addDefinedSymbol(Name, v, false);
360361
409410 {
410411 raw_svector_ostream OS(Buffer);
411412 Sym.printName(OS);
413 Buffer.c_str();
412414 }
413415
414416 const Function *F =
415417 cast(IRFile->getSymbolGV(Sym.getRawDataRefImpl()));
416 addDefinedFunctionSymbol(Buffer.c_str(), F);
417 }
418
419 void LTOModule::addDefinedFunctionSymbol(const char *Name, const Function *F) {
418 addDefinedFunctionSymbol(Buffer, F);
419 }
420
421 void LTOModule::addDefinedFunctionSymbol(StringRef Name, const Function *F) {
420422 // add to list of defined symbols
421423 addDefinedSymbol(Name, F, true);
422424 }
423425
424 void LTOModule::addDefinedSymbol(const char *Name, const GlobalValue *def,
426 void LTOModule::addDefinedSymbol(StringRef Name, const GlobalValue *def,
425427 bool isFunction) {
426428 // set alignment part log2() can have rounding errors
427429 uint32_t align = def->getAlignment();
470472 // fill information structure
471473 NameAndAttributes info;
472474 StringRef NameRef = Iter->first();
473 info.name = NameRef.data();
474 assert(info.name[NameRef.size()] == '\0');
475 info.name = NameRef;
476 assert(NameRef.data()[NameRef.size()] == '\0');
475477 info.attributes = attr;
476478 info.isFunction = isFunction;
477479 info.symbol = def;
482484
483485 /// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
484486 /// defined list.
485 void LTOModule::addAsmGlobalSymbol(const char *name,
487 void LTOModule::addAsmGlobalSymbol(StringRef name,
486488 lto_symbol_attributes scope) {
487489 auto IterBool = _defines.insert(name);
488490
490492 if (!IterBool.second)
491493 return;
492494
493 NameAndAttributes &info = _undefines[IterBool.first->first().data()];
495 NameAndAttributes &info = _undefines[IterBool.first->first()];
494496
495497 if (info.symbol == nullptr) {
496498 // FIXME: This is trying to take care of module ASM like this:
502504 // much.
503505
504506 // fill information structure
505 info.name = IterBool.first->first().data();
507 info.name = IterBool.first->first();
506508 info.attributes =
507509 LTO_SYMBOL_PERMISSIONS_DATA | LTO_SYMBOL_DEFINITION_REGULAR | scope;
508510 info.isFunction = false;
524526
525527 /// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the
526528 /// undefined list.
527 void LTOModule::addAsmGlobalSymbolUndef(const char *name) {
529 void LTOModule::addAsmGlobalSymbolUndef(StringRef name) {
528530 auto IterBool = _undefines.insert(std::make_pair(name, NameAndAttributes()));
529531
530 _asm_undefines.push_back(IterBool.first->first().data());
532 _asm_undefines.push_back(IterBool.first->first());
531533
532534 // we already have the symbol
533535 if (!IterBool.second)
536538 uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;
537539 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
538540 NameAndAttributes &info = IterBool.first->second;
539 info.name = IterBool.first->first().data();
541 info.name = IterBool.first->first();
540542 info.attributes = attr;
541543 info.isFunction = false;
542544 info.symbol = nullptr;
549551 {
550552 raw_svector_ostream OS(name);
551553 Sym.printName(OS);
554 name.c_str();
552555 }
553556
554557 auto IterBool = _undefines.insert(std::make_pair(name, NameAndAttributes()));
559562
560563 NameAndAttributes &info = IterBool.first->second;
561564
562 info.name = IterBool.first->first().data();
565 info.name = IterBool.first->first();
563566
564567 const GlobalValue *decl = IRFile->getSymbolGV(Sym.getRawDataRefImpl());
565568
586589 {
587590 raw_svector_ostream OS(Buffer);
588591 Sym.printName(OS);
592 Buffer.c_str();
589593 }
590 const char *Name = Buffer.c_str();
594 StringRef Name(Buffer);
591595
592596 if (IsUndefined)
593597 addAsmGlobalSymbolUndef(Name);
169169 }
170170
171171 bool lto_module_is_object_file(const char* path) {
172 return LTOModule::isBitcodeFile(path);
172 return LTOModule::isBitcodeFile(StringRef(path));
173173 }
174174
175175 bool lto_module_is_object_file_for_target(const char* path,
177177 ErrorOr> Buffer = MemoryBuffer::getFile(path);
178178 if (!Buffer)
179179 return false;
180 return LTOModule::isBitcodeForTarget(Buffer->get(), target_triplet_prefix);
180 return LTOModule::isBitcodeForTarget(Buffer->get(),
181 StringRef(target_triplet_prefix));
181182 }
182183
183184 bool lto_module_has_objc_category(const void *mem, size_t length) {
199200 std::unique_ptr buffer(LTOModule::makeBuffer(mem, length));
200201 if (!buffer)
201202 return false;
202 return LTOModule::isBitcodeForTarget(buffer.get(), target_triplet_prefix);
203 return LTOModule::isBitcodeForTarget(buffer.get(),
204 StringRef(target_triplet_prefix));
203205 }
204206
205207 lto_module_t lto_module_create(const char* path) {
206208 lto_initialize();
207209 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
208210 ErrorOr> M =
209 LTOModule::createFromFile(*LTOContext, path, Options);
211 LTOModule::createFromFile(*LTOContext, StringRef(path), Options);
210212 if (!M)
211213 return nullptr;
212214 return wrap(M->release());
215217 lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
216218 lto_initialize();
217219 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
218 ErrorOr> M =
219 LTOModule::createFromOpenFile(*LTOContext, fd, path, size, Options);
220 ErrorOr> M = LTOModule::createFromOpenFile(
221 *LTOContext, fd, StringRef(path), size, Options);
220222 if (!M)
221223 return nullptr;
222224 return wrap(M->release());
229231 lto_initialize();
230232 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
231233 ErrorOr> M = LTOModule::createFromOpenFileSlice(
232 *LTOContext, fd, path, map_size, offset, Options);
234 *LTOContext, fd, StringRef(path), map_size, offset, Options);
233235 if (!M)
234236 return nullptr;
235237 return wrap(M->release());
250252 const char *path) {
251253 lto_initialize();
252254 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
253 ErrorOr> M =
254 LTOModule::createFromBuffer(*LTOContext, mem, length, Options, path);
255 ErrorOr> M = LTOModule::createFromBuffer(
256 *LTOContext, mem, length, Options, StringRef(path));
255257 if (!M)
256258 return nullptr;
257259 return wrap(M->release());
266268 std::unique_ptr Context = llvm::make_unique();
267269 Context->setDiagnosticHandler(diagnosticHandler, nullptr, true);
268270
269 ErrorOr> M =
270 LTOModule::createInLocalContext(std::move(Context), mem, length, Options,
271 path);
271 ErrorOr> M = LTOModule::createInLocalContext(
272 std::move(Context), mem, length, Options, StringRef(path));
272273 if (!M)
273274 return nullptr;
274275 return wrap(M->release());
281282 lto_initialize();
282283 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
283284 ErrorOr> M = LTOModule::createFromBuffer(
284 unwrap(cg)->getContext(), mem, length, Options, path);
285 unwrap(cg)->getContext(), mem, length, Options, StringRef(path));
285286 return wrap(M->release());
286287 }
287288
292293 }
293294
294295 void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
295 return unwrap(mod)->setTargetTriple(triple);
296 return unwrap(mod)->setTargetTriple(StringRef(triple));
296297 }
297298
298299 unsigned int lto_module_get_num_symbols(lto_module_t mod) {
300301 }
301302
302303 const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
303 return unwrap(mod)->getSymbolName(index);
304 return unwrap(mod)->getSymbolName(index).data();
304305 }
305306
306307 lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
309310 }
310311
311312 const char* lto_module_get_linkeropts(lto_module_t mod) {
312 return unwrap(mod)->getLinkerOpts();
313 return unwrap(mod)->getLinkerOpts().data();
313314 }
314315
315316 void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,