Skip to content

Commit 4af3b7e

Browse files
committed
add utility
Signed-off-by: Nick Sarnie <[email protected]>
1 parent 89b0fe0 commit 4af3b7e

File tree

9 files changed

+53
-35
lines changed

9 files changed

+53
-35
lines changed

clang/include/clang/Driver/CommonArgs.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,14 @@ void setComplexRange(const Driver &D, StringRef NewOpt,
312312
LangOptions::ComplexRangeKind NewRange, StringRef &LastOpt,
313313
LangOptions::ComplexRangeKind &Range);
314314

315+
void constructLlvmLinkCommand(Compilation &C, const Tool &T,
316+
const JobAction &JA,
317+
const InputInfoList &JobInputs,
318+
const llvm::opt::ArgStringList &LinkerInputs,
319+
const InputInfo &Output,
320+
const llvm::opt::ArgList &Args,
321+
const char *OutputFilename = nullptr);
322+
315323
} // end namespace tools
316324
} // end namespace driver
317325
} // end namespace clang

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3637,3 +3637,28 @@ void tools::setComplexRange(const Driver &D, StringRef NewOpt,
36373637
LastOpt = NewOpt;
36383638
Range = NewRange;
36393639
}
3640+
3641+
void tools::constructLlvmLinkCommand(Compilation &C, const Tool &T,
3642+
const JobAction &JA,
3643+
const InputInfoList &JobInputs,
3644+
const ArgStringList &LinkerInputs,
3645+
const InputInfo &Output,
3646+
const llvm::opt::ArgList &Args,
3647+
const char *OutputFilename) {
3648+
// Construct llvm-link command.
3649+
// The output from llvm-link is a bitcode file.
3650+
3651+
assert(!LinkerInputs.empty() && !JobInputs.empty() &&
3652+
"Must have at least one input.");
3653+
3654+
ArgStringList LlvmLinkArgs(
3655+
{"-o", OutputFilename ? OutputFilename : Output.getFilename()});
3656+
3657+
LlvmLinkArgs.append(LinkerInputs);
3658+
3659+
const ToolChain &TC = T.getToolChain();
3660+
const char *LlvmLink = Args.MakeArgString(TC.GetProgramPath("llvm-link"));
3661+
C.addCommand(std::make_unique<Command>(JA, T, ResponseFileSupport::None(),
3662+
LlvmLink, LlvmLinkArgs, JobInputs,
3663+
Output));
3664+
}

clang/lib/Driver/ToolChains/HIPAMD.cpp

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,22 @@ using namespace llvm::opt;
3333
#define NULL_FILE "/dev/null"
3434
#endif
3535

36-
void AMDGCN::Linker::constructLlvmLinkCommand(Compilation &C,
37-
const JobAction &JA,
38-
const InputInfoList &Inputs,
39-
const InputInfo &Output,
40-
const llvm::opt::ArgList &Args) const {
41-
// Construct llvm-link command.
42-
// The output from llvm-link is a bitcode file.
43-
ArgStringList LlvmLinkArgs;
36+
void AMDGCN::Linker::constructLlvmLinkCommand(
37+
Compilation &C, const JobAction &JA, const InputInfoList &Inputs,
38+
const InputInfo &Output, const llvm::opt::ArgList &Args) const {
4439

45-
assert(!Inputs.empty() && "Must have at least one input.");
40+
ArgStringList LinkerInputs;
4641

47-
LlvmLinkArgs.append({"-o", Output.getFilename()});
4842
for (auto Input : Inputs)
49-
LlvmLinkArgs.push_back(Input.getFilename());
43+
LinkerInputs.push_back(Input.getFilename());
5044

5145
// Look for archive of bundled bitcode in arguments, and add temporary files
5246
// for the extracted archive of bitcode to inputs.
5347
auto TargetID = Args.getLastArgValue(options::OPT_mcpu_EQ);
54-
AddStaticDeviceLibsLinking(C, *this, JA, Inputs, Args, LlvmLinkArgs, "amdgcn",
48+
AddStaticDeviceLibsLinking(C, *this, JA, Inputs, Args, LinkerInputs, "amdgcn",
5549
TargetID, /*IsBitCodeSDL=*/true);
56-
57-
const char *LlvmLink =
58-
Args.MakeArgString(getToolChain().GetProgramPath("llvm-link"));
59-
C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
60-
LlvmLink, LlvmLinkArgs, Inputs,
61-
Output));
50+
tools::constructLlvmLinkCommand(C, *this, JA, Inputs, LinkerInputs, Output,
51+
Args);
6252
}
6353

6454
void AMDGCN::Linker::constructLldCommand(Compilation &C, const JobAction &JA,

clang/lib/Driver/ToolChains/HIPSPV.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,8 @@ void HIPSPV::Linker::constructLinkAndEmitSpirvCommand(
6969
"generic"; // SPIR-V is generic, no specific target ID like -mcpu
7070
tools::AddStaticDeviceLibsLinking(C, *this, JA, Inputs, Args, LinkArgs, Arch,
7171
Target, /*IsBitCodeSDL=*/true);
72-
LinkArgs.append({"-o", TempFile});
73-
const char *LlvmLink =
74-
Args.MakeArgString(getToolChain().GetProgramPath("llvm-link"));
75-
C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
76-
LlvmLink, LinkArgs, Inputs, Output));
72+
tools::constructLlvmLinkCommand(C, *this, JA, Inputs, LinkArgs, Output, Args,
73+
TempFile);
7774

