diff options
author | Ryotaro Kasuga <kasuga.ryotaro@fujitsu.com> | 2025-03-27 20:29:37 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-03-27 20:29:37 +0900 |
commit | 6c56a842b73cf2046b9ab8cf077890e11b758373 (patch) | |
tree | 700fba2f3391a2ae886f4f0b489679d20335364b /clang/lib/CodeGen/CGLoopInfo.cpp | |
parent | 3284559cca4bc64e78e8243bb34195216e8979ee (diff) | |
download | llvm-6c56a842b73cf2046b9ab8cf077890e11b758373.zip llvm-6c56a842b73cf2046b9ab8cf077890e11b758373.tar.gz llvm-6c56a842b73cf2046b9ab8cf077890e11b758373.tar.bz2 |
[clang][CodeGen] Generate follow-up metadata for loops in correct format (#131985)
When pragma of loop transformations is specified, follow-up metadata for
loops is generated after each transformation. On the LLVM side,
follow-up metadata is expected to be a list of properties, such as the
following:
```
!followup = !{!"llvm.loop.vectorize.followup_all", !mp, !isvectorized}
!mp = !{!"llvm.loop.mustprogress"}
!isvectorized = !{"llvm.loop.isvectorized"}
```
However, on the clang side, the generated metadata contains an MDNode
that has those properties, as shown below:
```
!followup = !{!"llvm.loop.vectorize.followup_all", !loop_id}
!loop_id = distinct !{!loop_id, !mp, !isvectorized}
!mp = !{!"llvm.loop.mustprogress"}
!isvectorized = !{"llvm.loop.isvectorized"}
```
According to the
[LangRef](https://llvm.org/docs/TransformMetadata.html#transformation-metadata-structure),
the LLVM side is correct. Due to this inconsistency, follow-up metadata
was not interpreted correctly, e.g., only one transformation is applied
when multiple pragmas are used.
This patch fixes clang side to emit followup metadata in correct format.
Diffstat (limited to 'clang/lib/CodeGen/CGLoopInfo.cpp')
-rw-r--r-- | clang/lib/CodeGen/CGLoopInfo.cpp | 133 |
1 files changed, 58 insertions, 75 deletions
diff --git a/clang/lib/CodeGen/CGLoopInfo.cpp b/clang/lib/CodeGen/CGLoopInfo.cpp index 4485712..2b7d788 100644 --- a/clang/lib/CodeGen/CGLoopInfo.cpp +++ b/clang/lib/CodeGen/CGLoopInfo.cpp @@ -22,20 +22,20 @@ using namespace clang::CodeGen; using namespace llvm; MDNode * -LoopInfo::createLoopPropertiesMetadata(ArrayRef<Metadata *> LoopProperties) { +LoopInfo::createFollowupMetadata(const char *FollowupName, + ArrayRef<llvm::Metadata *> LoopProperties) { LLVMContext &Ctx = Header->getContext(); - SmallVector<Metadata *, 4> NewLoopProperties; - NewLoopProperties.push_back(nullptr); - NewLoopProperties.append(LoopProperties.begin(), LoopProperties.end()); - MDNode *LoopID = MDNode::getDistinct(Ctx, NewLoopProperties); - LoopID->replaceOperandWith(0, LoopID); - return LoopID; + SmallVector<Metadata *, 4> Args; + Args.push_back(MDString::get(Ctx, FollowupName)); + Args.append(LoopProperties.begin(), LoopProperties.end()); + return MDNode::get(Ctx, Args); } -MDNode *LoopInfo::createPipeliningMetadata(const LoopAttributes &Attrs, - ArrayRef<Metadata *> LoopProperties, - bool &HasUserTransforms) { +SmallVector<Metadata *, 4> +LoopInfo::createPipeliningMetadata(const LoopAttributes &Attrs, + ArrayRef<Metadata *> LoopProperties, + bool &HasUserTransforms) { LLVMContext &Ctx = Header->getContext(); std::optional<bool> Enabled; @@ -44,23 +44,19 @@ MDNode *LoopInfo::createPipeliningMetadata(const LoopAttributes &Attrs, else if (Attrs.PipelineInitiationInterval != 0) Enabled = true; + SmallVector<Metadata *, 4> Args; + Args.append(LoopProperties.begin(), LoopProperties.end()); + if (Enabled != true) { - SmallVector<Metadata *, 4> NewLoopProperties; if (Enabled == false) { - NewLoopProperties.append(LoopProperties.begin(), LoopProperties.end()); - NewLoopProperties.push_back( + Args.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); + return Args; } - SmallVector<Metadata *, 4> Args; - Args.push_back(nullptr); - Args.append(LoopProperties.begin(), LoopProperties.end()); - if (Attrs.PipelineInitiationInterval > 0) { Metadata *Vals[] = { MDString::get(Ctx, "llvm.loop.pipeline.initiationinterval"), @@ -71,13 +67,11 @@ MDNode *LoopInfo::createPipeliningMetadata(const LoopAttributes &Attrs, // No follow-up: This is the last transformation. - MDNode *LoopID = MDNode::getDistinct(Ctx, Args); - LoopID->replaceOperandWith(0, LoopID); HasUserTransforms = true; - return LoopID; + return Args; } -MDNode * +SmallVector<Metadata *, 4> LoopInfo::createPartialUnrollMetadata(const LoopAttributes &Attrs, ArrayRef<Metadata *> LoopProperties, bool &HasUserTransforms) { @@ -108,11 +102,10 @@ LoopInfo::createPartialUnrollMetadata(const LoopAttributes &Attrs, MDNode::get(Ctx, MDString::get(Ctx, "llvm.loop.unroll.disable"))); bool FollowupHasTransforms = false; - MDNode *Followup = createPipeliningMetadata(Attrs, FollowupLoopProperties, - FollowupHasTransforms); + SmallVector<Metadata *, 4> Followup = createPipeliningMetadata( + Attrs, FollowupLoopProperties, FollowupHasTransforms); SmallVector<Metadata *, 4> Args; - Args.push_back(nullptr); Args.append(LoopProperties.begin(), LoopProperties.end()); // Setting unroll.count @@ -130,16 +123,14 @@ LoopInfo::createPartialUnrollMetadata(const LoopAttributes &Attrs, } if (FollowupHasTransforms) - Args.push_back(MDNode::get( - Ctx, {MDString::get(Ctx, "llvm.loop.unroll.followup_all"), Followup})); + Args.push_back( + createFollowupMetadata("llvm.loop.unroll.followup_all", Followup)); - MDNode *LoopID = MDNode::getDistinct(Ctx, Args); - LoopID->replaceOperandWith(0, LoopID); HasUserTransforms = true; - return LoopID; + return Args; } -MDNode * +SmallVector<Metadata *, 4> LoopInfo::createUnrollAndJamMetadata(const LoopAttributes &Attrs, ArrayRef<Metadata *> LoopProperties, bool &HasUserTransforms) { @@ -170,11 +161,10 @@ LoopInfo::createUnrollAndJamMetadata(const LoopAttributes &Attrs, MDNode::get(Ctx, MDString::get(Ctx, "llvm.loop.unroll_and_jam.disable"))); bool FollowupHasTransforms = false; - MDNode *Followup = createPartialUnrollMetadata(Attrs, FollowupLoopProperties, - FollowupHasTransforms); + SmallVector<Metadata *, 4> Followup = createPartialUnrollMetadata( + Attrs, FollowupLoopProperties, FollowupHasTransforms); SmallVector<Metadata *, 4> Args; - Args.push_back(nullptr); Args.append(LoopProperties.begin(), LoopProperties.end()); // Setting unroll_and_jam.count @@ -192,22 +182,18 @@ LoopInfo::createUnrollAndJamMetadata(const LoopAttributes &Attrs, } if (FollowupHasTransforms) - Args.push_back(MDNode::get( - Ctx, {MDString::get(Ctx, "llvm.loop.unroll_and_jam.followup_outer"), - Followup})); + Args.push_back(createFollowupMetadata( + "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})); + if (UnrollAndJamInnerFollowup.has_value()) + Args.push_back(createFollowupMetadata( + "llvm.loop.unroll_and_jam.followup_inner", *UnrollAndJamInnerFollowup)); - MDNode *LoopID = MDNode::getDistinct(Ctx, Args); - LoopID->replaceOperandWith(0, LoopID); HasUserTransforms = true; - return LoopID; + return Args; } -MDNode * +SmallVector<Metadata *, 4> LoopInfo::createLoopVectorizeMetadata(const LoopAttributes &Attrs, ArrayRef<Metadata *> LoopProperties, bool &HasUserTransforms) { @@ -244,11 +230,10 @@ LoopInfo::createLoopVectorizeMetadata(const LoopAttributes &Attrs, MDNode::get(Ctx, MDString::get(Ctx, "llvm.loop.isvectorized"))); bool FollowupHasTransforms = false; - MDNode *Followup = createUnrollAndJamMetadata(Attrs, FollowupLoopProperties, - FollowupHasTransforms); + SmallVector<Metadata *, 4> Followup = createUnrollAndJamMetadata( + Attrs, FollowupLoopProperties, FollowupHasTransforms); SmallVector<Metadata *, 4> Args; - Args.push_back(nullptr); Args.append(LoopProperties.begin(), LoopProperties.end()); // Setting vectorize.predicate when it has been specified and vectorization @@ -315,17 +300,14 @@ LoopInfo::createLoopVectorizeMetadata(const LoopAttributes &Attrs, } if (FollowupHasTransforms) - Args.push_back(MDNode::get( - Ctx, - {MDString::get(Ctx, "llvm.loop.vectorize.followup_all"), Followup})); + Args.push_back( + createFollowupMetadata("llvm.loop.vectorize.followup_all", Followup)); - MDNode *LoopID = MDNode::getDistinct(Ctx, Args); - LoopID->replaceOperandWith(0, LoopID); HasUserTransforms = true; - return LoopID; + return Args; } -MDNode * +SmallVector<Metadata *, 4> LoopInfo::createLoopDistributeMetadata(const LoopAttributes &Attrs, ArrayRef<Metadata *> LoopProperties, bool &HasUserTransforms) { @@ -352,11 +334,10 @@ LoopInfo::createLoopDistributeMetadata(const LoopAttributes &Attrs, } bool FollowupHasTransforms = false; - MDNode *Followup = + SmallVector<Metadata *, 4> Followup = createLoopVectorizeMetadata(Attrs, LoopProperties, FollowupHasTransforms); SmallVector<Metadata *, 4> Args; - Args.push_back(nullptr); Args.append(LoopProperties.begin(), LoopProperties.end()); Metadata *Vals[] = {MDString::get(Ctx, "llvm.loop.distribute.enable"), @@ -366,19 +347,17 @@ LoopInfo::createLoopDistributeMetadata(const LoopAttributes &Attrs, Args.push_back(MDNode::get(Ctx, Vals)); if (FollowupHasTransforms) - Args.push_back(MDNode::get( - Ctx, - {MDString::get(Ctx, "llvm.loop.distribute.followup_all"), Followup})); + Args.push_back( + createFollowupMetadata("llvm.loop.distribute.followup_all", Followup)); - MDNode *LoopID = MDNode::getDistinct(Ctx, Args); - LoopID->replaceOperandWith(0, LoopID); HasUserTransforms = true; - return LoopID; + return Args; } -MDNode *LoopInfo::createFullUnrollMetadata(const LoopAttributes &Attrs, - ArrayRef<Metadata *> LoopProperties, - bool &HasUserTransforms) { +SmallVector<Metadata *, 4> +LoopInfo::createFullUnrollMetadata(const LoopAttributes &Attrs, + ArrayRef<Metadata *> LoopProperties, + bool &HasUserTransforms) { LLVMContext &Ctx = Header->getContext(); std::optional<bool> Enabled; @@ -400,20 +379,17 @@ MDNode *LoopInfo::createFullUnrollMetadata(const LoopAttributes &Attrs, } SmallVector<Metadata *, 4> Args; - Args.push_back(nullptr); 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; - return LoopID; + return Args; } -MDNode *LoopInfo::createMetadata( +SmallVector<Metadata *, 4> LoopInfo::createMetadata( const LoopAttributes &Attrs, llvm::ArrayRef<llvm::Metadata *> AdditionalLoopProperties, bool &HasUserTransforms) { @@ -579,8 +555,8 @@ void LoopInfo::finish() { MDNode::get(Ctx, MDString::get(Ctx, "llvm.loop.isvectorized"))); bool InnerFollowupHasTransform = false; - MDNode *InnerFollowup = createMetadata(AfterJam, BeforeLoopProperties, - InnerFollowupHasTransform); + SmallVector<Metadata *, 4> InnerFollowup = createMetadata( + AfterJam, BeforeLoopProperties, InnerFollowupHasTransform); if (InnerFollowupHasTransform) Parent->UnrollAndJamInnerFollowup = InnerFollowup; } @@ -589,7 +565,14 @@ void LoopInfo::finish() { } bool HasUserTransforms = false; - LoopID = createMetadata(CurLoopAttr, {}, HasUserTransforms); + SmallVector<Metadata *, 4> Properties = + createMetadata(CurLoopAttr, {}, HasUserTransforms); + SmallVector<Metadata *, 4> Args; + Args.push_back(nullptr); + Args.append(Properties.begin(), Properties.end()); + LoopID = MDNode::getDistinct(Ctx, Args); + LoopID->replaceOperandWith(0, LoopID); + TempLoopID->replaceAllUsesWith(LoopID); } |