@@ -5024,15 +5024,24 @@ Action *Driver::BuildOffloadingActions(Compilation &C,
50245024 // Compiling HIP in device-only non-RDC mode requires linking each action
50255025 // individually.
50265026 for (Action *&A : DeviceActions) {
5027- // Special handling for the HIP SPIR-V toolchain because it doesn't use
5028- // the SPIR-V backend yet doesn't report the output as an object.
50295027 bool IsAMDGCNSPIRV = A->getOffloadingToolChain () &&
50305028 A->getOffloadingToolChain ()->getTriple ().getOS () ==
50315029 llvm::Triple::OSType::AMDHSA &&
50325030 A->getOffloadingToolChain ()->getTriple ().isSPIRV ();
5031+ bool UseSPIRVBackend = Args.hasFlag (options::OPT_use_spirv_backend,
5032+ options::OPT_no_use_spirv_backend,
5033+ /* Default=*/ false );
5034+
5035+ // Special handling for the HIP SPIR-V toolchain in device-only.
5036+ // The translator path has a linking step, whereas the SPIR-V backend path
5037+ // does not to avoid any external dependency such as spirv-link. The
5038+ // linking step is skipped for the SPIR-V backend path.
5039+ bool IsAMDGCNSPIRVWithBackend = IsAMDGCNSPIRV && UseSPIRVBackend;
5040+
50335041 if ((A->getType () != types::TY_Object && !IsAMDGCNSPIRV &&
50345042 A->getType () != types::TY_LTO_BC) ||
5035- HIPRelocatableObj || !HIPNoRDC || !offloadDeviceOnly ())
5043+ HIPRelocatableObj || !HIPNoRDC || !offloadDeviceOnly () ||
5044+ (IsAMDGCNSPIRVWithBackend && offloadDeviceOnly ()))
50365045 continue ;
50375046 ActionList LinkerInput = {A};
50385047 A = C.MakeAction <LinkJobAction>(LinkerInput, types::TY_Image);
@@ -5258,12 +5267,28 @@ Action *Driver::ConstructPhaseAction(
52585267 Args.hasArg (options::OPT_S) ? types::TY_LTO_IR : types::TY_LTO_BC;
52595268 return C.MakeAction <BackendJobAction>(Input, Output);
52605269 }
5270+ bool UseSPIRVBackend = Args.hasFlag (options::OPT_use_spirv_backend,
5271+ options::OPT_no_use_spirv_backend,
5272+ /* Default=*/ false );
5273+
5274+ auto OffloadingToolChain = Input->getOffloadingToolChain ();
5275+ // For AMD SPIRV, if offloadDeviceOnly(), we call the SPIRV backend unless
5276+ // LLVM bitcode was requested explicitly or RDC is set. If
5277+ // !offloadDeviceOnly, we emit LLVM bitcode, and clang-linker-wrapper will
5278+ // compile it to SPIRV.
5279+ bool UseSPIRVBackendForHipDeviceOnlyNoRDC =
5280+ TargetDeviceOffloadKind == Action::OFK_HIP && OffloadingToolChain &&
5281+ OffloadingToolChain->getTriple ().isSPIRV () && UseSPIRVBackend &&
5282+ offloadDeviceOnly () &&
5283+ !Args.hasFlag (options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc, false );
5284+
52615285 if (Args.hasArg (options::OPT_emit_llvm) ||
52625286 TargetDeviceOffloadKind == Action::OFK_SYCL ||
52635287 (((Input->getOffloadingToolChain () &&
52645288 Input->getOffloadingToolChain ()->getTriple ().isAMDGPU () &&
52655289 TargetDeviceOffloadKind != Action::OFK_None) ||
52665290 TargetDeviceOffloadKind == Action::OFK_HIP) &&
5291+ !UseSPIRVBackendForHipDeviceOnlyNoRDC &&
52675292 ((Args.hasFlag (options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc,
52685293 false ) ||
52695294 (Args.hasFlag (options::OPT_offload_new_driver,
@@ -5285,6 +5310,19 @@ Action *Driver::ConstructPhaseAction(
52855310 : types::TY_LLVM_BC;
52865311 return C.MakeAction <BackendJobAction>(Input, Output);
52875312 }
5313+
5314+ // The SPIRV backend compilation path for HIP must avoid external
5315+ // dependencies. The default compilation path assembles and links its
5316+ // output, but the SPIRV assembler and linker are external tools. This code
5317+ // ensures the backend emits binary SPIRV directly to bypass those steps and
5318+ // avoid failures. Without -save-temps, the compiler may already skip
5319+ // assembling and linking. With -save-temps, these steps must be explicitly
5320+ // disabled, as done here. We also force skipping these steps regardless of
5321+ // -save-temps to avoid relying on optimizations (unless -S is set).
5322+ // The current HIP bundling expects the type to be types::TY_Image
5323+ if (UseSPIRVBackendForHipDeviceOnlyNoRDC && !Args.hasArg (options::OPT_S))
5324+ return C.MakeAction <BackendJobAction>(Input, types::TY_Image);
5325+
52885326 return C.MakeAction <BackendJobAction>(Input, types::TY_PP_Asm);
52895327 }
52905328 case phases::Assemble:
0 commit comments