| 1 | //===--- HIPUtility.cpp - Common HIP Tool Chain Utilities -------*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "HIPUtility.h" |
| 10 | #include "clang/Driver/CommonArgs.h" |
| 11 | #include "clang/Driver/Compilation.h" |
| 12 | #include "clang/Options/Options.h" |
| 13 | #include "llvm/Support/Path.h" |
| 14 | |
| 15 | using namespace clang; |
| 16 | using namespace clang::driver; |
| 17 | using namespace clang::driver::tools; |
| 18 | using namespace llvm::opt; |
| 19 | |
| 20 | #if defined(_WIN32) || defined(_WIN64) |
| 21 | #define NULL_FILE "nul" |
| 22 | #else |
| 23 | #define NULL_FILE "/dev/null" |
| 24 | #endif |
| 25 | |
| 26 | namespace { |
| 27 | const unsigned HIPCodeObjectAlign = 4096; |
| 28 | } // namespace |
| 29 | |
| 30 | // Constructs a triple string for clang offload bundler. |
| 31 | static std::string normalizeForBundler(const llvm::Triple &OrigT, |
| 32 | StringRef BoundArch) { |
| 33 | llvm::Triple T(OrigT); |
| 34 | bool HasTargetID = !BoundArch.empty(); |
| 35 | |
| 36 | // FIXME: Short-term hack. The HIP runtime hardcodes the legacy |
| 37 | // "amdgcn-amd-amdhsa--" prefix when parsing the target IDs embedded in the |
| 38 | // fatbin bundle, so force it. |
| 39 | if (HasTargetID && T.isAMDGCN()) { |
| 40 | return ("amdgcn-" + T.getVendorName() + "-" + T.getOSName() + "-" + |
| 41 | T.getEnvironmentName()) |
| 42 | .str(); |
| 43 | } |
| 44 | |
| 45 | return HasTargetID ? (T.getArchName() + "-" + T.getVendorName() + "-" + |
| 46 | T.getOSName() + "-" + T.getEnvironmentName()) |
| 47 | .str() |
| 48 | : T.normalize(Form: llvm::Triple::CanonicalForm::FOUR_IDENT); |
| 49 | } |
| 50 | |
| 51 | // Construct a clang-offload-bundler command to bundle code objects for |
| 52 | // different devices into a HIP fat binary. |
| 53 | void HIP::constructHIPFatbinCommand(Compilation &C, const JobAction &JA, |
| 54 | llvm::StringRef OutputFileName, |
| 55 | const InputInfoList &Inputs, |
| 56 | const llvm::opt::ArgList &Args, |
| 57 | const Tool &T) { |
| 58 | // Construct clang-offload-bundler command to bundle object files for |
| 59 | // for different GPU archs. |
| 60 | ArgStringList BundlerArgs; |
| 61 | BundlerArgs.push_back(Elt: Args.MakeArgString(Str: "-type=o" )); |
| 62 | BundlerArgs.push_back( |
| 63 | Elt: Args.MakeArgString(Str: "-bundle-align=" + Twine(HIPCodeObjectAlign))); |
| 64 | |
| 65 | // ToDo: Remove the dummy host binary entry which is required by |
| 66 | // clang-offload-bundler. |
| 67 | std::string BundlerTargetArg = "-targets=host-x86_64-unknown-linux-gnu" ; |
| 68 | // AMDGCN: |
| 69 | // For code object version 2 and 3, the offload kind in bundle ID is 'hip' |
| 70 | // for backward compatibility. For code object version 4 and greater, the |
| 71 | // offload kind in bundle ID is 'hipv4'. |
| 72 | std::string OffloadKind = "hip" ; |
| 73 | if (T.getToolChain().getTriple().isAMDGCN() && |
| 74 | getAMDGPUCodeObjectVersion(D: C.getDriver(), Args) >= 4) |
| 75 | OffloadKind = OffloadKind + "v4" ; |
| 76 | for (const auto &II : Inputs) { |
| 77 | const auto *A = II.getAction(); |
| 78 | const llvm::Triple &InputTriple = A->getOffloadingToolChain()->getTriple(); |
| 79 | |
| 80 | BoundArch BA = A->getOffloadingArch(); |
| 81 | BundlerTargetArg += ',' + OffloadKind + '-'; |
| 82 | if (BA.ArchName == "amdgcnspirv" ) |
| 83 | BundlerTargetArg += "spirv64-amd-amdhsa-" ; |
| 84 | else |
| 85 | BundlerTargetArg += normalizeForBundler(OrigT: InputTriple, BoundArch: BA.ArchName); |
| 86 | if (BA) |
| 87 | BundlerTargetArg += '-' + BA.ArchName.str(); |
| 88 | } |
| 89 | BundlerArgs.push_back(Elt: Args.MakeArgString(Str: BundlerTargetArg)); |
| 90 | |
| 91 | // Use a NULL file as input for the dummy host binary entry |
| 92 | std::string BundlerInputArg = "-input=" NULL_FILE; |
| 93 | BundlerArgs.push_back(Elt: Args.MakeArgString(Str: BundlerInputArg)); |
| 94 | for (const auto &II : Inputs) { |
| 95 | BundlerInputArg = std::string("-input=" ) + II.getFilename(); |
| 96 | BundlerArgs.push_back(Elt: Args.MakeArgString(Str: BundlerInputArg)); |
| 97 | } |
| 98 | |
| 99 | std::string Output = std::string(OutputFileName); |
| 100 | auto *BundlerOutputArg = |
| 101 | Args.MakeArgString(Str: std::string("-output=" ).append(str: Output)); |
| 102 | BundlerArgs.push_back(Elt: BundlerOutputArg); |
| 103 | |
| 104 | addOffloadCompressArgs(TCArgs: Args, CmdArgs&: BundlerArgs); |
| 105 | |
| 106 | const char *Bundler = Args.MakeArgString( |
| 107 | Str: T.getToolChain().GetProgramPath(Name: "clang-offload-bundler" )); |
| 108 | C.addCommand(Cmd: std::make_unique<Command>( |
| 109 | args: JA, args: T, args: ResponseFileSupport::None(), args&: Bundler, args&: BundlerArgs, args: Inputs, |
| 110 | args: InputInfo(&JA, Args.MakeArgString(Str: Output)))); |
| 111 | } |
| 112 | |
| 113 | // Convenience function for creating temporary file for both modes of |
| 114 | // isSaveTempsEnabled(). |
| 115 | const char *HIP::getTempFile(Compilation &C, StringRef Prefix, |
| 116 | StringRef Extension) { |
| 117 | if (C.getDriver().isSaveTempsEnabled()) { |
| 118 | return C.getArgs().MakeArgString(Str: Prefix + "." + Extension); |
| 119 | } |
| 120 | auto TmpFile = C.getDriver().GetTemporaryPath(Prefix, Suffix: Extension); |
| 121 | return C.addTempFile(Name: C.getArgs().MakeArgString(Str: TmpFile)); |
| 122 | } |
| 123 | |