LLVM 24.0.0git
ForceFunctionAttrs.cpp
Go to the documentation of this file.
1//===- ForceFunctionAttrs.cpp - Force function attrs for debugging --------===//
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
10#include "IPOOptions.h"
11#include "llvm/IR/Function.h"
12#include "llvm/IR/Module.h"
13#include "llvm/Support/Debug.h"
17using namespace llvm;
18
19#define DEBUG_TYPE "forceattrs"
20
22 switch (Kind) {
23 case Attribute::AlwaysInline:
24 return F.hasFnAttribute(Attribute::NoInline);
25
26 case Attribute::NoInline:
27 return F.hasFnAttribute(Attribute::AlwaysInline);
28
29 case Attribute::OptimizeNone:
30 return F.hasFnAttribute(Attribute::MinSize) ||
31 F.hasFnAttribute(Attribute::OptimizeForSize) ||
32 F.hasFnAttribute(Attribute::OptimizeForDebugging);
33
34 case Attribute::MinSize:
35 return F.hasFnAttribute(Attribute::OptimizeNone) ||
36 F.hasFnAttribute(Attribute::OptimizeForDebugging);
37
38 case Attribute::OptimizeForSize:
39 return F.hasFnAttribute(Attribute::OptimizeNone) ||
40 F.hasFnAttribute(Attribute::OptimizeForDebugging);
41
42 case Attribute::OptimizeForDebugging:
43 return F.hasFnAttribute(Attribute::OptimizeNone) ||
44 F.hasFnAttribute(Attribute::MinSize) ||
45 F.hasFnAttribute(Attribute::OptimizeForSize);
46
47 default:
48 return false;
49 }
50}
51
52/// If F has any forced attributes given on the command line, add them.
53/// If F has any forced remove attributes given on the command line, remove
54/// them. When both force and force-remove are given to a function, the latter
55/// takes precedence.
56static void forceAttributes(const IPOOptions &Opts, Function &F) {
57 auto ParseFunctionAndAttr = [&](StringRef S) {
58 StringRef AttributeText;
59 if (S.contains(':')) {
60 auto KV = StringRef(S).split(':');
61 if (KV.first != F.getName())
62 return Attribute::None;
63 AttributeText = KV.second;
64 } else {
65 AttributeText = S;
66 }
67 auto Kind = Attribute::getAttrKindFromName(AttributeText);
68 if (Kind == Attribute::None || !Attribute::canUseAsFnAttr(Kind)) {
69 LLVM_DEBUG(dbgs() << "ForcedAttribute: " << AttributeText
70 << " unknown or not a function attribute!\n");
71 }
72 return Kind;
73 };
74
75 for (const auto &S : Opts.force_attribute) {
76 auto Kind = ParseFunctionAndAttr(S);
77 if (Kind == Attribute::None || F.hasFnAttribute(Kind) ||
79 continue;
80 F.addFnAttr(Kind);
81 }
82
83 for (const auto &S : Opts.force_remove_attribute) {
84 auto Kind = ParseFunctionAndAttr(S);
85 if (Kind == Attribute::None || !F.hasFnAttribute(Kind))
86 continue;
87 F.removeFnAttr(Kind);
88 }
89}
90
93 const IPOOptions &Opts = getIPOOptions(M);
94 bool Changed = false;
95 if (!Opts.forceattrs_csv_path.empty()) {
96 auto BufferOrError = MemoryBuffer::getFileOrSTDIN(Opts.forceattrs_csv_path);
97 if (!BufferOrError) {
98 std::error_code EC = BufferOrError.getError();
99 M.getContext().emitError("cannot open CSV file: " + EC.message());
100 return PreservedAnalyses::all();
101 }
102
103 StringRef Buffer = BufferOrError.get()->getBuffer();
106 for (; !It.is_at_end(); ++It) {
107 auto SplitPair = It->split(',');
108 if (SplitPair.second.empty())
109 continue;
110 Function *Func = M.getFunction(SplitPair.first);
111 if (Func) {
112 if (Func->isDeclaration())
113 continue;
114 auto SecondSplitPair = SplitPair.second.split('=');
115 if (!SecondSplitPair.second.empty()) {
116 Func->addFnAttr(SecondSplitPair.first, SecondSplitPair.second);
117 Changed = true;
118 } else {
119 auto AttrKind = Attribute::getAttrKindFromName(SplitPair.second);
120 if (AttrKind != Attribute::None &&
121 Attribute::canUseAsFnAttr(AttrKind) &&
122 !hasConflictingFnAttr(AttrKind, *Func)) {
123 // TODO: There could be string attributes without a value, we should
124 // support those, too.
125 Func->addFnAttr(AttrKind);
126 Changed = true;
127 } else
128 errs() << "Cannot add " << SplitPair.second
129 << " as an attribute name.\n";
130 }
131 } else {
132 errs() << "Function in CSV file at line " << It.line_number()
133 << " does not exist.\n";
134 // TODO: `report_fatal_error at end of pass for missing functions.
135 continue;
136 }
137 }
138 }
139 if (!Opts.force_attribute.empty() || !Opts.force_remove_attribute.empty()) {
140 for (Function &F : M.functions())
141 forceAttributes(Opts, F);
142 Changed = true;
143 }
144 // Just conservatively invalidate analyses if we've made any changes, this
145 // isn't likely to be important.
147}
static bool hasConflictingFnAttr(Attribute::AttrKind Kind, Function &F)
static void forceAttributes(const IPOOptions &Opts, Function &F)
If F has any forced attributes given on the command line, add them.
Super simple passes to force specific function attrs from the commandline into the IR for debugging p...
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition MD5.cpp:54
#define LLVM_DEBUG(...)
Definition Debug.h:119
static LLVM_ABI Attribute::AttrKind getAttrKindFromName(StringRef AttrName)
static LLVM_ABI bool canUseAsFnAttr(AttrKind Kind)
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results,...
Definition Attributes.h:126
@ None
No attributes have been set.
Definition Attributes.h:128
This interface provides simple read-only access to a block of memory, and provides simple methods for...
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition StringRef.h:728
A forward iterator which reads text lines from a buffer.
int64_t line_number() const
Return the current line number. May return any number at EOF.
bool is_at_end() const
Return true if we're an "end" iterator or have reached EOF.
This is an optimization pass for GlobalISel generic memory operations.
const IPOOptions & getIPOOptions(const LLVMContext &C)
Definition IPOOptions.h:36
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &)