clang 24.0.0git
HIPUtility.cpp
Go to the documentation of this file.
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"
14#include "llvm/Support/Path.h"
15
16using namespace clang;
17using namespace clang::driver;
18using namespace clang::driver::tools;
19using namespace llvm::opt;
20
21#if defined(_WIN32) || defined(_WIN64)
22#define NULL_FILE "nul"
23#else
24#define NULL_FILE "/dev/null"
25#endif
26
27namespace {
28const unsigned HIPCodeObjectAlign = 4096;
29} // namespace
30
31// Construct a clang-offload-bundler command to bundle code objects for
32// different devices into a HIP fat binary.
34 llvm::StringRef OutputFileName,
35 const InputInfoList &Inputs,
36 const llvm::opt::ArgList &Args,
37 const Tool &T) {
38 // Construct clang-offload-bundler command to bundle object files for
39 // for different GPU archs.
40 ArgStringList BundlerArgs;
41 BundlerArgs.push_back(Args.MakeArgString("-type=o"));
42 BundlerArgs.push_back(
43 Args.MakeArgString("-bundle-align=" + Twine(HIPCodeObjectAlign)));
44
45 // ToDo: Remove the dummy host binary entry which is required by
46 // clang-offload-bundler.
47 std::string BundlerTargetArg = "-targets=host-x86_64-unknown-linux-gnu";
48 // AMDGCN:
49 // For code object version 2 and 3, the offload kind in bundle ID is 'hip'
50 // for backward compatibility. For code object version 4 and greater, the
51 // offload kind in bundle ID is 'hipv4'.
52 std::string OffloadKind = "hip";
53 if (T.getToolChain().getTriple().isAMDGCN() &&
54 getAMDGPUCodeObjectVersion(C.getDriver(), Args) >= 4)
55 OffloadKind = OffloadKind + "v4";
56 for (const auto &II : Inputs) {
57 const auto *A = II.getAction();
58 const llvm::Triple &InputTriple = A->getOffloadingToolChain()->getTriple();
59
60 BoundArch BA = A->getOffloadingArch();
61 BundlerTargetArg += ',' + OffloadKind + '-';
62 if (BA.ArchName == "amdgcnspirv")
63 BundlerTargetArg += "spirv64-amd-amdhsa-";
64 else
65 BundlerTargetArg += normalizeForBundler(InputTriple, BA.ArchName);
66 if (BA)
67 BundlerTargetArg += '-' + BA.ArchName.str();
68 }
69 BundlerArgs.push_back(Args.MakeArgString(BundlerTargetArg));
70
71 // Use a NULL file as input for the dummy host binary entry
72 std::string BundlerInputArg = "-input=" NULL_FILE;
73 BundlerArgs.push_back(Args.MakeArgString(BundlerInputArg));
74 for (const auto &II : Inputs) {
75 BundlerInputArg = std::string("-input=") + II.getFilename();
76 BundlerArgs.push_back(Args.MakeArgString(BundlerInputArg));
77 }
78
79 std::string Output = std::string(OutputFileName);
80 auto *BundlerOutputArg =
81 Args.MakeArgString(std::string("-output=").append(Output));
82 BundlerArgs.push_back(BundlerOutputArg);
83
84 addOffloadCompressArgs(Args, BundlerArgs);
85
86 const char *Bundler = Args.MakeArgString(
87 T.getToolChain().GetProgramPath("clang-offload-bundler"));
88 C.addCommand(std::make_unique<Command>(
89 JA, T, ResponseFileSupport::None(), Bundler, BundlerArgs, Inputs,
90 InputInfo(&JA, Args.MakeArgString(Output))));
91}
92
93// Convenience function for creating temporary file for both modes of
94// isSaveTempsEnabled().
95const char *HIP::getTempFile(Compilation &C, StringRef Prefix,
96 StringRef Extension) {
97 if (C.getDriver().isSaveTempsEnabled()) {
98 return C.getArgs().MakeArgString(Prefix + "." + Extension);
99 }
100 auto TmpFile = C.getDriver().GetTemporaryPath(Prefix, Extension);
101 return C.addTempFile(C.getArgs().MakeArgString(TmpFile));
102}
#define NULL_FILE
Definition HIPAMD.cpp:33
Compilation - A set of tasks to perform for a single driver invocation.
Definition Compilation.h:46
InputInfo - Wrapper for information about an input source.
Definition InputInfo.h:22
Tool - Information on a specific compilation tool.
Definition Tool.h:32
void constructHIPFatbinCommand(Compilation &C, const JobAction &JA, StringRef OutputFileName, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const Tool &T)
const char * getTempFile(Compilation &C, StringRef Prefix, StringRef Extension)
void addOffloadCompressArgs(const llvm::opt::ArgList &TCArgs, llvm::opt::ArgStringList &CmdArgs)
unsigned getAMDGPUCodeObjectVersion(const Driver &D, const llvm::opt::ArgList &Args)
SmallVector< InputInfo, 4 > InputInfoList
Definition Driver.h:49
Top level wrappers for InstallAPI frontend operations.
std::string normalizeForBundler(const llvm::Triple &T, llvm::StringRef BoundArch)
Constructs the normalized triple string used inside an offload bundle entry ID for device triple T.
Definition TargetID.cpp:75
const FunctionProtoType * T
Represents a bound architecture for offload / multiple architecture compilation.
llvm::StringRef ArchName
static constexpr ResponseFileSupport None()
Returns a ResponseFileSupport indicating that response files are not supported.
Definition Job.h:79