Skip to content

[SYCL][FPGA] Refactor of statement attributes #4136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Jul 25, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 14 additions & 50 deletions clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -2182,17 +2182,26 @@ class Sema final {
SYCLIntelFPGAIVDepAttr *
BuildSYCLIntelFPGAIVDepAttr(const AttributeCommonInfo &CI, Expr *Expr1,
Expr *Expr2);
template <typename FPGALoopAttrT>
FPGALoopAttrT *BuildSYCLIntelFPGALoopAttr(const AttributeCommonInfo &A,
Expr *E = nullptr);

LoopUnrollHintAttr *BuildLoopUnrollHintAttr(const AttributeCommonInfo &A,
Expr *E);
OpenCLUnrollHintAttr *
BuildOpenCLLoopUnrollHintAttr(const AttributeCommonInfo &A, Expr *E);

SYCLIntelFPGALoopCountAttr *
BuildSYCLIntelFPGALoopCount(const AttributeCommonInfo &CI, Expr *E);
BuildSYCLIntelFPGALoopCountAttr(const AttributeCommonInfo &CI, Expr *E);
SYCLIntelFPGAInitiationIntervalAttr *
BuildSYCLIntelFPGAInitiationIntervalAttr(const AttributeCommonInfo &CI,
Expr *E);
SYCLIntelFPGAMaxConcurrencyAttr *
BuildSYCLIntelFPGAMaxConcurrencyAttr(const AttributeCommonInfo &CI, Expr *E);
SYCLIntelFPGAMaxInterleavingAttr *
BuildSYCLIntelFPGAMaxInterleavingAttr(const AttributeCommonInfo &CI, Expr *E);
SYCLIntelFPGASpeculatedIterationsAttr *
BuildSYCLIntelFPGASpeculatedIterationsAttr(const AttributeCommonInfo &CI,
Expr *E);
SYCLIntelFPGALoopCoalesceAttr *
BuildSYCLIntelFPGALoopCoalesceAttr(const AttributeCommonInfo &CI,
Expr *E = nullptr);

bool CheckQualifiedFunctionForTypeId(QualType T, SourceLocation Loc);

Expand Down Expand Up @@ -13527,51 +13536,6 @@ void Sema::AddOneConstantPowerTwoValueAttr(Decl *D,
D->addAttr(::new (Context) AttrType(Context, CI, E));
}

template <typename FPGALoopAttrT>
FPGALoopAttrT *Sema::BuildSYCLIntelFPGALoopAttr(const AttributeCommonInfo &A,
Expr *E) {
if (!E && !(A.getParsedKind() == ParsedAttr::AT_SYCLIntelFPGALoopCoalesce))
return nullptr;

if (E && !E->isInstantiationDependent()) {
Optional<llvm::APSInt> ArgVal = E->getIntegerConstantExpr(getASTContext());

if (!ArgVal) {
Diag(E->getExprLoc(), diag::err_attribute_argument_type)
<< A.getAttrName() << AANT_ArgumentIntegerConstant
<< E->getSourceRange();
return nullptr;
}

int Val = ArgVal->getSExtValue();

if (A.getParsedKind() == ParsedAttr::AT_SYCLIntelFPGAInitiationInterval ||
A.getParsedKind() == ParsedAttr::AT_SYCLIntelFPGALoopCoalesce) {
if (Val <= 0) {
Diag(E->getExprLoc(), diag::err_attribute_requires_positive_integer)
<< A.getAttrName() << /* positive */ 0;
return nullptr;
}
} else if (A.getParsedKind() ==
ParsedAttr::AT_SYCLIntelFPGAMaxConcurrency ||
A.getParsedKind() ==
ParsedAttr::AT_SYCLIntelFPGAMaxInterleaving ||
A.getParsedKind() ==
ParsedAttr::AT_SYCLIntelFPGASpeculatedIterations ||
A.getParsedKind() == ParsedAttr::AT_SYCLIntelFPGALoopCount) {
if (Val < 0) {
Diag(E->getExprLoc(), diag::err_attribute_requires_positive_integer)
<< A.getAttrName() << /* non-negative */ 1;
return nullptr;
}
} else {
llvm_unreachable("unknown sycl fpga loop attr");
}
}

return new (Context) FPGALoopAttrT(Context, A, E);
}

