|
| 1 | +/******************************************************************************* |
| 2 | + * |
| 3 | + * University of Illinois/NCSA |
| 4 | + * Open Source License |
| 5 | + * |
| 6 | + * Copyright (c) 2003-2017 University of Illinois at Urbana-Champaign. |
| 7 | + * Modifications (c) 2018 Advanced Micro Devices, Inc. |
| 8 | + * All rights reserved. |
| 9 | + * |
| 10 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | + * of this software and associated documentation files (the "Software"), to deal |
| 12 | + * with the Software without restriction, including without limitation the |
| 13 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 14 | + * sell copies of the Software, and to permit persons to whom the Software is |
| 15 | + * furnished to do so, subject to the following conditions: |
| 16 | + * |
| 17 | + * * Redistributions of source code must retain the above copyright notice, |
| 18 | + * this list of conditions and the following disclaimers. |
| 19 | + * |
| 20 | + * * Redistributions in binary form must reproduce the above copyright |
| 21 | + * notice, this list of conditions and the following disclaimers in the |
| 22 | + * documentation and/or other materials provided with the distribution. |
| 23 | + * |
| 24 | + * * Neither the names of the LLVM Team, University of Illinois at |
| 25 | + * Urbana-Champaign, nor the names of its contributors may be used to |
| 26 | + * endorse or promote products derived from this Software without specific |
| 27 | + * prior written permission. |
| 28 | + * |
| 29 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 30 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 31 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 32 | + * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 33 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 34 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH |
| 35 | + * THE SOFTWARE. |
| 36 | + * |
| 37 | + ******************************************************************************/ |
| 38 | + |
| 39 | +#include "comgr-spirv-command.h" |
| 40 | + |
| 41 | +#ifndef COMGR_DISABLE_SPIRV |
| 42 | +#include "comgr-diagnostic-handler.h" |
| 43 | + |
| 44 | +#include <LLVMSPIRVLib/LLVMSPIRVLib.h> |
| 45 | +#include <llvm/Bitcode/BitcodeWriter.h> |
| 46 | +#include <llvm/IR/LLVMContext.h> |
| 47 | + |
| 48 | +#include <sstream> |
| 49 | +#endif |
| 50 | + |
| 51 | +namespace COMGR { |
| 52 | +using namespace llvm; |
| 53 | +Error SPIRVCommand::writeExecuteOutput(StringRef CachedBuffer) { |
| 54 | + assert(OutputBuffer.empty()); |
| 55 | + OutputBuffer.reserve(CachedBuffer.size()); |
| 56 | + OutputBuffer.insert(OutputBuffer.end(), CachedBuffer.begin(), |
| 57 | + CachedBuffer.end()); |
| 58 | + return Error::success(); |
| 59 | +} |
| 60 | + |
| 61 | +Expected<StringRef> SPIRVCommand::readExecuteOutput() { |
| 62 | + return StringRef(OutputBuffer.data(), OutputBuffer.size()); |
| 63 | +} |
| 64 | + |
| 65 | +amd_comgr_status_t SPIRVCommand::execute(raw_ostream &LogS) { |
| 66 | +#ifndef COMGR_DISABLE_SPIRV |
| 67 | + LLVMContext Context; |
| 68 | + Context.setDiagnosticHandler( |
| 69 | + std::make_unique<AMDGPUCompilerDiagnosticHandler>(LogS), true); |
| 70 | + |
| 71 | + // TODO: With C++23, we should investigate replacing with spanstream |
| 72 | + // to avoid memory copies: |
| 73 | + // https://en.cppreference.com/w/cpp/io/basic_ispanstream |
| 74 | + std::istringstream ISS(std::string(InputBuffer.data(), InputBuffer.size())); |
| 75 | + |
| 76 | + Module *M; |
| 77 | + std::string Err; |
| 78 | + |
| 79 | + if (!readSpirv(Context, ISS, M, Err)) { |
| 80 | + LogS << "Failed to load SPIR-V as LLVM Module: " << Err << '\n'; |
| 81 | + return AMD_COMGR_STATUS_ERROR; |
| 82 | + } |
| 83 | + |
| 84 | + BitcodeWriter Writer(OutputBuffer); |
| 85 | + Writer.writeModule(*M, false, nullptr, false, nullptr); |
| 86 | + Writer.writeSymtab(); |
| 87 | + Writer.writeStrtab(); |
| 88 | + return AMD_COMGR_STATUS_SUCCESS; |
| 89 | +#else |
| 90 | + return AMD_COMGR_STATUS_ERROR; |
| 91 | +#endif |
| 92 | +} |
| 93 | + |
| 94 | +SPIRVCommand::ActionClass SPIRVCommand::getClass() const { |
| 95 | + // return an action class that is not allocated to distinguish it from any |
| 96 | + // clang action |
| 97 | + return clang::driver::Action::ActionClass::JobClassLast + 1; |
| 98 | +} |
| 99 | + |
| 100 | +void SPIRVCommand::addOptionsIdentifier(HashAlgorithm &) const { |
| 101 | + // do nothing, there are no options |
| 102 | + return; |
| 103 | +} |
| 104 | + |
| 105 | +Error SPIRVCommand::addInputIdentifier(HashAlgorithm &H) const { |
| 106 | + addString(H, InputBuffer); |
| 107 | + return Error::success(); |
| 108 | +} |
| 109 | +} // namespace COMGR |
0 commit comments