aboutsummaryrefslogtreecommitdiff
path: root/lib/Source.cpp
blob: 63c4a5a181743566d7e4fec96536444fda9105cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
 * Copyright 2012, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "bcc/Source.h"

#include "Log.h"
#include "bcc/BCCContext.h"

#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>
#include <llvm/IR/Verifier.h>
#include <llvm/Linker/Linker.h>
#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"

namespace {

// Helper function to load the bitcode. This uses "bitcode lazy load" feature to
// reduce the startup time. On success, return the LLVM module object created
// and take the ownership of input memory buffer (i.e., pInput). On error,
// return nullptr and will NOT take the ownership of pInput.
static inline std::unique_ptr<llvm::Module> helper_load_bitcode(llvm::LLVMContext &pContext,
                                                std::unique_ptr<llvm::MemoryBuffer> &&pInput) {
  auto bufferId = pInput->getBufferIdentifier();
  llvm::ErrorOr<std::unique_ptr<llvm::Module> > moduleOrError
      = llvm::getLazyBitcodeModule(std::move(pInput), pContext);
  if (std::error_code ec = moduleOrError.getError()) {
    ALOGE("Unable to parse the given bitcode file `%s'! (%s)",
          bufferId, ec.message().c_str());
  }

  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;
}

Source *Source::CreateFromBuffer(BCCContext &pContext,
                                 const char *pName,
                                 const char *pBitcode,
                                 size_t pBitcodeSize) {
  llvm::StringRef input_data(pBitcode, pBitcodeSize);
  std::unique_ptr<llvm::MemoryBuffer> input_memory =
      llvm::MemoryBuffer::getMemBuffer(input_data, "", false);

  if (input_memory == nullptr) {
    ALOGE("Unable to load bitcode `%s' from buffer!", pName);
    return nullptr;
  }

  auto managedModule = helper_load_bitcode(pContext.mImpl->mLLVMContext,
                                           std::move(input_memory));

  // Release the managed llvm::Module* since this object gets deleted either in
  // the error check below or in ~Source() (since pNoDelete is false).
  llvm::Module *module = managedModule.release();
  if (module == nullptr) {
    return nullptr;
  }

  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;
  }

  return result;
}

Source *Source::CreateFromFile(BCCContext &pContext, const std::string &pPath) {

  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> mb_or_error =
      llvm::MemoryBuffer::getFile(pPath);
  if (mb_or_error.getError()) {
    ALOGE("Failed to load bitcode from path %s! (%s)", pPath.c_str(),
          mb_or_error.getError().message().c_str());
    return nullptr;
  }
  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));

  // Release the managed llvm::Module* since this object gets deleted either in
  // the error check below or in ~Source() (since pNoDelete is false).
  llvm::Module *module = managedModule.release();
  if (module == nullptr) {
    return nullptr;
  }

  Source *result = CreateFromModule(pContext, pPath.c_str(), *module,
                                    compilerVersion, optimizationLevel,
                                    /* pNoDelete */false);
  if (result == nullptr) {
    delete module;
  }

  return result;
}

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);
  pModule.materializeAll();
  if (llvm::verifyModule(pModule, &ErrorStream)) {
    ALOGE("Bitcode of RenderScript module does not pass verification: `%s'!",
          ErrorStream.str().c_str());
    return nullptr;
  }

  Source *result = new (std::nothrow) Source(name, pContext, pModule, pNoDelete);
  if (result == nullptr) {
    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;
}

Source::Source(const char* name, BCCContext &pContext, llvm::Module &pModule,
               bool pNoDelete)
    : mName(name), mContext(pContext), mModule(&pModule), mMetadata(nullptr),
      mNoDelete(pNoDelete), mIsModuleDestroyed(false) {
    pContext.addSource(*this);
}

Source::~Source() {
  mContext.removeSource(*this);
  if (!mNoDelete && !mIsModuleDestroyed)
    delete mModule;
  delete mMetadata;
}

bool Source::merge(Source &pSource) {
  // TODO(srhines): Add back logging of actual diagnostics from linking.
  if (llvm::Linker::linkModules(*mModule, std::unique_ptr<llvm::Module>(&pSource.getModule())) != 0) {
    ALOGE("Failed to link source `%s' with `%s'!",
          getIdentifier().c_str(), pSource.getIdentifier().c_str());
    return false;
  }
  // pSource.getModule() is destroyed after linking.
  pSource.markModuleDestroyed();

  return true;
}

const std::string &Source::getIdentifier() const {
  return mModule->getModuleIdentifier();
}

void Source::addBuildChecksumMetadata(const char *buildChecksum) const {
    llvm::LLVMContext &context = mContext.mImpl->mLLVMContext;
    llvm::MDString *val = llvm::MDString::get(context, buildChecksum);
    llvm::NamedMDNode *node =
        mModule->getOrInsertNamedMetadata("#rs_build_checksum");
    node->addOperand(llvm::MDNode::get(context, val));
}

bool Source::getDebugInfoEnabled() const {
  return mModule->getNamedMetadata("llvm.dbg.cu") != nullptr;
}

bool Source::extractMetadata() {
  mMetadata = new bcinfo::MetadataExtractor(mModule);
  return mMetadata->extract();
}

} // namespace bcc