Allow stubbing of called constant addresses using command line argument. When specified (via command line) replaces all called constant addresses with a stubbed call to the first defined function in the bitcode file. This allows testing of subzero without having to fix that downstream code (after parsing) may not handle such addresses. BUG=None R=jvoung@chromium.org Review URL: https://codereview.chromium.org/902713002
diff --git a/src/IceClFlags.h b/src/IceClFlags.h index d2c0243..c5b7d4b 100644 --- a/src/IceClFlags.h +++ b/src/IceClFlags.h
@@ -27,10 +27,10 @@ UseSandboxing(false), PhiEdgeSplit(false), DecorateAsm(false), DumpStats(false), AllowUninitializedGlobals(false), TimeEachFunction(false), DisableIRGeneration(false), - AllowErrorRecovery(false), GenerateUnitTestMessages(false), - NumTranslationThreads(0), DefaultGlobalPrefix(""), - DefaultFunctionPrefix(""), TimingFocusOn(""), VerboseFocusOn(""), - TranslateOnly("") {} + AllowErrorRecovery(false), StubConstantCalls(false), + GenerateUnitTestMessages(false), NumTranslationThreads(0), + DefaultGlobalPrefix(""), DefaultFunctionPrefix(""), TimingFocusOn(""), + VerboseFocusOn(""), TranslateOnly("") {} bool DisableInternal; bool SubzeroTimingEnabled; bool DisableTranslation; @@ -46,6 +46,7 @@ bool TimeEachFunction; bool DisableIRGeneration; bool AllowErrorRecovery; + bool StubConstantCalls; bool GenerateUnitTestMessages; size_t NumTranslationThreads; // 0 means completely sequential IceString DefaultGlobalPrefix;
diff --git a/src/PNaClTranslator.cpp b/src/PNaClTranslator.cpp index 4d83578..ce7e09c 100644 --- a/src/PNaClTranslator.cpp +++ b/src/PNaClTranslator.cpp
@@ -163,7 +163,7 @@ NaClBitstreamCursor &Cursor, Ice::ErrorCode &ErrorStatus) : NaClBitcodeParser(Cursor), Translator(Translator), Header(Header), ErrorStatus(ErrorStatus), NumErrors(0), NextDefiningFunctionID(0), - BlockParser(nullptr) {} + BlockParser(nullptr), StubbedConstCallValue(nullptr) {} ~TopLevelParser() override {} @@ -319,6 +319,24 @@ return C; } + /// Returns a defined function reference to be used in place of + /// called constant addresses. Returns the corresponding operand + /// to replace the calling address with. Reports an error if + /// a stub could not be found, returning the CallValue. + Ice::Operand *getStubbedConstCallValue(Ice::Operand *CallValue) { + if (StubbedConstCallValue) + return StubbedConstCallValue; + for (unsigned i = 0; i < getNumFunctionIDs(); ++i) { + Ice::FunctionDeclaration *Func = getFunctionByID(i); + if (!Func->isProto()) { + StubbedConstCallValue = getOrCreateGlobalConstantByID(i); + return StubbedConstCallValue; + } + } + Error("Unable to find function definition to stub constant calls with"); + return CallValue; + } + /// Returns the number of function declarations in the bitcode file. unsigned getNumFunctionIDs() const { return FunctionDeclarationList.size(); } @@ -393,6 +411,8 @@ // The block parser currently being applied. Used for error // reporting. BlockParserBaseClass *BlockParser; + // Value to use to stub constant calls. + Ice::Operand *StubbedConstCallValue; bool ParseBlock(unsigned BlockID) override; @@ -2441,6 +2461,10 @@ } } } else { + if (getFlags().StubConstantCalls && + llvm::isa<Ice::ConstantInteger32>(Callee)) { + Callee = Context->getStubbedConstCallValue(Callee); + } ReturnType = Context->getSimpleTypeByID(Values[2]); }
diff --git a/src/llvm2ice.cpp b/src/llvm2ice.cpp index a98c75d..2f31765 100644 --- a/src/llvm2ice.cpp +++ b/src/llvm2ice.cpp
@@ -172,6 +172,12 @@ cl::desc("Allow error recovery when reading PNaCl bitcode."), cl::init(false)); +// TODO(kschimpf) Remove once the emitter handles these cases. +static cl::opt<bool> + StubConstantCalls("stub-const-calls", + cl::desc("Stub indirect calls to constants."), + cl::init(false)); + static cl::opt<bool> LLVMVerboseErrors( "verbose-llvm-parse-errors", cl::desc("Print out more descriptive PNaCl bitcode parse errors when " @@ -313,6 +319,7 @@ Flags.TranslateOnly = TranslateOnly; Flags.DisableIRGeneration = DisableIRGeneration; Flags.AllowErrorRecovery = AllowErrorRecovery; + Flags.StubConstantCalls = StubConstantCalls; // Force -build-on-read=0 for .ll files. const std::string LLSuffix = ".ll";