Skip to content

Commit 6ac26ad

Browse files
authored
[SYCL][FPGA] Refactor of statement attributes (#4136)
This patch 1. refactors six statement attributes to align with upstream: SYCLIntelFPGALoopCountAttr SYCLIntelFPGAInitiationIntervalAttr SYCLIntelFPGAMaxConcurrencyAttr SYCLIntelFPGAMaxInterleavingAttr SYCLIntelFPGASpeculatedIterationsAttr SYCLIntelFPGALoopCoalesceAttr 2. stores expression as ConstantExpr in Semantic Attributes 3. removes generic function "BuildSYCLIntelFPGALoopAttr" 4. updates codegen codes to use single variable for loop metadata and value. Signed-off-by: Soumi Manna <[email protected]>
1 parent 155acd1 commit 6ac26ad

File tree

7 files changed

+470
-193
lines changed

7 files changed

+470
-193
lines changed

clang/include/clang/Sema/Sema.h

Lines changed: 13 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2182,17 +2182,25 @@ class Sema final {
21822182
SYCLIntelFPGAIVDepAttr *
21832183
BuildSYCLIntelFPGAIVDepAttr(const AttributeCommonInfo &CI, Expr *Expr1,
21842184
Expr *Expr2);
2185-
template <typename FPGALoopAttrT>
2186-
FPGALoopAttrT *BuildSYCLIntelFPGALoopAttr(const AttributeCommonInfo &A,
2187-
Expr *E = nullptr);
2188-
21892185
LoopUnrollHintAttr *BuildLoopUnrollHintAttr(const AttributeCommonInfo &A,
21902186
Expr *E);
21912187
OpenCLUnrollHintAttr *
21922188
BuildOpenCLLoopUnrollHintAttr(const AttributeCommonInfo &A, Expr *E);
21932189

21942190
SYCLIntelFPGALoopCountAttr *
2195-
BuildSYCLIntelFPGALoopCount(const AttributeCommonInfo &CI, Expr *E);
2191+
BuildSYCLIntelFPGALoopCountAttr(const AttributeCommonInfo &CI, Expr *E);
2192+
SYCLIntelFPGAInitiationIntervalAttr *
2193+
BuildSYCLIntelFPGAInitiationIntervalAttr(const AttributeCommonInfo &CI,
2194+
Expr *E);
2195+
SYCLIntelFPGAMaxConcurrencyAttr *
2196+
BuildSYCLIntelFPGAMaxConcurrencyAttr(const AttributeCommonInfo &CI, Expr *E);
2197+
SYCLIntelFPGAMaxInterleavingAttr *
2198+
BuildSYCLIntelFPGAMaxInterleavingAttr(const AttributeCommonInfo &CI, Expr *E);
2199+
SYCLIntelFPGASpeculatedIterationsAttr *
2200+
BuildSYCLIntelFPGASpeculatedIterationsAttr(const AttributeCommonInfo &CI,
2201+
Expr *E);
2202+
SYCLIntelFPGALoopCoalesceAttr *
2203+
BuildSYCLIntelFPGALoopCoalesceAttr(const AttributeCommonInfo &CI, Expr *E);
21962204

21972205
bool CheckQualifiedFunctionForTypeId(QualType T, SourceLocation Loc);
21982206

@@ -13533,51 +13541,6 @@ void Sema::AddOneConstantPowerTwoValueAttr(Decl *D,
1353313541
D->addAttr(::new (Context) AttrType(Context, CI, E));
1353413542
}
1353513543

13536-
template <typename FPGALoopAttrT>
13537-
FPGALoopAttrT *Sema::BuildSYCLIntelFPGALoopAttr(const AttributeCommonInfo &A,
13538-
Expr *E) {
13539-
if (!E && !(A.getParsedKind() == ParsedAttr::AT_SYCLIntelFPGALoopCoalesce))
13540-
return nullptr;
13541-
13542-
if (E && !E->isInstantiationDependent()) {
13543-
Optional<llvm::APSInt> ArgVal = E->getIntegerConstantExpr(getASTContext());
13544-
13545-
if (!ArgVal) {
13546-
Diag(E->getExprLoc(), diag::err_attribute_argument_type)
13547-
<< A.getAttrName() << AANT_ArgumentIntegerConstant
13548-
<< E->getSourceRange();
13549-
return nullptr;
13550-
}
13551-
13552-
int Val = ArgVal->getSExtValue();
13553-
13554-
if (A.getParsedKind() == ParsedAttr::AT_SYCLIntelFPGAInitiationInterval ||
13555-
A.getParsedKind() == ParsedAttr::AT_SYCLIntelFPGALoopCoalesce) {
13556-
if (Val <= 0) {
13557-
Diag(E->getExprLoc(), diag::err_attribute_requires_positive_integer)
13558-
<< A.getAttrName() << /* positive */ 0;
13559-
return nullptr;
13560-
}
13561-
} else if (A.getParsedKind() ==
13562-
ParsedAttr::AT_SYCLIntelFPGAMaxConcurrency ||
13563-
A.getParsedKind() ==
13564-
ParsedAttr::AT_SYCLIntelFPGAMaxInterleaving ||
13565-
A.getParsedKind() ==
13566-
ParsedAttr::AT_SYCLIntelFPGASpeculatedIterations ||
13567-
A.getParsedKind() == ParsedAttr::AT_SYCLIntelFPGALoopCount) {
13568-
if (Val < 0) {
13569-
Diag(E->getExprLoc(), diag::err_attribute_requires_positive_integer)
13570-
<< A.getAttrName() << /* non-negative */ 1;
13571-
return nullptr;
13572-
}
13573-
} else {
13574-
llvm_unreachable("unknown sycl fpga loop attr");
13575-
}
13576-
}
13577-
13578-
return new (Context) FPGALoopAttrT(Context, A, E);
13579-
}
13580-
1358113544
/// RAII object that enters a new expression evaluation context.
1358213545
class EnterExpressionEvaluationContext {
1358313546
Sema &Actions;

clang/lib/CodeGen/CGLoopInfo.cpp

Lines changed: 47 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -552,11 +552,11 @@ MDNode *LoopInfo::createMetadata(
552552
}
553553

554554
// Setting max_concurrency attribute with number of threads
555-
if (Attrs.SYCLMaxConcurrencyEnable) {
556-
Metadata *Vals[] = {MDString::get(Ctx, "llvm.loop.max_concurrency.count"),
557-
ConstantAsMetadata::get(ConstantInt::get(
558-
llvm::Type::getInt32Ty(Ctx),
559-
Attrs.SYCLMaxConcurrencyNThreads))};
555+
if (Attrs.SYCLMaxConcurrencyNThreads) {
556+
Metadata *Vals[] = {
557+
MDString::get(Ctx, "llvm.loop.max_concurrency.count"),
558+
ConstantAsMetadata::get(ConstantInt::get(
559+
llvm::Type::getInt32Ty(Ctx), *Attrs.SYCLMaxConcurrencyNThreads))};
560560
LoopProperties.push_back(MDNode::get(Ctx, Vals));
561561
}
562562

@@ -582,11 +582,11 @@ MDNode *LoopInfo::createMetadata(
582582
LoopProperties.push_back(MDNode::get(Ctx, Vals));
583583
}
584584

585-
if (Attrs.SYCLMaxInterleavingEnable) {
585+
if (Attrs.SYCLMaxInterleavingNInvocations) {
586586
Metadata *Vals[] = {MDString::get(Ctx, "llvm.loop.max_interleaving.count"),
587587
ConstantAsMetadata::get(ConstantInt::get(
588588
llvm::Type::getInt32Ty(Ctx),
589-
Attrs.SYCLMaxInterleavingNInvocations))};
589+
*Attrs.SYCLMaxInterleavingNInvocations))};
590590
LoopProperties.push_back(MDNode::get(Ctx, Vals));
591591
}
592592

@@ -596,16 +596,16 @@ MDNode *LoopInfo::createMetadata(
596596
LoopProperties.push_back(MDNode::get(Ctx, Vals));
597597
}
598598

599-
if (Attrs.SYCLSpeculatedIterationsEnable) {
599+
if (Attrs.SYCLSpeculatedIterationsNIterations) {
600600
Metadata *Vals[] = {
601601
MDString::get(Ctx, "llvm.loop.intel.speculated.iterations.count"),
602602
ConstantAsMetadata::get(
603603
ConstantInt::get(llvm::Type::getInt32Ty(Ctx),
604-
Attrs.SYCLSpeculatedIterationsNIterations))};
604+
*Attrs.SYCLSpeculatedIterationsNIterations))};
605605
LoopProperties.push_back(MDNode::get(Ctx, Vals));
606606
}
607607

