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"
|
2020-03-12 11:22:14 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
[Clang] Add llvm.loop.unroll.disable to loops with -fno-unroll-loops.
Currently Clang does not respect -fno-unroll-loops during LTO. During
D76916 it was suggested to respect -fno-unroll-loops on a TU basis.
This patch uses the existing llvm.loop.unroll.disable metadata to
disable loop unrolling explicitly for each loop in the TU if
unrolling is disabled. This should ensure that loops from TUs compiled
with -fno-unroll-loops are skipped by the unroller during LTO.
This also means that if a loop from a TU with -fno-unroll-loops
gets inlined into a TU without this option, the loop won't be
unrolled.
Due to the fact that some transforms might drop loop metadata, there
potentially are cases in which we still unroll loops from TUs with
-fno-unroll-loops. I think we should fix those issues rather than
introducing a function attribute to disable loop unrolling during LTO.
Improving the metadata handling will benefit other use cases, like
various loop pragmas, too. And it is an improvement to clang completely
ignoring -fno-unroll-loops during LTO.
If that direction looks good, we can use a similar approach to also
respect -fno-vectorize during LTO, at least for LoopVectorize.
In the future, this might also allow us to remove the UnrollLoops option
LLVM's PassManagerBuilder.
Reviewers: Meinersbur, hfinkel, dexonsmith, tejohnson
Reviewed By: Meinersbur, tejohnson
Differential Revision: https://reviews.llvm.org/D77058
2020-04-07 20:43:48 +08:00
|
|
|
#include "clang/Basic/CodeGenOptions.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;
|
|
|
|
|
2019-04-02 01:47:41 +08:00
|
|
|
MDNode *
|
|
|
|
LoopInfo::createLoopPropertiesMetadata(ArrayRef<Metadata *> LoopProperties) {
|
|
|
|
LLVMContext &Ctx = Header->getContext();
|
|
|
|
SmallVector<Metadata *, 4> NewLoopProperties;
|
2020-10-24 05:55:41 +08:00
|
|
|
NewLoopProperties.push_back(nullptr);
|
2019-04-02 01:47:41 +08:00
|
|
|
NewLoopProperties.append(LoopProperties.begin(), LoopProperties.end());
|
|
|
|
|
|
|
|
MDNode *LoopID = MDNode::getDistinct(Ctx, NewLoopProperties);
|
|
|
|
LoopID->replaceOperandWith(0, LoopID);
|
|
|
|
return LoopID;
|
|
|
|
}
|
2014-05-22 16:54:05 +08:00
|
|
|
|
2019-04-02 01:47:41 +08:00
|
|
|
MDNode *LoopInfo::createPipeliningMetadata(const LoopAttributes &Attrs,
|
|
|
|
ArrayRef<Metadata *> LoopProperties,
|
|
|
|
bool &HasUserTransforms) {
|
|
|
|
LLVMContext &Ctx = Header->getContext();
|
|
|
|
|
|
|
|
Optional<bool> Enabled;
|
|
|
|
if (Attrs.PipelineDisabled)
|
|
|
|
Enabled = false;
|
|
|
|
else if (Attrs.PipelineInitiationInterval != 0)
|
|
|
|
Enabled = true;
|
|
|
|
|
|
|
|
if (Enabled != true) {
|
|
|
|
SmallVector<Metadata *, 4> NewLoopProperties;
|
|
|
|
if (Enabled == false) {
|
|
|
|
NewLoopProperties.append(LoopProperties.begin(), LoopProperties.end());
|
|
|
|
NewLoopProperties.push_back(
|
|
|
|
MDNode::get(Ctx, {MDString::get(Ctx, "llvm.loop.pipeline.disable"),
|
|
|
|
ConstantAsMetadata::get(ConstantInt::get(
|
|
|
|
llvm::Type::getInt1Ty(Ctx), 1))}));
|
|
|
|
LoopProperties = NewLoopProperties;
|
|
|
|
}
|
|
|
|
return createLoopPropertiesMetadata(LoopProperties);
|
|
|
|
}
|
2014-05-22 16:54:05 +08:00
|
|
|
|
2014-12-10 02:39:32 +08:00
|
|
|
SmallVector<Metadata *, 4> Args;
|
2020-10-24 05:55:41 +08:00
|
|
|
Args.push_back(nullptr);
|
2019-04-02 01:47:41 +08:00
|
|
|
Args.append(LoopProperties.begin(), LoopProperties.end());
|
2014-05-22 16:54:05 +08:00
|
|
|
|
2019-04-02 01:47:41 +08:00
|
|
|
if (Attrs.PipelineInitiationInterval > 0) {
|
|
|
|
Metadata *Vals[] = {
|
|
|
|
MDString::get(Ctx, "llvm.loop.pipeline.initiationinterval"),
|
|
|
|
ConstantAsMetadata::get(ConstantInt::get(
|
|
|
|
llvm::Type::getInt32Ty(Ctx), Attrs.PipelineInitiationInterval))};
|
2014-05-22 16:54:05 +08:00
|
|
|
Args.push_back(MDNode::get(Ctx, Vals));
|
|
|
|
}
|
|
|
|
|
2019-04-02 01:47:41 +08:00
|
|
|
// No follow-up: This is the last transformation.
|
|
|
|
|
|
|
|
MDNode *LoopID = MDNode::getDistinct(Ctx, Args);
|
|
|
|
LoopID->replaceOperandWith(0, LoopID);
|
|
|
|
HasUserTransforms = true;
|
|
|
|
return LoopID;
|
|
|
|
}
|
|
|
|
|
|
|
|
MDNode *
|
|
|
|
LoopInfo::createPartialUnrollMetadata(const LoopAttributes &Attrs,
|
|
|
|
ArrayRef<Metadata *> LoopProperties,
|
|
|
|
bool &HasUserTransforms) {
|
|
|
|
LLVMContext &Ctx = Header->getContext();
|
|
|
|
|
|
|
|
Optional<bool> Enabled;
|
|
|
|
if (Attrs.UnrollEnable == LoopAttributes::Disable)
|
|
|
|
Enabled = false;
|
|
|
|
else if (Attrs.UnrollEnable == LoopAttributes::Full)
|
|
|
|
Enabled = None;
|
|
|
|
else if (Attrs.UnrollEnable != LoopAttributes::Unspecified ||
|
|
|
|
Attrs.UnrollCount != 0)
|
|
|
|
Enabled = true;
|
|
|
|
|
|
|
|
if (Enabled != true) {
|
|
|
|
// createFullUnrollMetadata will already have added llvm.loop.unroll.disable
|
|
|
|
// if unrolling is disabled.
|
|
|
|
return createPipeliningMetadata(Attrs, LoopProperties, HasUserTransforms);
|
2014-05-22 16:54:05 +08:00
|
|
|
}
|
|
|
|
|
2019-04-02 01:47:41 +08:00
|
|
|
SmallVector<Metadata *, 4> FollowupLoopProperties;
|
|
|
|
|
|
|
|
// Apply all loop properties to the unrolled loop.
|
|
|
|
FollowupLoopProperties.append(LoopProperties.begin(), LoopProperties.end());
|
|
|
|
|
|
|
|
// Don't unroll an already unrolled loop.
|
|
|
|
FollowupLoopProperties.push_back(
|
|
|
|
MDNode::get(Ctx, MDString::get(Ctx, "llvm.loop.unroll.disable")));
|
|
|
|
|
|
|
|
bool FollowupHasTransforms = false;
|
|
|
|
MDNode *Followup = createPipeliningMetadata(Attrs, FollowupLoopProperties,
|
|
|
|
FollowupHasTransforms);
|
|
|
|
|
|
|
|
SmallVector<Metadata *, 4> Args;
|
2020-10-24 05:55:41 +08:00
|
|
|
Args.push_back(nullptr);
|
2019-04-02 01:47:41 +08:00
|
|
|
Args.append(LoopProperties.begin(), LoopProperties.end());
|
|
|
|
|
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(
|
2019-04-02 01:47:41 +08:00
|
|
|
llvm::Type::getInt32Ty(Ctx), Attrs.UnrollCount))};
|
2015-07-28 04:10:20 +08:00
|
|
|
Args.push_back(MDNode::get(Ctx, Vals));
|
|
|
|
}
|
|
|
|
|
2019-04-02 01:47:41 +08:00
|
|
|
// Setting unroll.full or unroll.disable
|
|
|
|
if (Attrs.UnrollEnable == LoopAttributes::Enable) {
|
|
|
|
Metadata *Vals[] = {MDString::get(Ctx, "llvm.loop.unroll.enable")};
|
2018-08-01 22:36:12 +08:00
|
|
|
Args.push_back(MDNode::get(Ctx, Vals));
|
|
|
|
}
|
|
|
|
|
2019-04-02 01:47:41 +08:00
|
|
|
if (FollowupHasTransforms)
|
|
|
|
Args.push_back(MDNode::get(
|
|
|
|
Ctx, {MDString::get(Ctx, "llvm.loop.unroll.followup_all"), Followup}));
|
2014-05-22 16:54:05 +08:00
|
|
|
|
2019-04-02 01:47:41 +08:00
|
|
|
MDNode *LoopID = MDNode::getDistinct(Ctx, Args);
|
|
|
|
LoopID->replaceOperandWith(0, LoopID);
|
|
|
|
HasUserTransforms = true;
|
|
|
|
return LoopID;
|
|
|
|
}
|
|
|
|
|
|
|
|
MDNode *
|
|
|
|
LoopInfo::createUnrollAndJamMetadata(const LoopAttributes &Attrs,
|
|
|
|
ArrayRef<Metadata *> LoopProperties,
|
|
|
|
bool &HasUserTransforms) {
|
|
|
|
LLVMContext &Ctx = Header->getContext();
|
|
|
|
|
|
|
|
Optional<bool> Enabled;
|
|
|
|
if (Attrs.UnrollAndJamEnable == LoopAttributes::Disable)
|
|
|
|
Enabled = false;
|
|
|
|
else if (Attrs.UnrollAndJamEnable == LoopAttributes::Enable ||
|
|
|
|
Attrs.UnrollAndJamCount != 0)
|
|
|
|
Enabled = true;
|
|
|
|
|
|
|
|
if (Enabled != true) {
|
|
|
|
SmallVector<Metadata *, 4> NewLoopProperties;
|
|
|
|
if (Enabled == false) {
|
|
|
|
NewLoopProperties.append(LoopProperties.begin(), LoopProperties.end());
|
|
|
|
NewLoopProperties.push_back(MDNode::get(
|
|
|
|
Ctx, MDString::get(Ctx, "llvm.loop.unroll_and_jam.disable")));
|
|
|
|
LoopProperties = NewLoopProperties;
|
|
|
|
}
|
|
|
|
return createPartialUnrollMetadata(Attrs, LoopProperties,
|
|
|
|
HasUserTransforms);
|
2015-07-28 04:10:20 +08:00
|
|
|
}
|
|
|
|
|
2019-04-02 01:47:41 +08:00
|
|
|
SmallVector<Metadata *, 4> FollowupLoopProperties;
|
|
|
|
FollowupLoopProperties.append(LoopProperties.begin(), LoopProperties.end());
|
|
|
|
FollowupLoopProperties.push_back(
|
|
|
|
MDNode::get(Ctx, MDString::get(Ctx, "llvm.loop.unroll_and_jam.disable")));
|
|
|
|
|
|
|
|
bool FollowupHasTransforms = false;
|
|
|
|
MDNode *Followup = createPartialUnrollMetadata(Attrs, FollowupLoopProperties,
|
|
|
|
FollowupHasTransforms);
|
|
|
|
|
|
|
|
SmallVector<Metadata *, 4> Args;
|
2020-10-24 05:55:41 +08:00
|
|
|
Args.push_back(nullptr);
|
2019-04-02 01:47:41 +08:00
|
|
|
Args.append(LoopProperties.begin(), LoopProperties.end());
|
|
|
|
|
|
|
|
// Setting unroll_and_jam.count
|
|
|
|
if (Attrs.UnrollAndJamCount > 0) {
|
|
|
|
Metadata *Vals[] = {
|
|
|
|
MDString::get(Ctx, "llvm.loop.unroll_and_jam.count"),
|
|
|
|
ConstantAsMetadata::get(ConstantInt::get(llvm::Type::getInt32Ty(Ctx),
|
|
|
|
Attrs.UnrollAndJamCount))};
|
2018-08-01 22:36:12 +08:00
|
|
|
Args.push_back(MDNode::get(Ctx, Vals));
|
|
|
|
}
|
|
|
|
|
2019-04-02 01:47:41 +08:00
|
|
|
if (Attrs.UnrollAndJamEnable == LoopAttributes::Enable) {
|
|
|
|
Metadata *Vals[] = {MDString::get(Ctx, "llvm.loop.unroll_and_jam.enable")};
|
2016-06-14 20:04:26 +08:00
|
|
|
Args.push_back(MDNode::get(Ctx, Vals));
|
|
|
|
}
|
|
|
|
|
2019-04-02 01:47:41 +08:00
|
|
|
if (FollowupHasTransforms)
|
2018-12-21 05:24:54 +08:00
|
|
|
Args.push_back(MDNode::get(
|
2019-04-02 01:47:41 +08:00
|
|
|
Ctx, {MDString::get(Ctx, "llvm.loop.unroll_and_jam.followup_outer"),
|
|
|
|
Followup}));
|
|
|
|
|
|
|
|
if (UnrollAndJamInnerFollowup)
|
|
|
|
Args.push_back(MDNode::get(
|
|
|
|
Ctx, {MDString::get(Ctx, "llvm.loop.unroll_and_jam.followup_inner"),
|
|
|
|
UnrollAndJamInnerFollowup}));
|
|
|
|
|
|
|
|
MDNode *LoopID = MDNode::getDistinct(Ctx, Args);
|
|
|
|
LoopID->replaceOperandWith(0, LoopID);
|
|
|
|
HasUserTransforms = true;
|
|
|
|
return LoopID;
|
|
|
|
}
|
|
|
|
|
|
|
|
MDNode *
|
|
|
|
LoopInfo::createLoopVectorizeMetadata(const LoopAttributes &Attrs,
|
|
|
|
ArrayRef<Metadata *> LoopProperties,
|
|
|
|
bool &HasUserTransforms) {
|
|
|
|
LLVMContext &Ctx = Header->getContext();
|
|
|
|
|
|
|
|
Optional<bool> Enabled;
|
|
|
|
if (Attrs.VectorizeEnable == LoopAttributes::Disable)
|
|
|
|
Enabled = false;
|
|
|
|
else if (Attrs.VectorizeEnable != LoopAttributes::Unspecified ||
|
2019-07-25 15:33:13 +08:00
|
|
|
Attrs.VectorizePredicateEnable != LoopAttributes::Unspecified ||
|
2020-10-02 14:46:42 +08:00
|
|
|
Attrs.InterleaveCount != 0 || Attrs.VectorizeWidth != 0 ||
|
|
|
|
Attrs.VectorizeScalable != LoopAttributes::Unspecified)
|
2019-04-02 01:47:41 +08:00
|
|
|
Enabled = true;
|
|
|
|
|
|
|
|
if (Enabled != true) {
|
|
|
|
SmallVector<Metadata *, 4> NewLoopProperties;
|
|
|
|
if (Enabled == false) {
|
|
|
|
NewLoopProperties.append(LoopProperties.begin(), LoopProperties.end());
|
|
|
|
NewLoopProperties.push_back(
|
|
|
|
MDNode::get(Ctx, {MDString::get(Ctx, "llvm.loop.vectorize.enable"),
|
|
|
|
ConstantAsMetadata::get(ConstantInt::get(
|
|
|
|
llvm::Type::getInt1Ty(Ctx), 0))}));
|
|
|
|
LoopProperties = NewLoopProperties;
|
|
|
|
}
|
|
|
|
return createUnrollAndJamMetadata(Attrs, LoopProperties, HasUserTransforms);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply all loop properties to the vectorized loop.
|
|
|
|
SmallVector<Metadata *, 4> FollowupLoopProperties;
|
|
|
|
FollowupLoopProperties.append(LoopProperties.begin(), LoopProperties.end());
|
|
|
|
|
|
|
|
// Don't vectorize an already vectorized loop.
|
|
|
|
FollowupLoopProperties.push_back(
|
|
|
|
MDNode::get(Ctx, MDString::get(Ctx, "llvm.loop.isvectorized")));
|
|
|
|
|
|
|
|
bool FollowupHasTransforms = false;
|
|
|
|
MDNode *Followup = createUnrollAndJamMetadata(Attrs, FollowupLoopProperties,
|
|
|
|
FollowupHasTransforms);
|
|
|
|
|
|
|
|
SmallVector<Metadata *, 4> Args;
|
2020-10-24 05:55:41 +08:00
|
|
|
Args.push_back(nullptr);
|
2019-04-02 01:47:41 +08:00
|
|
|
Args.append(LoopProperties.begin(), LoopProperties.end());
|
|
|
|
|
2021-02-14 05:57:21 +08:00
|
|
|
// Setting vectorize.predicate when it has been specified and vectorization
|
|
|
|
// has not been disabled.
|
2019-08-15 14:24:40 +08:00
|
|
|
bool IsVectorPredicateEnabled = false;
|
2021-02-14 05:57:21 +08:00
|
|
|
if (Attrs.VectorizePredicateEnable != LoopAttributes::Unspecified) {
|
2019-08-15 14:24:40 +08:00
|
|
|
IsVectorPredicateEnabled =
|
|
|
|
(Attrs.VectorizePredicateEnable == LoopAttributes::Enable);
|
|
|
|
|
2019-07-25 15:33:13 +08:00
|
|
|
Metadata *Vals[] = {
|
|
|
|
MDString::get(Ctx, "llvm.loop.vectorize.predicate.enable"),
|
2019-08-15 14:24:40 +08:00
|
|
|
ConstantAsMetadata::get(ConstantInt::get(llvm::Type::getInt1Ty(Ctx),
|
|
|
|
IsVectorPredicateEnabled))};
|
2019-07-25 15:33:13 +08:00
|
|
|
Args.push_back(MDNode::get(Ctx, Vals));
|
|
|
|
}
|
|
|
|
|
2019-04-02 01:47:41 +08:00
|
|
|
// Setting vectorize.width
|
|
|
|
if (Attrs.VectorizeWidth > 0) {
|
|
|
|
Metadata *Vals[] = {
|
|
|
|
MDString::get(Ctx, "llvm.loop.vectorize.width"),
|
|
|
|
ConstantAsMetadata::get(ConstantInt::get(llvm::Type::getInt32Ty(Ctx),
|
|
|
|
Attrs.VectorizeWidth))};
|
2020-10-02 14:46:42 +08:00
|
|
|
|
|
|
|
Args.push_back(MDNode::get(Ctx, Vals));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Attrs.VectorizeScalable != LoopAttributes::Unspecified) {
|
|
|
|
bool IsScalable = Attrs.VectorizeScalable == LoopAttributes::Enable;
|
|
|
|
Metadata *Vals[] = {
|
|
|
|
MDString::get(Ctx, "llvm.loop.vectorize.scalable.enable"),
|
|
|
|
ConstantAsMetadata::get(
|
|
|
|
ConstantInt::get(llvm::Type::getInt1Ty(Ctx), IsScalable))};
|
2019-04-02 01:47:41 +08:00
|
|
|
Args.push_back(MDNode::get(Ctx, Vals));
|
2018-12-21 05:24:54 +08:00
|
|
|
}
|
|
|
|
|
2019-04-02 01:47:41 +08:00
|
|
|
// Setting interleave.count
|
|
|
|
if (Attrs.InterleaveCount > 0) {
|
2019-01-05 01:20:00 +08:00
|
|
|
Metadata *Vals[] = {
|
2019-04-02 01:47:41 +08:00
|
|
|
MDString::get(Ctx, "llvm.loop.interleave.count"),
|
|
|
|
ConstantAsMetadata::get(ConstantInt::get(llvm::Type::getInt32Ty(Ctx),
|
|
|
|
Attrs.InterleaveCount))};
|
2019-01-05 01:20:00 +08:00
|
|
|
Args.push_back(MDNode::get(Ctx, Vals));
|
|
|
|
}
|
|
|
|
|
2019-12-11 18:37:40 +08:00
|
|
|
// vectorize.enable is set if:
|
|
|
|
// 1) loop hint vectorize.enable is set, or
|
|
|
|
// 2) it is implied when vectorize.predicate is set, or
|
2020-10-02 14:46:42 +08:00
|
|
|
// 3) it is implied when vectorize.width is set to a value > 1
|
|
|
|
// 4) it is implied when vectorize.scalable.enable is true
|
|
|
|
// 5) it is implied when vectorize.width is unset (0) and the user
|
|
|
|
// explicitly requested fixed-width vectorization, i.e.
|
|
|
|
// vectorize.scalable.enable is false.
|
2019-08-15 14:24:40 +08:00
|
|
|
if (Attrs.VectorizeEnable != LoopAttributes::Unspecified ||
|
2021-02-14 05:57:21 +08:00
|
|
|
(IsVectorPredicateEnabled && Attrs.VectorizeWidth != 1) ||
|
|
|
|
Attrs.VectorizeWidth > 1 ||
|
2020-10-02 14:46:42 +08:00
|
|
|
Attrs.VectorizeScalable == LoopAttributes::Enable ||
|
|
|
|
(Attrs.VectorizeScalable == LoopAttributes::Disable &&
|
|
|
|
Attrs.VectorizeWidth != 1)) {
|
2019-12-11 18:37:40 +08:00
|
|
|
bool AttrVal = Attrs.VectorizeEnable != LoopAttributes::Disable;
|
|
|
|
Args.push_back(
|
|
|
|
MDNode::get(Ctx, {MDString::get(Ctx, "llvm.loop.vectorize.enable"),
|
|
|
|
ConstantAsMetadata::get(ConstantInt::get(
|
|
|
|
llvm::Type::getInt1Ty(Ctx), AttrVal))}));
|
2019-01-05 01:20:00 +08:00
|
|
|
}
|
|
|
|
|
2019-04-02 01:47:41 +08:00
|
|
|
if (FollowupHasTransforms)
|
|
|
|
Args.push_back(MDNode::get(
|
|
|
|
Ctx,
|
|
|
|
{MDString::get(Ctx, "llvm.loop.vectorize.followup_all"), Followup}));
|
|
|
|
|
2020-10-24 05:55:41 +08:00
|
|
|
MDNode *LoopID = MDNode::getDistinct(Ctx, Args);
|
2019-04-02 01:47:41 +08:00
|
|
|
LoopID->replaceOperandWith(0, LoopID);
|
|
|
|
HasUserTransforms = true;
|
|
|
|
return LoopID;
|
|
|
|
}
|
|
|
|
|
|
|
|
MDNode *
|
|
|
|
LoopInfo::createLoopDistributeMetadata(const LoopAttributes &Attrs,
|
|
|
|
ArrayRef<Metadata *> LoopProperties,
|
|
|
|
bool &HasUserTransforms) {
|
|
|
|
LLVMContext &Ctx = Header->getContext();
|
|
|
|
|
|
|
|
Optional<bool> Enabled;
|
|
|
|
if (Attrs.DistributeEnable == LoopAttributes::Disable)
|
|
|
|
Enabled = false;
|
|
|
|
if (Attrs.DistributeEnable == LoopAttributes::Enable)
|
|
|
|
Enabled = true;
|
|
|
|
|
|
|
|
if (Enabled != true) {
|
|
|
|
SmallVector<Metadata *, 4> NewLoopProperties;
|
|
|
|
if (Enabled == false) {
|
|
|
|
NewLoopProperties.append(LoopProperties.begin(), LoopProperties.end());
|
|
|
|
NewLoopProperties.push_back(
|
|
|
|
MDNode::get(Ctx, {MDString::get(Ctx, "llvm.loop.distribute.enable"),
|
|
|
|
ConstantAsMetadata::get(ConstantInt::get(
|
|
|
|
llvm::Type::getInt1Ty(Ctx), 0))}));
|
|
|
|
LoopProperties = NewLoopProperties;
|
|
|
|
}
|
|
|
|
return createLoopVectorizeMetadata(Attrs, LoopProperties,
|
|
|
|
HasUserTransforms);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FollowupHasTransforms = false;
|
|
|
|
MDNode *Followup =
|
|
|
|
createLoopVectorizeMetadata(Attrs, LoopProperties, FollowupHasTransforms);
|
|
|
|
|
|
|
|
SmallVector<Metadata *, 4> Args;
|
2020-10-24 05:55:41 +08:00
|
|
|
Args.push_back(nullptr);
|
2019-04-02 01:47:41 +08:00
|
|
|
Args.append(LoopProperties.begin(), LoopProperties.end());
|
|
|
|
|
|
|
|
Metadata *Vals[] = {MDString::get(Ctx, "llvm.loop.distribute.enable"),
|
|
|
|
ConstantAsMetadata::get(ConstantInt::get(
|
|
|
|
llvm::Type::getInt1Ty(Ctx),
|
|
|
|
(Attrs.DistributeEnable == LoopAttributes::Enable)))};
|
|
|
|
Args.push_back(MDNode::get(Ctx, Vals));
|
|
|
|
|
|
|
|
if (FollowupHasTransforms)
|
|
|
|
Args.push_back(MDNode::get(
|
|
|
|
Ctx,
|
|
|
|
{MDString::get(Ctx, "llvm.loop.distribute.followup_all"), Followup}));
|
|
|
|
|
2020-10-24 05:55:41 +08:00
|
|
|
MDNode *LoopID = MDNode::getDistinct(Ctx, Args);
|
2014-05-22 16:54:05 +08:00
|
|
|
LoopID->replaceOperandWith(0, LoopID);
|
2019-04-02 01:47:41 +08:00
|
|
|
HasUserTransforms = true;
|
|
|
|
return LoopID;
|
|
|
|
}
|
|
|
|
|
|
|
|
MDNode *LoopInfo::createFullUnrollMetadata(const LoopAttributes &Attrs,
|
|
|
|
ArrayRef<Metadata *> LoopProperties,
|
|
|
|
bool &HasUserTransforms) {
|
|
|
|
LLVMContext &Ctx = Header->getContext();
|
|
|
|
|
|
|
|
Optional<bool> Enabled;
|
|
|
|
if (Attrs.UnrollEnable == LoopAttributes::Disable)
|
|
|
|
Enabled = false;
|
|
|
|
else if (Attrs.UnrollEnable == LoopAttributes::Full)
|
|
|
|
Enabled = true;
|
|
|
|
|
|
|
|
if (Enabled != true) {
|
|
|
|
SmallVector<Metadata *, 4> NewLoopProperties;
|
|
|
|
if (Enabled == false) {
|
|
|
|
NewLoopProperties.append(LoopProperties.begin(), LoopProperties.end());
|
|
|
|
NewLoopProperties.push_back(
|
|
|
|
MDNode::get(Ctx, MDString::get(Ctx, "llvm.loop.unroll.disable")));
|
|
|
|
LoopProperties = NewLoopProperties;
|
|
|
|
}
|
|
|
|
return createLoopDistributeMetadata(Attrs, LoopProperties,
|
|
|
|
HasUserTransforms);
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<Metadata *, 4> Args;
|
2020-10-24 05:55:41 +08:00
|
|
|
Args.push_back(nullptr);
|
2019-04-02 01:47:41 +08:00
|
|
|
Args.append(LoopProperties.begin(), LoopProperties.end());
|
|
|
|
Args.push_back(MDNode::get(Ctx, MDString::get(Ctx, "llvm.loop.unroll.full")));
|
|
|
|
|
|
|
|
// No follow-up: there is no loop after full unrolling.
|
|
|
|
// TODO: Warn if there are transformations after full unrolling.
|
|
|
|
|
|
|
|
MDNode *LoopID = MDNode::getDistinct(Ctx, Args);
|
|
|
|
LoopID->replaceOperandWith(0, LoopID);
|
|
|
|
HasUserTransforms = true;
|
2014-05-22 16:54:05 +08:00
|
|
|
return LoopID;
|
|
|
|
}
|
|
|
|
|
2019-04-02 01:47:41 +08:00
|
|
|
MDNode *LoopInfo::createMetadata(
|
|
|
|
const LoopAttributes &Attrs,
|
|
|
|
llvm::ArrayRef<llvm::Metadata *> AdditionalLoopProperties,
|
|
|
|
bool &HasUserTransforms) {
|
|
|
|
SmallVector<Metadata *, 3> LoopProperties;
|
|
|
|
|
|
|
|
// If we have a valid start debug location for the loop, add it.
|
|
|
|
if (StartLoc) {
|
|
|
|
LoopProperties.push_back(StartLoc.getAsMDNode());
|
|
|
|
|
|
|
|
// If we also have a valid end debug location for the loop, add it.
|
|
|
|
if (EndLoc)
|
|
|
|
LoopProperties.push_back(EndLoc.getAsMDNode());
|
|
|
|
}
|
|
|
|
|
2020-11-03 05:03:21 +08:00
|
|
|
LLVMContext &Ctx = Header->getContext();
|
|
|
|
if (Attrs.MustProgress)
|
|
|
|
LoopProperties.push_back(
|
|
|
|
MDNode::get(Ctx, MDString::get(Ctx, "llvm.loop.mustprogress")));
|
|
|
|
|
2019-04-02 01:47:41 +08:00
|
|
|
assert(!!AccGroup == Attrs.IsParallel &&
|
|
|
|
"There must be an access group iff the loop is parallel");
|
|
|
|
if (Attrs.IsParallel) {
|
|
|
|
LoopProperties.push_back(MDNode::get(
|
|
|
|
Ctx, {MDString::get(Ctx, "llvm.loop.parallel_accesses"), AccGroup}));
|
|
|
|
}
|
|
|
|
|
|
|
|
LoopProperties.insert(LoopProperties.end(), AdditionalLoopProperties.begin(),
|
|
|
|
AdditionalLoopProperties.end());
|
|
|
|
return createFullUnrollMetadata(Attrs, LoopProperties, HasUserTransforms);
|
|
|
|
}
|
|
|
|
|
2014-05-22 16:54:05 +08:00
|
|
|
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),
|
2019-07-25 15:33:13 +08:00
|
|
|
UnrollAndJamEnable(LoopAttributes::Unspecified),
|
|
|
|
VectorizePredicateEnable(LoopAttributes::Unspecified), VectorizeWidth(0),
|
2020-10-02 14:46:42 +08:00
|
|
|
VectorizeScalable(LoopAttributes::Unspecified), InterleaveCount(0),
|
|
|
|
UnrollCount(0), UnrollAndJamCount(0),
|
2019-01-05 01:20:00 +08:00
|
|
|
DistributeEnable(LoopAttributes::Unspecified), PipelineDisabled(false),
|
2020-11-03 05:03:21 +08:00
|
|
|
PipelineInitiationInterval(0), MustProgress(false) {}
|
2014-05-22 16:54:05 +08:00
|
|
|
|
|
|
|
void LoopAttributes::clear() {
|
|
|
|
IsParallel = false;
|
2015-07-15 07:03:09 +08:00
|
|
|
VectorizeWidth = 0;
|
2020-10-02 14:46:42 +08:00
|
|
|
VectorizeScalable = LoopAttributes::Unspecified;
|
2015-07-15 07:03:09 +08:00
|
|
|
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;
|
2019-07-25 15:33:13 +08:00
|
|
|
VectorizePredicateEnable = 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;
|
2020-11-03 05:03:21 +08:00
|
|
|
MustProgress = false;
|
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,
|
2019-04-02 01:47:41 +08:00
|
|
|
const llvm::DebugLoc &StartLoc, const llvm::DebugLoc &EndLoc,
|
|
|
|
LoopInfo *Parent)
|
|
|
|
: Header(Header), Attrs(Attrs), StartLoc(StartLoc), EndLoc(EndLoc),
|
|
|
|
Parent(Parent) {
|
|
|
|
|
|
|
|
if (Attrs.IsParallel) {
|
|
|
|
// Create an access group for this loop.
|
|
|
|
LLVMContext &Ctx = Header->getContext();
|
|
|
|
AccGroup = MDNode::getDistinct(Ctx, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Attrs.IsParallel && Attrs.VectorizeWidth == 0 &&
|
2020-10-02 14:46:42 +08:00
|
|
|
Attrs.VectorizeScalable == LoopAttributes::Unspecified &&
|
2019-04-02 01:47:41 +08:00
|
|
|
Attrs.InterleaveCount == 0 && Attrs.UnrollCount == 0 &&
|
|
|
|
Attrs.UnrollAndJamCount == 0 && !Attrs.PipelineDisabled &&
|
|
|
|
Attrs.PipelineInitiationInterval == 0 &&
|
2019-07-25 15:33:13 +08:00
|
|
|
Attrs.VectorizePredicateEnable == LoopAttributes::Unspecified &&
|
2019-04-02 01:47:41 +08:00
|
|
|
Attrs.VectorizeEnable == LoopAttributes::Unspecified &&
|
|
|
|
Attrs.UnrollEnable == LoopAttributes::Unspecified &&
|
|
|
|
Attrs.UnrollAndJamEnable == LoopAttributes::Unspecified &&
|
|
|
|
Attrs.DistributeEnable == LoopAttributes::Unspecified && !StartLoc &&
|
2020-11-03 05:03:21 +08:00
|
|
|
!EndLoc && !Attrs.MustProgress)
|
2019-04-02 01:47:41 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
TempLoopID = MDNode::getTemporary(Header->getContext(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LoopInfo::finish() {
|
|
|
|
// We did not annotate the loop body instructions because there are no
|
|
|
|
// attributes for this loop.
|
|
|
|
if (!TempLoopID)
|
|
|
|
return;
|
|
|
|
|
|
|
|
MDNode *LoopID;
|
|
|
|
LoopAttributes CurLoopAttr = Attrs;
|
|
|
|
LLVMContext &Ctx = Header->getContext();
|
|
|
|
|
|
|
|
if (Parent && (Parent->Attrs.UnrollAndJamEnable ||
|
|
|
|
Parent->Attrs.UnrollAndJamCount != 0)) {
|
|
|
|
// Parent unroll-and-jams this loop.
|
|
|
|
// Split the transformations in those that happens before the unroll-and-jam
|
|
|
|
// and those after.
|
|
|
|
|
|
|
|
LoopAttributes BeforeJam, AfterJam;
|
|
|
|
|
|
|
|
BeforeJam.IsParallel = AfterJam.IsParallel = Attrs.IsParallel;
|
|
|
|
|
|
|
|
BeforeJam.VectorizeWidth = Attrs.VectorizeWidth;
|
2020-10-02 14:46:42 +08:00
|
|
|
BeforeJam.VectorizeScalable = Attrs.VectorizeScalable;
|
2019-04-02 01:47:41 +08:00
|
|
|
BeforeJam.InterleaveCount = Attrs.InterleaveCount;
|
|
|
|
BeforeJam.VectorizeEnable = Attrs.VectorizeEnable;
|
|
|
|
BeforeJam.DistributeEnable = Attrs.DistributeEnable;
|
2019-07-25 15:33:13 +08:00
|
|
|
BeforeJam.VectorizePredicateEnable = Attrs.VectorizePredicateEnable;
|
2019-04-02 01:47:41 +08:00
|
|
|
|
|
|
|
switch (Attrs.UnrollEnable) {
|
|
|
|
case LoopAttributes::Unspecified:
|
|
|
|
case LoopAttributes::Disable:
|
|
|
|
BeforeJam.UnrollEnable = Attrs.UnrollEnable;
|
|
|
|
AfterJam.UnrollEnable = Attrs.UnrollEnable;
|
|
|
|
break;
|
|
|
|
case LoopAttributes::Full:
|
|
|
|
BeforeJam.UnrollEnable = LoopAttributes::Full;
|
|
|
|
break;
|
|
|
|
case LoopAttributes::Enable:
|
|
|
|
AfterJam.UnrollEnable = LoopAttributes::Enable;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-07-25 15:33:13 +08:00
|
|
|
AfterJam.VectorizePredicateEnable = Attrs.VectorizePredicateEnable;
|
2019-04-02 01:47:41 +08:00
|
|
|
AfterJam.UnrollCount = Attrs.UnrollCount;
|
|
|
|
AfterJam.PipelineDisabled = Attrs.PipelineDisabled;
|
|
|
|
AfterJam.PipelineInitiationInterval = Attrs.PipelineInitiationInterval;
|
|
|
|
|
|
|
|
// If this loop is subject of an unroll-and-jam by the parent loop, and has
|
|
|
|
// an unroll-and-jam annotation itself, we have to decide whether to first
|
|
|
|
// apply the parent's unroll-and-jam or this loop's unroll-and-jam. The
|
|
|
|
// UnrollAndJam pass processes loops from inner to outer, so we apply the
|
|
|
|
// inner first.
|
|
|
|
BeforeJam.UnrollAndJamCount = Attrs.UnrollAndJamCount;
|
|
|
|
BeforeJam.UnrollAndJamEnable = Attrs.UnrollAndJamEnable;
|
|
|
|
|
|
|
|
// Set the inner followup metadata to process by the outer loop. Only
|
|
|
|
// consider the first inner loop.
|
|
|
|
if (!Parent->UnrollAndJamInnerFollowup) {
|
|
|
|
// Splitting the attributes into a BeforeJam and an AfterJam part will
|
|
|
|
// stop 'llvm.loop.isvectorized' (generated by vectorization in BeforeJam)
|
|
|
|
// to be forwarded to the AfterJam part. We detect the situation here and
|
|
|
|
// add it manually.
|
|
|
|
SmallVector<Metadata *, 1> BeforeLoopProperties;
|
|
|
|
if (BeforeJam.VectorizeEnable != LoopAttributes::Unspecified ||
|
2019-07-25 15:33:13 +08:00
|
|
|
BeforeJam.VectorizePredicateEnable != LoopAttributes::Unspecified ||
|
2020-10-02 14:46:42 +08:00
|
|
|
BeforeJam.InterleaveCount != 0 || BeforeJam.VectorizeWidth != 0 ||
|
|
|
|
BeforeJam.VectorizeScalable == LoopAttributes::Enable)
|
2019-04-02 01:47:41 +08:00
|
|
|
BeforeLoopProperties.push_back(
|
|
|
|
MDNode::get(Ctx, MDString::get(Ctx, "llvm.loop.isvectorized")));
|
|
|
|
|
|
|
|
bool InnerFollowupHasTransform = false;
|
|
|
|
MDNode *InnerFollowup = createMetadata(AfterJam, BeforeLoopProperties,
|
|
|
|
InnerFollowupHasTransform);
|
|
|
|
if (InnerFollowupHasTransform)
|
|
|
|
Parent->UnrollAndJamInnerFollowup = InnerFollowup;
|
|
|
|
}
|
|
|
|
|
|
|
|
CurLoopAttr = BeforeJam;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasUserTransforms = false;
|
|
|
|
LoopID = createMetadata(CurLoopAttr, {}, HasUserTransforms);
|
|
|
|
TempLoopID->replaceAllUsesWith(LoopID);
|
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) {
|
2019-08-19 21:37:41 +08:00
|
|
|
Active.emplace_back(
|
|
|
|
new LoopInfo(Header, StagedAttrs, StartLoc, EndLoc,
|
|
|
|
Active.empty() ? nullptr : Active.back().get()));
|
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,
|
[Clang] Add llvm.loop.unroll.disable to loops with -fno-unroll-loops.
Currently Clang does not respect -fno-unroll-loops during LTO. During
D76916 it was suggested to respect -fno-unroll-loops on a TU basis.
This patch uses the existing llvm.loop.unroll.disable metadata to
disable loop unrolling explicitly for each loop in the TU if
unrolling is disabled. This should ensure that loops from TUs compiled
with -fno-unroll-loops are skipped by the unroller during LTO.
This also means that if a loop from a TU with -fno-unroll-loops
gets inlined into a TU without this option, the loop won't be
unrolled.
Due to the fact that some transforms might drop loop metadata, there
potentially are cases in which we still unroll loops from TUs with
-fno-unroll-loops. I think we should fix those issues rather than
introducing a function attribute to disable loop unrolling during LTO.
Improving the metadata handling will benefit other use cases, like
various loop pragmas, too. And it is an improvement to clang completely
ignoring -fno-unroll-loops during LTO.
If that direction looks good, we can use a similar approach to also
respect -fno-vectorize during LTO, at least for LoopVectorize.
In the future, this might also allow us to remove the UnrollLoops option
LLVM's PassManagerBuilder.
Reviewers: Meinersbur, hfinkel, dexonsmith, tejohnson
Reviewed By: Meinersbur, tejohnson
Differential Revision: https://reviews.llvm.org/D77058
2020-04-07 20:43:48 +08:00
|
|
|
const clang::CodeGenOptions &CGOpts,
|
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,
|
2020-11-03 05:03:21 +08:00
|
|
|
const llvm::DebugLoc &EndLoc, bool MustProgress) {
|
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:
|
[OpenCL] Generate 'unroll.enable' metadata for __attribute__((opencl_unroll_hint))
Summary:
[OpenCL] Generate 'unroll.enable' metadata for __attribute__((opencl_unroll_hint))
For both !{!"llvm.loop.unroll.enable"} and !{!"llvm.loop.unroll.full"} the unroller
will try to fully unroll a loop unless the trip count is not known at compile time.
In that case for '.full' metadata no unrolling will be processed, while for '.enable'
the loop will be partially unrolled with a heuristically chosen unroll factor.
See: docs/LanguageExtensions.rst
From https://www.khronos.org/registry/OpenCL/sdk/2.0/docs/man/xhtml/attributes-loopUnroll.html
__attribute__((opencl_unroll_hint))
for (int i=0; i<2; i++)
{
...
}
In the example above, the compiler will determine how much to unroll the loop.
Before the patch for __attribute__((opencl_unroll_hint)) was generated metadata
!{!"llvm.loop.unroll.full"}, which limits ability of loop unroller to decide, how
much to unroll the loop.
Reviewers: Anastasia, yaxunl
Reviewed By: Anastasia
Subscribers: zzheng, dmgreen, jdoerfert, cfe-commits, asavonic, AlexeySotkin
Tags: #clang
Differential Revision: https://reviews.llvm.org/D59493
llvm-svn: 356571
2019-03-21 00:43:07 +08:00
|
|
|
// 0 - enable unroll (no argument).
|
2016-02-20 02:30:11 +08:00
|
|
|
// 1 - disable unroll.
|
|
|
|
// other positive integer n - unroll by n.
|
|
|
|
if (OpenCLHint) {
|
|
|
|
ValueInt = OpenCLHint->getUnrollHint();
|
|
|
|
if (ValueInt == 0) {
|
[OpenCL] Generate 'unroll.enable' metadata for __attribute__((opencl_unroll_hint))
Summary:
[OpenCL] Generate 'unroll.enable' metadata for __attribute__((opencl_unroll_hint))
For both !{!"llvm.loop.unroll.enable"} and !{!"llvm.loop.unroll.full"} the unroller
will try to fully unroll a loop unless the trip count is not known at compile time.
In that case for '.full' metadata no unrolling will be processed, while for '.enable'
the loop will be partially unrolled with a heuristically chosen unroll factor.
See: docs/LanguageExtensions.rst
From https://www.khronos.org/registry/OpenCL/sdk/2.0/docs/man/xhtml/attributes-loopUnroll.html
__attribute__((opencl_unroll_hint))
for (int i=0; i<2; i++)
{
...
}
In the example above, the compiler will determine how much to unroll the loop.
Before the patch for __attribute__((opencl_unroll_hint)) was generated metadata
!{!"llvm.loop.unroll.full"}, which limits ability of loop unroller to decide, how
much to unroll the loop.
Reviewers: Anastasia, yaxunl
Reviewed By: Anastasia
Subscribers: zzheng, dmgreen, jdoerfert, cfe-commits, asavonic, AlexeySotkin
Tags: #clang
Differential Revision: https://reviews.llvm.org/D59493
llvm-svn: 356571
2019-03-21 00:43:07 +08:00
|
|
|
State = LoopHintAttr::Enable;
|
2016-02-20 02:30:11 +08:00
|
|
|
} 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);
|
2020-10-02 14:46:42 +08:00
|
|
|
setVectorizeScalable(LoopAttributes::Unspecified);
|
2015-07-28 04:10:20 +08:00
|
|
|
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;
|
2019-07-25 15:33:13 +08:00
|
|
|
case LoopHintAttr::VectorizePredicate:
|
|
|
|
setVectorizePredicateState(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;
|
2019-07-25 15:33:13 +08:00
|
|
|
case LoopHintAttr::VectorizePredicate:
|
|
|
|
setVectorizePredicateState(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:
|
2019-07-25 15:33:13 +08:00
|
|
|
case LoopHintAttr::VectorizePredicate:
|
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:
|
2019-07-25 15:33:13 +08:00
|
|
|
case LoopHintAttr::VectorizePredicate:
|
2015-08-11 01:29:39 +08:00
|
|
|
llvm_unreachable("Options cannot be used with 'full' hint.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2020-10-02 14:46:42 +08:00
|
|
|
case LoopHintAttr::FixedWidth:
|
|
|
|
case LoopHintAttr::ScalableWidth:
|
2015-07-28 04:10:20 +08:00
|
|
|
switch (Option) {
|
|
|
|
case LoopHintAttr::VectorizeWidth:
|
2020-10-02 14:46:42 +08:00
|
|
|
setVectorizeScalable(State == LoopHintAttr::ScalableWidth
|
|
|
|
? LoopAttributes::Enable
|
|
|
|
: LoopAttributes::Disable);
|
|
|
|
if (LH->getValue())
|
|
|
|
setVectorizeWidth(ValueInt);
|
2015-07-28 04:10:20 +08:00
|
|
|
break;
|
2020-10-02 14:46:42 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("Options cannot be used with 'scalable' hint.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LoopHintAttr::Numeric:
|
|
|
|
switch (Option) {
|
2015-07-28 04:10:20 +08:00
|
|
|
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:
|
2019-07-25 15:33:13 +08:00
|
|
|
case LoopHintAttr::VectorizePredicate:
|
2015-07-28 04:10:20 +08:00
|
|
|
case LoopHintAttr::Vectorize:
|
2020-10-02 14:46:42 +08:00
|
|
|
case LoopHintAttr::VectorizeWidth:
|
2015-07-28 04:10:20 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 05:03:21 +08:00
|
|
|
setMustProgress(MustProgress);
|
|
|
|
|
[Clang] Add llvm.loop.unroll.disable to loops with -fno-unroll-loops.
Currently Clang does not respect -fno-unroll-loops during LTO. During
D76916 it was suggested to respect -fno-unroll-loops on a TU basis.
This patch uses the existing llvm.loop.unroll.disable metadata to
disable loop unrolling explicitly for each loop in the TU if
unrolling is disabled. This should ensure that loops from TUs compiled
with -fno-unroll-loops are skipped by the unroller during LTO.
This also means that if a loop from a TU with -fno-unroll-loops
gets inlined into a TU without this option, the loop won't be
unrolled.
Due to the fact that some transforms might drop loop metadata, there
potentially are cases in which we still unroll loops from TUs with
-fno-unroll-loops. I think we should fix those issues rather than
introducing a function attribute to disable loop unrolling during LTO.
Improving the metadata handling will benefit other use cases, like
various loop pragmas, too. And it is an improvement to clang completely
ignoring -fno-unroll-loops during LTO.
If that direction looks good, we can use a similar approach to also
respect -fno-vectorize during LTO, at least for LoopVectorize.
In the future, this might also allow us to remove the UnrollLoops option
LLVM's PassManagerBuilder.
Reviewers: Meinersbur, hfinkel, dexonsmith, tejohnson
Reviewed By: Meinersbur, tejohnson
Differential Revision: https://reviews.llvm.org/D77058
2020-04-07 20:43:48 +08:00
|
|
|
if (CGOpts.OptimizationLevel > 0)
|
|
|
|
// Disable unrolling for the loop, if unrolling is disabled (via
|
|
|
|
// -fno-unroll-loops) and no pragmas override the decision.
|
|
|
|
if (!CGOpts.UnrollLoops &&
|
|
|
|
(StagedAttrs.UnrollEnable == LoopAttributes::Unspecified &&
|
|
|
|
StagedAttrs.UnrollCount == 0))
|
|
|
|
setUnrollState(LoopAttributes::Disable);
|
|
|
|
|
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");
|
2019-08-19 21:37:41 +08:00
|
|
|
Active.back()->finish();
|
2014-05-22 16:54:05 +08:00
|
|
|
Active.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LoopInfoStack::InsertHelper(Instruction *I) const {
|
2018-12-21 05:24:54 +08:00
|
|
|
if (I->mayReadOrWriteMemory()) {
|
|
|
|
SmallVector<Metadata *, 4> AccessGroups;
|
2019-08-19 21:37:41 +08:00
|
|
|
for (const auto &AL : Active) {
|
2018-12-21 05:24:54 +08:00
|
|
|
// Here we assume that every loop that has an access group is parallel.
|
2019-08-19 21:37:41 +08:00
|
|
|
if (MDNode *Group = AL->getAccessGroup())
|
2018-12-21 05:24:54 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|