Skip to content

Commit 72d9b05

Browse files
authored
[SYCL][Driver] Move offload mismatch warning into SYCLActionBuilder (#7434)
The SYCLActionBuilder handles the parsing of the arguments to determine the SYCL triples and architectures being used, so doing the check there means we can just re-use that rather than having to parse the triples and target arguments again. This fixes the issue where for CUDA and HIP where the check failed to take into account the offload architecture and would just compare the section name that has the triple and the offload architecture against just the triple which would always fail even when the triples and offload architectures were otherwise matching. The new binaries for the test where generated from a `dummy.cpp` C++ file containing just `void foo() {}`, and: ``` clang++ -fsycl -fsycl-targets=nvptx64-nvidia-cuda dummy.cpp -c -o objnvptx64-sm_50.o llvm-ar cr libnvptx64-sm_50.a objnvptx64-sm_50.o clang++ -fsycl -fsycl-targets=amdgcn-amd-amdhsa -Xsycl-target-backend --offload-arch=gfx908 dummy.cpp -c -o objamdgcn-gfx908.o -fno-sycl-libspirv llvm-ar cr libamdgcn-gfx908.a objamdgcn-gfx908.o ``` This patch solves the incorrect warning issues reported in #6636 and #7300
1 parent 764e2ba commit 72d9b05

File tree

6 files changed

+119
-79
lines changed

6 files changed

+119
-79
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 65 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,9 +1731,6 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
17311731
if (checkForSYCLDefaultDevice(*C, *TranslatedArgs))
17321732
setSYCLDefaultTriple(true);
17331733

1734-
// Check missing targets in archives/objects based on inputs from the user.
1735-
checkForOffloadMismatch(*C, *TranslatedArgs);
1736-
17371734
// Populate the tool chains for the offloading devices, if any.
17381735
CreateOffloadingDeviceToolChains(*C, Inputs);
17391736

@@ -3606,81 +3603,6 @@ bool Driver::checkForOffloadStaticLib(Compilation &C,
36063603
return false;
36073604
}
36083605

3609-
// Goes through all of the arguments, including inputs expected for the
3610-
// linker directly, to determine if the targets contained in the objects and
3611-
// archives match target expectations being performed.
3612-
void Driver::checkForOffloadMismatch(Compilation &C,
3613-
DerivedArgList &Args) const {
3614-
// Check only if enabled with -fsycl
3615-
if (!Args.hasFlag(options::OPT_fsycl, options::OPT_fno_sycl, false))
3616-
return;
3617-
3618-
SmallVector<const char *, 16> OffloadLibArgs(getLinkerArgs(C, Args, true));
3619-
// Gather all of the sections seen in the offload objects/archives
3620-
SmallVector<std::string, 4> UniqueSections;
3621-
for (StringRef OLArg : OffloadLibArgs) {
3622-
SmallVector<std::string, 4> Sections(getOffloadSections(C, OLArg));
3623-
for (auto Section : Sections) {
3624-
// We only care about sections that start with 'sycl-'. Also remove
3625-
// the prefix before adding it.
3626-
std::string Prefix("sycl-");
3627-
if (Section.compare(0, Prefix.length(), Prefix) != 0)
3628-
continue;
3629-
std::string Arch = Section.substr(Prefix.length());
3630-
// There are a few different variants for FPGA, if we see one, just
3631-
// use the default FPGA triple to reduce possible match confusion.
3632-
if (Arch.compare(0, 4, "fpga") == 0)
3633-
Arch = C.getDriver().MakeSYCLDeviceTriple("spir64_fpga").str();
3634-
if (std::find(UniqueSections.begin(), UniqueSections.end(), Arch) ==
3635-
UniqueSections.end())
3636-
UniqueSections.push_back(Arch);
3637-
}
3638-
}
3639-
3640-
if (!UniqueSections.size())
3641-
return;
3642-
3643-
// Put together list of user defined and implied targets, we will diagnose
3644-
// each target individually.
3645-
SmallVector<StringRef, 4> Targets;
3646-
if (const Arg *A = Args.getLastArg(options::OPT_fsycl_targets_EQ)) {
3647-
for (StringRef Val : A->getValues()) {
3648-
if (auto ValidDevice = isIntelGPUTarget(Val)) {
3649-
if (!ValidDevice->empty())
3650-
Targets.push_back(Args.MakeArgString(
3651-
C.getDriver().MakeSYCLDeviceTriple("spir64_gen").str() + "-" +
3652-
*ValidDevice));
3653-
continue;
3654-
}
3655-
Targets.push_back(Val);
3656-
}
3657-
} else { // Implied targets
3658-
// No -fsycl-targets given, check based on -fintelfpga or default device
3659-
bool SYCLfpga = C.getInputArgs().hasArg(options::OPT_fintelfpga);
3660-
// -fsycl -fintelfpga implies spir64_fpga
3661-
Targets.push_back(SYCLfpga ? "spir64_fpga" : getDefaultSYCLArch(C));
3662-
}
3663-
3664-
for (auto SyclTarget : Targets) {
3665-
// Match found sections with user and implied targets.
3666-
llvm::Triple TT(C.getDriver().MakeSYCLDeviceTriple(SyclTarget));
3667-
// If any matching section is found, we are good.
3668-
if (std::find(UniqueSections.begin(), UniqueSections.end(), TT.str()) !=
3669-
UniqueSections.end())
3670-
continue;
3671-
// Didn't find any matches, return the full list for the diagnostic.
3672-
SmallString<128> ArchListStr;
3673-
int Cnt = 0;
3674-
for (std::string Section : UniqueSections) {
3675-
if (Cnt)
3676-
ArchListStr += ", ";
3677-
ArchListStr += Section;
3678-
Cnt++;
3679-
}
3680-
Diag(diag::warn_drv_sycl_target_missing) << SyclTarget << ArchListStr;
3681-
}
3682-
}
3683-
36843606
/// Check whether the given input tree contains any clang-offload-dependency
36853607
/// actions.
36863608
static bool ContainsOffloadDepsAction(const Action *A) {
@@ -5718,6 +5640,68 @@ class OffloadingActionBuilder final {
57185640
return false;
57195641
}
57205642

5643+
// Goes through all of the arguments, including inputs expected for the
5644+
// linker directly, to determine if the targets contained in the objects and
5645+
// archives match target expectations being performed.
5646+
void
5647+
checkForOffloadMismatch(Compilation &C, DerivedArgList &Args,
5648+
SmallVector<DeviceTargetInfo, 4> &Targets) const {
5649+
if (Targets.empty())
5650+
return;
5651+
5652+
SmallVector<const char *, 16> OffloadLibArgs(
5653+
getLinkerArgs(C, Args, true));
5654+
// Gather all of the sections seen in the offload objects/archives
5655+
SmallVector<std::string, 4> UniqueSections;
5656+
for (StringRef OLArg : OffloadLibArgs) {
5657+
SmallVector<std::string, 4> Sections(getOffloadSections(C, OLArg));
5658+
for (auto Section : Sections) {
5659+
// We only care about sections that start with 'sycl-'. Also remove
5660+
// the prefix before adding it.
5661+
std::string Prefix("sycl-");
5662+
if (Section.compare(0, Prefix.length(), Prefix) != 0)
5663+
continue;
5664+
5665+
std::string Arch = Section.substr(Prefix.length());
5666+
5667+
// There are a few different variants for FPGA, if we see one, just
5668+
// use the default FPGA triple to reduce possible match confusion.
5669+
if (Arch.compare(0, 4, "fpga") == 0)
5670+
Arch = C.getDriver().MakeSYCLDeviceTriple("spir64_fpga").str();
5671+
if (std::find(UniqueSections.begin(), UniqueSections.end(), Arch) ==
5672+
UniqueSections.end())
5673+
UniqueSections.push_back(Arch);
5674+
}
5675+
}
5676+
5677+
if (!UniqueSections.size())
5678+
return;
5679+
5680+
for (auto SyclTarget : Targets) {
5681+
std::string SectionTriple = SyclTarget.TC->getTriple().str();
5682+
if (SyclTarget.BoundArch) {
5683+
SectionTriple += "-";
5684+
SectionTriple += SyclTarget.BoundArch;
5685+
}
5686+
5687+
// If any matching section is found, we are good.
5688+
if (std::find(UniqueSections.begin(), UniqueSections.end(),
5689+
SectionTriple) != UniqueSections.end())
5690+
continue;
5691+
// Didn't find any matches, return the full list for the diagnostic.
5692+
SmallString<128> ArchListStr;
5693+
int Cnt = 0;
5694+
for (std::string Section : UniqueSections) {
5695+
if (Cnt)
5696+
ArchListStr += ", ";
5697+
ArchListStr += Section;
5698+
Cnt++;
5699+
}
5700+
C.getDriver().Diag(diag::warn_drv_sycl_target_missing)
5701+
<< SectionTriple << ArchListStr;
5702+
}
5703+
}
5704+
57215705
bool initialize() override {
57225706
// Get the SYCL toolchains. If we don't get any, the action builder will
57235707
// know there is nothing to do related to SYCL offloading.
@@ -5914,6 +5898,9 @@ class OffloadingActionBuilder final {
59145898
const auto *TC = ToolChains.front();
59155899
SYCLTargetInfoList.emplace_back(TC, nullptr);
59165900
}
5901+
5902+
checkForOffloadMismatch(C, Args, SYCLTargetInfoList);
5903+
59175904
DeviceLinkerInputs.resize(SYCLTargetInfoList.size());
59185905
return false;
59195906
}
3.73 KB
Binary file not shown.
3.71 KB
Binary file not shown.
3.52 KB
Binary file not shown.
3.49 KB
Binary file not shown.

clang/test/Driver/sycl-target-mismatch.cpp

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// RUN: %clangxx -fsycl -fsycl-targets=spir64_gen %S/Inputs/SYCL/objlin64.o \
99
// RUN: -### %s 2>&1 \
1010
// RUN: | FileCheck %s -check-prefix=SPIR64_GEN_DIAG
11-
// SPIR64_GEN_DIAG: linked binaries do not contain expected 'spir64_gen' target; found targets: 'spir64-unknown-unknown{{.*}}, spir64-unknown-unknown{{.*}}' [-Wsycl-target]
11+
// SPIR64_GEN_DIAG: linked binaries do not contain expected 'spir64_gen-unknown-unknown' target; found targets: 'spir64-unknown-unknown{{.*}}, spir64-unknown-unknown{{.*}}' [-Wsycl-target]
1212

1313
// RUN: %clangxx -fsycl -fsycl-targets=spir64 %S/Inputs/SYCL/liblin64.a \
1414
// RUN: -### %s 2>&1 \
@@ -23,3 +23,56 @@
2323
// RUN: -Wno-sycl-target -### %s 2>&1 \
2424
// RUN: | FileCheck %s -check-prefix=SPIR64_DIAG
2525
// SPIR64_DIAG-NOT: linked binaries do not contain expected
26+
27+
// RUN: %clangxx -fsycl -fsycl-targets=nvptx64-nvidia-cuda -Xsycl-target-backend --cuda-gpu-arch=sm_60 \
28+
// RUN: %S/Inputs/SYCL/libnvptx64-sm_50.a -### %s 2>&1 \
29+
// RUN: | FileCheck %s -check-prefix=NVPTX64_DIAG
30+
// RUN: %clangxx -fsycl -fsycl-targets=nvptx64-nvidia-cuda -Xsycl-target-backend --cuda-gpu-arch=sm_60 \
31+
// RUN: -L%S/Inputs/SYCL -lnvptx64-sm_50 -### %s 2>&1 \
32+
// RUN: | FileCheck %s -check-prefix=NVPTX64_DIAG
33+
// RUN: %clangxx -fsycl -fsycl-targets=nvptx64-nvidia-cuda -Xsycl-target-backend --cuda-gpu-arch=sm_60 \
34+
// RUN: %S/Inputs/SYCL/objnvptx64-sm_50.o -### %s 2>&1 \
35+
// RUN: | FileCheck %s -check-prefix=NVPTX64_DIAG
36+
// NVPTX64_DIAG: linked binaries do not contain expected 'nvptx64-nvidia-cuda-sm_60' target; found targets: 'nvptx64-nvidia-cuda-sm_50' [-Wsycl-target]
37+
38+
// RUN: %clangxx -fsycl -fsycl-targets=nvptx64-nvidia-cuda -Xsycl-target-backend --cuda-gpu-arch=sm_50 \
39+
// RUN: %S/Inputs/SYCL/libnvptx64-sm_50.a -### %s 2>&1 \
40+
// RUN: | FileCheck %s -check-prefix=NVPTX64_MATCH_DIAG
41+
// RUN: %clangxx -fsycl -fsycl-targets=nvptx64-nvidia-cuda -Xsycl-target-backend --cuda-gpu-arch=sm_50 \
42+
// RUN: -L%S/Inputs/SYCL -lnvptx64-sm_50 -### %s 2>&1 \
43+
// RUN: | FileCheck %s -check-prefix=NVPTX64_MATCH_DIAG
44+
// RUN: %clangxx -fsycl -fsycl-targets=nvptx64-nvidia-cuda -Xsycl-target-backend --cuda-gpu-arch=sm_50 \
45+
// RUN: %S/Inputs/SYCL/objnvptx64-sm_50.o -### %s 2>&1 \
46+
// RUN: | FileCheck %s -check-prefix=NVPTX64_MATCH_DIAG
47+
// RUN: %clangxx -fsycl -fsycl-targets=nvptx64-nvidia-cuda \
48+
// RUN: %S/Inputs/SYCL/objnvptx64-sm_50.o -### %s 2>&1 \
49+
// RUN: | FileCheck %s -check-prefix=NVPTX64_MATCH_DIAG
50+
// RUN: %clangxx -fsycl -fsycl-targets=nvptx64-nvidia-cuda -Xsycl-target-backend --cuda-gpu-arch=sm_60 \
51+
// RUN: -Wno-sycl-target %S/Inputs/SYCL/objnvptx64-sm_50.o -### %s 2>&1 \
52+
// RUN: | FileCheck %s -check-prefix=NVPTX64_MATCH_DIAG
53+
// NVPTX64_MATCH_DIAG-NOT: linked binaries do not contain expected
54+
55+
// RUN: %clangxx -fsycl -fsycl-targets=amdgcn-amd-amdhsa -Xsycl-target-backend --offload-arch=gfx906 \
56+
// RUN: %S/Inputs/SYCL/libamdgcn-gfx908.a -### %s 2>&1 \
57+
// RUN: | FileCheck %s -check-prefix=AMDGCN_DIAG
58+
// RUN: %clangxx -fsycl -fsycl-targets=amdgcn-amd-amdhsa -Xsycl-target-backend --offload-arch=gfx906 \
59+
// RUN: -L%S/Inputs/SYCL -lamdgcn-gfx908 -### %s 2>&1 \
60+
// RUN: | FileCheck %s -check-prefix=AMDGCN_DIAG
61+
// RUN: %clangxx -fsycl -fsycl-targets=amdgcn-amd-amdhsa -Xsycl-target-backend --offload-arch=gfx906 \
62+
// RUN: %S/Inputs/SYCL/objamdgcn-gfx908.o -### %s 2>&1 \
63+
// RUN: | FileCheck %s -check-prefix=AMDGCN_DIAG
64+
// AMDGCN_DIAG: linked binaries do not contain expected 'amdgcn-amd-amdhsa-gfx906' target; found targets: 'amdgcn-amd-amdhsa-gfx908' [-Wsycl-target]
65+
66+
// RUN: %clangxx -fsycl -fsycl-targets=amdgcn-amd-amdhsa -Xsycl-target-backend --offload-arch=gfx908 \
67+
// RUN: %S/Inputs/SYCL/libamdgcn-gfx908.a -### %s 2>&1 \
68+
// RUN: | FileCheck %s -check-prefix=AMDGCN_MATCH_DIAG
69+
// RUN: %clangxx -fsycl -fsycl-targets=amdgcn-amd-amdhsa -Xsycl-target-backend --offload-arch=gfx908 \
70+
// RUN: -L%S/Inputs/SYCL -lamdgcn-gfx908 -### %s 2>&1 \
71+
// RUN: | FileCheck %s -check-prefix=AMDGCN_MATCH_DIAG
72+
// RUN: %clangxx -fsycl -fsycl-targets=amdgcn-amd-amdhsa -Xsycl-target-backend --offload-arch=gfx908 \
73+
// RUN: %S/Inputs/SYCL/objamdgcn-gfx908.o -### %s 2>&1 \
74+
// RUN: | FileCheck %s -check-prefix=AMDGCN_MATCH_DIAG
75+
// RUN: %clangxx -fsycl -fsycl-targets=amdgcn-amd-amdhsa -Xsycl-target-backend --offload-arch=gfx906 \
76+
// RUN: -Wno-sycl-target %S/Inputs/SYCL/objamdgcn-gfx908.o -### %s 2>&1 \
77+
// RUN: | FileCheck %s -check-prefix=AMDGCN_MATCH_DIAG
78+
// AMDGCN_MATCH_DIAG-NOT: linked binaries do not contain expected

0 commit comments

Comments
 (0)