diff --git a/llvm/lib/Transforms/IPO/ForceFunctionAttrs.cpp b/llvm/lib/Transforms/IPO/ForceFunctionAttrs.cpp --- a/llvm/lib/Transforms/IPO/ForceFunctionAttrs.cpp +++ b/llvm/lib/Transforms/IPO/ForceFunctionAttrs.cpp @@ -11,6 +11,8 @@ #include "llvm/IR/Module.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" +#include "llvm/Support/LineIterator.h" +#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/raw_ostream.h" using namespace llvm; @@ -30,6 +32,11 @@ "example -force-remove-attribute=foo:noinline. This " "option can be specified multiple times.")); +static cl::opt CSVFilePath( + "func-annotator-csv-file-path", cl::Hidden, + cl::desc("CSV file containing function names and optimization level as " + "attribute")); + /// If F has any forced attributes given on the command line, add them. /// If F has any forced remove attributes given on the command line, remove /// them. When both force and force-remove are given to a function, the latter @@ -68,12 +75,51 @@ } PreservedAnalyses ForceFunctionAttrsPass::run(Module &M, - ModuleAnalysisManager &) { - if (!hasForceAttributes()) - return PreservedAnalyses::all(); + ModuleAnalysisManager &AM) { + + if (!CSVFilePath.empty()) { + + auto BufferOrError = MemoryBuffer::getFileOrSTDIN(CSVFilePath); + if (!BufferOrError) { + report_fatal_error("Cannot open CSV file"); + } - for (Function &F : M.functions()) - forceAttributes(F); + StringRef Buffer = BufferOrError.get()->getBuffer(); + auto MemoryBuffer = MemoryBuffer::getMemBuffer(Buffer); + line_iterator It(*MemoryBuffer); + + for (++It; !It.is_at_end(); ++It) { + auto SplitPair = It->split(','); + if (!SplitPair.second.empty()) { + Function *Func = M.getFunction(SplitPair.first); + if (Func) { + if (Func->isDeclaration()) { + continue; + } + auto SecondSplitPair = SplitPair.second.split('='); + if (!SecondSplitPair.second.empty()) { + Func->addFnAttr(SecondSplitPair.first, SecondSplitPair.second); + } else { + auto AttrKind = Attribute::getAttrKindFromName(SplitPair.second); + if (AttrKind != Attribute::None && + Attribute::canUseAsFnAttr(AttrKind)) { + Func->addFnAttr(AttrKind); + } + } + } else { + errs() << "Function in CSV file at line " << It.line_number() + << " does not exist\n"; + continue; + } + } + } + } else { + if (!hasForceAttributes()) + return PreservedAnalyses::all(); + + for (Function &F : M.functions()) + forceAttributes(F); + } // Just conservatively invalidate analyses, this isn't likely to be important. return PreservedAnalyses::none(); diff --git a/llvm/test/Transforms/ForcedFunctionAttrs/FunctionAnnotation.csv b/llvm/test/Transforms/ForcedFunctionAttrs/FunctionAnnotation.csv new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/ForcedFunctionAttrs/FunctionAnnotation.csv @@ -0,0 +1,6 @@ +FunctionName,OptimizationLevel +first_function,opt-level=O1 +second_function,cold +third_function,opt-level=O1 +fourth_function,opt-level=O2 +fifth_function,foo=bar diff --git a/llvm/test/Transforms/ForcedFunctionAttrs/FunctionAnnotator.ll b/llvm/test/Transforms/ForcedFunctionAttrs/FunctionAnnotator.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/ForcedFunctionAttrs/FunctionAnnotator.ll @@ -0,0 +1,33 @@ +; RUN: opt -passes='forceattrs' -func-annotator-csv-file-path="%S/FunctionAnnotation.csv" -S < %s | FileCheck %s +define void @first_function() { +; CHECK: @first_function() #0 + ret void +} + +define void @second_function() { +; CHECK: @second_function() #1 + ret void +} + +define void @third_function() { +; CHECK: @third_function() #0 + ret void +} + +define void @fourth_function() { +; CHECK: @fourth_function() #2 + ret void +} + +define void @fifth_function() { +; CHECK: @fifth_function() #3 + ret void +} + +; CHECK-LABEL: attributes #0 = { "opt-level"="O1" } + +; CHECK-LABEL: attributes #1 = { cold } + +; CHECK-LABEL: attributes #2 = { "opt-level"="O2" } + +; CHECK-LABEL: attributes #3 = { "foo"="bar" }