Skip to content

[SYCL] Extend -fsycl-device-obj to dump assembly #17390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions clang/include/clang/Basic/DiagnosticDriverKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,10 @@ def err_drv_fsycl_wrong_optimization_options : Error<
def warn_drv_fsycl_add_default_spec_consts_image_flag_in_non_AOT : Warning<
"-fsycl-add-default-spec-consts-image flag has an effect only in Ahead of Time Compilation mode (AOT)">,
InGroup<SyclTarget>;
def warn_drv_fsycl_device_obj_asm_device_only : Warning<
"-fsycl-device-obj=asm flag has an effect only when compiling device code "
"and emitting assembly, make sure both -fsycl-device-only and -S flags are "
"present; will be ignored">, InGroup<UnusedCommandLineArgument>;
def warn_drv_ftarget_register_alloc_mode_pvc : Warning<
"using '%0' to set GRF mode on PVC hardware is deprecated; use '-ftarget-register-alloc-mode=pvc:%1'">,
InGroup<Deprecated>;
Expand Down
4 changes: 2 additions & 2 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -7051,8 +7051,8 @@ defm sycl_id_queries_fit_in_int: BoolFOption<"sycl-id-queries-fit-in-int",
BothFlags<[], [ClangOption, CLOption, CC1Option], " that SYCL ID queries fit "
"within MAX_INT.">>;
def fsycl_device_obj_EQ : Joined<["-"], "fsycl-device-obj=">,
Values<"spirv,llvmir">, HelpText<"Specify format of device code stored in "
"the resulting object. Valid values are: spirv, llvmir (default)">;
Values<"spirv,llvmir,asm">, HelpText<"Specify format of device code stored "
"in the resulting object. Valid values are: spirv, asm, llvmir (default)">;
def fsycl_use_bitcode : Flag<["-"], "fsycl-use-bitcode">,
Alias<fsycl_device_obj_EQ>, AliasArgs<["llvmir"]>, Flags<[Deprecated]>,
HelpText<"Use LLVM bitcode instead of SPIR-V in fat objects (deprecated)">;
Expand Down
20 changes: 17 additions & 3 deletions clang/lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1297,17 +1297,21 @@ void Driver::CreateOffloadingDeviceToolChains(Compilation &C,
C.getInputArgs().getLastArg(options::OPT_fsycl_range_rounding_EQ);
checkSingleArgValidity(RangeRoundingPreference, {"disable", "force", "on"});

// Evaluation of -fsycl-device-obj is slightly different, we will emit
// a warning and inform the user of the default behavior used.
// Evaluation of -fsycl-device-obj is slightly different, we will emit a
// warning and inform the user of the default behavior used.
// TODO: General usage of this option is to check for 'spirv' and fallthrough
// to using llvmir. This can be improved to be more obvious in usage.
if (Arg *DeviceObj = C.getInputArgs().getLastArgNoClaim(
options::OPT_fsycl_device_obj_EQ)) {
const bool SYCLDeviceOnly = C.getDriver().offloadDeviceOnly();
const bool EmitAsm = C.getInputArgs().getLastArgNoClaim(options::OPT_S);
StringRef ArgValue(DeviceObj->getValue());
SmallVector<StringRef, 2> DeviceObjValues = {"spirv", "llvmir"};
SmallVector<StringRef, 3> DeviceObjValues = {"spirv", "llvmir", "asm"};
if (llvm::find(DeviceObjValues, ArgValue) == DeviceObjValues.end())
Diag(clang::diag::warn_ignoring_value_using_default)
<< DeviceObj->getSpelling().split('=').first << ArgValue << "llvmir";
else if (ArgValue == "asm" && (!SYCLDeviceOnly || !EmitAsm))
Diag(clang::diag::warn_drv_fsycl_device_obj_asm_device_only);
}

Arg *SYCLForceTarget =
Expand Down Expand Up @@ -1542,6 +1546,7 @@ void Driver::CreateOffloadingDeviceToolChains(Compilation &C,
addSYCLDefaultTriple(C, UniqueSYCLTriplesVec);
}
}

