2014-05-22 16:54:05 +08:00
|
|
|
//===---- CGLoopInfo.cpp - 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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CGLoopInfo.h"
|
2015-07-28 04:10:20 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2015-06-12 07:23:17 +08:00
|
|
|
#include "clang/AST/Attr.h"
|
2014-05-22 16:54:05 +08:00
|
|
|
#include "llvm/IR/BasicBlock.h"
|
2018-10-18 16:16:20 +08:00
|
|
|
#include "llvm/IR/CFG.h"
|
2014-05-22 16:54:05 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/InstrTypes.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/Metadata.h"
|
2015-06-09 07:27:35 +08:00
|
|
|
using namespace clang::CodeGen;
|
2014-05-22 16:54:05 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
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
|
|
|
static MDNode *createMetadata(LLVMContext &Ctx, const LoopAttributes &Attrs,
|
2016-11-25 00:01:20 +08:00
|
|
|
const llvm::DebugLoc &StartLoc,
|
2018-12-21 05:24:54 +08:00
|
|
|
const llvm::DebugLoc &EndLoc, MDNode *&AccGroup) {
|
2014-05-22 16:54:05 +08:00
|
|
|
|
2015-07-15 07:03:09 +08:00
|
|
|
if (!Attrs.IsParallel && Attrs.VectorizeWidth == 0 &&
|
2015-07-28 04:10:20 +08:00
|
|
|
Attrs.InterleaveCount == 0 && Attrs.UnrollCount == 0 &&
|
2019-01-05 01:20:00 +08:00
|
|
|
Attrs.UnrollAndJamCount == 0 && !Attrs.PipelineDisabled &&
|
|
|
|
Attrs.PipelineInitiationInterval == 0 &&
|
2015-07-28 04:10:20 +08:00
|
|
|
Attrs.VectorizeEnable == LoopAttributes::Unspecified &&
|
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
|
|
|
Attrs.UnrollEnable == LoopAttributes::Unspecified &&
|
2018-08-01 22:36:12 +08:00
|
|
|
Attrs.UnrollAndJamEnable == LoopAttributes::Unspecified &&
|
|
|
|
Attrs.DistributeEnable == LoopAttributes::Unspecified && !StartLoc &&
|
|
|
|
!EndLoc)
|
2014-05-22 16:54:05 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2014-12-10 02:39:32 +08:00
|
|
|
SmallVector<Metadata *, 4> Args;
|
2014-05-22 16:54:05 +08:00
|
|
|
// Reserve operand 0 for loop id self reference.
|
2015-01-20 05:30:48 +08:00
|
|
|
auto TempNode = MDNode::getTemporary(Ctx, None);
|
|
|
|
Args.push_back(TempNode.get());
|
2014-05-22 16:54:05 +08:00
|
|
|
|
2016-11-10 22:44:30 +08:00
|
|
|
// If we have a valid start debug location for the loop, add it.
|
|
|
|
if (StartLoc) {
|
|
|
|
Args.push_back(StartLoc.getAsMDNode());
|
|
|
|
|
|
|
|
// If we also have a valid end debug location for the loop, add it.
|
|
|
|
if (EndLoc)
|
|
|
|
Args.push_back(EndLoc.getAsMDNode());
|
|
|
|
}
|
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
|
|
|
|
2015-07-15 07:03:09 +08:00
|
|
|
// Setting vectorize.width
|
|
|
|
if (Attrs.VectorizeWidth > 0) {
|
2014-12-10 02:39:32 +08:00
|
|
|
Metadata *Vals[] = {MDString::get(Ctx, "llvm.loop.vectorize.width"),
|
|
|
|
ConstantAsMetadata::get(ConstantInt::get(
|
2015-07-15 07:03:09 +08:00
|
|
|
Type::getInt32Ty(Ctx), Attrs.VectorizeWidth))};
|
2014-05-22 16:54:05 +08:00
|
|
|
Args.push_back(MDNode::get(Ctx, Vals));
|
|
|
|
}
|
|
|
|
|
2015-07-15 07:03:09 +08:00
|
|
|
// Setting interleave.count
|
|
|
|
if (Attrs.InterleaveCount > 0) {
|
2014-12-10 02:39:32 +08:00
|
|
|
Metadata *Vals[] = {MDString::get(Ctx, "llvm.loop.interleave.count"),
|
|
|
|
ConstantAsMetadata::get(ConstantInt::get(
|
2015-07-15 07:03:09 +08:00
|
|
|
Type::getInt32Ty(Ctx), Attrs.InterleaveCount))};
|
2014-05-22 16:54:05 +08:00
|
|
|
Args.push_back(MDNode::get(Ctx, Vals));
|
|
|
|
}
|
|
|
|
|
2018-08-01 22:36:12 +08:00
|
|
|
// Setting unroll.count
|
2015-07-28 04:10:20 +08:00
|
|
|
if (Attrs.UnrollCount > 0) {
|
|
|
|
Metadata *Vals[] = {MDString::get(Ctx, "llvm.loop.unroll.count"),
|
|
|
|
ConstantAsMetadata::get(ConstantInt::get(
|
|
|
|
Type::getInt32Ty(Ctx), Attrs.UnrollCount))};
|
|
|
|
Args.push_back(MDNode::get(Ctx, Vals));
|
|
|
|
}
|
|
|
|
|
2018-08-01 22:36:12 +08:00
|
|
|
// Setting unroll_and_jam.count
|
|
|
|
if (Attrs.UnrollAndJamCount > 0) {
|
|
|
|
Metadata *Vals[] = {MDString::get(Ctx, "llvm.loop.unroll_and_jam.count"),
|
|
|
|
ConstantAsMetadata::get(ConstantInt::get(
|
|
|
|
Type::getInt32Ty(Ctx), Attrs.UnrollAndJamCount))};
|
|
|
|
Args.push_back(MDNode::get(Ctx, Vals));
|
|
|
|
}
|
|
|
|
|
2015-07-15 07:03:09 +08:00
|
|
|
// Setting vectorize.enable
|
|
|
|
if (Attrs.VectorizeEnable != LoopAttributes::Unspecified) {
|
|
|
|
Metadata *Vals[] = {MDString::get(Ctx, "llvm.loop.vectorize.enable"),
|
|
|
|
ConstantAsMetadata::get(ConstantInt::get(
|
|
|
|
Type::getInt1Ty(Ctx), (Attrs.VectorizeEnable ==
|
|
|
|
LoopAttributes::Enable)))};
|
2014-05-22 16:54:05 +08:00
|
|
|
Args.push_back(MDNode::get(Ctx, Vals));
|
|
|
|
}
|
|
|
|
|
2015-07-28 04:10:20 +08:00
|
|
|
// Setting unroll.full or unroll.disable
|
|
|
|
if (Attrs.UnrollEnable != LoopAttributes::Unspecified) {
|
2015-08-11 01:29:39 +08:00
|
|
|
std::string Name;
|
|
|
|
if (Attrs.UnrollEnable == LoopAttributes::Enable)
|
|
|
|
Name = "llvm.loop.unroll.enable";
|
|
|
|
else if (Attrs.UnrollEnable == LoopAttributes::Full)
|
|
|
|
Name = "llvm.loop.unroll.full";
|
|
|
|
else
|
|
|
|
Name = "llvm.loop.unroll.disable";
|
|
|
|
Metadata *Vals[] = {MDString::get(Ctx, Name)};
|
2015-07-28 04:10:20 +08:00
|
|
|
Args.push_back(MDNode::get(Ctx, Vals));
|
|
|
|
}
|
|
|
|
|
2018-08-01 22:36:12 +08:00
|
|
|
// Setting unroll_and_jam.full or unroll_and_jam.disable
|
|
|
|
if (Attrs.UnrollAndJamEnable != LoopAttributes::Unspecified) {
|
|
|
|
std::string Name;
|
|
|
|
if (Attrs.UnrollAndJamEnable == LoopAttributes::Enable)
|
|
|
|
Name = "llvm.loop.unroll_and_jam.enable";
|
|
|
|
else if (Attrs.UnrollAndJamEnable == LoopAttributes::Full)
|
|
|
|
Name = "llvm.loop.unroll_and_jam.full";
|
|
|
|
else
|
|
|
|
Name = "llvm.loop.unroll_and_jam.disable";
|
|
|
|
Metadata *Vals[] = {MDString::get(Ctx, Name)};
|
|
|
|
Args.push_back(MDNode::get(Ctx, Vals));
|
|
|
|
}
|
|
|
|
|
2016-06-14 20:04:26 +08:00
|
|
|
if (Attrs.DistributeEnable != LoopAttributes::Unspecified) {
|
|
|
|
Metadata *Vals[] = {MDString::get(Ctx, "llvm.loop.distribute.enable"),
|
|
|
|
ConstantAsMetadata::get(ConstantInt::get(
|
|
|
|
Type::getInt1Ty(Ctx), (Attrs.DistributeEnable ==
|
|
|
|
LoopAttributes::Enable)))};
|
|
|
|
Args.push_back(MDNode::get(Ctx, Vals));
|
|
|
|
}
|
|
|
|
|
2018-12-21 05:24:54 +08:00
|
|
|
if (Attrs.IsParallel) {
|
|
|
|
AccGroup = MDNode::getDistinct(Ctx, {});
|
|
|
|
Args.push_back(MDNode::get(
|
|
|
|
Ctx, {MDString::get(Ctx, "llvm.loop.parallel_accesses"), AccGroup}));
|
|
|
|
}
|
|
|
|
|
2019-01-05 01:20:00 +08:00
|
|
|
if (Attrs.PipelineDisabled) {
|
|
|
|
Metadata *Vals[] = {
|
|
|
|
MDString::get(Ctx, "llvm.loop.pipeline.disable"),
|
|
|
|
ConstantAsMetadata::get(ConstantInt::get(
|
|
|
|
Type::getInt1Ty(Ctx), (Attrs.PipelineDisabled == true)))};
|
|
|
|
Args.push_back(MDNode::get(Ctx, Vals));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Attrs.PipelineInitiationInterval > 0) {
|
|
|
|
Metadata *Vals[] = {
|
|
|
|
MDString::get(Ctx, "llvm.loop.pipeline.initiationinterval"),
|
|
|
|
ConstantAsMetadata::get(ConstantInt::get(
|
|
|
|
Type::getInt32Ty(Ctx), Attrs.PipelineInitiationInterval))};
|
|
|
|
Args.push_back(MDNode::get(Ctx, Vals));
|
|
|
|
}
|
|
|
|
|
2014-05-22 16:54:05 +08:00
|
|
|
// Set the first operand to itself.
|
2014-12-10 02:39:32 +08:00
|
|
|
MDNode *LoopID = MDNode::get(Ctx, Args);
|
2014-05-22 16:54:05 +08:00
|
|
|
LoopID->replaceOperandWith(0, LoopID);
|
|
|
|
return LoopID;
|
|
|
|
}
|
|
|
|
|
|
|
|
LoopAttributes::LoopAttributes(bool IsParallel)
|
2015-07-15 07:03:09 +08:00
|
|
|
: IsParallel(IsParallel), VectorizeEnable(LoopAttributes::Unspecified),
|
2018-08-01 22:36:12 +08:00
|
|
|
UnrollEnable(LoopAttributes::Unspecified),
|
|
|
|
UnrollAndJamEnable(LoopAttributes::Unspecified), VectorizeWidth(0),
|
|
|
|
InterleaveCount(0), UnrollCount(0), UnrollAndJamCount(0),
|
2019-01-05 01:20:00 +08:00
|
|
|
DistributeEnable(LoopAttributes::Unspecified), PipelineDisabled(false),
|
|
|
|
PipelineInitiationInterval(0) {}
|
2014-05-22 16:54:05 +08:00
|
|
|
|
|
|
|
void LoopAttributes::clear() {
|
|
|
|
IsParallel = false;
|
2015-07-15 07:03:09 +08:00
|
|
|
VectorizeWidth = 0;
|
|
|
|
InterleaveCount = 0;
|
2015-07-28 04:10:20 +08:00
|
|
|
UnrollCount = 0;
|
2018-08-01 22:36:12 +08:00
|
|
|
UnrollAndJamCount = 0;
|
2015-07-15 07:03:09 +08:00
|
|
|
VectorizeEnable = LoopAttributes::Unspecified;
|
2015-07-28 04:10:20 +08:00
|
|
|
UnrollEnable = LoopAttributes::Unspecified;
|
2018-08-01 22:36:12 +08:00
|
|
|
UnrollAndJamEnable = LoopAttributes::Unspecified;
|
2016-08-24 12:31:56 +08:00
|
|
|
DistributeEnable = LoopAttributes::Unspecified;
|
2019-01-05 01:20:00 +08:00
|
|
|
PipelineDisabled = false;
|
|
|
|
PipelineInitiationInterval = 0;
|
2014-05-22 16:54:05 +08:00
|
|
|
}
|
|
|
|
|
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::LoopInfo(BasicBlock *Header, const LoopAttributes &Attrs,
|
2016-11-25 00:01:20 +08:00
|
|
|
const llvm::DebugLoc &StartLoc, const llvm::DebugLoc &EndLoc)
|
2014-05-22 16:54:05 +08:00
|
|
|
: LoopID(nullptr), Header(Header), Attrs(Attrs) {
|
2018-12-21 05:24:54 +08:00
|
|
|
LoopID =
|
|
|
|
createMetadata(Header->getContext(), Attrs, StartLoc, EndLoc, AccGroup);
|
2014-05-22 16:54:05 +08:00
|
|
|
}
|
|
|
|
|
2016-11-25 00:01:20 +08:00
|
|
|
void LoopInfoStack::push(BasicBlock *Header, const llvm::DebugLoc &StartLoc,
|
|
|
|
const llvm::DebugLoc &EndLoc) {
|
2016-11-10 22:44:30 +08:00
|
|
|
Active.push_back(LoopInfo(Header, StagedAttrs, StartLoc, EndLoc));
|
2015-07-28 04:10:20 +08:00
|
|
|
// Clear the attributes so nested loops do not inherit them.
|
|
|
|
StagedAttrs.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LoopInfoStack::push(BasicBlock *Header, clang::ASTContext &Ctx,
|
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
|
|
|
ArrayRef<const clang::Attr *> Attrs,
|
2016-11-25 00:01:20 +08:00
|
|
|
const llvm::DebugLoc &StartLoc,
|
|
|
|
const llvm::DebugLoc &EndLoc) {
|
2015-07-28 04:10:20 +08:00
|
|
|
|
|
|
|
// Identify loop hint attributes from Attrs.
|
2015-06-12 07:23:17 +08:00
|
|
|
for (const auto *Attr : Attrs) {
|
|
|
|
const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(Attr);
|
2016-02-20 02:30:11 +08:00
|
|
|
const OpenCLUnrollHintAttr *OpenCLHint =
|
|
|
|
dyn_cast<OpenCLUnrollHintAttr>(Attr);
|
2015-06-12 07:23:17 +08:00
|
|
|
|
|
|
|
// Skip non loop hint attributes
|
2016-02-20 02:30:11 +08:00
|
|
|
if (!LH && !OpenCLHint) {
|
2015-06-12 07:23:17 +08:00
|
|
|
continue;
|
2016-02-20 02:30:11 +08:00
|
|
|
}
|
2015-06-12 07:23:17 +08:00
|
|
|
|
2016-02-20 02:30:11 +08:00
|
|
|
LoopHintAttr::OptionType Option = LoopHintAttr::Unroll;
|
|
|
|
LoopHintAttr::LoopHintState State = LoopHintAttr::Disable;
|
2015-07-28 04:10:20 +08:00
|
|
|
unsigned ValueInt = 1;
|
2016-02-20 02:30:11 +08:00
|
|
|
// Translate opencl_unroll_hint attribute argument to
|
|
|
|
// equivalent LoopHintAttr enums.
|
2018-07-31 03:24:48 +08:00
|
|
|
// OpenCL v2.0 s6.11.5:
|
2016-02-20 02:30:11 +08:00
|
|
|
// 0 - full unroll (no argument).
|
|
|
|
// 1 - disable unroll.
|
|
|
|
// other positive integer n - unroll by n.
|
|
|
|
if (OpenCLHint) {
|
|
|
|
ValueInt = OpenCLHint->getUnrollHint();
|
|
|
|
if (ValueInt == 0) {
|
|
|
|
State = LoopHintAttr::Full;
|
|
|
|
} else if (ValueInt != 1) {
|
|
|
|
Option = LoopHintAttr::UnrollCount;
|
|
|
|
State = LoopHintAttr::Numeric;
|
|
|
|
}
|
|
|
|
} else if (LH) {
|
|
|
|
auto *ValueExpr = LH->getValue();
|
|
|
|
if (ValueExpr) {
|
|
|
|
llvm::APSInt ValueAPS = ValueExpr->EvaluateKnownConstInt(Ctx);
|
|
|
|
ValueInt = ValueAPS.getSExtValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
Option = LH->getOption();
|
|
|
|
State = LH->getState();
|
2015-07-28 04:10:20 +08:00
|
|
|
}
|
|
|
|
switch (State) {
|
|
|
|
case LoopHintAttr::Disable:
|
|
|
|
switch (Option) {
|
|
|
|
case LoopHintAttr::Vectorize:
|
|
|
|
// Disable vectorization by specifying a width of 1.
|
|
|
|
setVectorizeWidth(1);
|
|
|
|
break;
|
|
|
|
case LoopHintAttr::Interleave:
|
|
|
|
// Disable interleaving by speciyfing a count of 1.
|
|
|
|
setInterleaveCount(1);
|
|
|
|
break;
|
|
|
|
case LoopHintAttr::Unroll:
|
2015-08-11 01:29:39 +08:00
|
|
|
setUnrollState(LoopAttributes::Disable);
|
2015-07-28 04:10:20 +08:00
|
|
|
break;
|
2018-08-01 22:36:12 +08:00
|
|
|
case LoopHintAttr::UnrollAndJam:
|
|
|
|
setUnrollAndJamState(LoopAttributes::Disable);
|
|
|
|
break;
|
2016-06-14 20:04:26 +08:00
|
|
|
case LoopHintAttr::Distribute:
|
|
|
|
setDistributeState(false);
|
|
|
|
break;
|
2019-01-05 01:20:00 +08:00
|
|
|
case LoopHintAttr::PipelineDisabled:
|
|
|
|
setPipelineDisabled(true);
|
|
|
|
break;
|
2015-07-28 04:10:20 +08:00
|
|
|
case LoopHintAttr::UnrollCount:
|
2018-08-01 22:36:12 +08:00
|
|
|
case LoopHintAttr::UnrollAndJamCount:
|
2015-07-28 04:10:20 +08:00
|
|
|
case LoopHintAttr::VectorizeWidth:
|
|
|
|
case LoopHintAttr::InterleaveCount:
|
2019-01-05 01:20:00 +08:00
|
|
|
case LoopHintAttr::PipelineInitiationInterval:
|
2015-07-28 04:10:20 +08:00
|
|
|
llvm_unreachable("Options cannot be disabled.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LoopHintAttr::Enable:
|
|
|
|
switch (Option) {
|
|
|
|
case LoopHintAttr::Vectorize:
|
|
|
|
case LoopHintAttr::Interleave:
|
|
|
|
setVectorizeEnable(true);
|
|
|
|
break;
|
|
|
|
case LoopHintAttr::Unroll:
|
2015-08-11 01:29:39 +08:00
|
|
|
setUnrollState(LoopAttributes::Enable);
|
2015-07-28 04:10:20 +08:00
|
|
|
break;
|
2018-08-01 22:36:12 +08:00
|
|
|
case LoopHintAttr::UnrollAndJam:
|
|
|
|
setUnrollAndJamState(LoopAttributes::Enable);
|
|
|
|
break;
|
2016-06-14 20:04:26 +08:00
|
|
|
case LoopHintAttr::Distribute:
|
|
|
|
setDistributeState(true);
|
|
|
|
break;
|
2015-07-28 04:10:20 +08:00
|
|
|
case LoopHintAttr::UnrollCount:
|
2018-08-01 22:36:12 +08:00
|
|
|
case LoopHintAttr::UnrollAndJamCount:
|
2015-07-28 04:10:20 +08:00
|
|
|
case LoopHintAttr::VectorizeWidth:
|
|
|
|
case LoopHintAttr::InterleaveCount:
|
2019-01-05 01:20:00 +08:00
|
|
|
case LoopHintAttr::PipelineDisabled:
|
|
|
|
case LoopHintAttr::PipelineInitiationInterval:
|
2015-07-28 04:10:20 +08:00
|
|
|
llvm_unreachable("Options cannot enabled.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LoopHintAttr::AssumeSafety:
|
|
|
|
switch (Option) {
|
|
|
|
case LoopHintAttr::Vectorize:
|
|
|
|
case LoopHintAttr::Interleave:
|
2015-06-12 07:23:17 +08:00
|
|
|
// Apply "llvm.mem.parallel_loop_access" metadata to load/stores.
|
|
|
|
setParallel(true);
|
2015-07-28 04:10:20 +08:00
|
|
|
setVectorizeEnable(true);
|
|
|
|
break;
|
|
|
|
case LoopHintAttr::Unroll:
|
2018-08-01 22:36:12 +08:00
|
|
|
case LoopHintAttr::UnrollAndJam:
|
2015-07-28 04:10:20 +08:00
|
|
|
case LoopHintAttr::UnrollCount:
|
2018-08-01 22:36:12 +08:00
|
|
|
case LoopHintAttr::UnrollAndJamCount:
|
2015-07-28 04:10:20 +08:00
|
|
|
case LoopHintAttr::VectorizeWidth:
|
|
|
|
case LoopHintAttr::InterleaveCount:
|
2016-06-14 20:04:26 +08:00
|
|
|
case LoopHintAttr::Distribute:
|
2019-01-05 01:20:00 +08:00
|
|
|
case LoopHintAttr::PipelineDisabled:
|
|
|
|
case LoopHintAttr::PipelineInitiationInterval:
|
2015-07-28 04:10:20 +08:00
|
|
|
llvm_unreachable("Options cannot be used to assume mem safety.");
|
|
|
|
break;
|
2015-06-12 07:23:17 +08:00
|
|
|
}
|
|
|
|
break;
|
2015-08-11 01:29:39 +08:00
|
|
|
case LoopHintAttr::Full:
|
|
|
|
switch (Option) {
|
|
|
|
case LoopHintAttr::Unroll:
|
|
|
|
setUnrollState(LoopAttributes::Full);
|
|
|
|
break;
|
2018-08-01 22:36:12 +08:00
|
|
|
case LoopHintAttr::UnrollAndJam:
|
|
|
|
setUnrollAndJamState(LoopAttributes::Full);
|
|
|
|
break;
|
2015-08-11 01:29:39 +08:00
|
|
|
case LoopHintAttr::Vectorize:
|
|
|
|
case LoopHintAttr::Interleave:
|
|
|
|
case LoopHintAttr::UnrollCount:
|
2018-08-01 22:36:12 +08:00
|
|
|
case LoopHintAttr::UnrollAndJamCount:
|
2015-08-11 01:29:39 +08:00
|
|
|
case LoopHintAttr::VectorizeWidth:
|
|
|
|
case LoopHintAttr::InterleaveCount:
|
2016-06-14 20:04:26 +08:00
|
|
|
case LoopHintAttr::Distribute:
|
2019-01-05 01:20:00 +08:00
|
|
|
case LoopHintAttr::PipelineDisabled:
|
|
|
|
case LoopHintAttr::PipelineInitiationInterval:
|
2015-08-11 01:29:39 +08:00
|
|
|
llvm_unreachable("Options cannot be used with 'full' hint.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LoopHintAttr::Numeric:
|
2015-07-28 04:10:20 +08:00
|
|
|
switch (Option) {
|
|
|
|
case LoopHintAttr::VectorizeWidth:
|
|
|
|
setVectorizeWidth(ValueInt);
|
|
|
|
break;
|
|
|
|
case LoopHintAttr::InterleaveCount:
|
|
|
|
setInterleaveCount(ValueInt);
|
|
|
|
break;
|
|
|
|
case LoopHintAttr::UnrollCount:
|
|
|
|
setUnrollCount(ValueInt);
|
|
|
|
break;
|
2018-08-01 22:36:12 +08:00
|
|
|
case LoopHintAttr::UnrollAndJamCount:
|
|
|
|
setUnrollAndJamCount(ValueInt);
|
|
|
|
break;
|
2019-01-05 01:20:00 +08:00
|
|
|
case LoopHintAttr::PipelineInitiationInterval:
|
|
|
|
setPipelineInitiationInterval(ValueInt);
|
|
|
|
break;
|
2015-07-28 04:10:20 +08:00
|
|
|
case LoopHintAttr::Unroll:
|
2018-08-01 22:36:12 +08:00
|
|
|
case LoopHintAttr::UnrollAndJam:
|
2015-07-28 04:10:20 +08:00
|
|
|
case LoopHintAttr::Vectorize:
|
|
|
|
case LoopHintAttr::Interleave:
|
2016-06-14 20:04:26 +08:00
|
|
|
case LoopHintAttr::Distribute:
|
2019-01-05 01:20:00 +08:00
|
|
|
case LoopHintAttr::PipelineDisabled:
|
2015-08-11 01:29:39 +08:00
|
|
|
llvm_unreachable("Options cannot be assigned a value.");
|
2015-07-28 04:10:20 +08:00
|
|
|
break;
|
|
|
|
}
|
2015-06-12 07:23:17 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-28 04:10:20 +08:00
|
|
|
/// Stage the attributes.
|
2016-11-10 22:44:30 +08:00
|
|
|
push(Header, StartLoc, EndLoc);
|
2014-05-22 16:54:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LoopInfoStack::pop() {
|
|
|
|
assert(!Active.empty() && "No active loops to pop");
|
|
|
|
Active.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LoopInfoStack::InsertHelper(Instruction *I) const {
|
2018-12-21 05:24:54 +08:00
|
|
|
if (I->mayReadOrWriteMemory()) {
|
|
|
|
SmallVector<Metadata *, 4> AccessGroups;
|
|
|
|
for (const LoopInfo &AL : Active) {
|
|
|
|
// Here we assume that every loop that has an access group is parallel.
|
|
|
|
if (MDNode *Group = AL.getAccessGroup())
|
|
|
|
AccessGroups.push_back(Group);
|
|
|
|
}
|
|
|
|
MDNode *UnionMD = nullptr;
|
|
|
|
if (AccessGroups.size() == 1)
|
|
|
|
UnionMD = cast<MDNode>(AccessGroups[0]);
|
|
|
|
else if (AccessGroups.size() >= 2)
|
|
|
|
UnionMD = MDNode::get(I->getContext(), AccessGroups);
|
|
|
|
I->setMetadata("llvm.access.group", UnionMD);
|
|
|
|
}
|
|
|
|
|
2014-05-22 16:54:05 +08:00
|
|
|
if (!hasInfo())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const LoopInfo &L = getInfo();
|
|
|
|
if (!L.getLoopID())
|
|
|
|
return;
|
|
|
|
|
2018-10-18 16:16:20 +08:00
|
|
|
if (I->isTerminator()) {
|
|
|
|
for (BasicBlock *Succ : successors(I))
|
|
|
|
if (Succ == L.getHeader()) {
|
|
|
|
I->setMetadata(llvm::LLVMContext::MD_loop, L.getLoopID());
|
2014-05-22 16:54:05 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|