[clang] [clang-sycl-linker] Fix use of uninitialized memory in temp files (PR #114488)
sarnex wrote: PR title fixed, thanks. @jhuber6 @bader This should be ready for merge. CI fails are not related and the failing test is passing in the sanitizer tests now. Thx. https://github.com/llvm/llvm-project/pull/114488 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][SYCL] Fix use of uninitalized memory in temp files (PR #114488)
https://github.com/sarnex created https://github.com/llvm/llvm-project/pull/114488 This fixes the current sanitizer CI failures. I manually confirmed the fix with a MemorySanitizer build. >From 51227ad50bff12eff5a4f835686f13d2444848ea Mon Sep 17 00:00:00 2001 From: "Sarnie, Nick" Date: Thu, 31 Oct 2024 16:22:33 -0700 Subject: [PATCH] [Clang][SYCL] Fix use of uninitalized memory in temp files Signed-off-by: Sarnie, Nick --- clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp b/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp index 0639b95c76e218..076458a275d986 100644 --- a/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp +++ b/clang/tools/clang-sycl-linker/ClangSYCLLinker.cpp @@ -230,7 +230,7 @@ Expected linkDeviceInputFiles(ArrayRef InputFiles, CmdArgs.push_back("--suppress-warnings"); if (Error Err = executeCommands(*LLVMLinkPath, CmdArgs)) return std::move(Err); - return *OutFileOrErr; + return Args.MakeArgString(*OutFileOrErr); } // This utility function is used to gather all SYCL device library files that ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-sycl-linker] Fix use of uninitialized memory in temp files (PR #114488)
https://github.com/sarnex edited https://github.com/llvm/llvm-project/pull/114488 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] __has_builtin should return false for aux triple builtins (PR #121839)
sarnex wrote: > Is it possible that we could just skip generating the builtin IDs at all for > the aux target? Or does that break something. Seems like we can't, I tried this: ``` diff --git a/clang/lib/Basic/Builtins.cpp b/clang/lib/Basic/Builtins.cpp index c6e54b89e848..904c64f05c36 100644 --- a/clang/lib/Basic/Builtins.cpp +++ b/clang/lib/Basic/Builtins.cpp @@ -64,8 +64,8 @@ void Builtin::Context::InitializeTarget(const TargetInfo &Target, const TargetInfo *AuxTarget) { assert(TSRecords.empty() && "Already initialized target?"); TSRecords = Target.getTargetBuiltins(); - if (AuxTarget) -AuxTSRecords = AuxTarget->getTargetBuiltins(); + // if (AuxTarget) + // AuxTSRecords = AuxTarget->getTargetBuiltins(); } ``` and got these failures ``` Failed Tests (3): Clang :: CodeGenHipStdPar/unsupported-builtins.cpp Clang :: Headers/__cpuidex_conflict.c Clang :: Sema/builtin-spirv-amdgcn-atomic-inc-dec-failure.cpp ``` and then reverted that and tried this ``` diff --git a/clang/lib/Basic/Builtins.cpp b/clang/lib/Basic/Builtins.cpp index c6e54b89e848..34e1d0aa4b68 100644 --- a/clang/lib/Basic/Builtins.cpp +++ b/clang/lib/Basic/Builtins.cpp @@ -153,9 +153,9 @@ void Builtin::Context::initializeBuiltins(IdentifierTable &Table, Table.get(TSRecords[i].Name).setBuiltinID(i + Builtin::FirstTSBuiltin); // Step #3: Register target-specific builtins for AuxTarget. - for (unsigned i = 0, e = AuxTSRecords.size(); i != e; ++i) -Table.get(AuxTSRecords[i].Name) -.setBuiltinID(i + Builtin::FirstTSBuiltin + TSRecords.size()); + //for (unsigned i = 0, e = AuxTSRecords.size(); i != e; ++i) + // Table.get(AuxTSRecords[i].Name) + // .setBuiltinID(i + Builtin::FirstTSBuiltin + TSRecords.size()); // Step #4: Unregister any builtins specified by -fno-builtin-foo. for (llvm::StringRef Name : LangOpts.NoBuiltinFuncs) { ``` and got it broke the same tests ``` Failed Tests (3): Clang :: CodeGenHipStdPar/unsupported-builtins.cpp Clang :: Headers/__cpuidex_conflict.c Clang :: Sema/builtin-spirv-amdgcn-atomic-inc-dec-failure.cpp ``` https://github.com/llvm/llvm-project/pull/121839 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] __has_builtin should return false for aux triple builtins (PR #121839)
sarnex wrote: `.CodeGenHipStdPar/unsupported-builtins.cpp` is pretty interesting actually, it looks like it tests for some behavior in CodeGen that seems like it's trying to fix the exact same problem https://github.com/llvm/llvm-project/pull/121839 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] __has_builtin should return false for aux triple builtins (PR #121839)
https://github.com/sarnex created https://github.com/llvm/llvm-project/pull/121839 Currently, `__has_builtin` will return true when passed a builtin that is only supported on the aux target. I found this when `__has_builtin` was called with an X86 builtin but the current target was SPIR-V. If we know for sure the builtin can't be supported on the current target, the function should return false. We can't simply check if it's an aux builtin, see the test mentioned in the comment. >From 997373350540448d91f9884b98cbdc0df058a7a3 Mon Sep 17 00:00:00 2001 From: "Sarnie, Nick" Date: Mon, 6 Jan 2025 11:48:07 -0800 Subject: [PATCH] [Clang] __has_builtin should return false for aux triple builtins Signed-off-by: Sarnie, Nick --- clang/include/clang/Basic/Builtins.h | 5 + clang/lib/Basic/Builtins.cpp | 19 +++ clang/lib/Lex/PPMacroExpansion.cpp | 20 +--- clang/test/Preprocessor/builtin_aux_info.cpp | 12 4 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 clang/test/Preprocessor/builtin_aux_info.cpp diff --git a/clang/include/clang/Basic/Builtins.h b/clang/include/clang/Basic/Builtins.h index 63559d977ce6b6..0939f95b0922c1 100644 --- a/clang/include/clang/Basic/Builtins.h +++ b/clang/include/clang/Basic/Builtins.h @@ -74,6 +74,7 @@ struct Info { const char *Features; HeaderDesc Header; LanguageID Langs; + bool operator==(const Info &Other) const; }; /// Holds information about both target-independent and @@ -268,6 +269,10 @@ class Context { /// for AuxTarget). unsigned getAuxBuiltinID(unsigned ID) const { return ID - TSRecords.size(); } + // Return true if the AuxBuiltin ID represents a target-specific builtin that + // is always unsupported on the default target. + bool isAuxBuiltinIDAlwaysUnsupportedOnDefaultTarget(unsigned ID) const; + /// Returns true if this is a libc/libm function without the '__builtin_' /// prefix. static bool isBuiltinFunc(llvm::StringRef Name); diff --git a/clang/lib/Basic/Builtins.cpp b/clang/lib/Basic/Builtins.cpp index 588183788de322..c6e54b89e848cc 100644 --- a/clang/lib/Basic/Builtins.cpp +++ b/clang/lib/Basic/Builtins.cpp @@ -41,6 +41,14 @@ static constexpr Builtin::Info BuiltinInfo[] = { #include "clang/Basic/Builtins.inc" }; +bool Builtin::Info::operator==(const Builtin::Info &Other) const { + auto StrCompare = [](StringRef A, StringRef B) { return A == B; }; + return Name == Other.Name && StrCompare(Type, Other.Type) && + StrCompare(Attributes, Other.Attributes) && + StrCompare(Features, Other.Features) && Header.ID == Other.Header.ID && + Langs == Other.Langs; +} + const Builtin::Info &Builtin::Context::getRecord(unsigned ID) const { if (ID < Builtin::FirstTSBuiltin) return BuiltinInfo[ID]; @@ -183,6 +191,17 @@ unsigned Builtin::Context::getRequiredVectorWidth(unsigned ID) const { return Width; } +bool Builtin::Context::isAuxBuiltinIDAlwaysUnsupportedOnDefaultTarget( +unsigned ID) const { + assert(isAuxTargetBuiltinID(ID) && "Expected aux target builtin ID"); + const auto &Record = getRecord(ID); + for (const auto &MainTargetBuiltin : TSRecords) +if (Record == MainTargetBuiltin) + return false; + + return true; +} + bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg, const char *Fmt) const { assert(Fmt && "Not passed a format string"); diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp index 347c13da0ad215..13d9a0094a5827 100644 --- a/clang/lib/Lex/PPMacroExpansion.cpp +++ b/clang/lib/Lex/PPMacroExpansion.cpp @@ -1804,8 +1804,9 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) { diag::err_feature_check_malformed); if (!II) return false; -else if (II->getBuiltinID() != 0) { - switch (II->getBuiltinID()) { +auto BuiltinID = II->getBuiltinID(); +if (BuiltinID != 0) { + switch (BuiltinID) { case Builtin::BI__builtin_cpu_is: return getTargetInfo().supportsCpuIs(); case Builtin::BI__builtin_cpu_init: @@ -1818,8 +1819,21 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) { // usual allocation and deallocation functions. Required by libc++ return 201802; default: +// We may get here because of aux builtins which may not be +// supported on the default target, for example if we have an X86 +// specific builtin and the current target is SPIR-V. Sometimes we +// rely on __has_builtin returning true when passed a builtin that +// is not supported on the default target due to LangOpts but is +// supported on the aux target. See +// test/Headers/__cpuidex_conflict.c for an example. If the builtin +// is a
[clang] [llvm] [Driver][clang-linker-wrapper] Add initial support for OpenMP offloading to generic SPIR-V (PR #120145)
sarnex wrote: > > > Sure, what's left for this to work? I'm probably going to be messing > > > around with the OpenMP 'DeviceRTL' more, likely killing off the > > > 'fatbinary' and just using the per-target runtime dir stuff. I'm going to > > > assume this wouldn't work well with SPIR-V since they don't have a > > > consistent toolchain set up yet. What's we'd need is something like this. > > > > > > If you mean the entire flow, first we need some work in the OMP FE to > > generate valid OMP SPIR-V (some of the stuff we do in DeviceRTL with > > specifying the addressspaces explicitly on globals make the OMP FE generate > > bad IR because it never had to deal with SPIR-V's weird global var > > addressspace /addrspacecast rules before) > > But assuming that works, then yeah we will have to figure out how we want > > to generate DeviceRTL. I'm not too familiar with the per-target dir stuff, > > but if the problem is we will have a single RTL for all SPIR-V arches but > > there will be multiple vendors so the triple won't lead us to the right > > directory since we only generated one RTL archive for all SPIR-V, then yeah > > we'll need something special but it should be doable. If I completely > > whiffed what you were talking about let me know. > > After that we need the actual runtime plugin (at least for Intel devices), > > which we are working on but it's big. > > The vendor should be part of the triple, so realistically we'd have > `spirv64-unknown-amd` or `spirv64-unknown-intel`. Cool, I don't see any major issues then, just minor stuff I'll have to adapt to. https://github.com/llvm/llvm-project/pull/120145 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [Driver][clang-linker-wrapper] Add initial support for OpenMP offloading to generic SPIR-V (PR #120145)
sarnex wrote: > Sure, what's left for this to work? I'm probably going to be messing around > with the OpenMP 'DeviceRTL' more, likely killing off the 'fatbinary' and just > using the per-target runtime dir stuff. I'm going to assume this wouldn't > work well with SPIR-V since they don't have a consistent toolchain set up > yet. What's we'd need is something like this. If you mean the entire flow, first we need some work in the OMP FE to generate valid OMP SPIR-V (some of the stuff we do in DeviceRTL with specifying the addressspaces explicitly on globals make the OMP FE generate bad IR because it never had to deal with SPIR-V's weird global var addressspace /addrspacecast rules before) But assuming that works, then yeah we will have to figure out how we want to generate DeviceRTL. I'm not too familiar with the per-target dir stuff, but if the problem is we will have a single RTL for all SPIR-V arches but multiple vendors so the triple won't lead us to the right directory since we only generated one RTL archive for all SPIR-V, then yeah we'll need something special but it should be doable. If I completely whiffed what you were talking about let me know. After that we need the actual runtime plugin (at least for Intel devices), which we are working on but it's big. https://github.com/llvm/llvm-project/pull/120145 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] __has_builtin should return false for aux triple builtins (PR #121839)
sarnex wrote: > It is not quite trying to fix a problem:). In hipstdpar mode there's no > host/device segregation, and it is possible to codegen IR for host only bits > (e.g., parts of the stdlib). We delay resolving whether a construct is or > isn't viable to the ME, and just place in these special stubs where the > otherwise unsupported construct (in this case a builtin) is used. Then, iff > it turns out that the code path that reaches the unsupported construct is > accessible, we error out - please see https://reviews.llvm.org/D155850 and > https://reviews.llvm.org/D155856 for historical context. > > I don't quite think that this change interacts with that at all (I'll look > again), but it might interact in some subtle way with offload languages such > as e.g. HIP which jump through some hoops to keep the AST consistent, so I've > added @yxsamliu to this review. Ah, thanks for the clarification for for adding reviewers! https://github.com/llvm/llvm-project/pull/121839 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] __has_builtin should return false for aux triple builtins (PR #121839)
https://github.com/sarnex ready_for_review https://github.com/llvm/llvm-project/pull/121839 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Driver][SPIR-V] Use consistent tools to convert between text and binary form (PR #120266)
sarnex wrote: @bader Do you mind reviewing this as a member of the SPIR-V team (you already reviewed this internally) or recommend someone else to review, I'm having trouble finding someone :) https://github.com/llvm/llvm-project/pull/120266 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] __has_builtin should return false for aux triple builtins (PR #121839)
https://github.com/sarnex updated https://github.com/llvm/llvm-project/pull/121839 >From 997373350540448d91f9884b98cbdc0df058a7a3 Mon Sep 17 00:00:00 2001 From: "Sarnie, Nick" Date: Mon, 6 Jan 2025 11:48:07 -0800 Subject: [PATCH 1/2] [Clang] __has_builtin should return false for aux triple builtins Signed-off-by: Sarnie, Nick --- clang/include/clang/Basic/Builtins.h | 5 + clang/lib/Basic/Builtins.cpp | 19 +++ clang/lib/Lex/PPMacroExpansion.cpp | 20 +--- clang/test/Preprocessor/builtin_aux_info.cpp | 12 4 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 clang/test/Preprocessor/builtin_aux_info.cpp diff --git a/clang/include/clang/Basic/Builtins.h b/clang/include/clang/Basic/Builtins.h index 63559d977ce6b6..0939f95b0922c1 100644 --- a/clang/include/clang/Basic/Builtins.h +++ b/clang/include/clang/Basic/Builtins.h @@ -74,6 +74,7 @@ struct Info { const char *Features; HeaderDesc Header; LanguageID Langs; + bool operator==(const Info &Other) const; }; /// Holds information about both target-independent and @@ -268,6 +269,10 @@ class Context { /// for AuxTarget). unsigned getAuxBuiltinID(unsigned ID) const { return ID - TSRecords.size(); } + // Return true if the AuxBuiltin ID represents a target-specific builtin that + // is always unsupported on the default target. + bool isAuxBuiltinIDAlwaysUnsupportedOnDefaultTarget(unsigned ID) const; + /// Returns true if this is a libc/libm function without the '__builtin_' /// prefix. static bool isBuiltinFunc(llvm::StringRef Name); diff --git a/clang/lib/Basic/Builtins.cpp b/clang/lib/Basic/Builtins.cpp index 588183788de322..c6e54b89e848cc 100644 --- a/clang/lib/Basic/Builtins.cpp +++ b/clang/lib/Basic/Builtins.cpp @@ -41,6 +41,14 @@ static constexpr Builtin::Info BuiltinInfo[] = { #include "clang/Basic/Builtins.inc" }; +bool Builtin::Info::operator==(const Builtin::Info &Other) const { + auto StrCompare = [](StringRef A, StringRef B) { return A == B; }; + return Name == Other.Name && StrCompare(Type, Other.Type) && + StrCompare(Attributes, Other.Attributes) && + StrCompare(Features, Other.Features) && Header.ID == Other.Header.ID && + Langs == Other.Langs; +} + const Builtin::Info &Builtin::Context::getRecord(unsigned ID) const { if (ID < Builtin::FirstTSBuiltin) return BuiltinInfo[ID]; @@ -183,6 +191,17 @@ unsigned Builtin::Context::getRequiredVectorWidth(unsigned ID) const { return Width; } +bool Builtin::Context::isAuxBuiltinIDAlwaysUnsupportedOnDefaultTarget( +unsigned ID) const { + assert(isAuxTargetBuiltinID(ID) && "Expected aux target builtin ID"); + const auto &Record = getRecord(ID); + for (const auto &MainTargetBuiltin : TSRecords) +if (Record == MainTargetBuiltin) + return false; + + return true; +} + bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg, const char *Fmt) const { assert(Fmt && "Not passed a format string"); diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp index 347c13da0ad215..13d9a0094a5827 100644 --- a/clang/lib/Lex/PPMacroExpansion.cpp +++ b/clang/lib/Lex/PPMacroExpansion.cpp @@ -1804,8 +1804,9 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) { diag::err_feature_check_malformed); if (!II) return false; -else if (II->getBuiltinID() != 0) { - switch (II->getBuiltinID()) { +auto BuiltinID = II->getBuiltinID(); +if (BuiltinID != 0) { + switch (BuiltinID) { case Builtin::BI__builtin_cpu_is: return getTargetInfo().supportsCpuIs(); case Builtin::BI__builtin_cpu_init: @@ -1818,8 +1819,21 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) { // usual allocation and deallocation functions. Required by libc++ return 201802; default: +// We may get here because of aux builtins which may not be +// supported on the default target, for example if we have an X86 +// specific builtin and the current target is SPIR-V. Sometimes we +// rely on __has_builtin returning true when passed a builtin that +// is not supported on the default target due to LangOpts but is +// supported on the aux target. See +// test/Headers/__cpuidex_conflict.c for an example. If the builtin +// is an aux builtin and it can never be supported on the default +// target, __has_builtin should return false. +if (getBuiltinInfo().isAuxBuiltinID(BuiltinID) && + getBuiltinInfo().isAuxBuiltinIDAlwaysUnsupportedOnDefaultTarget( +BuiltinID)) + return false; return Builtin::evaluateRequiredTargetFeatures( -g
[clang] [llvm] [Driver][clang-linker-wrapper] Add initial support for OpenMP offloading to generic SPIR-V (PR #120145)
https://github.com/sarnex updated https://github.com/llvm/llvm-project/pull/120145 >From e1b9b503b1e9b8ebf5a9c94dcefd0c47ab009019 Mon Sep 17 00:00:00 2001 From: "Sarnie, Nick" Date: Mon, 16 Dec 2024 09:25:44 -0800 Subject: [PATCH 01/10] [Driver][clang-linker-wrapper] Add initial support for OpenMP offloading to generic SPIR-V This is the first of a series of patches to add support for OpenMP offloading to SPIR-V through liboffload with the first intended target being Intel GPUs. This patch implements the basic driver and `clang-linker-wrapper` work for JIT mode. There are still many missing pieces, so this is not yet usable. We introduce `spirv64-intel-unknown` as the only currently supported triple. The user-facing argument to enable offloading will be `-fopenmp -fopenmp-targets=spirv64-intel` Add a new `SPIRVOpenMPToolChain` toolchain based on the existing general SPIR-V toolchain which will call all the required SPIR-V tools as well as add the device RTL as an argument to the linker. As there is no production quality SPIR-V linker available, manually create an ELF binary containing the offloading image in a way that fits into the existing `liboffload` infrastructure. This ELF will eventually be passed to a runtime plugin that interacts with the Intel GPU runtime. There is also a small fix to an issue I found when trying to assemble SPIR-V when in text format. Signed-off-by: Sarnie, Nick --- clang/include/clang/Driver/Options.td | 2 + clang/lib/Driver/CMakeLists.txt | 1 + clang/lib/Driver/Driver.cpp | 40 +++-- clang/lib/Driver/ToolChains/CommonArgs.cpp| 9 +- clang/lib/Driver/ToolChains/SPIRV.cpp | 5 +- clang/lib/Driver/ToolChains/SPIRV.h | 2 +- clang/lib/Driver/ToolChains/SPIRVOpenMP.cpp | 36 clang/lib/Driver/ToolChains/SPIRVOpenMP.h | 29 +++ clang/lib/Frontend/CompilerInvocation.cpp | 1 + .../lib/libomptarget-spirv64-spirv64-intel.bc | 0 clang/test/Driver/spirv-openmp-toolchain.c| 71 +++ clang/test/Driver/spirv-toolchain.cl | 6 +- .../ClangLinkerWrapper.cpp| 17 ++-- .../llvm/Frontend/Offloading/Utility.h| 5 ++ llvm/include/llvm/TargetParser/Triple.h | 3 +- llvm/lib/Frontend/Offloading/CMakeLists.txt | 1 + llvm/lib/Frontend/Offloading/Utility.cpp | 86 +++ llvm/lib/TargetParser/Triple.cpp | 2 + 18 files changed, 296 insertions(+), 20 deletions(-) create mode 100644 clang/lib/Driver/ToolChains/SPIRVOpenMP.cpp create mode 100644 clang/lib/Driver/ToolChains/SPIRVOpenMP.h create mode 100644 clang/test/Driver/Inputs/spirv-openmp/lib/libomptarget-spirv64-spirv64-intel.bc create mode 100644 clang/test/Driver/spirv-openmp-toolchain.c diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index bed2a56b003512..a46fa1353af587 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -1493,6 +1493,8 @@ def libomptarget_amdgcn_bc_path_EQ : Joined<["--"], "libomptarget-amdgcn-bc-path HelpText<"Path to libomptarget-amdgcn bitcode library">, Alias; def libomptarget_nvptx_bc_path_EQ : Joined<["--"], "libomptarget-nvptx-bc-path=">, Group, HelpText<"Path to libomptarget-nvptx bitcode library">; +def libomptarget_spirv_bc_path_EQ : Joined<["--"], "libomptarget-spirv-bc-path=">, Group, + HelpText<"Path to libomptarget-spirv bitcode library">; def dD : Flag<["-"], "dD">, Group, Visibility<[ClangOption, CC1Option]>, HelpText<"Print macro definitions in -E mode in addition to normal output">; def dI : Flag<["-"], "dI">, Group, Visibility<[ClangOption, CC1Option]>, diff --git a/clang/lib/Driver/CMakeLists.txt b/clang/lib/Driver/CMakeLists.txt index 4fd10bf671512f..57d04c3fefa843 100644 --- a/clang/lib/Driver/CMakeLists.txt +++ b/clang/lib/Driver/CMakeLists.txt @@ -77,6 +77,7 @@ add_clang_library(clangDriver ToolChains/RISCVToolchain.cpp ToolChains/Solaris.cpp ToolChains/SPIRV.cpp + ToolChains/SPIRVOpenMP.cpp ToolChains/TCE.cpp ToolChains/UEFI.cpp ToolChains/VEToolchain.cpp diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index dc84c1b9d1cc4e..c74a474f487d95 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -43,6 +43,7 @@ #include "ToolChains/PS4CPU.h" #include "ToolChains/RISCVToolchain.h" #include "ToolChains/SPIRV.h" +#include "ToolChains/SPIRVOpenMP.h" #include "ToolChains/Solaris.h" #include "ToolChains/TCE.h" #include "ToolChains/UEFI.h" @@ -166,6 +167,20 @@ getHIPOffloadTargetTriple(const Driver &D, const ArgList &Args) { return std::nullopt; } +static std::optional +getSPIRVOffloadTargetTriple(const Driver &D, const ArgList &Args) { + if (!Args.hasArg(options::OPT_offload_EQ)) +return llvm::Triple( +"spirv64-intel"); // Only vendor "intel" is currently supported. + auto TT = getOffloadTargetTri
[clang] [llvm] [Driver][clang-linker-wrapper] Add initial support for OpenMP offloading to generic SPIR-V (PR #120145)
https://github.com/sarnex updated https://github.com/llvm/llvm-project/pull/120145 >From e1b9b503b1e9b8ebf5a9c94dcefd0c47ab009019 Mon Sep 17 00:00:00 2001 From: "Sarnie, Nick" Date: Mon, 16 Dec 2024 09:25:44 -0800 Subject: [PATCH 01/11] [Driver][clang-linker-wrapper] Add initial support for OpenMP offloading to generic SPIR-V This is the first of a series of patches to add support for OpenMP offloading to SPIR-V through liboffload with the first intended target being Intel GPUs. This patch implements the basic driver and `clang-linker-wrapper` work for JIT mode. There are still many missing pieces, so this is not yet usable. We introduce `spirv64-intel-unknown` as the only currently supported triple. The user-facing argument to enable offloading will be `-fopenmp -fopenmp-targets=spirv64-intel` Add a new `SPIRVOpenMPToolChain` toolchain based on the existing general SPIR-V toolchain which will call all the required SPIR-V tools as well as add the device RTL as an argument to the linker. As there is no production quality SPIR-V linker available, manually create an ELF binary containing the offloading image in a way that fits into the existing `liboffload` infrastructure. This ELF will eventually be passed to a runtime plugin that interacts with the Intel GPU runtime. There is also a small fix to an issue I found when trying to assemble SPIR-V when in text format. Signed-off-by: Sarnie, Nick --- clang/include/clang/Driver/Options.td | 2 + clang/lib/Driver/CMakeLists.txt | 1 + clang/lib/Driver/Driver.cpp | 40 +++-- clang/lib/Driver/ToolChains/CommonArgs.cpp| 9 +- clang/lib/Driver/ToolChains/SPIRV.cpp | 5 +- clang/lib/Driver/ToolChains/SPIRV.h | 2 +- clang/lib/Driver/ToolChains/SPIRVOpenMP.cpp | 36 clang/lib/Driver/ToolChains/SPIRVOpenMP.h | 29 +++ clang/lib/Frontend/CompilerInvocation.cpp | 1 + .../lib/libomptarget-spirv64-spirv64-intel.bc | 0 clang/test/Driver/spirv-openmp-toolchain.c| 71 +++ clang/test/Driver/spirv-toolchain.cl | 6 +- .../ClangLinkerWrapper.cpp| 17 ++-- .../llvm/Frontend/Offloading/Utility.h| 5 ++ llvm/include/llvm/TargetParser/Triple.h | 3 +- llvm/lib/Frontend/Offloading/CMakeLists.txt | 1 + llvm/lib/Frontend/Offloading/Utility.cpp | 86 +++ llvm/lib/TargetParser/Triple.cpp | 2 + 18 files changed, 296 insertions(+), 20 deletions(-) create mode 100644 clang/lib/Driver/ToolChains/SPIRVOpenMP.cpp create mode 100644 clang/lib/Driver/ToolChains/SPIRVOpenMP.h create mode 100644 clang/test/Driver/Inputs/spirv-openmp/lib/libomptarget-spirv64-spirv64-intel.bc create mode 100644 clang/test/Driver/spirv-openmp-toolchain.c diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index bed2a56b003512..a46fa1353af587 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -1493,6 +1493,8 @@ def libomptarget_amdgcn_bc_path_EQ : Joined<["--"], "libomptarget-amdgcn-bc-path HelpText<"Path to libomptarget-amdgcn bitcode library">, Alias; def libomptarget_nvptx_bc_path_EQ : Joined<["--"], "libomptarget-nvptx-bc-path=">, Group, HelpText<"Path to libomptarget-nvptx bitcode library">; +def libomptarget_spirv_bc_path_EQ : Joined<["--"], "libomptarget-spirv-bc-path=">, Group, + HelpText<"Path to libomptarget-spirv bitcode library">; def dD : Flag<["-"], "dD">, Group, Visibility<[ClangOption, CC1Option]>, HelpText<"Print macro definitions in -E mode in addition to normal output">; def dI : Flag<["-"], "dI">, Group, Visibility<[ClangOption, CC1Option]>, diff --git a/clang/lib/Driver/CMakeLists.txt b/clang/lib/Driver/CMakeLists.txt index 4fd10bf671512f..57d04c3fefa843 100644 --- a/clang/lib/Driver/CMakeLists.txt +++ b/clang/lib/Driver/CMakeLists.txt @@ -77,6 +77,7 @@ add_clang_library(clangDriver ToolChains/RISCVToolchain.cpp ToolChains/Solaris.cpp ToolChains/SPIRV.cpp + ToolChains/SPIRVOpenMP.cpp ToolChains/TCE.cpp ToolChains/UEFI.cpp ToolChains/VEToolchain.cpp diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index dc84c1b9d1cc4e..c74a474f487d95 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -43,6 +43,7 @@ #include "ToolChains/PS4CPU.h" #include "ToolChains/RISCVToolchain.h" #include "ToolChains/SPIRV.h" +#include "ToolChains/SPIRVOpenMP.h" #include "ToolChains/Solaris.h" #include "ToolChains/TCE.h" #include "ToolChains/UEFI.h" @@ -166,6 +167,20 @@ getHIPOffloadTargetTriple(const Driver &D, const ArgList &Args) { return std::nullopt; } +static std::optional +getSPIRVOffloadTargetTriple(const Driver &D, const ArgList &Args) { + if (!Args.hasArg(options::OPT_offload_EQ)) +return llvm::Triple( +"spirv64-intel"); // Only vendor "intel" is currently supported. + auto TT = getOffloadTargetTri
[clang] [llvm] [clang-linker-wrapper] Add ELF packaging for spirv64-intel OpenMP images (PR #125737)
https://github.com/sarnex updated https://github.com/llvm/llvm-project/pull/125737 >From d4b3358e1ccbae6889aaef280431f06a115102e0 Mon Sep 17 00:00:00 2001 From: "Sarnie, Nick" Date: Wed, 22 Jan 2025 08:35:49 -0800 Subject: [PATCH] [clang-linker-wrapper] Add ELF packaging for spirv64-intel OpenMP images Signed-off-by: Sarnie, Nick --- .../Inputs/clang-linker-wrapper-spirv-elf.o | Bin 0 -> 1344 bytes .../clang-linker-wrapper-spirv-elf.cpp| 14 +++ clang/test/Tooling/lit.local.cfg | 6 ++ clang/test/lit.site.cfg.py.in | 1 + .../ClangLinkerWrapper.cpp| 15 .../llvm/Frontend/Offloading/Utility.h| 6 ++ llvm/lib/Frontend/Offloading/CMakeLists.txt | 1 + llvm/lib/Frontend/Offloading/Utility.cpp | 85 ++ 8 files changed, 128 insertions(+) create mode 100644 clang/test/Tooling/Inputs/clang-linker-wrapper-spirv-elf.o create mode 100644 clang/test/Tooling/clang-linker-wrapper-spirv-elf.cpp diff --git a/clang/test/Tooling/Inputs/clang-linker-wrapper-spirv-elf.o b/clang/test/Tooling/Inputs/clang-linker-wrapper-spirv-elf.o new file mode 100644 index ..3e5bddcedfff16395b215ef438b5c4ce7c4ada9b GIT binary patch literal 1344 zcmb_bPiqrV5T8wIs#a?%B0@a`yi_Ecq)D2Mh(t=YlwgZc(1Wy_-M7iw{m14Rs|b1$ zz4-+MzkuJN;K8rpu@^spcOlN~&ZgT(y*W$v_kO>bc{4lj<=saAVL{U%GY#&;3CmP~ zqr%C$mn?g55hHguKmGc#@rRVg(ev+6o|eB+{xKUld7 z!FpdGOB*n+nB-Az5uce^o*4`)6A_QyFsQ87b$o`dtgMWrjK+6sH6){#1KkS!n&(Z@ zS2c>m7s8S{MYgSGceT-K7;8Ns_x-F+OXg9@es(FnIYj2)_ zc39SyFU_C-jLqIeoql9j==ET+{GqUZxwM4=#Q$Ms)aT&$^elJ!#iu>jkzSomkZYk< z;V_y{Hs3_jr8ty8pYMx6f}>ke1itA8pa-E8`lH9Y)v+|K1JG$adgwS_XxeTtOoctu zwsE$|V&R#(80|T+=?lPPiKRI}eC-qdCwqqbkfa^?7p#w?n&N=(~s^^-g-=u zv&lJV{&~BDWT8I|5#HzZ9VW+l5!dS3;w4Y&*Oj linkDevice(ArrayRef InputFiles, } } +Error containerizeRawImage(std::unique_ptr &Img, OffloadKind Kind, + const ArgList &Args) { + llvm::Triple Triple(Args.getLastArgValue(OPT_triple_EQ)); + if (Kind != OFK_OpenMP || !Triple.isSPIRV() || + Triple.getVendor() != llvm::Triple::Intel) +return Error::success(); + if (Error E = offloading::intel::containerizeOpenMPSPIRVImage(Img)) +return E; + return Error::success(); +} + Expected writeOffloadFile(const OffloadFile &File) { const OffloadBinary &Binary = *File.getBinary(); @@ -960,6 +971,10 @@ Expected> linkAndWrapDeviceFiles( return createFileError(*OutputOrErr, EC); } + // Manually containerize offloading images not in ELF format. + if (Error E = containerizeRawImage(*FileOrErr, Kind, LinkerArgs)) +return E; + std::scoped_lock Guard(ImageMtx); OffloadingImage TheImage{}; TheImage.TheImageKind = diff --git a/llvm/include/llvm/Frontend/Offloading/Utility.h b/llvm/include/llvm/Frontend/Offloading/Utility.h index 7932fd5acbe1e26..9140371a8c2ed21 100644 --- a/llvm/include/llvm/Frontend/Offloading/Utility.h +++ b/llvm/include/llvm/Frontend/Offloading/Utility.h @@ -10,6 +10,7 @@ #define LLVM_FRONTEND_OFFLOADING_UTILITY_H #include +#include #include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringRef.h" @@ -152,6 +153,11 @@ Error getAMDGPUMetaDataFromImage(MemoryBufferRef MemBuffer, StringMap &KernelInfoMap, uint16_t &ELFABIVersion); } // namespace amdgpu +namespace intel { +/// Containerizes an offloading binary into the ELF binary format expected by +/// the Intel runtime offload plugin. +Error containerizeOpenMPSPIRVImage(std::unique_ptr &Binary); +} // namespace intel } // namespace offloading } // namespace llvm diff --git a/llvm/lib/Frontend/Offloading/CMakeLists.txt b/llvm/lib/Frontend/Offloading/CMakeLists.txt index ce445ad9cc4cb60..8e1ede9c72b391a 100644 --- a/llvm/lib/Frontend/Offloading/CMakeLists.txt +++ b/llvm/lib/Frontend/Offloading/CMakeLists.txt @@ -12,6 +12,7 @@ add_llvm_component_library(LLVMFrontendOffloading Core BinaryFormat Object + ObjectYAML Support TransformUtils TargetParser diff --git a/llvm/lib/Frontend/Offloading/Utility.cpp b/llvm/lib/Frontend/Offloading/Utility.cpp index 8117a42b8a45cd1..f9c74ab975d1023 100644 --- a/llvm/lib/Frontend/Offloading/Utility.cpp +++ b/llvm/lib/Frontend/Offloading/Utility.cpp @@ -15,6 +15,8 @@ #include "llvm/IR/GlobalVariable.h" #include "llvm/IR/Value.h" #include "llvm/Object/ELFObjectFile.h" +#include "llvm/ObjectYAML/ELFYAML.h" +#include "llvm/ObjectYAML/yaml2obj.h" #include "llvm/Support/MemoryBufferRef.h" #include "llvm/Transforms/Utils/ModuleUtils.h" @@ -373,3 +375,86 @@ Error llvm::offloading::amdgpu::getAMDGPUMetaDataFromImage( } return Error::success(); } +Error offloading::intel::containerizeOpenMPSPIRVImage( +std::unique_ptr &Img) { + constexpr char INTEL_ONEOMP_OFFLOAD_VERSION[] = "1.0"; + constexpr int NT_INTEL_ONEOMP_OFFLOAD_VERSION = 1; + constexpr int NT_INTEL_ONEOMP_OFFLOAD_IM
[clang] [llvm] [clang-linker-wrapper] Add ELF packaging for spirv64-intel OpenMP images (PR #125737)
https://github.com/sarnex updated https://github.com/llvm/llvm-project/pull/125737 >From d4b3358e1ccbae6889aaef280431f06a115102e0 Mon Sep 17 00:00:00 2001 From: "Sarnie, Nick" Date: Wed, 22 Jan 2025 08:35:49 -0800 Subject: [PATCH 1/2] [clang-linker-wrapper] Add ELF packaging for spirv64-intel OpenMP images Signed-off-by: Sarnie, Nick --- .../Inputs/clang-linker-wrapper-spirv-elf.o | Bin 0 -> 1344 bytes .../clang-linker-wrapper-spirv-elf.cpp| 14 +++ clang/test/Tooling/lit.local.cfg | 6 ++ clang/test/lit.site.cfg.py.in | 1 + .../ClangLinkerWrapper.cpp| 15 .../llvm/Frontend/Offloading/Utility.h| 6 ++ llvm/lib/Frontend/Offloading/CMakeLists.txt | 1 + llvm/lib/Frontend/Offloading/Utility.cpp | 85 ++ 8 files changed, 128 insertions(+) create mode 100644 clang/test/Tooling/Inputs/clang-linker-wrapper-spirv-elf.o create mode 100644 clang/test/Tooling/clang-linker-wrapper-spirv-elf.cpp diff --git a/clang/test/Tooling/Inputs/clang-linker-wrapper-spirv-elf.o b/clang/test/Tooling/Inputs/clang-linker-wrapper-spirv-elf.o new file mode 100644 index ..3e5bddcedfff16395b215ef438b5c4ce7c4ada9b GIT binary patch literal 1344 zcmb_bPiqrV5T8wIs#a?%B0@a`yi_Ecq)D2Mh(t=YlwgZc(1Wy_-M7iw{m14Rs|b1$ zz4-+MzkuJN;K8rpu@^spcOlN~&ZgT(y*W$v_kO>bc{4lj<=saAVL{U%GY#&;3CmP~ zqr%C$mn?g55hHguKmGc#@rRVg(ev+6o|eB+{xKUld7 z!FpdGOB*n+nB-Az5uce^o*4`)6A_QyFsQ87b$o`dtgMWrjK+6sH6){#1KkS!n&(Z@ zS2c>m7s8S{MYgSGceT-K7;8Ns_x-F+OXg9@es(FnIYj2)_ zc39SyFU_C-jLqIeoql9j==ET+{GqUZxwM4=#Q$Ms)aT&$^elJ!#iu>jkzSomkZYk< z;V_y{Hs3_jr8ty8pYMx6f}>ke1itA8pa-E8`lH9Y)v+|K1JG$adgwS_XxeTtOoctu zwsE$|V&R#(80|T+=?lPPiKRI}eC-qdCwqqbkfa^?7p#w?n&N=(~s^^-g-=u zv&lJV{&~BDWT8I|5#HzZ9VW+l5!dS3;w4Y&*Oj linkDevice(ArrayRef InputFiles, } } +Error containerizeRawImage(std::unique_ptr &Img, OffloadKind Kind, + const ArgList &Args) { + llvm::Triple Triple(Args.getLastArgValue(OPT_triple_EQ)); + if (Kind != OFK_OpenMP || !Triple.isSPIRV() || + Triple.getVendor() != llvm::Triple::Intel) +return Error::success(); + if (Error E = offloading::intel::containerizeOpenMPSPIRVImage(Img)) +return E; + return Error::success(); +} + Expected writeOffloadFile(const OffloadFile &File) { const OffloadBinary &Binary = *File.getBinary(); @@ -960,6 +971,10 @@ Expected> linkAndWrapDeviceFiles( return createFileError(*OutputOrErr, EC); } + // Manually containerize offloading images not in ELF format. + if (Error E = containerizeRawImage(*FileOrErr, Kind, LinkerArgs)) +return E; + std::scoped_lock Guard(ImageMtx); OffloadingImage TheImage{}; TheImage.TheImageKind = diff --git a/llvm/include/llvm/Frontend/Offloading/Utility.h b/llvm/include/llvm/Frontend/Offloading/Utility.h index 7932fd5acbe1e2..9140371a8c2ed2 100644 --- a/llvm/include/llvm/Frontend/Offloading/Utility.h +++ b/llvm/include/llvm/Frontend/Offloading/Utility.h @@ -10,6 +10,7 @@ #define LLVM_FRONTEND_OFFLOADING_UTILITY_H #include +#include #include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringRef.h" @@ -152,6 +153,11 @@ Error getAMDGPUMetaDataFromImage(MemoryBufferRef MemBuffer, StringMap &KernelInfoMap, uint16_t &ELFABIVersion); } // namespace amdgpu +namespace intel { +/// Containerizes an offloading binary into the ELF binary format expected by +/// the Intel runtime offload plugin. +Error containerizeOpenMPSPIRVImage(std::unique_ptr &Binary); +} // namespace intel } // namespace offloading } // namespace llvm diff --git a/llvm/lib/Frontend/Offloading/CMakeLists.txt b/llvm/lib/Frontend/Offloading/CMakeLists.txt index ce445ad9cc4cb6..8e1ede9c72b391 100644 --- a/llvm/lib/Frontend/Offloading/CMakeLists.txt +++ b/llvm/lib/Frontend/Offloading/CMakeLists.txt @@ -12,6 +12,7 @@ add_llvm_component_library(LLVMFrontendOffloading Core BinaryFormat Object + ObjectYAML Support TransformUtils TargetParser diff --git a/llvm/lib/Frontend/Offloading/Utility.cpp b/llvm/lib/Frontend/Offloading/Utility.cpp index 8117a42b8a45cd..f9c74ab975d102 100644 --- a/llvm/lib/Frontend/Offloading/Utility.cpp +++ b/llvm/lib/Frontend/Offloading/Utility.cpp @@ -15,6 +15,8 @@ #include "llvm/IR/GlobalVariable.h" #include "llvm/IR/Value.h" #include "llvm/Object/ELFObjectFile.h" +#include "llvm/ObjectYAML/ELFYAML.h" +#include "llvm/ObjectYAML/yaml2obj.h" #include "llvm/Support/MemoryBufferRef.h" #include "llvm/Transforms/Utils/ModuleUtils.h" @@ -373,3 +375,86 @@ Error llvm::offloading::amdgpu::getAMDGPUMetaDataFromImage( } return Error::success(); } +Error offloading::intel::containerizeOpenMPSPIRVImage( +std::unique_ptr &Img) { + constexpr char INTEL_ONEOMP_OFFLOAD_VERSION[] = "1.0"; + constexpr int NT_INTEL_ONEOMP_OFFLOAD_VERSION = 1; + constexpr int NT_INTEL_ONEOMP_OFFLOAD_IMAG
[clang] [llvm] [clang-linker-wrapper] Add ELF packaging for spirv64-intel OpenMP images (PR #125737)
@@ -373,3 +375,86 @@ Error llvm::offloading::amdgpu::getAMDGPUMetaDataFromImage( } return Error::success(); } +Error offloading::intel::containerizeOpenMPSPIRVImage( +std::unique_ptr &Img) { + constexpr char INTEL_ONEOMP_OFFLOAD_VERSION[] = "1.0"; + constexpr int NT_INTEL_ONEOMP_OFFLOAD_VERSION = 1; + constexpr int NT_INTEL_ONEOMP_OFFLOAD_IMAGE_COUNT = 2; + constexpr int NT_INTEL_ONEOMP_OFFLOAD_IMAGE_AUX = 3; + + // Start creating notes for the ELF container. + std::vector Notes; + std::string Version = toHex(INTEL_ONEOMP_OFFLOAD_VERSION); + Notes.emplace_back(ELFYAML::NoteEntry{"INTELONEOMPOFFLOAD", +yaml::BinaryRef(Version), +NT_INTEL_ONEOMP_OFFLOAD_VERSION}); + + // The AuxInfo string will hold auxiliary information for the image. + // ELFYAML::NoteEntry structures will hold references to the + // string, so we have to make sure the string is valid. + std::string AuxInfo; + + // TODO: Pass compile/link opts + StringRef CompileOpts = ""; + StringRef LinkOpts = ""; + + unsigned ImageFmt = 1; // SPIR-V format + + AuxInfo = toHex((Twine(0) + Twine('\0') + Twine(ImageFmt) + Twine('\0') + + CompileOpts + Twine('\0') + LinkOpts) + .str()); + Notes.emplace_back(ELFYAML::NoteEntry{"INTELONEOMPOFFLOAD", +yaml::BinaryRef(AuxInfo), +NT_INTEL_ONEOMP_OFFLOAD_IMAGE_AUX}); + + std::string ImgCount = toHex(Twine(1).str()); // always one image per ELF + Notes.emplace_back(ELFYAML::NoteEntry{"INTELONEOMPOFFLOAD", +yaml::BinaryRef(ImgCount), +NT_INTEL_ONEOMP_OFFLOAD_IMAGE_COUNT}); + + std::string YamlFile; + llvm::raw_string_ostream YamlFileStream(YamlFile); + + // Write YAML template file. + { sarnex wrote: No, will remove, thanks https://github.com/llvm/llvm-project/pull/125737 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [clang-linker-wrapper] Add ELF packaging for spirv64-intel OpenMP images (PR #125737)
sarnex wrote: Sorry, I thought the feedback from the other PR was to check in a binary, will remove and generate it as part of the test. https://github.com/llvm/llvm-project/pull/125737 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [clang-linker-wrapper] Add ELF packaging for spirv64-intel OpenMP images (PR #125737)
https://github.com/sarnex edited https://github.com/llvm/llvm-project/pull/125737 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [clang-linker-wrapper] Add ELF packaging for spirv64-intel OpenMP images (PR #125737)
https://github.com/sarnex edited https://github.com/llvm/llvm-project/pull/125737 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [clang-linker-wrapper] Add ELF packaging for spirv64-intel OpenMP images (PR #125737)
https://github.com/sarnex edited https://github.com/llvm/llvm-project/pull/125737 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [clang-linker-wrapper] Add ELF packaging for spirv64-intel OpenMP images (PR #125737)
https://github.com/sarnex edited https://github.com/llvm/llvm-project/pull/125737 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add __has_target_builtin macro (PR #126324)
https://github.com/sarnex created https://github.com/llvm/llvm-project/pull/126324 None >From fe555ce59762edf71f43d0cd61978d60f5f949ef Mon Sep 17 00:00:00 2001 From: "Sarnie, Nick" Date: Fri, 7 Feb 2025 14:57:12 -0800 Subject: [PATCH] [Clang] Add __has_target_builtin macro Signed-off-by: Sarnie, Nick --- clang/docs/LanguageExtensions.rst | 33 +++ clang/include/clang/Lex/Preprocessor.h| 1 + clang/lib/Lex/PPMacroExpansion.cpp| 17 +++--- .../test/Preprocessor/has_target_builtin.cpp | 18 ++ 4 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 clang/test/Preprocessor/has_target_builtin.cpp diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst index 973cf8f9d091c30..87f7df50471e790 100644 --- a/clang/docs/LanguageExtensions.rst +++ b/clang/docs/LanguageExtensions.rst @@ -67,6 +67,10 @@ It can be used like this: ``__has_builtin`` should not be used to detect support for a builtin macro; use ``#ifdef`` instead. + When using device offloading, a builtin is considered available if it is + available on either the host or the device targets. + Use ``__has_target_builtin`` to consider only the current target. + ``__has_constexpr_builtin`` --- @@ -96,6 +100,35 @@ the header file to conditionally make a function constexpr whenever the constant evaluation of the corresponding builtin (for example, ``std::fmax`` calls ``__builtin_fmax``) is supported in Clang. +``__has_target_builtin`` +- + +This function-like macro takes a single identifier argument that is the name of +a builtin function, a builtin pseudo-function (taking one or more type +arguments), or a builtin template. +It evaluates to 1 if the builtin is supported on the current target or 0 if not. +The behavior is different than ``__has_builtin`` when there is an auxiliary target, +such when offloading to a target device. +It can be used like this: + +.. code-block:: c++ + + #ifndef __has_target_builtin // Optional of course. +#define __has_target_builtin(x) 0 // Compatibility with non-clang compilers. + #endif + + ... + #if __has_target_builtin(__builtin_trap) +__builtin_trap(); + #else +abort(); + #endif + ... + +.. note:: + ``__has_target_builtin`` should not be used to detect support for a builtin macro; + use ``#ifdef`` instead. + .. _langext-__has_feature-__has_extension: ``__has_feature`` and ``__has_extension`` diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h index 2bf4d1a16699430..240fe28aba93e33 100644 --- a/clang/include/clang/Lex/Preprocessor.h +++ b/clang/include/clang/Lex/Preprocessor.h @@ -174,6 +174,7 @@ class Preprocessor { IdentifierInfo *Ident__has_extension;// __has_extension IdentifierInfo *Ident__has_builtin; // __has_builtin IdentifierInfo *Ident__has_constexpr_builtin;// __has_constexpr_builtin + IdentifierInfo *Ident__has_target_builtin; // __has_target_builtin IdentifierInfo *Ident__has_attribute;// __has_attribute IdentifierInfo *Ident__has_embed;// __has_embed IdentifierInfo *Ident__has_include; // __has_include diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp index 347c13da0ad215a..b7b870e1a7fca82 100644 --- a/clang/lib/Lex/PPMacroExpansion.cpp +++ b/clang/lib/Lex/PPMacroExpansion.cpp @@ -357,6 +357,7 @@ void Preprocessor::RegisterBuiltinMacros() { Ident__has_builtin = RegisterBuiltinMacro("__has_builtin"); Ident__has_constexpr_builtin = RegisterBuiltinMacro("__has_constexpr_builtin"); + Ident__has_target_builtin = RegisterBuiltinMacro("__has_target_builtin"); Ident__has_attribute = RegisterBuiltinMacro("__has_attribute"); if (!getLangOpts().CPlusPlus) Ident__has_c_attribute = RegisterBuiltinMacro("__has_c_attribute"); @@ -1797,15 +1798,17 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) { diag::err_feature_check_malformed); return II && HasExtension(*this, II->getName()); }); - } else if (II == Ident__has_builtin) { + } else if (II == Ident__has_builtin || II == Ident__has_target_builtin) { +bool IsHasTargetBuiltin = II == Ident__has_target_builtin; EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false, - [this](Token &Tok, bool &HasLexedNextToken) -> int { + [this, IsHasTargetBuiltin](Token &Tok, bool &HasLexedNextToken) -> int { IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this, diag::err_feature_check_malformed); if (!II) return false; -else if (II->getBuiltinID() != 0) { - switch (II->getBuiltinID()) { +auto BuiltinID = II->getBuiltinID(); +if (BuiltinID != 0) { + switch (BuiltinID) { case B
[clang] [Clang] Add __has_target_builtin macro (PR #126324)
https://github.com/sarnex edited https://github.com/llvm/llvm-project/pull/126324 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add __has_target_builtin macro (PR #126324)
https://github.com/sarnex updated https://github.com/llvm/llvm-project/pull/126324 >From fe555ce59762edf71f43d0cd61978d60f5f949ef Mon Sep 17 00:00:00 2001 From: "Sarnie, Nick" Date: Fri, 7 Feb 2025 14:57:12 -0800 Subject: [PATCH 1/2] [Clang] Add __has_target_builtin macro Signed-off-by: Sarnie, Nick --- clang/docs/LanguageExtensions.rst | 33 +++ clang/include/clang/Lex/Preprocessor.h| 1 + clang/lib/Lex/PPMacroExpansion.cpp| 17 +++--- .../test/Preprocessor/has_target_builtin.cpp | 18 ++ 4 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 clang/test/Preprocessor/has_target_builtin.cpp diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst index 973cf8f9d091c30..87f7df50471e790 100644 --- a/clang/docs/LanguageExtensions.rst +++ b/clang/docs/LanguageExtensions.rst @@ -67,6 +67,10 @@ It can be used like this: ``__has_builtin`` should not be used to detect support for a builtin macro; use ``#ifdef`` instead. + When using device offloading, a builtin is considered available if it is + available on either the host or the device targets. + Use ``__has_target_builtin`` to consider only the current target. + ``__has_constexpr_builtin`` --- @@ -96,6 +100,35 @@ the header file to conditionally make a function constexpr whenever the constant evaluation of the corresponding builtin (for example, ``std::fmax`` calls ``__builtin_fmax``) is supported in Clang. +``__has_target_builtin`` +- + +This function-like macro takes a single identifier argument that is the name of +a builtin function, a builtin pseudo-function (taking one or more type +arguments), or a builtin template. +It evaluates to 1 if the builtin is supported on the current target or 0 if not. +The behavior is different than ``__has_builtin`` when there is an auxiliary target, +such when offloading to a target device. +It can be used like this: + +.. code-block:: c++ + + #ifndef __has_target_builtin // Optional of course. +#define __has_target_builtin(x) 0 // Compatibility with non-clang compilers. + #endif + + ... + #if __has_target_builtin(__builtin_trap) +__builtin_trap(); + #else +abort(); + #endif + ... + +.. note:: + ``__has_target_builtin`` should not be used to detect support for a builtin macro; + use ``#ifdef`` instead. + .. _langext-__has_feature-__has_extension: ``__has_feature`` and ``__has_extension`` diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h index 2bf4d1a16699430..240fe28aba93e33 100644 --- a/clang/include/clang/Lex/Preprocessor.h +++ b/clang/include/clang/Lex/Preprocessor.h @@ -174,6 +174,7 @@ class Preprocessor { IdentifierInfo *Ident__has_extension;// __has_extension IdentifierInfo *Ident__has_builtin; // __has_builtin IdentifierInfo *Ident__has_constexpr_builtin;// __has_constexpr_builtin + IdentifierInfo *Ident__has_target_builtin; // __has_target_builtin IdentifierInfo *Ident__has_attribute;// __has_attribute IdentifierInfo *Ident__has_embed;// __has_embed IdentifierInfo *Ident__has_include; // __has_include diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp index 347c13da0ad215a..b7b870e1a7fca82 100644 --- a/clang/lib/Lex/PPMacroExpansion.cpp +++ b/clang/lib/Lex/PPMacroExpansion.cpp @@ -357,6 +357,7 @@ void Preprocessor::RegisterBuiltinMacros() { Ident__has_builtin = RegisterBuiltinMacro("__has_builtin"); Ident__has_constexpr_builtin = RegisterBuiltinMacro("__has_constexpr_builtin"); + Ident__has_target_builtin = RegisterBuiltinMacro("__has_target_builtin"); Ident__has_attribute = RegisterBuiltinMacro("__has_attribute"); if (!getLangOpts().CPlusPlus) Ident__has_c_attribute = RegisterBuiltinMacro("__has_c_attribute"); @@ -1797,15 +1798,17 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) { diag::err_feature_check_malformed); return II && HasExtension(*this, II->getName()); }); - } else if (II == Ident__has_builtin) { + } else if (II == Ident__has_builtin || II == Ident__has_target_builtin) { +bool IsHasTargetBuiltin = II == Ident__has_target_builtin; EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false, - [this](Token &Tok, bool &HasLexedNextToken) -> int { + [this, IsHasTargetBuiltin](Token &Tok, bool &HasLexedNextToken) -> int { IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this, diag::err_feature_check_malformed); if (!II) return false; -else if (II->getBuiltinID() != 0) { - switch (II->getBuiltinID()) { +auto BuiltinID = II->getBuiltinID(); +if (BuiltinID != 0) { + switch (BuiltinID) { case Bui
[clang] [LinkerWrapper] Clean up options after proper forwarding (PR #126297)
@@ -9223,6 +9223,8 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA, Args.MakeArgString("--host-triple=" + getToolChain().getTripleString())); if (Args.hasArg(options::OPT_v)) CmdArgs.push_back("--wrapper-verbose"); + if (Arg *A = Args.getLastArg(options::OPT_cuda_path_EQ)) sarnex wrote: i see this is already in the `CompilerOptions` set above, do we need this explicit handling because `--cuda-path` is a `clang-linker-wrapper` option as well as a clang one? if so, is there an easy way we could remove one of the two? https://github.com/llvm/llvm-project/pull/126297 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [LinkerWrapper] Clean up options after proper forwarding (PR #126297)
https://github.com/sarnex edited https://github.com/llvm/llvm-project/pull/126297 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [LinkerWrapper] Clean up options after proper forwarding (PR #126297)
https://github.com/sarnex edited https://github.com/llvm/llvm-project/pull/126297 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [LinkerWrapper] Clean up options after proper forwarding (PR #126297)
@@ -21,16 +21,16 @@ __attribute__((visibility("protected"), used)) int x; // RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \ // RUN: --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=NVPTX-LINK -// NVPTX-LINK: clang{{.*}} -o {{.*}}.img --target=nvptx64-nvidia-cuda -march=sm_70 -O2 -flto {{.*}}.o {{.*}}.o +// NVPTX-LINK: clang{{.*}} -o {{.*}}.img --target=nvptx64-nvidia-cuda -march=sm_70 {{.*}}.o {{.*}}.o // RUN: clang-offload-packager -o %t.out \ // RUN: --image=file=%t.elf.o,kind=openmp,triple=nvptx64-nvidia-cuda,arch=sm_70 \ // RUN: --image=file=%t.elf.o,kind=openmp,triple=nvptx64-nvidia-cuda,arch=sm_70 // RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.o -fembed-offload-object=%t.out -// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run --device-debug -O0 \ +// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run --device-compiler=-g \ // RUN: --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=NVPTX-LINK-DEBUG -// NVPTX-LINK-DEBUG: clang{{.*}} -o {{.*}}.img --target=nvptx64-nvidia-cuda -march=sm_70 -O2 -flto {{.*}}.o {{.*}}.o -g sarnex wrote: in all cases where we removed `-flto` from the test, it's still passed as part of the `--device-compiler` string but we just aren't checking that right? https://github.com/llvm/llvm-project/pull/126297 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [LinkerWrapper] Clean up options after proper forwarding (PR #126297)
@@ -9223,6 +9223,8 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA, Args.MakeArgString("--host-triple=" + getToolChain().getTripleString())); if (Args.hasArg(options::OPT_v)) CmdArgs.push_back("--wrapper-verbose"); + if (Arg *A = Args.getLastArg(options::OPT_cuda_path_EQ)) sarnex wrote: thanks, makes sense https://github.com/llvm/llvm-project/pull/126297 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add __has_target_builtin macro (PR #126324)
https://github.com/sarnex updated https://github.com/llvm/llvm-project/pull/126324 >From fe555ce59762edf71f43d0cd61978d60f5f949ef Mon Sep 17 00:00:00 2001 From: "Sarnie, Nick" Date: Fri, 7 Feb 2025 14:57:12 -0800 Subject: [PATCH 1/3] [Clang] Add __has_target_builtin macro Signed-off-by: Sarnie, Nick --- clang/docs/LanguageExtensions.rst | 33 +++ clang/include/clang/Lex/Preprocessor.h| 1 + clang/lib/Lex/PPMacroExpansion.cpp| 17 +++--- .../test/Preprocessor/has_target_builtin.cpp | 18 ++ 4 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 clang/test/Preprocessor/has_target_builtin.cpp diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst index 973cf8f9d091c30..87f7df50471e790 100644 --- a/clang/docs/LanguageExtensions.rst +++ b/clang/docs/LanguageExtensions.rst @@ -67,6 +67,10 @@ It can be used like this: ``__has_builtin`` should not be used to detect support for a builtin macro; use ``#ifdef`` instead. + When using device offloading, a builtin is considered available if it is + available on either the host or the device targets. + Use ``__has_target_builtin`` to consider only the current target. + ``__has_constexpr_builtin`` --- @@ -96,6 +100,35 @@ the header file to conditionally make a function constexpr whenever the constant evaluation of the corresponding builtin (for example, ``std::fmax`` calls ``__builtin_fmax``) is supported in Clang. +``__has_target_builtin`` +- + +This function-like macro takes a single identifier argument that is the name of +a builtin function, a builtin pseudo-function (taking one or more type +arguments), or a builtin template. +It evaluates to 1 if the builtin is supported on the current target or 0 if not. +The behavior is different than ``__has_builtin`` when there is an auxiliary target, +such when offloading to a target device. +It can be used like this: + +.. code-block:: c++ + + #ifndef __has_target_builtin // Optional of course. +#define __has_target_builtin(x) 0 // Compatibility with non-clang compilers. + #endif + + ... + #if __has_target_builtin(__builtin_trap) +__builtin_trap(); + #else +abort(); + #endif + ... + +.. note:: + ``__has_target_builtin`` should not be used to detect support for a builtin macro; + use ``#ifdef`` instead. + .. _langext-__has_feature-__has_extension: ``__has_feature`` and ``__has_extension`` diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h index 2bf4d1a16699430..240fe28aba93e33 100644 --- a/clang/include/clang/Lex/Preprocessor.h +++ b/clang/include/clang/Lex/Preprocessor.h @@ -174,6 +174,7 @@ class Preprocessor { IdentifierInfo *Ident__has_extension;// __has_extension IdentifierInfo *Ident__has_builtin; // __has_builtin IdentifierInfo *Ident__has_constexpr_builtin;// __has_constexpr_builtin + IdentifierInfo *Ident__has_target_builtin; // __has_target_builtin IdentifierInfo *Ident__has_attribute;// __has_attribute IdentifierInfo *Ident__has_embed;// __has_embed IdentifierInfo *Ident__has_include; // __has_include diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp index 347c13da0ad215a..b7b870e1a7fca82 100644 --- a/clang/lib/Lex/PPMacroExpansion.cpp +++ b/clang/lib/Lex/PPMacroExpansion.cpp @@ -357,6 +357,7 @@ void Preprocessor::RegisterBuiltinMacros() { Ident__has_builtin = RegisterBuiltinMacro("__has_builtin"); Ident__has_constexpr_builtin = RegisterBuiltinMacro("__has_constexpr_builtin"); + Ident__has_target_builtin = RegisterBuiltinMacro("__has_target_builtin"); Ident__has_attribute = RegisterBuiltinMacro("__has_attribute"); if (!getLangOpts().CPlusPlus) Ident__has_c_attribute = RegisterBuiltinMacro("__has_c_attribute"); @@ -1797,15 +1798,17 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) { diag::err_feature_check_malformed); return II && HasExtension(*this, II->getName()); }); - } else if (II == Ident__has_builtin) { + } else if (II == Ident__has_builtin || II == Ident__has_target_builtin) { +bool IsHasTargetBuiltin = II == Ident__has_target_builtin; EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false, - [this](Token &Tok, bool &HasLexedNextToken) -> int { + [this, IsHasTargetBuiltin](Token &Tok, bool &HasLexedNextToken) -> int { IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this, diag::err_feature_check_malformed); if (!II) return false; -else if (II->getBuiltinID() != 0) { - switch (II->getBuiltinID()) { +auto BuiltinID = II->getBuiltinID(); +if (BuiltinID != 0) { + switch (BuiltinID) { case Bui
[clang] [llvm] [clang-linker-wrapper][lit] Fix OpenMP SPIR-V ELF test again (PR #126142)
https://github.com/sarnex ready_for_review https://github.com/llvm/llvm-project/pull/126142 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [clang-linker-wrapper][lit] Fix OpenMP SPIR-V ELF test again (PR #126142)
https://github.com/sarnex closed https://github.com/llvm/llvm-project/pull/126142 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [LinkerWrapper] Clean up options after proper forwarding (PR #126297)
@@ -9220,13 +9220,24 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA, for (StringRef Arg : LinkerArgs) CmdArgs.push_back(Args.MakeArgString( "--device-linker=" + TC->getTripleString() + "=" + Arg)); + + // Forward the LTO mode relying on the Driver's parsing. + if (C.getDriver().getOffloadLTOMode() == LTOK_Full) +CmdArgs.push_back(Args.MakeArgString( sarnex wrote: should we add a test for this? https://github.com/llvm/llvm-project/pull/126297 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [LinkerWrapper] Clean up options after proper forwarding (PR #126297)
@@ -9220,13 +9220,24 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA, for (StringRef Arg : LinkerArgs) CmdArgs.push_back(Args.MakeArgString( "--device-linker=" + TC->getTripleString() + "=" + Arg)); + + // Forward the LTO mode relying on the Driver's parsing. + if (C.getDriver().getOffloadLTOMode() == LTOK_Full) +CmdArgs.push_back(Args.MakeArgString( +"--device-compiler=" + TC->getTripleString() + "=-flto=full")); sarnex wrote: this ends up getting passed to the `clang` call from `linkDevice`, where we are guaranteed we are not offloading (as in no two-phase compile) and we are compiling for the device offload triple right? basically im confirming that we never need to pass `-foffload-lto` instead of `-flto` https://github.com/llvm/llvm-project/pull/126297 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [LinkerWrapper] Clean up options after proper forwarding (PR #126297)
https://github.com/sarnex edited https://github.com/llvm/llvm-project/pull/126297 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [LinkerWrapper] Clean up options after proper forwarding (PR #126297)
@@ -9220,13 +9220,24 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA, for (StringRef Arg : LinkerArgs) CmdArgs.push_back(Args.MakeArgString( "--device-linker=" + TC->getTripleString() + "=" + Arg)); + + // Forward the LTO mode relying on the Driver's parsing. + if (C.getDriver().getOffloadLTOMode() == LTOK_Full) +CmdArgs.push_back(Args.MakeArgString( +"--device-compiler=" + TC->getTripleString() + "=-flto=full")); sarnex wrote: it makes sense to me, `-foffload-lto` is to specify the device triple in offloading targets, and `-flto` is for the main target https://github.com/llvm/llvm-project/pull/126297 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [LinkerWrapper] Clean up options after proper forwarding (PR #126297)
https://github.com/sarnex approved this pull request. https://github.com/llvm/llvm-project/pull/126297 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [OpenMP][OpenMPIRBuilder] Add initial changes for SPIR-V target frontend support (PR #125920)
sarnex wrote: @jhuber6 Ping on this one when you have time, thx :) https://github.com/llvm/llvm-project/pull/125920 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add __has_target_builtin macro (PR #126324)
https://github.com/sarnex ready_for_review https://github.com/llvm/llvm-project/pull/126324 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add __has_target_builtin macro (PR #126324)
https://github.com/sarnex edited https://github.com/llvm/llvm-project/pull/126324 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [OpenMP][OpenMPIRBuilder] Add initial changes for SPIR-V target frontend support (PR #125920)
https://github.com/sarnex edited https://github.com/llvm/llvm-project/pull/125920 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [OpenMP][OpenMPIRBuilder] Add initial changes for SPIR-V target frontend support (PR #125920)
https://github.com/sarnex edited https://github.com/llvm/llvm-project/pull/125920 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [OpenMP][OpenMPIRBuilder] Add initial changes for SPIR-V target frontend support (PR #125920)
https://github.com/sarnex edited https://github.com/llvm/llvm-project/pull/125920 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [OpenMP][OpenMPIRBuilder] Add initial changes for SPIR-V target frontend support (PR #125920)
https://github.com/sarnex closed https://github.com/llvm/llvm-project/pull/125920 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add __has_target_builtin macro (PR #126324)
@@ -1797,55 +1798,62 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) { diag::err_feature_check_malformed); return II && HasExtension(*this, II->getName()); }); - } else if (II == Ident__has_builtin) { sarnex wrote: The clang-format CI job fails if I don't apply all the suggested changes, which includes changes in unrelated parts of the code. I heard in the past we kind of do fix-as-you-go with clang-format where sometimes stuff like this happens. https://github.com/llvm/llvm-project/pull/126324 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [OpenMP] Remove 'libomptarget.devicertl.a' fatbinary and use static library (PR #126143)
sarnex wrote: > FYI you'll probably need to make the weird spir-v linker tool handle static > libraries. It's really easy since you just need to iterate over the archive > members since I doubt we care about real symbol resolution here. sounds good, im planning to open a pr to start building the devicertl for spir-v soon, probably after this is merged, so ill deal with it then https://github.com/llvm/llvm-project/pull/126143 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [OpenMP] Remove 'libomptarget.devicertl.a' fatbinary and use static library (PR #126143)
https://github.com/sarnex approved this pull request. not qualified to review in detail but idea sounds like a good cleanup to me https://github.com/llvm/llvm-project/pull/126143 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add __has_target_builtin macro (PR #126324)
@@ -1797,55 +1798,62 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) { diag::err_feature_check_malformed); return II && HasExtension(*this, II->getName()); }); - } else if (II == Ident__has_builtin) { sarnex wrote: Shouldn't be too bad, will try it. https://github.com/llvm/llvm-project/pull/126324 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add __has_target_builtin macro (PR #126324)
@@ -1797,55 +1798,62 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) { diag::err_feature_check_malformed); return II && HasExtension(*this, II->getName()); }); - } else if (II == Ident__has_builtin) { sarnex wrote: Thanks Aaron, I'll do that in the future. Yaxun marked this issue as resolved I'm going to assume I don't need to take any action this time, but I'm happy to if others prefer it. https://github.com/llvm/llvm-project/pull/126324 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add __has_target_builtin macro (PR #126324)
@@ -357,6 +357,7 @@ void Preprocessor::RegisterBuiltinMacros() { Ident__has_builtin = RegisterBuiltinMacro("__has_builtin"); Ident__has_constexpr_builtin = RegisterBuiltinMacro("__has_constexpr_builtin"); + Ident__has_target_builtin = RegisterBuiltinMacro("__has_target_builtin"); sarnex wrote: Thanks, will do this. I can't find a good way to detect offloading languages in general here, so I'm just going to check for CUDA/HIP/SYCLDevice/OpenMPDevice, let me know if there's some common logic I can rely on that I missed. https://github.com/llvm/llvm-project/pull/126324 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][NFC] clang-format __has_builtin implementation (PR #126571)
https://github.com/sarnex created https://github.com/llvm/llvm-project/pull/126571 None >From 67ee9b6380ef9acaad961edf36c1401579b64a37 Mon Sep 17 00:00:00 2001 From: "Sarnie, Nick" Date: Mon, 10 Feb 2025 10:36:26 -0800 Subject: [PATCH] [Clang][NFC] clang-format has_builtin implementation Signed-off-by: Sarnie, Nick --- clang/lib/Lex/PPMacroExpansion.cpp | 95 +++--- 1 file changed, 48 insertions(+), 47 deletions(-) diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp index 347c13da0ad215a..944966a791add58 100644 --- a/clang/lib/Lex/PPMacroExpansion.cpp +++ b/clang/lib/Lex/PPMacroExpansion.cpp @@ -1798,54 +1798,55 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) { return II && HasExtension(*this, II->getName()); }); } else if (II == Ident__has_builtin) { -EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, false, - [this](Token &Tok, bool &HasLexedNextToken) -> int { -IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this, - diag::err_feature_check_malformed); -if (!II) - return false; -else if (II->getBuiltinID() != 0) { - switch (II->getBuiltinID()) { - case Builtin::BI__builtin_cpu_is: -return getTargetInfo().supportsCpuIs(); - case Builtin::BI__builtin_cpu_init: -return getTargetInfo().supportsCpuInit(); - case Builtin::BI__builtin_cpu_supports: -return getTargetInfo().supportsCpuSupports(); - case Builtin::BI__builtin_operator_new: - case Builtin::BI__builtin_operator_delete: -// denotes date of behavior change to support calling arbitrary -// usual allocation and deallocation functions. Required by libc++ -return 201802; - default: -return Builtin::evaluateRequiredTargetFeatures( -getBuiltinInfo().getRequiredFeatures(II->getBuiltinID()), -getTargetInfo().getTargetOpts().FeatureMap); +EvaluateFeatureLikeBuiltinMacro( +OS, Tok, II, *this, false, +[this](Token &Tok, bool &HasLexedNextToken) -> int { + IdentifierInfo *II = ExpectFeatureIdentifierInfo( + Tok, *this, diag::err_feature_check_malformed); + if (!II) +return false; + else if (II->getBuiltinID() != 0) { +switch (II->getBuiltinID()) { +case Builtin::BI__builtin_cpu_is: + return getTargetInfo().supportsCpuIs(); +case Builtin::BI__builtin_cpu_init: + return getTargetInfo().supportsCpuInit(); +case Builtin::BI__builtin_cpu_supports: + return getTargetInfo().supportsCpuSupports(); +case Builtin::BI__builtin_operator_new: +case Builtin::BI__builtin_operator_delete: + // denotes date of behavior change to support calling arbitrary + // usual allocation and deallocation functions. Required by libc++ + return 201802; +default: + return Builtin::evaluateRequiredTargetFeatures( + getBuiltinInfo().getRequiredFeatures(II->getBuiltinID()), + getTargetInfo().getTargetOpts().FeatureMap); +} +return true; + } else if (IsBuiltinTrait(Tok)) { +return true; + } else if (II->getTokenID() != tok::identifier && + II->getName().starts_with("__builtin_")) { +return true; + } else { +return llvm::StringSwitch(II->getName()) +// Report builtin templates as being builtins. +.Case("__make_integer_seq", getLangOpts().CPlusPlus) +.Case("__type_pack_element", getLangOpts().CPlusPlus) +.Case("__builtin_common_type", getLangOpts().CPlusPlus) +// Likewise for some builtin preprocessor macros. +// FIXME: This is inconsistent; we usually suggest detecting +// builtin macros via #ifdef. Don't add more cases here. +.Case("__is_target_arch", true) +.Case("__is_target_vendor", true) +.Case("__is_target_os", true) +.Case("__is_target_environment", true) +.Case("__is_target_variant_os", true) +.Case("__is_target_variant_environment", true) +.Default(false); } - return true; -} else if (IsBuiltinTrait(Tok)) { - return true; -} else if (II->getTokenID() != tok::identifier && - II->getName().starts_with("__builtin_")) { - return true; -} else { - return llvm::StringSwitch(II->getName()) - // Report builtin templates as being builtins. - .Case("__make_integer_seq", getLangOpts().CPlusPlus) - .Case("__type_pack_elem
[clang] [Clang][NFC] clang-format __has_builtin implementation (PR #126571)
https://github.com/sarnex edited https://github.com/llvm/llvm-project/pull/126571 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-linker-wrapper][lit] Temporarily disable OpenMP SPIR-V ELF test (PR #126104)
sarnex wrote: Merging this now to unbreak CI, will address any feedback in followup https://github.com/llvm/llvm-project/pull/126104 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-linker-wrapper][lit] Temporarily disable OpenMP SPIR-V ELF test (PR #126104)
https://github.com/sarnex closed https://github.com/llvm/llvm-project/pull/126104 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-linker-wrapper][lit] Fix SPIR-V OpenMP ELF test (PR #126098)
https://github.com/sarnex created https://github.com/llvm/llvm-project/pull/126098 Fix for https://github.com/llvm/llvm-project/pull/125737 >From daf3c08fc5c13d0bdd2851c110cb89bd9f853a6a Mon Sep 17 00:00:00 2001 From: "Sarnie, Nick" Date: Thu, 6 Feb 2025 09:33:29 -0800 Subject: [PATCH] [clang-linker-wrapper][lit] Fix SPIR-V OpenMP ELF test Signed-off-by: Sarnie, Nick --- clang/test/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/clang/test/CMakeLists.txt b/clang/test/CMakeLists.txt index 9ce8365910d5594..e9eb54a67204cda 100644 --- a/clang/test/CMakeLists.txt +++ b/clang/test/CMakeLists.txt @@ -21,6 +21,7 @@ llvm_canonicalize_cmake_booleans( LLVM_WITH_Z3 PPC_LINUX_DEFAULT_IEEELONGDOUBLE LLVM_TOOL_LLVM_DRIVER_BUILD + LLVM_INCLUDE_SPIRV_TOOLS_TESTS ) configure_lit_site_cfg( ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-linker-wrapper][lit] Fix SPIR-V OpenMP ELF test (PR #126098)
https://github.com/sarnex ready_for_review https://github.com/llvm/llvm-project/pull/126098 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-linker-wrapper][lit] Fix SPIR-V OpenMP ELF test (PR #126098)
sarnex wrote: thanks for the help, it would have taken me forever to find the problem without the advice https://github.com/llvm/llvm-project/pull/126098 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [clang-linker-wrapper] Add ELF packaging for spirv64-intel OpenMP images (PR #125737)
sarnex wrote: https://github.com/llvm/llvm-project/pull/126098 https://github.com/llvm/llvm-project/pull/125737 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [OpenMP][OpenMPIRBuilder] Add initial changes for SPIR-V target frontend support (PR #125920)
https://github.com/sarnex ready_for_review https://github.com/llvm/llvm-project/pull/125920 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Forward arguments to the device compiler better (PR #125957)
@@ -9270,11 +9260,6 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA, }); } - // If we disable the GPU C library support it needs to be forwarded to the - // link job. - if (!Args.hasFlag(options::OPT_gpulibc, options::OPT_nogpulibc, true)) sarnex wrote: i dont see these options in either of the new option sets, is this code not needed anymore or is it handled somewhere else already? https://github.com/llvm/llvm-project/pull/125957 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Forward arguments to the device compiler better (PR #125957)
https://github.com/sarnex approved this pull request. https://github.com/llvm/llvm-project/pull/125957 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [clang-linker-wrapper] Add ELF packaging for spirv64-intel OpenMP images (PR #125737)
sarnex wrote: what should i do in that case? can i just ignore the failure or is there a way to clear the build cache? https://github.com/llvm/llvm-project/pull/125737 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [clang-linker-wrapper] Add ELF packaging for spirv64-intel OpenMP images (PR #125737)
sarnex wrote: yeah my test should only run if `LLVM_INCLUDE_SPIRV_TOOLS_TESTS` is set, which does not appear to be the case, so i'm surprised it's running. it indeed did not run in the clean build. ``` UNSUPPORTED: Clang :: Tooling/clang-linker-wrapper-spirv-elf.cpp (20383 of 84127) ``` the lit feature i added should only be enabled if `LLVM_INCLUDE_SPIRV_TOOLS_TESTS` is set, so to be honest i have no idea what the problem is https://github.com/llvm/llvm-project/pull/125737 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [clang-linker-wrapper] Add ELF packaging for spirv64-intel OpenMP images (PR #125737)
sarnex wrote: ok cool if you can repro it it's almost surely a real issue. can you share the repro steps, my local repro has it as unsupported as i expect https://github.com/llvm/llvm-project/pull/125737 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [clang-linker-wrapper] Add ELF packaging for spirv64-intel OpenMP images (PR #125737)
sarnex wrote: yes i will revert if i dont have a fix pr in 30 minutes https://github.com/llvm/llvm-project/pull/125737 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [clang-linker-wrapper] Add ELF packaging for spirv64-intel OpenMP images (PR #125737)
sarnex wrote: thanks a lot, i think i see the problem, but would help if i could repro :P https://github.com/llvm/llvm-project/pull/125737 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [clang-linker-wrapper] Add ELF packaging for spirv64-intel OpenMP images (PR #125737)
https://github.com/sarnex ready_for_review https://github.com/llvm/llvm-project/pull/125737 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [clang-linker-wrapper] Add ELF packaging for spirv64-intel OpenMP images (PR #125737)
https://github.com/sarnex edited https://github.com/llvm/llvm-project/pull/125737 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [OpenMP][OpenMPIRBuilder] Add initial changes for SPIR-V target frontend support (PR #125920)
https://github.com/sarnex updated https://github.com/llvm/llvm-project/pull/125920 >From 1ee97d674c707d4b07d1e39f943adc94bb16d205 Mon Sep 17 00:00:00 2001 From: "Sarnie, Nick" Date: Tue, 4 Feb 2025 13:09:48 -0800 Subject: [PATCH 1/2] [OpenMP][OpenMPIRBuilder] Add initial changes for SPIR-V target frontend support Signed-off-by: Sarnie, Nick --- clang/include/clang/Basic/TargetInfo.h | 2 +- clang/lib/CodeGen/CodeGenModule.cpp | 6 -- .../test/OpenMP/spirv_target_codegen_basic.cpp | 17 + .../llvm/Frontend/OpenMP/OMPGridValues.h| 11 +++ llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp | 4 5 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 clang/test/OpenMP/spirv_target_codegen_basic.cpp diff --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h index b9e46a5e7d1ca5e..070cc792ca7db62 100644 --- a/clang/include/clang/Basic/TargetInfo.h +++ b/clang/include/clang/Basic/TargetInfo.h @@ -1662,7 +1662,7 @@ class TargetInfo : public TransferrableTargetInfo, // access target-specific GPU grid values that must be consistent between // host RTL (plugin), deviceRTL and clang. virtual const llvm::omp::GV &getGridValue() const { -llvm_unreachable("getGridValue not implemented on this target"); +return llvm::omp::SPIRVGridValues; } /// Retrieve the name of the platform as it is used in the diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 02615bb13dfb8a7..846b00f08973253 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -486,8 +486,10 @@ void CodeGenModule::createOpenMPRuntime() { case llvm::Triple::nvptx: case llvm::Triple::nvptx64: case llvm::Triple::amdgcn: -assert(getLangOpts().OpenMPIsTargetDevice && - "OpenMP AMDGPU/NVPTX is only prepared to deal with device code."); + case llvm::Triple::spirv64: +assert( +getLangOpts().OpenMPIsTargetDevice && +"OpenMP AMDGPU/NVPTX/SPIRV is only prepared to deal with device code."); OpenMPRuntime.reset(new CGOpenMPRuntimeGPU(*this)); break; default: diff --git a/clang/test/OpenMP/spirv_target_codegen_basic.cpp b/clang/test/OpenMP/spirv_target_codegen_basic.cpp new file mode 100644 index 000..20b1d52e7a4afc1 --- /dev/null +++ b/clang/test/OpenMP/spirv_target_codegen_basic.cpp @@ -0,0 +1,17 @@ +// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple x86_64-unknown-linux -fopenmp-targets=spirv64-intel -emit-llvm-bc %s -o %t-host.bc +// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple spirv64-intel -fopenmp-targets=spirv64-intel -emit-llvm %s -fopenmp-is-target-device -fopenmp-host-ir-file-path %t-host.bc -o - | FileCheck %s + +// expected-no-diagnostics + +// CHECK: @__omp_offloading_{{.*}}_dynamic_environment = weak_odr protected addrspace(1) global %struct.DynamicEnvironmentTy zeroinitializer +// CHECK: @__omp_offloading_{{.*}}_kernel_environment = weak_odr protected addrspace(1) constant %struct.KernelEnvironmentTy + +// CHECK: define weak_odr protected spir_kernel void @__omp_offloading_{{.*}} + +int main() { + int ret = 0; + #pragma omp target + for(int i = 0; i < 5; i++) +ret++; + return ret; +} diff --git a/llvm/include/llvm/Frontend/OpenMP/OMPGridValues.h b/llvm/include/llvm/Frontend/OpenMP/OMPGridValues.h index bfac2d734b81d8e..788a3c8a56f3806 100644 --- a/llvm/include/llvm/Frontend/OpenMP/OMPGridValues.h +++ b/llvm/include/llvm/Frontend/OpenMP/OMPGridValues.h @@ -120,6 +120,17 @@ static constexpr GV NVPTXGridValues = { 128, // GV_Default_WG_Size }; +/// For generic SPIR-V GPUs +static constexpr GV SPIRVGridValues = { +256, // GV_Slot_Size +64,// GV_Warp_Size +(1 << 16), // GV_Max_Teams +440, // GV_Default_Num_Teams +896, // GV_SimpleBufferSize +1024, // GV_Max_WG_Size, +256, // GV_Default_WG_Size +}; + } // namespace omp } // namespace llvm diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp index 695b15ac31f380e..26baf836e8714b6 100644 --- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp +++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp @@ -159,6 +159,8 @@ static const omp::GV &getGridValue(const Triple &T, Function *Kernel) { } if (T.isNVPTX()) return omp::NVPTXGridValues; + if (T.isSPIRV()) +return omp::SPIRVGridValues; llvm_unreachable("No grid value available for this architecture!"); } @@ -6470,6 +6472,8 @@ void OpenMPIRBuilder::setOutlinedTargetRegionFunctionAttributes( OutlinedFn->setCallingConv(CallingConv::AMDGPU_KERNEL); else if (T.isNVPTX()) OutlinedFn->setCallingConv(CallingConv::PTX_Kernel); +else if (T.isSPIRV()) + OutlinedFn->setCallingConv(CallingConv::SPIR_KERNEL); } } >From 626b15ba7d3dbfbe58df7b13d6aded852ca58d77 Mon Sep 17 00:00:00 2001 From: "Sarnie, Nick"
[clang] [llvm] [OpenMP][OpenMPIRBuilder] Add initial changes for SPIR-V target frontend support (PR #125920)
https://github.com/sarnex created https://github.com/llvm/llvm-project/pull/125920 I expect (many) other changes will be required, but let's get started with something simple. >From 1ee97d674c707d4b07d1e39f943adc94bb16d205 Mon Sep 17 00:00:00 2001 From: "Sarnie, Nick" Date: Tue, 4 Feb 2025 13:09:48 -0800 Subject: [PATCH] [OpenMP][OpenMPIRBuilder] Add initial changes for SPIR-V target frontend support Signed-off-by: Sarnie, Nick --- clang/include/clang/Basic/TargetInfo.h | 2 +- clang/lib/CodeGen/CodeGenModule.cpp | 6 -- .../test/OpenMP/spirv_target_codegen_basic.cpp | 17 + .../llvm/Frontend/OpenMP/OMPGridValues.h| 11 +++ llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp | 4 5 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 clang/test/OpenMP/spirv_target_codegen_basic.cpp diff --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h index b9e46a5e7d1ca5e..070cc792ca7db62 100644 --- a/clang/include/clang/Basic/TargetInfo.h +++ b/clang/include/clang/Basic/TargetInfo.h @@ -1662,7 +1662,7 @@ class TargetInfo : public TransferrableTargetInfo, // access target-specific GPU grid values that must be consistent between // host RTL (plugin), deviceRTL and clang. virtual const llvm::omp::GV &getGridValue() const { -llvm_unreachable("getGridValue not implemented on this target"); +return llvm::omp::SPIRVGridValues; } /// Retrieve the name of the platform as it is used in the diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 02615bb13dfb8a7..846b00f08973253 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -486,8 +486,10 @@ void CodeGenModule::createOpenMPRuntime() { case llvm::Triple::nvptx: case llvm::Triple::nvptx64: case llvm::Triple::amdgcn: -assert(getLangOpts().OpenMPIsTargetDevice && - "OpenMP AMDGPU/NVPTX is only prepared to deal with device code."); + case llvm::Triple::spirv64: +assert( +getLangOpts().OpenMPIsTargetDevice && +"OpenMP AMDGPU/NVPTX/SPIRV is only prepared to deal with device code."); OpenMPRuntime.reset(new CGOpenMPRuntimeGPU(*this)); break; default: diff --git a/clang/test/OpenMP/spirv_target_codegen_basic.cpp b/clang/test/OpenMP/spirv_target_codegen_basic.cpp new file mode 100644 index 000..20b1d52e7a4afc1 --- /dev/null +++ b/clang/test/OpenMP/spirv_target_codegen_basic.cpp @@ -0,0 +1,17 @@ +// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple x86_64-unknown-linux -fopenmp-targets=spirv64-intel -emit-llvm-bc %s -o %t-host.bc +// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple spirv64-intel -fopenmp-targets=spirv64-intel -emit-llvm %s -fopenmp-is-target-device -fopenmp-host-ir-file-path %t-host.bc -o - | FileCheck %s + +// expected-no-diagnostics + +// CHECK: @__omp_offloading_{{.*}}_dynamic_environment = weak_odr protected addrspace(1) global %struct.DynamicEnvironmentTy zeroinitializer +// CHECK: @__omp_offloading_{{.*}}_kernel_environment = weak_odr protected addrspace(1) constant %struct.KernelEnvironmentTy + +// CHECK: define weak_odr protected spir_kernel void @__omp_offloading_{{.*}} + +int main() { + int ret = 0; + #pragma omp target + for(int i = 0; i < 5; i++) +ret++; + return ret; +} diff --git a/llvm/include/llvm/Frontend/OpenMP/OMPGridValues.h b/llvm/include/llvm/Frontend/OpenMP/OMPGridValues.h index bfac2d734b81d8e..788a3c8a56f3806 100644 --- a/llvm/include/llvm/Frontend/OpenMP/OMPGridValues.h +++ b/llvm/include/llvm/Frontend/OpenMP/OMPGridValues.h @@ -120,6 +120,17 @@ static constexpr GV NVPTXGridValues = { 128, // GV_Default_WG_Size }; +/// For generic SPIR-V GPUs +static constexpr GV SPIRVGridValues = { +256, // GV_Slot_Size +64,// GV_Warp_Size +(1 << 16), // GV_Max_Teams +440, // GV_Default_Num_Teams +896, // GV_SimpleBufferSize +1024, // GV_Max_WG_Size, +256, // GV_Default_WG_Size +}; + } // namespace omp } // namespace llvm diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp index 695b15ac31f380e..26baf836e8714b6 100644 --- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp +++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp @@ -159,6 +159,8 @@ static const omp::GV &getGridValue(const Triple &T, Function *Kernel) { } if (T.isNVPTX()) return omp::NVPTXGridValues; + if (T.isSPIRV()) +return omp::SPIRVGridValues; llvm_unreachable("No grid value available for this architecture!"); } @@ -6470,6 +6472,8 @@ void OpenMPIRBuilder::setOutlinedTargetRegionFunctionAttributes( OutlinedFn->setCallingConv(CallingConv::AMDGPU_KERNEL); else if (T.isNVPTX()) OutlinedFn->setCallingConv(CallingConv::PTX_Kernel); +else if (T.isSPIRV()) + OutlinedFn->setCallingConv(CallingConv::SPIR_KERNEL); } } ___
[clang] [llvm] [clang-linker-wrapper][lit] Fix OpenMP SPIR-V ELF test again (PR #126142)
https://github.com/sarnex created https://github.com/llvm/llvm-project/pull/126142 I think this one will actually fix it. >From 9d0419edc1cc30d3e97c5e3e31f781cf5ad14e1a Mon Sep 17 00:00:00 2001 From: "Sarnie, Nick" Date: Thu, 6 Feb 2025 14:24:08 -0800 Subject: [PATCH] [clang-linker-wrapper][lit] Fix OpenMP SPIR-V ELF test again Signed-off-by: Sarnie, Nick --- clang/test/Tooling/clang-linker-wrapper-spirv-elf.cpp | 2 -- llvm/utils/gn/secondary/clang/test/BUILD.gn | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/clang/test/Tooling/clang-linker-wrapper-spirv-elf.cpp b/clang/test/Tooling/clang-linker-wrapper-spirv-elf.cpp index 50457f47868a028..4f8658064e857d0 100644 --- a/clang/test/Tooling/clang-linker-wrapper-spirv-elf.cpp +++ b/clang/test/Tooling/clang-linker-wrapper-spirv-elf.cpp @@ -1,6 +1,4 @@ // Verify the ELF packaging of OpenMP SPIR-V device images. -// FIXME: Re-enable when spirv-tools feature detection fixed -// UNSUPPORTED: system-linux // REQUIRES: system-linux // REQUIRES: spirv-tools // RUN: mkdir -p %t_tmp diff --git a/llvm/utils/gn/secondary/clang/test/BUILD.gn b/llvm/utils/gn/secondary/clang/test/BUILD.gn index 1c88d447658ce0c..2d6ad23ae58ce96 100644 --- a/llvm/utils/gn/secondary/clang/test/BUILD.gn +++ b/llvm/utils/gn/secondary/clang/test/BUILD.gn @@ -74,6 +74,7 @@ write_lit_config("lit_site_cfg") { "Python3_EXECUTABLE=$python_path", "USE_Z3_SOLVER=", "PPC_LINUX_DEFAULT_IEEELONGDOUBLE=0", +"LLVM_INCLUDE_SPIRV_TOOLS_TESTS=0", ] if (clang_enable_static_analyzer) { ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [clang-linker-wrapper][lit] Fix OpenMP SPIR-V ELF test again (PR #126142)
https://github.com/sarnex updated https://github.com/llvm/llvm-project/pull/126142 >From 504d1993bd1adce8ad627f6aa0f9d73a9967b473 Mon Sep 17 00:00:00 2001 From: "Sarnie, Nick" Date: Thu, 6 Feb 2025 14:24:08 -0800 Subject: [PATCH] [clang-linker-wrapper][lit] Fix OpenMP SPIR-V ELF test again Signed-off-by: Sarnie, Nick --- clang/test/Tooling/clang-linker-wrapper-spirv-elf.cpp | 2 -- clang/test/lit.site.cfg.py.in | 2 +- llvm/utils/gn/secondary/clang/test/BUILD.gn | 1 + 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/clang/test/Tooling/clang-linker-wrapper-spirv-elf.cpp b/clang/test/Tooling/clang-linker-wrapper-spirv-elf.cpp index 50457f47868a028..4f8658064e857d0 100644 --- a/clang/test/Tooling/clang-linker-wrapper-spirv-elf.cpp +++ b/clang/test/Tooling/clang-linker-wrapper-spirv-elf.cpp @@ -1,6 +1,4 @@ // Verify the ELF packaging of OpenMP SPIR-V device images. -// FIXME: Re-enable when spirv-tools feature detection fixed -// UNSUPPORTED: system-linux // REQUIRES: system-linux // REQUIRES: spirv-tools // RUN: mkdir -p %t_tmp diff --git a/clang/test/lit.site.cfg.py.in b/clang/test/lit.site.cfg.py.in index ce10e9128a1dfe1..6890da5327cb975 100644 --- a/clang/test/lit.site.cfg.py.in +++ b/clang/test/lit.site.cfg.py.in @@ -43,7 +43,7 @@ config.llvm_external_lit = path(r"@LLVM_EXTERNAL_LIT@") config.standalone_build = @CLANG_BUILT_STANDALONE@ config.ppc_linux_default_ieeelongdouble = @PPC_LINUX_DEFAULT_IEEELONGDOUBLE@ config.have_llvm_driver = @LLVM_TOOL_LLVM_DRIVER_BUILD@ -config.spirv_tools_tests = "@LLVM_INCLUDE_SPIRV_TOOLS_TESTS@" +config.spirv_tools_tests = @LLVM_INCLUDE_SPIRV_TOOLS_TESTS@ config.substitutions.append(("%llvm-version-major", "@LLVM_VERSION_MAJOR@")) import lit.llvm diff --git a/llvm/utils/gn/secondary/clang/test/BUILD.gn b/llvm/utils/gn/secondary/clang/test/BUILD.gn index 1c88d447658ce0c..2d6ad23ae58ce96 100644 --- a/llvm/utils/gn/secondary/clang/test/BUILD.gn +++ b/llvm/utils/gn/secondary/clang/test/BUILD.gn @@ -74,6 +74,7 @@ write_lit_config("lit_site_cfg") { "Python3_EXECUTABLE=$python_path", "USE_Z3_SOLVER=", "PPC_LINUX_DEFAULT_IEEELONGDOUBLE=0", +"LLVM_INCLUDE_SPIRV_TOOLS_TESTS=0", ] if (clang_enable_static_analyzer) { ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [clang-linker-wrapper] Add ELF packaging for spirv64-intel OpenMP images (PR #125737)
https://github.com/sarnex closed https://github.com/llvm/llvm-project/pull/125737 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [clang-linker-wrapper] Add ELF packaging for spirv64-intel OpenMP images (PR #125737)
sarnex wrote: looking https://github.com/llvm/llvm-project/pull/125737 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Forward `-Xarch_ -Wl,foo` for GPU toolchains (PR #126248)
@@ -9203,7 +9203,9 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA, ArgStringList CompilerArgs; ArgStringList LinkerArgs; for (Arg *A : C.getArgsForToolChain(TC, /*BoundArch=*/"", Kind)) { -if (ShouldForward(CompilerOptions, A)) +if (A->getOption().matches(OPT_Zlinker_input)) sarnex wrote: i assume we can't use `render()` with `OPT_Zlinker_input`? https://github.com/llvm/llvm-project/pull/126248 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Forward `-Xarch_ -Wl,foo` for GPU toolchains (PR #126248)
https://github.com/sarnex approved this pull request. https://github.com/llvm/llvm-project/pull/126248 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Forward `-Xarch_ -Wl,foo` for GPU toolchains (PR #126248)
@@ -9203,7 +9203,9 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA, ArgStringList CompilerArgs; ArgStringList LinkerArgs; for (Arg *A : C.getArgsForToolChain(TC, /*BoundArch=*/"", Kind)) { -if (ShouldForward(CompilerOptions, A)) +if (A->getOption().matches(OPT_Zlinker_input)) sarnex wrote: got it https://github.com/llvm/llvm-project/pull/126248 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [clang-linker-wrapper][lit] Fix OpenMP SPIR-V ELF test again (PR #126142)
https://github.com/sarnex edited https://github.com/llvm/llvm-project/pull/126142 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [clang-linker-wrapper][lit] Fix OpenMP SPIR-V ELF test again (PR #126142)
https://github.com/sarnex edited https://github.com/llvm/llvm-project/pull/126142 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [clang-linker-wrapper][lit] Fix OpenMP SPIR-V ELF test again (PR #126142)
https://github.com/sarnex edited https://github.com/llvm/llvm-project/pull/126142 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [clang-linker-wrapper][lit] Fix OpenMP SPIR-V ELF test again (PR #126142)
https://github.com/sarnex edited https://github.com/llvm/llvm-project/pull/126142 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [clang-linker-wrapper][lit] Fix OpenMP SPIR-V ELF test again (PR #126142)
https://github.com/sarnex edited https://github.com/llvm/llvm-project/pull/126142 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [clang-linker-wrapper][lit] Fix OpenMP SPIR-V ELF test again (PR #126142)
https://github.com/sarnex edited https://github.com/llvm/llvm-project/pull/126142 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [clang-linker-wrapper][lit] Fix OpenMP SPIR-V ELF test again (PR #126142)
https://github.com/sarnex edited https://github.com/llvm/llvm-project/pull/126142 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][NFC] clang-format __has_builtin implementation (PR #126571)
https://github.com/sarnex ready_for_review https://github.com/llvm/llvm-project/pull/126571 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang][NFC] clang-format __has_builtin implementation (PR #126571)
https://github.com/sarnex closed https://github.com/llvm/llvm-project/pull/126571 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add __has_target_builtin macro (PR #126324)
https://github.com/sarnex edited https://github.com/llvm/llvm-project/pull/126324 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add __has_target_builtin macro (PR #126324)
@@ -96,6 +101,37 @@ the header file to conditionally make a function constexpr whenever the constant evaluation of the corresponding builtin (for example, ``std::fmax`` calls ``__builtin_fmax``) is supported in Clang. +``__has_target_builtin`` + + +This function-like macro takes a single identifier argument that is the name of +a builtin function, a builtin pseudo-function (taking one or more type +arguments), or a builtin template. +It evaluates to 1 if the builtin is supported on the current target or 0 if not. +The behavior is different than ``__has_builtin`` when there is an auxiliary target, +such when offloading to a target device. +It can be used like this: + +.. code-block:: c++ + + #ifndef __has_target_builtin // Optional of course. +#define __has_target_builtin(x) 0 // Compatibility with non-clang compilers. + #endif + + ... + #if __has_target_builtin(__builtin_trap) +__builtin_trap(); + #else +abort(); + #endif + ... + +.. note:: + ``__has_target_builtin`` should not be used to detect support for a builtin macro; + use ``#ifdef`` instead. + + ``__has_target_built`` is only defined for offloading targets. sarnex wrote: wow thats embarrassing, thanks https://github.com/llvm/llvm-project/pull/126324 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add __has_target_builtin macro (PR #126324)
@@ -96,6 +101,37 @@ the header file to conditionally make a function constexpr whenever the constant evaluation of the corresponding builtin (for example, ``std::fmax`` calls ``__builtin_fmax``) is supported in Clang. +``__has_target_builtin`` + + +This function-like macro takes a single identifier argument that is the name of +a builtin function, a builtin pseudo-function (taking one or more type +arguments), or a builtin template. +It evaluates to 1 if the builtin is supported on the current target or 0 if not. +The behavior is different than ``__has_builtin`` when there is an auxiliary target, +such when offloading to a target device. sarnex wrote: cant english today, thanks https://github.com/llvm/llvm-project/pull/126324 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add __has_target_builtin macro (PR #126324)
@@ -1797,55 +1798,62 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) { diag::err_feature_check_malformed); return II && HasExtension(*this, II->getName()); }); - } else if (II == Ident__has_builtin) { sarnex wrote: @AaronBallman Yeah here the clang-format CI [failed](https://github.com/llvm/llvm-project/actions/runs/13249258371/job/36983046889?pr=126324) telling me I need to update unrelated code. Should I just ignore it? https://github.com/llvm/llvm-project/pull/126324 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add __has_target_builtin macro (PR #126324)
https://github.com/sarnex updated https://github.com/llvm/llvm-project/pull/126324 >From 46cce74568bca5a3e80e50def4348bc734448362 Mon Sep 17 00:00:00 2001 From: "Sarnie, Nick" Date: Fri, 7 Feb 2025 14:57:12 -0800 Subject: [PATCH 1/3] [Clang] Add __has_target_builtin macro Signed-off-by: Sarnie, Nick --- clang/docs/LanguageExtensions.rst | 33 +++ clang/include/clang/Lex/Preprocessor.h| 1 + clang/lib/Lex/PPMacroExpansion.cpp| 17 +++--- .../test/Preprocessor/has_target_builtin.cpp | 18 ++ 4 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 clang/test/Preprocessor/has_target_builtin.cpp diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst index 973cf8f9d091c30..057ad564f970bb4 100644 --- a/clang/docs/LanguageExtensions.rst +++ b/clang/docs/LanguageExtensions.rst @@ -67,6 +67,10 @@ It can be used like this: ``__has_builtin`` should not be used to detect support for a builtin macro; use ``#ifdef`` instead. + When using device offloading, a builtin is considered available if it is + available on either the host or the device targets. + Use ``__has_target_builtin`` to consider only the current target. + ``__has_constexpr_builtin`` --- @@ -96,6 +100,35 @@ the header file to conditionally make a function constexpr whenever the constant evaluation of the corresponding builtin (for example, ``std::fmax`` calls ``__builtin_fmax``) is supported in Clang. +``__has_target_builtin`` + + +This function-like macro takes a single identifier argument that is the name of +a builtin function, a builtin pseudo-function (taking one or more type +arguments), or a builtin template. +It evaluates to 1 if the builtin is supported on the current target or 0 if not. +The behavior is different than ``__has_builtin`` when there is an auxiliary target, +such when offloading to a target device. +It can be used like this: + +.. code-block:: c++ + + #ifndef __has_target_builtin // Optional of course. +#define __has_target_builtin(x) 0 // Compatibility with non-clang compilers. + #endif + + ... + #if __has_target_builtin(__builtin_trap) +__builtin_trap(); + #else +abort(); + #endif + ... + +.. note:: + ``__has_target_builtin`` should not be used to detect support for a builtin macro; + use ``#ifdef`` instead. + .. _langext-__has_feature-__has_extension: ``__has_feature`` and ``__has_extension`` diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h index 2bf4d1a16699430..240fe28aba93e33 100644 --- a/clang/include/clang/Lex/Preprocessor.h +++ b/clang/include/clang/Lex/Preprocessor.h @@ -174,6 +174,7 @@ class Preprocessor { IdentifierInfo *Ident__has_extension;// __has_extension IdentifierInfo *Ident__has_builtin; // __has_builtin IdentifierInfo *Ident__has_constexpr_builtin;// __has_constexpr_builtin + IdentifierInfo *Ident__has_target_builtin; // __has_target_builtin IdentifierInfo *Ident__has_attribute;// __has_attribute IdentifierInfo *Ident__has_embed;// __has_embed IdentifierInfo *Ident__has_include; // __has_include diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp index 944966a791add58..9ec75a08316a1aa 100644 --- a/clang/lib/Lex/PPMacroExpansion.cpp +++ b/clang/lib/Lex/PPMacroExpansion.cpp @@ -357,6 +357,7 @@ void Preprocessor::RegisterBuiltinMacros() { Ident__has_builtin = RegisterBuiltinMacro("__has_builtin"); Ident__has_constexpr_builtin = RegisterBuiltinMacro("__has_constexpr_builtin"); + Ident__has_target_builtin = RegisterBuiltinMacro("__has_target_builtin"); Ident__has_attribute = RegisterBuiltinMacro("__has_attribute"); if (!getLangOpts().CPlusPlus) Ident__has_c_attribute = RegisterBuiltinMacro("__has_c_attribute"); @@ -1797,16 +1798,18 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) { diag::err_feature_check_malformed); return II && HasExtension(*this, II->getName()); }); - } else if (II == Ident__has_builtin) { + } else if (II == Ident__has_builtin || II == Ident__has_target_builtin) { +bool IsHasTargetBuiltin = II == Ident__has_target_builtin; EvaluateFeatureLikeBuiltinMacro( OS, Tok, II, *this, false, -[this](Token &Tok, bool &HasLexedNextToken) -> int { +[this, IsHasTargetBuiltin](Token &Tok, bool &HasLexedNextToken) -> int { IdentifierInfo *II = ExpectFeatureIdentifierInfo( Tok, *this, diag::err_feature_check_malformed); if (!II) return false; - else if (II->getBuiltinID() != 0) { -switch (II->getBuiltinID()) { + auto BuiltinID = II->getBuiltinID(); + if (BuiltinID != 0) { +switch (BuiltinID) {
[clang] [Clang] Add __has_target_builtin macro (PR #126324)
@@ -96,6 +101,37 @@ the header file to conditionally make a function constexpr whenever the constant evaluation of the corresponding builtin (for example, ``std::fmax`` calls ``__builtin_fmax``) is supported in Clang. +``__has_target_builtin`` + + +This function-like macro takes a single identifier argument that is the name of +a builtin function, a builtin pseudo-function (taking one or more type +arguments), or a builtin template. +It evaluates to 1 if the builtin is supported on the current target or 0 if not. +The behavior is different than ``__has_builtin`` when there is an auxiliary target, +such when offloading to a target device. +It can be used like this: + +.. code-block:: c++ + + #ifndef __has_target_builtin // Optional of course. +#define __has_target_builtin(x) 0 // Compatibility with non-clang compilers. + #endif sarnex wrote: hopefully the latest commit has the use case youre looking for https://github.com/llvm/llvm-project/pull/126324 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add __has_target_builtin macro (PR #126324)
@@ -96,6 +101,37 @@ the header file to conditionally make a function constexpr whenever the constant evaluation of the corresponding builtin (for example, ``std::fmax`` calls ``__builtin_fmax``) is supported in Clang. +``__has_target_builtin`` + + +This function-like macro takes a single identifier argument that is the name of +a builtin function, a builtin pseudo-function (taking one or more type +arguments), or a builtin template. +It evaluates to 1 if the builtin is supported on the current target or 0 if not. +The behavior is different than ``__has_builtin`` when there is an auxiliary target, +such when offloading to a target device. sarnex wrote: thanks, i like the way you worded it so i'll use most of this verbtaim https://github.com/llvm/llvm-project/pull/126324 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add __has_target_builtin macro (PR #126324)
https://github.com/sarnex updated https://github.com/llvm/llvm-project/pull/126324 >From 46cce74568bca5a3e80e50def4348bc734448362 Mon Sep 17 00:00:00 2001 From: "Sarnie, Nick" Date: Fri, 7 Feb 2025 14:57:12 -0800 Subject: [PATCH 1/4] [Clang] Add __has_target_builtin macro Signed-off-by: Sarnie, Nick --- clang/docs/LanguageExtensions.rst | 33 +++ clang/include/clang/Lex/Preprocessor.h| 1 + clang/lib/Lex/PPMacroExpansion.cpp| 17 +++--- .../test/Preprocessor/has_target_builtin.cpp | 18 ++ 4 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 clang/test/Preprocessor/has_target_builtin.cpp diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst index 973cf8f9d091c30..057ad564f970bb4 100644 --- a/clang/docs/LanguageExtensions.rst +++ b/clang/docs/LanguageExtensions.rst @@ -67,6 +67,10 @@ It can be used like this: ``__has_builtin`` should not be used to detect support for a builtin macro; use ``#ifdef`` instead. + When using device offloading, a builtin is considered available if it is + available on either the host or the device targets. + Use ``__has_target_builtin`` to consider only the current target. + ``__has_constexpr_builtin`` --- @@ -96,6 +100,35 @@ the header file to conditionally make a function constexpr whenever the constant evaluation of the corresponding builtin (for example, ``std::fmax`` calls ``__builtin_fmax``) is supported in Clang. +``__has_target_builtin`` + + +This function-like macro takes a single identifier argument that is the name of +a builtin function, a builtin pseudo-function (taking one or more type +arguments), or a builtin template. +It evaluates to 1 if the builtin is supported on the current target or 0 if not. +The behavior is different than ``__has_builtin`` when there is an auxiliary target, +such when offloading to a target device. +It can be used like this: + +.. code-block:: c++ + + #ifndef __has_target_builtin // Optional of course. +#define __has_target_builtin(x) 0 // Compatibility with non-clang compilers. + #endif + + ... + #if __has_target_builtin(__builtin_trap) +__builtin_trap(); + #else +abort(); + #endif + ... + +.. note:: + ``__has_target_builtin`` should not be used to detect support for a builtin macro; + use ``#ifdef`` instead. + .. _langext-__has_feature-__has_extension: ``__has_feature`` and ``__has_extension`` diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h index 2bf4d1a16699430..240fe28aba93e33 100644 --- a/clang/include/clang/Lex/Preprocessor.h +++ b/clang/include/clang/Lex/Preprocessor.h @@ -174,6 +174,7 @@ class Preprocessor { IdentifierInfo *Ident__has_extension;// __has_extension IdentifierInfo *Ident__has_builtin; // __has_builtin IdentifierInfo *Ident__has_constexpr_builtin;// __has_constexpr_builtin + IdentifierInfo *Ident__has_target_builtin; // __has_target_builtin IdentifierInfo *Ident__has_attribute;// __has_attribute IdentifierInfo *Ident__has_embed;// __has_embed IdentifierInfo *Ident__has_include; // __has_include diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp index 944966a791add58..9ec75a08316a1aa 100644 --- a/clang/lib/Lex/PPMacroExpansion.cpp +++ b/clang/lib/Lex/PPMacroExpansion.cpp @@ -357,6 +357,7 @@ void Preprocessor::RegisterBuiltinMacros() { Ident__has_builtin = RegisterBuiltinMacro("__has_builtin"); Ident__has_constexpr_builtin = RegisterBuiltinMacro("__has_constexpr_builtin"); + Ident__has_target_builtin = RegisterBuiltinMacro("__has_target_builtin"); Ident__has_attribute = RegisterBuiltinMacro("__has_attribute"); if (!getLangOpts().CPlusPlus) Ident__has_c_attribute = RegisterBuiltinMacro("__has_c_attribute"); @@ -1797,16 +1798,18 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) { diag::err_feature_check_malformed); return II && HasExtension(*this, II->getName()); }); - } else if (II == Ident__has_builtin) { + } else if (II == Ident__has_builtin || II == Ident__has_target_builtin) { +bool IsHasTargetBuiltin = II == Ident__has_target_builtin; EvaluateFeatureLikeBuiltinMacro( OS, Tok, II, *this, false, -[this](Token &Tok, bool &HasLexedNextToken) -> int { +[this, IsHasTargetBuiltin](Token &Tok, bool &HasLexedNextToken) -> int { IdentifierInfo *II = ExpectFeatureIdentifierInfo( Tok, *this, diag::err_feature_check_malformed); if (!II) return false; - else if (II->getBuiltinID() != 0) { -switch (II->getBuiltinID()) { + auto BuiltinID = II->getBuiltinID(); + if (BuiltinID != 0) { +switch (BuiltinID) {
[clang] [Clang] Add __has_target_builtin macro (PR #126324)
https://github.com/sarnex updated https://github.com/llvm/llvm-project/pull/126324 >From 46cce74568bca5a3e80e50def4348bc734448362 Mon Sep 17 00:00:00 2001 From: "Sarnie, Nick" Date: Fri, 7 Feb 2025 14:57:12 -0800 Subject: [PATCH 1/7] [Clang] Add __has_target_builtin macro Signed-off-by: Sarnie, Nick --- clang/docs/LanguageExtensions.rst | 33 +++ clang/include/clang/Lex/Preprocessor.h| 1 + clang/lib/Lex/PPMacroExpansion.cpp| 17 +++--- .../test/Preprocessor/has_target_builtin.cpp | 18 ++ 4 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 clang/test/Preprocessor/has_target_builtin.cpp diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst index 973cf8f9d091c30..057ad564f970bb4 100644 --- a/clang/docs/LanguageExtensions.rst +++ b/clang/docs/LanguageExtensions.rst @@ -67,6 +67,10 @@ It can be used like this: ``__has_builtin`` should not be used to detect support for a builtin macro; use ``#ifdef`` instead. + When using device offloading, a builtin is considered available if it is + available on either the host or the device targets. + Use ``__has_target_builtin`` to consider only the current target. + ``__has_constexpr_builtin`` --- @@ -96,6 +100,35 @@ the header file to conditionally make a function constexpr whenever the constant evaluation of the corresponding builtin (for example, ``std::fmax`` calls ``__builtin_fmax``) is supported in Clang. +``__has_target_builtin`` + + +This function-like macro takes a single identifier argument that is the name of +a builtin function, a builtin pseudo-function (taking one or more type +arguments), or a builtin template. +It evaluates to 1 if the builtin is supported on the current target or 0 if not. +The behavior is different than ``__has_builtin`` when there is an auxiliary target, +such when offloading to a target device. +It can be used like this: + +.. code-block:: c++ + + #ifndef __has_target_builtin // Optional of course. +#define __has_target_builtin(x) 0 // Compatibility with non-clang compilers. + #endif + + ... + #if __has_target_builtin(__builtin_trap) +__builtin_trap(); + #else +abort(); + #endif + ... + +.. note:: + ``__has_target_builtin`` should not be used to detect support for a builtin macro; + use ``#ifdef`` instead. + .. _langext-__has_feature-__has_extension: ``__has_feature`` and ``__has_extension`` diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h index 2bf4d1a16699430..240fe28aba93e33 100644 --- a/clang/include/clang/Lex/Preprocessor.h +++ b/clang/include/clang/Lex/Preprocessor.h @@ -174,6 +174,7 @@ class Preprocessor { IdentifierInfo *Ident__has_extension;// __has_extension IdentifierInfo *Ident__has_builtin; // __has_builtin IdentifierInfo *Ident__has_constexpr_builtin;// __has_constexpr_builtin + IdentifierInfo *Ident__has_target_builtin; // __has_target_builtin IdentifierInfo *Ident__has_attribute;// __has_attribute IdentifierInfo *Ident__has_embed;// __has_embed IdentifierInfo *Ident__has_include; // __has_include diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp index 944966a791add58..9ec75a08316a1aa 100644 --- a/clang/lib/Lex/PPMacroExpansion.cpp +++ b/clang/lib/Lex/PPMacroExpansion.cpp @@ -357,6 +357,7 @@ void Preprocessor::RegisterBuiltinMacros() { Ident__has_builtin = RegisterBuiltinMacro("__has_builtin"); Ident__has_constexpr_builtin = RegisterBuiltinMacro("__has_constexpr_builtin"); + Ident__has_target_builtin = RegisterBuiltinMacro("__has_target_builtin"); Ident__has_attribute = RegisterBuiltinMacro("__has_attribute"); if (!getLangOpts().CPlusPlus) Ident__has_c_attribute = RegisterBuiltinMacro("__has_c_attribute"); @@ -1797,16 +1798,18 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) { diag::err_feature_check_malformed); return II && HasExtension(*this, II->getName()); }); - } else if (II == Ident__has_builtin) { + } else if (II == Ident__has_builtin || II == Ident__has_target_builtin) { +bool IsHasTargetBuiltin = II == Ident__has_target_builtin; EvaluateFeatureLikeBuiltinMacro( OS, Tok, II, *this, false, -[this](Token &Tok, bool &HasLexedNextToken) -> int { +[this, IsHasTargetBuiltin](Token &Tok, bool &HasLexedNextToken) -> int { IdentifierInfo *II = ExpectFeatureIdentifierInfo( Tok, *this, diag::err_feature_check_malformed); if (!II) return false; - else if (II->getBuiltinID() != 0) { -switch (II->getBuiltinID()) { + auto BuiltinID = II->getBuiltinID(); + if (BuiltinID != 0) { +switch (BuiltinID) {
[clang] [clang-linker-wrapper][lit] Fix SPIR-V ELF test when spirv-tools feature is available (PR #126756)
https://github.com/sarnex created https://github.com/llvm/llvm-project/pull/126756 My last change made the test not run when the `spirv-tools` feature is not available, which is always the case in CI for clang tests, but it fails if `spirv-tools` is available for the following reasons: 1) We didn't build `spirv-link` as part of the internal `SPIRV-Tools` build, which is required by the `clang` call in `clang-linker-wrapper`, I already fixed that [here](https://github.com/llvm/llvm-project/pull/126319). 2) We didn't depend on the `SPIRV-Tools` CMake targets in clang tests, so depending on what CMake targets were build before running `check-clang`, `SPIR-V Tools` might not have been built. 3) We didn't check for `llvm-spirv` being available, which is not part of `SPIRV-Tools` but is currently required for SPIR-V compilation. Manually confirmed this works. >From a758efe88d1e58a5b53dd7a4b7da6e174f645356 Mon Sep 17 00:00:00 2001 From: "Sarnie, Nick" Date: Tue, 11 Feb 2025 07:57:11 -0800 Subject: [PATCH] [clang-linker-wrapper][lit] Fix SPIR-V ELF test when spirv-tools feature is available Signed-off-by: Sarnie, Nick --- clang/test/CMakeLists.txt | 9 + clang/test/Tooling/clang-linker-wrapper-spirv-elf.cpp | 1 + clang/test/Tooling/lit.local.cfg | 6 ++ 3 files changed, 16 insertions(+) diff --git a/clang/test/CMakeLists.txt b/clang/test/CMakeLists.txt index e9eb54a67204cda..b796a51ef600e72 100644 --- a/clang/test/CMakeLists.txt +++ b/clang/test/CMakeLists.txt @@ -103,6 +103,15 @@ if(CLANG_BUILD_EXAMPLES AND CLANG_PLUGIN_SUPPORT) ) endif () +if(LLVM_INCLUDE_SPIRV_TOOLS_TESTS) + list(APPEND CLANG_TEST_DEPS +spirv-dis +spirv-val +spirv-as +spirv-link +) +endif() + set(CLANG_TEST_PARAMS USE_Z3_SOLVER=0 ) diff --git a/clang/test/Tooling/clang-linker-wrapper-spirv-elf.cpp b/clang/test/Tooling/clang-linker-wrapper-spirv-elf.cpp index 4f8658064e857d0..9b16727d7419251 100644 --- a/clang/test/Tooling/clang-linker-wrapper-spirv-elf.cpp +++ b/clang/test/Tooling/clang-linker-wrapper-spirv-elf.cpp @@ -1,6 +1,7 @@ // Verify the ELF packaging of OpenMP SPIR-V device images. // REQUIRES: system-linux // REQUIRES: spirv-tools +// REQUIRES: llvm-spirv // RUN: mkdir -p %t_tmp // RUN: cd %t_tmp // RUN: %clangxx -fopenmp -fopenmp-targets=spirv64-intel -nogpulib -c -o %t_clang-linker-wrapper-spirv-elf.o %s diff --git a/clang/test/Tooling/lit.local.cfg b/clang/test/Tooling/lit.local.cfg index bc2a096c8f64f88..9083a48c7bb2a4f 100644 --- a/clang/test/Tooling/lit.local.cfg +++ b/clang/test/Tooling/lit.local.cfg @@ -1,3 +1,5 @@ +import shutil + if not config.root.clang_staticanalyzer: config.unsupported = True @@ -6,3 +8,7 @@ if config.spirv_tools_tests: config.substitutions.append(("spirv-dis", os.path.join(config.llvm_tools_dir, "spirv-dis"))) config.substitutions.append(("spirv-val", os.path.join(config.llvm_tools_dir, "spirv-val"))) config.substitutions.append(("spirv-as", os.path.join(config.llvm_tools_dir, "spirv-as"))) +config.substitutions.append(("spirv-link", os.path.join(config.llvm_tools_dir, "spirv-link"))) + +if shutil.which("llvm-spirv"): +config.available_features.add("llvm-spirv") ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-linker-wrapper][lit] Fix SPIR-V ELF test when spirv-tools feature is available (PR #126756)
https://github.com/sarnex edited https://github.com/llvm/llvm-project/pull/126756 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-linker-wrapper][lit] Fix SPIR-V ELF test when spirv-tools feature is available (PR #126756)
https://github.com/sarnex edited https://github.com/llvm/llvm-project/pull/126756 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Clang] Add __has_target_builtin macro (PR #126324)
https://github.com/sarnex edited https://github.com/llvm/llvm-project/pull/126324 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits