LLVM 24.0.0git
AMDGPUPrintf.cpp
Go to the documentation of this file.
1//===- AMDGPUPrintf.cpp ---------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Helpers for the AMDGPU printf runtime interface, shared by the lowerings of
10// printf calls on the AMDGPU target.
11//
12//===----------------------------------------------------------------------===//
13
18#include "llvm/Support/MD5.h"
19
20using namespace llvm;
21
22// Scan the format string to locate all specifiers, and mark the ones that
23// specify a string, i.e, the "%s" specifier with optional '*' characters.
25 static const char ConvSpecifiers[] = "diouxXfFeEgGaAcspn";
26 // Skip the first argument, the format string.
27 unsigned ArgIdx = 1;
28
29 size_t SpecPos;
30 while ((SpecPos = Str.find('%')) != StringRef::npos) {
31 Str = Str.drop_front(SpecPos + 1);
32 // "%%" prints a '%' and takes no argument.
33 if (Str.consume_front("%"))
34 continue;
35 size_t SpecEnd = Str.find_first_of(ConvSpecifiers);
36 if (SpecEnd == StringRef::npos)
37 return;
38 // Each '*' in the specifier takes an argument of its own.
39 ArgIdx += Str.take_front(SpecEnd).count('*');
40 if (Str[SpecEnd] == 's')
41 BV.set(ArgIdx);
42 Str = Str.drop_front(SpecEnd + 1);
43 ++ArgIdx;
44 }
45}
46
48 MD5 Hasher;
49 MD5::MD5Result Hash;
50 Hasher.update(Fmt);
51 Hasher.final(Hash);
52 return Hash.low();
53}
54
56 // Try sticking to llvm.printf.fmts format, although we are not going to
57 // use the ID and argument size fields while printing,
58 return "0:0:" +
59 llvm::utohexstr(getAMDGPUPrintfFormatHash(Fmt), /*LowerCase=*/true) +
60 "," + Fmt.str();
61}
62
65 std::string Str(S.str() + '\0');
66
67 DataExtractor Extractor(Str, /*IsLittleEndian=*/true);
69 while (Offset && Offset.tell() < Str.size()) {
70 const uint64_t ReadSize = 4;
71 uint64_t ReadNow = std::min(ReadSize, Str.size() - Offset.tell());
72 uint64_t ReadBytes = 0;
73 switch (ReadNow) {
74 default:
75 llvm_unreachable("min(4, X) > 4?");
76 case 1:
77 ReadBytes = Extractor.getU8(Offset);
78 break;
79 case 2:
80 ReadBytes = Extractor.getU16(Offset);
81 break;
82 case 3:
83 ReadBytes = Extractor.getU24(Offset);
84 break;
85 case 4:
86 ReadBytes = Extractor.getU32(Offset);
87 break;
88 }
89 cantFail(Offset.takeError(), "failed to read bytes from constant array");
90 Words.push_back(static_cast<uint32_t>(ReadBytes));
91 }
92 // Additional padding for 8 byte alignment
93 int Rem = (Str.size() % 8);
94 if (Rem > 0 && Rem <= 4)
95 Words.push_back(0);
96}
Helpers for the AMDGPU printf runtime interface, shared by the lowerings of printf calls on the AMDGP...
This file contains some functions that are useful when dealing with strings.
A class representing a position in a DataExtractor, as well as any error encountered during extractio...
LLVM_ABI void update(ArrayRef< uint8_t > Data)
Updates the hash for the byte stream provided.
Definition MD5.cpp:188
LLVM_ABI void final(MD5Result &Result)
Finishes off the hash and puts the result in result.
Definition MD5.cpp:233
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
void set(unsigned Idx)
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
static constexpr size_t npos
Definition StringRef.h:58
std::string str() const
Get the contents as an std::string.
Definition StringRef.h:222
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
std::string utohexstr(uint64_t X, bool LowerCase=false, unsigned Width=0)
LLVM_ABI std::string getAMDGPUPrintfFormatMetadata(StringRef Fmt)
Returns the llvm.printf.fmts entry that maps the ID of the constant format string Fmt back to the str...
LLVM_ABI uint64_t getAMDGPUPrintfFormatHash(StringRef Fmt)
Returns the ID that buffered printf stores in place of the constant format string Fmt the low 64 bits...
LLVM_ABI void locateAMDGPUPrintfCStrings(SparseBitVector< 8 > &BV, StringRef Fmt)
Marks the printf arguments that the format string Fmt specifies as strings, i.e.
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition Error.h:769
LLVM_ABI void packAMDGPUPrintfConstantString(StringRef Str, SmallVectorImpl< uint32_t > &Words)
Splits the constant string argument Str, including its terminating NUL, into the little-endian 32-bit...
uint64_t low() const
Definition MD5.h:47