llvm.org GIT mirror llvm / 7724e8e
[PM] Port TTI to the new pass manager, introducing a TargetIRAnalysis to produce it. This adds a function to the TargetMachine that produces this analysis via a callback for each function. This in turn faves the way to produce a *different* TTI per-function with the correct subtarget cached. I've also done the necessary wiring in the opt tool to thread the target machine down and make it available to the pass registry so that we can construct this analysis from a target machine when available. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@227721 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 4 years ago
11 changed file(s) with 150 addition(s) and 14 deletion(s). Raw diff Collapse all Expand all
3131 class Function;
3232 class GlobalValue;
3333 class Loop;
34 class PreservedAnalyses;
3435 class Type;
3536 class User;
3637 class Value;
7475 // We need to define the destructor out-of-line to define our sub-classes
7576 // out-of-line.
7677 ~TargetTransformInfo();
78
79 /// \brief Handle the invalidation of this information.
80 ///
81 /// When used as a result of \c TargetIRAnalysis this method will be called
82 /// when the function this was computed for changes. When it returns false,
83 /// the information is preserved across those changes.
84 bool invalidate(Function &, const PreservedAnalyses &) {
85 // FIXME: We should probably in some way ensure that the subtarget
86 // information for a function hasn't changed.
87 return false;
88 }
7789
7890 /// \name Generic Target Information
7991 /// @{
705717 TargetTransformInfo::TargetTransformInfo(T Impl)
706718 : TTIImpl(new Model(Impl)) {}
707719
720 /// \brief Analysis pass providing the \c TargetTransformInfo.
721 ///
722 /// The core idea of the TargetIRAnalysis is to expose an interface through
723 /// which LLVM targets can analyze and provide information about the middle
724 /// end's target-independent IR. This supports use cases such as target-aware
725 /// cost modeling of IR constructs.
726 ///
727 /// This is a function analysis because much of the cost modeling for targets
728 /// is done in a subtarget specific way and LLVM supports compiling different
729 /// functions targeting different subtargets in order to support runtime
730 /// dispatch according to the observed subtarget.
731 class TargetIRAnalysis {
732 public:
733 typedef TargetTransformInfo Result;
734
735 /// \brief Opaque, unique identifier for this analysis pass.
736 static void *ID() { return (void *)&PassID; }
737
738 /// \brief Provide access to a name for this pass for debugging purposes.
739 static StringRef name() { return "TargetIRAnalysis"; }
740
741 /// \brief Default construct a target IR analysis.
742 ///
743 /// This will use the module's datalayout to construct a baseline
744 /// conservative TTI result.
745 TargetIRAnalysis();
746
747 /// \brief Construct an IR analysis pass around a target-provide callback.
748 ///
749 /// The callback will be called with a particular function for which the TTI
750 /// is needed and must return a TTI object for that function.
751 TargetIRAnalysis(std::function TTICallback);
752
753 // Value semantics. We spell out the constructors for MSVC.
754 TargetIRAnalysis(const TargetIRAnalysis &Arg)
755 : TTICallback(Arg.TTICallback) {}
756 TargetIRAnalysis(TargetIRAnalysis &&Arg)
757 : TTICallback(std::move(Arg.TTICallback)) {}
758 TargetIRAnalysis &operator=(const TargetIRAnalysis &RHS) {
759 TTICallback = RHS.TTICallback;
760 return *this;
761 }
762 TargetIRAnalysis &operator=(TargetIRAnalysis &&RHS) {
763 TTICallback = std::move(RHS.TTICallback);
764 return *this;
765 }
766
767 Result run(Function &F);
768
769 private:
770 static char PassID;
771
772 /// \brief The callback used to produce a result.
773 ///
774 /// We use a completely opaque callback so that targets can provide whatever
775 /// mechanism they desire for constructing the TTI for a given function.
776 ///
777 /// FIXME: Should we really use std::function? It's relatively inefficient.
778 /// It might be possible to arrange for even stateful callbacks to outlive
779 /// the analysis and thus use a function_ref which would be lighter weight.
780 /// This may also be less error prone as the callback is likely to reference
781 /// the external TargetMachine, and that reference needs to never dangle.
782 std::function TTICallback;
783
784 /// \brief Helper function used as the callback in the default constructor.
785 static Result getDefaultTTI(Function &F);
786 };
787
708788 /// \brief Wrapper pass for TargetTransformInfo.
709789 ///
710790 /// This pass can be constructed from a TTI object which it stores internally
3333 class DataLayout;
3434 class TargetLibraryInfo;
3535 class TargetFrameLowering;
36 class TargetIRAnalysis;
3637 class TargetIntrinsicInfo;
3738 class TargetLowering;
3839 class TargetPassConfig;
186187 /// sections.
187188 void setFunctionSections(bool);
188189
190 /// \brief Get a \c TargetIRAnalysis appropriate for the target.
191 ///
192 /// This is used to construct the new pass manager's target IR analysis pass,
193 /// set up appropriately for this target machine.
194 virtual TargetIRAnalysis getTargetIRAnalysis();
195
189196 /// \brief Get a TTI implementation for the target.
190197 ///
191198 /// Targets should override this method to provide target-accurate
1313 #include "llvm/IR/Instruction.h"
1414 #include "llvm/IR/Instructions.h"
1515 #include "llvm/IR/IntrinsicInst.h"
16 #include "llvm/IR/Module.h"
1617 #include "llvm/IR/Operator.h"
1718 #include "llvm/Support/ErrorHandling.h"
1819
254255
255256 TargetTransformInfo::Concept::~Concept() {}
256257
258 TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}
259
260 TargetIRAnalysis::TargetIRAnalysis(
261 std::function TTICallback)
262 : TTICallback(TTICallback) {}
263
264 TargetIRAnalysis::Result TargetIRAnalysis::run(Function &F) {
265 return TTICallback(F);
266 }
267
268 char TargetIRAnalysis::PassID;
269
270 TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(Function &F) {
271 return Result(F.getParent()->getDataLayout());
272 }
273
257274 // Register the basic pass.
258275 INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti",
259276 "Target Transform Information", false, true)
171171 Options.DataSections = V;
172172 }
173173
174 TargetIRAnalysis TargetMachine::getTargetIRAnalysis() {
175 // While targets are free to just override getTTI and rely on this analysis,
176 // it would be more efficient to override and provide an analysis that could
177 // directly construct that target's TTI without the virtual call.
178 return TargetIRAnalysis([this](Function &) { return getTTI(); });
179 }
180
174181 TargetTransformInfo TargetMachine::getTTI() {
175182 return TargetTransformInfo(getDataLayout());
176183 }
277277 ; CHECK-TLI-NOT: Running analysis: TargetLibraryAnalysis
278278 ; CHECK-TLI: Finished pass manager
279279
280 ; RUN: opt -disable-output -disable-verify -debug-pass-manager %s 2>&1 \
281 ; RUN: -passes='require,invalidate,require' \
282 ; RUN: | FileCheck %s --check-prefix=CHECK-TIRA
283 ; CHECK-TIRA: Starting pass manager
284 ; CHECK-TIRA: Running pass: RequireAnalysisPass
285 ; CHECK-TIRA: Running analysis: TargetIRAnalysis
286 ; CHECK-TIRA: Running pass: InvalidateAllAnalysesPass
287 ; CHECK-TIRA-NOT: Invalidating analysis: TargetIRAnalysis
288 ; CHECK-TIRA: Running pass: RequireAnalysisPass
289 ; CHECK-TIRA-NOT: Running analysis: TargetIRAnalysis
290 ; CHECK-TIRA: Finished pass manager
291
280292 define void @foo() {
281293 ret void
282294 }
2626 #include "llvm/Support/CommandLine.h"
2727 #include "llvm/Support/ErrorHandling.h"
2828 #include "llvm/Support/ToolOutputFile.h"
29 #include "llvm/Target/TargetMachine.h"
2930
3031 using namespace llvm;
3132 using namespace opt_tool;
3536 cl::desc("Print pass management debugging information"));
3637
3738 bool llvm::runPassPipeline(StringRef Arg0, LLVMContext &Context, Module &M,
38 tool_output_file *Out, StringRef PassPipeline,
39 OutputKind OK, VerifierKind VK) {
40 Passes P;
39 TargetMachine *TM, tool_output_file *Out,
40 StringRef PassPipeline, OutputKind OK,
41 VerifierKind VK) {
42 Passes P(TM);
4143
4244 FunctionAnalysisManager FAM(DebugPM);
4345 CGSCCAnalysisManager CGAM(DebugPM);
2525 namespace llvm {
2626 class LLVMContext;
2727 class Module;
28 class TargetMachine;
2829 class tool_output_file;
2930
3031 namespace opt_tool {
4748 /// file. It's interface is consequentially somewhat ad-hoc, but will go away
4849 /// when the transition finishes.
4950 bool runPassPipeline(StringRef Arg0, LLVMContext &Context, Module &M,
50 tool_output_file *Out, StringRef PassPipeline,
51 opt_tool::OutputKind OK, opt_tool::VerifierKind VK);
51 TargetMachine *TM, tool_output_file *Out,
52 StringRef PassPipeline, opt_tool::OutputKind OK,
53 opt_tool::VerifierKind VK);
5254 }
5355
5456 #endif
5454 FUNCTION_ANALYSIS("loops", LoopAnalysis())
5555 FUNCTION_ANALYSIS("no-op-function", NoOpFunctionAnalysis())
5656 FUNCTION_ANALYSIS("targetlibinfo", TargetLibraryAnalysis())
57 FUNCTION_ANALYSIS("targetir",
58 TM ? TM->getTargetIRAnalysis() : TargetIRAnalysis())
5759 #undef FUNCTION_ANALYSIS
5860
5961 #ifndef FUNCTION_PASS
1919 #include "llvm/Analysis/LazyCallGraph.h"
2020 #include "llvm/Analysis/LoopInfo.h"
2121 #include "llvm/Analysis/TargetLibraryInfo.h"
22 #include "llvm/Analysis/TargetTransformInfo.h"
2223 #include "llvm/IR/Dominators.h"
2324 #include "llvm/IR/IRPrintingPasses.h"
2425 #include "llvm/IR/PassManager.h"
2526 #include "llvm/IR/Verifier.h"
2627 #include "llvm/Support/Debug.h"
28 #include "llvm/Target/TargetMachine.h"
2729 #include "llvm/Transforms/InstCombine/InstCombine.h"
2830 #include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
2931
2020 #include "llvm/IR/PassManager.h"
2121
2222 namespace llvm {
23 class TargetMachine;
2324
2425 /// \brief This class provides access to all of LLVM's passes.
2526 ///
2829 /// of the built-in passes, and those may reference these members during
2930 /// construction.
3031 class Passes {
32 TargetMachine *TM;
33
3134 public:
35 explicit Passes(TargetMachine *TM = nullptr) : TM(TM) {}
36
3237 /// \brief Registers all available module analysis passes.
3338 ///
3439 /// This is an interface that can be used to populate a \c
369369 }
370370 }
371371
372 Triple ModuleTriple(M->getTargetTriple());
373 TargetMachine *Machine = nullptr;
374 if (ModuleTriple.getArch())
375 Machine = GetTargetMachine(ModuleTriple);
376 std::unique_ptr TM(Machine);
377
372378 // If the output is set to be emitted to standard out, and standard out is a
373379 // console, print out a warning message and refuse to do it. We don't
374380 // impress anyone by spewing tons of binary goo to a terminal.
390396 // The user has asked to use the new pass manager and provided a pipeline
391397 // string. Hand off the rest of the functionality to the new code for that
392398 // layer.
393 return runPassPipeline(argv[0], Context, *M, Out.get(), PassPipeline,
394 OK, VK)
399 return runPassPipeline(argv[0], Context, *M, TM.get(), Out.get(),
400 PassPipeline, OK, VK)
395401 ? 0
396402 : 1;
397403 }
402408 PassManager Passes;
403409
404410 // Add an appropriate TargetLibraryInfo pass for the module's triple.
405 TargetLibraryInfoImpl TLII(Triple(M->getTargetTriple()));
411 TargetLibraryInfoImpl TLII(ModuleTriple);
406412
407413 // The -disable-simplify-libcalls flag actually disables all builtin optzns.
408414 if (DisableSimplifyLibCalls)
418424
419425 if (DL)
420426 Passes.add(new DataLayoutPass());
421
422 Triple ModuleTriple(M->getTargetTriple());
423 TargetMachine *Machine = nullptr;
424 if (ModuleTriple.getArch())
425 Machine = GetTargetMachine(Triple(ModuleTriple));
426 std::unique_ptr TM(Machine);
427427
428428 // Add internal analysis passes from the target machine.
429429 Passes.add(createTargetTransformInfoWrapperPass(