LLVM: lib/Transforms/IPO/ForceFunctionAttrs.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
17using namespace llvm;
18
19#define DEBUG_TYPE "forceattrs"
20
24 "Add an attribute to a function. This can be a "
25 "pair of 'function-name:attribute-name', to apply an attribute to a "
26 "specific function. For "
27 "example -force-attribute=foo:noinline. Specifying only an attribute "
28 "will apply the attribute to every function in the module. This "
29 "option can be specified multiple times."));
30
33 cl::desc("Remove an attribute from a function. This can be a "
34 "pair of 'function-name:attribute-name' to remove an attribute "
35 "from a specific function. For "
36 "example -force-remove-attribute=foo:noinline. Specifying only an "
37 "attribute will remove the attribute from all functions in the "
38 "module. This "
39 "option can be specified multiple times."));
40
44 "Path to CSV file containing lines of function names and attributes to "
45 "add to them in the form of `f1,attr1` or `f2,attr2=str`."));
46
47
48
49
50
52 auto ParseFunctionAndAttr = [&](StringRef S) {
54 if (S.contains(':')) {
56 if (KV.first != F.getName())
58 AttributeText = KV.second;
59 } else {
60 AttributeText = S;
61 }
64 LLVM_DEBUG(dbgs() << "ForcedAttribute: " << AttributeText
65 << " unknown or not a function attribute!\n");
66 }
67 return Kind;
68 };
69
71 auto Kind = ParseFunctionAndAttr(S);
73 continue;
74 F.addFnAttr(Kind);
75 }
76
78 auto Kind = ParseFunctionAndAttr(S);
80 continue;
81 F.removeFnAttr(Kind);
82 }
83}
84
88
94 if (!BufferOrError) {
95 std::error_code EC = BufferOrError.getError();
96 M.getContext().emitError("cannot open CSV file: " + EC.message());
98 }
99
100 StringRef Buffer = BufferOrError.get()->getBuffer();
104 auto SplitPair = It->split(',');
105 if (SplitPair.second.empty())
106 continue;
107 Function *Func = M.getFunction(SplitPair.first);
108 if (Func) {
109 if (Func->isDeclaration())
110 continue;
111 auto SecondSplitPair = SplitPair.second.split('=');
112 if (!SecondSplitPair.second.empty()) {
113 Func->addFnAttr(SecondSplitPair.first, SecondSplitPair.second);
115 } else {
119
120
121 Func->addFnAttr(AttrKind);
123 } else
124 errs() << "Cannot add " << SplitPair.second
125 << " as an attribute name.\n";
126 }
127 } else {
128 errs() << "Function in CSV file at line " << It.line_number()
129 << " does not exist.\n";
130
131 continue;
132 }
133 }
134 }
136 for (Function &F : M.functions())
139 }
140
141
143}
static void forceAttributes(Function &F)
If F has any forced attributes given on the command line, add them.
Definition ForceFunctionAttrs.cpp:51
static bool hasForceAttributes()
Definition ForceFunctionAttrs.cpp:85
static cl::list< std::string > ForceAttributes("force-attribute", cl::Hidden, cl::desc("Add an attribute to a function. This can be a " "pair of 'function-name:attribute-name', to apply an attribute to a " "specific function. For " "example -force-attribute=foo:noinline. Specifying only an attribute " "will apply the attribute to every function in the module. This " "option can be specified multiple times."))
static cl::list< std::string > ForceRemoveAttributes("force-remove-attribute", cl::Hidden, cl::desc("Remove an attribute from a function. This can be a " "pair of 'function-name:attribute-name' to remove an attribute " "from a specific function. For " "example -force-remove-attribute=foo:noinline. Specifying only an " "attribute will remove the attribute from all functions in the " "module. This " "option can be specified multiple times."))
static cl::opt< std::string > CSVFilePath("forceattrs-csv-path", cl::Hidden, cl::desc("Path to CSV file containing lines of function names and attributes to " "add to them in the form of `f1,attr1` or `f2,attr2=str`."))
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.
static LLVM_ABI Attribute::AttrKind getAttrKindFromName(StringRef AttrName)
static LLVM_ABI bool canUseAsFnAttr(AttrKind Kind)
@ None
No attributes have been set.
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.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
StringRef - Represent a constant reference to a string, i.e.
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
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.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
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.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &)
Definition ForceFunctionAttrs.cpp:89