diff options
author | David Sherwood <david.sherwood@arm.com> | 2020-10-02 07:46:42 +0100 |
---|---|---|
committer | David Sherwood <david.sherwood@arm.com> | 2021-01-08 11:37:27 +0000 |
commit | 38d18d93534d290d045bbbfa86337e70f1139dc2 (patch) | |
tree | 34584f20cd70ee97154ca19c5480a1d156debd74 /clang/lib/CodeGen/CGLoopInfo.cpp | |
parent | 1e7efd397ac259429df9f212c9ec16bec8ff2533 (diff) | |
download | llvm-38d18d93534d290d045bbbfa86337e70f1139dc2.zip llvm-38d18d93534d290d045bbbfa86337e70f1139dc2.tar.gz llvm-38d18d93534d290d045bbbfa86337e70f1139dc2.tar.bz2 |
[SVE] Add support to vectorize_width loop pragma for scalable vectors
This patch adds support for two new variants of the vectorize_width
pragma:
1. vectorize_width(X[, fixed|scalable]) where an optional second
parameter is passed to the vectorize_width pragma, which indicates if
the user wishes to use fixed width or scalable vectorization. For
example the user can now write something like:
#pragma clang loop vectorize_width(4, fixed)
or
#pragma clang loop vectorize_width(4, scalable)
In the absence of a second parameter it is assumed the user wants
fixed width vectorization, in order to maintain compatibility with
existing code.
2. vectorize_width(fixed|scalable) where the width is left unspecified,
but the user hints what type of vectorization they prefer, either
fixed width or scalable.
I have implemented this by making use of the LLVM loop hint attribute:
llvm.loop.vectorize.scalable.enable
Tests were added to
clang/test/CodeGenCXX/pragma-loop.cpp
for both the 'fixed' and 'scalable' optional parameter.
See this thread for context: http://lists.llvm.org/pipermail/cfe-dev/2020-November/067262.html
Differential Revision: https://reviews.llvm.org/D89031
Diffstat (limited to 'clang/lib/CodeGen/CGLoopInfo.cpp')
-rw-r--r-- | clang/lib/CodeGen/CGLoopInfo.cpp | 52 |
1 files changed, 44 insertions, 8 deletions
diff --git a/clang/lib/CodeGen/CGLoopInfo.cpp b/clang/lib/CodeGen/CGLoopInfo.cpp index be4993e..8ba4059 100644 --- a/clang/lib/CodeGen/CGLoopInfo.cpp +++ b/clang/lib/CodeGen/CGLoopInfo.cpp @@ -217,7 +217,8 @@ LoopInfo::createLoopVectorizeMetadata(const LoopAttributes &Attrs, Enabled = false; else if (Attrs.VectorizeEnable != LoopAttributes::Unspecified || Attrs.VectorizePredicateEnable != LoopAttributes::Unspecified || - Attrs.InterleaveCount != 0 || Attrs.VectorizeWidth != 0) + Attrs.InterleaveCount != 0 || Attrs.VectorizeWidth != 0 || + Attrs.VectorizeScalable != LoopAttributes::Unspecified) Enabled = true; if (Enabled != true) { @@ -271,6 +272,16 @@ LoopInfo::createLoopVectorizeMetadata(const LoopAttributes &Attrs, MDString::get(Ctx, "llvm.loop.vectorize.width"), ConstantAsMetadata::get(ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Attrs.VectorizeWidth))}; + + 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))}; Args.push_back(MDNode::get(Ctx, Vals)); } @@ -286,10 +297,16 @@ LoopInfo::createLoopVectorizeMetadata(const LoopAttributes &Attrs, // vectorize.enable is set if: // 1) loop hint vectorize.enable is set, or // 2) it is implied when vectorize.predicate is set, or - // 3) it is implied when vectorize.width is set. + // 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. if (Attrs.VectorizeEnable != LoopAttributes::Unspecified || - IsVectorPredicateEnabled || - Attrs.VectorizeWidth > 1 ) { + IsVectorPredicateEnabled || Attrs.VectorizeWidth > 1 || + Attrs.VectorizeScalable == LoopAttributes::Enable || + (Attrs.VectorizeScalable == LoopAttributes::Disable && + Attrs.VectorizeWidth != 1)) { bool AttrVal = Attrs.VectorizeEnable != LoopAttributes::Disable; Args.push_back( MDNode::get(Ctx, {MDString::get(Ctx, "llvm.loop.vectorize.enable"), @@ -433,13 +450,15 @@ LoopAttributes::LoopAttributes(bool IsParallel) UnrollEnable(LoopAttributes::Unspecified), UnrollAndJamEnable(LoopAttributes::Unspecified), VectorizePredicateEnable(LoopAttributes::Unspecified), VectorizeWidth(0), - InterleaveCount(0), UnrollCount(0), UnrollAndJamCount(0), + VectorizeScalable(LoopAttributes::Unspecified), InterleaveCount(0), + UnrollCount(0), UnrollAndJamCount(0), DistributeEnable(LoopAttributes::Unspecified), PipelineDisabled(false), PipelineInitiationInterval(0), MustProgress(false) {} void LoopAttributes::clear() { IsParallel = false; VectorizeWidth = 0; + VectorizeScalable = LoopAttributes::Unspecified; InterleaveCount = 0; UnrollCount = 0; UnrollAndJamCount = 0; @@ -466,6 +485,7 @@ LoopInfo::LoopInfo(BasicBlock *Header, const LoopAttributes &Attrs, } if (!Attrs.IsParallel && Attrs.VectorizeWidth == 0 && + Attrs.VectorizeScalable == LoopAttributes::Unspecified && Attrs.InterleaveCount == 0 && Attrs.UnrollCount == 0 && Attrs.UnrollAndJamCount == 0 && !Attrs.PipelineDisabled && Attrs.PipelineInitiationInterval == 0 && @@ -501,6 +521,7 @@ void LoopInfo::finish() { BeforeJam.IsParallel = AfterJam.IsParallel = Attrs.IsParallel; BeforeJam.VectorizeWidth = Attrs.VectorizeWidth; + BeforeJam.VectorizeScalable = Attrs.VectorizeScalable; BeforeJam.InterleaveCount = Attrs.InterleaveCount; BeforeJam.VectorizeEnable = Attrs.VectorizeEnable; BeforeJam.DistributeEnable = Attrs.DistributeEnable; @@ -543,7 +564,8 @@ void LoopInfo::finish() { SmallVector<Metadata *, 1> BeforeLoopProperties; if (BeforeJam.VectorizeEnable != LoopAttributes::Unspecified || BeforeJam.VectorizePredicateEnable != LoopAttributes::Unspecified || - BeforeJam.InterleaveCount != 0 || BeforeJam.VectorizeWidth != 0) + BeforeJam.InterleaveCount != 0 || BeforeJam.VectorizeWidth != 0 || + BeforeJam.VectorizeScalable == LoopAttributes::Enable) BeforeLoopProperties.push_back( MDNode::get(Ctx, MDString::get(Ctx, "llvm.loop.isvectorized"))); @@ -620,6 +642,7 @@ void LoopInfoStack::push(BasicBlock *Header, clang::ASTContext &Ctx, case LoopHintAttr::Vectorize: // Disable vectorization by specifying a width of 1. setVectorizeWidth(1); + setVectorizeScalable(LoopAttributes::Unspecified); break; case LoopHintAttr::Interleave: // Disable interleaving by speciyfing a count of 1. @@ -721,11 +744,23 @@ void LoopInfoStack::push(BasicBlock *Header, clang::ASTContext &Ctx, break; } break; - case LoopHintAttr::Numeric: + case LoopHintAttr::FixedWidth: + case LoopHintAttr::ScalableWidth: switch (Option) { case LoopHintAttr::VectorizeWidth: - setVectorizeWidth(ValueInt); + setVectorizeScalable(State == LoopHintAttr::ScalableWidth + ? LoopAttributes::Enable + : LoopAttributes::Disable); + if (LH->getValue()) + setVectorizeWidth(ValueInt); break; + default: + llvm_unreachable("Options cannot be used with 'scalable' hint."); + break; + } + break; + case LoopHintAttr::Numeric: + switch (Option) { case LoopHintAttr::InterleaveCount: setInterleaveCount(ValueInt); break; @@ -742,6 +777,7 @@ void LoopInfoStack::push(BasicBlock *Header, clang::ASTContext &Ctx, case LoopHintAttr::UnrollAndJam: case LoopHintAttr::VectorizePredicate: case LoopHintAttr::Vectorize: + case LoopHintAttr::VectorizeWidth: case LoopHintAttr::Interleave: case LoopHintAttr::Distribute: case LoopHintAttr::PipelineDisabled: |