LLVM 20.0.0git
Remark.cpp
Go to the documentation of this file.
1//===- Remark.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// Implementation of the Remark type and the C API.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/Remarks/Remark.h"
14#include "llvm/ADT/APInt.h"
15#include "llvm/ADT/ArrayRef.h"
16#include <optional>
17
18using namespace llvm;
19using namespace llvm::remarks;
20
21std::string Remark::getArgsAsMsg() const {
22 std::string Str;
24 for (const Argument &Arg : Args)
25 OS << Arg.Val;
26 return Str;
27}
28
29/// Returns the value of a specified key parsed from StringRef.
30std::optional<int> Argument::getValAsInt() const {
31 APInt KeyVal;
32 if (Val.getAsInteger(10, KeyVal))
33 return std::nullopt;
34 return KeyVal.getSExtValue();
35}
36
37bool Argument::isValInt() const { return getValAsInt().has_value(); }
38
40 OS << "{ "
41 << "File: " << SourceFilePath << ", Line: " << SourceLine
42 << " Column:" << SourceColumn << " }\n";
43}
44
46 OS << Key << ": " << Val << "\n";
47}
48
50 OS << "Name: ";
51 OS << RemarkName << "\n";
52 OS << "Type: " << typeToStr(RemarkType) << "\n";
53 OS << "FunctionName: " << FunctionName << "\n";
54 OS << "PassName: " << PassName << "\n";
55 if (Loc)
56 OS << "Loc: " << Loc.value();
57 if (Hotness)
58 OS << "Hotness: " << Hotness;
59 if (!Args.empty()) {
60 OS << "Args:\n";
61 for (auto Arg : Args)
62 OS << "\t" << Arg;
63 }
64}
65
66// Create wrappers for C Binding types (see CBindingWrapping.h).
68
70 return unwrap(String)->data();
71}
72
74 return unwrap(String)->size();
75}
76
77extern "C" LLVMRemarkStringRef
79 return wrap(&unwrap(DL)->SourceFilePath);
80}
81
83 return unwrap(DL)->SourceLine;
84}
85
86extern "C" uint32_t
88 return unwrap(DL)->SourceColumn;
89}
90
92 return wrap(&unwrap(Arg)->Key);
93}
94
96 return wrap(&unwrap(Arg)->Val);
97}
98
99extern "C" LLVMRemarkDebugLocRef
101 if (const std::optional<RemarkLocation> &Loc = unwrap(Arg)->Loc)
102 return wrap(&*Loc);
103 return nullptr;
104}
105
107 delete unwrap(Remark);
108}
109
111 // Assume here that the enums can be converted both ways.
112 return static_cast<LLVMRemarkType>(unwrap(Remark)->RemarkType);
113}
114
115extern "C" LLVMRemarkStringRef
117 return wrap(&unwrap(Remark)->PassName);
118}
119
120extern "C" LLVMRemarkStringRef
122 return wrap(&unwrap(Remark)->RemarkName);
123}
124
125extern "C" LLVMRemarkStringRef
127 return wrap(&unwrap(Remark)->FunctionName);
128}
129
130extern "C" LLVMRemarkDebugLocRef
132 if (const std::optional<RemarkLocation> &Loc = unwrap(Remark)->Loc)
133 return wrap(&*Loc);
134 return nullptr;
135}
136
138 if (const std::optional<uint64_t> &Hotness = unwrap(Remark)->Hotness)
139 return *Hotness;
140 return 0;
141}
142
144 return unwrap(Remark)->Args.size();
145}
146
147extern "C" LLVMRemarkArgRef
149 ArrayRef<Argument> Args = unwrap(Remark)->Args;
150 // No arguments to iterate on.
151 if (Args.empty())
152 return nullptr;
153 return reinterpret_cast<LLVMRemarkArgRef>(
154 const_cast<Argument *>(Args.begin()));
155}
156
157extern "C" LLVMRemarkArgRef
159 // No more arguments to iterate on.
160 if (ArgIt == nullptr)
161 return nullptr;
162
163 auto It = (ArrayRef<Argument>::const_iterator)ArgIt;
164 auto Next = std::next(It);
165 if (Next == unwrap(Remark)->Args.end())
166 return nullptr;
167
168 return reinterpret_cast<LLVMRemarkArgRef>(const_cast<Argument *>(Next));
169}
aarch64 promote const
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)
raw_pwrite_stream & OS
static const char PassName[]
Class for arbitrary precision integers.
Definition: APInt.h:78
int64_t getSExtValue() const
Get sign extended value.
Definition: APInt.h:1520
This class represents an incoming formal argument to a Function.
Definition: Argument.h:31
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:455
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:661
LLVMRemarkStringRef LLVMRemarkEntryGetPassName(LLVMRemarkEntryRef Remark)
Get the name of the pass that emitted this remark.
Definition: Remark.cpp:116
LLVMRemarkStringRef LLVMRemarkArgGetKey(LLVMRemarkArgRef Arg)
Returns the key of an argument.
Definition: Remark.cpp:91
const char * LLVMRemarkStringGetData(LLVMRemarkStringRef String)
Returns the buffer holding the string.
Definition: Remark.cpp:69
uint32_t LLVMRemarkStringGetLen(LLVMRemarkStringRef String)
Returns the size of the string.
Definition: Remark.cpp:73
struct LLVMRemarkOpaqueEntry * LLVMRemarkEntryRef
A remark emitted by the compiler.
Definition: Remarks.h:140
LLVMRemarkType
The type of the emitted remark.
Definition: Remarks.h:41
struct LLVMRemarkOpaqueString * LLVMRemarkStringRef
String containing a buffer and a length.
Definition: Remarks.h:57
void LLVMRemarkEntryDispose(LLVMRemarkEntryRef Remark)
Free the resources used by the remark entry.
Definition: Remark.cpp:106
struct LLVMRemarkOpaqueArg * LLVMRemarkArgRef
Element of the "Args" list.
Definition: Remarks.h:109
uint32_t LLVMRemarkDebugLocGetSourceLine(LLVMRemarkDebugLocRef DL)
Return the line in the source file for a debug location.
Definition: Remark.cpp:82
enum LLVMRemarkType LLVMRemarkEntryGetType(LLVMRemarkEntryRef Remark)
The type of the remark.
Definition: Remark.cpp:110
LLVMRemarkArgRef LLVMRemarkEntryGetFirstArg(LLVMRemarkEntryRef Remark)
Get a new iterator to iterate over a remark's argument.
Definition: Remark.cpp:148
LLVMRemarkArgRef LLVMRemarkEntryGetNextArg(LLVMRemarkArgRef It, LLVMRemarkEntryRef Remark)
Get the next argument in Remark from the position of It.
Definition: Remark.cpp:158
LLVMRemarkStringRef LLVMRemarkDebugLocGetSourceFilePath(LLVMRemarkDebugLocRef DL)
Return the path to the source file for a debug location.
Definition: Remark.cpp:78
LLVMRemarkDebugLocRef LLVMRemarkEntryGetDebugLoc(LLVMRemarkEntryRef Remark)
Returns the debug location that is attached to this remark.
Definition: Remark.cpp:131
uint32_t LLVMRemarkEntryGetNumArgs(LLVMRemarkEntryRef Remark)
The number of arguments the remark holds.
Definition: Remark.cpp:143
LLVMRemarkStringRef LLVMRemarkEntryGetRemarkName(LLVMRemarkEntryRef Remark)
Get an identifier of the remark.
Definition: Remark.cpp:121
LLVMRemarkStringRef LLVMRemarkArgGetValue(LLVMRemarkArgRef Arg)
Returns the value of an argument.
Definition: Remark.cpp:95
uint32_t LLVMRemarkDebugLocGetSourceColumn(LLVMRemarkDebugLocRef DL)
Return the column in the source file for a debug location.
Definition: Remark.cpp:87
struct LLVMRemarkOpaqueDebugLoc * LLVMRemarkDebugLocRef
DebugLoc containing File, Line and Column.
Definition: Remarks.h:78
LLVMRemarkDebugLocRef LLVMRemarkArgGetDebugLoc(LLVMRemarkArgRef Arg)
Returns the debug location that is attached to the value of this argument.
Definition: Remark.cpp:100
LLVMRemarkStringRef LLVMRemarkEntryGetFunctionName(LLVMRemarkEntryRef Remark)
Get the name of the function being processed when the remark was emitted.
Definition: Remark.cpp:126
uint64_t LLVMRemarkEntryGetHotness(LLVMRemarkEntryRef Remark)
Return the hotness of the remark.
Definition: Remark.cpp:137
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
StringRef typeToStr(Type Ty)
Definition: Remark.h:77
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Attribute unwrap(LLVMAttributeRef Attr)
Definition: Attributes.h:320
LLVMAttributeRef wrap(Attribute Attr)
Definition: Attributes.h:315
A key-value pair with a debug location that is used to display the remarks at the right place in the ...
Definition: Remark.h:46
void print(raw_ostream &OS) const
Implement operator<< on Argument.
Definition: Remark.cpp:45
std::optional< int > getValAsInt() const
Return the value of argument as int.
Definition: Remark.cpp:30
bool isValInt() const
Check if the argument value can be parsed as int.
Definition: Remark.cpp:37
void print(raw_ostream &OS) const
Implement operator<< on RemarkLocation.
Definition: Remark.cpp:39
StringRef SourceFilePath
Absolute path of the source file corresponding to this remark.
Definition: Remark.h:33
A remark type used for both emission and parsing.
Definition: Remark.h:97
void print(raw_ostream &OS) const
Implement operator<< on Remark.
Definition: Remark.cpp:49
Type RemarkType
The type of the remark.
Definition: Remark.h:99
StringRef PassName
Name of the pass that triggers the emission of this remark.
Definition: Remark.h:102
std::optional< RemarkLocation > Loc
The location in the source file of the remark.
Definition: Remark.h:113
std::optional< uint64_t > Hotness
If profile information is available, this is the number of times the corresponding code was executed ...
Definition: Remark.h:117
StringRef RemarkName
Textual identifier for the remark (single-word, camel-case).
Definition: Remark.h:107
std::string getArgsAsMsg() const
Return a message composed from the arguments as a string.
Definition: Remark.cpp:21
StringRef FunctionName
Mangled name of the function that triggers the emssion of this remark.
Definition: Remark.h:110
SmallVector< Argument, 5 > Args
Arguments collected via the streaming interface.
Definition: Remark.h:120