/// RAII object that enters a new expression evaluation context.
class EnterExpressionEvaluationContext {
Sema &Actions;
Expand Down
108 changes: 50 additions & 58 deletions clang/lib/CodeGen/CGLoopInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,11 +552,10 @@ MDNode *LoopInfo::createMetadata(
}

// Setting max_concurrency attribute with number of threads
if (Attrs.SYCLMaxConcurrencyEnable) {
Metadata *Vals[] = {MDString::get(Ctx, "llvm.loop.max_concurrency.count"),
for (auto &MC : Attrs.SYCLMaxConcurrencyNThreads) {
Metadata *Vals[] = {MDString::get(Ctx, MC.first),
ConstantAsMetadata::get(ConstantInt::get(
llvm::Type::getInt32Ty(Ctx),
Attrs.SYCLMaxConcurrencyNThreads))};
llvm::Type::getInt32Ty(Ctx), MC.second))};
LoopProperties.push_back(MDNode::get(Ctx, Vals));
}

Expand All @@ -582,11 +581,10 @@ MDNode *LoopInfo::createMetadata(
LoopProperties.push_back(MDNode::get(Ctx, Vals));
}

if (Attrs.SYCLMaxInterleavingEnable) {
Metadata *Vals[] = {MDString::get(Ctx, "llvm.loop.max_interleaving.count"),
for (auto &MI : Attrs.SYCLMaxInterleavingNInvocations) {
Metadata *Vals[] = {MDString::get(Ctx, MI.first),
ConstantAsMetadata::get(ConstantInt::get(
llvm::Type::getInt32Ty(Ctx),
Attrs.SYCLMaxInterleavingNInvocations))};
llvm::Type::getInt32Ty(Ctx), MI.second))};
LoopProperties.push_back(MDNode::get(Ctx, Vals));
}

Expand All @@ -596,12 +594,10 @@ MDNode *LoopInfo::createMetadata(
LoopProperties.push_back(MDNode::get(Ctx, Vals));
}

