BitcodeTranslator.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright 2011-2012, The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "bcinfo/BitcodeTranslator.h"
  17. #include "bcinfo/BitcodeWrapper.h"
  18. #include "BitReader_2_7/BitReader_2_7.h"
  19. #include "BitReader_3_0/BitReader_3_0.h"
  20. #include "BitWriter_3_2/ReaderWriter_3_2.h"
  21. #include "StripUnkAttr/strip_unknown_attributes.h"
  22. #define LOG_TAG "bcinfo"
  23. #include <log/log.h>
  24. #include "llvm/Bitcode/BitstreamWriter.h"
  25. #include "llvm/Bitcode/ReaderWriter.h"
  26. #include "llvm/IR/LLVMContext.h"
  27. #include "llvm/IR/Module.h"
  28. #include "llvm/Support/MemoryBuffer.h"
  29. #include "llvm/Support/raw_ostream.h"
  30. #include <cstdlib>
  31. #include <climits>
  32. namespace bcinfo {
  33. /**
  34. * Define minimum and maximum target API versions. These correspond to the
  35. * same API levels used by the standard Android SDK.
  36. *
  37. * LLVM 2.7
  38. * 11 - Honeycomb
  39. * 12 - Honeycomb MR1
  40. * 13 - Honeycomb MR2
  41. *
  42. * LLVM 3.0
  43. * 14 - Ice Cream Sandwich
  44. * 15 - Ice Cream Sandwich MR1
  45. *
  46. * LLVM 3.1
  47. * 16 - Ice Cream Sandwich MR2
  48. */
  49. static const unsigned int kMinimumAPIVersion = 11;
  50. static const unsigned int kMaximumAPIVersion = RS_VERSION;
  51. static const unsigned int kCurrentAPIVersion = 10000;
  52. static const unsigned int kDevelopmentAPIVersion = UINT_MAX;
  53. /**
  54. * The minimum version which does not require translation (i.e. is already
  55. * compatible with LLVM's default bitcode reader).
  56. */
  57. static const unsigned int kMinimumUntranslatedVersion = 16;
  58. static const unsigned int kMinimumCompatibleVersion_LLVM_3_0 = 14;
  59. static const unsigned int kMinimumCompatibleVersion_LLVM_2_7 = 11;
  60. static void stripUnknownAttributes(llvm::Module *M) {
  61. for (llvm::Function &F : *M)
  62. slang::stripUnknownAttributes(F);
  63. }
  64. BitcodeTranslator::BitcodeTranslator(const char *bitcode, size_t bitcodeSize,
  65. unsigned int version)
  66. : mBitcode(bitcode), mBitcodeSize(bitcodeSize), mTranslatedBitcode(nullptr),
  67. mTranslatedBitcodeSize(0), mVersion(version) {
  68. return;
  69. }
  70. BitcodeTranslator::~BitcodeTranslator() {
  71. if (mVersion < kMinimumUntranslatedVersion) {
  72. // We didn't actually do a translation in the alternate case, so deleting
  73. // the bitcode would be improper.
  74. delete [] mTranslatedBitcode;
  75. }
  76. mTranslatedBitcode = nullptr;
  77. return;
  78. }
  79. bool BitcodeTranslator::translate() {
  80. if (!mBitcode || !mBitcodeSize) {
  81. ALOGE("Invalid/empty bitcode");
  82. return false;
  83. }
  84. BitcodeWrapper BCWrapper(mBitcode, mBitcodeSize);
  85. if (BCWrapper.getTargetAPI() != mVersion) {
  86. ALOGE("Bitcode wrapper (%u) and translator (%u) disagree about target API",
  87. BCWrapper.getTargetAPI(), mVersion);
  88. }
  89. if ((mVersion != kDevelopmentAPIVersion) &&
  90. (mVersion != kCurrentAPIVersion) &&
  91. ((mVersion < kMinimumAPIVersion) ||
  92. (mVersion > kMaximumAPIVersion))) {
  93. ALOGE("Invalid API version: %u is out of range ('%u' - '%u')", mVersion,
  94. kMinimumAPIVersion, kMaximumAPIVersion);
  95. return false;
  96. }
  97. // We currently don't need to transcode any API version higher than 14 or
  98. // the current API version (i.e. 10000)
  99. if (mVersion >= kMinimumUntranslatedVersion) {
  100. mTranslatedBitcode = mBitcode;
  101. mTranslatedBitcodeSize = mBitcodeSize;
  102. return true;
  103. }
  104. // Do the actual transcoding by invoking a 2.7-era bitcode reader that can
  105. // then write the bitcode back out in a more modern (acceptable) version.
  106. std::unique_ptr<llvm::LLVMContext> mContext(new llvm::LLVMContext());
  107. std::unique_ptr<llvm::MemoryBuffer> MEM(
  108. llvm::MemoryBuffer::getMemBuffer(
  109. llvm::StringRef(mBitcode, mBitcodeSize), "", false));
  110. std::string error;
  111. llvm::ErrorOr<llvm::MemoryBufferRef> MBOrErr = MEM->getMemBufferRef();
  112. llvm::ErrorOr<llvm::Module *> MOrErr(nullptr);
  113. if (mVersion >= kMinimumCompatibleVersion_LLVM_3_0) {
  114. MOrErr = llvm_3_0::parseBitcodeFile(*MBOrErr, *mContext);
  115. } else if (mVersion >= kMinimumCompatibleVersion_LLVM_2_7) {
  116. MOrErr = llvm_2_7::parseBitcodeFile(*MBOrErr, *mContext);
  117. } else {
  118. ALOGE("No compatible bitcode reader for API version %d", mVersion);
  119. return false;
  120. }
  121. if (std::error_code EC = MOrErr.getError()) {
  122. ALOGE("Could not parse bitcode file");
  123. ALOGE("%s", EC.message().c_str());
  124. return false;
  125. }
  126. // Module ownership is handled by the context, so we don't need to free it.
  127. llvm::Module *module = MOrErr.get();
  128. stripUnknownAttributes(module);
  129. std::string Buffer;
  130. llvm::raw_string_ostream OS(Buffer);
  131. // Use the LLVM 3.2 bitcode writer, instead of the top-of-tree version.
  132. llvm_3_2::WriteBitcodeToFile(module, OS);
  133. OS.flush();
  134. AndroidBitcodeWrapper wrapper;
  135. size_t actualWrapperLen = writeAndroidBitcodeWrapper(
  136. &wrapper, Buffer.size(), kMinimumUntranslatedVersion,
  137. BCWrapper.getCompilerVersion(), BCWrapper.getOptimizationLevel());
  138. if (!actualWrapperLen) {
  139. ALOGE("Couldn't produce bitcode wrapper!");
  140. return false;
  141. }
  142. mTranslatedBitcodeSize = actualWrapperLen + Buffer.size();
  143. char *c = new char[mTranslatedBitcodeSize];
  144. memcpy(c, &wrapper, actualWrapperLen);
  145. memcpy(c + actualWrapperLen, Buffer.c_str(), Buffer.size());
  146. mTranslatedBitcode = c;
  147. return true;
  148. }
  149. } // namespace bcinfo