608-
for (auto &VC : Attrs.SYCLIntelFPGAVariantCount) {
608+
for (const auto &VC : Attrs.SYCLIntelFPGAVariantCount) {
609609
Metadata *Vals[] = {MDString::get(Ctx, VC.first),
610610
ConstantAsMetadata::get(ConstantInt::get(
611611
llvm::Type::getInt32Ty(Ctx), VC.second))};
@@ -622,15 +622,12 @@ LoopAttributes::LoopAttributes(bool IsParallel)
622622
UnrollAndJamEnable(LoopAttributes::Unspecified),
623623
VectorizePredicateEnable(LoopAttributes::Unspecified), VectorizeWidth(0),
624624
VectorizeScalable(LoopAttributes::Unspecified), InterleaveCount(0),
625-
SYCLIInterval(0), SYCLMaxConcurrencyEnable(false),
626-
SYCLMaxConcurrencyNThreads(0), SYCLLoopCoalesceEnable(false),
625+
SYCLIInterval(0), SYCLLoopCoalesceEnable(false),
627626
SYCLLoopCoalesceNLevels(0), SYCLLoopPipeliningDisable(false),
628-
SYCLMaxInterleavingEnable(false), SYCLMaxInterleavingNInvocations(0),
629-
SYCLSpeculatedIterationsEnable(false),
630-
SYCLSpeculatedIterationsNIterations(0), UnrollCount(0),
631-
UnrollAndJamCount(0), DistributeEnable(LoopAttributes::Unspecified),
632-
PipelineDisabled(false), PipelineInitiationInterval(0),
633-
SYCLNofusionEnable(false), MustProgress(false) {}
627+
UnrollCount(0), UnrollAndJamCount(0),
628+
DistributeEnable(LoopAttributes::Unspecified), PipelineDisabled(false),
629+
PipelineInitiationInterval(0), SYCLNofusionEnable(false),
630+
MustProgress(false) {}
634631

635632
void LoopAttributes::clear() {
636633
IsParallel = false;
@@ -640,15 +637,12 @@ void LoopAttributes::clear() {
640637
GlobalSYCLIVDepInfo.reset();
641638
ArraySYCLIVDepInfo.clear();
642639
SYCLIInterval = 0;
643-
SYCLMaxConcurrencyEnable = false;
644-
SYCLMaxConcurrencyNThreads = 0;
640+
SYCLMaxConcurrencyNThreads.reset();
645641
SYCLLoopCoalesceEnable = false;
646642
SYCLLoopCoalesceNLevels = 0;
647643
SYCLLoopPipeliningDisable = false;
648-
SYCLMaxInterleavingEnable = false;
649-
SYCLMaxInterleavingNInvocations = 0;
650-
SYCLSpeculatedIterationsEnable = false;
651-
SYCLSpeculatedIterationsNIterations = 0;
644+
SYCLMaxInterleavingNInvocations.reset();
645+
SYCLSpeculatedIterationsNIterations.reset();
652646
SYCLIntelFPGAVariantCount.clear();
653647
UnrollCount = 0;
654648
UnrollAndJamCount = 0;
@@ -679,14 +673,12 @@ LoopInfo::LoopInfo(BasicBlock *Header, const LoopAttributes &Attrs,
679673
Attrs.VectorizeScalable == LoopAttributes::Unspecified &&
680674
Attrs.InterleaveCount == 0 && !Attrs.GlobalSYCLIVDepInfo.hasValue() &&
681675
Attrs.ArraySYCLIVDepInfo.empty() && Attrs.SYCLIInterval == 0 &&
682-
Attrs.SYCLMaxConcurrencyEnable == false &&
676+
!Attrs.SYCLMaxConcurrencyNThreads &&
683677
Attrs.SYCLLoopCoalesceEnable == false &&
684678
Attrs.SYCLLoopCoalesceNLevels == 0 &&
685679
Attrs.SYCLLoopPipeliningDisable == false &&
686-
Attrs.SYCLMaxInterleavingEnable == false &&
687-
Attrs.SYCLMaxInterleavingNInvocations == 0 &&
688-
Attrs.SYCLSpeculatedIterationsEnable == false &&
689-
Attrs.SYCLSpeculatedIterationsNIterations == 0 &&
680+
!Attrs.SYCLMaxInterleavingNInvocations &&
681+
!Attrs.SYCLSpeculatedIterationsNIterations &&
690682
Attrs.SYCLIntelFPGAVariantCount.empty() && Attrs.UnrollCount == 0 &&
691683
Attrs.UnrollAndJamCount == 0 && !Attrs.PipelineDisabled &&
692684
Attrs.PipelineInitiationInterval == 0 &&
@@ -1025,59 +1017,60 @@ void LoopInfoStack::push(BasicBlock *Header, clang::ASTContext &Ctx,
10251017
IntelFPGAIVDep->getArrayDecl());
10261018

10271019
if (const auto *IntelFPGAII =
1028-
dyn_cast<SYCLIntelFPGAInitiationIntervalAttr>(A))
1029-
setSYCLIInterval(IntelFPGAII->getIntervalExpr()
1030-
->getIntegerConstantExpr(Ctx)
1031-
->getSExtValue());
1020+
dyn_cast<SYCLIntelFPGAInitiationIntervalAttr>(A)) {
1021+
const auto *CE = cast<ConstantExpr>(IntelFPGAII->getIntervalExpr());
1022+
llvm::APSInt ArgVal = CE->getResultAsAPSInt();
1023+
setSYCLIInterval(ArgVal.getSExtValue());
1024+
}
10321025

10331026
if (const auto *IntelFPGAMaxConcurrency =
10341027
dyn_cast<SYCLIntelFPGAMaxConcurrencyAttr>(A)) {
1035-
setSYCLMaxConcurrencyEnable();
1036-
setSYCLMaxConcurrencyNThreads(IntelFPGAMaxConcurrency->getNThreadsExpr()
1037-
->getIntegerConstantExpr(Ctx)
1038-
->getSExtValue());
1028+
const auto *CE =
1029+
cast<ConstantExpr>(IntelFPGAMaxConcurrency->getNThreadsExpr());
1030+
llvm::APSInt ArgVal = CE->getResultAsAPSInt();
1031+
setSYCLMaxConcurrencyNThreads(ArgVal.getSExtValue());
10391032
}
10401033

10411034
if (const auto *IntelFPGALoopCountAvg =
10421035
dyn_cast<SYCLIntelFPGALoopCountAttr>(A)) {
1043-
unsigned int Count = IntelFPGALoopCountAvg->getNTripCount()
1044-
->getIntegerConstantExpr(Ctx)
1045-
->getSExtValue();
1036+
const auto *CE =
1037+
cast<ConstantExpr>(IntelFPGALoopCountAvg->getNTripCount());
1038+
llvm::APSInt ArgVal = CE->getResultAsAPSInt();
10461039
const char *Var = IntelFPGALoopCountAvg->isMax()
10471040
? "llvm.loop.intel.loopcount_max"
10481041
: IntelFPGALoopCountAvg->isMin()
10491042
? "llvm.loop.intel.loopcount_min"
10501043
: "llvm.loop.intel.loopcount_avg";
1051-
setSYCLIntelFPGAVariantCount(Var, Count);
1044+
setSYCLIntelFPGAVariantCount(Var, ArgVal.getSExtValue());
10521045
}
10531046

10541047
if (const auto *IntelFPGALoopCoalesce =
10551048
dyn_cast<SYCLIntelFPGALoopCoalesceAttr>(A)) {
1056-
if (auto *LCE = IntelFPGALoopCoalesce->getNExpr())
1057-
setSYCLLoopCoalesceNLevels(
1058-
LCE->getIntegerConstantExpr(Ctx)->getSExtValue());
1059-
else
1049+
if (const auto *LCE = IntelFPGALoopCoalesce->getNExpr()) {
1050+
const auto *CE = cast<ConstantExpr>(LCE);
1051+
llvm::APSInt ArgVal = CE->getResultAsAPSInt();
1052+
setSYCLLoopCoalesceNLevels(ArgVal.getSExtValue());
1053+
} else {
10601054
setSYCLLoopCoalesceEnable();
1055+
}
10611056
}
10621057

10631058
if (isa<SYCLIntelFPGADisableLoopPipeliningAttr>(A))
10641059
setSYCLLoopPipeliningDisable();
10651060

10661061
if (const auto *IntelFPGAMaxInterleaving =
10671062
dyn_cast<SYCLIntelFPGAMaxInterleavingAttr>(A)) {
1068-
setSYCLMaxInterleavingEnable();
1069-
setSYCLMaxInterleavingNInvocations(IntelFPGAMaxInterleaving->getNExpr()
1070-
->getIntegerConstantExpr(Ctx)
1071-
->getSExtValue());
1063+
const auto *CE = cast<ConstantExpr>(IntelFPGAMaxInterleaving->getNExpr());
1064+
llvm::APSInt ArgVal = CE->getResultAsAPSInt();
1065+
setSYCLMaxInterleavingNInvocations(ArgVal.getSExtValue());
10721066
}
10731067

10741068
if (const auto *IntelFPGASpeculatedIterations =
10751069
dyn_cast<SYCLIntelFPGASpeculatedIterationsAttr>(A)) {
1076-
setSYCLSpeculatedIterationsEnable();
1077-
setSYCLSpeculatedIterationsNIterations(
1078-
IntelFPGASpeculatedIterations->getNExpr()
1079-
->getIntegerConstantExpr(Ctx)
1080-
->getSExtValue());
1070+
const auto *CE =
1071+
cast<ConstantExpr>(IntelFPGASpeculatedIterations->getNExpr());
1072+
llvm::APSInt ArgVal = CE->getResultAsAPSInt();
1073+
setSYCLSpeculatedIterationsNIterations(ArgVal.getSExtValue());
10811074
}
10821075

10831076
if (isa<SYCLIntelFPGANofusionAttr>(A))

clang/lib/CodeGen/CGLoopInfo.h

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H
1616

1717
#include "llvm/ADT/ArrayRef.h"
18+
#include "llvm/ADT/Optional.h"
1819
#include "llvm/ADT/SmallVector.h"
1920
#include "llvm/IR/DebugLoc.h"
2021
#include "llvm/IR/Value.h"
@@ -111,11 +112,8 @@ struct LoopAttributes {
111112
/// Value for llvm.loop.ii.count metadata.
112113
unsigned SYCLIInterval;
113114

114-
/// Flag for llvm.loop.max_concurrency.count metadata.
115-
bool SYCLMaxConcurrencyEnable;
116-
117115
/// Value for llvm.loop.max_concurrency.count metadata.
118-
unsigned SYCLMaxConcurrencyNThreads;
116+
llvm::Optional<unsigned> SYCLMaxConcurrencyNThreads;
119117

120118
/// Value for count variant (min/max/avg) and count metadata.
121119
llvm::SmallVector<std::pair<const char *, unsigned int>, 2>
@@ -130,17 +128,11 @@ struct LoopAttributes {
130128
/// Flag for llvm.loop.intel.pipelining.enable, i32 0 metadata.
131129
bool SYCLLoopPipeliningDisable;
132130

133-
/// Flag for llvm.loop.max_interleaving.count metadata.
134-
bool SYCLMaxInterleavingEnable;
135-
136131
/// Value for llvm.loop.max_interleaving.count metadata.
137-
unsigned SYCLMaxInterleavingNInvocations;
138-
139-
/// Flag for llvm.loop.intel.speculated.iterations.count metadata.
140-
bool SYCLSpeculatedIterationsEnable;
132+
llvm::Optional<unsigned> SYCLMaxInterleavingNInvocations;
141133

142134
/// Value for llvm.loop.intel.speculated.iterations.count metadata.
143-
unsigned SYCLSpeculatedIterationsNIterations;
135+
llvm::Optional<unsigned> SYCLSpeculatedIterationsNIterations;
144136

145137
/// llvm.unroll.
146138
unsigned UnrollCount;
@@ -363,12 +355,7 @@ class LoopInfoStack {
363355
/// Set value of an initiation interval for the next loop pushed.
364356
void setSYCLIInterval(unsigned C) { StagedAttrs.SYCLIInterval = C; }
365357

366-
/// Set flag of max_concurrency for the next loop pushed.
367-
void setSYCLMaxConcurrencyEnable() {
368-
StagedAttrs.SYCLMaxConcurrencyEnable = true;
369-
}
370-
371-
/// Set value of threads for the next loop pushed.
358+
/// Set value of max_concurrency for the next loop pushed.
372359
void setSYCLMaxConcurrencyNThreads(unsigned C) {
373360
StagedAttrs.SYCLMaxConcurrencyNThreads = C;
374361
}
@@ -388,22 +375,12 @@ class LoopInfoStack {
388375
StagedAttrs.SYCLLoopPipeliningDisable = true;
389376
}
390377

391-
/// Set flag of max_interleaving for the next loop pushed.
392-
void setSYCLMaxInterleavingEnable() {
393-
StagedAttrs.SYCLMaxInterleavingEnable = true;
394-
}
395-
396378
/// Set value of max interleaved invocations for the next loop pushed.
397379
void setSYCLMaxInterleavingNInvocations(unsigned C) {
398380
StagedAttrs.SYCLMaxInterleavingNInvocations = C;
399381
}
400382

401-
/// Set flag of speculated_iterations for the next loop pushed.
402-
void setSYCLSpeculatedIterationsEnable() {
403-
StagedAttrs.SYCLSpeculatedIterationsEnable = true;
404-
}
405-
406-
/// Set value of concurrent speculated iterations for the next loop pushed.
383+
/// Set value of speculated iterations for the next loop pushed.
407384
void setSYCLSpeculatedIterationsNIterations(unsigned C) {
408385
StagedAttrs.SYCLSpeculatedIterationsNIterations = C;
409386
}

0 commit comments

Comments
 (0)