Skip to content

Commit

Permalink
[Driver][SYCL][NewOffloadModel] Improve arch association for device
Browse files Browse the repository at this point in the history
When using the new offloading model, the values being passed to
-fsycl-targets were not fully being realized when attempting to target
specific architectures.  Uses of -fsycl-targets=intel_gpu_* were not
properly setting the arch values that are used down the line (namely the
packaging step).

Improve this situation by populating the assocated architecture mappings
with what is seen when parsing the -fsycl-targets option.  This applies
to all intel_gpu, nvidia_gpu and amd_gpu targets.
  • Loading branch information
mdtoguchi committed May 23, 2024
1 parent f739a6d commit 1622e6f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
12 changes: 11 additions & 1 deletion clang/lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,7 @@ void Driver::CreateOffloadingDeviceToolChains(Compilation &C,
}
bool HasSYCLTargetsOption = SYCLTargets;

llvm::StringMap<llvm::DenseSet<StringRef>> DerivedArchs;
llvm::StringMap<StringRef> FoundNormalizedTriples;
llvm::SmallVector<llvm::Triple, 4> UniqueSYCLTriplesVec;
if (HasSYCLTargetsOption) {
Expand All @@ -1192,24 +1193,28 @@ void Driver::CreateOffloadingDeviceToolChains(Compilation &C,
<< SYCLForceTarget->getAsString(C.getInputArgs());

for (StringRef Val : SYCLTargetsValues->getValues()) {
StringRef Arch;
StringRef UserTargetName(Val);
if (auto Device = gen::isGPUTarget<gen::IntelGPU>(Val)) {
if (Device->empty()) {
Diag(clang::diag::err_drv_invalid_sycl_target) << Val;
continue;
}
Arch = Device->data();
UserTargetName = "spir64_gen";
} else if (auto Device = gen::isGPUTarget<gen::NvidiaGPU>(Val)) {
if (Device->empty()) {
Diag(clang::diag::err_drv_invalid_sycl_target) << Val;
continue;
}
Arch = Device->data();
UserTargetName = "nvptx64-nvidia-cuda";
} else if (auto Device = gen::isGPUTarget<gen::AmdGPU>(Val)) {
if (Device->empty()) {
Diag(clang::diag::err_drv_invalid_sycl_target) << Val;
continue;
}
Arch = Device->data();
UserTargetName = "amdgcn-amd-amdhsa";
} else if (Val == "native_cpu") {
const ToolChain *HostTC =
Expand All @@ -1236,7 +1241,10 @@ void Driver::CreateOffloadingDeviceToolChains(Compilation &C,
// Store the current triple so that we can check for duplicates in
// the following iterations.
FoundNormalizedTriples[NormalizedName] = Val;
UniqueSYCLTriplesVec.push_back(MakeSYCLDeviceTriple(UserTargetName));
llvm::Triple DeviceTriple(MakeSYCLDeviceTriple(UserTargetName));
UniqueSYCLTriplesVec.push_back(DeviceTriple);
if (!Arch.empty())
DerivedArchs[DeviceTriple.getTriple()].insert(Arch);
}
addSYCLDefaultTriple(C, UniqueSYCLTriplesVec);
} else
Expand Down Expand Up @@ -1277,6 +1285,8 @@ void Driver::CreateOffloadingDeviceToolChains(Compilation &C,
auto SYCLTC = &getOffloadingDeviceToolChain(C.getInputArgs(), TT, *HostTC,
Action::OFK_SYCL);
C.addOffloadDeviceToolChain(SYCLTC, Action::OFK_SYCL);
if (DerivedArchs.contains(TT.getTriple()))
KnownArchs[SYCLTC] = DerivedArchs[TT.getTriple()];
}

//
Expand Down
23 changes: 23 additions & 0 deletions clang/test/Driver/sycl-offload-new-driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,26 @@
// RUN: -shared %s 2>&1 \
// RUN: | FileCheck -check-prefix=CHECK_SHARED %s
// CHECK_SHARED: clang-linker-wrapper{{.*}} "-shared"

// Verify 'arch' offload-packager values for known targets
// RUN: %clangxx -### --target=x86_64-unknown-linux-gnu -fsycl \
// RUN: -fsycl-targets=spir64 --offload-new-driver %s 2>&1 \
// RUN: | FileCheck -check-prefix=CHK_ARCH \
// RUN: -DTRIPLE=spir64-unknown-unknown -DARCH= %s
// RUN: %clangxx -### --target=x86_64-unknown-linux-gnu -fsycl \
// RUN: -fsycl-targets=intel_gpu_pvc --offload-new-driver %s 2>&1 \
// RUN: | FileCheck -check-prefix=CHK_ARCH \
// RUN: -DTRIPLE=spir64_gen-unknown-unknown -DARCH=pvc %s
// RUN: %clangxx -### --target=x86_64-unknown-linux-gnu -fsycl \
// RUN: -fno-sycl-libspirv -fsycl-targets=amd_gpu_gfx900 \
// RUN: -nogpulib --offload-new-driver %s 2>&1 \
// RUN: | FileCheck -check-prefix=CHK_ARCH \
// RUN: -DTRIPLE=amdgcn-amd-amdhsa -DARCH=gfx900 %s
// RUN: %clangxx -### --target=x86_64-unknown-linux-gnu -fsycl \
// RUN: -fno-sycl-libspirv -fsycl-targets=nvidia_gpu_sm_50 \
// RUN: -nogpulib --offload-new-driver %s 2>&1 \
// RUN: | FileCheck -check-prefix=CHK_ARCH \
// RUN: -DTRIPLE=nvptx64-nvidia-cuda -DARCH=sm_50 %s
// CHK_ARCH: clang{{.*}} "-triple" "[[TRIPLE]]"
// CHK_ARCH-SAME: "-fsycl-is-device" {{.*}} "--offload-new-driver"{{.*}} "-o" "[[CC1DEVOUT:.+\.bc]]"
// CHK_ARCH-NEXT: clang-offload-packager{{.*}} "--image=file=[[CC1DEVOUT]],triple=[[TRIPLE]],arch=[[ARCH]],kind=sycl"

0 comments on commit 1622e6f

Please sign in to comment.