Skip to content

Commit 928e413

Browse files
jmmartinezmgcarrasco
authored andcommitted
[HIP][SPIRV] Create an intermediate file for the llvm-link output (llvm#162096)
Before this patch we had: > clang -cc1 source.c -o bitcode.bc > llvm-link -o a.out bitcode.bc > llvm-spirv -o a.out a.out Now we have: > clang -cc1 source.c -o bitcode.bc > llvm-link -o a-linked.bc bitcode.bc > llvm-spirv -o a.out a-linked.bc Co-authored-by: Manuel Carrasco <[email protected]>
1 parent 64716ca commit 928e413

File tree

6 files changed

+24
-19
lines changed

6 files changed

+24
-19
lines changed

clang/lib/Driver/ToolChains/HIPAMD.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,12 @@ void AMDGCN::Linker::constructLinkAndEmitSpirvCommand(
168168
const InputInfo &Output, const llvm::opt::ArgList &Args) const {
169169
assert(!Inputs.empty() && "Must have at least one input.");
170170

171-
constructLlvmLinkCommand(C, JA, Inputs, Output, Args);
171+
std::string LinkedBCFilePrefix(
172+
Twine(llvm::sys::path::stem(Output.getFilename()), "-linked").str());
173+
const char *LinkedBCFilePath = HIP::getTempFile(C, LinkedBCFilePrefix, "bc");
174+
InputInfo LinkedBCFile(&JA, LinkedBCFilePath, Output.getBaseInput());
172175

173-
// Linked BC is now in Output
176+
constructLlvmLinkCommand(C, JA, Inputs, LinkedBCFile, Args);
174177

175178
// Emit SPIR-V binary.
176179
llvm::opt::ArgStringList TrArgs{
@@ -180,7 +183,7 @@ void AMDGCN::Linker::constructLinkAndEmitSpirvCommand(
180183
"--spirv-lower-const-expr",
181184
"--spirv-preserve-auxdata",
182185
"--spirv-debug-info-version=nonsemantic-shader-200"};
183-
SPIRV::constructTranslateCommand(C, *this, JA, Output, Output, TrArgs);
186+
SPIRV::constructTranslateCommand(C, *this, JA, Output, LinkedBCFile, TrArgs);
184187
}
185188

186189
// For amdgcn the inputs of the linker job are device bitcode and output is

clang/lib/Driver/ToolChains/HIPSPV.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,6 @@ using namespace clang::driver::tools;
2222
using namespace clang;
2323
using namespace llvm::opt;
2424

25-
// Convenience function for creating temporary file for both modes of
26-
// isSaveTempsEnabled().
27-
static const char *getTempFile(Compilation &C, StringRef Prefix,
28-
StringRef Extension) {
29-
if (C.getDriver().isSaveTempsEnabled()) {
30-
return C.getArgs().MakeArgString(Prefix + "." + Extension);
31-
}
32-
auto TmpFile = C.getDriver().GetTemporaryPath(Prefix, Extension);
33-
return C.addTempFile(C.getArgs().MakeArgString(TmpFile));
34-
}
35-
3625
// Locates HIP pass plugin.
3726
static std::string findPassPlugin(const Driver &D,
3827
const llvm::opt::ArgList &Args) {
@@ -65,7 +54,7 @@ void HIPSPV::Linker::constructLinkAndEmitSpirvCommand(
6554

6655
assert(!Inputs.empty() && "Must have at least one input.");
6756
std::string Name = std::string(llvm::sys::path::stem(Output.getFilename()));
68-
const char *TempFile = getTempFile(C, Name + "-link", "bc");
57+
const char *TempFile = HIP::getTempFile(C, Name + "-link", "bc");
6958

7059
// Link LLVM bitcode.
7160
ArgStringList LinkArgs{};
@@ -93,7 +82,7 @@ void HIPSPV::Linker::constructLinkAndEmitSpirvCommand(
9382
auto PassPluginPath = findPassPlugin(C.getDriver(), Args);
9483
if (!PassPluginPath.empty()) {
9584
const char *PassPathCStr = C.getArgs().MakeArgString(PassPluginPath);
96-
const char *OptOutput = getTempFile(C, Name + "-lower", "bc");
85+
const char *OptOutput = HIP::getTempFile(C, Name + "-lower", "bc");
9786
ArgStringList OptArgs{TempFile, "-load-pass-plugin",
9887
PassPathCStr, "-passes=hip-post-link-passes",
9988
"-o", OptOutput};

clang/lib/Driver/ToolChains/HIPUtility.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,3 +472,14 @@ void HIP::constructGenerateObjFileFromHIPFatBinary(
472472
D.getClangProgramPath(), ClangArgs,
473473
Inputs, Output, D.getPrependArg()));
474474
}
475+
476+
// Convenience function for creating temporary file for both modes of
477+
// isSaveTempsEnabled().
478+
const char *HIP::getTempFile(Compilation &C, StringRef Prefix,
479+
StringRef Extension) {
480+
if (C.getDriver().isSaveTempsEnabled()) {
481+
return C.getArgs().MakeArgString(Prefix + "." + Extension);
482+
}
483+
auto TmpFile = C.getDriver().GetTemporaryPath(Prefix, Extension);
484+
return C.addTempFile(C.getArgs().MakeArgString(TmpFile));
485+
}

clang/lib/Driver/ToolChains/HIPUtility.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ namespace driver {
1616
namespace tools {
1717
namespace HIP {
1818

19+
const char *getTempFile(Compilation &C, StringRef Prefix, StringRef Extension);
20+
1921
// Construct command for creating HIP fatbin.
2022
void constructHIPFatbinCommand(Compilation &C, const JobAction &JA,
2123
StringRef OutputFileName,

clang/test/Driver/hip-toolchain-no-rdc.hip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@
207207
//
208208

209209
// AMDGCNSPIRV: "-cc1" "-triple" "spirv64-amd-amdhsa" {{.*}}"-emit-llvm-bc" {{.*}}"-fembed-bitcode=marker" "-disable-llvm-passes" {{.*}} "-o" "[[AMDGCNSPV_BC:.*bc]]"
210-
// AMDGCNSPIRV: {{".*llvm-link.*"}} "-o" "[[AMDGCNSPV_TMP:.*out]]" "[[AMDGCNSPV_BC]]"
210+
// AMDGCNSPIRV: {{".*llvm-link.*"}} "-o" "[[AMDGCNSPV_TMP:.*bc]]" "[[AMDGCNSPV_BC]]"
211211
// AMDGCNSPIRV: {{".*llvm-spirv.*"}} "--spirv-max-version=1.6" "--spirv-ext=+all" {{.*}} "[[AMDGCNSPV_TMP]]" {{.*}}"-o" "[[AMDGCNSPV_CO:.*out]]"
212212
// AMDGCNSPIRV: "-cc1" "-triple" "amdgcn-amd-amdhsa" {{.*}}"-emit-obj" {{.*}}"-target-cpu" "gfx900"{{.*}} "-o" "[[GFX900_OBJ:.*o]]"
213213
// AMDGCNSPIRV: {{".*lld.*"}} {{.*}}"-plugin-opt=mcpu=gfx900" {{.*}} "-o" "[[GFX900_CO:.*out]]" {{.*}}"[[GFX900_OBJ]]"

clang/test/Driver/spirv-amd-toolchain.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
// RUN: %clang -### --target=spirv64-amd-amdhsa %s -nogpulib -nogpuinc 2>&1 \
1616
// RUN: | FileCheck %s --check-prefix=INVOCATION
1717
// INVOCATION: "-cc1" "-triple" "spirv64-amd-amdhsa" {{.*}}"-disable-llvm-optzns" {{.*}} "-o" "[[OUTPUT:.+]]" "-x" "c"
18-
// INVOCATION: "{{.*}}llvm-link" "-o" "a.out" "[[OUTPUT]]"
19-
// INVOCATION: "{{.*}}llvm-spirv" "--spirv-max-version=1.6" "--spirv-ext=+all" "--spirv-allow-unknown-intrinsics" "--spirv-lower-const-expr" "--spirv-preserve-auxdata" "--spirv-debug-info-version=nonsemantic-shader-200" "a.out" "-o" "a.out"
18+
// INVOCATION: "{{.*}}llvm-link" "-o" "[[LINKED_OUTPUT:.+]]" "[[OUTPUT]]"
19+
// INVOCATION: "{{.*}}llvm-spirv" "--spirv-max-version=1.6" "--spirv-ext=+all" "--spirv-allow-unknown-intrinsics" "--spirv-lower-const-expr" "--spirv-preserve-auxdata" "--spirv-debug-info-version=nonsemantic-shader-200" "[[LINKED_OUTPUT]]" "-o" "a.out"

0 commit comments

Comments
 (0)