if (Attrs.SYCLSpeculatedIterationsEnable) {
Metadata *Vals[] = {
MDString::get(Ctx, "llvm.loop.intel.speculated.iterations.count"),
ConstantAsMetadata::get(
ConstantInt::get(llvm::Type::getInt32Ty(Ctx),
Attrs.SYCLSpeculatedIterationsNIterations))};
for (auto &SI : Attrs.SYCLSpeculatedIterationsNIterations) {
Metadata *Vals[] = {MDString::get(Ctx, SI.first),
ConstantAsMetadata::get(ConstantInt::get(
llvm::Type::getInt32Ty(Ctx), SI.second))};
LoopProperties.push_back(MDNode::get(Ctx, Vals));
}

Expand All @@ -622,15 +618,12 @@ LoopAttributes::LoopAttributes(bool IsParallel)
UnrollAndJamEnable(LoopAttributes::Unspecified),
VectorizePredicateEnable(LoopAttributes::Unspecified), VectorizeWidth(0),
VectorizeScalable(LoopAttributes::Unspecified), InterleaveCount(0),
SYCLIInterval(0), SYCLMaxConcurrencyEnable(false),
SYCLMaxConcurrencyNThreads(0), SYCLLoopCoalesceEnable(false),
SYCLIInterval(0), SYCLLoopCoalesceEnable(false),
SYCLLoopCoalesceNLevels(0), SYCLLoopPipeliningDisable(false),
SYCLMaxInterleavingEnable(false), SYCLMaxInterleavingNInvocations(0),
SYCLSpeculatedIterationsEnable(false),
SYCLSpeculatedIterationsNIterations(0), UnrollCount(0),
UnrollAndJamCount(0), DistributeEnable(LoopAttributes::Unspecified),
PipelineDisabled(false), PipelineInitiationInterval(0),
SYCLNofusionEnable(false), MustProgress(false) {}
UnrollCount(0), UnrollAndJamCount(0),
DistributeEnable(LoopAttributes::Unspecified), PipelineDisabled(false),
PipelineInitiationInterval(0), SYCLNofusionEnable(false),
MustProgress(false) {}

void LoopAttributes::clear() {
IsParallel = false;
Expand All @@ -640,15 +633,12 @@ void LoopAttributes::clear() {
GlobalSYCLIVDepInfo.reset();
ArraySYCLIVDepInfo.clear();
SYCLIInterval = 0;
SYCLMaxConcurrencyEnable = false;
SYCLMaxConcurrencyNThreads = 0;
SYCLMaxConcurrencyNThreads.clear();
SYCLLoopCoalesceEnable = false;
SYCLLoopCoalesceNLevels = 0;
SYCLLoopPipeliningDisable = false;
SYCLMaxInterleavingEnable = false;
SYCLMaxInterleavingNInvocations = 0;
SYCLSpeculatedIterationsEnable = false;
SYCLSpeculatedIterationsNIterations = 0;
SYCLMaxInterleavingNInvocations.clear();
SYCLSpeculatedIterationsNIterations.clear();
SYCLIntelFPGAVariantCount.clear();
UnrollCount = 0;
UnrollAndJamCount = 0;
Expand Down Expand Up @@ -679,14 +669,12 @@ LoopInfo::LoopInfo(BasicBlock *Header, const LoopAttributes &Attrs,
Attrs.VectorizeScalable == LoopAttributes::Unspecified &&
Attrs.InterleaveCount == 0 && !Attrs.GlobalSYCLIVDepInfo.hasValue() &&
Attrs.ArraySYCLIVDepInfo.empty() && Attrs.SYCLIInterval == 0 &&
Attrs.SYCLMaxConcurrencyEnable == false &&
Attrs.SYCLMaxConcurrencyNThreads.empty() &&
Attrs.SYCLLoopCoalesceEnable == false &&
Attrs.SYCLLoopCoalesceNLevels == 0 &&
Attrs.SYCLLoopPipeliningDisable == false &&
Attrs.SYCLMaxInterleavingEnable == false &&
Attrs.SYCLMaxInterleavingNInvocations == 0 &&
Attrs.SYCLSpeculatedIterationsEnable == false &&
Attrs.SYCLSpeculatedIterationsNIterations == 0 &&
Attrs.SYCLMaxInterleavingNInvocations.empty() &&
Attrs.SYCLSpeculatedIterationsNIterations.empty() &&
Attrs.SYCLIntelFPGAVariantCount.empty() && Attrs.UnrollCount == 0 &&
Attrs.UnrollAndJamCount == 0 && !Attrs.PipelineDisabled &&
Attrs.PipelineInitiationInterval == 0 &&
Expand Down Expand Up @@ -1025,59 +1013,63 @@ void LoopInfoStack::push(BasicBlock *Header, clang::ASTContext &Ctx,
IntelFPGAIVDep->getArrayDecl());

if (const auto *IntelFPGAII =
dyn_cast<SYCLIntelFPGAInitiationIntervalAttr>(A))
setSYCLIInterval(IntelFPGAII->getIntervalExpr()
->getIntegerConstantExpr(Ctx)
->getSExtValue());
dyn_cast<SYCLIntelFPGAInitiationIntervalAttr>(A)) {
const auto *CE = cast<ConstantExpr>(IntelFPGAII->getIntervalExpr());
llvm::APSInt ArgVal = CE->getResultAsAPSInt();
setSYCLIInterval(ArgVal.getSExtValue());
}

if (const auto *IntelFPGAMaxConcurrency =
dyn_cast<SYCLIntelFPGAMaxConcurrencyAttr>(A)) {
setSYCLMaxConcurrencyEnable();
setSYCLMaxConcurrencyNThreads(IntelFPGAMaxConcurrency->getNThreadsExpr()
->getIntegerConstantExpr(Ctx)
->getSExtValue());
const auto *CE =
cast<ConstantExpr>(IntelFPGAMaxConcurrency->getNThreadsExpr());
llvm::APSInt ArgVal = CE->getResultAsAPSInt();
const char *Var = "llvm.loop.max_concurrency.count";
setSYCLMaxConcurrencyNThreads(Var, ArgVal.getSExtValue());
}

if (const auto *IntelFPGALoopCountAvg =
dyn_cast<SYCLIntelFPGALoopCountAttr>(A)) {
unsigned int Count = IntelFPGALoopCountAvg->getNTripCount()
->getIntegerConstantExpr(Ctx)
->getSExtValue();
const auto *CE =
cast<ConstantExpr>(IntelFPGALoopCountAvg->getNTripCount());
llvm::APSInt ArgVal = CE->getResultAsAPSInt();
const char *Var = IntelFPGALoopCountAvg->isMax()
? "llvm.loop.intel.loopcount_max"
: IntelFPGALoopCountAvg->isMin()
? "llvm.loop.intel.loopcount_min"
: "llvm.loop.intel.loopcount_avg";
setSYCLIntelFPGAVariantCount(Var, Count);
setSYCLIntelFPGAVariantCount(Var, ArgVal.getSExtValue());
}

if (const auto *IntelFPGALoopCoalesce =
dyn_cast<SYCLIntelFPGALoopCoalesceAttr>(A)) {
if (auto *LCE = IntelFPGALoopCoalesce->getNExpr())
setSYCLLoopCoalesceNLevels(
LCE->getIntegerConstantExpr(Ctx)->getSExtValue());
else
if (auto *LCE = IntelFPGALoopCoalesce->getNExpr()) {
const auto *CE = cast<ConstantExpr>(LCE);
llvm::APSInt ArgVal = CE->getResultAsAPSInt();
setSYCLLoopCoalesceNLevels(ArgVal.getSExtValue());
} else {
setSYCLLoopCoalesceEnable();
}
}

if (isa<SYCLIntelFPGADisableLoopPipeliningAttr>(A))
setSYCLLoopPipeliningDisable();

if (const auto *IntelFPGAMaxInterleaving =
dyn_cast<SYCLIntelFPGAMaxInterleavingAttr>(A)) {
setSYCLMaxInterleavingEnable();
setSYCLMaxInterleavingNInvocations(IntelFPGAMaxInterleaving->getNExpr()
->getIntegerConstantExpr(Ctx)
->getSExtValue());
const auto *CE = cast<ConstantExpr>(IntelFPGAMaxInterleaving->getNExpr());
llvm::APSInt ArgVal = CE->getResultAsAPSInt();
const char *Var = "llvm.loop.max_interleaving.count";
setSYCLMaxInterleavingNInvocations(Var, ArgVal.getSExtValue());
}

if (const auto *IntelFPGASpeculatedIterations =
dyn_cast<SYCLIntelFPGASpeculatedIterationsAttr>(A)) {
setSYCLSpeculatedIterationsEnable();
setSYCLSpeculatedIterationsNIterations(
IntelFPGASpeculatedIterations->getNExpr()
->getIntegerConstantExpr(Ctx)
->getSExtValue());
const auto *CE =
cast<ConstantExpr>(IntelFPGASpeculatedIterations->getNExpr());
llvm::APSInt ArgVal = CE->getResultAsAPSInt();
const char *Var = "llvm.loop.intel.speculated.iterations.count";
setSYCLSpeculatedIterationsNIterations(Var, ArgVal.getSExtValue());
}

if (isa<SYCLIntelFPGANofusionAttr>(A))
Expand Down
59 changes: 20 additions & 39 deletions clang/lib/CodeGen/CGLoopInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,9 @@ struct LoopAttributes {
/// Value for llvm.loop.ii.count metadata.
unsigned SYCLIInterval;

/// Flag for llvm.loop.max_concurrency.count metadata.
bool SYCLMaxConcurrencyEnable;

/// Value for llvm.loop.max_concurrency.count metadata.
unsigned SYCLMaxConcurrencyNThreads;
/// Value for max_concurrency variant and metadata.
llvm::SmallVector<std::pair<const char *, unsigned int>, 2>
SYCLMaxConcurrencyNThreads;

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

/// Flag for llvm.loop.max_interleaving.count metadata.
bool SYCLMaxInterleavingEnable;

/// Value for llvm.loop.max_interleaving.count metadata.
unsigned SYCLMaxInterleavingNInvocations;

/// Flag for llvm.loop.intel.speculated.iterations.count metadata.
bool SYCLSpeculatedIterationsEnable;
/// Value for max_interleaving variant and metadata.
llvm::SmallVector<std::pair<const char *, unsigned int>, 2>
SYCLMaxInterleavingNInvocations;

/// Value for llvm.loop.intel.speculated.iterations.count metadata.
unsigned SYCLSpeculatedIterationsNIterations;
/// Value for speculated.iterations variant and metadata.
llvm::SmallVector<std::pair<const char *, unsigned int>, 2>
SYCLSpeculatedIterationsNIterations;

/// llvm.unroll.
unsigned UnrollCount;
Expand Down Expand Up @@ -363,14 +357,9 @@ class LoopInfoStack {
/// Set value of an initiation interval for the next loop pushed.
void setSYCLIInterval(unsigned C) { StagedAttrs.SYCLIInterval = C; }

/// Set flag of max_concurrency for the next loop pushed.
void setSYCLMaxConcurrencyEnable() {
StagedAttrs.SYCLMaxConcurrencyEnable = true;
}

/// Set value of threads for the next loop pushed.
void setSYCLMaxConcurrencyNThreads(unsigned C) {
StagedAttrs.SYCLMaxConcurrencyNThreads = C;
/// Set variant and value of max_concurrency for the next loop pushed.
void setSYCLMaxConcurrencyNThreads(const char *Var, unsigned int Value) {
StagedAttrs.SYCLMaxConcurrencyNThreads.push_back({Var, Value});
}

/// Set flag of loop_coalesce for the next loop pushed.
Expand All @@ -388,24 +377,16 @@ class LoopInfoStack {
StagedAttrs.SYCLLoopPipeliningDisable = true;
}

/// Set flag of max_interleaving for the next loop pushed.
void setSYCLMaxInterleavingEnable() {
StagedAttrs.SYCLMaxInterleavingEnable = true;
}

/// Set value of max interleaved invocations for the next loop pushed.
void setSYCLMaxInterleavingNInvocations(unsigned C) {
StagedAttrs.SYCLMaxInterleavingNInvocations = C;
}

/// Set flag of speculated_iterations for the next loop pushed.
void setSYCLSpeculatedIterationsEnable() {
StagedAttrs.SYCLSpeculatedIterationsEnable = true;
/// Set variant and value of max interleaved invocations for the next loop
/// pushed.
void setSYCLMaxInterleavingNInvocations(const char *Var, unsigned int Value) {
StagedAttrs.SYCLMaxInterleavingNInvocations.push_back({Var, Value});
}

/// Set value of concurrent speculated iterations for the next loop pushed.
void setSYCLSpeculatedIterationsNIterations(unsigned C) {
StagedAttrs.SYCLSpeculatedIterationsNIterations = C;
/// Set variant and value of speculated iterations for the next loop pushed.
void setSYCLSpeculatedIterationsNIterations(const char *Var,
unsigned int Value) {
StagedAttrs.SYCLSpeculatedIterationsNIterations.push_back({Var, Value});
}

/// Set value of variant and loop count for the next loop pushed.
Expand Down
Loading