Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 1 | /* |
Zonr Chang | ccc39a8 | 2012-04-05 10:44:53 +0800 | [diff] [blame] | 2 | * Copyright 2010-2012, The Android Open Source Project |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 3 | * |
Zonr Chang | ccc39a8 | 2012-04-05 10:44:53 +0800 | [diff] [blame] | 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 |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 7 | * |
Zonr Chang | ccc39a8 | 2012-04-05 10:44:53 +0800 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 9 | * |
Zonr Chang | ccc39a8 | 2012-04-05 10:44:53 +0800 | [diff] [blame] | 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. |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 15 | */ |
| 16 | |
Zonr Chang | c72c4dd | 2012-04-12 15:38:53 +0800 | [diff] [blame] | 17 | #include "bcc/Script.h" |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 18 | |
Jean-Luc Brouillet | 0a2acce | 2017-02-17 13:29:47 -0800 | [diff] [blame] | 19 | #include "Assert.h" |
| 20 | #include "Log.h" |
| 21 | |
| 22 | #include "bcc/CompilerConfig.h" |
Zonr Chang | c72c4dd | 2012-04-12 15:38:53 +0800 | [diff] [blame] | 23 | #include "bcc/Source.h" |
Logan | 033f46e | 2011-01-06 05:51:24 +0800 | [diff] [blame] | 24 | |
David Gross | cf5afcb | 2017-03-20 15:23:26 -0700 | [diff] [blame] | 25 | #include "bcinfo/MetadataExtractor.h" |
| 26 | |
| 27 | #include <llvm/IR/Module.h> |
| 28 | |
Zonr Chang | ccc39a8 | 2012-04-05 10:44:53 +0800 | [diff] [blame] | 29 | using namespace bcc; |
Stephen Hines | ead5ccb | 2012-05-03 12:30:38 -0700 | [diff] [blame] | 30 | |
Jean-Luc Brouillet | 0a2acce | 2017-02-17 13:29:47 -0800 | [diff] [blame] | 31 | Script::Script(Source *pSource) |
| 32 | : mSource(pSource), |
| 33 | mOptimizationLevel(llvm::CodeGenOpt::Aggressive), |
| 34 | mLinkRuntimeCallback(nullptr), mEmbedInfo(false), mEmbedGlobalInfo(false), |
| 35 | mEmbedGlobalInfoSkipConstant(false) {} |
| 36 | |
| 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); |
Shih-wei Liao | d2a5a0e | 2012-04-25 03:40:50 -0700 | [diff] [blame] | 46 | return false; |
Stephen Hines | ead5ccb | 2012-05-03 12:30:38 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Jean-Luc Brouillet | 0a2acce | 2017-02-17 13:29:47 -0800 | [diff] [blame] | 49 | if (mLinkRuntimeCallback != nullptr) { |
| 50 | mLinkRuntimeCallback(this, &mSource->getModule(), |
| 51 | &libclcore_source->getModule()); |
Stephen Hines | ead5ccb | 2012-05-03 12:30:38 -0700 | [diff] [blame] | 52 | } |
Jean-Luc Brouillet | 0a2acce | 2017-02-17 13:29:47 -0800 | [diff] [blame] | 53 | |
David Gross | cf5afcb | 2017-03-20 15:23:26 -0700 | [diff] [blame] | 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. |
David Gross | e159e8c | 2017-03-27 11:49:15 -0700 | [diff] [blame] | 69 | // See frameworks/rs/driver/README.txt regarding libclcore_source |
| 70 | // obeying compiler version guarantees. |
David Gross | cf5afcb | 2017-03-20 15:23:26 -0700 | [diff] [blame] | 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); |
Jean-Luc Brouillet | 0a2acce | 2017-02-17 13:29:47 -0800 | [diff] [blame] | 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; |
Stephen Hines | ead5ccb | 2012-05-03 12:30:38 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Jean-Luc Brouillet | 0a2acce | 2017-02-17 13:29:47 -0800 | [diff] [blame] | 90 | bool Script::mergeSource(Source &pSource) { return mSource->merge(pSource); } |