From aff88dea12baa1a9ec4afda82021717a0d7e6c68 Mon Sep 17 00:00:00 2001 From: Joseph Huber Date: Tue, 29 Jul 2025 16:34:38 -0500 Subject: [PATCH] [HIP] Handle `-fhip-emit-reloctable` in the new driver Summary: Support for this was missing, here it pretty much overrides the normal bundling behavior and also requires a few errors to be emitted. --- clang/lib/Driver/Driver.cpp | 19 +++++++++++++++++-- clang/test/Driver/hip-dependent-options.hip | 14 +++++++++++++- clang/test/Driver/hip-phases.hip | 8 +++++++- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index 99de9519f3697..586f287843f3e 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -3606,7 +3606,7 @@ class OffloadingActionBuilder final { if (!CompileDeviceOnly) { C.getDriver().Diag(diag::err_opt_not_valid_without_opt) << "-fhip-emit-relocatable" - << "--cuda-device-only"; + << "--offload-device-only"; } } } @@ -4774,6 +4774,21 @@ Action *Driver::BuildOffloadingActions(Compilation &C, C.isOffloadingHostKind(Action::OFK_HIP) && !Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc, false); + bool HIPRelocatableObj = + C.isOffloadingHostKind(Action::OFK_HIP) && + Args.hasFlag(options::OPT_fhip_emit_relocatable, + options::OPT_fno_hip_emit_relocatable, false); + + if (!HIPNoRDC && HIPRelocatableObj) + C.getDriver().Diag(diag::err_opt_not_valid_with_opt) + << "-fhip-emit-relocatable" + << "-fgpu-rdc"; + + if (!offloadDeviceOnly() && HIPRelocatableObj) + C.getDriver().Diag(diag::err_opt_not_valid_without_opt) + << "-fhip-emit-relocatable" + << "--offload-device-only"; + // For HIP non-rdc non-device-only compilation, create a linker wrapper // action for each host object to link, bundle and wrap device files in // it. @@ -4894,7 +4909,7 @@ Action *Driver::BuildOffloadingActions(Compilation &C, A->getOffloadingToolChain()->getTriple().isSPIRV(); if ((A->getType() != types::TY_Object && !IsAMDGCNSPIRV && A->getType() != types::TY_LTO_BC) || - !HIPNoRDC || !offloadDeviceOnly()) + HIPRelocatableObj || !HIPNoRDC || !offloadDeviceOnly()) continue; ActionList LinkerInput = {A}; A = C.MakeAction(LinkerInput, types::TY_Image); diff --git a/clang/test/Driver/hip-dependent-options.hip b/clang/test/Driver/hip-dependent-options.hip index b0dc7f289e813..e65fec2b08d7b 100644 --- a/clang/test/Driver/hip-dependent-options.hip +++ b/clang/test/Driver/hip-dependent-options.hip @@ -4,6 +4,12 @@ // RUN: %S/Inputs/hip_multiple_inputs/a.cu \ // RUN: %S/Inputs/hip_multiple_inputs/b.hip --gpu-bundle-output \ // RUN: 2>&1 | FileCheck -check-prefixes=RELOCRDC %s +// RUN: not %clang -### --target=x86_64-linux-gnu --offload-new-driver \ +// RUN: -x hip --cuda-gpu-arch=gfx803 --cuda-gpu-arch=gfx900 \ +// RUN: -c -fhip-emit-relocatable -nogpuinc -nogpulib --cuda-device-only -fgpu-rdc \ +// RUN: %S/Inputs/hip_multiple_inputs/a.cu \ +// RUN: %S/Inputs/hip_multiple_inputs/b.hip --gpu-bundle-output \ +// RUN: 2>&1 | FileCheck -check-prefixes=RELOCRDC %s // RELOCRDC: error: option '-fhip-emit-relocatable' cannot be specified with '-fgpu-rdc' @@ -13,5 +19,11 @@ // RUN: %S/Inputs/hip_multiple_inputs/a.cu \ // RUN: %S/Inputs/hip_multiple_inputs/b.hip --gpu-bundle-output \ // RUN: 2>&1 | FileCheck -check-prefixes=RELOCHOST %s +// RUN: not %clang -### --target=x86_64-linux-gnu --offload-new-driver \ +// RUN: -x hip --cuda-gpu-arch=gfx803 --cuda-gpu-arch=gfx900 \ +// RUN: -c -fhip-emit-relocatable -nogpuinc -nogpulib \ +// RUN: %S/Inputs/hip_multiple_inputs/a.cu \ +// RUN: %S/Inputs/hip_multiple_inputs/b.hip --gpu-bundle-output \ +// RUN: 2>&1 | FileCheck -check-prefixes=RELOCHOST %s -// RELOCHOST: error: option '-fhip-emit-relocatable' cannot be specified without '--cuda-device-only' +// RELOCHOST: error: option '-fhip-emit-relocatable' cannot be specified without '--offload-device-only' diff --git a/clang/test/Driver/hip-phases.hip b/clang/test/Driver/hip-phases.hip index 0ad5d76cf575e..6bac97ab8064b 100644 --- a/clang/test/Driver/hip-phases.hip +++ b/clang/test/Driver/hip-phases.hip @@ -275,10 +275,16 @@ // RUN: %clang -x hip --target=x86_64-unknown-linux-gnu -ccc-print-phases --no-offload-new-driver \ // RUN: --cuda-gpu-arch=gfx803 %s --cuda-device-only -fhip-emit-relocatable 2>&1 \ // RUN: | FileCheck -check-prefixes=RELOC %s +// RUN: %clang -x hip --target=x86_64-unknown-linux-gnu -ccc-print-phases --offload-new-driver \ +// RUN: --cuda-gpu-arch=gfx803 %s --cuda-device-only -fhip-emit-relocatable 2>&1 \ +// RUN: | FileCheck -check-prefixes=RELOC %s // // RUN: %clang -x hip --target=x86_64-unknown-linux-gnu -ccc-print-phases --no-offload-new-driver \ // RUN: --cuda-gpu-arch=gfx803 %s --cuda-device-only -fhip-emit-relocatable -Wl,--disable-new-dtags \ // RUN: 2>&1 | FileCheck -check-prefixes=RELOC %s +// RUN: %clang -x hip --target=x86_64-unknown-linux-gnu -ccc-print-phases --offload-new-driver \ +// RUN: --cuda-gpu-arch=gfx803 %s --cuda-device-only -fhip-emit-relocatable -Wl,--disable-new-dtags \ +// RUN: 2>&1 | FileCheck -check-prefixes=RELOC %s // // RELOC-DAG: [[P0:[0-9]+]]: input, "{{.*}}hip-phases.hip", [[T:hip]], (device-[[T]], [[ARCH:gfx803]]) // RELOC-DAG: [[P1:[0-9]+]]: preprocessor, {[[P0]]}, [[T]]-cpp-output, (device-[[T]], [[ARCH]]) @@ -286,7 +292,7 @@ // RELOC-DAG: [[P3:[0-9]+]]: backend, {[[P2]]}, assembler, (device-[[T]], [[ARCH]]) // RELOC-DAG: [[P4:[0-9]+]]: assembler, {[[P3]]}, object, (device-[[T]], [[ARCH]]) // RELOC-NOT: linker -// RELOC-DAG: [[P5:[0-9]+]]: offload, "device-[[T]] (amdgcn-amd-amdhsa:[[ARCH]])" {[[P4]]}, object +// RELOC-DAG: [[P5:[0-9]+]]: offload, "device-[[T]] (amdgcn-amd-amdhsa:[[ARCH]])" {[[P4]]} // RELOC-NOT: host //