https://github.com/steffenlarsen updated https://github.com/llvm/llvm-project/pull/238399
>From 6112073ca3e641c84c7e900165f2b48369549602 Mon Sep 17 00:00:00 2001 From: Steffen Holst Larsen <[email protected]> Date: Tue, 3 Nov 2026 09:21:29 -0600 Subject: [PATCH] [CIR][AMDGPU] Move AMDGPU printf emit helpers (NFC) Move the AMDGPU printf emit helpers from llvm/lib/Support/AMDGPUPrintf.cpp to llvm/lib/Transforms/Utils/AMDGPUEmitPrintf.cpp. Signed-off-by: Steffen Holst Larsen <[email protected]> --- .../CIR/Lowering/DirectToLLVM/CMakeLists.txt | 1 + .../LowerToLLVMOffloadPrintfAMDGPU.cpp | 2 +- llvm/include/llvm/Support/AMDGPUPrintf.h | 53 ---------- .../llvm/Transforms/Utils/AMDGPUEmitPrintf.h | 29 ++++++ llvm/lib/Support/AMDGPUPrintf.cpp | 96 ------------------- llvm/lib/Support/CMakeLists.txt | 1 - .../lib/Transforms/Utils/AMDGPUEmitPrintf.cpp | 81 +++++++++++++++- .../gn/secondary/llvm/lib/Support/BUILD.gn | 1 - 8 files changed, 111 insertions(+), 153 deletions(-) delete mode 100644 llvm/include/llvm/Support/AMDGPUPrintf.h delete mode 100644 llvm/lib/Support/AMDGPUPrintf.cpp diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt b/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt index 3eea0ed15c381a..915456b90ecb5c 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt +++ b/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt @@ -1,6 +1,7 @@ set(LLVM_LINK_COMPONENTS Core Support + TransformUtils ) add_clang_library(clangCIRLoweringDirectToLLVM diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMOffloadPrintfAMDGPU.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMOffloadPrintfAMDGPU.cpp index 26f31a931a4de4..62883b562d89f7 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMOffloadPrintfAMDGPU.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMOffloadPrintfAMDGPU.cpp @@ -18,8 +18,8 @@ #include "mlir/IR/BuiltinOps.h" #include "clang/CIR/Dialect/IR/CIRDialect.h" #include "llvm/ADT/SparseBitVector.h" -#include "llvm/Support/AMDGPUPrintf.h" #include "llvm/Support/MathExtras.h" +#include "llvm/Transforms/Utils/AMDGPUEmitPrintf.h" namespace cir { namespace direct { diff --git a/llvm/include/llvm/Support/AMDGPUPrintf.h b/llvm/include/llvm/Support/AMDGPUPrintf.h deleted file mode 100644 index ba382e470482b7..00000000000000 --- a/llvm/include/llvm/Support/AMDGPUPrintf.h +++ /dev/null @@ -1,53 +0,0 @@ -//===- AMDGPUPrintf.h -------------------------------------------*- C++ -*-===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// -// -/// \file -/// Helpers for the AMDGPU printf runtime interface, shared by the lowerings of -/// printf calls on the AMDGPU target. -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_SUPPORT_AMDGPUPRINTF_H -#define LLVM_SUPPORT_AMDGPUPRINTF_H - -#include "llvm/ADT/SmallVector.h" -#include "llvm/ADT/SparseBitVector.h" -#include "llvm/ADT/StringRef.h" -#include "llvm/Support/Compiler.h" -#include <cstdint> -#include <string> - -namespace llvm { - -/// Marks the printf arguments that the format string \p Fmt specifies as -/// strings, i.e. with a "%s" specifier. Argument 0 is the format string itself, -/// and each '*' in a specifier consumes an argument of its own. -LLVM_ABI void locateAMDGPUPrintfCStrings(SparseBitVector<8> &BV, StringRef Fmt); - -/// Returns the ID that buffered printf stores in place of the constant format -/// string \p Fmt the low 64 bits of its MD5 hash. -LLVM_ABI uint64_t getAMDGPUPrintfFormatHash(StringRef Fmt); - -/// Returns the llvm.printf.fmts entry that maps the ID of the constant format -/// string \p Fmt back to the string. -LLVM_ABI std::string getAMDGPUPrintfFormatMetadata(StringRef Fmt); - -/// The llvm.printf.fmts entry added when a module only uses buffered printf -/// with non-constant format strings. -inline constexpr StringLiteral AMDGPUPrintfNonConstFormatMetadata = - "0:0:ffffffff,\"Non const format string\""; - -/// Splits the constant string argument \p Str, including its terminating NUL, -/// into the little-endian 32-bit words that buffered printf stores for it, -/// padded to a multiple of 8 bytes. -LLVM_ABI void packAMDGPUPrintfConstantString(StringRef Str, - SmallVectorImpl<uint32_t> &Words); - -} // end namespace llvm - -#endif // LLVM_SUPPORT_AMDGPUPRINTF_H diff --git a/llvm/include/llvm/Transforms/Utils/AMDGPUEmitPrintf.h b/llvm/include/llvm/Transforms/Utils/AMDGPUEmitPrintf.h index e7adf91b3f5695..f8419ea243792d 100644 --- a/llvm/include/llvm/Transforms/Utils/AMDGPUEmitPrintf.h +++ b/llvm/include/llvm/Transforms/Utils/AMDGPUEmitPrintf.h @@ -14,14 +14,43 @@ #ifndef LLVM_TRANSFORMS_UTILS_AMDGPUEMITPRINTF_H #define LLVM_TRANSFORMS_UTILS_AMDGPUEMITPRINTF_H +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/SparseBitVector.h" +#include "llvm/ADT/StringRef.h" #include "llvm/IR/IRBuilder.h" #include "llvm/Support/Compiler.h" +#include <cstdint> +#include <string> namespace llvm { LLVM_ABI Value *emitAMDGPUPrintfCall(IRBuilder<> &Builder, ArrayRef<Value *> Args, bool isBuffered); +/// Marks the printf arguments that the format string \p Fmt specifies as +/// strings, i.e. with a "%s" specifier. Argument 0 is the format string itself, +/// and each '*' in a specifier consumes an argument of its own. +LLVM_ABI void locateAMDGPUPrintfCStrings(SparseBitVector<8> &BV, StringRef Fmt); + +/// Returns the ID that buffered printf stores in place of the constant format +/// string \p Fmt the low 64 bits of its MD5 hash. +LLVM_ABI uint64_t getAMDGPUPrintfFormatHash(StringRef Fmt); + +/// Returns the llvm.printf.fmts entry that maps the ID of the constant format +/// string \p Fmt back to the string. +LLVM_ABI std::string getAMDGPUPrintfFormatMetadata(StringRef Fmt); + +/// The llvm.printf.fmts entry added when a module only uses buffered printf +/// with non-constant format strings. +inline constexpr StringLiteral AMDGPUPrintfNonConstFormatMetadata = + "0:0:ffffffff,\"Non const format string\""; + +/// Splits the constant string argument \p Str, including its terminating NUL, +/// into the little-endian 32-bit words that buffered printf stores for it, +/// padded to a multiple of 8 bytes. +LLVM_ABI void packAMDGPUPrintfConstantString(StringRef Str, + SmallVectorImpl<uint32_t> &Words); + } // end namespace llvm #endif // LLVM_TRANSFORMS_UTILS_AMDGPUEMITPRINTF_H diff --git a/llvm/lib/Support/AMDGPUPrintf.cpp b/llvm/lib/Support/AMDGPUPrintf.cpp deleted file mode 100644 index a596b3374796be..00000000000000 --- a/llvm/lib/Support/AMDGPUPrintf.cpp +++ /dev/null @@ -1,96 +0,0 @@ -//===- AMDGPUPrintf.cpp ---------------------------------------------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// -// -// Helpers for the AMDGPU printf runtime interface, shared by the lowerings of -// printf calls on the AMDGPU target. -// -//===----------------------------------------------------------------------===// - -#include "llvm/Support/AMDGPUPrintf.h" -#include "llvm/ADT/StringExtras.h" -#include "llvm/Support/DataExtractor.h" -#include "llvm/Support/ErrorHandling.h" -#include "llvm/Support/MD5.h" - -using namespace llvm; - -// Scan the format string to locate all specifiers, and mark the ones that -// specify a string, i.e, the "%s" specifier with optional '*' characters. -void llvm::locateAMDGPUPrintfCStrings(SparseBitVector<8> &BV, StringRef Str) { - static const char ConvSpecifiers[] = "diouxXfFeEgGaAcspn"; - // Skip the first argument, the format string. - unsigned ArgIdx = 1; - - size_t SpecPos; - while ((SpecPos = Str.find('%')) != StringRef::npos) { - Str = Str.drop_front(SpecPos + 1); - // "%%" prints a '%' and takes no argument. - if (Str.consume_front("%")) - continue; - size_t SpecEnd = Str.find_first_of(ConvSpecifiers); - if (SpecEnd == StringRef::npos) - return; - // Each '*' in the specifier takes an argument of its own. - ArgIdx += Str.take_front(SpecEnd).count('*'); - if (Str[SpecEnd] == 's') - BV.set(ArgIdx); - Str = Str.drop_front(SpecEnd + 1); - ++ArgIdx; - } -} - -uint64_t llvm::getAMDGPUPrintfFormatHash(StringRef Fmt) { - MD5 Hasher; - MD5::MD5Result Hash; - Hasher.update(Fmt); - Hasher.final(Hash); - return Hash.low(); -} - -std::string llvm::getAMDGPUPrintfFormatMetadata(StringRef Fmt) { - // Try sticking to llvm.printf.fmts format, although we are not going to - // use the ID and argument size fields while printing, - return "0:0:" + - llvm::utohexstr(getAMDGPUPrintfFormatHash(Fmt), /*LowerCase=*/true) + - "," + Fmt.str(); -} - -void llvm::packAMDGPUPrintfConstantString(StringRef S, - SmallVectorImpl<uint32_t> &Words) { - std::string Str(S.str() + '\0'); - - DataExtractor Extractor(Str, /*IsLittleEndian=*/true); - DataExtractor::Cursor Offset(0); - while (Offset && Offset.tell() < Str.size()) { - const uint64_t ReadSize = 4; - uint64_t ReadNow = std::min(ReadSize, Str.size() - Offset.tell()); - uint64_t ReadBytes = 0; - switch (ReadNow) { - default: - llvm_unreachable("min(4, X) > 4?"); - case 1: - ReadBytes = Extractor.getU8(Offset); - break; - case 2: - ReadBytes = Extractor.getU16(Offset); - break; - case 3: - ReadBytes = Extractor.getU24(Offset); - break; - case 4: - ReadBytes = Extractor.getU32(Offset); - break; - } - cantFail(Offset.takeError(), "failed to read bytes from constant array"); - Words.push_back(static_cast<uint32_t>(ReadBytes)); - } - // Additional padding for 8 byte alignment - int Rem = (Str.size() % 8); - if (Rem > 0 && Rem <= 4) - Words.push_back(0); -} diff --git a/llvm/lib/Support/CMakeLists.txt b/llvm/lib/Support/CMakeLists.txt index a235991aa1689e..3d3e27d89ef352 100644 --- a/llvm/lib/Support/CMakeLists.txt +++ b/llvm/lib/Support/CMakeLists.txt @@ -156,7 +156,6 @@ add_subdirectory(BLAKE3) add_llvm_component_library(LLVMSupport ABIBreak.cpp AMDGPUMetadata.cpp - AMDGPUPrintf.cpp APFixedPoint.cpp APFloat.cpp APInt.cpp diff --git a/llvm/lib/Transforms/Utils/AMDGPUEmitPrintf.cpp b/llvm/lib/Transforms/Utils/AMDGPUEmitPrintf.cpp index 925e12379d9bb5..e5981bc260b81c 100644 --- a/llvm/lib/Transforms/Utils/AMDGPUEmitPrintf.cpp +++ b/llvm/lib/Transforms/Utils/AMDGPUEmitPrintf.cpp @@ -16,9 +16,12 @@ #include "llvm/Transforms/Utils/AMDGPUEmitPrintf.h" #include "llvm/ADT/SparseBitVector.h" +#include "llvm/ADT/StringExtras.h" #include "llvm/Analysis/ValueTracking.h" #include "llvm/IR/Module.h" -#include "llvm/Support/AMDGPUPrintf.h" +#include "llvm/Support/DataExtractor.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/MD5.h" #include "llvm/Support/MathExtras.h" using namespace llvm; @@ -176,6 +179,82 @@ static Value *processArg(IRBuilder<> &Builder, Value *Desc, Value *Arg, return appendArg(Builder, Desc, Arg, IsLast); } +// Scan the format string to locate all specifiers, and mark the ones that +// specify a string, i.e, the "%s" specifier with optional '*' characters. +void llvm::locateAMDGPUPrintfCStrings(SparseBitVector<8> &BV, StringRef Str) { + static const char ConvSpecifiers[] = "diouxXfFeEgGaAcspn"; + // Skip the first argument, the format string. + unsigned ArgIdx = 1; + + size_t SpecPos; + while ((SpecPos = Str.find('%')) != StringRef::npos) { + Str = Str.drop_front(SpecPos + 1); + // "%%" prints a '%' and takes no argument. + if (Str.consume_front("%")) + continue; + size_t SpecEnd = Str.find_first_of(ConvSpecifiers); + if (SpecEnd == StringRef::npos) + return; + // Each '*' in the specifier takes an argument of its own. + ArgIdx += Str.take_front(SpecEnd).count('*'); + if (Str[SpecEnd] == 's') + BV.set(ArgIdx); + Str = Str.drop_front(SpecEnd + 1); + ++ArgIdx; + } +} + +uint64_t llvm::getAMDGPUPrintfFormatHash(StringRef Fmt) { + MD5 Hasher; + MD5::MD5Result Hash; + Hasher.update(Fmt); + Hasher.final(Hash); + return Hash.low(); +} + +std::string llvm::getAMDGPUPrintfFormatMetadata(StringRef Fmt) { + // Try sticking to llvm.printf.fmts format, although we are not going to + // use the ID and argument size fields while printing, + return "0:0:" + + llvm::utohexstr(getAMDGPUPrintfFormatHash(Fmt), /*LowerCase=*/true) + + "," + Fmt.str(); +} + +void llvm::packAMDGPUPrintfConstantString(StringRef S, + SmallVectorImpl<uint32_t> &Words) { + std::string Str(S.str() + '\0'); + + DataExtractor Extractor(Str, /*IsLittleEndian=*/true); + DataExtractor::Cursor Offset(0); + while (Offset && Offset.tell() < Str.size()) { + const uint64_t ReadSize = 4; + uint64_t ReadNow = std::min(ReadSize, Str.size() - Offset.tell()); + uint64_t ReadBytes = 0; + switch (ReadNow) { + default: + llvm_unreachable("min(4, X) > 4?"); + case 1: + ReadBytes = Extractor.getU8(Offset); + break; + case 2: + ReadBytes = Extractor.getU16(Offset); + break; + case 3: + ReadBytes = Extractor.getU24(Offset); + break; + case 4: + ReadBytes = Extractor.getU32(Offset); + break; + } + cantFail(Offset.takeError(), "failed to read bytes from constant array"); + Words.push_back(static_cast<uint32_t>(ReadBytes)); + } + // Additional padding for 8 byte alignment + int Rem = (Str.size() % 8); + if (Rem > 0 && Rem <= 4) + Words.push_back(0); +} + // helper struct to package the string related data struct StringData { StringRef Str; diff --git a/llvm/utils/gn/secondary/llvm/lib/Support/BUILD.gn b/llvm/utils/gn/secondary/llvm/lib/Support/BUILD.gn index 01fd88048cb10e..35f364e073a69c 100644 --- a/llvm/utils/gn/secondary/llvm/lib/Support/BUILD.gn +++ b/llvm/utils/gn/secondary/llvm/lib/Support/BUILD.gn @@ -39,7 +39,6 @@ static_library("Support") { "AArch64BuildAttributes.cpp", "ABIBreak.cpp", "AMDGPUMetadata.cpp", - "AMDGPUPrintf.cpp", "APFixedPoint.cpp", "APFloat.cpp", "APInt.cpp", _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
