From 5d1ae6346b15539421d055d3dfc94fabe0d7c558 Mon Sep 17 00:00:00 2001 From: Kazu Hirata Date: Sat, 19 Nov 2022 15:36:50 -0800 Subject: [PATCH] [Analysis] Teach getOptionalIntLoopAttribute to return std::optional (NFC) This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716 --- llvm/include/llvm/Analysis/LoopInfo.h | 5 +++-- llvm/lib/Analysis/LoopInfo.cpp | 8 ++++---- llvm/lib/Transforms/Scalar/WarnMissedTransforms.cpp | 2 +- llvm/lib/Transforms/Utils/LoopUtils.cpp | 10 +++++----- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/llvm/include/llvm/Analysis/LoopInfo.h b/llvm/include/llvm/Analysis/LoopInfo.h index eb0b9ef..865b17b6 100644 --- a/llvm/include/llvm/Analysis/LoopInfo.h +++ b/llvm/include/llvm/Analysis/LoopInfo.h @@ -49,6 +49,7 @@ #include "llvm/Pass.h" #include "llvm/Support/Allocator.h" #include +#include #include namespace llvm { @@ -1333,8 +1334,8 @@ Optional getOptionalBoolLoopAttribute(const Loop *TheLoop, bool getBooleanLoopAttribute(const Loop *TheLoop, StringRef Name); /// Find named metadata for a loop with an integer value. -llvm::Optional -getOptionalIntLoopAttribute(const Loop *TheLoop, StringRef Name); +std::optional getOptionalIntLoopAttribute(const Loop *TheLoop, + StringRef Name); /// Find named metadata for a loop with an integer value. Return \p Default if /// not set. diff --git a/llvm/lib/Analysis/LoopInfo.cpp b/llvm/lib/Analysis/LoopInfo.cpp index 90077aa..057fc93 100644 --- a/llvm/lib/Analysis/LoopInfo.cpp +++ b/llvm/lib/Analysis/LoopInfo.cpp @@ -1086,16 +1086,16 @@ bool llvm::getBooleanLoopAttribute(const Loop *TheLoop, StringRef Name) { return getOptionalBoolLoopAttribute(TheLoop, Name).value_or(false); } -llvm::Optional llvm::getOptionalIntLoopAttribute(const Loop *TheLoop, - StringRef Name) { +std::optional llvm::getOptionalIntLoopAttribute(const Loop *TheLoop, + StringRef Name) { const MDOperand *AttrMD = findStringMetadataForLoop(TheLoop, Name).value_or(nullptr); if (!AttrMD) - return None; + return std::nullopt; ConstantInt *IntMD = mdconst::extract_or_null(AttrMD->get()); if (!IntMD) - return None; + return std::nullopt; return IntMD->getSExtValue(); } diff --git a/llvm/lib/Transforms/Scalar/WarnMissedTransforms.cpp b/llvm/lib/Transforms/Scalar/WarnMissedTransforms.cpp index 8367e61c..eeed11c 100644 --- a/llvm/lib/Transforms/Scalar/WarnMissedTransforms.cpp +++ b/llvm/lib/Transforms/Scalar/WarnMissedTransforms.cpp @@ -50,7 +50,7 @@ static void warnAboutLeftoverTransformations(Loop *L, LLVM_DEBUG(dbgs() << "Leftover vectorization transformation\n"); Optional VectorizeWidth = getOptionalElementCountLoopAttribute(L); - Optional InterleaveCount = + std::optional InterleaveCount = getOptionalIntLoopAttribute(L, "llvm.loop.interleave.count"); if (!VectorizeWidth || VectorizeWidth->isVector()) diff --git a/llvm/lib/Transforms/Utils/LoopUtils.cpp b/llvm/lib/Transforms/Utils/LoopUtils.cpp index 636392a..b69735a 100644 --- a/llvm/lib/Transforms/Utils/LoopUtils.cpp +++ b/llvm/lib/Transforms/Utils/LoopUtils.cpp @@ -249,11 +249,11 @@ void llvm::addStringMetadataToLoop(Loop *TheLoop, const char *StringMD, Optional llvm::getOptionalElementCountLoopAttribute(const Loop *TheLoop) { - Optional Width = + std::optional Width = getOptionalIntLoopAttribute(TheLoop, "llvm.loop.vectorize.width"); if (Width) { - Optional IsScalable = getOptionalIntLoopAttribute( + std::optional IsScalable = getOptionalIntLoopAttribute( TheLoop, "llvm.loop.vectorize.scalable.enable"); return ElementCount::get(*Width, IsScalable.value_or(false)); } @@ -354,7 +354,7 @@ TransformationMode llvm::hasUnrollTransformation(const Loop *L) { if (getBooleanLoopAttribute(L, "llvm.loop.unroll.disable")) return TM_SuppressedByUser; - Optional Count = + std::optional Count = getOptionalIntLoopAttribute(L, "llvm.loop.unroll.count"); if (Count) return Count.value() == 1 ? TM_SuppressedByUser : TM_ForcedByUser; @@ -375,7 +375,7 @@ TransformationMode llvm::hasUnrollAndJamTransformation(const Loop *L) { if (getBooleanLoopAttribute(L, "llvm.loop.unroll_and_jam.disable")) return TM_SuppressedByUser; - Optional Count = + std::optional Count = getOptionalIntLoopAttribute(L, "llvm.loop.unroll_and_jam.count"); if (Count) return Count.value() == 1 ? TM_SuppressedByUser : TM_ForcedByUser; @@ -398,7 +398,7 @@ TransformationMode llvm::hasVectorizeTransformation(const Loop *L) { Optional VectorizeWidth = getOptionalElementCountLoopAttribute(L); - Optional InterleaveCount = + std::optional InterleaveCount = getOptionalIntLoopAttribute(L, "llvm.loop.interleave.count"); // 'Forcing' vector width and interleave count to one effectively disables -- 2.7.4