Skip to content

Commit

Permalink
[SYCL][Driver] Enhance -Xarch_device and -Xarch_host for sycl (#12478)
Browse files Browse the repository at this point in the history
Add simple support to enhance -Xarch_device and -Xarch_host in sycl
offloading which can support multiple arguments in single '-Xarch_*' and
'-mllvm <opt>' is supported as well.

---------

Signed-off-by: jinge90 <ge.jin@intel.com>
  • Loading branch information
jinge90 authored Feb 5, 2024
1 parent ab86d0d commit f7bdae8
Show file tree
Hide file tree
Showing 2 changed files with 219 additions and 0 deletions.
57 changes: 57 additions & 0 deletions clang/lib/Driver/Compilation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,61 @@ Compilation::~Compilation() {
delete Arg.second;
}

static void HandleXarchArgs(DerivedArgList *OffloadArgList, const Driver &D,
bool IsDevice) {
if (!OffloadArgList)
return;

if (IsDevice && !OffloadArgList->hasArg(options::OPT_Xarch_device))
return;

if (!IsDevice && !OffloadArgList->hasArg(options::OPT_Xarch_host))
return;

bool NeedHandle = false;
std::vector<std::string> XarchValues;
XarchValues = IsDevice
? OffloadArgList->getAllArgValues(options::OPT_Xarch_device)
: OffloadArgList->getAllArgValues(options::OPT_Xarch_host);
SmallVector<StringRef, 8> XarchValueRefs;
for (auto XarchV : XarchValues) {
if (XarchV.find(' ') != std::string::npos) {
NeedHandle = true;
StringRef XarchVRef(XarchV);
SmallVector<StringRef, 8> XarchVecs;
XarchVRef.trim().split(XarchVecs, ' ', -1, false);
size_t Index;
const size_t XSize = XarchVecs.size();
for (Index = 0; Index < XSize; ++Index) {
if (XarchVecs[Index].compare("-mllvm") == 0) {
if (Index < (XSize - 1)) {
XarchValueRefs.push_back(OffloadArgList->MakeArgStringRef(
(StringRef("-mllvm=") + XarchVecs[Index + 1]).str()));
Index++;
continue;
} else
D.Diag(clang::diag::err_drv_missing_argument) << "-mllvm" << 1;
} else
XarchValueRefs.push_back(
OffloadArgList->MakeArgStringRef(XarchVecs[Index]));
}
} else
XarchValueRefs.push_back(StringRef(XarchV));
}

if (NeedHandle) {
auto Xarch_OPT =
IsDevice ? options::OPT_Xarch_device : options::OPT_Xarch_host;
OffloadArgList->eraseArg(Xarch_OPT);
for (auto XarchV : XarchValueRefs) {
Arg *A = OffloadArgList->MakeSeparateArg(
nullptr, D.getOpts().getOption(Xarch_OPT), XarchV);
A->claim();
OffloadArgList->append(A);
}
}
}

const DerivedArgList &
Compilation::getArgsForToolChain(const ToolChain *TC, StringRef BoundArch,
Action::OffloadKind DeviceOffloadKind) {
Expand All @@ -82,9 +137,11 @@ Compilation::getArgsForToolChain(const ToolChain *TC, StringRef BoundArch,

DerivedArgList *NewDAL = nullptr;
if (!OffloadArgs) {
HandleXarchArgs(TranslatedArgs, getDriver(), false);
NewDAL = TC->TranslateXarchArgs(*TranslatedArgs, BoundArch,
DeviceOffloadKind, &AllocatedArgs);
} else {
HandleXarchArgs(OffloadArgs, getDriver(), true);
NewDAL = TC->TranslateXarchArgs(*OffloadArgs, BoundArch, DeviceOffloadKind,
&AllocatedArgs);
if (!NewDAL)
Expand Down
162 changes: 162 additions & 0 deletions clang/test/Driver/sycl-xarch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
///
/// Perform several driver tests for SYCL -Xarch_device/host on Linux
///

// UNSUPPORTED: system-windows

/// ###########################################################################

/// test behavior of -Xarch_device with 1 option for SYCL compiler, the flag
/// should be passed to device compilation only.
// RUN: %clangxx -fsycl %s -Xarch_device -fsanitize=address -### 2>&1 \
// RUN: | FileCheck %s -check-prefix=SYCL_XARCH_DEVICE_OPTION
// RUN: %clangxx -fsycl %s -Xarch_device -fsanitize=address -### 2>&1 \
// RUN: | FileCheck %s -check-prefix=SYCL_XARCH_DEVICE_ONLY
// SYCL_XARCH_DEVICE_OPTION: clang{{.*}} "-fsycl-is-device"
// SYCL_XARCH_DEVICE_OPTION-SAME: -fsanitize=address
// SYCL_XARCH_DEVICE_OPTION-SAME: -fsanitize-address-use-after-return=never
// SYCL_XARCH_DEVICE_OPTION-SAME: -fno-sanitize-address-use-after-scope
// SYCL_XARCH_DEVICE_OPTION-SAME: "-mllvm" "-asan-instrumentation-with-call-threshold=0"
// SYCL_XARCH_DEVICE_OPTION-SAME: "-mllvm" "-asan-stack=0"
// SYCL_XARCH_DEVICE_OPTION-SAME: "-mllvm" "-asan-globals=0"
// SYCL_XARCH_DEVICE_ONLY: llc{{.*}} "-filetype=obj"
// SYCL_XARCH_DEVICE_ONLY-NOT: fsanitize=address

/// test behavior of -Xarch_device with multiple options for SYCL compiler, the
/// flags should be passed to device compilation only.
// RUN: %clangxx -fsycl %s -Xarch_device "-fsanitize=address -DXARCH_DEVICE_TEST -mllvm -enable-merge-functions" -### 2>&1 \
// RUN: | FileCheck %s -check-prefix=SYCL_XARCH_DEVICE_OPTIONS1
// RUN: %clangxx -fsycl %s -Xarch_device "-fsanitize=address -DXARCH_DEVICE_TEST -mllvm -enable-merge-functions" -### 2>&1 \
// RUN: | FileCheck %s -check-prefix=SYCL_XARCH_DEVICE_OPTIONS1
// RUN: %clangxx -fsycl %s -Xarch_device "-fsanitize=address -DXARCH_DEVICE_TEST -mllvm -enable-merge-functions" -### 2>&1 \
// RUN: | FileCheck %s -check-prefix=SYCL_XARCH_DEVICE_OPTIONS2
// RUN: %clangxx -fsycl %s -Xarch_device "-fsanitize=address -DXARCH_DEVICE_TEST -mllvm -enable-merge-functions" -### 2>&1 \
// RUN: | FileCheck %s -check-prefix=SYCL_XARCH_DEVICE_OPTIONS3
// SYCL_XARCH_DEVICE_OPTIONS1: clang{{.*}} "-fsycl-is-device"
// SYCL_XARCH_DEVICE_OPTIONS1-SAME: -fsanitize=address
// SYCL_XARCH_DEVICE_OPTIONS1-SAME: -fsanitize-address-use-after-return=never
// SYCL_XARCH_DEVICE_OPTIONS1-SAME: -fno-sanitize-address-use-after-scope
// SYCL_XARCH_DEVICE_OPTIONS1-SAME: "-mllvm" "-asan-instrumentation-with-call-threshold=0"
// SYCL_XARCH_DEVICE_OPTIONS1-SAME: "-mllvm" "-asan-stack=0"
// SYCL_XARCH_DEVICE_OPTIONS1-SAME: "-mllvm" "-asan-globals=0"
// SYCL_XARCH_DEVICE_OPTIONS2: clang{{.*}} "-fsycl-is-device"
// SYCL_XARCH_DEVICE_OPTIONS2-SAME: XARCH_DEVICE_TEST
// SYCL_XARCH_DEVICE_OPTIONS3: clang{{.*}} "-fsycl-is-device"
// SYCL_XARCH_DEVICE_OPTIONS3-SAME: "-mllvm" "-enable-merge-functions"


/// test behavior of -Xarch_host with 1 option for SYCL compiler, the flag
/// should be passed to host compilation only.
// RUN: %clangxx -fsycl %s -Xarch_host -fsanitize=address -### 2>&1 \
// RUN: | FileCheck %s -check-prefix=SYCL_XARCH_HOST_OPTION
// RUN: %clangxx -fsycl %s -Xarch_host -fsanitize=address -### 2>&1 \
// RUN: | FileCheck %s -check-prefix=SYCL_XARCH_HOST_ONLY
// SYCL_XARCH_HOST_OPTION: clang{{.*}} "-fsycl-is-host"
// SYCL_XARCH_HOST_OPTION-SAME: -fsanitize=address
// SYCL_XARCH_HOST_OPTION-SAME: -fsanitize-address-use-after-scope
// SYCL_XARCH_HOST_OPTION-NEXT: libclang_rt.asan
// SYCL_XARCH_HOST_ONLY: clang{{.*}} "-fsycl-is-device"
// SYCL_XARCH_HOST_ONLY-NOT: -fsanitize=address
// SYCL_XARCH_HOST_ONLY: clang{{.*}} "-fsycl-is-host"

/// test behavior of -Xarch_host with multiple options for SYCL compiler, the
/// flags should be passed to host compilation only.
// RUN: %clangxx -fsycl %s -Xarch_host "-fsanitize=address -DXARCH_HOST_TEST -mllvm -enable-merge-functions" -### 2>&1 \
// RUN: | FileCheck %s -check-prefix=SYCL_XARCH_HOST_OPTIONS1
// RUN: %clangxx -fsycl %s -Xarch_host "-fsanitize=address -DXARCH_HOST_TEST -mllvm -enable-merge-functions" -### 2>&1 \
// RUN: | FileCheck %s -check-prefix=SYCL_XARCH_HOST_OPTIONS2
// RUN: %clangxx -fsycl %s -Xarch_host "-fsanitize=address -DXARCH_HOST_TEST -mllvm -enable-merge-functions" -### 2>&1 \
// RUN: | FileCheck %s -check-prefix=SYCL_XARCH_HOST_OPTIONS3
// SYCL_XARCH_HOST_OPTIONS1: clang{{.*}} "-fsycl-is-host"
// SYCL_XARCH_HOST_OPTIONS1-SAME: -fsanitize=address
// SYCL_XARCH_HOST_OPTIONS1-SAME: -fsanitize-address-use-after-scope
// SYCL_XARCH_HOST_OPTIONS2: clang{{.*}} "-fsycl-is-host"
// SYCL_XARCH_HOST_OPTIONS2-SAME: XARCH_HOST_TEST
// SYCL_XARCH_HOST_OPTIONS3: clang{{.*}} "-fsycl-is-host"
// SYCL_XARCH_HOST_OPTIONS3-SAME: "-mllvm" "-enable-merge-functions"

// test behavior of combination of -Xarch_device and -Xarch_device.
// RUN: %clangxx -fsycl %s -Xarch_device "-fsanitize=address -mllvm -enable-merge-functions" \
// RUN: -Xarch_host "-fsanitize=memory -DUSE_XARCH_HOST -fno-builtin" -### 2>&1 \
// RUN: | FileCheck %s -check-prefix=SYCL_XARCH_COM_DEVICE_OPTIONS1
// RUN: %clangxx -fsycl %s -Xarch_device "-fsanitize=address -mllvm -enable-merge-functions" \
// RUN: -Xarch_host "-fsanitize=memory -DUSE_XARCH_HOST -fno-builtin" -### 2>&1 \
// RUN: | FileCheck %s -check-prefix=SYCL_XARCH_COM_DEVICE_OPTIONS2
// RUN: %clangxx -fsycl %s -Xarch_device "-fsanitize=address -mllvm -enable-merge-functions" \
// RUN: -Xarch_host "-fsanitize=memory -DUSE_XARCH_HOST -fno-builtin" -### 2>&1 \
// RUN: | FileCheck %s -check-prefix=SYCL_XARCH_COM_NO_DEVICE
// RUN: %clangxx -fsycl %s -Xarch_device "-fsanitize=address -mllvm -enable-merge-functions" \
// RUN: -Xarch_host "-fsanitize=memory -DUSE_XARCH_HOST -fno-builtin" -### 2>&1 \
// RUN: | FileCheck %s -check-prefix=SYCL_XARCH_COM_HOST_OPTIONS1
// RUN: %clangxx -fsycl %s -Xarch_device "-fsanitize=address -mllvm -enable-merge-functions" \
// RUN: -Xarch_host "-fsanitize=memory -DUSE_XARCH_HOST -fno-builtin" -### 2>&1 \
// RUN: | FileCheck %s -check-prefix=SYCL_XARCH_COM_HOST_OPTIONS2
// RUN: %clangxx -fsycl %s -Xarch_device "-fsanitize=address -mllvm -enable-merge-functions" \
// RUN: -Xarch_host "-fsanitize=memory -DUSE_XARCH_HOST -fno-builtin" -### 2>&1 \
// RUN: | FileCheck %s -check-prefix=SYCL_XARCH_COM_HOST_OPTIONS3
// RUN: %clangxx -fsycl %s -Xarch_device "-fsanitize=address -mllvm -enable-merge-functions" \
// RUN: -Xarch_host "-fsanitize=memory -DUSE_XARCH_HOST -fno-builtin" -### 2>&1 \
// RUN: | FileCheck %s -check-prefix=SYCL_XARCH_COM_NO_HOST
// SYCL_XARCH_COM_DEVICE_OPTIONS1: clang{{.*}} "-fsycl-is-device"
// SYCL_XARCH_COM_DEVICE_OPTIONS1-SAME: -fsanitize=address
// SYCL_XARCH_COM_DEVICE_OPTIONS1-SAME: -fsanitize-address-use-after-return=never
// SYCL_XARCH_COM_DEVICE_OPTIONS1-SAME: -fno-sanitize-address-use-after-scope
// SYCL_XARCH_COM_DEVICE_OPTIONS1-SAME: "-mllvm" "-asan-instrumentation-with-call-threshold=0"
// SYCL_XARCH_COM_DEVICE_OPTIONS1-SAME: "-mllvm" "-asan-stack=0"
// SYCL_XARCH_COM_DEVICE_OPTIONS1-SAME: "-mllvm" "-asan-globals=0"
// SYCL_XARCH_COM_DEVICE_OPTIONS2: clang{{.*}} "-fsycl-is-device"
// SYCL_XARCH_COM_DEVICE_OPTIONS2-SAME: "-mllvm" "-enable-merge-functions"
// SYCL_XARCH_COM_NO_DEVICE: clang{{.*}} "-fsycl-is-device"
// SYCL_XARCH_COM_NO_DEVICE-NOT: USE_XARCH_HOST
// SYCL_XARCH_COM_NO_DEVICE: clang{{.*}} "-fsycl-is-host"
// SYCL_XARCH_COM_HOST_OPTIONS1: clang{{.*}} "-fsycl-is-host"
// SYCL_XARCH_COM_HOST_OPTIONS1-SAME: -fsanitize=memory
// SYCL_XARCH_COM_HOST_OPTIONS1-NEXT: libclang_rt.msan
// SYCL_XARCH_COM_HOST_OPTIONS2: clang{{.*}} "-fsycl-is-host"
// SYCL_XARCH_COM_HOST_OPTIONS2-SAME: USE_XARCH_HOST
// SYCL_XARCH_COM_HOST_OPTIONS3: clang{{.*}} "-fsycl-is-host"
// SYCL_XARCH_COM_HOST_OPTIONS3-SAME: -fno-builtin
// SYCL_XARCH_COM_NO_HOST: clang{{.*}} "-fsycl-is-host"
// SYCL_XARCH_COM_NO_HOST-NOT: "-mllvm" "-enable-merge-functions"


// test behavior of multiple usage of -Xarch_host in single command line
// RUN: %clangxx -fsycl %s -Xarch_host "-fsanitize=address -mllvm -enable-merge-functions" \
// RUN: -Xarch_host -DFOO -Xarch_host -DFOO1 -### 2>&1 \
// RUN: | FileCheck %s -check-prefix=SYCL_XARCH_HOST_MULTIPLE1
// RUN: %clangxx -fsycl %s -Xarch_host "-fsanitize=address -mllvm -enable-merge-functions" \
// RUN: -Xarch_host -DFOO -Xarch_host -DFOO1 -### 2>&1 \
// RUN: | FileCheck %s -check-prefix=SYCL_XARCH_HOST_MULTIPLE2
// RUN: %clangxx -fsycl %s -Xarch_host "-fsanitize=address -mllvm -enable-merge-functions" \
// RUN: -Xarch_host -DFOO -Xarch_host -DFOO1 -### 2>&1 \
// RUN: | FileCheck %s -check-prefix=SYCL_XARCH_HOST_MULTIPLE3
// RUN: %clangxx -fsycl %s -Xarch_host "-fsanitize=address -mllvm -enable-merge-functions" \
// RUN: -Xarch_host -DFOO -Xarch_host -DFOO1 -### 2>&1 \
// RUN: | FileCheck %s -check-prefix=SYCL_XARCH_HOST_MULTIPLE4
// RUN: %clangxx -fsycl %s -Xarch_host "-fsanitize=address -mllvm -enable-merge-functions" \
// RUN: -Xarch_host -DFOO -Xarch_host -DFOO1 -### 2>&1 \
// RUN: | FileCheck %s -check-prefix=SYCL_XARCH_NO_DEVICE_MULTIPLE1
// RUN: %clangxx -fsycl %s -Xarch_host "-fsanitize=address -mllvm -enable-merge-functions" \
// RUN: -Xarch_host -DFOO -Xarch_host -DFOO1 -### 2>&1 \
// RUN: | FileCheck %s -check-prefix=SYCL_XARCH_NO_DEVICE_MULTIPLE2
// RUN: %clangxx -fsycl %s -Xarch_host "-fsanitize=address -mllvm -enable-merge-functions" \
// RUN: -Xarch_host -DFOO -Xarch_host -DFOO1 -### 2>&1 \
// RUN: | FileCheck %s -check-prefix=SYCL_XARCH_NO_DEVICE_MULTIPLE3
// SYCL_XARCH_HOST_MULTIPLE1: clang{{.*}} "-fsycl-is-host"
// SYCL_XARCH_HOST_MULTIPLE1-SAME: -fsanitize=address
// SYCL_XARCH_HOST_MULTIPLE1-NEXT: libclang_rt.asan
// SYCL_XARCH_HOST_MULTIPLE2: clang{{.*}} "-fsycl-is-host"
// SYCL_XARCH_HOST_MULTIPLE2-SAME: "-mllvm" "-enable-merge-functions"
// SYCL_XARCH_HOST_MULTIPLE3: clang{{.*}} "-fsycl-is-host"
// SYCL_XARCH_HOST_MULTIPLE3-SAME: "FOO"
// SYCL_XARCH_HOST_MULTIPLE4: clang{{.*}} "-fsycl-is-host"
// SYCL_XARCH_HOST_MULTIPLE4-SAME: "FOO1"
// SYCL_XARCH_NO_DEVICE_MULTIPLE1: clang{{.*}} "-fsycl-is-device"
// SYCL_XARCH_NO_DEVICE_MULTIPLE1-NOT: -fsanitize=address
// SYCL_XARCH_NO_DEVICE_MULTIPLE1: llc{{.*}} "-filetype=obj"
// SYCL_XARCH_NO_DEVICE_MULTIPLE2: clang{{.*}} "-fsycl-is-device"
// SYCL_XARCH_NO_DEVICE_MULTIPLE2-NOT: "-mllvm" "-enable-merge-functions"
// SYCL_XARCH_NO_DEVICE_MULTIPLE2: llc{{.*}} "-filetype=obj"
// SYCL_XARCH_NO_DEVICE_MULTIPLE3: clang{{.*}} "-fsycl-is-device"
// SYCL_XARCH_NO_DEVICE_MULTIPLE3-NOT: "FOO"
// SYCL_XARCH_NO_DEVICE_MULTIPLE3: llc{{.*}} "-filetype=obj"

0 comments on commit f7bdae8

Please sign in to comment.