1 /*
2 * Copyright 2010-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
17 #include "bcc/Script.h"
18
19 #include "Assert.h"
20 #include "Log.h"
21
22 #include "bcc/CompilerConfig.h"
23 #include "bcc/Source.h"
24
25 #include "bcinfo/MetadataExtractor.h"
26
27 #include <llvm/IR/Module.h>
28
29 using namespace bcc;
30
Script(Source * pSource)31 Script::Script(Source *pSource)
32 : mSource(pSource),
33 mOptimizationLevel(llvm::CodeGenOpt::Aggressive),
34 mLinkRuntimeCallback(nullptr), mEmbedInfo(false), mEmbedGlobalInfo(false),
35 mEmbedGlobalInfoSkipConstant(false) {}
36
LinkRuntime(const char * core_lib)37 bool Script::LinkRuntime(const char *core_lib) {
38 bccAssert(core_lib != nullptr);
39
40 // Using the same context with the source.
41 BCCContext &context = mSource->getContext();
42
43 Source *libclcore_source = Source::CreateFromFile(context, core_lib);
44 if (libclcore_source == nullptr) {
45 ALOGE("Failed to load Renderscript library '%s' to link!", core_lib);
46 return false;
47 }
48
49 if (mLinkRuntimeCallback != nullptr) {
50 mLinkRuntimeCallback(this, &mSource->getModule(),
51 &libclcore_source->getModule());
52 }
53
54 // For every named metadata node in the source (libclcore_source),
55 // the merge process ensures there is a same-named metadata node in
56 // the destination (mSource) (creating it if necessary) and appends
57 // all of the source node's operands to the end of the destination
58 // node's operands. In the case of the wrapper metadata
59 //
60 // kWrapperMetadataName -> (compilerVersion, optimizationLevel)
61 //
62 // this is not the behavior we want. Instead, we want to retain the
63 // source wrapper metadata:
64 // - compiler version in libclcore_source is 0, a nonsensical value.
65 // As documented in slang_version.h, libclcore_source must not
66 // violate any compiler version guarantees, so the right thing to
67 // do is retain the compiler version from source, which specifies
68 // which guarantees source (and hence the merged code) satisfies.
69 // See frameworks/rs/driver/README.txt regarding libclcore_source
70 // obeying compiler version guarantees.
71 // - optimization level in source and libclcore_source is meaningful.
72 // We simply define the optimization level in the linked code to
73 // be the optimization level of source.
74 // The easiest way to retain the source wrapper metadata is to delete
75 // the libclcore_source wrapper metadata.
76 llvm::Module &libclcore_module = libclcore_source->getModule();
77 llvm::NamedMDNode *const wrapperMDNode =
78 libclcore_module.getNamedMetadata(bcinfo::MetadataExtractor::kWrapperMetadataName);
79 bccAssert(wrapperMDNode != nullptr);
80 libclcore_module.eraseNamedMetadata(wrapperMDNode);
81 if (!mSource->merge(*libclcore_source)) {
82 ALOGE("Failed to link Renderscript library '%s'!", core_lib);
83 delete libclcore_source;
84 return false;
85 }
86
87 return true;
88 }
89
mergeSource(Source & pSource)90 bool Script::mergeSource(Source &pSource) { return mSource->merge(pSource); }
91