From 894dff0bc907e43b3c9217ba08c653484923aa35 Mon Sep 17 00:00:00 2001 From: Alexey Bader Date: Thu, 18 Dec 2025 10:05:42 -0800 Subject: [PATCH 1/4] [SYCL][NFC] Refactor addSYCLDefaultTriple This patch merges two loops traversing SYCL triples into one. Also optimizes the usage of addSYCLDefaultTriple inside CreateOffloadingDeviceToolChains. The default triple is guranted to be added at the beginning of the list. Instead of building the default triple again in CreateOffloadingDeviceToolChains, we re-use the value added by addSYCLDefaultTriple to the list of the triples. --- clang/lib/Driver/Driver.cpp | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index 8da9419c81b6c..f24af408e93e4 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -101,6 +101,7 @@ #include "llvm/Support/raw_ostream.h" #include "llvm/TargetParser/Host.h" #include "llvm/TargetParser/RISCVISAInfo.h" +#include #include // ::getenv #include #include @@ -1189,18 +1190,19 @@ static bool addSYCLDefaultTriple(Compilation &C, llvm::Triple DefaultTriple = C.getDriver().getSYCLDeviceTriple(getDefaultSYCLArch(C)); for (const auto &SYCLTriple : SYCLTriples) { + // Check current set of triples to see if the default has already been set. if (SYCLTriple == DefaultTriple) return false; - // If we encounter a known non-spir* target, do not add the default triple. - if (SYCLTriple.isNVPTX() || SYCLTriple.isAMDGCN()) - return false; - } - // Check current set of triples to see if the default has already been set. - for (const auto &SYCLTriple : SYCLTriples) { + // TODO: Do we need this check? Don't we already detect the default triple + // in the previous condition? if (SYCLTriple.getSubArch() == llvm::Triple::NoSubArch && SYCLTriple.isSPIROrSPIRV()) return false; + // If we encounter a known non-spir* target, do not add the default triple. + if (SYCLTriple.isNVPTX() || SYCLTriple.isAMDGCN()) + return false; } + // TODO: Why do we insert at the beginning? This is expensive operation. SYCLTriples.insert(SYCLTriples.begin(), DefaultTriple); return true; } @@ -1504,9 +1506,10 @@ void Driver::CreateOffloadingDeviceToolChains(Compilation &C, Triples.push_back(T); } if (addSYCLDefaultTriple(C, Triples)) { - llvm::Triple TT = - llvm::Triple(getSYCLDeviceTriple(getDefaultSYCLArch(C))); - auto &TC = getOffloadToolChain(C.getInputArgs(), Action::OFK_SYCL, TT, + assert(!Triples.empty() && + "addSYCLDefaultTriple should add at least one triple"); + auto &TC = getOffloadToolChain(C.getInputArgs(), Action::OFK_SYCL, + Triples.front(), C.getDefaultToolChain().getTriple()); C.addOffloadDeviceToolChain(&TC, Action::OFK_SYCL); } From c828f2150c2c94a6a3cc4121bcc2d50e5e64dbe2 Mon Sep 17 00:00:00 2001 From: Alexey Bader Date: Thu, 18 Dec 2025 15:35:32 -0800 Subject: [PATCH 2/4] Simplify "early exit" conditions for existing triples. --- clang/lib/Driver/Driver.cpp | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index f24af408e93e4..839dbb1bf4ec8 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -1180,21 +1180,16 @@ llvm::Triple Driver::getSYCLDeviceTriple(StringRef TargetArch, return llvm::Triple(TargetArch); } +/// Returns true if a triple is added to SYCLTriples, false otherwise static bool addSYCLDefaultTriple(Compilation &C, SmallVectorImpl &SYCLTriples) { - /// Returns true if a triple is added to SYCLTriples, false otherwise if (!C.getDriver().isSYCLDefaultTripleImplied()) return false; if (C.getInputArgs().hasArg(options::OPT_fsycl_force_target_EQ)) return false; - llvm::Triple DefaultTriple = - C.getDriver().getSYCLDeviceTriple(getDefaultSYCLArch(C)); for (const auto &SYCLTriple : SYCLTriples) { - // Check current set of triples to see if the default has already been set. - if (SYCLTriple == DefaultTriple) - return false; - // TODO: Do we need this check? Don't we already detect the default triple - // in the previous condition? + // Check current set of triples to see if the default (i.e. spir64) has + // already been set. if (SYCLTriple.getSubArch() == llvm::Triple::NoSubArch && SYCLTriple.isSPIROrSPIRV()) return false; @@ -1202,8 +1197,8 @@ static bool addSYCLDefaultTriple(Compilation &C, if (SYCLTriple.isNVPTX() || SYCLTriple.isAMDGCN()) return false; } - // TODO: Why do we insert at the beginning? This is expensive operation. - SYCLTriples.insert(SYCLTriples.begin(), DefaultTriple); + SYCLTriples.insert(SYCLTriples.begin(), + C.getDriver().getSYCLDeviceTriple(getDefaultSYCLArch(C))); return true; } From ab5553c0d92fcafa7dc7fbf47717986027acd3ce Mon Sep 17 00:00:00 2001 From: Alexey Bader Date: Thu, 18 Dec 2025 16:00:15 -0800 Subject: [PATCH 3/4] Add default SYCL triple to the end of the SYCL triples vector. --- clang/lib/Driver/Driver.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index 839dbb1bf4ec8..acacd5b12a28f 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -1197,8 +1197,8 @@ static bool addSYCLDefaultTriple(Compilation &C, if (SYCLTriple.isNVPTX() || SYCLTriple.isAMDGCN()) return false; } - SYCLTriples.insert(SYCLTriples.begin(), - C.getDriver().getSYCLDeviceTriple(getDefaultSYCLArch(C))); + SYCLTriples.push_back( + C.getDriver().getSYCLDeviceTriple(getDefaultSYCLArch(C))); return true; } @@ -1504,7 +1504,7 @@ void Driver::CreateOffloadingDeviceToolChains(Compilation &C, assert(!Triples.empty() && "addSYCLDefaultTriple should add at least one triple"); auto &TC = getOffloadToolChain(C.getInputArgs(), Action::OFK_SYCL, - Triples.front(), + Triples.back(), C.getDefaultToolChain().getTriple()); C.addOffloadDeviceToolChain(&TC, Action::OFK_SYCL); } @@ -6320,8 +6320,8 @@ class OffloadingActionBuilder final { if (addSYCLDefaultTriple(C, SYCLTripleList)) { // If a SYCLDefaultTriple is added to SYCLTripleList, // add new target to SYCLTargetInfoList - llvm::Triple TT = SYCLTripleList.front(); - auto TCIt = llvm::find_if( + llvm::Triple TT = SYCLTripleList.back(); + const auto *TCIt = llvm::find_if( ToolChains, [&](auto &TC) { return TT == TC->getTriple(); }); SYCLTargetInfoList.emplace_back(*TCIt, nullptr); } From 75c6eb00ed337e3257cd97f3e2ea634d02c0383b Mon Sep 17 00:00:00 2001 From: Alexey Bader Date: Thu, 18 Dec 2025 18:21:36 -0800 Subject: [PATCH 4/4] Align code with the comment. --- clang/lib/Driver/Driver.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index acacd5b12a28f..ac41db5e296c3 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -1194,7 +1194,7 @@ static bool addSYCLDefaultTriple(Compilation &C, SYCLTriple.isSPIROrSPIRV()) return false; // If we encounter a known non-spir* target, do not add the default triple. - if (SYCLTriple.isNVPTX() || SYCLTriple.isAMDGCN()) + if (!SYCLTriple.isSPIROrSPIRV()) return false; } SYCLTriples.push_back(