|
| 1 | +/******************************************************************************* |
| 2 | + * |
| 3 | + * University of Illinois/NCSA |
| 4 | + * Open Source License |
| 5 | + * |
| 6 | + * Copyright (c) 2018 Advanced Micro Devices, Inc. All Rights Reserved. |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * with the Software without restriction, including without limitation the |
| 11 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 12 | + * sell copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * * Redistributions of source code must retain the above copyright notice, |
| 16 | + * this list of conditions and the following disclaimers. |
| 17 | + * |
| 18 | + * * Redistributions in binary form must reproduce the above copyright |
| 19 | + * notice, this list of conditions and the following disclaimers in the |
| 20 | + * documentation and/or other materials provided with the distribution. |
| 21 | + * |
| 22 | + * * Neither the names of Advanced Micro Devices, Inc. nor the names of its |
| 23 | + * contributors may be used to endorse or promote products derived from |
| 24 | + * this Software without specific prior written permission. |
| 25 | + * |
| 26 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 27 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 28 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 29 | + * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 30 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 31 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH |
| 32 | + * THE SOFTWARE. |
| 33 | + * |
| 34 | + ******************************************************************************/ |
| 35 | + |
| 36 | +#include "amd_comgr.h" |
| 37 | +#include "common.h" |
| 38 | + |
| 39 | +int main(int argc, char *argv[]) { |
| 40 | + char *BundleData; |
| 41 | + size_t BundleSize; |
| 42 | + |
| 43 | + if (argc < 4) { |
| 44 | + printf("Usage: %s <bc bundle> <arch> <bc output>\n", argv[0]); |
| 45 | + return -1; |
| 46 | + } |
| 47 | + |
| 48 | + const char *BundlePath = argv[1]; |
| 49 | + const char *Arch = argv[2]; |
| 50 | + const char *BitcodePath = argv[3]; |
| 51 | + |
| 52 | + amd_comgr_data_t OneBundle; |
| 53 | + amd_comgr_data_set_t InputBundles; |
| 54 | + |
| 55 | + BundleSize = setBuf(BundlePath, &BundleData); |
| 56 | + |
| 57 | + amd_comgr_(create_data_set(&InputBundles)); |
| 58 | + amd_comgr_(create_data(AMD_COMGR_DATA_KIND_BC_BUNDLE, &OneBundle)); |
| 59 | + amd_comgr_(set_data(OneBundle, BundleSize, BundleData)); |
| 60 | + amd_comgr_(set_data_name(OneBundle, "bundle.bc")); |
| 61 | + amd_comgr_(data_set_add(InputBundles, OneBundle)); |
| 62 | + |
| 63 | + amd_comgr_data_set_t OutputBitcode; |
| 64 | + amd_comgr_(create_data_set(&OutputBitcode)); |
| 65 | + |
| 66 | + amd_comgr_action_info_t DataAction; |
| 67 | + amd_comgr_(create_action_info(&DataAction)); |
| 68 | + |
| 69 | + const char *AllArch[] = {Arch}; |
| 70 | + amd_comgr_(action_info_set_bundle_entry_ids(DataAction, AllArch, 1)); |
| 71 | + amd_comgr_(do_action(AMD_COMGR_ACTION_UNBUNDLE, DataAction, InputBundles, |
| 72 | + OutputBitcode)); |
| 73 | + |
| 74 | + size_t Count; |
| 75 | + amd_comgr_(action_data_count(OutputBitcode, AMD_COMGR_DATA_KIND_BC, &Count)); |
| 76 | + |
| 77 | + if (Count != 1) { |
| 78 | + printf("AMD_COMGR_ACTION_COMPILE_SOURCE_TO_BC Failed: " |
| 79 | + "produced %zu BC objects (expected 1)\n", |
| 80 | + Count); |
| 81 | + exit(1); |
| 82 | + } |
| 83 | + |
| 84 | + amd_comgr_data_t OneBitcode; |
| 85 | + amd_comgr_(action_data_get_data(OutputBitcode, AMD_COMGR_DATA_KIND_BC, 0, |
| 86 | + &OneBitcode)); |
| 87 | + |
| 88 | + size_t BufferSize; |
| 89 | + amd_comgr_(get_data(OneBitcode, &BufferSize, 0x0)); |
| 90 | + char *Buffer = (char *)malloc(BufferSize); |
| 91 | + amd_comgr_(get_data(OneBitcode, &BufferSize, Buffer)); |
| 92 | + |
| 93 | + FILE *BitcodeFile = fopen(BitcodePath, "wb"); |
| 94 | + fwrite(Buffer, 1, BufferSize, BitcodeFile); |
| 95 | + fclose(BitcodeFile); |
| 96 | + |
| 97 | + free(Buffer); |
| 98 | + amd_comgr_(release_data(OneBitcode)); |
| 99 | + amd_comgr_(release_data(OneBundle)); |
| 100 | + amd_comgr_(destroy_action_info(DataAction)); |
| 101 | + amd_comgr_(destroy_data_set(OutputBitcode)); |
| 102 | + amd_comgr_(destroy_data_set(InputBundles)); |
| 103 | + |
| 104 | + return 0; |
| 105 | +} |
0 commit comments