Skip to content

Commit

Permalink
[OpenMP] Fix using the target ID when using the new driver
Browse files Browse the repository at this point in the history
AMDGPU sometimes uses a novel formatting for their offloading
architecture called the target id. This merges the attributes and the
architecture name into a single string. Previously, we were passing this
as the canonical architecture name. This caused the linker wrapper to
fail to find relevant libraries and then pass an incalid CPU name. This
patch changes the handling in the offload packager to handle the
canonical architecture and then extract the features.

Reviewed By: yaxunl

Differential Revision: https://reviews.llvm.org/D150998
  • Loading branch information
jhuber6 committed Jun 8, 2023
1 parent 9f00eb9 commit dc81d2a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
16 changes: 13 additions & 3 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8403,7 +8403,7 @@ void OffloadPackager::ConstructJob(Compilation &C, const JobAction &JA,
C.getArgsForToolChain(TC, OffloadAction->getOffloadingArch(),
OffloadAction->getOffloadingDeviceKind());
StringRef File = C.getArgs().MakeArgString(TC->getInputFilename(Input));
StringRef Arch = (OffloadAction->getOffloadingArch())
StringRef Arch = OffloadAction->getOffloadingArch()
? OffloadAction->getOffloadingArch()
: TCArgs.getLastArgValue(options::OPT_march_EQ);
StringRef Kind =
Expand All @@ -8416,14 +8416,24 @@ void OffloadPackager::ConstructJob(Compilation &C, const JobAction &JA,
llvm::copy_if(Features, std::back_inserter(FeatureArgs),
[](StringRef Arg) { return !Arg.startswith("-target"); });

if (TC->getTriple().isAMDGPU()) {
for (StringRef Feature : llvm::split(Arch.split(':').second, ':')) {
FeatureArgs.emplace_back(
Args.MakeArgString(Feature.take_back() + Feature.drop_back()));
}
}

// TODO: We need to pass in the full target-id and handle it properly in the
// linker wrapper.
SmallVector<std::string> Parts{
"file=" + File.str(),
"triple=" + TC->getTripleString(),
"arch=" + Arch.str(),
"arch=" + getProcessorFromTargetID(TC->getTriple(), Arch).str(),
"kind=" + Kind.str(),
};

if (TC->getDriver().isUsingLTO(/* IsOffload */ true))
if (TC->getDriver().isUsingLTO(/* IsOffload */ true) ||
TC->getTriple().isAMDGPU())
for (StringRef Feature : FeatureArgs)
Parts.emplace_back("feature=" + Feature.str());

Expand Down
4 changes: 4 additions & 0 deletions clang/test/Driver/amdgpu-openmp-toolchain.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,7 @@
// RUN: --rocm-device-lib-path=%S/Inputs/rocm/amdgcn/bitcode -fopenmp-new-driver %s 2>&1 | \
// RUN: FileCheck %s --check-prefix=CHECK-LIB-DEVICE-NOGPULIB
// CHECK-LIB-DEVICE-NOGPULIB-NOT: "-cc1" {{.*}}ocml.bc"{{.*}}ockl.bc"{{.*}}oclc_daz_opt_on.bc"{{.*}}oclc_unsafe_math_off.bc"{{.*}}oclc_finite_only_off.bc"{{.*}}oclc_correctly_rounded_sqrt_on.bc"{{.*}}oclc_wavefrontsize64_on.bc"{{.*}}oclc_isa_version_803.bc"

// RUN: %clang -### -target x86_64-pc-linux-gnu -fopenmp --offload-arch=gfx90a:sramecc-:xnack+ \
// RUN: -nogpulib %s 2>&1 | FileCheck %s --check-prefix=CHECK-TARGET-ID
// CHECK-TARGET-ID: clang-offload-packager{{.*}}arch=gfx90a,kind=openmp,feature=-sramecc,feature=+xnack

0 comments on commit dc81d2a

Please sign in to comment.