Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(123)

Side by Side Diff: src/PNaClTranslator.cpp

Issue 876083007: Subzero: Emit functions and global initializers in a separate thread. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: More code review changes Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/PNaClTranslator.h ('k') | src/assembler.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 //===- subzero/src/PNaClTranslator.cpp - ICE from bitcode -----------------===// 1 //===- subzero/src/PNaClTranslator.cpp - ICE from bitcode -----------------===//
2 // 2 //
3 // The Subzero Code Generator 3 // The Subzero Code Generator
4 // 4 //
5 // This file is distributed under the University of Illinois Open Source 5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 // 9 //
10 // This file implements the PNaCl bitcode file to Ice, to machine code 10 // This file implements the PNaCl bitcode file to Ice, to machine code
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 TopLevelParser(const TopLevelParser &) = delete; 156 TopLevelParser(const TopLevelParser &) = delete;
157 TopLevelParser &operator=(const TopLevelParser &) = delete; 157 TopLevelParser &operator=(const TopLevelParser &) = delete;
158 158
159 public: 159 public:
160 typedef std::vector<Ice::FunctionDeclaration *> FunctionDeclarationListType; 160 typedef std::vector<Ice::FunctionDeclaration *> FunctionDeclarationListType;
161 161
162 TopLevelParser(Ice::Translator &Translator, NaClBitcodeHeader &Header, 162 TopLevelParser(Ice::Translator &Translator, NaClBitcodeHeader &Header,
163 NaClBitstreamCursor &Cursor, Ice::ErrorCode &ErrorStatus) 163 NaClBitstreamCursor &Cursor, Ice::ErrorCode &ErrorStatus)
164 : NaClBitcodeParser(Cursor), Translator(Translator), Header(Header), 164 : NaClBitcodeParser(Cursor), Translator(Translator), Header(Header),
165 ErrorStatus(ErrorStatus), NumErrors(0), NextDefiningFunctionID(0), 165 ErrorStatus(ErrorStatus), NumErrors(0), NextDefiningFunctionID(0),
166 VariableDeclarations(new Ice::VariableDeclarationList),
166 BlockParser(nullptr), StubbedConstCallValue(nullptr) {} 167 BlockParser(nullptr), StubbedConstCallValue(nullptr) {}
167 168
168 ~TopLevelParser() override {} 169 ~TopLevelParser() override {}
169 170
170 Ice::Translator &getTranslator() { return Translator; } 171 Ice::Translator &getTranslator() { return Translator; }
171 172
172 void setBlockParser(BlockParserBaseClass *NewBlockParser) { 173 void setBlockParser(BlockParserBaseClass *NewBlockParser) {
173 BlockParser = NewBlockParser; 174 BlockParser = NewBlockParser;
174 } 175 }
175 176
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 } 259 }
259 260
260 /// Returns the corresponding constant associated with a global declaration. 261 /// Returns the corresponding constant associated with a global declaration.
261 /// (i.e. relocatable). 262 /// (i.e. relocatable).
262 Ice::Constant *getOrCreateGlobalConstantByID(unsigned ID) { 263 Ice::Constant *getOrCreateGlobalConstantByID(unsigned ID) {
263 // TODO(kschimpf): Can this be built when creating global initializers? 264 // TODO(kschimpf): Can this be built when creating global initializers?
264 Ice::Constant *C; 265 Ice::Constant *C;
265 if (ID >= ValueIDConstants.size()) { 266 if (ID >= ValueIDConstants.size()) {
266 C = nullptr; 267 C = nullptr;
267 unsigned ExpectedSize = 268 unsigned ExpectedSize =
268 FunctionDeclarationList.size() + VariableDeclarations.size(); 269 FunctionDeclarationList.size() + VariableDeclarations->size();
269 if (ID >= ExpectedSize) 270 if (ID >= ExpectedSize)
270 ExpectedSize = ID; 271 ExpectedSize = ID;
271 ValueIDConstants.resize(ExpectedSize); 272 ValueIDConstants.resize(ExpectedSize);
272 } else { 273 } else {
273 C = ValueIDConstants[ID]; 274 C = ValueIDConstants[ID];
274 } 275 }
275 if (C != nullptr) 276 if (C != nullptr)
276 return C; 277 return C;
277 278
278 if (isIRGenerationDisabled()) { 279 if (isIRGenerationDisabled()) {
279 ValueIDConstants[ID] = nullptr; 280 ValueIDConstants[ID] = nullptr;
280 return nullptr; 281 return nullptr;
281 } 282 }
282 283
283 // If reached, no such constant exists, create one. 284 // If reached, no such constant exists, create one.
284 // TODO(kschimpf) Don't get addresses of intrinsic function declarations. 285 // TODO(kschimpf) Don't get addresses of intrinsic function declarations.
285 Ice::GlobalDeclaration *Decl = nullptr; 286 Ice::GlobalDeclaration *Decl = nullptr;
286 unsigned FcnIDSize = FunctionDeclarationList.size(); 287 unsigned FcnIDSize = FunctionDeclarationList.size();
287 bool IsUndefined = false; 288 bool IsUndefined = false;
288 if (ID < FcnIDSize) { 289 if (ID < FcnIDSize) {
289 Decl = FunctionDeclarationList[ID]; 290 Decl = FunctionDeclarationList[ID];
290 const auto Func = llvm::cast<Ice::FunctionDeclaration>(Decl); 291 const auto Func = llvm::cast<Ice::FunctionDeclaration>(Decl);
291 IsUndefined = Func->isProto(); 292 IsUndefined = Func->isProto();
292 } else if ((ID - FcnIDSize) < VariableDeclarations.size()) { 293 } else if ((ID - FcnIDSize) < VariableDeclarations->size()) {
293 Decl = VariableDeclarations[ID - FcnIDSize]; 294 Decl = VariableDeclarations->at(ID - FcnIDSize);
294 const auto Var = llvm::cast<Ice::VariableDeclaration>(Decl); 295 const auto Var = llvm::cast<Ice::VariableDeclaration>(Decl);
295 IsUndefined = !Var->hasInitializer(); 296 IsUndefined = !Var->hasInitializer();
296 } 297 }
297 Ice::IceString Name; 298 Ice::IceString Name;
298 bool SuppressMangling; 299 bool SuppressMangling;
299 if (Decl) { 300 if (Decl) {
300 Name = Decl->getName(); 301 Name = Decl->getName();
301 SuppressMangling = Decl->getSuppressMangling(); 302 SuppressMangling = Decl->getSuppressMangling();
302 } else { 303 } else {
303 std::string Buffer; 304 std::string Buffer;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
336 Error("Unable to find function definition to stub constant calls with"); 337 Error("Unable to find function definition to stub constant calls with");
337 return CallValue; 338 return CallValue;
338 } 339 }
339 340
340 /// Returns the number of function declarations in the bitcode file. 341 /// Returns the number of function declarations in the bitcode file.
341 unsigned getNumFunctionIDs() const { return FunctionDeclarationList.size(); } 342 unsigned getNumFunctionIDs() const { return FunctionDeclarationList.size(); }
342 343
343 /// Returns the number of global declarations (i.e. IDs) defined in 344 /// Returns the number of global declarations (i.e. IDs) defined in
344 /// the bitcode file. 345 /// the bitcode file.
345 unsigned getNumGlobalIDs() const { 346 unsigned getNumGlobalIDs() const {
346 return FunctionDeclarationList.size() + VariableDeclarations.size(); 347 return FunctionDeclarationList.size() + VariableDeclarations->size();
347 } 348 }
348 349
349 /// Creates Count global variable declarations. 350 /// Creates Count global variable declarations.
350 void CreateGlobalVariables(size_t Count) { 351 void CreateGlobalVariables(size_t Count) {
351 assert(VariableDeclarations.empty()); 352 assert(VariableDeclarations->empty());
352 for (size_t i = 0; i < Count; ++i) { 353 for (size_t i = 0; i < Count; ++i) {
353 VariableDeclarations.push_back(Ice::VariableDeclaration::create()); 354 VariableDeclarations->push_back(Ice::VariableDeclaration::create());
354 } 355 }
355 } 356 }
356 357
357 /// Returns the number of global variable declarations in the 358 /// Returns the number of global variable declarations in the
358 /// bitcode file. 359 /// bitcode file.
359 Ice::SizeT getNumGlobalVariables() const { 360 Ice::SizeT getNumGlobalVariables() const {
360 return VariableDeclarations.size(); 361 return VariableDeclarations->size();
361 } 362 }
362 363
363 /// Returns the global variable declaration with the given index. 364 /// Returns the global variable declaration with the given index.
364 Ice::VariableDeclaration *getGlobalVariableByID(unsigned Index) { 365 Ice::VariableDeclaration *getGlobalVariableByID(unsigned Index) {
365 if (Index < VariableDeclarations.size()) 366 if (Index < VariableDeclarations->size())
366 return VariableDeclarations[Index]; 367 return VariableDeclarations->at(Index);
367 return reportGetGlobalVariableByIDError(Index); 368 return reportGetGlobalVariableByIDError(Index);
368 } 369 }
369 370
370 /// Returns the global declaration (variable or function) with the 371 /// Returns the global declaration (variable or function) with the
371 /// given Index. 372 /// given Index.
372 Ice::GlobalDeclaration *getGlobalDeclarationByID(size_t Index) { 373 Ice::GlobalDeclaration *getGlobalDeclarationByID(size_t Index) {
373 size_t NumFunctionIds = FunctionDeclarationList.size(); 374 size_t NumFunctionIds = FunctionDeclarationList.size();
374 if (Index < NumFunctionIds) 375 if (Index < NumFunctionIds)
375 return getFunctionByID(Index); 376 return getFunctionByID(Index);
376 else 377 else
377 return getGlobalVariableByID(Index - NumFunctionIds); 378 return getGlobalVariableByID(Index - NumFunctionIds);
378 } 379 }
379 380
380 /// Returns the list of parsed global variable declarations. 381 /// Returns the list of parsed global variable declarations.
381 const Ice::VariableDeclarationList &getGlobalVariables() { 382 Ice::VariableDeclarationList *getGlobalVariables() {
382 return VariableDeclarations; 383 return VariableDeclarations;
383 } 384 }
384 385
385 private: 386 private:
386 // The translator associated with the parser. 387 // The translator associated with the parser.
387 Ice::Translator &Translator; 388 Ice::Translator &Translator;
388 // The bitcode header. 389 // The bitcode header.
389 NaClBitcodeHeader &Header; 390 NaClBitcodeHeader &Header;
390 // The exit status that should be set to true if an error occurs. 391 // The exit status that should be set to true if an error occurs.
391 Ice::ErrorCode &ErrorStatus; 392 Ice::ErrorCode &ErrorStatus;
392 // The number of errors reported. 393 // The number of errors reported.
393 unsigned NumErrors; 394 unsigned NumErrors;
394 // The types associated with each type ID. 395 // The types associated with each type ID.
395 std::vector<ExtendedType> TypeIDValues; 396 std::vector<ExtendedType> TypeIDValues;
396 // The set of functions (prototype and defined). 397 // The set of functions (prototype and defined).
397 FunctionDeclarationListType FunctionDeclarationList; 398 FunctionDeclarationListType FunctionDeclarationList;
398 // The ID of the next possible defined function ID in 399 // The ID of the next possible defined function ID in
399 // FunctionDeclarationList. FunctionDeclarationList is filled 400 // FunctionDeclarationList. FunctionDeclarationList is filled
400 // first. It's the set of functions (either defined or isproto). Then 401 // first. It's the set of functions (either defined or isproto). Then
401 // function definitions are encountered/parsed and 402 // function definitions are encountered/parsed and
402 // NextDefiningFunctionID is incremented to track the next 403 // NextDefiningFunctionID is incremented to track the next
403 // actually-defined function. 404 // actually-defined function.
404 size_t NextDefiningFunctionID; 405 size_t NextDefiningFunctionID;
405 // The set of global variables. 406 // The set of global variables.
406 Ice::VariableDeclarationList VariableDeclarations; 407 Ice::VariableDeclarationList *VariableDeclarations;
407 // Relocatable constants associated with global declarations. 408 // Relocatable constants associated with global declarations.
408 std::vector<Ice::Constant *> ValueIDConstants; 409 std::vector<Ice::Constant *> ValueIDConstants;
409 // Error recovery value to use when getFuncSigTypeByID fails. 410 // Error recovery value to use when getFuncSigTypeByID fails.
410 Ice::FuncSigType UndefinedFuncSigType; 411 Ice::FuncSigType UndefinedFuncSigType;
411 // The block parser currently being applied. Used for error 412 // The block parser currently being applied. Used for error
412 // reporting. 413 // reporting.
413 BlockParserBaseClass *BlockParser; 414 BlockParserBaseClass *BlockParser;
414 // Value to use to stub constant calls. 415 // Value to use to stub constant calls.
415 Ice::Operand *StubbedConstCallValue; 416 Ice::Operand *StubbedConstCallValue;
416 417
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
488 return FunctionDeclarationList[0]; 489 return FunctionDeclarationList[0];
489 report_fatal_error("Unable to continue"); 490 report_fatal_error("Unable to continue");
490 } 491 }
491 492
492 Ice::VariableDeclaration * 493 Ice::VariableDeclaration *
493 TopLevelParser::reportGetGlobalVariableByIDError(unsigned Index) { 494 TopLevelParser::reportGetGlobalVariableByIDError(unsigned Index) {
494 std::string Buffer; 495 std::string Buffer;
495 raw_string_ostream StrBuf(Buffer); 496 raw_string_ostream StrBuf(Buffer);
496 StrBuf << "Global index " << Index 497 StrBuf << "Global index " << Index
497 << " not allowed. Out of range. Must be less than " 498 << " not allowed. Out of range. Must be less than "
498 << VariableDeclarations.size(); 499 << VariableDeclarations->size();
499 BlockError(StrBuf.str()); 500 BlockError(StrBuf.str());
500 // TODO(kschimpf) Remove error recovery once implementation complete. 501 // TODO(kschimpf) Remove error recovery once implementation complete.
501 if (!VariableDeclarations.empty()) 502 if (!VariableDeclarations->empty())
502 return VariableDeclarations[0]; 503 return VariableDeclarations->at(0);
503 report_fatal_error("Unable to continue"); 504 report_fatal_error("Unable to continue");
504 } 505 }
505 506
506 Ice::Type TopLevelParser::convertToIceTypeError(Type *LLVMTy) { 507 Ice::Type TopLevelParser::convertToIceTypeError(Type *LLVMTy) {
507 std::string Buffer; 508 std::string Buffer;
508 raw_string_ostream StrBuf(Buffer); 509 raw_string_ostream StrBuf(Buffer);
509 StrBuf << "Invalid LLVM type: " << *LLVMTy; 510 StrBuf << "Invalid LLVM type: " << *LLVMTy;
510 Error(StrBuf.str()); 511 Error(StrBuf.str());
511 return Ice::IceType_void; 512 return Ice::IceType_void;
512 } 513 }
(...skipping 579 matching lines...) Expand 10 before | Expand all | Expand 10 after
1092 const Ice::TimerStackIdT StackID = Ice::GlobalContext::TSK_Funcs; 1093 const Ice::TimerStackIdT StackID = Ice::GlobalContext::TSK_Funcs;
1093 Ice::TimerIdT TimerID = 0; 1094 Ice::TimerIdT TimerID = 0;
1094 const bool TimeThisFunction = ALLOW_DUMP && getFlags().TimeEachFunction; 1095 const bool TimeThisFunction = ALLOW_DUMP && getFlags().TimeEachFunction;
1095 if (TimeThisFunction) { 1096 if (TimeThisFunction) {
1096 TimerID = getTranslator().getContext()->getTimerID(StackID, 1097 TimerID = getTranslator().getContext()->getTimerID(StackID,
1097 FuncDecl->getName()); 1098 FuncDecl->getName());
1098 getTranslator().getContext()->pushTimer(TimerID, StackID); 1099 getTranslator().getContext()->pushTimer(TimerID, StackID);
1099 } 1100 }
1100 1101
1101 if (!isIRGenerationDisabled()) 1102 if (!isIRGenerationDisabled())
1102 Func = Ice::Cfg::create(getTranslator().getContext()); 1103 Func = Ice::Cfg::create(getTranslator().getContext(),
1104 getTranslator().getNextSequenceNumber());
1103 Ice::Cfg::setCurrentCfg(Func.get()); 1105 Ice::Cfg::setCurrentCfg(Func.get());
1104 1106
1105 // TODO(kschimpf) Clean up API to add a function signature to 1107 // TODO(kschimpf) Clean up API to add a function signature to
1106 // a CFG. 1108 // a CFG.
1107 const Ice::FuncSigType &Signature = FuncDecl->getSignature(); 1109 const Ice::FuncSigType &Signature = FuncDecl->getSignature();
1108 if (isIRGenerationDisabled()) { 1110 if (isIRGenerationDisabled()) {
1109 CurrentNode = nullptr; 1111 CurrentNode = nullptr;
1110 for (Ice::Type ArgType : Signature.getArgList()) { 1112 for (Ice::Type ArgType : Signature.getArgList()) {
1111 (void)ArgType; 1113 (void)ArgType;
1112 setNextLocalInstIndex(nullptr); 1114 setNextLocalInstIndex(nullptr);
(...skipping 14 matching lines...) Expand all
1127 // the translator function. This is because translation may be 1129 // the translator function. This is because translation may be
1128 // done asynchronously in a separate thread. 1130 // done asynchronously in a separate thread.
1129 if (TimeThisFunction) 1131 if (TimeThisFunction)
1130 getTranslator().getContext()->popTimer(TimerID, StackID); 1132 getTranslator().getContext()->popTimer(TimerID, StackID);
1131 1133
1132 Ice::Cfg::setCurrentCfg(nullptr); 1134 Ice::Cfg::setCurrentCfg(nullptr);
1133 // Note: Once any errors have been found, we turn off all 1135 // Note: Once any errors have been found, we turn off all
1134 // translation of all remaining functions. This allows successive 1136 // translation of all remaining functions. This allows successive
1135 // parsing errors to be reported, without adding extra checks to 1137 // parsing errors to be reported, without adding extra checks to
1136 // the translator for such parsing errors. 1138 // the translator for such parsing errors.
1137 if (Context->getNumErrors() == 0) { 1139 if (Context->getNumErrors() == 0 && Func) {
1138 getTranslator().translateFcn(std::move(Func)); 1140 getTranslator().translateFcn(std::move(Func));
1139 // The translator now has ownership of Func. 1141 // The translator now has ownership of Func.
1140 } else { 1142 } else {
1141 Func.reset(); 1143 Func.reset();
1142 } 1144 }
1143 1145
1144 return ParserResult; 1146 return ParserResult;
1145 } 1147 }
1146 1148
1147 ~FunctionParser() final {} 1149 ~FunctionParser() final {}
(...skipping 1655 matching lines...) Expand 10 before | Expand all | Expand 10 after
2803 // True if we have already installed names for unnamed global declarations, 2805 // True if we have already installed names for unnamed global declarations,
2804 // and have generated global constant initializers. 2806 // and have generated global constant initializers.
2805 bool GlobalDeclarationNamesAndInitializersInstalled; 2807 bool GlobalDeclarationNamesAndInitializersInstalled;
2806 2808
2807 // Generates names for unnamed global addresses (i.e. functions and 2809 // Generates names for unnamed global addresses (i.e. functions and
2808 // global variables). Then lowers global variable declaration 2810 // global variables). Then lowers global variable declaration
2809 // initializers to the target. May be called multiple times. Only 2811 // initializers to the target. May be called multiple times. Only
2810 // the first call will do the installation. 2812 // the first call will do the installation.
2811 void InstallGlobalNamesAndGlobalVarInitializers() { 2813 void InstallGlobalNamesAndGlobalVarInitializers() {
2812 if (!GlobalDeclarationNamesAndInitializersInstalled) { 2814 if (!GlobalDeclarationNamesAndInitializersInstalled) {
2815 Ice::VariableDeclarationList *VariableDeclarations =
2816 Context->getGlobalVariables();
2813 Ice::Translator &Trans = getTranslator(); 2817 Ice::Translator &Trans = getTranslator();
2814 const Ice::IceString &GlobalPrefix = getFlags().DefaultGlobalPrefix; 2818 const Ice::IceString &GlobalPrefix = getFlags().DefaultGlobalPrefix;
2815 if (!GlobalPrefix.empty()) { 2819 if (!GlobalPrefix.empty()) {
2816 uint32_t NameIndex = 0; 2820 uint32_t NameIndex = 0;
2817 for (Ice::VariableDeclaration *Var : Context->getGlobalVariables()) { 2821 for (Ice::VariableDeclaration *Var : *VariableDeclarations) {
2818 installDeclarationName(Trans, Var, GlobalPrefix, "global", NameIndex); 2822 installDeclarationName(Trans, Var, GlobalPrefix, "global", NameIndex);
2819 } 2823 }
2820 } 2824 }
2821 const Ice::IceString &FunctionPrefix = getFlags().DefaultFunctionPrefix; 2825 const Ice::IceString &FunctionPrefix = getFlags().DefaultFunctionPrefix;
2822 if (!FunctionPrefix.empty()) { 2826 if (!FunctionPrefix.empty()) {
2823 uint32_t NameIndex = 0; 2827 uint32_t NameIndex = 0;
2824 for (Ice::FunctionDeclaration *Func : 2828 for (Ice::FunctionDeclaration *Func :
2825 Context->getFunctionDeclarationList()) { 2829 Context->getFunctionDeclarationList()) {
2826 installDeclarationName(Trans, Func, FunctionPrefix, "function", 2830 installDeclarationName(Trans, Func, FunctionPrefix, "function",
2827 NameIndex); 2831 NameIndex);
2828 } 2832 }
2829 } 2833 }
2830 getTranslator().lowerGlobals(Context->getGlobalVariables()); 2834 getTranslator().lowerGlobals(VariableDeclarations);
2831 GlobalDeclarationNamesAndInitializersInstalled = true; 2835 GlobalDeclarationNamesAndInitializersInstalled = true;
2832 } 2836 }
2833 } 2837 }
2834 2838
2835 void installDeclarationName(Ice::Translator &Trans, 2839 void installDeclarationName(Ice::Translator &Trans,
2836 Ice::GlobalDeclaration *Decl, 2840 Ice::GlobalDeclaration *Decl,
2837 const Ice::IceString &Prefix, const char *Context, 2841 const Ice::IceString &Prefix, const char *Context,
2838 uint32_t &NameIndex) { 2842 uint32_t &NameIndex) {
2839 if (!Decl->hasName()) { 2843 if (!Decl->hasName()) {
2840 Decl->setName(Trans.createUnnamedName(Prefix, NameIndex)); 2844 Decl->setName(Trans.createUnnamedName(Prefix, NameIndex));
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
3021 3025
3022 if (TopLevelBlocks != 1) { 3026 if (TopLevelBlocks != 1) {
3023 errs() << IRFilename 3027 errs() << IRFilename
3024 << ": Contains more than one module. Found: " << TopLevelBlocks 3028 << ": Contains more than one module. Found: " << TopLevelBlocks
3025 << "\n"; 3029 << "\n";
3026 ErrorStatus.assign(EC_Bitcode); 3030 ErrorStatus.assign(EC_Bitcode);
3027 } 3031 }
3028 } 3032 }
3029 3033
3030 } // end of namespace Ice 3034 } // end of namespace Ice
OLDNEW
« no previous file with comments | « src/PNaClTranslator.h ('k') | src/assembler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698