Skip to content

Commit 6ad1170

Browse files
author
Samuel Antao
committedJul 27, 2016
Refactor how include paths are appended to the command arguments.
Summary: This patch aims at removing redundancy in the way include paths for the regular and offloading toolchains are appended to the arguments list in the clang tool. This was suggested by @rsmith in response to r275931. Reviewers: rsmith, tra Subscribers: rsmith, cfe-commits Differential Revision: https://reviews.llvm.org/D22518 llvm-svn: 276929
1 parent f226056 commit 6ad1170

File tree

2 files changed

+39
-57
lines changed

2 files changed

+39
-57
lines changed
 

‎clang/lib/Driver/Tools.cpp

+30-57
Original file line numberDiff line numberDiff line change
@@ -296,56 +296,25 @@ static bool forwardToGCC(const Option &O) {
296296
!O.hasFlag(options::DriverOption) && !O.hasFlag(options::LinkerInput);
297297
}
298298

299-
/// Add the C++ include args of other offloading toolchains. If this is a host
300-
/// job, the device toolchains are added. If this is a device job, the host
301-
/// toolchains will be added.
302-
static void addExtraOffloadCXXStdlibIncludeArgs(Compilation &C,
303-
const JobAction &JA,
304-
const ArgList &Args,
305-
ArgStringList &CmdArgs) {
306-
307-
if (JA.isHostOffloading(Action::OFK_Cuda))
308-
C.getSingleOffloadToolChain<Action::OFK_Cuda>()
309-
->AddClangCXXStdlibIncludeArgs(Args, CmdArgs);
310-
else if (JA.isDeviceOffloading(Action::OFK_Cuda))
311-
C.getSingleOffloadToolChain<Action::OFK_Host>()
312-
->AddClangCXXStdlibIncludeArgs(Args, CmdArgs);
313-
314-
// TODO: Add support for other programming models here.
315-
}
316-
317-
/// Add the C include args of other offloading toolchains. If this is a host
318-
/// job, the device toolchains are added. If this is a device job, the host
319-
/// toolchains will be added.
320-
static void addExtraOffloadClangSystemIncludeArgs(Compilation &C,
321-
const JobAction &JA,
322-
const ArgList &Args,
323-
ArgStringList &CmdArgs) {
324-
325-
if (JA.isHostOffloading(Action::OFK_Cuda))
326-
C.getSingleOffloadToolChain<Action::OFK_Cuda>()->AddClangSystemIncludeArgs(
327-
Args, CmdArgs);
328-
else if (JA.isDeviceOffloading(Action::OFK_Cuda))
329-
C.getSingleOffloadToolChain<Action::OFK_Host>()->AddClangSystemIncludeArgs(
330-
Args, CmdArgs);
331-
332-
// TODO: Add support for other programming models here.
333-
}
334-
335-
/// Add the include args that are specific of each offloading programming model.
336-
static void addExtraOffloadSpecificIncludeArgs(Compilation &C,
337-
const JobAction &JA,
338-
const ArgList &Args,
339-
ArgStringList &CmdArgs) {
340-
299+
/// Apply \a Work on the current tool chain \a RegularToolChain and any other
300+
/// offloading tool chain that is associated with the current action \a JA.
301+
static void
302+
forAllAssociatedToolChains(Compilation &C, const JobAction &JA,
303+
const ToolChain &RegularToolChain,
304+
llvm::function_ref<void(const ToolChain &)> Work) {
305+
// Apply Work on the current/regular tool chain.
306+
Work(RegularToolChain);
307+
308+
// Apply Work on all the offloading tool chains associated with the current
309+
// action.
341310
if (JA.isHostOffloading(Action::OFK_Cuda))
342-
C.getSingleOffloadToolChain<Action::OFK_Host>()->AddCudaIncludeArgs(
343-
Args, CmdArgs);
311+
Work(*C.getSingleOffloadToolChain<Action::OFK_Host>());
344312
else if (JA.isDeviceOffloading(Action::OFK_Cuda))
345-
C.getSingleOffloadToolChain<Action::OFK_Cuda>()->AddCudaIncludeArgs(
346-
Args, CmdArgs);
313+
Work(*C.getSingleOffloadToolChain<Action::OFK_Cuda>());
347314

348-
// TODO: Add support for other programming models here.
315+
//
316+
// TODO: Add support for other offloading programming models here.
317+
//
349318
}
350319

351320
void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
@@ -622,22 +591,26 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
622591
// of an offloading programming model.
623592

624593
// Add C++ include arguments, if needed.
625-
if (types::isCXX(Inputs[0].getType())) {
626-
getToolChain().AddClangCXXStdlibIncludeArgs(Args, CmdArgs);
627-
addExtraOffloadCXXStdlibIncludeArgs(C, JA, Args, CmdArgs);
628-
}
594+
if (types::isCXX(Inputs[0].getType()))
595+
forAllAssociatedToolChains(C, JA, getToolChain(),
596+
[&Args, &CmdArgs](const ToolChain &TC) {
597+
TC.AddClangCXXStdlibIncludeArgs(Args, CmdArgs);
598+
});
629599

630600
// Add system include arguments for all targets but IAMCU.
631-
if (!IsIAMCU) {
632-
getToolChain().AddClangSystemIncludeArgs(Args, CmdArgs);
633-
addExtraOffloadClangSystemIncludeArgs(C, JA, Args, CmdArgs);
634-
} else {
601+
if (!IsIAMCU)
602+
forAllAssociatedToolChains(C, JA, getToolChain(),
603+
[&Args, &CmdArgs](const ToolChain &TC) {
604+
TC.AddClangSystemIncludeArgs(Args, CmdArgs);
605+
});
606+
else {
635607
// For IAMCU add special include arguments.
636608
getToolChain().AddIAMCUIncludeArgs(Args, CmdArgs);
637609
}
638610

639-
// Add offload include arguments, if needed.
640-
addExtraOffloadSpecificIncludeArgs(C, JA, Args, CmdArgs);
611+
// Add offload include arguments specific for CUDA if that is required.
612+
if (JA.isOffloading(Action::OFK_Cuda))
613+
getToolChain().AddCudaIncludeArgs(Args, CmdArgs);
641614
}
642615

643616
// FIXME: Move to target hook.

‎clang/test/Driver/cuda-version-check.cu

+9
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@
3939
// RUN: --no-cuda-version-check %s | \
4040
// RUN: FileCheck %s --check-prefix=OK
4141

42+
// We need to make sure the version check is done only for the device toolchain,
43+
// therefore we should not get an error in host-only mode. We use the -S here
44+
// to avoid the error being produced in case by the assembler tool, which does
45+
// the same check.
46+
// RUN: %clang -v -### --cuda-gpu-arch=sm_60 --cuda-host-only --sysroot=%S/Inputs/CUDA -S 2>&1 %s | \
47+
// RUN: FileCheck %s --check-prefix=OK
48+
// RUN: %clang -v -### --cuda-gpu-arch=sm_60 --cuda-device-only --sysroot=%S/Inputs/CUDA -S 2>&1 %s | \
49+
// RUN: FileCheck %s --check-prefix=ERR_SM60
50+
4251
// OK-NOT: error: GPU arch
4352

4453
// OK_SM35-NOT: error: GPU arch sm_35

0 commit comments

Comments
 (0)
Please sign in to comment.