blob: d84012c103eae9c0a270d7ccbede6412f82c3a56 [file] [log] [blame]
Logancf3e5212010-12-29 01:44:55 +08001/*
Zonr Changccc39a82012-04-05 10:44:53 +08002 * Copyright 2010-2012, The Android Open Source Project
Logancf3e5212010-12-29 01:44:55 +08003 *
Zonr Changccc39a82012-04-05 10:44:53 +08004 * 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
Logancf3e5212010-12-29 01:44:55 +08007 *
Zonr Changccc39a82012-04-05 10:44:53 +08008 * http://www.apache.org/licenses/LICENSE-2.0
Logancf3e5212010-12-29 01:44:55 +08009 *
Zonr Changccc39a82012-04-05 10:44:53 +080010 * 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.
Logancf3e5212010-12-29 01:44:55 +080015 */
16
Zonr Changc72c4dd2012-04-12 15:38:53 +080017#include "bcc/Script.h"
Logancf3e5212010-12-29 01:44:55 +080018
Jean-Luc Brouillet0a2acce2017-02-17 13:29:47 -080019#include "Assert.h"
20#include "Log.h"
21
22#include "bcc/CompilerConfig.h"
Zonr Changc72c4dd2012-04-12 15:38:53 +080023#include "bcc/Source.h"
Logan033f46e2011-01-06 05:51:24 +080024
David Grosscf5afcb2017-03-20 15:23:26 -070025#include "bcinfo/MetadataExtractor.h"
26
27#include <llvm/IR/Module.h>
28
Zonr Changccc39a82012-04-05 10:44:53 +080029using namespace bcc;
Stephen Hinesead5ccb2012-05-03 12:30:38 -070030
Jean-Luc Brouillet0a2acce2017-02-17 13:29:47 -080031Script::Script(Source *pSource)
32 : mSource(pSource),
33 mOptimizationLevel(llvm::CodeGenOpt::Aggressive),
34 mLinkRuntimeCallback(nullptr), mEmbedInfo(false), mEmbedGlobalInfo(false),
35 mEmbedGlobalInfoSkipConstant(false) {}
36
37bool 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);
Shih-wei Liaod2a5a0e2012-04-25 03:40:50 -070046 return false;
Stephen Hinesead5ccb2012-05-03 12:30:38 -070047 }
48
Jean-Luc Brouillet0a2acce2017-02-17 13:29:47 -080049 if (mLinkRuntimeCallback != nullptr) {
50 mLinkRuntimeCallback(this, &mSource->getModule(),
51 &libclcore_source->getModule());
Stephen Hinesead5ccb2012-05-03 12:30:38 -070052 }
Jean-Luc Brouillet0a2acce2017-02-17 13:29:47 -080053
David Grosscf5afcb2017-03-20 15:23:26 -070054 // 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.
David Grosse159e8c2017-03-27 11:49:15 -070069 // See frameworks/rs/driver/README.txt regarding libclcore_source
70 // obeying compiler version guarantees.
David Grosscf5afcb2017-03-20 15:23:26 -070071 // - 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);
Jean-Luc Brouillet0a2acce2017-02-17 13:29:47 -080081 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;
Stephen Hinesead5ccb2012-05-03 12:30:38 -070088}
89
Jean-Luc Brouillet0a2acce2017-02-17 13:29:47 -080090bool Script::mergeSource(Source &pSource) { return mSource->merge(pSource); }