llvm.org GIT mirror llvm / 54de287
Revert "Use StringRef in LTOModule implementation (NFC)" This reverts commit r282997, a windows bot is asserting in one test apparently. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@283456 91177308-0d34-0410-b5e6-96231b3b80d8 Mehdi Amini 3 years ago
4 changed file(s) with 68 addition(s) and 69 deletion(s). Raw diff Collapse all Expand all
3636 struct LTOModule {
3737 private:
3838 struct NameAndAttributes {
39 StringRef name;
39 const char *name;
4040 uint32_t attributes;
4141 bool isFunction;
4242 const GlobalValue *symbol;
5353 // _defines and _undefines only needed to disambiguate tentative definitions
5454 StringSet<> _defines;
5555 StringMap _undefines;
56 std::vector<StringRef> _asm_undefines;
56 std::vector<const char*> _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(StringRef path);
65 static bool isBitcodeFile(const char *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, StringRef path,
93 createFromFile(LLVMContext &Context, const char *path,
9494 const TargetOptions &options);
9595 static ErrorOr>
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,
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,
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 StringRef getSymbolName(uint32_t index) {
142 const char *getSymbolName(uint32_t index) {
143143 if (index < _symbols.size())
144144 return _symbols[index].name;
145 return StringRef();
145 return nullptr;
146146 }
147147
148148 const GlobalValue *getSymbolGV(uint32_t index) {
151151 return nullptr;
152152 }
153153
154 StringRef getLinkerOpts() { return LinkerOpts; }
155
156 const std::vector &getAsmUndefinedRefs() { return _asm_undefines; }
154 const char *getLinkerOpts() {
155 return LinkerOpts.c_str();
156 }
157
158 const std::vector &getAsmUndefinedRefs() {
159 return _asm_undefines;
160 }
157161
158162 private:
159163 /// Parse metadata from the module
169173 bool isFunc);
170174
171175 /// Add a defined symbol to the list.
172 void addDefinedSymbol(StringRef Name, const GlobalValue *def,
176 void addDefinedSymbol(const char *Name, const GlobalValue *def,
173177 bool isFunction);
174178
175179 /// Add a data symbol as defined to the list.
176180 void addDefinedDataSymbol(const object::BasicSymbolRef &Sym);
177 void addDefinedDataSymbol(StringRef Name, const GlobalValue *v);
181 void addDefinedDataSymbol(const char*Name, const GlobalValue *v);
178182
179183 /// Add a function symbol as defined to the list.
180184 void addDefinedFunctionSymbol(const object::BasicSymbolRef &Sym);
181 void addDefinedFunctionSymbol(StringRef Name, const Function *F);
185 void addDefinedFunctionSymbol(const char *Name, const Function *F);
182186
183187 /// Add a global symbol from module-level ASM to the defined list.
184 void addAsmGlobalSymbol(StringRef, lto_symbol_attributes scope);
188 void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope);
185189
186190 /// Add a global symbol from module-level ASM to the undefined list.
187 void addAsmGlobalSymbolUndef(StringRef);
191 void addAsmGlobalSymbolUndef(const char *);
188192
189193 /// Parse i386/ppc ObjC class data structure.
190194 void addObjCClass(const GlobalVariable *clgv);
130130 }
131131
132132 void LTOCodeGenerator::setAsmUndefinedRefs(LTOModule *Mod) {
133 const std::vector<StringRef> &undefs = Mod->getAsmUndefinedRefs();
133 const std::vector<const char *> &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(StringRef Path) {
64 bool LTOModule::isBitcodeFile(const char *Path) {
6565 ErrorOr> BufferOrErr =
6666 MemoryBuffer::getFile(Path);
6767 if (!BufferOrErr)
105105 }
106106
107107 ErrorOr>
108 LTOModule::createFromFile(LLVMContext &Context, StringRef path,
108 LTOModule::createFromFile(LLVMContext &Context, const char *path,
109109 const TargetOptions &options) {
110110 ErrorOr> BufferOrErr =
111111 MemoryBuffer::getFile(path);
119119 }
120120
121121 ErrorOr>
122 LTOModule::createFromOpenFile(LLVMContext &Context, int fd, StringRef path,
122 LTOModule::createFromOpenFile(LLVMContext &Context, int fd, const char *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, StringRef path,
129 size_t map_size, off_t offset,
130 const TargetOptions &options) {
128 LTOModule::createFromOpenFileSlice(LLVMContext &Context, int fd,
129 const char *path, size_t map_size,
130 off_t offset, 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();
282 info.name = IterBool.first->first().data();
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();
295 info.name = Iter->first().data();
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();
321 info.name = IterBool.first->first().data();
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();
340 info.name = IterBool.first->first().data();
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();
352351 }
353352
354353 const GlobalValue *V = IRFile->getSymbolGV(Sym.getRawDataRefImpl());
355 addDefinedDataSymbol(Buffer, V);
356 }
357
358 void LTOModule::addDefinedDataSymbol(StringRef Name, const GlobalValue *v) {
354 addDefinedDataSymbol(Buffer.c_str(), V);
355 }
356
357 void LTOModule::addDefinedDataSymbol(const char *Name, const GlobalValue *v) {
359358 // Add to list of defined symbols.
360359 addDefinedSymbol(Name, v, false);
361360
410409 {
411410 raw_svector_ostream OS(Buffer);
412411 Sym.printName(OS);
413 Buffer.c_str();
414412 }
415413
416414 const Function *F =
417415 cast(IRFile->getSymbolGV(Sym.getRawDataRefImpl()));
418 addDefinedFunctionSymbol(Buffer, F);
419 }
420
421 void LTOModule::addDefinedFunctionSymbol(StringRef Name, const Function *F) {
416 addDefinedFunctionSymbol(Buffer.c_str(), F);
417 }
418
419 void LTOModule::addDefinedFunctionSymbol(const char *Name, const Function *F) {
422420 // add to list of defined symbols
423421 addDefinedSymbol(Name, F, true);
424422 }
425423
426 void LTOModule::addDefinedSymbol(StringRef Name, const GlobalValue *def,
424 void LTOModule::addDefinedSymbol(const char *Name, const GlobalValue *def,
427425 bool isFunction) {
428426 // set alignment part log2() can have rounding errors
429427 uint32_t align = def->getAlignment();
472470 // fill information structure
473471 NameAndAttributes info;
474472 StringRef NameRef = Iter->first();
475 info.name = NameRef;
476 assert(NameRef.data()[NameRef.size()] == '\0');
473 info.name = NameRef.data();
474 assert(info.name[NameRef.size()] == '\0');
477475 info.attributes = attr;
478476 info.isFunction = isFunction;
479477 info.symbol = def;
484482
485483 /// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
486484 /// defined list.
487 void LTOModule::addAsmGlobalSymbol(StringRef name,
485 void LTOModule::addAsmGlobalSymbol(const char *name,
488486 lto_symbol_attributes scope) {
489487 auto IterBool = _defines.insert(name);
490488
492490 if (!IterBool.second)
493491 return;
494492
495 NameAndAttributes &info = _undefines[IterBool.first->first()];
493 NameAndAttributes &info = _undefines[IterBool.first->first().data()];
496494
497495 if (info.symbol == nullptr) {
498496 // FIXME: This is trying to take care of module ASM like this:
504502 // much.
505503
506504 // fill information structure
507 info.name = IterBool.first->first();
505 info.name = IterBool.first->first().data();
508506 info.attributes =
509507 LTO_SYMBOL_PERMISSIONS_DATA | LTO_SYMBOL_DEFINITION_REGULAR | scope;
510508 info.isFunction = false;
526524
527525 /// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the
528526 /// undefined list.
529 void LTOModule::addAsmGlobalSymbolUndef(StringRef name) {
527 void LTOModule::addAsmGlobalSymbolUndef(const char *name) {
530528 auto IterBool = _undefines.insert(std::make_pair(name, NameAndAttributes()));
531529
532 _asm_undefines.push_back(IterBool.first->first());
530 _asm_undefines.push_back(IterBool.first->first().data());
533531
534532 // we already have the symbol
535533 if (!IterBool.second)
538536 uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;
539537 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
540538 NameAndAttributes &info = IterBool.first->second;
541 info.name = IterBool.first->first();
539 info.name = IterBool.first->first().data();
542540 info.attributes = attr;
543541 info.isFunction = false;
544542 info.symbol = nullptr;
551549 {
552550 raw_svector_ostream OS(name);
553551 Sym.printName(OS);
554 name.c_str();
555552 }
556553
557554 auto IterBool = _undefines.insert(std::make_pair(name, NameAndAttributes()));
562559
563560 NameAndAttributes &info = IterBool.first->second;
564561
565 info.name = IterBool.first->first();
562 info.name = IterBool.first->first().data();
566563
567564 const GlobalValue *decl = IRFile->getSymbolGV(Sym.getRawDataRefImpl());
568565
589586 {
590587 raw_svector_ostream OS(Buffer);
591588 Sym.printName(OS);
592 Buffer.c_str();
593589 }
594 StringRef Name(Buffer);
590 const char *Name = Buffer.c_str();
595591
596592 if (IsUndefined)
597593 addAsmGlobalSymbolUndef(Name);
169169 }
170170
171171 bool lto_module_is_object_file(const char* path) {
172 return LTOModule::isBitcodeFile(StringRef(path));
172 return LTOModule::isBitcodeFile(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(),
181 StringRef(target_triplet_prefix));
180 return LTOModule::isBitcodeForTarget(Buffer->get(), target_triplet_prefix);
182181 }
183182
184183 bool lto_module_has_objc_category(const void *mem, size_t length) {
200199 std::unique_ptr buffer(LTOModule::makeBuffer(mem, length));
201200 if (!buffer)
202201 return false;
203 return LTOModule::isBitcodeForTarget(buffer.get(),
204 StringRef(target_triplet_prefix));
202 return LTOModule::isBitcodeForTarget(buffer.get(), target_triplet_prefix);
205203 }
206204
207205 lto_module_t lto_module_create(const char* path) {
208206 lto_initialize();
209207 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
210208 ErrorOr> M =
211 LTOModule::createFromFile(*LTOContext, StringRef(path), Options);
209 LTOModule::createFromFile(*LTOContext, path, Options);
212210 if (!M)
213211 return nullptr;
214212 return wrap(M->release());
217215 lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
218216 lto_initialize();
219217 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
220 ErrorOr> M = LTOModule::createFromOpenFile(
221 *LTOContext, fd, StringRef(path), size, Options);
218 ErrorOr> M =
219 LTOModule::createFromOpenFile(*LTOContext, fd, path, size, Options);
222220 if (!M)
223221 return nullptr;
224222 return wrap(M->release());
231229 lto_initialize();
232230 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
233231 ErrorOr> M = LTOModule::createFromOpenFileSlice(
234 *LTOContext, fd, StringRef(path), map_size, offset, Options);
232 *LTOContext, fd, path, map_size, offset, Options);
235233 if (!M)
236234 return nullptr;
237235 return wrap(M->release());
252250 const char *path) {
253251 lto_initialize();
254252 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
255 ErrorOr> M = LTOModule::createFromBuffer(
256 *LTOContext, mem, length, Options, StringRef(path));
253 ErrorOr> M =
254 LTOModule::createFromBuffer(*LTOContext, mem, length, Options, path);
257255 if (!M)
258256 return nullptr;
259257 return wrap(M->release());
268266 std::unique_ptr Context = llvm::make_unique();
269267 Context->setDiagnosticHandler(diagnosticHandler, nullptr, true);
270268
271 ErrorOr> M = LTOModule::createInLocalContext(
272 std::move(Context), mem, length, Options, StringRef(path));
269 ErrorOr> M =
270 LTOModule::createInLocalContext(std::move(Context), mem, length, Options,
271 path);
273272 if (!M)
274273 return nullptr;
275274 return wrap(M->release());
282281 lto_initialize();
283282 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
284283 ErrorOr> M = LTOModule::createFromBuffer(
285 unwrap(cg)->getContext(), mem, length, Options, StringRef(path));
284 unwrap(cg)->getContext(), mem, length, Options, path);
286285 return wrap(M->release());
287286 }
288287
293292 }
294293
295294 void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
296 return unwrap(mod)->setTargetTriple(StringRef(triple));
295 return unwrap(mod)->setTargetTriple(triple);
297296 }
298297
299298 unsigned int lto_module_get_num_symbols(lto_module_t mod) {
301300 }
302301
303302 const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
304 return unwrap(mod)->getSymbolName(index).data();
303 return unwrap(mod)->getSymbolName(index);
305304 }
306305
307306 lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
310309 }
311310
312311 const char* lto_module_get_linkeropts(lto_module_t mod) {
313 return unwrap(mod)->getLinkerOpts().data();
312 return unwrap(mod)->getLinkerOpts();
314313 }
315314
316315 void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,