https://github.com/martin-luecke updated https://github.com/llvm/llvm-project/pull/243824
From 5c616cd1a5312285b67067269e4773b1313d6008 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Paul=20L=C3=BCcke?= <[email protected]> Date: Mon, 9 Nov 2026 12:58:40 +0000 Subject: [PATCH] [Clang] Share externalized device symbol suffix generation Move CUDA/HIP device symbol suffix generation into CodeGenUtils for reuse by CIR. Preserve the naming rules for compilation identifiers, source files and macros, and the file lookup diagnostics. Assisted by: GPT 6.1 Sol --- .../include/clang/CodeGenUtils/ModuleUtils.h | 17 +++++++ clang/lib/CodeGen/CodeGenModule.cpp | 41 +--------------- clang/lib/CodeGenUtils/ModuleUtils.cpp | 49 +++++++++++++++++++ .../llvm-project-overlay/clang/BUILD.bazel | 1 + 4 files changed, 69 insertions(+), 39 deletions(-) diff --git a/clang/include/clang/CodeGenUtils/ModuleUtils.h b/clang/include/clang/CodeGenUtils/ModuleUtils.h index 37b4eaa9fac151..b5625c263df14d 100644 --- a/clang/include/clang/CodeGenUtils/ModuleUtils.h +++ b/clang/include/clang/CodeGenUtils/ModuleUtils.h @@ -20,8 +20,25 @@ #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/SmallVector.h" +namespace clang { +class PreprocessorOptions; +} // namespace clang + +namespace llvm { +class raw_ostream; +namespace vfs { +class FileSystem; +} // namespace vfs +} // namespace llvm + namespace clang::CodeGenUtils { +/// Print the unique suffix for an externalized CUDA/HIP device symbol. +void printPostfixForExternalizedDecl(llvm::raw_ostream &OS, + const ASTContext &Ctx, const Decl &D, + const PreprocessorOptions &PPOpts, + llvm::vfs::FileSystem &FS); + /// Determines whether the language options require us to model /// unwind exceptions. We treat -fexceptions as mandating this /// except under the fragile ObjC ABI with only ObjC exceptions diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index f0ea493e172229..8e16665018582b 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -8981,45 +8981,8 @@ bool CodeGenModule::stopAutoInit() { void CodeGenModule::printPostfixForExternalizedDecl(llvm::raw_ostream &OS, const Decl *D) const { - // ptxas does not allow '.' in symbol names. On the other hand, HIP prefers - // postfix beginning with '.' since the symbol name can be demangled. - if (LangOpts.HIP) - OS << (isa<VarDecl>(D) ? ".static." : ".intern."); - else - OS << (isa<VarDecl>(D) ? "__static__" : "__intern__"); - - // If the CUID is not specified we try to generate a unique postfix. - if (getLangOpts().CUID.empty()) { - SourceManager &SM = getContext().getSourceManager(); - PresumedLoc PLoc = SM.getPresumedLoc(D->getLocation()); - assert(PLoc.isValid() && "Source location is expected to be valid."); - - // Get the hash of the user defined macros. - llvm::MD5 Hash; - llvm::MD5::MD5Result Result; - for (const auto &Arg : PreprocessorOpts.Macros) - Hash.update(Arg.first); - Hash.final(Result); - - // Get the UniqueID for the file containing the decl. - llvm::sys::fs::UniqueID ID; - auto Status = FS->status(PLoc.getFilename()); - if (!Status) { - PLoc = SM.getPresumedLoc(D->getLocation(), /*UseLineDirectives=*/false); - assert(PLoc.isValid() && "Source location is expected to be valid."); - Status = FS->status(PLoc.getFilename()); - } - if (!Status) { - SM.getDiagnostics().Report(diag::err_cannot_open_file) - << PLoc.getFilename() << Status.getError().message(); - } else { - ID = Status->getUniqueID(); - } - OS << llvm::format("%x", ID.getFile()) << llvm::format("%x", ID.getDevice()) - << "_" << llvm::utohexstr(Result.low(), /*LowerCase=*/true, /*Width=*/8); - } else { - OS << getContext().getCUIDHash(); - } + CodeGenUtils::printPostfixForExternalizedDecl(OS, getContext(), *D, + PreprocessorOpts, *FS); } void CodeGenModule::moveLazyEmissionStates(CodeGenModule *NewBuilder) { diff --git a/clang/lib/CodeGenUtils/ModuleUtils.cpp b/clang/lib/CodeGenUtils/ModuleUtils.cpp index 847ba79c890fff..d23ac48f0f6b47 100644 --- a/clang/lib/CodeGenUtils/ModuleUtils.cpp +++ b/clang/lib/CodeGenUtils/ModuleUtils.cpp @@ -8,11 +8,60 @@ #include "clang/CodeGenUtils/ModuleUtils.h" #include "clang/AST/Attr.h" +#include "clang/Basic/DiagnosticFrontend.h" +#include "clang/Basic/SourceManager.h" #include "clang/Basic/TargetInfo.h" +#include "clang/Lex/PreprocessorOptions.h" #include "llvm/ADT/SetVector.h" +#include "llvm/ADT/StringExtras.h" +#include "llvm/Support/Format.h" +#include "llvm/Support/MD5.h" +#include "llvm/Support/VirtualFileSystem.h" +#include "llvm/Support/raw_ostream.h" namespace clang::CodeGenUtils { +void printPostfixForExternalizedDecl(llvm::raw_ostream &OS, + const ASTContext &Ctx, const Decl &D, + const PreprocessorOptions &PPOpts, + llvm::vfs::FileSystem &FS) { + // ptxas does not allow '.', whereas HIP preserves demangleable names. + if (Ctx.getLangOpts().HIP) + OS << (isa<VarDecl>(D) ? ".static." : ".intern."); + else + OS << (isa<VarDecl>(D) ? "__static__" : "__intern__"); + + if (!Ctx.getLangOpts().CUID.empty()) { + OS << Ctx.getCUIDHash(); + return; + } + + const SourceManager &SM = Ctx.getSourceManager(); + PresumedLoc PLoc = SM.getPresumedLoc(D.getLocation()); + assert(PLoc.isValid() && "source location is expected to be valid"); + + llvm::MD5 Hash; + llvm::MD5::MD5Result Result; + for (const auto &Arg : PPOpts.Macros) + Hash.update(Arg.first); + Hash.final(Result); + + auto Status = FS.status(PLoc.getFilename()); + if (!Status) { + PLoc = SM.getPresumedLoc(D.getLocation(), /*UseLineDirectives=*/false); + assert(PLoc.isValid() && "source location is expected to be valid"); + Status = FS.status(PLoc.getFilename()); + } + if (!Status) { + SM.getDiagnostics().Report(diag::err_cannot_open_file) + << PLoc.getFilename() << Status.getError().message(); + return; + } + llvm::sys::fs::UniqueID ID = Status->getUniqueID(); + OS << llvm::format("%x", ID.getFile()) << llvm::format("%x", ID.getDevice()) + << "_" << llvm::utohexstr(Result.low(), /*LowerCase=*/true, /*Width=*/8); +} + LangAS getGlobalConstantAddressSpace(const LangOptions &LangOpts, const TargetInfo &Target) { // OpenCL v1.2 s6.5.3: a string literal is in the constant address space. diff --git a/utils/bazel/llvm-project-overlay/clang/BUILD.bazel b/utils/bazel/llvm-project-overlay/clang/BUILD.bazel index 1fe94811cb5442..a570c87b7c7b4a 100644 --- a/utils/bazel/llvm-project-overlay/clang/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/clang/BUILD.bazel @@ -2197,6 +2197,7 @@ cc_library( deps = [ ":ast", ":basic", + ":lex", "//llvm:CodeGen", "//llvm:Core", "//llvm:Demangle", _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
