Skip to content

Commit

Permalink
[SYCL][FPGA] Refactor of statement attributes (#4136)
Browse files Browse the repository at this point in the history
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 <soumi.manna@intel.com>
  • Loading branch information
smanna12 authored Jul 25, 2021
1 parent 155acd1 commit 6ac26ad
Show file tree
Hide file tree
Showing 7 changed files with 470 additions and 193 deletions.
63 changes: 13 additions & 50 deletions clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -2182,17 +2182,25 @@ 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);

bool CheckQualifiedFunctionForTypeId(QualType T, SourceLocation Loc);

Expand Down Expand Up @@ -13533,51 +13541,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
101 changes: 47 additions & 54 deletions clang/lib/CodeGen/CGLoopInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,11 +552,11 @@ MDNode *LoopInfo::createMetadata(
}

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

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

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

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

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

for (auto &VC : Attrs.SYCLIntelFPGAVariantCount) {
for (const auto &VC : Attrs.SYCLIntelFPGAVariantCount) {
Metadata *Vals[] = {MDString::get(Ctx, VC.first),
ConstantAsMetadata::get(ConstantInt::get(
llvm::Type::getInt32Ty(Ctx), VC.second))};
Expand All @@ -622,15 +622,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 +637,12 @@ void LoopAttributes::clear() {
GlobalSYCLIVDepInfo.reset();
ArraySYCLIVDepInfo.clear();
SYCLIInterval = 0;
SYCLMaxConcurrencyEnable = false;
SYCLMaxConcurrencyNThreads = 0;
SYCLMaxConcurrencyNThreads.reset();
SYCLLoopCoalesceEnable = false;
SYCLLoopCoalesceNLevels = 0;
SYCLLoopPipeliningDisable = false;
SYCLMaxInterleavingEnable = false;
SYCLMaxInterleavingNInvocations = 0;
SYCLSpeculatedIterationsEnable = false;
SYCLSpeculatedIterationsNIterations = 0;
SYCLMaxInterleavingNInvocations.reset();
SYCLSpeculatedIterationsNIterations.reset();
SYCLIntelFPGAVariantCount.clear();
UnrollCount = 0;
UnrollAndJamCount = 0;
Expand Down Expand Up @@ -679,14 +673,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 &&
Attrs.SYCLLoopCoalesceEnable == false &&
Attrs.SYCLLoopCoalesceNLevels == 0 &&
Attrs.SYCLLoopPipeliningDisable == false &&
Attrs.SYCLMaxInterleavingEnable == false &&
Attrs.SYCLMaxInterleavingNInvocations == 0 &&
Attrs.SYCLSpeculatedIterationsEnable == false &&
Attrs.SYCLSpeculatedIterationsNIterations == 0 &&
!Attrs.SYCLMaxInterleavingNInvocations &&
!Attrs.SYCLSpeculatedIterationsNIterations &&
Attrs.SYCLIntelFPGAVariantCount.empty() && Attrs.UnrollCount == 0 &&
Attrs.UnrollAndJamCount == 0 && !Attrs.PipelineDisabled &&
Attrs.PipelineInitiationInterval == 0 &&
Expand Down Expand Up @@ -1025,59 +1017,60 @@ 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();
setSYCLMaxConcurrencyNThreads(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 (const 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();
setSYCLMaxInterleavingNInvocations(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();
setSYCLSpeculatedIterationsNIterations(ArgVal.getSExtValue());
}

if (isa<SYCLIntelFPGANofusionAttr>(A))
Expand Down
35 changes: 6 additions & 29 deletions clang/lib/CodeGen/CGLoopInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#define LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H

#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/IR/Value.h"
Expand Down Expand Up @@ -111,11 +112,8 @@ 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;
llvm::Optional<unsigned> 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,11 @@ 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;
llvm::Optional<unsigned> SYCLMaxInterleavingNInvocations;

/// Value for llvm.loop.intel.speculated.iterations.count metadata.
unsigned SYCLSpeculatedIterationsNIterations;
llvm::Optional<unsigned> SYCLSpeculatedIterationsNIterations;

/// llvm.unroll.
unsigned UnrollCount;
Expand Down Expand Up @@ -363,12 +355,7 @@ 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.
/// Set value of max_concurrency for the next loop pushed.
void setSYCLMaxConcurrencyNThreads(unsigned C) {
StagedAttrs.SYCLMaxConcurrencyNThreads = C;
}
Expand All @@ -388,22 +375,12 @@ 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 value of concurrent speculated iterations for the next loop pushed.
/// Set value of speculated iterations for the next loop pushed.
void setSYCLSpeculatedIterationsNIterations(unsigned C) {
StagedAttrs.SYCLSpeculatedIterationsNIterations = C;
}
Expand Down
Loading

0 comments on commit 6ac26ad

Please sign in to comment.