diff options
author | David Gross <dgross@google.com> | 2017-03-20 15:23:26 -0700 |
---|---|---|
committer | David Gross <dgross@google.com> | 2017-03-22 17:41:41 +0000 |
commit | cf5afcb229806de1b62dee5a7cc0c7ba6a69f482 (patch) | |
tree | 1ead7a95a014196b037e11d2ee69a0ae8ea59c9a /lib/Source.cpp | |
parent | c26dd0180bf7e40d7b120fa112e944ee7cc049ca (diff) | |
download | libbcc-cf5afcb229806de1b62dee5a7cc0c7ba6a69f482.tar.gz |
Make bitcode wrapper info available to MetadataExtractor.
Copy bitcode wrapper information (compiler version, optimization
level) from bitcode wrapper to Module metadata, where it can be found
by MetadataExtractor instantiated from Module. Previously, such a
MetadataExtractor ignored the information in the bitcode wrapper;
instead, it
- nonsensically set compiler version to RS_VERSION, which is an API
level rather than a slang version; and
- set optimization level to 3.
Remove unused MetadataExtractor::getTargetAPI():
- Old behavior:
- For user bitcode, got actual targetAPI.
- For libclcore, got 0.
- For linked bitcode (user bitcode + libclcore), got
uninitialized/garbage value.
- It's not clear what a sensible value would be for libclcore or for
linked bitcode (or perhaps it should be an error to query the
targetAPI in those cases). What a sensible value would be depends
on how we intend to use it.
This change enables a forthcoming libbcc change that needs to know the
slang version.
Bug: http://b/29154200
Bug: http://b/28070272
Test: full_fugu-eng
Tried ( modified slang, modified bcc) and
(unmodified slang, modified bcc):
- libbcc/tests/run_lit_tests.sh; RsTest; cts -m RenderscriptTest
- confirmed that our driver is loaded rather than libPVRRS.so
- By instrumenting modified bcc and running RsTest, confirmed
that:
- MetadataExtractor properly obtains slang version from Module.
"Modified slang" is a forthcoming slang change that changes
the slang version.
Change-Id: Ifc9c1348c96a88bf7ddb344721f67f89359a4fe6
Diffstat (limited to 'lib/Source.cpp')
-rw-r--r-- | lib/Source.cpp | 75 |
1 files changed, 55 insertions, 20 deletions
diff --git a/lib/Source.cpp b/lib/Source.cpp index 3ec08e4..d99eb10 100644 --- a/lib/Source.cpp +++ b/lib/Source.cpp @@ -22,6 +22,7 @@ #include <new> #include <llvm/ADT/STLExtras.h> +#include <llvm/ADT/StringExtras.h> #include <llvm/Bitcode/ReaderWriter.h> #include <llvm/IR/LLVMContext.h> #include <llvm/IR/Module.h> @@ -30,6 +31,8 @@ #include <llvm/Support/MemoryBuffer.h> #include "llvm/Support/raw_ostream.h" +#include "Assert.h" +#include "bcinfo/BitcodeWrapper.h" #include "bcinfo/MetadataExtractor.h" #include "BCCContextImpl.h" @@ -52,10 +55,45 @@ static inline std::unique_ptr<llvm::Module> helper_load_bitcode(llvm::LLVMContex return std::move(moduleOrError.get()); } +static void helper_get_module_metadata_from_bitcode_wrapper( + uint32_t *compilerVersion, uint32_t *optimizationLevel, + const bcinfo::BitcodeWrapper &wrapper) { + *compilerVersion = wrapper.getCompilerVersion(); + *optimizationLevel = wrapper.getOptimizationLevel(); +} + +static void helper_set_module_metadata_from_bitcode_wrapper(llvm::Module &module, + const uint32_t compilerVersion, + const uint32_t optimizationLevel) { + llvm::LLVMContext &llvmContext = module.getContext(); + + llvm::NamedMDNode *const wrapperMDNode = + module.getOrInsertNamedMetadata(bcinfo::MetadataExtractor::kWrapperMetadataName); + bccAssert(wrapperMDNode->getNumOperands() == 0); // expect to have just now created this node + + llvm::SmallVector<llvm::Metadata *, 2> wrapperInfo = { + llvm::MDString::get(llvmContext, llvm::utostr(compilerVersion)), + llvm::MDString::get(llvmContext, llvm::utostr(optimizationLevel)) + }; + + wrapperMDNode->addOperand(llvm::MDTuple::get(llvmContext, wrapperInfo)); +} + } // end anonymous namespace namespace bcc { +unsigned Source::getCompilerVersion() const { + return bcinfo::MetadataExtractor(&getModule()).getCompilerVersion(); +} + +void Source::getWrapperInformation(unsigned *compilerVersion, + unsigned *optimizationLevel) const { + const bcinfo::MetadataExtractor &me = bcinfo::MetadataExtractor(&getModule()); + *compilerVersion = me.getCompilerVersion(); + *optimizationLevel = me.getOptimizationLevel(); +} + void Source::setModule(llvm::Module *pModule) { if (!mNoDelete && (mModule != pModule)) delete mModule; mModule = pModule; @@ -84,7 +122,12 @@ Source *Source::CreateFromBuffer(BCCContext &pContext, return nullptr; } - Source *result = CreateFromModule(pContext, pName, *module, /* pNoDelete */false); + uint32_t compilerVersion, optimizationLevel; + helper_get_module_metadata_from_bitcode_wrapper(&compilerVersion, &optimizationLevel, + bcinfo::BitcodeWrapper(pBitcode, pBitcodeSize)); + Source *result = CreateFromModule(pContext, pName, *module, + compilerVersion, optimizationLevel, + /* pNoDelete */false); if (result == nullptr) { delete module; } @@ -103,6 +146,11 @@ Source *Source::CreateFromFile(BCCContext &pContext, const std::string &pPath) { } std::unique_ptr<llvm::MemoryBuffer> input_data = std::move(mb_or_error.get()); + uint32_t compilerVersion, optimizationLevel; + helper_get_module_metadata_from_bitcode_wrapper(&compilerVersion, &optimizationLevel, + bcinfo::BitcodeWrapper(input_data->getBufferStart(), + input_data->getBufferSize())); + std::unique_ptr<llvm::MemoryBuffer> input_memory(input_data.release()); auto managedModule = helper_load_bitcode(pContext.mImpl->mLLVMContext, std::move(input_memory)); @@ -114,7 +162,9 @@ Source *Source::CreateFromFile(BCCContext &pContext, const std::string &pPath) { return nullptr; } - Source *result = CreateFromModule(pContext, pPath.c_str(), *module, /* pNoDelete */false); + Source *result = CreateFromModule(pContext, pPath.c_str(), *module, + compilerVersion, optimizationLevel, + /* pNoDelete */false); if (result == nullptr) { delete module; } @@ -123,6 +173,8 @@ Source *Source::CreateFromFile(BCCContext &pContext, const std::string &pPath) { } Source *Source::CreateFromModule(BCCContext &pContext, const char* name, llvm::Module &pModule, + const uint32_t compilerVersion, + const uint32_t optimizationLevel, bool pNoDelete) { std::string ErrorInfo; llvm::raw_string_ostream ErrorStream(ErrorInfo); @@ -138,6 +190,7 @@ Source *Source::CreateFromModule(BCCContext &pContext, const char* name, llvm::M ALOGE("Out of memory during Source object allocation for `%s'!", pModule.getModuleIdentifier().c_str()); } + helper_set_module_metadata_from_bitcode_wrapper(pModule, compilerVersion, optimizationLevel); return result; } @@ -168,24 +221,6 @@ bool Source::merge(Source &pSource) { return true; } -Source *Source::CreateEmpty(BCCContext &pContext, const std::string &pName) { - // Create an empty module - llvm::Module *module = - new (std::nothrow) llvm::Module(pName, pContext.mImpl->mLLVMContext); - - if (module == nullptr) { - ALOGE("Out of memory when creating empty LLVM module `%s'!", pName.c_str()); - return nullptr; - } - - Source *result = CreateFromModule(pContext, pName.c_str(), *module, /* pNoDelete */false); - if (result == nullptr) { - delete module; - } - - return result; -} - const std::string &Source::getIdentifier() const { return mModule->getModuleIdentifier(); } |