Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:48:11

0001 //===- Codegen/IRBuilder.h - The IR builder used by Polly -*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // The Polly IRBuilder file contains Polly specific extensions for the IRBuilder
0010 // that are used e.g. to emit the llvm.loop.parallel metadata.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef POLLY_CODEGEN_IRBUILDER_H
0015 #define POLLY_CODEGEN_IRBUILDER_H
0016 
0017 #include "llvm/ADT/MapVector.h"
0018 #include "llvm/IR/IRBuilder.h"
0019 
0020 namespace llvm {
0021 class Loop;
0022 class SCEV;
0023 class ScalarEvolution;
0024 } // namespace llvm
0025 
0026 namespace polly {
0027 class Scop;
0028 struct BandAttr;
0029 
0030 /// Helper class to annotate newly generated SCoPs with metadata.
0031 ///
0032 /// The annotations are twofold:
0033 ///   1) Loops are stored in a stack-like structure in the order they are
0034 ///      constructed and the LoopID metadata node is added to the backedge.
0035 ///      Contained memory instructions and loop headers are annotated according
0036 ///      to all parallel surrounding loops.
0037 ///   2) The new SCoP is assumed alias free (either due to the result of
0038 ///      AliasAnalysis queries or runtime alias checks). We annotate therefore
0039 ///      all memory instruction with alias scopes to indicate that fact to
0040 ///      later optimizations.
0041 ///      These alias scopes live in a new alias domain only used in this SCoP.
0042 ///      Each base pointer has its own alias scope and is annotated to not
0043 ///      alias with any access to different base pointers.
0044 class ScopAnnotator {
0045 public:
0046   ScopAnnotator();
0047   ~ScopAnnotator();
0048 
0049   /// Build all alias scopes for the given SCoP.
0050   void buildAliasScopes(Scop &S);
0051 
0052   /// Add a new loop @p L which is parallel if @p IsParallel is true.
0053   void pushLoop(llvm::Loop *L, bool IsParallel);
0054 
0055   /// Remove the last added loop.
0056   void popLoop(bool isParallel);
0057 
0058   /// Annotate the new instruction @p I for all parallel loops.
0059   void annotate(llvm::Instruction *I);
0060 
0061   /// Annotate the loop latch @p B.
0062   /// Last argument is optional, if no value is passed, we don't annotate
0063   /// any vectorize metadata.
0064   void annotateLoopLatch(
0065       llvm::BranchInst *B, bool IsParallel,
0066       std::optional<bool> EnableVectorizeMetadata = std::nullopt) const;
0067 
0068   /// Add alternative alias based pointers
0069   ///
0070   /// When annotating instructions with alias scope metadata, the right metadata
0071   /// is identified through the base pointer of the memory access. In some cases
0072   /// (e.g. OpenMP code generation), the base pointer of the memory accesses is
0073   /// not the original base pointer, but was changed when passing the original
0074   /// base pointer over a function boundary. This function allows to provide a
0075   /// map that maps from these new base pointers to the original base pointers
0076   /// to allow the ScopAnnotator to still find the right alias scop annotations.
0077   ///
0078   /// @param NewMap A map from new base pointers to original base pointers.
0079   void addAlternativeAliasBases(
0080       llvm::DenseMap<llvm::AssertingVH<llvm::Value>,
0081                      llvm::AssertingVH<llvm::Value>> &NewMap) {
0082     AlternativeAliasBases.insert(NewMap.begin(), NewMap.end());
0083   }
0084 
0085   /// Delete the set of alternative alias bases
0086   void resetAlternativeAliasBases() { AlternativeAliasBases.clear(); }
0087 
0088   /// Stack for surrounding BandAttr annotations.
0089   llvm::SmallVector<BandAttr *, 8> LoopAttrEnv;
0090   BandAttr *&getStagingAttrEnv() { return LoopAttrEnv.back(); }
0091   BandAttr *getActiveAttrEnv() const {
0092     return LoopAttrEnv[LoopAttrEnv.size() - 2];
0093   }
0094 
0095 private:
0096   /// The ScalarEvolution analysis we use to find base pointers.
0097   llvm::ScalarEvolution *SE;
0098 
0099   /// All loops currently under construction.
0100   llvm::SmallVector<llvm::Loop *, 8> ActiveLoops;
0101 
0102   /// Access groups for the parallel loops currently under construction.
0103   llvm::SmallVector<llvm::MDNode *, 8> ParallelLoops;
0104 
0105   /// The alias scope domain for the current SCoP.
0106   llvm::MDNode *AliasScopeDomain;
0107 
0108   /// A map from base pointers to its alias scope.
0109   llvm::MapVector<llvm::AssertingVH<llvm::Value>, llvm::MDNode *> AliasScopeMap;
0110 
0111   /// A map from base pointers to an alias scope list of other pointers.
0112   llvm::DenseMap<llvm::AssertingVH<llvm::Value>, llvm::MDNode *>
0113       OtherAliasScopeListMap;
0114 
0115   llvm::DenseMap<llvm::AssertingVH<llvm::Value>, llvm::AssertingVH<llvm::Value>>
0116       AlternativeAliasBases;
0117 };
0118 
0119 /// Add Polly specifics when running IRBuilder.
0120 ///
0121 /// This is used to add additional items such as e.g. the llvm.loop.parallel
0122 /// metadata.
0123 class IRInserter final : public llvm::IRBuilderDefaultInserter {
0124 public:
0125   IRInserter() = default;
0126   IRInserter(ScopAnnotator &A) : Annotator(&A) {}
0127 
0128   void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name,
0129                     llvm::BasicBlock::iterator InsertPt) const override {
0130     llvm::IRBuilderDefaultInserter::InsertHelper(I, Name, InsertPt);
0131     if (Annotator)
0132       Annotator->annotate(I);
0133   }
0134 
0135 private:
0136   ScopAnnotator *Annotator = nullptr;
0137 };
0138 
0139 // TODO: We should not name instructions in NDEBUG builds.
0140 //
0141 // We currently always name instructions, as the polly test suite currently
0142 // matches for certain names.
0143 typedef llvm::IRBuilder<llvm::ConstantFolder, IRInserter> PollyIRBuilder;
0144 
0145 } // namespace polly
0146 #endif