2014-05-22 16:54:05 +08:00
|
|
|
//===---- CGLoopInfo.h - LLVM CodeGen for loop metadata -*- C++ -*---------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-05-22 16:54:05 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This is the internal state used for llvm translation for loop statement
|
|
|
|
// metadata.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-14 00:25:19 +08:00
|
|
|
#ifndef LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H
|
|
|
|
#define LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H
|
2014-05-22 16:54:05 +08:00
|
|
|
|
2015-06-12 07:23:17 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2014-05-22 16:54:05 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
Add a loop's debug location to its llvm.loop metadata
Getting accurate locations for loops is important, because those locations are
used by the frontend to generate optimization remarks. Currently, optimization
remarks for loops often appear on the wrong line, often the first line of the
loop body instead of the loop itself. This is confusing because that line might
itself be another loop, or might be somewhere else completely if the body was
an inlined function call. This happens because of the way we find the loop's
starting location. First, we look for a preheader, and if we find one, and its
terminator has a debug location, then we use that. Otherwise, we look for a
location on an instruction in the loop header.
The fallback heuristic is not bad, but will almost always find the beginning of
the body, and not the loop statement itself. The preheader location search
often fails because there's often not a preheader, and even when there is a
preheader, depending on how it was formed, it sometimes carries the location of
some preceeding code.
I don't see any good theoretical way to fix this problem. On the other hand,
this seems like a straightforward solution: Put the debug location in the
loop's llvm.loop metadata. When emitting debug information, this commit causes
us to add the debug location as an operand to each loop's llvm.loop metadata.
Thus, we now generate this metadata for all loops (not just loops with
optimization hints) when we're otherwise generating debug information.
The remark test case changes depend on the companion LLVM commit r270771.
llvm-svn: 270772
2016-05-26 05:53:24 +08:00
|
|
|
#include "llvm/IR/DebugLoc.h"
|
2014-05-22 16:54:05 +08:00
|
|
|
#include "llvm/IR/Value.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
class BasicBlock;
|
|
|
|
class Instruction;
|
|
|
|
class MDNode;
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
namespace clang {
|
2015-06-12 07:23:17 +08:00
|
|
|
class Attr;
|
2015-07-28 04:10:20 +08:00
|
|
|
class ASTContext;
|
2014-05-22 16:54:05 +08:00
|
|
|
namespace CodeGen {
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Attributes that may be specified on loops.
|
2014-05-22 16:54:05 +08:00
|
|
|
struct LoopAttributes {
|
|
|
|
explicit LoopAttributes(bool IsParallel = false);
|
|
|
|
void clear();
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Generate llvm.loop.parallel metadata for loads and stores.
|
2014-05-22 16:54:05 +08:00
|
|
|
bool IsParallel;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// State of loop vectorization or unrolling.
|
2015-08-11 01:29:39 +08:00
|
|
|
enum LVEnableState { Unspecified, Enable, Disable, Full };
|
2014-05-22 16:54:05 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Value for llvm.loop.vectorize.enable metadata.
|
2015-07-15 07:03:09 +08:00
|
|
|
LVEnableState VectorizeEnable;
|
2014-05-22 16:54:05 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Value for llvm.loop.unroll.* metadata (enable, disable, or full).
|
2015-07-28 04:10:20 +08:00
|
|
|
LVEnableState UnrollEnable;
|
|
|
|
|
2018-08-01 22:36:12 +08:00
|
|
|
/// Value for llvm.loop.unroll_and_jam.* metadata (enable, disable, or full).
|
|
|
|
LVEnableState UnrollAndJamEnable;
|
|
|
|
|
2019-07-25 15:33:13 +08:00
|
|
|
/// Value for llvm.loop.vectorize.predicate metadata
|
|
|
|
LVEnableState VectorizePredicateEnable;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Value for llvm.loop.vectorize.width metadata.
|
2015-07-15 07:03:09 +08:00
|
|
|
unsigned VectorizeWidth;
|
2014-05-22 16:54:05 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Value for llvm.loop.interleave.count metadata.
|
2015-07-15 07:03:09 +08:00
|
|
|
unsigned InterleaveCount;
|
2015-07-28 04:10:20 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// llvm.unroll.
|
2015-07-28 04:10:20 +08:00
|
|
|
unsigned UnrollCount;
|
2016-06-14 20:04:26 +08:00
|
|
|
|
2018-08-01 22:36:12 +08:00
|
|
|
/// llvm.unroll.
|
|
|
|
unsigned UnrollAndJamCount;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Value for llvm.loop.distribute.enable metadata.
|
2016-06-14 20:04:26 +08:00
|
|
|
LVEnableState DistributeEnable;
|
2019-01-05 01:20:00 +08:00
|
|
|
|
|
|
|
/// Value for llvm.loop.pipeline.disable metadata.
|
|
|
|
bool PipelineDisabled;
|
|
|
|
|
|
|
|
/// Value for llvm.loop.pipeline.iicount metadata.
|
|
|
|
unsigned PipelineInitiationInterval;
|
2014-05-22 16:54:05 +08:00
|
|
|
};
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Information used when generating a structured loop.
|
2014-05-22 16:54:05 +08:00
|
|
|
class LoopInfo {
|
|
|
|
public:
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Construct a new LoopInfo for the loop with entry Header.
|
Add a loop's debug location to its llvm.loop metadata
Getting accurate locations for loops is important, because those locations are
used by the frontend to generate optimization remarks. Currently, optimization
remarks for loops often appear on the wrong line, often the first line of the
loop body instead of the loop itself. This is confusing because that line might
itself be another loop, or might be somewhere else completely if the body was
an inlined function call. This happens because of the way we find the loop's
starting location. First, we look for a preheader, and if we find one, and its
terminator has a debug location, then we use that. Otherwise, we look for a
location on an instruction in the loop header.
The fallback heuristic is not bad, but will almost always find the beginning of
the body, and not the loop statement itself. The preheader location search
often fails because there's often not a preheader, and even when there is a
preheader, depending on how it was formed, it sometimes carries the location of
some preceeding code.
I don't see any good theoretical way to fix this problem. On the other hand,
this seems like a straightforward solution: Put the debug location in the
loop's llvm.loop metadata. When emitting debug information, this commit causes
us to add the debug location as an operand to each loop's llvm.loop metadata.
Thus, we now generate this metadata for all loops (not just loops with
optimization hints) when we're otherwise generating debug information.
The remark test case changes depend on the companion LLVM commit r270771.
llvm-svn: 270772
2016-05-26 05:53:24 +08:00
|
|
|
LoopInfo(llvm::BasicBlock *Header, const LoopAttributes &Attrs,
|
2019-04-02 01:47:41 +08:00
|
|
|
const llvm::DebugLoc &StartLoc, const llvm::DebugLoc &EndLoc,
|
|
|
|
LoopInfo *Parent);
|
2014-05-22 16:54:05 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Get the loop id metadata for this loop.
|
2019-04-02 01:47:41 +08:00
|
|
|
llvm::MDNode *getLoopID() const { return TempLoopID.get(); }
|
2014-05-22 16:54:05 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Get the header block of this loop.
|
2014-05-22 16:54:05 +08:00
|
|
|
llvm::BasicBlock *getHeader() const { return Header; }
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Get the set of attributes active for this loop.
|
2014-05-22 16:54:05 +08:00
|
|
|
const LoopAttributes &getAttributes() const { return Attrs; }
|
|
|
|
|
2018-12-21 05:24:54 +08:00
|
|
|
/// Return this loop's access group or nullptr if it does not have one.
|
|
|
|
llvm::MDNode *getAccessGroup() const { return AccGroup; }
|
|
|
|
|
2019-04-02 01:47:41 +08:00
|
|
|
/// Create the loop's metadata. Must be called after its nested loops have
|
|
|
|
/// been processed.
|
|
|
|
void finish();
|
|
|
|
|
2014-05-22 16:54:05 +08:00
|
|
|
private:
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Loop ID metadata.
|
2019-04-02 01:47:41 +08:00
|
|
|
llvm::TempMDTuple TempLoopID;
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Header block of this loop.
|
2014-05-22 16:54:05 +08:00
|
|
|
llvm::BasicBlock *Header;
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The attributes for this loop.
|
2014-05-22 16:54:05 +08:00
|
|
|
LoopAttributes Attrs;
|
2018-12-21 05:24:54 +08:00
|
|
|
/// The access group for memory accesses parallel to this loop.
|
|
|
|
llvm::MDNode *AccGroup = nullptr;
|
2019-04-02 01:47:41 +08:00
|
|
|
/// Start location of this loop.
|
|
|
|
llvm::DebugLoc StartLoc;
|
|
|
|
/// End location of this loop.
|
|
|
|
llvm::DebugLoc EndLoc;
|
|
|
|
/// The next outer loop, or nullptr if this is the outermost loop.
|
|
|
|
LoopInfo *Parent;
|
|
|
|
/// If this loop has unroll-and-jam metadata, this can be set by the inner
|
|
|
|
/// loop's LoopInfo to set the llvm.loop.unroll_and_jam.followup_inner
|
|
|
|
/// metadata.
|
|
|
|
llvm::MDNode *UnrollAndJamInnerFollowup = nullptr;
|
|
|
|
|
|
|
|
/// Create a LoopID without any transformations.
|
|
|
|
llvm::MDNode *
|
|
|
|
createLoopPropertiesMetadata(llvm::ArrayRef<llvm::Metadata *> LoopProperties);
|
|
|
|
|
|
|
|
/// Create a LoopID for transformations.
|
|
|
|
///
|
|
|
|
/// The methods call each other in case multiple transformations are applied
|
|
|
|
/// to a loop. The transformation first to be applied will use LoopID of the
|
|
|
|
/// next transformation in its followup attribute.
|
|
|
|
///
|
|
|
|
/// @param Attrs The loop's transformations.
|
|
|
|
/// @param LoopProperties Non-transformation properties such as debug
|
|
|
|
/// location, parallel accesses and disabled
|
|
|
|
/// transformations. These are added to the returned
|
|
|
|
/// LoopID.
|
|
|
|
/// @param HasUserTransforms [out] Set to true if the returned MDNode encodes
|
|
|
|
/// at least one transformation.
|
|
|
|
///
|
|
|
|
/// @return A LoopID (metadata node) that can be used for the llvm.loop
|
|
|
|
/// annotation or followup-attribute.
|
|
|
|
/// @{
|
|
|
|
llvm::MDNode *
|
|
|
|
createPipeliningMetadata(const LoopAttributes &Attrs,
|
|
|
|
llvm::ArrayRef<llvm::Metadata *> LoopProperties,
|
|
|
|
bool &HasUserTransforms);
|
|
|
|
llvm::MDNode *
|
|
|
|
createPartialUnrollMetadata(const LoopAttributes &Attrs,
|
|
|
|
llvm::ArrayRef<llvm::Metadata *> LoopProperties,
|
|
|
|
bool &HasUserTransforms);
|
|
|
|
llvm::MDNode *
|
|
|
|
createUnrollAndJamMetadata(const LoopAttributes &Attrs,
|
|
|
|
llvm::ArrayRef<llvm::Metadata *> LoopProperties,
|
|
|
|
bool &HasUserTransforms);
|
|
|
|
llvm::MDNode *
|
|
|
|
createLoopVectorizeMetadata(const LoopAttributes &Attrs,
|
|
|
|
llvm::ArrayRef<llvm::Metadata *> LoopProperties,
|
|
|
|
bool &HasUserTransforms);
|
|
|
|
llvm::MDNode *
|
|
|
|
createLoopDistributeMetadata(const LoopAttributes &Attrs,
|
|
|
|
llvm::ArrayRef<llvm::Metadata *> LoopProperties,
|
|
|
|
bool &HasUserTransforms);
|
|
|
|
llvm::MDNode *
|
|
|
|
createFullUnrollMetadata(const LoopAttributes &Attrs,
|
|
|
|
llvm::ArrayRef<llvm::Metadata *> LoopProperties,
|
|
|
|
bool &HasUserTransforms);
|
|
|
|
/// @}
|
|
|
|
|
|
|
|
/// Create a LoopID for this loop, including transformation-unspecific
|
|
|
|
/// metadata such as debug location.
|
|
|
|
///
|
|
|
|
/// @param Attrs This loop's attributes and transformations.
|
|
|
|
/// @param LoopProperties Additional non-transformation properties to add
|
|
|
|
/// to the LoopID, such as transformation-specific
|
|
|
|
/// metadata that are not covered by @p Attrs.
|
|
|
|
/// @param HasUserTransforms [out] Set to true if the returned MDNode encodes
|
|
|
|
/// at least one transformation.
|
|
|
|
///
|
|
|
|
/// @return A LoopID (metadata node) that can be used for the llvm.loop
|
|
|
|
/// annotation.
|
|
|
|
llvm::MDNode *createMetadata(const LoopAttributes &Attrs,
|
|
|
|
llvm::ArrayRef<llvm::Metadata *> LoopProperties,
|
|
|
|
bool &HasUserTransforms);
|
2014-05-22 16:54:05 +08:00
|
|
|
};
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// A stack of loop information corresponding to loop nesting levels.
|
2014-05-22 16:54:05 +08:00
|
|
|
/// This stack can be used to prepare attributes which are applied when a loop
|
|
|
|
/// is emitted.
|
|
|
|
class LoopInfoStack {
|
2015-02-16 06:54:08 +08:00
|
|
|
LoopInfoStack(const LoopInfoStack &) = delete;
|
|
|
|
void operator=(const LoopInfoStack &) = delete;
|
2014-05-22 16:54:05 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
LoopInfoStack() {}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Begin a new structured loop. The set of staged attributes will be
|
2014-05-22 16:54:05 +08:00
|
|
|
/// applied to the loop and then cleared.
|
2016-11-25 00:01:20 +08:00
|
|
|
void push(llvm::BasicBlock *Header, const llvm::DebugLoc &StartLoc,
|
|
|
|
const llvm::DebugLoc &EndLoc);
|
2015-07-28 04:10:20 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Begin a new structured loop. Stage attributes from the Attrs list.
|
2015-07-28 04:10:20 +08:00
|
|
|
/// The staged attributes are applied to the loop and then cleared.
|
|
|
|
void push(llvm::BasicBlock *Header, clang::ASTContext &Ctx,
|
2016-11-25 00:01:20 +08:00
|
|
|
llvm::ArrayRef<const Attr *> Attrs, const llvm::DebugLoc &StartLoc,
|
|
|
|
const llvm::DebugLoc &EndLoc);
|
2014-05-22 16:54:05 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// End the current loop.
|
2014-05-22 16:54:05 +08:00
|
|
|
void pop();
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Return the top loop id metadata.
|
2014-05-22 16:54:05 +08:00
|
|
|
llvm::MDNode *getCurLoopID() const { return getInfo().getLoopID(); }
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Return true if the top loop is parallel.
|
2014-05-22 16:54:05 +08:00
|
|
|
bool getCurLoopParallel() const {
|
|
|
|
return hasInfo() ? getInfo().getAttributes().IsParallel : false;
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Function called by the CodeGenFunction when an instruction is
|
2014-05-22 16:54:05 +08:00
|
|
|
/// created.
|
|
|
|
void InsertHelper(llvm::Instruction *I) const;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Set the next pushed loop as parallel.
|
2014-05-22 16:54:05 +08:00
|
|
|
void setParallel(bool Enable = true) { StagedAttrs.IsParallel = Enable; }
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Set the next pushed loop 'vectorize.enable'
|
2015-07-15 07:03:09 +08:00
|
|
|
void setVectorizeEnable(bool Enable = true) {
|
|
|
|
StagedAttrs.VectorizeEnable =
|
|
|
|
Enable ? LoopAttributes::Enable : LoopAttributes::Disable;
|
2014-05-22 16:54:05 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Set the next pushed loop as a distribution candidate.
|
2016-06-14 20:04:26 +08:00
|
|
|
void setDistributeState(bool Enable = true) {
|
|
|
|
StagedAttrs.DistributeEnable =
|
|
|
|
Enable ? LoopAttributes::Enable : LoopAttributes::Disable;
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Set the next pushed loop unroll state.
|
2015-08-11 01:29:39 +08:00
|
|
|
void setUnrollState(const LoopAttributes::LVEnableState &State) {
|
|
|
|
StagedAttrs.UnrollEnable = State;
|
2015-07-28 04:10:20 +08:00
|
|
|
}
|
|
|
|
|
2019-07-25 15:33:13 +08:00
|
|
|
/// Set the next pushed vectorize predicate state.
|
|
|
|
void setVectorizePredicateState(const LoopAttributes::LVEnableState &State) {
|
|
|
|
StagedAttrs.VectorizePredicateEnable = State;
|
|
|
|
}
|
|
|
|
|
2018-08-01 22:36:12 +08:00
|
|
|
/// Set the next pushed loop unroll_and_jam state.
|
|
|
|
void setUnrollAndJamState(const LoopAttributes::LVEnableState &State) {
|
|
|
|
StagedAttrs.UnrollAndJamEnable = State;
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Set the vectorize width for the next loop pushed.
|
2015-07-15 07:03:09 +08:00
|
|
|
void setVectorizeWidth(unsigned W) { StagedAttrs.VectorizeWidth = W; }
|
2014-05-22 16:54:05 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Set the interleave count for the next loop pushed.
|
2015-07-15 07:03:09 +08:00
|
|
|
void setInterleaveCount(unsigned C) { StagedAttrs.InterleaveCount = C; }
|
2014-05-22 16:54:05 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Set the unroll count for the next loop pushed.
|
2015-07-28 04:10:20 +08:00
|
|
|
void setUnrollCount(unsigned C) { StagedAttrs.UnrollCount = C; }
|
|
|
|
|
2018-08-01 22:36:12 +08:00
|
|
|
/// \brief Set the unroll count for the next loop pushed.
|
|
|
|
void setUnrollAndJamCount(unsigned C) { StagedAttrs.UnrollAndJamCount = C; }
|
|
|
|
|
2019-01-05 01:20:00 +08:00
|
|
|
/// Set the pipeline disabled state.
|
|
|
|
void setPipelineDisabled(bool S) { StagedAttrs.PipelineDisabled = S; }
|
|
|
|
|
|
|
|
/// Set the pipeline initiation interval.
|
|
|
|
void setPipelineInitiationInterval(unsigned C) {
|
|
|
|
StagedAttrs.PipelineInitiationInterval = C;
|
|
|
|
}
|
|
|
|
|
2014-05-22 16:54:05 +08:00
|
|
|
private:
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns true if there is LoopInfo on the stack.
|
2014-05-22 16:54:05 +08:00
|
|
|
bool hasInfo() const { return !Active.empty(); }
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Return the LoopInfo for the current loop. HasInfo should be called
|
2014-05-22 16:54:05 +08:00
|
|
|
/// first to ensure LoopInfo is present.
|
2019-08-19 21:37:41 +08:00
|
|
|
const LoopInfo &getInfo() const { return *Active.back(); }
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The set of attributes that will be applied to the next pushed loop.
|
2014-05-22 16:54:05 +08:00
|
|
|
LoopAttributes StagedAttrs;
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Stack of active loops.
|
2019-08-19 21:37:41 +08:00
|
|
|
llvm::SmallVector<std::unique_ptr<LoopInfo>, 4> Active;
|
2014-05-22 16:54:05 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace CodeGen
|
|
|
|
} // end namespace clang
|
|
|
|
|
2014-08-14 00:25:19 +08:00
|
|
|
#endif
|