// -fno-sycl-libspirv flag is reserved for very unusual cases where the
// libspirv library is not linked when using CUDA/HIP: so output appropriate
// warnings.
Expand Down Expand Up @@ -5320,6 +5325,15 @@ class OffloadingActionBuilder final {
types::TY_SPIRV);
if (SYCLDeviceOnly)
continue;
} else if (SYCLDeviceOnly && Args.hasArg(options::OPT_S) &&
Args.getLastArgValue(options::OPT_fsycl_device_obj_EQ)
.equals_insensitive("asm")) {
auto *CompileAction =
C.MakeAction<CompileJobAction>(A, types::TY_LLVM_BC);
A = C.MakeAction<BackendJobAction>(CompileAction, types::TY_PP_Asm);

if (SYCLDeviceOnly)
continue;
} else {
if (Args.hasArg(options::OPT_fsyntax_only))
OutputType = types::TY_Nothing;
Expand Down
39 changes: 39 additions & 0 deletions clang/test/Driver/sycl-device-obj-asm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
///
/// Check that we call into backend assembler, when using `asm` as device
/// object format, namely:
/// `backend, {2}, assembler, (device-sycl, ...)`

// REQUIRES: nvptx-registered-target,amdgpu-registered-target

/// Check -fsycl-device-obj=asm for AMD.
// RUN: %clang -fsycl-device-only -fsycl -fsycl-targets=amdgcn-amd-amdhsa -Xsycl-target-backend=amdgcn-amd-amdhsa --offload-arch=gfx90a -fsycl-device-obj=asm -S %s 2>&1 -ccc-print-phases -o - | FileCheck %s --check-prefix=CHECK-AMD
// CHECK-AMD: 0: input, "{{.+\.cpp}}", c++, (device-sycl, gfx90a)
// CHECK-AMD: 1: preprocessor, {0}, c++-cpp-output, (device-sycl, gfx90a)
// CHECK-AMD: 2: compiler, {1}, ir, (device-sycl, gfx90a)
// CHECK-AMD: 3: backend, {2}, assembler, (device-sycl, gfx90a)
// CHECK-AMD: 4: offload, "device-sycl (amdgcn-amd-amdhsa:gfx90a)" {3}, assembler

/// Check -fsycl-device-obj=asm for Nvidia.
// RUN: %clang -fsycl-device-only -fsycl -fsycl-targets=nvptx64-nvidia-cuda -Xsycl-target-backend=nvptx64-nvidia-cuda --cuda-gpu-arch=sm_50 -fsycl-device-obj=asm -S %s 2>&1 -ccc-print-phases -o - | FileCheck %s --check-prefix=CHECK-PTX
// CHECK-PTX: 0: input, "{{.+\.cpp}}", c++, (device-sycl, sm_50)
// CHECK-PTX: 1: preprocessor, {0}, c++-cpp-output, (device-sycl, sm_50)
// CHECK-PTX: 2: compiler, {1}, ir, (device-sycl, sm_50)
// CHECK-PTX: 3: backend, {2}, assembler, (device-sycl, sm_50)
// CHECK-PTX: 4: offload, "device-sycl (nvptx64-nvidia-cuda:sm_50)" {3}, assembler

/// Check -fsycl-device-obj option when emitting llvm IR.
// RUN: %clang -fsycl-device-only -fsycl -fsycl-targets=nvptx64-nvidia-cuda -Xsycl-target-backend=nvptx64-nvidia-cuda --cuda-gpu-arch=sm_50 -fsycl-device-obj=llvmir -S %s 2>&1 -ccc-print-phases -o - | FileCheck %s --check-prefix=CHECK-LLVMIR
// CHECK-LLVMIR: 0: input, "{{.+\.cpp}}", c++, (device-sycl, sm_50)
// CHECK-LLVMIR: 1: preprocessor, {0}, c++-cpp-output, (device-sycl, sm_50)
// CHECK-LLVMIR: 2: compiler, {1}, ir, (device-sycl, sm_50)
// CHECK-LLVMIR: 3: offload, "device-sycl (nvptx64-nvidia-cuda:sm_50)" {2}, ir

/// -fsycl-device-obj=asm should always be accompanied by -fsycl-device-only
/// and -S, check that the compiler issues a correct warning message:
// RUN: %clang -nocudalib -fsycl-device-only -fsycl -fsycl-targets=nvptx64-nvidia-cuda -Xsycl-target-backend=nvptx64-nvidia-cuda --cuda-gpu-arch=sm_50 -fsycl-device-obj=asm %s 2>&1 -o - | FileCheck %s --check-prefix=CHECK-NO-DEV-ONLY-NO-S
// CHECK-NO-DEV-ONLY-NO-S: warning: -fsycl-device-obj=asm flag has an effect only when compiling device code and emitting assembly, make sure both -fsycl-device-only and -S flags are present; will be ignored [-Wunused-command-line-argument]

/// -fsycl-device-obj=asm will finish at generating assembly stage, hence
/// inform users that generating library will not be possible (ignore -c)
// RUN: %clang -nocudalib -fsycl-device-only -fsycl -fsycl-targets=nvptx64-nvidia-cuda -Xsycl-target-backend=nvptx64-nvidia-cuda --cuda-gpu-arch=sm_50 -fsycl-device-obj=asm %s 2>&1 -fsycl-device-only -S -c -o - | FileCheck %s --check-prefix=CHECK-DASH-C-IGNORE
// CHECK-DASH-C-IGNORE: warning: argument unused during compilation: '-c' [-Wunused-command-line-argument]
3 changes: 2 additions & 1 deletion sycl/doc/UsersManual.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,8 @@ and not recommended to use in production environment.
**`-fsycl-device-obj=<arg>`** [EXPERIMENTAL]

Specify format of device code stored in the resulting object. The <arg> can
be one of the following: "spirv" - SPIR-V is emitted, "llvmir" - LLVM-IR
be one of the following: "spirv" - SPIR-V, "asm" - assembly output when
possible (PTX, when targetting Nvidia devices) , or "llvmir" - LLVM-IR
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
possible (PTX, when targetting Nvidia devices) , or "llvmir" - LLVM-IR
possible (PTX, when targeting NVIDIA devices), or "llvmir" - LLVM-IR

bitcode format is emitted (default).

**`-fsycl-help[=backend]`**
Expand Down