7875
// Post-link HIP lowering.
7976

clang/lib/Driver/ToolChains/SPIRV.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,15 @@ void SPIRV::constructLLVMLinkCommand(Compilation &C, const Tool &T,
7474
const JobAction &JA,
7575
const InputInfo &Output,
7676
const InputInfoList &Inputs,
77-
const llvm::opt::ArgStringList &Args) {
78-
// Construct llvm-link command.
79-
// The output from llvm-link is a bitcode file.
80-
ArgStringList LlvmLinkArgs;
77+
const llvm::opt::ArgList &Args) {
8178

82-
assert(!Inputs.empty() && "Must have at least one input.");
79+
ArgStringList LlvmLinkArgs;
8380

84-
LlvmLinkArgs.append({"-o", Output.getFilename()});
8581
for (auto Input : Inputs)
8682
LlvmLinkArgs.push_back(Input.getFilename());
8783

84+
tools::constructLlvmLinkCommand(C, T, JA, Inputs, LlvmLinkArgs, Output, Args);
85+
8886
const char *LlvmLink =
8987
C.getArgs().MakeArgString(T.getToolChain().GetProgramPath("llvm-link"));
9088
C.addCommand(std::make_unique<Command>(JA, T, ResponseFileSupport::None(),
@@ -144,7 +142,7 @@ void SPIRV::Linker::ConstructJob(Compilation &C, const JobAction &JA,
144142
const ArgList &Args,
145143
const char *LinkingOutput) const {
146144
if (JA.getType() == types::TY_LLVM_BC) {
147-
constructLLVMLinkCommand(C, *this, JA, Output, Inputs, {});
145+
constructLLVMLinkCommand(C, *this, JA, Output, Inputs, Args);
148146
return;
149147
}
150148
const ToolChain &ToolChain = getToolChain();

clang/lib/Driver/ToolChains/SPIRV.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void constructAssembleCommand(Compilation &C, const Tool &T,
3030
void constructLLVMLinkCommand(Compilation &C, const Tool &T,
3131
const JobAction &JA, const InputInfo &Output,
3232
const InputInfoList &Inputs,
33-
const llvm::opt::ArgStringList &Args);
33+
const llvm::opt::ArgList &Args);
3434

3535
class LLVM_LIBRARY_VISIBILITY Translator : public Tool {
3636
public:

clang/test/Driver/hipspv-link-static-library.hip

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
// Verify that the input files are added before the SDL files in llvm-link command
2323
// This tests the ordering fix to match HIPAMD behavior
2424
// SDL-LINK: "{{.*}}clang-offload-bundler" "-unbundle" "-type=a" "-input={{.*}}libSDL.a" "-targets=hip-spirv64-unknown-unknown-unknown-generic" "-output=[[SDL_A:.*\.a]]" "-allow-missing-bundles"
25-
// SDL-LINK: "{{.*}}llvm-link" "{{.*}}.bc" "[[SDL_A]]" "-o"
25+
// SDL-LINK: "{{.*}}llvm-link" "-o" "{{.*}}.bc" "{{.*}}.bc" "[[SDL_A]]"
2626

2727
// SDL-ARCHIVE: "{{.*}}clang-offload-bundler" "-unbundle" "-type=a" "-input={{.*}}libSDL.a" "-targets=hip-spirv64-unknown-unknown-unknown-generic" "-output=[[SDL_A:.*\.a]]" "-allow-missing-bundles"
28-
// SDL-ARCHIVE: "{{.*}}llvm-link" "{{.*}}.bc" "[[SDL_A]]" "-o"
28+
// SDL-ARCHIVE: "{{.*}}llvm-link" "-o" "{{.*}}.bc" "{{.*}}.bc" "[[SDL_A]]"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
// CHECK-SAME: {{.*}} [[B_SRC]]
4242

4343
// Link device code, lower it with HIPSPV passes and emit SPIR-V binary.
44-
// CHECK: {{".*llvm-link.*"}} [[A_BC1]] [[B_BC1]] "-o" [[AB_LINK:".*bc"]]
44+
// CHECK: {{".*llvm-link.*"}} "-o" [[AB_LINK:".*bc"]] [[A_BC1]] [[B_BC1]]
4545
// CHECK: {{".*opt.*"}} [[AB_LINK]] "-load-pass-plugin"
4646
// CHECK-SAME: "{{.*}}/Inputs/hipspv/lib/libLLVMHipSpvPasses.so"
4747
// CHECK-SAME: "-o" [[AB_LOWER:".*bc"]]

clang/test/Driver/hipspv-toolchain.hip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// CHECK-SAME: "-o" [[DEV_BC:".*bc"]]
1515
// CHECK-SAME: "-x" "hip"
1616

17-
// CHECK: {{".*llvm-link"}} [[DEV_BC]] "-o" [[LINK_BC:".*bc"]]
17+
// CHECK: {{".*llvm-link"}} "-o" [[LINK_BC:".*bc"]] [[DEV_BC]]
1818

1919
// CHECK: {{".*opt"}} [[LINK_BC]] "-load-pass-plugin"
2020
// CHECK-SAME: {{".*/hipspv/lib/libLLVMHipSpvPasses.so"}}

0 commit comments

Comments
 (0)