@@ -22,20 +22,20 @@ using namespace clang::CodeGen;
2222using namespace llvm ;
2323
2424MDNode *
25- LoopInfo::createLoopPropertiesMetadata (ArrayRef<Metadata *> LoopProperties) {
25+ LoopInfo::createFollowupMetadata (const char *FollowupName,
26+ ArrayRef<llvm::Metadata *> LoopProperties) {
2627 LLVMContext &Ctx = Header->getContext ();
27- SmallVector<Metadata *, 4 > NewLoopProperties;
28- NewLoopProperties.push_back (nullptr );
29- NewLoopProperties.append (LoopProperties.begin (), LoopProperties.end ());
3028
31- MDNode *LoopID = MDNode::getDistinct (Ctx, NewLoopProperties);
32- LoopID->replaceOperandWith (0 , LoopID);
33- return LoopID;
29+ SmallVector<Metadata *, 4 > Args;
30+ Args.push_back (MDString::get (Ctx, FollowupName));
31+ Args.append (LoopProperties.begin (), LoopProperties.end ());
32+ return MDNode::get (Ctx, Args);
3433}
3534
36- MDNode *LoopInfo::createPipeliningMetadata (const LoopAttributes &Attrs,
37- ArrayRef<Metadata *> LoopProperties,
38- bool &HasUserTransforms) {
35+ SmallVector<Metadata *, 4 >
36+ LoopInfo::createPipeliningMetadata (const LoopAttributes &Attrs,
37+ ArrayRef<Metadata *> LoopProperties,
38+ bool &HasUserTransforms) {
3939 LLVMContext &Ctx = Header->getContext ();
4040
4141 std::optional<bool > Enabled;
@@ -44,23 +44,19 @@ MDNode *LoopInfo::createPipeliningMetadata(const LoopAttributes &Attrs,
4444 else if (Attrs.PipelineInitiationInterval != 0 )
4545 Enabled = true ;
4646
47+ SmallVector<Metadata *, 4 > Args;
48+ Args.append (LoopProperties.begin (), LoopProperties.end ());
49+
4750 if (Enabled != true ) {
48- SmallVector<Metadata *, 4 > NewLoopProperties;
4951 if (Enabled == false ) {
50- NewLoopProperties.append (LoopProperties.begin (), LoopProperties.end ());
51- NewLoopProperties.push_back (
52+ Args.push_back (
5253 MDNode::get (Ctx, {MDString::get (Ctx, " llvm.loop.pipeline.disable" ),
5354 ConstantAsMetadata::get (ConstantInt::get (
5455 llvm::Type::getInt1Ty (Ctx), 1 ))}));
55- LoopProperties = NewLoopProperties;
5656 }
57- return createLoopPropertiesMetadata (LoopProperties) ;
57+ return Args ;
5858 }
5959
60- SmallVector<Metadata *, 4 > Args;
61- Args.push_back (nullptr );
62- Args.append (LoopProperties.begin (), LoopProperties.end ());
63-
6460 if (Attrs.PipelineInitiationInterval > 0 ) {
6561 Metadata *Vals[] = {
6662 MDString::get (Ctx, " llvm.loop.pipeline.initiationinterval" ),
@@ -71,13 +67,11 @@ MDNode *LoopInfo::createPipeliningMetadata(const LoopAttributes &Attrs,
7167
7268 // No follow-up: This is the last transformation.
7369
74- MDNode *LoopID = MDNode::getDistinct (Ctx, Args);
75- LoopID->replaceOperandWith (0 , LoopID);
7670 HasUserTransforms = true ;
77- return LoopID ;
71+ return Args ;
7872}
7973
80- MDNode *
74+ SmallVector<Metadata *, 4 >
8175LoopInfo::createPartialUnrollMetadata (const LoopAttributes &Attrs,
8276 ArrayRef<Metadata *> LoopProperties,
8377 bool &HasUserTransforms) {
@@ -108,11 +102,10 @@ LoopInfo::createPartialUnrollMetadata(const LoopAttributes &Attrs,
108102 MDNode::get (Ctx, MDString::get (Ctx, " llvm.loop.unroll.disable" )));
109103
110104 bool FollowupHasTransforms = false ;
111- MDNode * Followup = createPipeliningMetadata (Attrs, FollowupLoopProperties,
112- FollowupHasTransforms);
105+ SmallVector<Metadata *, 4 > Followup = createPipeliningMetadata (
106+ Attrs, FollowupLoopProperties, FollowupHasTransforms);
113107
114108 SmallVector<Metadata *, 4 > Args;
115- Args.push_back (nullptr );
116109 Args.append (LoopProperties.begin (), LoopProperties.end ());
117110
118111 // Setting unroll.count
@@ -130,16 +123,14 @@ LoopInfo::createPartialUnrollMetadata(const LoopAttributes &Attrs,
130123 }
131124
132125 if (FollowupHasTransforms)
133- Args.push_back (MDNode::get (
134- Ctx, { MDString::get (Ctx, " llvm.loop.unroll.followup_all" ) , Followup} ));
126+ Args.push_back (
127+ createFollowupMetadata ( " llvm.loop.unroll.followup_all" , Followup));
135128
136- MDNode *LoopID = MDNode::getDistinct (Ctx, Args);
137- LoopID->replaceOperandWith (0 , LoopID);
138129 HasUserTransforms = true ;
139- return LoopID ;
130+ return Args ;
140131}
141132
142- MDNode *
133+ SmallVector<Metadata *, 4 >
143134LoopInfo::createUnrollAndJamMetadata (const LoopAttributes &Attrs,
144135 ArrayRef<Metadata *> LoopProperties,
145136 bool &HasUserTransforms) {
@@ -170,11 +161,10 @@ LoopInfo::createUnrollAndJamMetadata(const LoopAttributes &Attrs,
170161 MDNode::get (Ctx, MDString::get (Ctx, " llvm.loop.unroll_and_jam.disable" )));
171162
172163 bool FollowupHasTransforms = false ;
173- MDNode * Followup = createPartialUnrollMetadata (Attrs, FollowupLoopProperties,
174- FollowupHasTransforms);
164+ SmallVector<Metadata *, 4 > Followup = createPartialUnrollMetadata (
165+ Attrs, FollowupLoopProperties, FollowupHasTransforms);
175166
176167 SmallVector<Metadata *, 4 > Args;
177- Args.push_back (nullptr );
178168 Args.append (LoopProperties.begin (), LoopProperties.end ());
179169
180170 // Setting unroll_and_jam.count
@@ -192,22 +182,18 @@ LoopInfo::createUnrollAndJamMetadata(const LoopAttributes &Attrs,
192182 }
193183
194184 if (FollowupHasTransforms)
195- Args.push_back (MDNode::get (
196- Ctx, {MDString::get (Ctx, " llvm.loop.unroll_and_jam.followup_outer" ),
197- Followup}));
185+ Args.push_back (createFollowupMetadata (
186+ " llvm.loop.unroll_and_jam.followup_outer" , Followup));
198187
199- if (UnrollAndJamInnerFollowup)
200- Args.push_back (MDNode::get (
201- Ctx, {MDString::get (Ctx, " llvm.loop.unroll_and_jam.followup_inner" ),
202- UnrollAndJamInnerFollowup}));
188+ if (UnrollAndJamInnerFollowup.has_value ())
189+ Args.push_back (createFollowupMetadata (
190+ " llvm.loop.unroll_and_jam.followup_inner" , *UnrollAndJamInnerFollowup));
203191
204- MDNode *LoopID = MDNode::getDistinct (Ctx, Args);
205- LoopID->replaceOperandWith (0 , LoopID);
206192 HasUserTransforms = true ;
207- return LoopID ;
193+ return Args ;
208194}
209195
210- MDNode *
196+ SmallVector<Metadata *, 4 >
211197LoopInfo::createLoopVectorizeMetadata (const LoopAttributes &Attrs,
212198 ArrayRef<Metadata *> LoopProperties,
213199 bool &HasUserTransforms) {
@@ -244,11 +230,10 @@ LoopInfo::createLoopVectorizeMetadata(const LoopAttributes &Attrs,
244230 MDNode::get (Ctx, MDString::get (Ctx, " llvm.loop.isvectorized" )));
245231
246232 bool FollowupHasTransforms = false ;
247- MDNode * Followup = createUnrollAndJamMetadata (Attrs, FollowupLoopProperties,
248- FollowupHasTransforms);
233+ SmallVector<Metadata *, 4 > Followup = createUnrollAndJamMetadata (
234+ Attrs, FollowupLoopProperties, FollowupHasTransforms);
249235
250236 SmallVector<Metadata *, 4 > Args;
251- Args.push_back (nullptr );
252237 Args.append (LoopProperties.begin (), LoopProperties.end ());
253238
254239 // Setting vectorize.predicate when it has been specified and vectorization
@@ -315,17 +300,14 @@ LoopInfo::createLoopVectorizeMetadata(const LoopAttributes &Attrs,
315300 }
316301
317302 if (FollowupHasTransforms)
318- Args.push_back (MDNode::get (
319- Ctx,
320- {MDString::get (Ctx, " llvm.loop.vectorize.followup_all" ), Followup}));
303+ Args.push_back (
304+ createFollowupMetadata (" llvm.loop.vectorize.followup_all" , Followup));
321305
322- MDNode *LoopID = MDNode::getDistinct (Ctx, Args);
323- LoopID->replaceOperandWith (0 , LoopID);
324306 HasUserTransforms = true ;
325- return LoopID ;
307+ return Args ;
326308}
327309
328- MDNode *
310+ SmallVector<Metadata *, 4 >
329311LoopInfo::createLoopDistributeMetadata (const LoopAttributes &Attrs,
330312 ArrayRef<Metadata *> LoopProperties,
331313 bool &HasUserTransforms) {
@@ -352,11 +334,10 @@ LoopInfo::createLoopDistributeMetadata(const LoopAttributes &Attrs,
352334 }
353335
354336 bool FollowupHasTransforms = false ;
355- MDNode * Followup =
337+ SmallVector<Metadata *, 4 > Followup =
356338 createLoopVectorizeMetadata (Attrs, LoopProperties, FollowupHasTransforms);
357339
358340 SmallVector<Metadata *, 4 > Args;
359- Args.push_back (nullptr );
360341 Args.append (LoopProperties.begin (), LoopProperties.end ());
361342
362343 Metadata *Vals[] = {MDString::get (Ctx, " llvm.loop.distribute.enable" ),
@@ -366,19 +347,17 @@ LoopInfo::createLoopDistributeMetadata(const LoopAttributes &Attrs,
366347 Args.push_back (MDNode::get (Ctx, Vals));
367348
368349 if (FollowupHasTransforms)
369- Args.push_back (MDNode::get (
370- Ctx,
371- {MDString::get (Ctx, " llvm.loop.distribute.followup_all" ), Followup}));
350+ Args.push_back (
351+ createFollowupMetadata (" llvm.loop.distribute.followup_all" , Followup));
372352
373- MDNode *LoopID = MDNode::getDistinct (Ctx, Args);
374- LoopID->replaceOperandWith (0 , LoopID);
375353 HasUserTransforms = true ;
376- return LoopID ;
354+ return Args ;
377355}
378356
379- MDNode *LoopInfo::createFullUnrollMetadata (const LoopAttributes &Attrs,
380- ArrayRef<Metadata *> LoopProperties,
381- bool &HasUserTransforms) {
357+ SmallVector<Metadata *, 4 >
358+ LoopInfo::createFullUnrollMetadata (const LoopAttributes &Attrs,
359+ ArrayRef<Metadata *> LoopProperties,
360+ bool &HasUserTransforms) {
382361 LLVMContext &Ctx = Header->getContext ();
383362
384363 std::optional<bool > Enabled;
@@ -400,20 +379,17 @@ MDNode *LoopInfo::createFullUnrollMetadata(const LoopAttributes &Attrs,
400379 }
401380
402381 SmallVector<Metadata *, 4 > Args;
403- Args.push_back (nullptr );
404382 Args.append (LoopProperties.begin (), LoopProperties.end ());
405383 Args.push_back (MDNode::get (Ctx, MDString::get (Ctx, " llvm.loop.unroll.full" )));
406384
407385 // No follow-up: there is no loop after full unrolling.
408386 // TODO: Warn if there are transformations after full unrolling.
409387
410- MDNode *LoopID = MDNode::getDistinct (Ctx, Args);
411- LoopID->replaceOperandWith (0 , LoopID);
412388 HasUserTransforms = true ;
413- return LoopID ;
389+ return Args ;
414390}
415391
416- MDNode * LoopInfo::createMetadata (
392+ SmallVector<Metadata *, 4 > LoopInfo::createMetadata (
417393 const LoopAttributes &Attrs,
418394 llvm::ArrayRef<llvm::Metadata *> AdditionalLoopProperties,
419395 bool &HasUserTransforms) {
@@ -579,8 +555,8 @@ void LoopInfo::finish() {
579555 MDNode::get (Ctx, MDString::get (Ctx, " llvm.loop.isvectorized" )));
580556
581557 bool InnerFollowupHasTransform = false ;
582- MDNode * InnerFollowup = createMetadata (AfterJam, BeforeLoopProperties,
583- InnerFollowupHasTransform);
558+ SmallVector<Metadata *, 4 > InnerFollowup = createMetadata (
559+ AfterJam, BeforeLoopProperties, InnerFollowupHasTransform);
584560 if (InnerFollowupHasTransform)
585561 Parent->UnrollAndJamInnerFollowup = InnerFollowup;
586562 }
@@ -589,7 +565,14 @@ void LoopInfo::finish() {
589565 }
590566
591567 bool HasUserTransforms = false ;
592- LoopID = createMetadata (CurLoopAttr, {}, HasUserTransforms);
568+ SmallVector<Metadata *, 4 > Properties =
569+ createMetadata (CurLoopAttr, {}, HasUserTransforms);
570+ SmallVector<Metadata *, 4 > Args;
571+ Args.push_back (nullptr );
572+ Args.append (Properties.begin (), Properties.end ());
573+ LoopID = MDNode::getDistinct (Ctx, Args);
574+ LoopID->replaceOperandWith (0 , LoopID);
575+
593576 TempLoopID->replaceAllUsesWith (LoopID);
594577}
595578
0 commit comments